1 define(['./visualEditor.transformations'], function(transformations) {
\r
3 return function(sandbox) {
\r
6 node: $(sandbox.getTemplate('main')()),
\r
11 this.node.find('#rng-visualEditor-content').on('keyup', function() {
\r
15 this.node.find('#rng-visualEditor-meta').on('keyup', function() {
\r
19 this.node.on('mouseover', '[wlxml-tag]', function(e) { mediator.nodeHovered($(e.target));});
\r
20 this.node.on('mouseout', '[wlxml-tag]', function(e) { mediator.nodeBlured($(e.target));});
\r
21 this.node.on('click', '[wlxml-tag]', function(e) {
\r
22 console.log('clicked node type: '+e.target.nodeType);
\r
23 view._markSelected($(e.target));
\r
26 this.node.on('keyup', '#rng-visualEditor-contentWrapper', function(e) {
\r
27 var anchor = $(window.getSelection().anchorNode);
\r
28 if(anchor[0].nodeType === Node.TEXT_NODE)
\r
29 anchor = anchor.parent();
\r
30 if(!anchor.is('[wlxml-tag]'))
\r
32 view._markSelected(anchor);
\r
35 this.node.on('keydown', '#rng-visualEditor-contentWrapper', function(e) {
\r
36 if(e.which === 13) {
\r
38 view.insertNewNode(null, null);
\r
43 var metaTable = this.metaTable = this.node.find('#rng-visualEditor-meta table');
\r
45 this.node.find('.rng-visualEditor-metaAddBtn').click(function() {
\r
46 var newRow = view._addMetaRow('', '');
\r
47 $(newRow.find('td div')[0]).focus();
\r
51 this.metaTable.on('click', '.rng-visualEditor-metaRemoveBtn', function(e) {
\r
52 $(e.target).closest('tr').remove();
\r
56 this.metaTable.on('keydown', '[contenteditable]', function(e) {
\r
57 console.log(e.which);
\r
58 if(e.which === 13) {
\r
59 if($(document.activeElement).hasClass('rng-visualEditor-metaItemKey')) {
\r
60 metaTable.find('.rng-visualEditor-metaItemValue').focus();
\r
62 var input = $('<input>');
\r
63 input.appendTo('body').focus()
\r
64 view.node.find('.rng-visualEditor-metaAddBtn').focus();
\r
73 var observer = new MutationObserver(function(mutations) {
\r
74 mutations.forEach(function(mutation) {
\r
75 _.each(mutation.addedNodes, function(node) {
\r
77 node.parent().find('[wlxml-tag]').each(function() {
\r
80 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
85 var config = { attributes: true, childList: true, characterData: true, subtree: true };
\r
86 observer.observe(this.node.find('#rng-visualEditor-contentWrapper')[0], config);
\r
88 this.gridToggled = false;
90 _createNode: function(wlxmlTag, wlxmlClass) {
\r
91 var toBlock = ['div', 'document', 'section', 'header'];
\r
92 var htmlTag = _.contains(toBlock, wlxmlTag) ? 'div' : 'span';
\r
93 var toret = $('<' + htmlTag + '>');
\r
94 toret.attr('wlxml-tag', wlxmlTag);
\r
96 toret.attr('wlxml-class', wlxmlClass);
\r
99 insertNewNode: function(wlxmlTag, wlxmlClass) {
\r
100 //TODO: Insert inline
\r
101 var anchor = $(window.getSelection().anchorNode);
\r
102 var anchorOffset = window.getSelection().anchorOffset;
\r
103 if(anchor[0].nodeType === Node.TEXT_NODE)
\r
104 anchor = anchor.parent();
\r
105 if(anchor.text() === '') {
\r
106 var todel = anchor;
\r
107 anchor = anchor.parent();
\r
110 if(anchorOffset > 0 && anchorOffset < anchor.text().length) {
\r
111 if(wlxmlTag === null && wlxmlClass === null) {
\r
112 return this.splitWithNewNode(anchor);
\r
114 return this.wrapSelectionWithNewNode(wlxmlTag, wlxmlClass);
\r
116 var newNode = this._createNode(wlxmlTag || anchor.attr('wlxml-tag'), wlxmlClass || anchor.attr('wlxml-class'));
\r
117 if(anchorOffset === 0)
\r
118 anchor.before(newNode)
\r
120 anchor.after(newNode);
\r
121 mediator.nodeCreated(newNode);
\r
124 wrapSelectionWithNewNode: function(wlxmlTag, wlxmlClass) {
\r
125 var selection = window.getSelection();
\r
126 if(selection.anchorNode === selection.focusNode && selection.anchorNode.nodeType === Node.TEXT_NODE) {
\r
127 var startOffset = selection.anchorOffset;
\r
128 var endOffset = selection.focusOffset;
\r
129 if(startOffset > endOffset) {
\r
130 var tmp = startOffset;
\r
131 startOffset = endOffset;
\r
134 var node = selection.anchorNode;
\r
135 var prefix = node.data.substr(0, startOffset);
\r
136 var suffix = node.data.substr(endOffset);
\r
137 var core = node.data.substr(startOffset, endOffset - startOffset);
\r
138 var newNode = this._createNode(wlxmlTag, wlxmlClass);
\r
139 newNode.text(core || 'test');
\r
140 $(node).replaceWith(newNode);
\r
141 newNode.before(prefix);
\r
142 newNode.after(suffix);
\r
143 mediator.nodeCreated(newNode);
\r
147 splitWithNewNode: function(node) {
\r
148 var selection = window.getSelection();
\r
149 if(selection.anchorNode === selection.focusNode && selection.anchorNode.nodeType === Node.TEXT_NODE) {
\r
150 var startOffset = selection.anchorOffset;
\r
151 var endOffset = selection.focusOffset;
\r
152 if(startOffset > endOffset) {
\r
153 var tmp = startOffset;
\r
154 startOffset = endOffset;
\r
157 var anchor = selection.anchorNode;
\r
158 var prefix = anchor.data.substr(0, startOffset);
\r
159 var suffix = anchor.data.substr(endOffset);
\r
160 var prefixNode = this._createNode(node.attr('wlxml-tag'), node.attr('wlxml-class'));
\r
161 var newNode = this._createNode(node.attr('wlxml-tag'), node.attr('wlxml-class'));
\r
162 var suffixNode = this._createNode(node.attr('wlxml-tag'), node.attr('wlxml-class'));
\r
163 prefixNode.text(prefix);
\r
164 suffixNode.text(suffix);
\r
165 node.replaceWith(newNode);
\r
166 newNode.before(prefixNode);
\r
167 newNode.after(suffixNode);
\r
168 mediator.nodeCreated(newNode);
\r
172 getMetaData: function() {
\r
174 this.metaTable.find('tr').each(function() {
\r
176 var inputs = $(this).find('td [contenteditable]');
\r
177 var key = $(inputs[0]).text();
\r
178 var value = $(inputs[1]).text();
\r
179 toret[key] = value;
\r
181 console.log(toret);
\r
184 setMetaData: function(metadata) {
\r
186 this.metaTable.find('tr').remove();
\r
187 _.each(_.keys(metadata), function(key) {
\r
188 view._addMetaRow(key, metadata[key]);
\r
191 setBody: function(HTMLTree) {
\r
192 this.node.find('#rng-visualEditor-content').html(HTMLTree);
\r
194 getBody: function() {
\r
195 return this.node.find('#rng-visualEditor-content').html();
\r
197 _markSelected: function(node) {
\r
198 this.dimNode(node);
\r
200 this.node.find('.rng-current').removeClass('rng-current');
\r
202 node.addClass('rng-current');
\r
204 this.currentNode = node;
\r
205 mediator.nodeSelected(node);
\r
207 selectNode: function(node) {
\r
208 view._markSelected(node);
\r
209 var range = document.createRange();
\r
210 range.selectNodeContents(node[0]);
\r
211 range.collapse(false);
\r
213 var selection = document.getSelection();
\r
214 selection.removeAllRanges()
\r
215 selection.addRange(range);
\r
217 selectNodeById: function(id) {
\r
218 var node = this.node.find('#'+id);
\r
220 this.selectNode(node);
\r
222 highlightNode: function(node) {
\r
223 if(!this.gridToggled) {
\r
224 node.addClass('rng-hover');
\r
225 var label = node.attr('wlxml-tag');
\r
226 if(node.attr('wlxml-class'))
\r
227 label += ' / ' + node.attr('wlxml-class');
\r
228 var tag = $('<div>').addClass('rng-visualEditor-nodeHoverTag').text(label);
\r
232 dimNode: function(node) {
\r
233 if(!this.gridToggled) {
\r
234 node.removeClass('rng-hover');
\r
235 node.find('.rng-visualEditor-nodeHoverTag').remove();
\r
238 highlightNodeById: function(id) {
\r
239 var node = this.node.find('#'+id);
\r
241 this.highlightNode(node);
\r
243 dimNodeById: function(id) {
\r
244 var node = this.node.find('#'+id);
\r
246 this.dimNode(node);
\r
248 selectFirstNode: function() {
\r
249 var firstNodeWithText = this.node.find('[wlxml-tag]').filter(function() {
\r
250 return $(this).clone().children().remove().end().text().trim() !== '';
\r
253 if(firstNodeWithText.length)
\r
254 node = $(firstNodeWithText[0])
\r
256 node = this.node.find('[wlxml-class|="p"]')
\r
258 this.selectNode(node);
\r
260 _addMetaRow: function(key, value) {
\r
261 var newRow = $(sandbox.getTemplate('metaItem')({key: key || '', value: value || ''}));
\r
262 newRow.appendTo(this.metaTable);
\r
265 toggleGrid: function(toggle) {
\r
266 this.node.find('[wlxml-tag]').toggleClass('rng-hover', toggle);
\r
267 this.gridToggled = toggle;
\r
269 toggleTags: function(toggle) {
\r
275 var sideBarView = {
\r
276 node: view.node.find('#rng-visualEditor-sidebar'),
\r
277 setup: function() {
\r
279 this.node.find('#rng-visualEditor-sidebarButtons a').click(function(e) {
\r
280 e.preventDefault();
\r
281 e.stopPropagation();
\r
282 var target = $(e.currentTarget);
\r
283 if(!target.attr('data-content-id'))
\r
285 view.selectTab(target.attr('data-content-id'));
\r
287 view.selectTab('rng-visualEditor-edit');
\r
289 view.node.on('change', '.rng-visualEditor-editPaneNodeForm select', function(e) {
\r
290 var target = $(e.target);
\r
291 var attr = target.attr('id').split('-')[2].split('editPane')[1].substr(0,3) === 'Tag' ? 'tag' : 'class';
\r
292 mediator.getCurrentNode().attr('wlxml-'+attr, target.val());
\r
296 view.node.on('click', '.rng-visualEditor-editPaneSurrouding a', function(e) {
\r
297 var target = $(e.target);
\r
298 mediator.nodeDimmedById(target.attr('data-id'));
\r
299 mediator.nodeSelectedById(target.attr('data-id'));
\r
302 view.node.on('mouseenter', '.rng-visualEditor-editPaneSurrouding a', function(e) {
\r
303 var target = $(e.target);
\r
304 mediator.nodeHighlightedById(target.attr('data-id'));
\r
306 view.node.on('mouseleave', '.rng-visualEditor-editPaneSurrouding a', function(e) {
\r
307 var target = $(e.target);
\r
308 mediator.nodeDimmedById(target.attr('data-id'));
\r
311 selectTab: function(id) {
\r
312 this.node.find('.rng-visualEditor-sidebarContentItem').hide();
\r
313 this.node.find('#'+id).show();
\r
314 this.node.find('#rng-visualEditor-sidebarButtons li').removeClass('active');
\r
315 this.node.find('#rng-visualEditor-sidebarButtons li a[data-content-id=' + id + ']').parent().addClass('active');
\r
318 updateEditPane: function(node) {
\r
319 var pane = this.node.find('#rng-visualEditor-edit');
\r
320 var parentClass = node.parent().attr('wlxml-class');
\r
321 pane.html( $(sandbox.getTemplate('editPane')({tag: node.attr('wlxml-tag'), klass: node.attr('wlxml-class')})));
\r
323 var parent = node.parent('[wlxml-tag]').length ? {
\r
324 repr: node.parent().attr('wlxml-tag') + (parentClass ? ' / ' + parentClass : ''),
\r
325 id: node.parent().attr('id')
\r
328 node.children('[wlxml-tag]').each(function() {
\r
329 var child = $(this);
\r
330 var childClass = child.attr('wlxml-class');
\r
331 children.push({repr: child.attr('wlxml-tag') + (childClass ? ' / ' + childClass : ''), id: child.attr('id')});
\r
333 var naviTemplate = sandbox.getTemplate('editPaneNavigation')({parent: parent, children: children});
\r
334 pane.find('.rng-visualEditor-editPaneSurrouding > div').html($(naviTemplate));
\r
336 highlightNode: function(id) {
\r
337 var pane = this.node.find('#rng-visualEditor-edit');
\r
338 pane.find('a[data-id="'+id+'"]').addClass('rng-hover');
\r
340 dimNode: function(id) {
\r
341 var pane = this.node.find('#rng-visualEditor-edit');
\r
342 pane.find('a[data-id="' +id+'"]').removeClass('rng-hover');
\r
346 var toolbarView = {
\r
347 node: view.node.find('#rng-visualEditor-toolbar'),
\r
348 setup: function() {
\r
351 view.node.find('button').click(function(e) {
\r
352 var btn = $(e.currentTarget);
\r
353 if(btn.attr('data-btn-type') === 'toggle') {
\r
354 btn.toggleClass('active')
\r
355 mediator.toolbarButtonToggled(btn.attr('data-btn'), btn.hasClass('active'));
\r
357 if(btn.attr('data-btn-type') === 'cmd') {
\r
358 mediator.toolbarButtonCmd(btn.attr('data-btn'), btn.attr('data-meta'));
\r
362 getOption: function(option) {
\r
363 return this.node.find('.rng-visualEditor-toolbarOption[data-option=' + option +']').val();
\r
367 var statusBarView = {
\r
368 node: view.node.find('#rng-visualEditor-statusbar'),
\r
369 setup: function() {
\r
371 view.node.on('mouseenter', 'a', function(e) {
\r
372 var target = $(e.target);
\r
373 mediator.nodeHighlightedById(target.attr('data-id'));
\r
375 view.node.on('mouseleave', 'a', function(e) {
\r
376 var target = $(e.target);
\r
377 mediator.nodeDimmedById(target.attr('data-id'));
\r
379 view.node.on('click', 'a', function(e) {
\r
380 e.preventDefault();
\r
381 mediator.nodeSelectedById($(e.target).attr('data-id'));
\r
385 showNode: function(node) {
\r
387 this.node.html(sandbox.getTemplate('statusBarNodeDisplay')({node: node, parents: node.parents('[wlxml-tag]')}));
\r
388 //node.parents('[wlxml-tag]')
\r
391 highlightNode: function(id) {
\r
392 this.node.find('a[data-id="'+id+'"]').addClass('rng-hover');
\r
394 dimNode: function(id) {
\r
395 this.node.find('a[data-id="' +id+'"]').removeClass('rng-hover');
\r
400 sideBarView.setup();
\r
401 toolbarView.setup();
\r
402 statusBarView.setup();
\r
405 getCurrentNode: function() {
\r
406 return view.currentNode;
\r
408 nodeCreated: function(node) {
\r
409 view.selectNode(node);
\r
411 nodeSelected: function(node) {
\r
412 sideBarView.updateEditPane(node);
\r
413 statusBarView.showNode(node);
\r
415 nodeSelectedById: function(id) {
\r
416 view.selectNodeById(id);
\r
418 nodeHighlightedById: function(id) {
\r
419 view.highlightNodeById(id);
\r
421 nodeDimmedById: function(id) {
\r
422 view.dimNodeById(id);
\r
424 toolbarButtonToggled: function(btn, toggle) {
\r
426 view.toggleGrid(toggle);
\r
428 view.toggleTags(toggle);
\r
430 toolbarButtonCmd: function(btn, meta) {
\r
431 if(btn === 'new-node') {
\r
432 var wlxmlTag = toolbarView.getOption('newTag-tag');
\r
433 var wlxmlClass = toolbarView.getOption('newTag-class');
\r
435 var split = meta.split('/');
\r
436 wlxmlTag = split[0];
\r
437 wlxmlClass = split[1];
\r
439 if(window.getSelection().isCollapsed)
\r
440 view.insertNewNode(wlxmlTag, wlxmlClass);
\r
442 this.wrapWithNodeRequest(wlxmlTag, wlxmlClass);
\r
448 nodeHovered: function(node) {
\r
449 view.highlightNode(node);
\r
450 sideBarView.highlightNode(node.attr('id'));
\r
451 statusBarView.highlightNode(node.attr('id'));
\r
453 nodeBlured: function(node) {
\r
454 view.dimNode(node);
\r
455 sideBarView.dimNode(node.attr('id'));
\r
456 statusBarView.dimNode(node.attr('id'));
\r
458 wrapWithNodeRequest: function(wlxmlTag, wlxmlClass) {
\r
459 view.wrapSelectionWithNewNode(wlxmlTag, wlxmlClass);
\r
464 var isDirty = false;
\r
465 var wasShownAlready = false;
\r
469 start: function() {
\r
470 sandbox.publish('ready');
\r
472 getView: function() {
\r
475 setDocument: function(xml) {
\r
476 var transformed = transformations.fromXML.getDocumentDescription(xml);
\r
477 view.setBody(transformed.HTMLTree);
\r
478 view.setMetaData(transformed.metadata);
\r
481 getDocument: function() {
\r
482 return transformations.toXML.getXML({HTMLTree: view.getBody(), metadata: view.getMetaData()});
\r
484 isDirty: function() {
\r
487 setDirty: function(dirty) {
\r
490 onShowed: function() {
\r
491 if(!wasShownAlready) {
\r
492 wasShownAlready = true;
\r
493 view.selectFirstNode();
\r