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
17 scrollbarPosition: 0,
\r
21 this.node.find('#rng-module-documentCanvas-content').on('keyup', function() {
\r
23 sandbox.publish('contentChanged');
\r
26 this.node.on('mouseover', '[wlxml-tag]', function(e) {
\r
27 e.stopPropagation();
\r
28 sandbox.publish('nodeHovered', new wlxmlNode.Node($(e.target)));
\r
30 this.node.on('mouseout', '[wlxml-tag]', function(e) {
\r
31 e.stopPropagation();
\r
32 sandbox.publish('nodeBlured', new wlxmlNode.Node($(e.target)));
\r
34 this.node.on('click', '[wlxml-tag]', function(e) {
\r
35 e.stopPropagation();
\r
36 console.log('clicked node type: '+e.target.nodeType);
\r
37 view.selectNode(new wlxmlNode.Node($(e.target)));
\r
40 this.node.on('keyup', '#rng-module-documentCanvas-contentWrapper', function(e) {
\r
41 var anchor = $(window.getSelection().anchorNode);
\r
42 if(anchor[0].nodeType === Node.TEXT_NODE)
\r
43 anchor = anchor.parent();
\r
44 if(!anchor.is('[wlxml-tag]'))
\r
46 view.selectNode(new wlxmlNode.Node(anchor));
\r
49 this.node.on('keydown', '#rng-module-documentCanvas-contentWrapper', function(e) {
\r
50 if(e.which === 13) {
\r
52 view.insertNewNode(null, null);
\r
55 var anchor = window.getSelection().anchorNode;
\r
56 var len = anchor.length;
\r
60 $(anchor).parent().text('');
\r
65 this.node.onShow = function() {
\r
66 if(!view.shownAlready) {
\r
67 view.shownAlready = true;
\r
68 view.selectFirstNode();
\r
69 } else if(view.currentNode) {
\r
70 view.movecaretToNode(view.getNodeElement(view.currentNode));
\r
71 view.node.find('#rng-module-documentCanvas-contentWrapper').scrollTop(view.scrollbarPosition);
\r
74 this.node.onHide = function() {
\r
75 view.scrollbarPosition = view.node.find('#rng-module-documentCanvas-contentWrapper').scrollTop();
\r
78 this.gridToggled = false;
\r
80 _createNode: function(wlxmlTag, wlxmlClass) {
\r
81 var toBlock = ['div', 'document', 'section', 'header'];
\r
82 var htmlTag = _.contains(toBlock, wlxmlTag) ? 'div' : 'span';
\r
83 var toret = $('<' + htmlTag + '>');
\r
84 toret.attr('wlxml-tag', wlxmlTag);
\r
86 toret.attr('wlxml-class', wlxmlClass);
\r
87 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
90 insertNewNode: function(wlxmlTag, wlxmlClass) {
\r
91 //TODO: Insert inline
\r
92 var anchor = $(window.getSelection().anchorNode);
\r
93 var anchorOffset = window.getSelection().anchorOffset;
\r
94 if(anchor[0].nodeType === Node.TEXT_NODE)
\r
95 anchor = anchor.parent();
\r
96 if(anchor.text() === '') {
\r
98 anchor = anchor.parent();
\r
101 if(anchorOffset > 0 && anchorOffset < anchor.text().length) {
\r
102 if(wlxmlTag === null && wlxmlClass === null) {
\r
103 return this.splitWithNewNode(anchor);
\r
105 return this.wrapSelectionWithNewNode(wlxmlTag, wlxmlClass);
\r
107 var newNode = this._createNode(wlxmlTag || anchor.attr('wlxml-tag'), wlxmlClass || anchor.attr('wlxml-class'));
\r
108 if(anchorOffset === 0)
\r
109 anchor.before(newNode)
\r
111 anchor.after(newNode);
\r
112 this.selectNode(new wlxmlNode.Node(newNode), {movecaret: true});
\r
114 sandbox.publish('contentChanged');
\r
116 wrapSelectionWithNewNode: function(wlxmlTag, wlxmlClass) {
\r
117 var selection = window.getSelection();
\r
118 if(selection.anchorNode === selection.focusNode && selection.anchorNode.nodeType === Node.TEXT_NODE) {
\r
119 var startOffset = selection.anchorOffset;
\r
120 var endOffset = selection.focusOffset;
\r
121 if(startOffset > endOffset) {
\r
122 var tmp = startOffset;
\r
123 startOffset = endOffset;
\r
126 var node = selection.anchorNode;
\r
127 var prefix = node.data.substr(0, startOffset);
\r
128 var suffix = node.data.substr(endOffset);
\r
129 var core = node.data.substr(startOffset, endOffset - startOffset);
\r
130 var newNode = this._createNode(wlxmlTag, wlxmlClass);
\r
131 newNode.text(core || 'test');
\r
132 $(node).replaceWith(newNode);
\r
133 newNode.before(prefix);
\r
134 newNode.after(suffix);
\r
136 this.selectNode(new wlxmlNode.Node(newNode), {movecaret: true});
\r
138 sandbox.publish('contentChanged');
\r
141 splitWithNewNode: function(node) {
\r
142 var selection = window.getSelection();
\r
143 if(selection.anchorNode === selection.focusNode && selection.anchorNode.nodeType === Node.TEXT_NODE) {
\r
144 var startOffset = selection.anchorOffset;
\r
145 var endOffset = selection.focusOffset;
\r
146 if(startOffset > endOffset) {
\r
147 var tmp = startOffset;
\r
148 startOffset = endOffset;
\r
151 var anchor = selection.anchorNode;
\r
152 var prefix = anchor.data.substr(0, startOffset);
\r
153 var suffix = anchor.data.substr(endOffset);
\r
154 var prefixNode = this._createNode(node.attr('wlxml-tag'), node.attr('wlxml-class'));
\r
155 var newNode = this._createNode(node.attr('wlxml-tag'), node.attr('wlxml-class'));
\r
156 var suffixNode = this._createNode(node.attr('wlxml-tag'), node.attr('wlxml-class'));
\r
157 prefixNode.text(prefix);
\r
158 suffixNode.text(suffix);
\r
159 node.replaceWith(newNode);
\r
160 newNode.before(prefixNode);
\r
161 newNode.after(suffixNode);
\r
163 this.selectNode(new wlxmlNode.Node(newNode), {movecaret: true});
\r
165 sandbox.publish('contentChanged');
\r
168 setBody: function(HTMLTree) {
\r
169 this.node.find('#rng-module-documentCanvas-content').html(HTMLTree);
\r
171 getBody: function() {
\r
172 return this.node.find('#rng-module-documentCanvas-content').html();
\r
174 selectNode: function(wlxmlNode, options) {
\r
175 options = options || {};
\r
176 var nodeElement = this.getNodeElement(wlxmlNode)
\r
178 this.dimNode(wlxmlNode);
\r
180 this.node.find('.rng-module-documentCanvas-currentNode').removeClass('rng-module-documentCanvas-currentNode');
\r
181 nodeElement.addClass('rng-module-documentCanvas-currentNode');
\r
183 if(options.movecaret) {
\r
184 this.movecaretToNode(nodeElement);
\r
187 this.currentNode = wlxmlNode;
\r
188 sandbox.publish('nodeSelected', wlxmlNode);
\r
190 highlightNode: function(wlxmlNode) {
\r
191 var nodeElement = this.getNodeElement(wlxmlNode);
\r
192 if(!this.gridToggled) {
\r
193 nodeElement.addClass('rng-common-hoveredNode');
\r
194 var label = nodeElement.attr('wlxml-tag');
\r
195 if(nodeElement.attr('wlxml-class'))
\r
196 label += ' / ' + nodeElement.attr('wlxml-class');
\r
197 var tag = $('<div>').addClass('rng-module-documentCanvas-hoveredNodeTag').text(label);
\r
198 nodeElement.append(tag);
\r
201 dimNode: function(wlxmlNode) {
\r
202 var nodeElement = this.getNodeElement(wlxmlNode);
\r
203 if(!this.gridToggled) {
\r
204 nodeElement.removeClass('rng-common-hoveredNode');
\r
205 nodeElement.find('.rng-module-documentCanvas-hoveredNodeTag').remove();
\r
208 selectFirstNode: function() {
\r
209 var firstNodeWithText = this.node.find('[wlxml-tag]').filter(function() {
\r
210 return $(this).clone().children().remove().end().text().trim() !== '';
\r
213 if(firstNodeWithText.length)
\r
214 node = $(firstNodeWithText[0])
\r
216 node = this.node.find('[wlxml-class|="p"]')
\r
218 this.selectNode(new wlxmlNode.Node(node), {movecaret: true});
\r
220 toggleGrid: function(toggle) {
\r
221 this.node.find('[wlxml-tag]').toggleClass('rng-common-hoveredNode', toggle);
\r
222 this.gridToggled = toggle;
\r
224 getNodeElement: function(wlxmlNode) {
\r
225 return this.node.find('#'+wlxmlNode.id);
\r
227 movecaretToNode: function(nodeElement) {
\r
228 var range = document.createRange();
\r
229 range.selectNodeContents(nodeElement[0]);
\r
230 range.collapse(false);
\r
231 var selection = document.getSelection();
\r
232 selection.removeAllRanges()
\r
233 selection.addRange(range);
\r
241 start: function() { sandbox.publish('ready'); },
\r
242 getView: function() {
\r
245 setDocument: function(xml) {
\r
246 var transformed = transformations.fromXML.getDocumentDescription(xml);
\r
247 view.setBody(transformed.HTMLTree);
\r
248 sandbox.publish('documentSet');
\r
250 getDocument: function() {
\r
251 return transformations.toXML.getXML(view.getBody());
\r
253 modifyCurrentNode: function(attr, value) {
\r
254 if(view.currentNode) {
\r
255 view.getNodeElement(view.currentNode).attr('wlxml-'+attr, value);
\r
256 sandbox.publish('contentChanged');
\r
259 highlightNode: function(wlxmlNode) {
\r
260 view.highlightNode(wlxmlNode);
\r
262 dimNode: function(wlxmlNode) {
\r
263 view.dimNode(wlxmlNode);
\r
265 selectNode: function(wlxmlNode) {
\r
266 if(!wlxmlNode.is(view.currentNode))
\r
267 view.selectNode(wlxmlNode, {movecaret: true});
\r
269 toggleGrid: function(toggle) {
\r
270 view.toggleGrid(toggle);
\r
272 insertNewNode: function(wlxmlTag, wlxmlClass) {
\r
273 view.insertNewNode(wlxmlTag, wlxmlClass);
\r
275 wrapSelectionWithNewNode: function(wlxmlTag, wlxmlClass) {
\r
276 view.wrapSelectionWithNewNode(wlxmlTag, wlxmlClass);
\r