bb8ad4a52f6e806e931f301381e59d7364b82a2a
[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             });\r
55                       \r
56             this.node.onShow = function() {\r
57                 if(!view.shownAlready) {\r
58                     view.shownAlready = true;\r
59                     view.selectFirstNode();\r
60                 } else if(view.currentNode) {\r
61                     view.movecaretToNode(view.getNodeElement(view.currentNode));\r
62                     view.node.find('#rng-module-documentCanvas-contentWrapper').scrollTop(view.scrollbarPosition);\r
63                 }\r
64             };\r
65             this.node.onHide = function() {\r
66                 view.scrollbarPosition = view.node.find('#rng-module-documentCanvas-contentWrapper').scrollTop();\r
67             }\r
68                       \r
69             this.gridToggled = false;\r
70         },\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
76             if(wlxmlClass)\r
77                 toret.attr('wlxml-class', wlxmlClass);\r
78             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
79             return toret;\r
80         },\r
81         insertNewNode: function(wlxmlTag, wlxmlClass) {\r
82             //TODO: Insert inline\r
83             var anchor = $(window.getSelection().anchorNode);\r
84             var anchorOffset = window.getSelection().anchorOffset;\r
85             if(anchor[0].nodeType === Node.TEXT_NODE)\r
86                 anchor = anchor.parent();\r
87             if(anchor.text() === '') {\r
88                 var todel = anchor;\r
89                 anchor = anchor.parent();\r
90                 todel.remove();\r
91             }\r
92             if(anchorOffset > 0 && anchorOffset < anchor.text().length) {\r
93                 if(wlxmlTag === null && wlxmlClass === null) {\r
94                     return this.splitWithNewNode(anchor);\r
95                 }\r
96                 return this.wrapSelectionWithNewNode(wlxmlTag, wlxmlClass);\r
97             }\r
98             var newNode = this._createNode(wlxmlTag || anchor.attr('wlxml-tag'), wlxmlClass || anchor.attr('wlxml-class'));\r
99             if(anchorOffset === 0)\r
100                 anchor.before(newNode)\r
101             else\r
102                 anchor.after(newNode);\r
103             this.selectNode(new wlxmlNode.Node(newNode), {movecaret: true});\r
104             //isDirty = true;\r
105             sandbox.publish('contentChanged');\r
106         },\r
107         wrapSelectionWithNewNode: function(wlxmlTag, wlxmlClass) {\r
108             var selection = window.getSelection();\r
109             if(selection.anchorNode === selection.focusNode && selection.anchorNode.nodeType === Node.TEXT_NODE) {\r
110                 var startOffset = selection.anchorOffset;\r
111                 var endOffset = selection.focusOffset;\r
112                 if(startOffset > endOffset) {\r
113                     var tmp = startOffset;\r
114                     startOffset = endOffset;\r
115                     endOffset = tmp;\r
116                 }\r
117                 var node = selection.anchorNode;\r
118                 var prefix = node.data.substr(0, startOffset);\r
119                 var suffix = node.data.substr(endOffset);\r
120                 var core = node.data.substr(startOffset, endOffset - startOffset);\r
121                 var newNode = this._createNode(wlxmlTag, wlxmlClass);\r
122                 newNode.text(core || 'test');\r
123                 $(node).replaceWith(newNode);\r
124                 newNode.before(prefix);\r
125                 newNode.after(suffix);\r
126                 \r
127                 this.selectNode(new wlxmlNode.Node(newNode), {movecaret: true});\r
128                 //isDirty = true;\r
129                 sandbox.publish('contentChanged');\r
130             }\r
131         },\r
132         splitWithNewNode: function(node) {\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
140                     endOffset = tmp;\r
141                 }\r
142                 var anchor = selection.anchorNode;\r
143                 var prefix = anchor.data.substr(0, startOffset);\r
144                 var suffix = anchor.data.substr(endOffset);\r
145                 var prefixNode = this._createNode(node.attr('wlxml-tag'), node.attr('wlxml-class'));\r
146                 var newNode = this._createNode(node.attr('wlxml-tag'), node.attr('wlxml-class'));\r
147                 var suffixNode = this._createNode(node.attr('wlxml-tag'), node.attr('wlxml-class'));\r
148                 prefixNode.text(prefix);\r
149                 suffixNode.text(suffix);\r
150                 node.replaceWith(newNode);\r
151                 newNode.before(prefixNode);\r
152                 newNode.after(suffixNode);\r
153                 \r
154                 this.selectNode(new wlxmlNode.Node(newNode), {movecaret: true});\r
155                 //isDirty = true;\r
156                 sandbox.publish('contentChanged');\r
157             }\r
158         },\r
159         setBody: function(HTMLTree) {\r
160             this.node.find('#rng-module-documentCanvas-content').html(HTMLTree);\r
161         },\r
162         getBody: function() {\r
163             return this.node.find('#rng-module-documentCanvas-content').html();\r
164         }, \r
165         selectNode: function(wlxmlNode, options) {\r
166             options = options || {};\r
167             var nodeElement = this.getNodeElement(wlxmlNode)\r
168             \r
169             this.dimNode(wlxmlNode);\r
170             \r
171             this.node.find('.rng-module-documentCanvas-currentNode').removeClass('rng-module-documentCanvas-currentNode');\r
172             nodeElement.addClass('rng-module-documentCanvas-currentNode');\r
173              \r
174             if(options.movecaret) {\r
175                 this.movecaretToNode(nodeElement);\r
176             }\r
177             \r
178             this.currentNode = wlxmlNode;\r
179             sandbox.publish('nodeSelected', wlxmlNode);\r
180         },\r
181         highlightNode: function(wlxmlNode) {\r
182             var nodeElement = this.getNodeElement(wlxmlNode);\r
183             if(!this.gridToggled) {\r
184                 nodeElement.addClass('rng-common-hoveredNode');\r
185                 var label = nodeElement.attr('wlxml-tag');\r
186                 if(nodeElement.attr('wlxml-class'))\r
187                     label += ' / ' + nodeElement.attr('wlxml-class');\r
188                 var tag = $('<div>').addClass('rng-module-documentCanvas-hoveredNodeTag').text(label);\r
189                 nodeElement.append(tag);\r
190             }\r
191         },\r
192         dimNode: function(wlxmlNode) {\r
193             var nodeElement = this.getNodeElement(wlxmlNode);\r
194             if(!this.gridToggled) {\r
195                 nodeElement.removeClass('rng-common-hoveredNode');\r
196                 nodeElement.find('.rng-module-documentCanvas-hoveredNodeTag').remove();\r
197             }\r
198         },\r
199         selectFirstNode: function() {\r
200             var firstNodeWithText = this.node.find('[wlxml-tag]').filter(function() {\r
201                 return $(this).clone().children().remove().end().text().trim() !== '';\r
202             }).first();\r
203             var node;\r
204             if(firstNodeWithText.length)\r
205                 node = $(firstNodeWithText[0])\r
206             else {\r
207                 node = this.node.find('[wlxml-class|="p"]')\r
208             }\r
209             this.selectNode(new wlxmlNode.Node(node), {movecaret: true});\r
210         },\r
211         toggleGrid: function(toggle) {\r
212             this.node.find('[wlxml-tag]').toggleClass('rng-common-hoveredNode', toggle);\r
213             this.gridToggled = toggle;\r
214         },\r
215         getNodeElement: function(wlxmlNode) {\r
216             return this.node.find('#'+wlxmlNode.id);\r
217         },\r
218         movecaretToNode: function(nodeElement) {\r
219             var range = document.createRange();\r
220             range.selectNodeContents(nodeElement[0]);\r
221             range.collapse(false);\r
222             var selection = document.getSelection();\r
223             selection.removeAllRanges()\r
224             selection.addRange(range);\r
225         }\r
226     };\r
227     \r
228     view.setup();\r
229 \r
230     /* public api */\r
231     return {\r
232         start: function() { sandbox.publish('ready'); },\r
233         getView: function() { \r
234             return view.node;\r
235         },\r
236         setDocument: function(xml) {\r
237             var transformed = transformations.fromXML.getDocumentDescription(xml);\r
238             view.setBody(transformed.HTMLTree);\r
239             sandbox.publish('documentSet');\r
240         },\r
241         getDocument: function() {\r
242             return transformations.toXML.getXML(view.getBody());\r
243         },\r
244         modifyCurrentNode: function(attr, value) {\r
245             if(view.currentNode) {\r
246                 view.getNodeElement(view.currentNode).attr('wlxml-'+attr, value);\r
247                 sandbox.publish('contentChanged');\r
248             }\r
249         },\r
250         highlightNode: function(wlxmlNode) {\r
251             view.highlightNode(wlxmlNode);\r
252         },\r
253         dimNode: function(wlxmlNode) {\r
254             view.dimNode(wlxmlNode);\r
255         },\r
256         selectNode: function(wlxmlNode) {\r
257             if(!wlxmlNode.is(view.currentNode))\r
258                 view.selectNode(wlxmlNode, {movecaret: true});\r
259         },\r
260         toggleGrid: function(toggle) {\r
261             view.toggleGrid(toggle);\r
262         },\r
263         insertNewNode: function(wlxmlTag, wlxmlClass) {\r
264             view.insertNewNode(wlxmlTag, wlxmlClass);\r
265         },\r
266         wrapSelectionWithNewNode: function(wlxmlTag, wlxmlClass) {\r
267             view.wrapSelectionWithNewNode(wlxmlTag, wlxmlClass);\r
268         }\r
269     }\r
270     \r
271 };\r
272 \r
273 });