Layout informs its views on being shown
[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         setup: function() {\r
17             var view = this;\r
18 \r
19             this.node.find('#rng-module-documentCanvas-content').on('keyup', function() {\r
20                 //isDirty = true;\r
21                 sandbox.publish('contentChanged');\r
22             });\r
23 \r
24             this.node.on('mouseover', '[wlxml-tag]', function(e) {\r
25                 e.stopPropagation();\r
26                 sandbox.publish('nodeHovered', new wlxmlNode.Node($(e.target)));\r
27             });\r
28             this.node.on('mouseout', '[wlxml-tag]', function(e) {\r
29                 e.stopPropagation();\r
30                 sandbox.publish('nodeBlured', new wlxmlNode.Node($(e.target)));\r
31             });\r
32             this.node.on('click', '[wlxml-tag]', function(e) {\r
33                 e.stopPropagation();\r
34                 console.log('clicked node type: '+e.target.nodeType);\r
35                 view.selectNode(new wlxmlNode.Node($(e.target)));\r
36             });\r
37 \r
38             this.node.on('keyup', '#rng-module-documentCanvas-contentWrapper', function(e) {\r
39                 var anchor = $(window.getSelection().anchorNode);\r
40                 if(anchor[0].nodeType === Node.TEXT_NODE)\r
41                     anchor = anchor.parent();\r
42                 if(!anchor.is('[wlxml-tag]'))\r
43                     return;\r
44                 view.selectNode(new wlxmlNode.Node(anchor));\r
45             });\r
46             \r
47             this.node.on('keydown', '#rng-module-documentCanvas-contentWrapper', function(e) {\r
48                 if(e.which === 13) { \r
49                     e.preventDefault();\r
50                     view.insertNewNode(null, null);\r
51                 }\r
52             });\r
53             this.gridToggled = false;\r
54         },\r
55         _createNode: function(wlxmlTag, wlxmlClass) {\r
56             var toBlock = ['div', 'document', 'section', 'header'];\r
57             var htmlTag = _.contains(toBlock, wlxmlTag) ? 'div' : 'span';\r
58             var toret = $('<' + htmlTag + '>');\r
59             toret.attr('wlxml-tag', wlxmlTag);\r
60             if(wlxmlClass)\r
61                 toret.attr('wlxml-class', wlxmlClass);\r
62             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
63             return toret;\r
64         },\r
65         insertNewNode: function(wlxmlTag, wlxmlClass) {\r
66             //TODO: Insert inline\r
67             var anchor = $(window.getSelection().anchorNode);\r
68             var anchorOffset = window.getSelection().anchorOffset;\r
69             if(anchor[0].nodeType === Node.TEXT_NODE)\r
70                 anchor = anchor.parent();\r
71             if(anchor.text() === '') {\r
72                 var todel = anchor;\r
73                 anchor = anchor.parent();\r
74                 todel.remove();\r
75             }\r
76             if(anchorOffset > 0 && anchorOffset < anchor.text().length) {\r
77                 if(wlxmlTag === null && wlxmlClass === null) {\r
78                     return this.splitWithNewNode(anchor);\r
79                 }\r
80                 return this.wrapSelectionWithNewNode(wlxmlTag, wlxmlClass);\r
81             }\r
82             var newNode = this._createNode(wlxmlTag || anchor.attr('wlxml-tag'), wlxmlClass || anchor.attr('wlxml-class'));\r
83             if(anchorOffset === 0)\r
84                 anchor.before(newNode)\r
85             else\r
86                 anchor.after(newNode);\r
87             this.selectNode(new wlxmlNode.Node(newNode), {moveCarret: true});\r
88             //isDirty = true;\r
89             sandbox.publish('contentChanged');\r
90         },\r
91         wrapSelectionWithNewNode: function(wlxmlTag, wlxmlClass) {\r
92             var selection = window.getSelection();\r
93             if(selection.anchorNode === selection.focusNode && selection.anchorNode.nodeType === Node.TEXT_NODE) {\r
94                 var startOffset = selection.anchorOffset;\r
95                 var endOffset = selection.focusOffset;\r
96                 if(startOffset > endOffset) {\r
97                     var tmp = startOffset;\r
98                     startOffset = endOffset;\r
99                     endOffset = tmp;\r
100                 }\r
101                 var node = selection.anchorNode;\r
102                 var prefix = node.data.substr(0, startOffset);\r
103                 var suffix = node.data.substr(endOffset);\r
104                 var core = node.data.substr(startOffset, endOffset - startOffset);\r
105                 var newNode = this._createNode(wlxmlTag, wlxmlClass);\r
106                 newNode.text(core || 'test');\r
107                 $(node).replaceWith(newNode);\r
108                 newNode.before(prefix);\r
109                 newNode.after(suffix);\r
110                 \r
111                 this.selectNode(new wlxmlNode.Node(newNode), {moveCarret: true});\r
112                 //isDirty = true;\r
113                 sandbox.publish('contentChanged');\r
114             }\r
115         },\r
116         splitWithNewNode: function(node) {\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 anchor = selection.anchorNode;\r
127                 var prefix = anchor.data.substr(0, startOffset);\r
128                 var suffix = anchor.data.substr(endOffset);\r
129                 var prefixNode = this._createNode(node.attr('wlxml-tag'), node.attr('wlxml-class'));\r
130                 var newNode = this._createNode(node.attr('wlxml-tag'), node.attr('wlxml-class'));\r
131                 var suffixNode = this._createNode(node.attr('wlxml-tag'), node.attr('wlxml-class'));\r
132                 prefixNode.text(prefix);\r
133                 suffixNode.text(suffix);\r
134                 node.replaceWith(newNode);\r
135                 newNode.before(prefixNode);\r
136                 newNode.after(suffixNode);\r
137                 \r
138                 this.selectNode(new wlxmlNode.Node(newNode), {moveCarret: true});\r
139                 //isDirty = true;\r
140                 sandbox.publish('contentChanged');\r
141             }\r
142         },\r
143         setBody: function(HTMLTree) {\r
144             this.node.find('#rng-module-documentCanvas-content').html(HTMLTree);\r
145         },\r
146         getBody: function() {\r
147             return this.node.find('#rng-module-documentCanvas-content').html();\r
148         }, \r
149         selectNode: function(wlxmlNode, options) {\r
150             options = options || {};\r
151             var nodeElement = this.getNodeElement(wlxmlNode)\r
152             \r
153             this.dimNode(wlxmlNode);\r
154             \r
155             this.node.find('.rng-module-documentCanvas-currentNode').removeClass('rng-module-documentCanvas-currentNode');\r
156             nodeElement.addClass('rng-module-documentCanvas-currentNode');\r
157              \r
158             if(options.moveCarret) {\r
159                 var range = document.createRange();\r
160                 range.selectNodeContents(nodeElement[0]);\r
161                 range.collapse(false);\r
162                 var selection = document.getSelection();\r
163                 selection.removeAllRanges()\r
164                 selection.addRange(range);\r
165             }\r
166             \r
167             this.currentNode = wlxmlNode;\r
168             sandbox.publish('nodeSelected', wlxmlNode);\r
169         },\r
170         highlightNode: function(wlxmlNode) {\r
171             var nodeElement = this.getNodeElement(wlxmlNode);\r
172             if(!this.gridToggled) {\r
173                 nodeElement.addClass('rng-common-hoveredNode');\r
174                 var label = nodeElement.attr('wlxml-tag');\r
175                 if(nodeElement.attr('wlxml-class'))\r
176                     label += ' / ' + nodeElement.attr('wlxml-class');\r
177                 var tag = $('<div>').addClass('rng-module-documentCanvas-hoveredNodeTag').text(label);\r
178                 nodeElement.append(tag);\r
179             }\r
180         },\r
181         dimNode: function(wlxmlNode) {\r
182             var nodeElement = this.getNodeElement(wlxmlNode);\r
183             if(!this.gridToggled) {\r
184                 nodeElement.removeClass('rng-common-hoveredNode');\r
185                 nodeElement.find('.rng-module-documentCanvas-hoveredNodeTag').remove();\r
186             }\r
187         },\r
188         selectFirstNode: function() {\r
189             var firstNodeWithText = this.node.find('[wlxml-tag]').filter(function() {\r
190                 return $(this).clone().children().remove().end().text().trim() !== '';\r
191             }).first();\r
192             var node;\r
193             if(firstNodeWithText.length)\r
194                 node = $(firstNodeWithText[0])\r
195             else {\r
196                 node = this.node.find('[wlxml-class|="p"]')\r
197             }\r
198             this.selectNode(new wlxmlNode.Node(node));\r
199         },\r
200         toggleGrid: function(toggle) {\r
201             this.node.find('[wlxml-tag]').toggleClass('rng-common-hoveredNode', toggle);\r
202             this.gridToggled = toggle;\r
203         },\r
204         getNodeElement: function(wlxmlNode) {\r
205             return this.node.find('#'+wlxmlNode.id);\r
206         }\r
207     };\r
208     \r
209     view.setup();\r
210 \r
211     /* public api */\r
212     return {\r
213         start: function() { sandbox.publish('ready'); },\r
214         getView: function() { return view.node; },\r
215         setDocument: function(xml) {\r
216             var transformed = transformations.fromXML.getDocumentDescription(xml);\r
217             view.setBody(transformed.HTMLTree);\r
218             view.selectFirstNode();\r
219             sandbox.publish('documentSet');\r
220         },\r
221         getDocument: function() {\r
222             return transformations.toXML.getXML(view.getBody());\r
223         },\r
224         modifyCurrentNode: function(attr, value) {\r
225             if(view.currentNode) {\r
226                 view.getNodeElement(view.currentNode).attr('wlxml-'+attr, value);\r
227                 sandbox.publish('contentChanged');\r
228             }\r
229         },\r
230         highlightNode: function(wlxmlNode) {\r
231             view.highlightNode(wlxmlNode);\r
232         },\r
233         dimNode: function(wlxmlNode) {\r
234             view.dimNode(wlxmlNode);\r
235         },\r
236         selectNode: function(wlxmlNode) {\r
237             if(!wlxmlNode.is(view.currentNode))\r
238                 view.selectNode(wlxmlNode, {moveCarret: true});\r
239         },\r
240         toggleGrid: function(toggle) {\r
241             view.toggleGrid(toggle);\r
242         },\r
243         insertNewNode: function(wlxmlTag, wlxmlClass) {\r
244             view.insertNewNode(wlxmlTag, wlxmlClass);\r
245         },\r
246         wrapSelectionWithNewNode: function(wlxmlTag, wlxmlClass) {\r
247             view.wrapSelectionWithNewNode(wlxmlTag, wlxmlClass);\r
248         }\r
249     }\r
250     \r
251 };\r
252 \r
253 });