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 var target = $(e.currentTarget);
\r
282 if(!target.attr('data-content-id'))
\r
284 view.selectTab(target.attr('data-content-id'));
\r
286 view.selectTab('rng-visualEditor-edit');
\r
288 view.node.on('change', '.rng-visualEditor-editPaneNodeForm select', function(e) {
\r
289 var target = $(e.target);
\r
290 var attr = target.attr('id').split('-')[2].split('editPane')[1].substr(0,3) === 'Tag' ? 'tag' : 'class';
\r
291 mediator.getCurrentNode().attr('wlxml-'+attr, target.val());
\r
295 view.node.on('click', '.rng-visualEditor-editPaneSurrouding a', function(e) {
\r
296 var target = $(e.target);
\r
297 mediator.nodeDimmedById(target.attr('data-id'));
\r
298 mediator.nodeSelectedById(target.attr('data-id'));
\r
301 view.node.on('mouseenter', '.rng-visualEditor-editPaneSurrouding a', function(e) {
\r
302 var target = $(e.target);
\r
303 mediator.nodeHighlightedById(target.attr('data-id'));
\r
305 view.node.on('mouseleave', '.rng-visualEditor-editPaneSurrouding a', function(e) {
\r
306 var target = $(e.target);
\r
307 mediator.nodeDimmedById(target.attr('data-id'));
\r
310 selectTab: function(id) {
\r
311 this.node.find('.rng-visualEditor-sidebarContentItem').hide();
\r
312 this.node.find('#'+id).show();
\r
313 this.node.find('#rng-visualEditor-sidebarButtons li').removeClass('active');
\r
314 this.node.find('#rng-visualEditor-sidebarButtons li a[data-content-id=' + id + ']').parent().addClass('active');
\r
317 updateEditPane: function(node) {
\r
318 var pane = this.node.find('#rng-visualEditor-edit');
\r
319 var parentClass = node.parent().attr('wlxml-class');
\r
320 pane.html( $(sandbox.getTemplate('editPane')({tag: node.attr('wlxml-tag'), klass: node.attr('wlxml-class')})));
\r
322 var parent = node.parent('[wlxml-tag]').length ? {
\r
323 repr: node.parent().attr('wlxml-tag') + (parentClass ? ' / ' + parentClass : ''),
\r
324 id: node.parent().attr('id')
\r
327 node.children('[wlxml-tag]').each(function() {
\r
328 var child = $(this);
\r
329 var childClass = child.attr('wlxml-class');
\r
330 children.push({repr: child.attr('wlxml-tag') + (childClass ? ' / ' + childClass : ''), id: child.attr('id')});
\r
332 var naviTemplate = sandbox.getTemplate('editPaneNavigation')({parent: parent, children: children});
\r
333 pane.find('.rng-visualEditor-editPaneSurrouding > div').html($(naviTemplate));
\r
335 highlightNode: function(id) {
\r
336 var pane = this.node.find('#rng-visualEditor-edit');
\r
337 pane.find('a[data-id="'+id+'"]').addClass('rng-hover');
\r
339 dimNode: function(id) {
\r
340 var pane = this.node.find('#rng-visualEditor-edit');
\r
341 pane.find('a[data-id="' +id+'"]').removeClass('rng-hover');
\r
345 var toolbarView = {
\r
346 node: view.node.find('#rng-visualEditor-toolbar'),
\r
347 setup: function() {
\r
350 view.node.find('button').click(function(e) {
\r
351 var btn = $(e.currentTarget);
\r
352 if(btn.attr('data-btn-type') === 'toggle') {
\r
353 btn.toggleClass('active')
\r
354 mediator.toolbarButtonToggled(btn.attr('data-btn'), btn.hasClass('active'));
\r
356 if(btn.attr('data-btn-type') === 'cmd') {
\r
357 mediator.toolbarButtonCmd(btn.attr('data-btn'), btn.attr('data-meta'));
\r
361 getOption: function(option) {
\r
362 return this.node.find('.rng-visualEditor-toolbarOption[data-option=' + option +']').val();
\r
366 var statusBarView = {
\r
367 node: view.node.find('#rng-visualEditor-statusbar'),
\r
368 setup: function() {
\r
370 view.node.on('mouseenter', 'a', function(e) {
\r
371 var target = $(e.target);
\r
372 mediator.nodeHighlightedById(target.attr('data-id'));
\r
374 view.node.on('mouseleave', 'a', function(e) {
\r
375 var target = $(e.target);
\r
376 mediator.nodeDimmedById(target.attr('data-id'));
\r
378 view.node.on('click', 'a', function(e) {
\r
379 e.preventDefault();
\r
380 mediator.nodeSelectedById($(e.target).attr('data-id'));
\r
384 showNode: function(node) {
\r
386 this.node.html(sandbox.getTemplate('statusBarNodeDisplay')({node: node, parents: node.parents('[wlxml-tag]')}));
\r
387 //node.parents('[wlxml-tag]')
\r
390 highlightNode: function(id) {
\r
391 this.node.find('a[data-id="'+id+'"]').addClass('rng-hover');
\r
393 dimNode: function(id) {
\r
394 this.node.find('a[data-id="' +id+'"]').removeClass('rng-hover');
\r
399 sideBarView.setup();
\r
400 toolbarView.setup();
\r
401 statusBarView.setup();
\r
404 getCurrentNode: function() {
\r
405 return view.currentNode;
\r
407 nodeCreated: function(node) {
\r
408 view.selectNode(node);
\r
410 nodeSelected: function(node) {
\r
411 sideBarView.updateEditPane(node);
\r
412 statusBarView.showNode(node);
\r
414 nodeSelectedById: function(id) {
\r
415 view.selectNodeById(id);
\r
417 nodeHighlightedById: function(id) {
\r
418 view.highlightNodeById(id);
\r
420 nodeDimmedById: function(id) {
\r
421 view.dimNodeById(id);
\r
423 toolbarButtonToggled: function(btn, toggle) {
\r
425 view.toggleGrid(toggle);
\r
427 view.toggleTags(toggle);
\r
429 toolbarButtonCmd: function(btn, meta) {
\r
430 if(btn === 'new-node') {
\r
431 var wlxmlTag = toolbarView.getOption('newTag-tag');
\r
432 var wlxmlClass = toolbarView.getOption('newTag-class');
\r
434 var split = meta.split('/');
\r
435 wlxmlTag = split[0];
\r
436 wlxmlClass = split[1];
\r
438 if(window.getSelection().isCollapsed)
\r
439 view.insertNewNode(wlxmlTag, wlxmlClass);
\r
441 this.wrapWithNodeRequest(wlxmlTag, wlxmlClass);
\r
447 nodeHovered: function(node) {
\r
448 view.highlightNode(node);
\r
449 sideBarView.highlightNode(node.attr('id'));
\r
450 statusBarView.highlightNode(node.attr('id'));
\r
452 nodeBlured: function(node) {
\r
453 view.dimNode(node);
\r
454 sideBarView.dimNode(node.attr('id'));
\r
455 statusBarView.dimNode(node.attr('id'));
\r
457 wrapWithNodeRequest: function(wlxmlTag, wlxmlClass) {
\r
458 view.wrapSelectionWithNewNode(wlxmlTag, wlxmlClass);
\r
463 var isDirty = false;
\r
464 var wasShownAlready = false;
\r
468 start: function() {
\r
469 sandbox.publish('ready');
\r
471 getView: function() {
\r
474 setDocument: function(xml) {
\r
475 var transformed = transformations.fromXML.getDocumentDescription(xml);
\r
476 view.setBody(transformed.HTMLTree);
\r
477 view.setMetaData(transformed.metadata);
\r
480 getDocument: function() {
\r
481 return transformations.toXML.getXML({HTMLTree: view.getBody(), metadata: view.getMetaData()});
\r
483 isDirty: function() {
\r
486 setDirty: function(dirty) {
\r
489 onShowed: function() {
\r
490 if(!wasShownAlready) {
\r
491 wasShownAlready = true;
\r
492 view.selectFirstNode();
\r