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 view.insertNewNode(null, null);
\r
42 var metaTable = this.metaTable = this.node.find('#rng-visualEditor-meta table');
\r
44 this.node.find('.rng-visualEditor-metaAddBtn').click(function() {
\r
45 var newRow = view._addMetaRow('', '');
\r
46 $(newRow.find('td div')[0]).focus();
\r
50 this.metaTable.on('click', '.rng-visualEditor-metaRemoveBtn', function(e) {
\r
51 $(e.target).closest('tr').remove();
\r
55 this.metaTable.on('keydown', '[contenteditable]', function(e) {
\r
56 console.log(e.which);
\r
57 if(e.which === 13) {
\r
58 if($(document.activeElement).hasClass('rng-visualEditor-metaItemKey')) {
\r
59 metaTable.find('.rng-visualEditor-metaItemValue').focus();
\r
61 var input = $('<input>');
\r
62 input.appendTo('body').focus()
\r
63 view.node.find('.rng-visualEditor-metaAddBtn').focus();
\r
72 var observer = new MutationObserver(function(mutations) {
\r
73 mutations.forEach(function(mutation) {
\r
74 _.each(mutation.addedNodes, function(node) {
\r
76 node.parent().find('[wlxml-tag]').each(function() {
\r
79 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
84 var config = { attributes: true, childList: true, characterData: true, subtree: true };
\r
85 observer.observe(this.node.find('#rng-visualEditor-contentWrapper')[0], config);
\r
87 this.gridToggled = false;
89 _createNode: function(wlxmlTag, wlxmlClass) {
\r
90 var toBlock = ['div', 'document', 'section', 'header'];
\r
91 var htmlTag = _.contains(toBlock, wlxmlTag) ? 'div' : 'span';
\r
92 var toret = $('<' + htmlTag + '>');
\r
93 toret.attr('wlxml-tag', wlxmlTag);
\r
95 toret.attr('wlxml-class', wlxmlClass);
\r
98 insertNewNode: function(wlxmlTag, wlxmlClass) {
\r
99 //TODO: Insert inline
\r
100 var anchor = $(window.getSelection().anchorNode);
\r
101 if(anchor[0].nodeType === Node.TEXT_NODE)
\r
102 anchor = anchor.parent();
\r
103 if(anchor.text() === '') {
\r
104 var todel = anchor;
\r
105 anchor = anchor.parent();
\r
108 var newNode = this._createNode(wlxmlTag || anchor.attr('wlxml-tag'), wlxmlClass || anchor.attr('wlxml-class'));
\r
109 anchor.after(newNode);
\r
110 mediator.nodeCreated(newNode);
\r
113 wrapSelectionWithNewNode: function(wlxmlTag, wlxmlClass) {
\r
115 var selection = window.getSelection();
\r
116 if(selection.anchorNode === selection.focusNode && selection.anchorNode.nodeType === Node.TEXT_NODE) {
\r
117 var startOffset = selection.anchorOffset;
\r
118 var endOffset = selection.focusOffset;
\r
119 if(startOffset > endOffset) {
\r
120 var tmp = startOffset;
\r
121 startOffset = endOffset;
\r
124 var node = selection.anchorNode;
\r
125 var prefix = node.data.substr(0, startOffset);
\r
126 var suffix = node.data.substr(endOffset);
\r
127 var core = node.data.substr(startOffset, endOffset - startOffset);
\r
128 var newNode = this._createNode(wlxmlTag, wlxmlClass);
\r
129 newNode.text(core);
\r
130 $(node).replaceWith(newNode);
\r
131 newNode.before(prefix);
\r
132 newNode.after(suffix);
\r
133 mediator.nodeCreated(newNode);
\r
137 getMetaData: function() {
\r
139 this.metaTable.find('tr').each(function() {
\r
141 var inputs = $(this).find('td [contenteditable]');
\r
142 var key = $(inputs[0]).text();
\r
143 var value = $(inputs[1]).text();
\r
144 toret[key] = value;
\r
146 console.log(toret);
\r
149 setMetaData: function(metadata) {
\r
151 this.metaTable.find('tr').remove();
\r
152 _.each(_.keys(metadata), function(key) {
\r
153 view._addMetaRow(key, metadata[key]);
\r
156 setBody: function(HTMLTree) {
\r
157 this.node.find('#rng-visualEditor-content').html(HTMLTree);
\r
159 getBody: function() {
\r
160 return this.node.find('#rng-visualEditor-content').html();
\r
162 _markSelected: function(node) {
\r
163 this.dimNode(node);
\r
165 this.node.find('.rng-current').removeClass('rng-current');
\r
167 node.addClass('rng-current');
\r
169 this.currentNode = node;
\r
170 mediator.nodeSelected(node);
\r
172 selectNode: function(node) {
\r
173 view._markSelected(node);
\r
174 var range = document.createRange();
\r
175 range.selectNodeContents(node[0]);
\r
176 range.collapse(false);
\r
178 var selection = document.getSelection();
\r
179 selection.removeAllRanges()
\r
180 selection.addRange(range);
\r
182 selectNodeById: function(id) {
\r
183 var node = this.node.find('#'+id);
\r
185 this.selectNode(node);
\r
187 highlightNode: function(node) {
\r
188 if(!this.gridToggled) {
\r
189 node.addClass('rng-hover');
\r
190 var label = node.attr('wlxml-tag');
\r
191 if(node.attr('wlxml-class'))
\r
192 label += ' / ' + node.attr('wlxml-class');
\r
193 var tag = $('<div>').addClass('rng-visualEditor-nodeHoverTag').text(label);
\r
197 dimNode: function(node) {
\r
198 if(!this.gridToggled) {
\r
199 node.removeClass('rng-hover');
\r
200 node.find('.rng-visualEditor-nodeHoverTag').remove();
\r
203 highlightNodeById: function(id) {
\r
204 var node = this.node.find('#'+id);
\r
206 this.highlightNode(node);
\r
208 dimNodeById: function(id) {
\r
209 var node = this.node.find('#'+id);
\r
211 this.dimNode(node);
\r
213 selectFirstNode: function() {
\r
214 var firstNodeWithText = this.node.find('[wlxml-tag]').filter(function() {
\r
215 return $(this).clone().children().remove().end().text().trim() !== '';
\r
218 if(firstNodeWithText.length)
\r
219 node = $(firstNodeWithText[0])
\r
221 node = this.node.find('[wlxml-class|="p"]')
\r
223 this.selectNode(node);
\r
225 _addMetaRow: function(key, value) {
\r
226 var newRow = $(sandbox.getTemplate('metaItem')({key: key || '', value: value || ''}));
\r
227 newRow.appendTo(this.metaTable);
\r
230 toggleGrid: function(toggle) {
\r
231 this.node.find('[wlxml-tag]').toggleClass('rng-hover', toggle);
\r
232 this.gridToggled = toggle;
\r
234 toggleTags: function(toggle) {
\r
240 var sideBarView = {
\r
241 node: view.node.find('#rng-visualEditor-sidebar'),
\r
242 setup: function() {
\r
244 this.node.find('#rng-visualEditor-sidebarButtons a').click(function(e) {
\r
245 e.preventDefault();
\r
246 var target = $(e.currentTarget);
\r
247 if(!target.attr('data-content-id'))
\r
249 view.selectTab(target.attr('data-content-id'));
\r
251 view.selectTab('rng-visualEditor-edit');
\r
253 view.node.on('change', '.rng-visualEditor-editPaneNodeForm select', function(e) {
\r
254 var target = $(e.target);
\r
255 var attr = target.attr('id').split('-')[2].split('editPane')[1].substr(0,3) === 'Tag' ? 'tag' : 'class';
\r
256 mediator.getCurrentNode().attr('wlxml-'+attr, target.val());
\r
260 view.node.on('change', '.rng-visualEditor-editPaneSelectionForm select', function(e) {
\r
261 var target = $(e.target);
\r
262 mediator.wrapWithNodeRequest(target.val(), null);
\r
265 view.node.on('click', '.rng-visualEditor-editPaneSurrouding a', function(e) {
\r
266 var target = $(e.target);
\r
267 mediator.nodeDimmedById(target.attr('data-id'));
\r
268 mediator.nodeSelectedById(target.attr('data-id'));
\r
271 view.node.on('mouseenter', '.rng-visualEditor-editPaneSurrouding a', function(e) {
\r
272 var target = $(e.target);
\r
273 mediator.nodeHighlightedById(target.attr('data-id'));
\r
275 view.node.on('mouseleave', '.rng-visualEditor-editPaneSurrouding a', function(e) {
\r
276 var target = $(e.target);
\r
277 mediator.nodeDimmedById(target.attr('data-id'));
\r
280 selectTab: function(id) {
\r
281 this.node.find('.rng-visualEditor-sidebarContentItem').hide();
\r
282 this.node.find('#'+id).show();
\r
283 this.node.find('#rng-visualEditor-sidebarButtons li').removeClass('active');
\r
284 this.node.find('#rng-visualEditor-sidebarButtons li a[data-content-id=' + id + ']').parent().addClass('active');
\r
287 updateEditPane: function(node) {
\r
288 var pane = this.node.find('#rng-visualEditor-edit');
\r
289 pane.html( $(sandbox.getTemplate('editPane')({tag: node.attr('wlxml-tag'), klass: node.attr('wlxml-class')})));
\r
292 repr: node.parent().attr('wlxml-tag') + ' / ' + (node.parent().attr('wlxml-class') || '[[no class]]'),
\r
293 id: node.parent().attr('id')
\r
296 node.children('[wlxml-tag]').each(function() {
\r
297 var child = $(this);
\r
298 children.push({repr: child.attr('wlxml-tag') + ' / ' + (child.attr('wlxml-class') || '[[no class]]'), id: child.attr('id')});
\r
300 var naviTemplate = sandbox.getTemplate('editPaneNavigation')({parent: parent, children: children});
\r
301 pane.find('.rng-visualEditor-editPaneSurrouding > div').html($(naviTemplate));
\r
303 highlightNode: function(id) {
\r
304 var pane = this.node.find('#rng-visualEditor-edit');
\r
305 pane.find('a[data-id="'+id+'"]').addClass('rng-hover');
\r
307 dimNode: function(id) {
\r
308 var pane = this.node.find('#rng-visualEditor-edit');
\r
309 pane.find('a[data-id="' +id+'"]').removeClass('rng-hover');
\r
313 var toolbarView = {
\r
314 node: view.node.find('#rng-visualEditor-toolbar'),
\r
315 setup: function() {
\r
318 view.node.find('button').click(function(e) {
\r
319 var btn = $(e.currentTarget);
\r
320 if(btn.attr('data-btn-type') === 'toggle') {
\r
321 btn.toggleClass('active')
\r
322 mediator.toolbarButtonToggled(btn.attr('data-btn'), btn.hasClass('active'));
\r
324 if(btn.attr('data-btn-type') === 'cmd') {
\r
325 mediator.toolbarButtonCmd(btn.attr('data-btn'));
\r
329 getOption: function(option) {
\r
330 return this.node.find('.rng-visualEditor-toolbarOption[data-option=' + option +']').val();
\r
335 sideBarView.setup();
\r
336 toolbarView.setup();
\r
339 getCurrentNode: function() {
\r
340 return view.currentNode;
\r
342 nodeCreated: function(node) {
\r
343 view.selectNode(node);
\r
345 nodeSelected: function(node) {
\r
346 sideBarView.updateEditPane(node);
\r
348 nodeSelectedById: function(id) {
\r
349 view.selectNodeById(id);
\r
351 nodeHighlightedById: function(id) {
\r
352 view.highlightNodeById(id);
\r
354 nodeDimmedById: function(id) {
\r
355 view.dimNodeById(id);
\r
357 toolbarButtonToggled: function(btn, toggle) {
\r
359 view.toggleGrid(toggle);
\r
361 view.toggleTags(toggle);
\r
363 toolbarButtonCmd: function(btn) {
\r
364 if(btn === 'new-node') {
\r
365 if(window.getSelection().isCollapsed)
\r
366 view.insertNewNode(toolbarView.getOption('newTag-tag'), toolbarView.getOption('newTag-class'));
\r
368 this.wrapWithNodeRequest(toolbarView.getOption('newTag-tag'), toolbarView.getOption('newTag-class'));
\r
374 nodeHovered: function(node) {
\r
375 view.highlightNode(node);
\r
376 sideBarView.highlightNode(node.attr('id'));
\r
378 nodeBlured: function(node) {
\r
379 view.dimNode(node);
\r
380 sideBarView.dimNode(node.attr('id'));
\r
382 wrapWithNodeRequest: function(wlxmlTag, wlxmlClass) {
\r
383 view.wrapSelectionWithNewNode(wlxmlTag, wlxmlClass);
\r
388 var isDirty = false;
\r
389 var wasShownAlready = false;
\r
393 start: function() {
\r
394 sandbox.publish('ready');
\r
396 getView: function() {
\r
399 setDocument: function(xml) {
\r
400 var transformed = transformations.fromXML.getDocumentDescription(xml);
\r
401 view.setBody(transformed.HTMLTree);
\r
402 view.setMetaData(transformed.metadata);
\r
405 getDocument: function() {
\r
406 return transformations.toXML.getXML({HTMLTree: view.getBody(), metadata: view.getMetaData()});
\r
408 isDirty: function() {
\r
411 setDirty: function(dirty) {
\r
414 onShowed: function() {
\r
415 if(!wasShownAlready) {
\r
416 wasShownAlready = true;
\r
417 view.selectFirstNode();
\r