4e61b8898f38e05a01372bc08698679e8e7d4410
[fnpeditor.git] / modules / documentCanvas / documentCanvas.js
1 // Module that implements main WYSIWIG edit area\r
2 \r
3 define([\r
4 'libs/underscore-min',\r
5 './transformations', \r
6 './wlxmlNode',\r
7 'libs/text!./template.html'], function(_, transformations, wlxmlNode, template) {\r
8 \r
9 'use strict';\r
10 \r
11 return function(sandbox) {\r
12 \r
13     var view = {\r
14         node: $(_.template(template)()),\r
15         currentNode: null,\r
16         shownAlready: false,\r
17         scrollbarPosition: 0,\r
18         setup: function() {\r
19             var view = this;\r
20 \r
21             this.node.find('#rng-module-documentCanvas-content').on('keyup', function() {\r
22                 //isDirty = true;\r
23                 sandbox.publish('contentChanged');\r
24             });\r
25 \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
29             });\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
33             });\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
38             });\r
39 \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
45                     return;\r
46                 view.selectNode(new wlxmlNode.Node(anchor));\r
47             });\r
48             \r
49             this.node.on('keydown', '#rng-module-documentCanvas-contentWrapper', function(e) {\r
50                 if(e.which === 13) { \r
51                     e.preventDefault();\r
52                     view.insertNewNode(null, null);\r
53                 }\r
54                 if(e.which === 8) {\r
55                     var anchor = window.getSelection().anchorNode;\r
56                     var len = anchor.length;\r
57                     console.log(len);\r
58                     if(len === 1) {\r
59                         e.preventDefault();\r
60                         $(anchor).parent().text('');\r
61                     }\r
62                 }\r
63             });\r
64                       \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
72                 }\r
73             };\r
74             this.node.onHide = function() {\r
75                 view.scrollbarPosition = view.node.find('#rng-module-documentCanvas-contentWrapper').scrollTop();\r
76             }\r
77                       \r
78             this.gridToggled = false;\r
79         },\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
85             if(wlxmlClass)\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
88             return toret;\r
89         },\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
97                 var todel = anchor;\r
98                 anchor = anchor.parent();\r
99                 todel.remove();\r
100             }\r
101             if(anchorOffset > 0 && anchorOffset < anchor.text().length) {\r
102                 if(wlxmlTag === null && wlxmlClass === null) {\r
103                     return this.splitWithNewNode(anchor);\r
104                 }\r
105                 return this.wrapSelectionWithNewNode(wlxmlTag, wlxmlClass);\r
106             }\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
110             else\r
111                 anchor.after(newNode);\r
112             this.selectNode(new wlxmlNode.Node(newNode), {movecaret: true});\r
113             //isDirty = true;\r
114             sandbox.publish('contentChanged');\r
115         },\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
124                     endOffset = tmp;\r
125                 }\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
135                 \r
136                 this.selectNode(new wlxmlNode.Node(newNode), {movecaret: true});\r
137                 //isDirty = true;\r
138                 sandbox.publish('contentChanged');\r
139             }\r
140         },\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
149                     endOffset = tmp;\r
150                 }\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
162                 \r
163                 this.selectNode(new wlxmlNode.Node(newNode), {movecaret: true});\r
164                 //isDirty = true;\r
165                 sandbox.publish('contentChanged');\r
166             }\r
167         },\r
168         setBody: function(HTMLTree) {\r
169             this.node.find('#rng-module-documentCanvas-content').html(HTMLTree);\r
170         },\r
171         getBody: function() {\r
172             return this.node.find('#rng-module-documentCanvas-content').html();\r
173         }, \r
174         selectNode: function(wlxmlNode, options) {\r
175             options = options || {};\r
176             var nodeElement = this.getNodeElement(wlxmlNode)\r
177             \r
178             this.dimNode(wlxmlNode);\r
179             \r
180             this.node.find('.rng-module-documentCanvas-currentNode').removeClass('rng-module-documentCanvas-currentNode');\r
181             nodeElement.addClass('rng-module-documentCanvas-currentNode');\r
182              \r
183             if(options.movecaret) {\r
184                 this.movecaretToNode(nodeElement);\r
185             }\r
186             \r
187             this.currentNode = wlxmlNode;\r
188             sandbox.publish('nodeSelected', wlxmlNode);\r
189         },\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
199             }\r
200         },\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
206             }\r
207         },\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
211             }).first();\r
212             var node;\r
213             if(firstNodeWithText.length)\r
214                 node = $(firstNodeWithText[0])\r
215             else {\r
216                 node = this.node.find('[wlxml-class|="p"]')\r
217             }\r
218             this.selectNode(new wlxmlNode.Node(node), {movecaret: true});\r
219         },\r
220         toggleGrid: function(toggle) {\r
221             this.node.find('[wlxml-tag]').toggleClass('rng-common-hoveredNode', toggle);\r
222             this.gridToggled = toggle;\r
223         },\r
224         getNodeElement: function(wlxmlNode) {\r
225             return this.node.find('#'+wlxmlNode.id);\r
226         },\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
234         }\r
235     };\r
236     \r
237     view.setup();\r
238 \r
239     /* public api */\r
240     return {\r
241         start: function() { sandbox.publish('ready'); },\r
242         getView: function() { \r
243             return view.node;\r
244         },\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
249         },\r
250         getDocument: function() {\r
251             return transformations.toXML.getXML(view.getBody());\r
252         },\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
257             }\r
258         },\r
259         highlightNode: function(wlxmlNode) {\r
260             view.highlightNode(wlxmlNode);\r
261         },\r
262         dimNode: function(wlxmlNode) {\r
263             view.dimNode(wlxmlNode);\r
264         },\r
265         selectNode: function(wlxmlNode) {\r
266             if(!wlxmlNode.is(view.currentNode))\r
267                 view.selectNode(wlxmlNode, {movecaret: true});\r
268         },\r
269         toggleGrid: function(toggle) {\r
270             view.toggleGrid(toggle);\r
271         },\r
272         insertNewNode: function(wlxmlTag, wlxmlClass) {\r
273             view.insertNewNode(wlxmlTag, wlxmlClass);\r
274         },\r
275         wrapSelectionWithNewNode: function(wlxmlTag, wlxmlClass) {\r
276             view.wrapSelectionWithNewNode(wlxmlTag, wlxmlClass);\r
277         }\r
278     }\r
279     \r
280 };\r
281 \r
282 });