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) { $(e.target).addClass('rng-hover')});
\r
19 this.node.on('mouseout', '[wlxml-tag]', function(e) { $(e.target).removeClass('rng-hover')});
\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 selectFirstNode: function() {
\r
143 var firstNodeWithText = this.node.find('[wlxml-tag]').filter(function() {
\r
144 return $(this).clone().children().remove().end().text().trim() !== '';
\r
147 if(firstNodeWithText.length)
\r
148 node = $(firstNodeWithText[0])
\r
150 node = this.node.find('[wlxml-class|="p"]')
\r
152 this.selectNode(node);
\r
154 _addMetaRow: function(key, value) {
\r
155 var newRow = $(sandbox.getTemplate('metaItem')({key: key || '', value: value || ''}));
\r
156 newRow.appendTo(this.metaTable);
\r
162 var sideBarView = {
\r
163 node: view.node.find('#rng-visualEditor-sidebar'),
\r
164 setup: function() {
\r
166 this.node.find('#rng-visualEditor-sidebarButtons a').click(function(e) {
\r
167 e.preventDefault();
\r
168 var target = $(e.currentTarget);
\r
169 if(!target.attr('data-content-id'))
\r
171 view.selectTab(target.attr('data-content-id'));
\r
173 view.selectTab('rng-visualEditor-edit');
\r
175 view.node.on('change', '.rng-visualEditor-editPaneNodeForm select', function(e) {
\r
176 var target = $(e.target);
\r
177 var attr = target.attr('id').split('-')[2].split('editPane')[1].substr(0,3) === 'Tag' ? 'tag' : 'class';
\r
178 mediator.getCurrentNode().attr('wlxml-'+attr, target.val());
\r
182 view.node.on('change', '.rng-visualEditor-editPaneSelectionForm select', function(e) {
\r
183 var target = $(e.target);
\r
184 var selection = window.getSelection();
\r
185 if(selection.anchorNode === selection.focusNode && selection.anchorNode.nodeType === Node.TEXT_NODE) {
\r
186 var startOffset = selection.anchorOffset;
\r
187 var endOffset = selection.focusOffset;
\r
188 if(startOffset > endOffset) {
\r
189 var tmp = startOffset;
\r
190 startOffset = endOffset;
\r
193 var node = selection.anchorNode;
\r
194 var prefix = node.data.substr(0, startOffset);
\r
195 var suffix = node.data.substr(endOffset);
\r
196 var core = node.data.substr(startOffset, endOffset - startOffset);
\r
197 var newNode = $('<span wlxml-tag="' + target.val() + '">' + core + '</span>');
\r
198 $(node).replaceWith(newNode);
\r
199 newNode.before(prefix);
\r
200 newNode.after(suffix);
\r
201 mediator.nodeCreated(newNode);
\r
206 view.node.on('click', '.rng-visualEditor-editPaneSurrouding a', function(e) {
\r
207 var target = $(e.target);
\r
208 mediator.nodeSelectedById(target.attr('data-id'));
\r
211 selectTab: function(id) {
\r
212 this.node.find('.rng-visualEditor-sidebarContentItem').hide();
\r
213 this.node.find('#'+id).show();
\r
214 this.node.find('#rng-visualEditor-sidebarButtons li').removeClass('active');
\r
215 this.node.find('#rng-visualEditor-sidebarButtons li a[data-content-id=' + id + ']').parent().addClass('active');
\r
218 updateEditPane: function(node) {
\r
219 var pane = this.node.find('#rng-visualEditor-edit');
\r
220 pane.html( $(sandbox.getTemplate('editPane')({tag: node.attr('wlxml-tag'), klass: node.attr('wlxml-class')})));
\r
223 repr: node.parent().attr('wlxml-tag') + ' / ' + (node.parent().attr('wlxml-class') || '[[no class]]'),
\r
224 id: node.parent().attr('id')
\r
227 node.children().each(function() {
\r
228 var child = $(this);
\r
229 children.push({repr: child.attr('wlxml-tag') + ' / ' + (child.attr('wlxml-class') || '[[no class]]'), id: child.attr('id')});
\r
231 var naviTemplate = sandbox.getTemplate('editPaneNavigation')({parent: parent, children: children});
\r
232 pane.find('.rng-visualEditor-editPaneSurrouding > div').html($(naviTemplate));
\r
237 sideBarView.setup();
\r
240 getCurrentNode: function() {
\r
241 return view.currentNode;
\r
243 nodeCreated: function(node) {
\r
244 view.selectNode(node);
\r
247 nodeSelected: function(node) {
\r
248 sideBarView.updateEditPane(node);
\r
250 nodeSelectedById: function(id) {
\r
251 view.selectNodeById(id);
\r
255 var isDirty = false;
\r
256 var wasShownAlready = false;
\r
260 start: function() {
\r
261 sandbox.publish('ready');
\r
263 getView: function() {
\r
266 setDocument: function(xml) {
\r
267 var transformed = transformations.fromXML.getDocumentDescription(xml);
\r
268 view.setBody(transformed.HTMLTree);
\r
269 view.setMetaData(transformed.metadata);
\r
272 getDocument: function() {
\r
273 return transformations.toXML.getXML({HTMLTree: view.getBody(), metadata: view.getMetaData()});
\r
275 isDirty: function() {
\r
278 setDirty: function(dirty) {
\r
281 onShowed: function() {
\r
282 if(!wasShownAlready) {
\r
283 wasShownAlready = true;
\r
284 view.selectFirstNode();
\r