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 anchor.after(newNode);
\r
42 view.selectNode(newNode);
\r
47 var metaTable = this.metaTable = this.node.find('#rng-visualEditor-meta table');
\r
49 this.node.find('.rng-visualEditor-metaAddBtn').click(function() {
\r
50 var newRow = view._addMetaRow('', '');
\r
51 $(newRow.find('td div')[0]).focus();
\r
55 this.metaTable.on('click', '.rng-visualEditor-metaRemoveBtn', function(e) {
\r
56 $(e.target).closest('tr').remove();
\r
60 this.metaTable.on('keydown', '[contenteditable]', function(e) {
\r
61 console.log(e.which);
\r
62 if(e.which === 13) {
\r
63 if($(document.activeElement).hasClass('rng-visualEditor-metaItemKey')) {
\r
64 metaTable.find('.rng-visualEditor-metaItemValue').focus();
\r
66 var input = $('<input>');
\r
67 input.appendTo('body').focus()
\r
68 view.node.find('.rng-visualEditor-metaAddBtn').focus();
\r
77 getMetaData: function() {
\r
79 this.metaTable.find('tr').each(function() {
\r
81 var inputs = $(this).find('td [contenteditable]');
\r
82 var key = $(inputs[0]).text();
\r
83 var value = $(inputs[1]).text();
\r
89 setMetaData: function(metadata) {
\r
91 this.metaTable.find('tr').remove();
\r
92 _.each(_.keys(metadata), function(key) {
\r
93 view._addMetaRow(key, metadata[key]);
\r
96 setBody: function(HTMLTree) {
\r
97 this.node.find('#rng-visualEditor-content').html(HTMLTree);
\r
99 getBody: function() {
\r
100 return this.node.find('#rng-visualEditor-content').html();
\r
102 _markSelected: function(node) {
\r
103 this.node.find('.rng-current').removeClass('rng-current');
\r
104 node.addClass('rng-current');
\r
105 this.currentNode = node;
\r
106 mediator.nodeSelected(node);
\r
108 selectNode: function(node) {
\r
109 view._markSelected(node);
\r
110 var range = document.createRange();
\r
111 range.selectNodeContents(node[0]);
\r
112 range.collapse(false);
\r
114 var selection = document.getSelection();
\r
115 selection.removeAllRanges()
\r
116 selection.addRange(range);
\r
118 selectFirstNode: function() {
\r
119 var firstNodeWithText = this.node.find('[wlxml-tag]').filter(function() {
\r
120 return $(this).clone().children().remove().end().text().trim() !== '';
\r
123 if(firstNodeWithText.length)
\r
124 node = $(firstNodeWithText[0])
\r
126 node = this.node.find('[wlxml-class|="p"]')
\r
128 this.selectNode(node);
\r
130 _addMetaRow: function(key, value) {
\r
131 var newRow = $(sandbox.getTemplate('metaItem')({key: key || '', value: value || ''}));
\r
132 newRow.appendTo(this.metaTable);
\r
138 var sideBarView = {
\r
139 node: view.node.find('#rng-visualEditor-sidebar'),
\r
140 setup: function() {
\r
142 this.node.find('#rng-visualEditor-sidebarButtons a').click(function(e) {
\r
143 e.preventDefault();
\r
144 var target = $(e.currentTarget);
\r
145 if(!target.attr('data-content-id'))
\r
147 view.selectTab(target.attr('data-content-id'));
\r
149 view.selectTab('rng-visualEditor-edit');
\r
151 view.node.on('change', '.rng-visualEditor-editPaneNodeForm select', function(e) {
\r
152 var target = $(e.target);
\r
153 var attr = target.attr('id').split('-')[2].split('editPane')[1].substr(0,3) === 'Tag' ? 'tag' : 'class';
\r
154 mediator.getCurrentNode().attr('wlxml-'+attr, target.val());
\r
158 view.node.on('change', '.rng-visualEditor-editPaneSelectionForm select', function(e) {
\r
159 var target = $(e.target);
\r
160 var selection = window.getSelection();
\r
161 if(selection.anchorNode === selection.focusNode && selection.anchorNode.nodeType === Node.TEXT_NODE) {
\r
162 var startOffset = selection.anchorOffset;
\r
163 var endOffset = selection.focusOffset;
\r
164 if(startOffset > endOffset) {
\r
165 var tmp = startOffset;
\r
166 startOffset = endOffset;
\r
169 var node = selection.anchorNode;
\r
170 var prefix = node.data.substr(0, startOffset);
\r
171 var suffix = node.data.substr(endOffset);
\r
172 var core = node.data.substr(startOffset, endOffset - startOffset);
\r
173 var newNode = $('<span wlxml-tag="' + target.val() + '">' + core + '</span>');
\r
174 $(node).replaceWith(newNode);
\r
175 newNode.before(prefix);
\r
176 newNode.after(suffix);
\r
177 mediator.nodeCreated(newNode);
\r
182 selectTab: function(id) {
\r
183 this.node.find('.rng-visualEditor-sidebarContentItem').hide();
\r
184 this.node.find('#'+id).show();
\r
185 this.node.find('#rng-visualEditor-sidebarButtons li').removeClass('active');
\r
186 this.node.find('#rng-visualEditor-sidebarButtons li a[data-content-id=' + id + ']').parent().addClass('active');
\r
189 updateEditPane: function(node) {
\r
190 var pane = this.node.find('#rng-visualEditor-edit');
\r
191 pane.html( $(sandbox.getTemplate('editPane')({tag: node.attr('wlxml-tag'), klass: node.attr('wlxml-class')})));
\r
196 sideBarView.setup();
\r
199 getCurrentNode: function() {
\r
200 return view.currentNode;
\r
202 nodeCreated: function(node) {
\r
203 view.selectNode(node);
\r
206 nodeSelected: function(node) {
\r
207 sideBarView.updateEditPane(node);
\r
211 var isDirty = false;
\r
212 var wasShownAlready = false;
\r
216 start: function() {
\r
217 sandbox.publish('ready');
\r
219 getView: function() {
\r
222 setDocument: function(xml) {
\r
223 var transformed = transformations.fromXML.getDocumentDescription(xml);
\r
224 view.setBody(transformed.HTMLTree);
\r
225 view.setMetaData(transformed.metadata);
\r
228 getDocument: function() {
\r
229 return transformations.toXML.getXML({HTMLTree: view.getBody(), metadata: view.getMetaData()});
\r
231 isDirty: function() {
\r
234 setDirty: function(dirty) {
\r
237 onShowed: function() {
\r
238 if(!wasShownAlready) {
\r
239 wasShownAlready = true;
\r
240 view.selectFirstNode();
\r