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) { view.highlightNode($(e.target));});
\r
19 this.node.on('mouseout', '[wlxml-tag]', function(e) { view.dimNode($(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);
96 getMetaData: function() {
\r
98 this.metaTable.find('tr').each(function() {
\r
100 var inputs = $(this).find('td [contenteditable]');
\r
101 var key = $(inputs[0]).text();
\r
102 var value = $(inputs[1]).text();
\r
103 toret[key] = value;
\r
105 console.log(toret);
\r
108 setMetaData: function(metadata) {
\r
110 this.metaTable.find('tr').remove();
\r
111 _.each(_.keys(metadata), function(key) {
\r
112 view._addMetaRow(key, metadata[key]);
\r
115 setBody: function(HTMLTree) {
\r
116 this.node.find('#rng-visualEditor-content').html(HTMLTree);
\r
118 getBody: function() {
\r
119 return this.node.find('#rng-visualEditor-content').html();
\r
121 _markSelected: function(node) {
\r
122 this.node.find('.rng-current').removeClass('rng-current');
\r
123 node.addClass('rng-current');
\r
124 this.currentNode = node;
\r
125 mediator.nodeSelected(node);
\r
127 selectNode: function(node) {
\r
128 view._markSelected(node);
\r
129 var range = document.createRange();
\r
130 range.selectNodeContents(node[0]);
\r
131 range.collapse(false);
\r
133 var selection = document.getSelection();
\r
134 selection.removeAllRanges()
\r
135 selection.addRange(range);
\r
137 selectNodeById: function(id) {
\r
138 var node = this.node.find('#'+id);
\r
140 this.selectNode(node);
\r
142 highlightNode: function(node) {
\r
143 node.addClass('rng-hover');
\r
144 mediator.nodeHighlighted(node);
\r
146 dimNode: function(node) {
\r
147 node.removeClass('rng-hover');
\r
148 mediator.nodeDimmed(node);
\r
150 highlightNodeById: function(id) {
\r
151 var node = this.node.find('#'+id);
\r
153 this.highlightNode(node);
\r
155 dimNodeById: function(id) {
\r
156 var node = this.node.find('#'+id);
\r
158 this.dimNode(node);
\r
160 selectFirstNode: function() {
\r
161 var firstNodeWithText = this.node.find('[wlxml-tag]').filter(function() {
\r
162 return $(this).clone().children().remove().end().text().trim() !== '';
\r
165 if(firstNodeWithText.length)
\r
166 node = $(firstNodeWithText[0])
\r
168 node = this.node.find('[wlxml-class|="p"]')
\r
170 this.selectNode(node);
\r
172 _addMetaRow: function(key, value) {
\r
173 var newRow = $(sandbox.getTemplate('metaItem')({key: key || '', value: value || ''}));
\r
174 newRow.appendTo(this.metaTable);
\r
180 var sideBarView = {
\r
181 node: view.node.find('#rng-visualEditor-sidebar'),
\r
182 setup: function() {
\r
184 this.node.find('#rng-visualEditor-sidebarButtons a').click(function(e) {
\r
185 e.preventDefault();
\r
186 var target = $(e.currentTarget);
\r
187 if(!target.attr('data-content-id'))
\r
189 view.selectTab(target.attr('data-content-id'));
\r
191 view.selectTab('rng-visualEditor-edit');
\r
193 view.node.on('change', '.rng-visualEditor-editPaneNodeForm select', function(e) {
\r
194 var target = $(e.target);
\r
195 var attr = target.attr('id').split('-')[2].split('editPane')[1].substr(0,3) === 'Tag' ? 'tag' : 'class';
\r
196 mediator.getCurrentNode().attr('wlxml-'+attr, target.val());
\r
200 view.node.on('change', '.rng-visualEditor-editPaneSelectionForm select', function(e) {
\r
201 var target = $(e.target);
\r
202 var selection = window.getSelection();
\r
203 if(selection.anchorNode === selection.focusNode && selection.anchorNode.nodeType === Node.TEXT_NODE) {
\r
204 var startOffset = selection.anchorOffset;
\r
205 var endOffset = selection.focusOffset;
\r
206 if(startOffset > endOffset) {
\r
207 var tmp = startOffset;
\r
208 startOffset = endOffset;
\r
211 var node = selection.anchorNode;
\r
212 var prefix = node.data.substr(0, startOffset);
\r
213 var suffix = node.data.substr(endOffset);
\r
214 var core = node.data.substr(startOffset, endOffset - startOffset);
\r
215 var newNode = $('<span wlxml-tag="' + target.val() + '">' + core + '</span>');
\r
216 $(node).replaceWith(newNode);
\r
217 newNode.before(prefix);
\r
218 newNode.after(suffix);
\r
219 mediator.nodeCreated(newNode);
\r
224 view.node.on('click', '.rng-visualEditor-editPaneSurrouding a', function(e) {
\r
225 var target = $(e.target);
\r
226 mediator.nodeDimmedById(target.attr('data-id'));
\r
227 mediator.nodeSelectedById(target.attr('data-id'));
\r
230 view.node.on('mouseenter', '.rng-visualEditor-editPaneSurrouding a', function(e) {
\r
231 var target = $(e.target);
\r
232 mediator.nodeHighlightedById(target.attr('data-id'));
\r
234 view.node.on('mouseleave', '.rng-visualEditor-editPaneSurrouding a', function(e) {
\r
235 var target = $(e.target);
\r
236 mediator.nodeDimmedById(target.attr('data-id'));
\r
239 selectTab: function(id) {
\r
240 this.node.find('.rng-visualEditor-sidebarContentItem').hide();
\r
241 this.node.find('#'+id).show();
\r
242 this.node.find('#rng-visualEditor-sidebarButtons li').removeClass('active');
\r
243 this.node.find('#rng-visualEditor-sidebarButtons li a[data-content-id=' + id + ']').parent().addClass('active');
\r
246 updateEditPane: function(node) {
\r
247 var pane = this.node.find('#rng-visualEditor-edit');
\r
248 pane.html( $(sandbox.getTemplate('editPane')({tag: node.attr('wlxml-tag'), klass: node.attr('wlxml-class')})));
\r
251 repr: node.parent().attr('wlxml-tag') + ' / ' + (node.parent().attr('wlxml-class') || '[[no class]]'),
\r
252 id: node.parent().attr('id')
\r
255 node.children().each(function() {
\r
256 var child = $(this);
\r
257 children.push({repr: child.attr('wlxml-tag') + ' / ' + (child.attr('wlxml-class') || '[[no class]]'), id: child.attr('id')});
\r
259 var naviTemplate = sandbox.getTemplate('editPaneNavigation')({parent: parent, children: children});
\r
260 pane.find('.rng-visualEditor-editPaneSurrouding > div').html($(naviTemplate));
\r
262 highlightNode: function(id) {
\r
263 var pane = this.node.find('#rng-visualEditor-edit');
\r
264 pane.find('a[data-id="'+id+'"]').addClass('rng-hover');
\r
266 dimNode: function(id) {
\r
267 var pane = this.node.find('#rng-visualEditor-edit');
\r
268 pane.find('a[data-id="' +id+'"]').removeClass('rng-hover');
\r
273 sideBarView.setup();
\r
276 getCurrentNode: function() {
\r
277 return view.currentNode;
\r
279 nodeCreated: function(node) {
\r
280 view.selectNode(node);
\r
283 nodeSelected: function(node) {
\r
284 sideBarView.updateEditPane(node);
\r
286 nodeSelectedById: function(id) {
\r
287 view.selectNodeById(id);
\r
289 nodeHighlightedById: function(id) {
\r
290 view.highlightNodeById(id);
\r
292 nodeDimmedById: function(id) {
\r
293 view.dimNodeById(id);
\r
295 nodeHighlighted: function(node) {
\r
296 sideBarView.highlightNode(node.attr('id'));
\r
298 nodeDimmed: function(node) {
\r
299 sideBarView.dimNode(node.attr('id'));
\r
303 var isDirty = false;
\r
304 var wasShownAlready = false;
\r
308 start: function() {
\r
309 sandbox.publish('ready');
\r
311 getView: function() {
\r
314 setDocument: function(xml) {
\r
315 var transformed = transformations.fromXML.getDocumentDescription(xml);
\r
316 view.setBody(transformed.HTMLTree);
\r
317 view.setMetaData(transformed.metadata);
\r
320 getDocument: function() {
\r
321 return transformations.toXML.getXML({HTMLTree: view.getBody(), metadata: view.getMetaData()});
\r
323 isDirty: function() {
\r
326 setDirty: function(dirty) {
\r
329 onShowed: function() {
\r
330 if(!wasShownAlready) {
\r
331 wasShownAlready = true;
\r
332 view.selectFirstNode();
\r