1 rng.modules.visualEditor = function(sandbox) {
\r
2 var transformations = rng.modules.visualEditor.transformations;
\r
5 node: $(sandbox.getTemplate('main')()),
\r
10 this.node.find('#rng-visualEditor-content').on('keyup', function() {
\r
14 this.node.find('#rng-visualEditor-meta').on('keyup', function() {
\r
18 this.node.on('mouseover', '[wlxml-tag]', function(e) { mediator.nodeHovered($(e.target));});
\r
19 this.node.on('mouseout', '[wlxml-tag]', function(e) { mediator.nodeBlured($(e.target));});
\r
20 this.node.on('click', '[wlxml-tag]', function(e) {
\r
21 console.log('clicked node type: '+e.target.nodeType);
\r
22 view._markSelected($(e.target));
\r
25 this.node.on('keyup', '#rng-visualEditor-contentWrapper', function(e) {
\r
26 var anchor = $(window.getSelection().anchorNode);
\r
27 if(anchor[0].nodeType === Node.TEXT_NODE)
\r
28 anchor = anchor.parent();
\r
29 if(!anchor.is('[wlxml-tag]'))
\r
31 view._markSelected(anchor);
\r
34 this.node.on('keydown', '#rng-visualEditor-contentWrapper', function(e) {
\r
35 if(e.which === 13) {
\r
37 var anchor = $(window.getSelection().anchorNode);
\r
38 if(anchor[0].nodeType === Node.TEXT_NODE)
\r
39 anchor = anchor.parent();
\r
40 var newNode = anchor.clone().empty();
\r
41 newNode.attr('id', '');
\r
42 anchor.after(newNode);
\r
43 view.selectNode(newNode);
\r
48 var metaTable = this.metaTable = this.node.find('#rng-visualEditor-meta table');
\r
50 this.node.find('.rng-visualEditor-metaAddBtn').click(function() {
\r
51 var newRow = view._addMetaRow('', '');
\r
52 $(newRow.find('td div')[0]).focus();
\r
56 this.metaTable.on('click', '.rng-visualEditor-metaRemoveBtn', function(e) {
\r
57 $(e.target).closest('tr').remove();
\r
61 this.metaTable.on('keydown', '[contenteditable]', function(e) {
\r
62 console.log(e.which);
\r
63 if(e.which === 13) {
\r
64 if($(document.activeElement).hasClass('rng-visualEditor-metaItemKey')) {
\r
65 metaTable.find('.rng-visualEditor-metaItemValue').focus();
\r
67 var input = $('<input>');
\r
68 input.appendTo('body').focus()
\r
69 view.node.find('.rng-visualEditor-metaAddBtn').focus();
\r
78 var observer = new MutationObserver(function(mutations) {
\r
79 mutations.forEach(function(mutation) {
\r
80 if(mutation.addedNodes.length > 0) {
\r
81 console.log(mutation.addedNodes);
\r
83 _.each(mutation.addedNodes, function(node) {
\r
85 node.parent().find('[wlxml-tag]').each(function() {
\r
88 tag.attr('id', 'xxxxxxxx-xxxx-xxxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {var r = Math.random()*16|0,v=c=='x'?r:r&0x3|0x8;return v.toString(16);}));
\r
93 var config = { attributes: true, childList: true, characterData: true, subtree: true };
\r
94 observer.observe(this.node.find('#rng-visualEditor-contentWrapper')[0], config);
\r
96 this.gridToggled = false;
98 getMetaData: function() {
\r
100 this.metaTable.find('tr').each(function() {
\r
102 var inputs = $(this).find('td [contenteditable]');
\r
103 var key = $(inputs[0]).text();
\r
104 var value = $(inputs[1]).text();
\r
105 toret[key] = value;
\r
107 console.log(toret);
\r
110 setMetaData: function(metadata) {
\r
112 this.metaTable.find('tr').remove();
\r
113 _.each(_.keys(metadata), function(key) {
\r
114 view._addMetaRow(key, metadata[key]);
\r
117 setBody: function(HTMLTree) {
\r
118 this.node.find('#rng-visualEditor-content').html(HTMLTree);
\r
120 getBody: function() {
\r
121 return this.node.find('#rng-visualEditor-content').html();
\r
123 _markSelected: function(node) {
\r
124 this.dimNode(node);
\r
125 this.node.find('.rng-current').removeClass('rng-current');
\r
126 node.addClass('rng-current');
\r
127 this.currentNode = node;
\r
128 mediator.nodeSelected(node);
\r
130 selectNode: function(node) {
\r
131 view._markSelected(node);
\r
132 var range = document.createRange();
\r
133 range.selectNodeContents(node[0]);
\r
134 range.collapse(false);
\r
136 var selection = document.getSelection();
\r
137 selection.removeAllRanges()
\r
138 selection.addRange(range);
\r
140 selectNodeById: function(id) {
\r
141 var node = this.node.find('#'+id);
\r
143 this.selectNode(node);
\r
145 highlightNode: function(node) {
\r
146 if(!this.gridToggled) {
\r
147 node.addClass('rng-hover');
\r
148 var label = node.attr('wlxml-tag');
\r
149 if(node.attr('wlxml-class'))
\r
150 label += ' / ' + node.attr('wlxml-class');
\r
151 var tag = $('<div>').addClass('rng-visualEditor-nodeHoverTag').text(label);
\r
155 dimNode: function(node) {
\r
156 if(!this.gridToggled) {
\r
157 node.removeClass('rng-hover');
\r
158 node.find('.rng-visualEditor-nodeHoverTag').remove();
\r
161 highlightNodeById: function(id) {
\r
162 var node = this.node.find('#'+id);
\r
164 this.highlightNode(node);
\r
166 dimNodeById: function(id) {
\r
167 var node = this.node.find('#'+id);
\r
169 this.dimNode(node);
\r
171 selectFirstNode: function() {
\r
172 var firstNodeWithText = this.node.find('[wlxml-tag]').filter(function() {
\r
173 return $(this).clone().children().remove().end().text().trim() !== '';
\r
176 if(firstNodeWithText.length)
\r
177 node = $(firstNodeWithText[0])
\r
179 node = this.node.find('[wlxml-class|="p"]')
\r
181 this.selectNode(node);
\r
183 _addMetaRow: function(key, value) {
\r
184 var newRow = $(sandbox.getTemplate('metaItem')({key: key || '', value: value || ''}));
\r
185 newRow.appendTo(this.metaTable);
\r
188 toggleGrid: function(toggle) {
\r
189 this.node.find('[wlxml-tag]').toggleClass('rng-hover', toggle);
\r
190 this.gridToggled = toggle;
\r
192 toggleTags: function(toggle) {
\r
198 var sideBarView = {
\r
199 node: view.node.find('#rng-visualEditor-sidebar'),
\r
200 setup: function() {
\r
202 this.node.find('#rng-visualEditor-sidebarButtons a').click(function(e) {
\r
203 e.preventDefault();
\r
204 var target = $(e.currentTarget);
\r
205 if(!target.attr('data-content-id'))
\r
207 view.selectTab(target.attr('data-content-id'));
\r
209 view.selectTab('rng-visualEditor-edit');
\r
211 view.node.on('change', '.rng-visualEditor-editPaneNodeForm select', function(e) {
\r
212 var target = $(e.target);
\r
213 var attr = target.attr('id').split('-')[2].split('editPane')[1].substr(0,3) === 'Tag' ? 'tag' : 'class';
\r
214 mediator.getCurrentNode().attr('wlxml-'+attr, target.val());
\r
218 view.node.on('change', '.rng-visualEditor-editPaneSelectionForm select', function(e) {
\r
219 var target = $(e.target);
\r
220 var selection = window.getSelection();
\r
221 if(selection.anchorNode === selection.focusNode && selection.anchorNode.nodeType === Node.TEXT_NODE) {
\r
222 var startOffset = selection.anchorOffset;
\r
223 var endOffset = selection.focusOffset;
\r
224 if(startOffset > endOffset) {
\r
225 var tmp = startOffset;
\r
226 startOffset = endOffset;
\r
229 var node = selection.anchorNode;
\r
230 var prefix = node.data.substr(0, startOffset);
\r
231 var suffix = node.data.substr(endOffset);
\r
232 var core = node.data.substr(startOffset, endOffset - startOffset);
\r
233 var newNode = $('<span wlxml-tag="' + target.val() + '">' + core + '</span>');
\r
234 $(node).replaceWith(newNode);
\r
235 newNode.before(prefix);
\r
236 newNode.after(suffix);
\r
237 mediator.nodeCreated(newNode);
\r
242 view.node.on('click', '.rng-visualEditor-editPaneSurrouding a', function(e) {
\r
243 var target = $(e.target);
\r
244 mediator.nodeDimmedById(target.attr('data-id'));
\r
245 mediator.nodeSelectedById(target.attr('data-id'));
\r
248 view.node.on('mouseenter', '.rng-visualEditor-editPaneSurrouding a', function(e) {
\r
249 var target = $(e.target);
\r
250 mediator.nodeHighlightedById(target.attr('data-id'));
\r
252 view.node.on('mouseleave', '.rng-visualEditor-editPaneSurrouding a', function(e) {
\r
253 var target = $(e.target);
\r
254 mediator.nodeDimmedById(target.attr('data-id'));
\r
257 selectTab: function(id) {
\r
258 this.node.find('.rng-visualEditor-sidebarContentItem').hide();
\r
259 this.node.find('#'+id).show();
\r
260 this.node.find('#rng-visualEditor-sidebarButtons li').removeClass('active');
\r
261 this.node.find('#rng-visualEditor-sidebarButtons li a[data-content-id=' + id + ']').parent().addClass('active');
\r
264 updateEditPane: function(node) {
\r
265 var pane = this.node.find('#rng-visualEditor-edit');
\r
266 pane.html( $(sandbox.getTemplate('editPane')({tag: node.attr('wlxml-tag'), klass: node.attr('wlxml-class')})));
\r
269 repr: node.parent().attr('wlxml-tag') + ' / ' + (node.parent().attr('wlxml-class') || '[[no class]]'),
\r
270 id: node.parent().attr('id')
\r
273 node.children('[wlxml-tag]').each(function() {
\r
274 var child = $(this);
\r
275 children.push({repr: child.attr('wlxml-tag') + ' / ' + (child.attr('wlxml-class') || '[[no class]]'), id: child.attr('id')});
\r
277 var naviTemplate = sandbox.getTemplate('editPaneNavigation')({parent: parent, children: children});
\r
278 pane.find('.rng-visualEditor-editPaneSurrouding > div').html($(naviTemplate));
\r
280 highlightNode: function(id) {
\r
281 var pane = this.node.find('#rng-visualEditor-edit');
\r
282 pane.find('a[data-id="'+id+'"]').addClass('rng-hover');
\r
284 dimNode: function(id) {
\r
285 var pane = this.node.find('#rng-visualEditor-edit');
\r
286 pane.find('a[data-id="' +id+'"]').removeClass('rng-hover');
\r
290 var toolbarView = {
\r
291 node: view.node.find('#rng-visualEditor-toolbar'),
\r
292 setup: function() {
\r
295 view.node.find('button').click(function(e) {
\r
296 var btn = $(e.currentTarget);
\r
297 if(btn.attr('data-btn-type') === 'toggle') {
\r
298 btn.toggleClass('active')
\r
299 mediator.toolbarButtonToggled(btn.attr('data-btn'), btn.hasClass('active'));
\r
306 sideBarView.setup();
\r
307 toolbarView.setup();
\r
310 getCurrentNode: function() {
\r
311 return view.currentNode;
\r
313 nodeCreated: function(node) {
\r
314 view.selectNode(node);
\r
317 nodeSelected: function(node) {
\r
318 sideBarView.updateEditPane(node);
\r
320 nodeSelectedById: function(id) {
\r
321 view.selectNodeById(id);
\r
323 nodeHighlightedById: function(id) {
\r
324 view.highlightNodeById(id);
\r
326 nodeDimmedById: function(id) {
\r
327 view.dimNodeById(id);
\r
329 toolbarButtonToggled: function(btn, toggle) {
\r
331 view.toggleGrid(toggle);
\r
333 view.toggleTags(toggle);
\r
335 nodeHovered: function(node) {
\r
336 view.highlightNode(node);
\r
337 sideBarView.highlightNode(node.attr('id'));
\r
339 nodeBlured: function(node) {
\r
340 view.dimNode(node);
\r
341 sideBarView.dimNode(node.attr('id'));
\r
346 var isDirty = false;
\r
347 var wasShownAlready = false;
\r
351 start: function() {
\r
352 sandbox.publish('ready');
\r
354 getView: function() {
\r
357 setDocument: function(xml) {
\r
358 var transformed = transformations.fromXML.getDocumentDescription(xml);
\r
359 view.setBody(transformed.HTMLTree);
\r
360 view.setMetaData(transformed.metadata);
\r
363 getDocument: function() {
\r
364 return transformations.toXML.getXML({HTMLTree: view.getBody(), metadata: view.getMetaData()});
\r
366 isDirty: function() {
\r
369 setDirty: function(dirty) {
\r
372 onShowed: function() {
\r
373 if(!wasShownAlready) {
\r
374 wasShownAlready = true;
\r
375 view.selectFirstNode();
\r