1b919eeadb61429092066db085ef0807092ad316
[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 \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             \r
54             \r
55             var observer = new MutationObserver(function(mutations) {\r
56               mutations.forEach(function(mutation) {\r
57                 _.each(mutation.addedNodes, function(node) {\r
58                     node = $(node);\r
59                     node.parent().find('[wlxml-tag]').each(function() {\r
60                         tag = $(this);\r
61                         if(!tag.attr('id'))\r
62                             tag.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                     });\r
64                 });\r
65               });    \r
66             });\r
67             var config = { attributes: true, childList: true, characterData: true, subtree: true };\r
68             observer.observe(this.node.find('#rng-module-documentCanvas-contentWrapper')[0], config);\r
69             \r
70             this.gridToggled = false;\r
71         },\r
72         _createNode: function(wlxmlTag, wlxmlClass) {\r
73             var toBlock = ['div', 'document', 'section', 'header'];\r
74             var htmlTag = _.contains(toBlock, wlxmlTag) ? 'div' : 'span';\r
75             var toret = $('<' + htmlTag + '>');\r
76             toret.attr('wlxml-tag', wlxmlTag);\r
77             if(wlxmlClass)\r
78                 toret.attr('wlxml-class', wlxmlClass);\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(newNode);\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(newNode);\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(newNode);\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(node, options) {\r
166             options = options || {};\r
167             this.dimNode(node);\r
168             \r
169             this.node.find('.rng-module-documentCanvas-currentNode').removeClass('rng-module-documentCanvas-currentNode');\r
170             this.getNodeElement(node).addClass('rng-module-documentCanvas-currentNode');\r
171             this.currentNode = node;\r
172             \r
173             if(options.doFocus) {\r
174                 var range = document.createRange();\r
175                 range.selectNodeContents(this.getNodeElement(node)[0]);\r
176                 range.collapse(false);\r
177 \r
178                 var selection = document.getSelection();\r
179                 selection.removeAllRanges()\r
180                 selection.addRange(range);\r
181             }\r
182             \r
183             sandbox.publish('nodeSelected', node);\r
184         },\r
185         highlightNode: function(node) {\r
186             node = this.getNodeElement(node);\r
187             if(!this.gridToggled) {\r
188                 node.addClass('rng-common-hoveredNode');\r
189                 var label = node.attr('wlxml-tag');\r
190                 if(node.attr('wlxml-class'))\r
191                     label += ' / ' + node.attr('wlxml-class');\r
192                 var tag = $('<div>').addClass('rng-module-documentCanvas-hoveredNodeTag').text(label);\r
193                 node.append(tag);\r
194             }\r
195         },\r
196         dimNode: function(node) {\r
197             node = this.getNodeElement(node);\r
198             if(!this.gridToggled) {\r
199                 node.removeClass('rng-common-hoveredNode');\r
200                 node.find('.rng-module-documentCanvas-hoveredNodeTag').remove();\r
201             }\r
202         },\r
203         selectFirstNode: function() {\r
204             var firstNodeWithText = this.node.find('[wlxml-tag]').filter(function() {\r
205                 return $(this).clone().children().remove().end().text().trim() !== '';\r
206             }).first();\r
207             var node;\r
208             if(firstNodeWithText.length)\r
209                 node = $(firstNodeWithText[0])\r
210             else {\r
211                 node = this.node.find('[wlxml-class|="p"]')\r
212             }\r
213             this.selectNode(new wlxmlNode.Node(node));\r
214         },\r
215         toggleGrid: function(toggle) {\r
216             this.node.find('[wlxml-tag]').toggleClass('rng-common-hoveredNode', toggle);\r
217             this.gridToggled = toggle;\r
218         },\r
219         getNodeElement: function(wlxmlNode) {\r
220             return this.node.find('#'+wlxmlNode.id);\r
221         }\r
222     };\r
223     \r
224     view.setup();\r
225 \r
226     /* public api */\r
227     return {\r
228         start: function() { sandbox.publish('ready'); },\r
229         getView: function() { return view.node; },\r
230         setDocument: function(xml) {\r
231             var transformed = transformations.fromXML.getDocumentDescription(xml);\r
232             view.setBody(transformed.HTMLTree);\r
233             view.selectFirstNode();\r
234             //isDirty = false;\r
235         },\r
236         modifyCurrentNode: function(attr, value) {\r
237             if(view.currentNode)\r
238                 view.currentNode.attr('wlxml-'+attr, value);\r
239         },\r
240         highlightNode: function(wlxmlNode) {\r
241             view.highlightNode(wlxmlNode);\r
242         },\r
243         dimNode: function(wlxmlNode) {\r
244             view.dimNode(wlxmlNode);\r
245         },\r
246         selectNode: function(wlxmlNode) {\r
247             view.selectNode(wlxmlNode, {doFocus: true});\r
248         },\r
249         toggleGrid: function(toggle) {\r
250             view.toggleGrid(toggle);\r
251         },\r
252         insertNewNode: function(wlxmlTag, wlxmlClass) {\r
253             view.insertNewNode(wlxmlTag, wlxmlClass);\r
254         },\r
255         wrapSelectionWithNewNode: function(wlxmlTag, wlxmlClass) {\r
256             view.wrapSelectionWithNewNode(wlxmlTag, wlxmlClass);\r
257         }\r
258     }\r
259     \r
260 };\r
261 \r
262 });