702c812dadff0c5e54d05ca705d77bb474f71e40
[fnpeditor.git] / modules / visualEditor.js
1 rng.modules.visualEditor = function(sandbox) {\r
2     var transformations = rng.modules.visualEditor.transformations;\r
3 \r
4     var view = {\r
5         node: $(sandbox.getTemplate('main')()),\r
6         currentNode: null,\r
7         setup: function() {\r
8             var view = this;\r
9
10             this.node.find('#rng-visualEditor-content').on('keyup', function() {\r
11                 isDirty = true;\r
12             });\r
13             \r
14             this.node.find('#rng-visualEditor-meta').on('keyup', function() {\r
15                 isDirty = true;\r
16             });\r
17
18             this.node.on('mouseover', '[wlxml-tag]', function(e) { $(e.target).addClass('rng-hover')});\r
19             this.node.on('mouseout', '[wlxml-tag]', function(e) { $(e.target).removeClass('rng-hover')});\r
20             this.node.on('click', '[wlxml-tag]', function(e) {\r
21                 console.log('clicked node type: '+e.target.nodeType);\r
22                 view._markSelected($(e.target));\r
23             });\r
24 \r
25             this.node.on('keyup', '#rng-visualEditor-contentWrapper', function(e) {\r
26                 var anchor = $(window.getSelection().anchorNode);\r
27                 if(anchor[0].nodeType === Node.TEXT_NODE)\r
28                     anchor = anchor.parent();\r
29                 if(!anchor.is('[wlxml-tag]'))\r
30                     return;\r
31                 view._markSelected(anchor);\r
32             });\r
33             \r
34             this.node.on('keydown', '#rng-visualEditor-contentWrapper', function(e) {\r
35                 if(e.which === 13) { \r
36                     e.preventDefault();\r
37                     var anchor = $(window.getSelection().anchorNode);\r
38                     if(anchor[0].nodeType === Node.TEXT_NODE)\r
39                         anchor = anchor.parent();\r
40                     var newNode = anchor.clone().empty();\r
41                     anchor.after(newNode);\r
42                     view.selectNode(newNode);\r
43                 }\r
44             });\r
45             \r
46             \r
47             var metaTable = this.metaTable = this.node.find('#rng-visualEditor-meta table');\r
48             \r
49             this.node.find('.rng-visualEditor-metaAddBtn').click(function() {\r
50                 var newRow = view._addMetaRow('', '');\r
51                 $(newRow.find('td div')[0]).focus();\r
52                 isDirty = true;\r
53             });\r
54             \r
55             this.metaTable.on('click', '.rng-visualEditor-metaRemoveBtn', function(e) {\r
56                 $(e.target).closest('tr').remove();\r
57                 isDirty = true;\r
58             });\r
59             \r
60             this.metaTable.on('keydown', '[contenteditable]', function(e) {\r
61                 console.log(e.which);\r
62                 if(e.which === 13) { \r
63                     if($(document.activeElement).hasClass('rng-visualEditor-metaItemKey')) {\r
64                         metaTable.find('.rng-visualEditor-metaItemValue').focus();\r
65                     } else {\r
66                         var input = $('<input>');\r
67                         input.appendTo('body').focus()\r
68                         view.node.find('.rng-visualEditor-metaAddBtn').focus();\r
69                         input.remove();\r
70                     }\r
71                     e.preventDefault();\r
72                 }\r
73                 \r
74             });\r
75
76         },\r
77         getMetaData: function() {\r
78             var toret = {};\r
79             this.metaTable.find('tr').each(function() {\r
80                 var tr = $(this);\r
81                 var inputs = $(this).find('td [contenteditable]');\r
82                 var key = $(inputs[0]).text();\r
83                 var value = $(inputs[1]).text();\r
84                 toret[key] = value;\r
85             });\r
86             console.log(toret);\r
87             return toret;\r
88         },\r
89         setMetaData: function(metadata) {\r
90             var view = this;\r
91             this.metaTable.find('tr').remove();\r
92             _.each(_.keys(metadata), function(key) {    \r
93                 view._addMetaRow(key, metadata[key]);\r
94             });\r
95         },\r
96         setBody: function(HTMLTree) {\r
97             this.node.find('#rng-visualEditor-content').html(HTMLTree);\r
98         },\r
99         getBody: function() {\r
100             return this.node.find('#rng-visualEditor-content').html();\r
101         }, \r
102         _markSelected: function(node) {\r
103             this.node.find('.rng-current').removeClass('rng-current');\r
104             node.addClass('rng-current');\r
105             this.currentNode = node;\r
106             mediator.nodeSelected(node);\r
107         },\r
108         selectNode: function(node) {\r
109             view._markSelected(node);\r
110             var range = document.createRange();\r
111             range.selectNodeContents(node[0]);\r
112             range.collapse(false);\r
113 \r
114             var selection = document.getSelection();\r
115             selection.removeAllRanges()\r
116             selection.addRange(range);\r
117         },\r
118         selectFirstNode: function() {\r
119             var firstNodeWithText = this.node.find('[wlxml-tag]').filter(function() {\r
120                 return $(this).clone().children().remove().end().text().trim() !== '';\r
121             }).first();\r
122             var node;\r
123             if(firstNodeWithText.length)\r
124                 node = $(firstNodeWithText[0])\r
125             else {\r
126                 node = this.node.find('[wlxml-class|="p"]')\r
127             }\r
128             this.selectNode(node);\r
129         },\r
130         _addMetaRow: function(key, value) {\r
131             var newRow = $(sandbox.getTemplate('metaItem')({key: key || '', value: value || ''}));\r
132             newRow.appendTo(this.metaTable);\r
133             return newRow;\r
134         }\r
135     };\r
136     \r
137     \r
138     var sideBarView = {\r
139         node: view.node.find('#rng-visualEditor-sidebar'),\r
140         setup: function() {\r
141             var view = this;\r
142             this.node.find('#rng-visualEditor-sidebarButtons a').click(function(e) {\r
143                 e.preventDefault();\r
144                 var target = $(e.currentTarget);\r
145                 if(!target.attr('data-content-id'))\r
146                     return;\r
147                 view.selectTab(target.attr('data-content-id'));\r
148             });\r
149             view.selectTab('rng-visualEditor-edit');\r
150             \r
151             view.node.on('change', '.rng-visualEditor-editPaneNodeForm select', function(e) {\r
152                 var target = $(e.target);\r
153                 var attr = target.attr('id').split('-')[2].split('editPane')[1].substr(0,3) === 'Tag' ? 'tag' : 'class';\r
154                 mediator.getCurrentNode().attr('wlxml-'+attr, target.val());\r
155                 isDirty = true;\r
156             });\r
157             \r
158             view.node.on('change', '.rng-visualEditor-editPaneSelectionForm select', function(e) {\r
159                 var target = $(e.target);\r
160                 var selection = window.getSelection();\r
161                 if(selection.anchorNode === selection.focusNode && selection.anchorNode.nodeType === Node.TEXT_NODE) {\r
162                     var startOffset = selection.anchorOffset;\r
163                     var endOffset = selection.focusOffset;\r
164                     if(startOffset > endOffset) {\r
165                         var tmp = startOffset;\r
166                         startOffset = endOffset;\r
167                         endOffset = tmp;\r
168                     }\r
169                     var node = selection.anchorNode;\r
170                     var prefix = node.data.substr(0, startOffset);\r
171                     var suffix = node.data.substr(endOffset);\r
172                     var core = node.data.substr(startOffset, endOffset - startOffset);\r
173                     var newNode = $('<span wlxml-tag="' + target.val() + '">' + core + '</span>');\r
174                     $(node).replaceWith(newNode);\r
175                     newNode.before(prefix);\r
176                     newNode.after(suffix);\r
177                     mediator.nodeCreated(newNode);\r
178                     isDirty = true;\r
179                 }\r
180             });\r
181         },\r
182         selectTab: function(id) {\r
183            this.node.find('.rng-visualEditor-sidebarContentItem').hide();\r
184            this.node.find('#'+id).show();\r
185            this.node.find('#rng-visualEditor-sidebarButtons li').removeClass('active');\r
186            this.node.find('#rng-visualEditor-sidebarButtons li a[data-content-id=' + id + ']').parent().addClass('active');\r
187         \r
188         },\r
189         updateEditPane: function(node) {\r
190             var pane = this.node.find('#rng-visualEditor-edit');\r
191             pane.html( $(sandbox.getTemplate('editPane')({tag: node.attr('wlxml-tag'), klass: node.attr('wlxml-class')})));\r
192         }\r
193     }\r
194     \r
195     view.setup();\r
196     sideBarView.setup();\r
197     \r
198     var mediator = {\r
199         getCurrentNode: function() {\r
200             return view.currentNode;\r
201         },\r
202         nodeCreated: function(node) {\r
203             view.selectNode(node);\r
204             \r
205         },\r
206         nodeSelected: function(node) {\r
207             sideBarView.updateEditPane(node);\r
208         }\r
209     }\r
210     \r
211     var isDirty = false;\r
212     var wasShownAlready = false;\r
213     \r
214     \r
215     return {\r
216         start: function() {\r
217             sandbox.publish('ready');\r
218         },\r
219         getView: function() {\r
220             return view.node;\r
221         },\r
222         setDocument: function(xml) {\r
223             var transformed = transformations.fromXML.getDocumentDescription(xml);\r
224             view.setBody(transformed.HTMLTree);\r
225             view.setMetaData(transformed.metadata);\r
226             isDirty = false;\r
227         },\r
228         getDocument: function() {\r
229             return transformations.toXML.getXML({HTMLTree: view.getBody(), metadata: view.getMetaData()});\r
230         },\r
231         isDirty: function() {\r
232             return isDirty;\r
233         },\r
234         setDirty: function(dirty) {\r
235             isDirty = dirty;\r
236         },\r
237         onShowed: function() {\r
238             if(!wasShownAlready) {\r
239                 wasShownAlready = true;\r
240                 view.selectFirstNode();\r
241             }\r
242         }\r
243     \r
244     }\r
245 };