Layout and tabs view informs its views about being hidden
[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         setup: function() {\r
18             var view = this;\r
19 \r
20             this.node.find('#rng-module-documentCanvas-content').on('keyup', function() {\r
21                 //isDirty = true;\r
22                 sandbox.publish('contentChanged');\r
23             });\r
24 \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
28             });\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
32             });\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
37             });\r
38 \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
44                     return;\r
45                 view.selectNode(new wlxmlNode.Node(anchor));\r
46             });\r
47             \r
48             this.node.on('keydown', '#rng-module-documentCanvas-contentWrapper', function(e) {\r
49                 if(e.which === 13) { \r
50                     e.preventDefault();\r
51                     view.insertNewNode(null, null);\r
52                 }\r
53             });\r
54                       \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.movecaretToNode(view.getNodeElement(view.currentNode));\r
61                 }\r
62             };\r
63                       \r
64             this.gridToggled = false;\r
65         },\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
71             if(wlxmlClass)\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
74             return toret;\r
75         },\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
83                 var todel = anchor;\r
84                 anchor = anchor.parent();\r
85                 todel.remove();\r
86             }\r
87             if(anchorOffset > 0 && anchorOffset < anchor.text().length) {\r
88                 if(wlxmlTag === null && wlxmlClass === null) {\r
89                     return this.splitWithNewNode(anchor);\r
90                 }\r
91                 return this.wrapSelectionWithNewNode(wlxmlTag, wlxmlClass);\r
92             }\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
96             else\r
97                 anchor.after(newNode);\r
98             this.selectNode(new wlxmlNode.Node(newNode), {movecaret: true});\r
99             //isDirty = true;\r
100             sandbox.publish('contentChanged');\r
101         },\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
110                     endOffset = tmp;\r
111                 }\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
121                 \r
122                 this.selectNode(new wlxmlNode.Node(newNode), {movecaret: true});\r
123                 //isDirty = true;\r
124                 sandbox.publish('contentChanged');\r
125             }\r
126         },\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
135                     endOffset = tmp;\r
136                 }\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
148                 \r
149                 this.selectNode(new wlxmlNode.Node(newNode), {movecaret: true});\r
150                 //isDirty = true;\r
151                 sandbox.publish('contentChanged');\r
152             }\r
153         },\r
154         setBody: function(HTMLTree) {\r
155             this.node.find('#rng-module-documentCanvas-content').html(HTMLTree);\r
156         },\r
157         getBody: function() {\r
158             return this.node.find('#rng-module-documentCanvas-content').html();\r
159         }, \r
160         selectNode: function(wlxmlNode, options) {\r
161             options = options || {};\r
162             var nodeElement = this.getNodeElement(wlxmlNode)\r
163             \r
164             this.dimNode(wlxmlNode);\r
165             \r
166             this.node.find('.rng-module-documentCanvas-currentNode').removeClass('rng-module-documentCanvas-currentNode');\r
167             nodeElement.addClass('rng-module-documentCanvas-currentNode');\r
168              \r
169             if(options.movecaret) {\r
170                 this.movecaretToNode(nodeElement);\r
171             }\r
172             \r
173             this.currentNode = wlxmlNode;\r
174             sandbox.publish('nodeSelected', wlxmlNode);\r
175         },\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
185             }\r
186         },\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
192             }\r
193         },\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
197             }).first();\r
198             var node;\r
199             if(firstNodeWithText.length)\r
200                 node = $(firstNodeWithText[0])\r
201             else {\r
202                 node = this.node.find('[wlxml-class|="p"]')\r
203             }\r
204             this.selectNode(new wlxmlNode.Node(node), {movecaret: true});\r
205         },\r
206         toggleGrid: function(toggle) {\r
207             this.node.find('[wlxml-tag]').toggleClass('rng-common-hoveredNode', toggle);\r
208             this.gridToggled = toggle;\r
209         },\r
210         getNodeElement: function(wlxmlNode) {\r
211             return this.node.find('#'+wlxmlNode.id);\r
212         },\r
213         movecaretToNode: 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
220         }\r
221     };\r
222     \r
223     view.setup();\r
224 \r
225     /* public api */\r
226     return {\r
227         start: function() { sandbox.publish('ready'); },\r
228         getView: function() { \r
229             return view.node;\r
230         },\r
231         setDocument: function(xml) {\r
232             var transformed = transformations.fromXML.getDocumentDescription(xml);\r
233             view.setBody(transformed.HTMLTree);\r
234             sandbox.publish('documentSet');\r
235         },\r
236         getDocument: function() {\r
237             return transformations.toXML.getXML(view.getBody());\r
238         },\r
239         modifyCurrentNode: function(attr, value) {\r
240             if(view.currentNode) {\r
241                 view.getNodeElement(view.currentNode).attr('wlxml-'+attr, value);\r
242                 sandbox.publish('contentChanged');\r
243             }\r
244         },\r
245         highlightNode: function(wlxmlNode) {\r
246             view.highlightNode(wlxmlNode);\r
247         },\r
248         dimNode: function(wlxmlNode) {\r
249             view.dimNode(wlxmlNode);\r
250         },\r
251         selectNode: function(wlxmlNode) {\r
252             if(!wlxmlNode.is(view.currentNode))\r
253                 view.selectNode(wlxmlNode, {movecaret: true});\r
254         },\r
255         toggleGrid: function(toggle) {\r
256             view.toggleGrid(toggle);\r
257         },\r
258         insertNewNode: function(wlxmlTag, wlxmlClass) {\r
259             view.insertNewNode(wlxmlTag, wlxmlClass);\r
260         },\r
261         wrapSelectionWithNewNode: function(wlxmlTag, wlxmlClass) {\r
262             view.wrapSelectionWithNewNode(wlxmlTag, wlxmlClass);\r
263         }\r
264     }\r
265     \r
266 };\r
267 \r
268 });