1 // Module that implements main WYSIWIG edit area
\r
4 'libs/underscore-min',
\r
5 './transformations',
\r
7 'libs/text!./template.html'], function(_, transformations, wlxmlNode, template) {
\r
11 return function(sandbox) {
\r
14 node: $(_.template(template)()),
\r
16 shownAlready: false,
\r
20 this.node.find('#rng-module-documentCanvas-content').on('keyup', function() {
\r
22 sandbox.publish('contentChanged');
\r
25 this.node.on('mouseover', '[wlxml-tag]', function(e) {
\r
26 e.stopPropagation();
\r
27 sandbox.publish('nodeHovered', new wlxmlNode.Node($(e.target)));
\r
29 this.node.on('mouseout', '[wlxml-tag]', function(e) {
\r
30 e.stopPropagation();
\r
31 sandbox.publish('nodeBlured', new wlxmlNode.Node($(e.target)));
\r
33 this.node.on('click', '[wlxml-tag]', function(e) {
\r
34 e.stopPropagation();
\r
35 console.log('clicked node type: '+e.target.nodeType);
\r
36 view.selectNode(new wlxmlNode.Node($(e.target)));
\r
39 this.node.on('keyup', '#rng-module-documentCanvas-contentWrapper', function(e) {
\r
40 var anchor = $(window.getSelection().anchorNode);
\r
41 if(anchor[0].nodeType === Node.TEXT_NODE)
\r
42 anchor = anchor.parent();
\r
43 if(!anchor.is('[wlxml-tag]'))
\r
45 view.selectNode(new wlxmlNode.Node(anchor));
\r
48 this.node.on('keydown', '#rng-module-documentCanvas-contentWrapper', function(e) {
\r
49 if(e.which === 13) {
\r
51 view.insertNewNode(null, null);
\r
55 this.node.onShow = function() {
\r
56 if(!view.shownAlready) {
\r
57 view.shownAlready = true;
\r
58 view.selectFirstNode();
\r
59 } else if(view.currentNode) {
\r
60 view.moveCarretToNode(view.getNodeElement(view.currentNode));
\r
64 this.gridToggled = false;
\r
66 _createNode: function(wlxmlTag, wlxmlClass) {
\r
67 var toBlock = ['div', 'document', 'section', 'header'];
\r
68 var htmlTag = _.contains(toBlock, wlxmlTag) ? 'div' : 'span';
\r
69 var toret = $('<' + htmlTag + '>');
\r
70 toret.attr('wlxml-tag', wlxmlTag);
\r
72 toret.attr('wlxml-class', wlxmlClass);
\r
73 toret.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
76 insertNewNode: function(wlxmlTag, wlxmlClass) {
\r
77 //TODO: Insert inline
\r
78 var anchor = $(window.getSelection().anchorNode);
\r
79 var anchorOffset = window.getSelection().anchorOffset;
\r
80 if(anchor[0].nodeType === Node.TEXT_NODE)
\r
81 anchor = anchor.parent();
\r
82 if(anchor.text() === '') {
\r
84 anchor = anchor.parent();
\r
87 if(anchorOffset > 0 && anchorOffset < anchor.text().length) {
\r
88 if(wlxmlTag === null && wlxmlClass === null) {
\r
89 return this.splitWithNewNode(anchor);
\r
91 return this.wrapSelectionWithNewNode(wlxmlTag, wlxmlClass);
\r
93 var newNode = this._createNode(wlxmlTag || anchor.attr('wlxml-tag'), wlxmlClass || anchor.attr('wlxml-class'));
\r
94 if(anchorOffset === 0)
\r
95 anchor.before(newNode)
\r
97 anchor.after(newNode);
\r
98 this.selectNode(new wlxmlNode.Node(newNode), {moveCarret: true});
\r
100 sandbox.publish('contentChanged');
\r
102 wrapSelectionWithNewNode: function(wlxmlTag, wlxmlClass) {
\r
103 var selection = window.getSelection();
\r
104 if(selection.anchorNode === selection.focusNode && selection.anchorNode.nodeType === Node.TEXT_NODE) {
\r
105 var startOffset = selection.anchorOffset;
\r
106 var endOffset = selection.focusOffset;
\r
107 if(startOffset > endOffset) {
\r
108 var tmp = startOffset;
\r
109 startOffset = endOffset;
\r
112 var node = selection.anchorNode;
\r
113 var prefix = node.data.substr(0, startOffset);
\r
114 var suffix = node.data.substr(endOffset);
\r
115 var core = node.data.substr(startOffset, endOffset - startOffset);
\r
116 var newNode = this._createNode(wlxmlTag, wlxmlClass);
\r
117 newNode.text(core || 'test');
\r
118 $(node).replaceWith(newNode);
\r
119 newNode.before(prefix);
\r
120 newNode.after(suffix);
\r
122 this.selectNode(new wlxmlNode.Node(newNode), {moveCarret: true});
\r
124 sandbox.publish('contentChanged');
\r
127 splitWithNewNode: function(node) {
\r
128 var selection = window.getSelection();
\r
129 if(selection.anchorNode === selection.focusNode && selection.anchorNode.nodeType === Node.TEXT_NODE) {
\r
130 var startOffset = selection.anchorOffset;
\r
131 var endOffset = selection.focusOffset;
\r
132 if(startOffset > endOffset) {
\r
133 var tmp = startOffset;
\r
134 startOffset = endOffset;
\r
137 var anchor = selection.anchorNode;
\r
138 var prefix = anchor.data.substr(0, startOffset);
\r
139 var suffix = anchor.data.substr(endOffset);
\r
140 var prefixNode = this._createNode(node.attr('wlxml-tag'), node.attr('wlxml-class'));
\r
141 var newNode = this._createNode(node.attr('wlxml-tag'), node.attr('wlxml-class'));
\r
142 var suffixNode = this._createNode(node.attr('wlxml-tag'), node.attr('wlxml-class'));
\r
143 prefixNode.text(prefix);
\r
144 suffixNode.text(suffix);
\r
145 node.replaceWith(newNode);
\r
146 newNode.before(prefixNode);
\r
147 newNode.after(suffixNode);
\r
149 this.selectNode(new wlxmlNode.Node(newNode), {moveCarret: true});
\r
151 sandbox.publish('contentChanged');
\r
154 setBody: function(HTMLTree) {
\r
155 this.node.find('#rng-module-documentCanvas-content').html(HTMLTree);
\r
157 getBody: function() {
\r
158 return this.node.find('#rng-module-documentCanvas-content').html();
\r
160 selectNode: function(wlxmlNode, options) {
\r
161 options = options || {};
\r
162 var nodeElement = this.getNodeElement(wlxmlNode)
\r
164 this.dimNode(wlxmlNode);
\r
166 this.node.find('.rng-module-documentCanvas-currentNode').removeClass('rng-module-documentCanvas-currentNode');
\r
167 nodeElement.addClass('rng-module-documentCanvas-currentNode');
\r
169 if(options.moveCarret) {
\r
170 this.moveCarretToNode(nodeElement);
\r
173 this.currentNode = wlxmlNode;
\r
174 sandbox.publish('nodeSelected', wlxmlNode);
\r
176 highlightNode: function(wlxmlNode) {
\r
177 var nodeElement = this.getNodeElement(wlxmlNode);
\r
178 if(!this.gridToggled) {
\r
179 nodeElement.addClass('rng-common-hoveredNode');
\r
180 var label = nodeElement.attr('wlxml-tag');
\r
181 if(nodeElement.attr('wlxml-class'))
\r
182 label += ' / ' + nodeElement.attr('wlxml-class');
\r
183 var tag = $('<div>').addClass('rng-module-documentCanvas-hoveredNodeTag').text(label);
\r
184 nodeElement.append(tag);
\r
187 dimNode: function(wlxmlNode) {
\r
188 var nodeElement = this.getNodeElement(wlxmlNode);
\r
189 if(!this.gridToggled) {
\r
190 nodeElement.removeClass('rng-common-hoveredNode');
\r
191 nodeElement.find('.rng-module-documentCanvas-hoveredNodeTag').remove();
\r
194 selectFirstNode: function() {
\r
195 var firstNodeWithText = this.node.find('[wlxml-tag]').filter(function() {
\r
196 return $(this).clone().children().remove().end().text().trim() !== '';
\r
199 if(firstNodeWithText.length)
\r
200 node = $(firstNodeWithText[0])
\r
202 node = this.node.find('[wlxml-class|="p"]')
\r
204 this.selectNode(new wlxmlNode.Node(node), {moveCarret: true});
\r
206 toggleGrid: function(toggle) {
\r
207 this.node.find('[wlxml-tag]').toggleClass('rng-common-hoveredNode', toggle);
\r
208 this.gridToggled = toggle;
\r
210 getNodeElement: function(wlxmlNode) {
\r
211 return this.node.find('#'+wlxmlNode.id);
\r
213 moveCarretToNode: function(nodeElement) {
\r
214 var range = document.createRange();
\r
215 range.selectNodeContents(nodeElement[0]);
\r
216 range.collapse(false);
\r
217 var selection = document.getSelection();
\r
218 selection.removeAllRanges()
\r
219 selection.addRange(range);
\r
227 start: function() { sandbox.publish('ready'); },
\r
228 getView: function() { return view.node; },
\r
229 setDocument: function(xml) {
\r
230 var transformed = transformations.fromXML.getDocumentDescription(xml);
\r
231 view.setBody(transformed.HTMLTree);
\r
232 sandbox.publish('documentSet');
\r
234 getDocument: function() {
\r
235 return transformations.toXML.getXML(view.getBody());
\r
237 modifyCurrentNode: function(attr, value) {
\r
238 if(view.currentNode) {
\r
239 view.getNodeElement(view.currentNode).attr('wlxml-'+attr, value);
\r
240 sandbox.publish('contentChanged');
\r
243 highlightNode: function(wlxmlNode) {
\r
244 view.highlightNode(wlxmlNode);
\r
246 dimNode: function(wlxmlNode) {
\r
247 view.dimNode(wlxmlNode);
\r
249 selectNode: function(wlxmlNode) {
\r
250 if(!wlxmlNode.is(view.currentNode))
\r
251 view.selectNode(wlxmlNode, {moveCarret: true});
\r
253 toggleGrid: function(toggle) {
\r
254 view.toggleGrid(toggle);
\r
256 insertNewNode: function(wlxmlTag, wlxmlClass) {
\r
257 view.insertNewNode(wlxmlTag, wlxmlClass);
\r
259 wrapSelectionWithNewNode: function(wlxmlTag, wlxmlClass) {
\r
260 view.wrapSelectionWithNewNode(wlxmlTag, wlxmlClass);
\r