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