1 // Module that implements main WYSIWIG edit area
\r
4 'libs/underscore-min',
\r
5 './transformations',
\r
6 'libs/text!./template.html'], function(_, transformations, template) {
\r
10 return function(sandbox) {
\r
13 node: $(_.template(template)()),
\r
18 this.node.find('#rng-module-documentCanvas-content').on('keyup', function() {
\r
20 sandbox.publish('contentChanged');
\r
23 this.node.on('mouseover', '[wlxml-tag]', function(e) {
\r
24 e.stopPropagation();
\r
25 sandbox.publish('nodeHovered', $(e.target));
\r
27 this.node.on('mouseout', '[wlxml-tag]', function(e) {
\r
28 e.stopPropagation();
\r
29 sandbox.publish('nodeBlured', $(e.target));
\r
31 this.node.on('click', '[wlxml-tag]', function(e) {
\r
32 e.stopPropagation();
\r
33 console.log('clicked node type: '+e.target.nodeType);
\r
34 view._markSelected($(e.target));
\r
37 this.node.on('keyup', '#rng-module-documentCanvas-contentWrapper', function(e) {
\r
38 var anchor = $(window.getSelection().anchorNode);
\r
39 if(anchor[0].nodeType === Node.TEXT_NODE)
\r
40 anchor = anchor.parent();
\r
41 if(!anchor.is('[wlxml-tag]'))
\r
43 view._markSelected(anchor);
\r
46 this.node.on('keydown', '#rng-module-documentCanvas-contentWrapper', function(e) {
\r
47 if(e.which === 13) {
\r
49 view.insertNewNode(null, null);
\r
54 var observer = new MutationObserver(function(mutations) {
\r
55 mutations.forEach(function(mutation) {
\r
56 _.each(mutation.addedNodes, function(node) {
\r
58 node.parent().find('[wlxml-tag]').each(function() {
\r
61 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
66 var config = { attributes: true, childList: true, characterData: true, subtree: true };
\r
67 observer.observe(this.node.find('#rng-module-documentCanvas-contentWrapper')[0], config);
\r
69 this.gridToggled = false;
\r
71 _createNode: function(wlxmlTag, wlxmlClass) {
\r
72 var toBlock = ['div', 'document', 'section', 'header'];
\r
73 var htmlTag = _.contains(toBlock, wlxmlTag) ? 'div' : 'span';
\r
74 var toret = $('<' + htmlTag + '>');
\r
75 toret.attr('wlxml-tag', wlxmlTag);
\r
77 toret.attr('wlxml-class', wlxmlClass);
\r
80 insertNewNode: function(wlxmlTag, wlxmlClass) {
\r
81 //TODO: Insert inline
\r
82 var anchor = $(window.getSelection().anchorNode);
\r
83 var anchorOffset = window.getSelection().anchorOffset;
\r
84 if(anchor[0].nodeType === Node.TEXT_NODE)
\r
85 anchor = anchor.parent();
\r
86 if(anchor.text() === '') {
\r
88 anchor = anchor.parent();
\r
91 if(anchorOffset > 0 && anchorOffset < anchor.text().length) {
\r
92 if(wlxmlTag === null && wlxmlClass === null) {
\r
93 return this.splitWithNewNode(anchor);
\r
95 return this.wrapSelectionWithNewNode(wlxmlTag, wlxmlClass);
\r
97 var newNode = this._createNode(wlxmlTag || anchor.attr('wlxml-tag'), wlxmlClass || anchor.attr('wlxml-class'));
\r
98 if(anchorOffset === 0)
\r
99 anchor.before(newNode)
\r
101 anchor.after(newNode);
\r
102 this.selectNode(newNode);
\r
104 sandbox.publish('contentChanged');
\r
106 wrapSelectionWithNewNode: function(wlxmlTag, wlxmlClass) {
\r
107 var selection = window.getSelection();
\r
108 if(selection.anchorNode === selection.focusNode && selection.anchorNode.nodeType === Node.TEXT_NODE) {
\r
109 var startOffset = selection.anchorOffset;
\r
110 var endOffset = selection.focusOffset;
\r
111 if(startOffset > endOffset) {
\r
112 var tmp = startOffset;
\r
113 startOffset = endOffset;
\r
116 var node = selection.anchorNode;
\r
117 var prefix = node.data.substr(0, startOffset);
\r
118 var suffix = node.data.substr(endOffset);
\r
119 var core = node.data.substr(startOffset, endOffset - startOffset);
\r
120 var newNode = this._createNode(wlxmlTag, wlxmlClass);
\r
121 newNode.text(core || 'test');
\r
122 $(node).replaceWith(newNode);
\r
123 newNode.before(prefix);
\r
124 newNode.after(suffix);
\r
126 this.selectNode(newNode);
\r
128 sandbox.publish('contentChanged');
\r
131 splitWithNewNode: function(node) {
\r
132 var selection = window.getSelection();
\r
133 if(selection.anchorNode === selection.focusNode && selection.anchorNode.nodeType === Node.TEXT_NODE) {
\r
134 var startOffset = selection.anchorOffset;
\r
135 var endOffset = selection.focusOffset;
\r
136 if(startOffset > endOffset) {
\r
137 var tmp = startOffset;
\r
138 startOffset = endOffset;
\r
141 var anchor = selection.anchorNode;
\r
142 var prefix = anchor.data.substr(0, startOffset);
\r
143 var suffix = anchor.data.substr(endOffset);
\r
144 var prefixNode = this._createNode(node.attr('wlxml-tag'), node.attr('wlxml-class'));
\r
145 var newNode = this._createNode(node.attr('wlxml-tag'), node.attr('wlxml-class'));
\r
146 var suffixNode = this._createNode(node.attr('wlxml-tag'), node.attr('wlxml-class'));
\r
147 prefixNode.text(prefix);
\r
148 suffixNode.text(suffix);
\r
149 node.replaceWith(newNode);
\r
150 newNode.before(prefixNode);
\r
151 newNode.after(suffixNode);
\r
153 this.selectNode(newNode);
\r
155 sandbox.publish('contentChanged');
\r
158 setBody: function(HTMLTree) {
\r
159 this.node.find('#rng-module-documentCanvas-content').html(HTMLTree);
\r
161 getBody: function() {
\r
162 return this.node.find('#rng-module-documentCanvas-content').html();
\r
164 _markSelected: function(node) {
\r
165 this.dimNode(node);
\r
167 this.node.find('.rng-module-documentCanvas-currentNode').removeClass('rng-module-documentCanvas-currentNode');
\r
169 node.addClass('rng-module-documentCanvas-currentNode');
\r
171 this.currentNode = node;
\r
172 sandbox.publish('nodeSelected', node);
\r
175 selectNode: function(node) {
\r
176 view._markSelected(node);
\r
177 var range = document.createRange();
\r
178 range.selectNodeContents(node[0]);
\r
179 range.collapse(false);
\r
181 var selection = document.getSelection();
\r
182 selection.removeAllRanges()
\r
183 selection.addRange(range);
\r
185 selectNodeById: function(id) {
\r
186 var node = this.node.find('#'+id);
\r
188 this.selectNode(node);
\r
190 highlightNode: function(node) {
\r
191 if(!this.gridToggled) {
\r
192 node.addClass('rng-common-hoveredNode');
\r
193 var label = node.attr('wlxml-tag');
\r
194 if(node.attr('wlxml-class'))
\r
195 label += ' / ' + node.attr('wlxml-class');
\r
196 var tag = $('<div>').addClass('rng-module-documentCanvas-hoveredNodeTag').text(label);
\r
200 dimNode: function(node) {
\r
201 if(!this.gridToggled) {
\r
202 node.removeClass('rng-common-hoveredNode');
\r
203 node.find('.rng-module-documentCanvas-hoveredNodeTag').remove();
\r
206 highlightNodeById: function(id) {
\r
207 var node = this.node.find('#'+id);
\r
209 this.highlightNode(node);
\r
211 dimNodeById: function(id) {
\r
212 var node = this.node.find('#'+id);
\r
214 this.dimNode(node);
\r
216 selectFirstNode: function() {
\r
217 var firstNodeWithText = this.node.find('[wlxml-tag]').filter(function() {
\r
218 return $(this).clone().children().remove().end().text().trim() !== '';
\r
221 if(firstNodeWithText.length)
\r
222 node = $(firstNodeWithText[0])
\r
224 node = this.node.find('[wlxml-class|="p"]')
\r
226 this.selectNode(node);
\r
228 toggleGrid: function(toggle) {
\r
229 this.node.find('[wlxml-tag]').toggleClass('rng-common-hoveredNode', toggle);
\r
230 this.gridToggled = toggle;
\r
238 start: function() { sandbox.publish('ready'); },
\r
239 getView: function() { return view.node; },
\r
240 setDocument: function(xml) {
\r
241 var transformed = transformations.fromXML.getDocumentDescription(xml);
\r
242 view.setBody(transformed.HTMLTree);
\r
243 view.selectFirstNode();
\r
246 modifyCurrentNode: function(attr, value) {
\r
247 if(view.currentNode)
\r
248 view.currentNode.attr('wlxml-'+attr, value);
\r
250 highlightNode: function(id) {
\r
251 view.highlightNodeById(id);
\r
253 dimNode: function(id) {
\r
254 view.dimNodeById(id);
\r
256 selectNode: function(id) {
\r
257 view.selectNodeById(id);
\r
259 toggleGrid: function(toggle) {
\r
260 view.toggleGrid(toggle);
\r
262 insertNewNode: function(wlxmlTag, wlxmlClass) {
\r
263 view.insertNewNode(wlxmlTag, wlxmlClass);
\r
265 wrapSelectionWithNewNode: function(wlxmlTag, wlxmlClass) {
\r
266 view.wrapSelectionWithNewNode(wlxmlTag, wlxmlClass);
\r