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
35 var metaTable = this.metaTable = this.node.find('#rng-visualEditor-meta table');
\r
37 this.node.find('.rng-visualEditor-metaAddBtn').click(function() {
\r
38 var newRow = view._addMetaRow('', '');
\r
39 $(newRow.find('td div')[0]).focus();
\r
43 this.metaTable.on('click', '.rng-visualEditor-metaRemoveBtn', function(e) {
\r
44 $(e.target).closest('tr').remove();
\r
48 this.metaTable.on('keydown', '[contenteditable]', function(e) {
\r
49 console.log(e.which);
\r
50 if(e.which === 13) {
\r
51 if($(document.activeElement).hasClass('rng-visualEditor-metaItemKey')) {
\r
52 metaTable.find('.rng-visualEditor-metaItemValue').focus();
\r
54 var input = $('<input>');
\r
55 input.appendTo('body').focus()
\r
56 view.node.find('.rng-visualEditor-metaAddBtn').focus();
\r
65 getMetaData: function() {
\r
67 this.metaTable.find('tr').each(function() {
\r
69 var inputs = $(this).find('td [contenteditable]');
\r
70 var key = $(inputs[0]).text();
\r
71 var value = $(inputs[1]).text();
\r
77 setMetaData: function(metadata) {
\r
79 this.metaTable.find('tr').remove();
\r
80 _.each(_.keys(metadata), function(key) {
\r
81 view._addMetaRow(key, metadata[key]);
\r
84 setBody: function(HTMLTree) {
\r
85 this.node.find('#rng-visualEditor-content').html(HTMLTree);
\r
87 getBody: function() {
\r
88 return this.node.find('#rng-visualEditor-content').html();
\r
90 _markSelected: function(node) {
\r
91 this.node.find('.rng-current').removeClass('rng-current');
\r
92 node.addClass('rng-current');
\r
93 this.currentNode = node;
\r
94 mediator.nodeSelected(node);
\r
96 selectFirstNode: function() {
\r
97 var firstNodeWithText = this.node.find('[wlxml-tag]').filter(function() {
\r
98 return $(this).clone().children().remove().end().text().trim() !== '';
\r
100 if(firstNodeWithText.length)
\r
101 $(firstNodeWithText[0]).click().focus();
\r
103 _addMetaRow: function(key, value) {
\r
104 var newRow = $(sandbox.getTemplate('metaItem')({key: key || '', value: value || ''}));
\r
105 newRow.appendTo(this.metaTable);
\r
111 var sideBarView = {
\r
112 node: view.node.find('#rng-visualEditor-sidebar'),
\r
113 setup: function() {
\r
115 this.node.find('#rng-visualEditor-sidebarButtons a').click(function(e) {
\r
116 e.preventDefault();
\r
117 var target = $(e.currentTarget);
\r
118 if(!target.attr('data-content-id'))
\r
120 view.selectTab(target.attr('data-content-id'));
\r
122 view.selectTab('rng-visualEditor-edit');
\r
124 view.node.on('change', '.rng-visualEditor-editPaneNodeForm select', function(e) {
\r
125 var target = $(e.target);
\r
126 var attr = target.attr('id').split('-')[2].split('editPane')[1].substr(0,3) === 'Tag' ? 'tag' : 'class';
\r
127 mediator.getCurrentNode().attr('wlxml-'+attr, target.val());
\r
131 view.node.on('change', '.rng-visualEditor-editPaneSelectionForm select', function(e) {
\r
132 var target = $(e.target);
\r
133 var selection = window.getSelection();
\r
134 if(selection.anchorNode === selection.focusNode && selection.anchorNode.nodeType === Node.TEXT_NODE) {
\r
135 var startOffset = selection.anchorOffset;
\r
136 var endOffset = selection.focusOffset;
\r
137 if(startOffset > endOffset) {
\r
138 var tmp = startOffset;
\r
139 startOffset = endOffset;
\r
142 var node = selection.anchorNode;
\r
143 var prefix = node.data.substr(0, startOffset);
\r
144 var suffix = node.data.substr(endOffset);
\r
145 var core = node.data.substr(startOffset, endOffset - startOffset);
\r
146 var newNode = $('<span wlxml-tag="' + target.val() + '">' + core + '</span>');
\r
147 $(node).replaceWith(newNode);
\r
148 newNode.before(prefix);
\r
149 newNode.after(suffix);
\r
150 mediator.nodeCreated(newNode);
\r
155 selectTab: function(id) {
\r
156 this.node.find('.rng-visualEditor-sidebarContentItem').hide();
\r
157 this.node.find('#'+id).show();
\r
158 this.node.find('#rng-visualEditor-sidebarButtons li').removeClass('active');
\r
159 this.node.find('#rng-visualEditor-sidebarButtons li a[data-content-id=' + id + ']').parent().addClass('active');
\r
162 updateEditPane: function(node) {
\r
163 var pane = this.node.find('#rng-visualEditor-edit');
\r
164 pane.html( $(sandbox.getTemplate('editPane')({tag: node.attr('wlxml-tag'), klass: node.attr('wlxml-class')})));
\r
169 sideBarView.setup();
\r
172 getCurrentNode: function() {
\r
173 return view.currentNode;
\r
175 nodeCreated: function(node) {
\r
176 view._markSelected(node);
\r
178 var range = document.createRange();
\r
179 range.selectNodeContents(node[0]);
\r
180 range.collapse(false);
\r
182 var selection = document.getSelection();
\r
183 selection.removeAllRanges()
\r
184 selection.addRange(range);
\r
187 nodeSelected: function(node) {
\r
188 sideBarView.updateEditPane(node);
\r
192 var isDirty = false;
\r
193 var wasShownAlready = false;
\r
197 start: function() {
\r
198 sandbox.publish('ready');
\r
200 getView: function() {
\r
203 setDocument: function(xml) {
\r
204 var transformed = transformations.fromXML.getDocumentDescription(xml);
\r
205 view.setBody(transformed.HTMLTree);
\r
206 view.setMetaData(transformed.metadata);
\r
209 getDocument: function() {
\r
210 return transformations.toXML.getXML({HTMLTree: view.getBody(), metadata: view.getMetaData()});
\r
212 isDirty: function() {
\r
215 setDirty: function(dirty) {
\r
218 onShowed: function() {
\r
219 if(!wasShownAlready) {
\r
220 wasShownAlready = true;
\r
221 view.selectFirstNode();
\r