a16513289b1b7fa422f0e287a89d89e969371dd5
[fnpeditor.git] / src / editor / modules / documentCanvas / commands.js
1 define([
2 './canvas/utils',
3 'views/dialog/dialog',
4 'fnpjs/datetime'
5 ], function(utils, Dialog, datetime) {
6     
7 'use strict';
8 /* globals gettext */
9
10
11 var gridToggled = false;
12
13 var commands = {
14     _cmds: {},
15     register: function(name, command) {
16         this._cmds[name] = command;
17     },
18
19     run: function(name, params, canvas, user) {
20         return this._cmds[name](canvas, params, user);
21     }
22 };
23
24 commands.register('undo', function(canvas) {
25     var doc = canvas.wlxmlDocument;
26
27     doc.undo();
28 });
29
30 commands.register('redo', function(canvas) {
31     var doc = canvas.wlxmlDocument;
32
33     doc.redo();
34 });
35
36 commands.register('remove-node', function(canvas) {
37     canvas.getCurrentNodeElement().wlxmlNode.detach();
38 });
39
40 commands.register('unwrap-node', function(canvas) {
41     var cursor = canvas.getCursor(),
42         selectionStart = cursor.getSelectionStart(),
43         selectionEnd = cursor.getSelectionEnd(),
44         parent1 = selectionStart.element.parent() || undefined,
45         parent2 = selectionEnd.element.parent() || undefined;
46
47     var selectionAnchor = cursor.getSelectionAnchor(),
48         node1 = parent1.wlxmlNode,
49         node2 = parent2.wlxmlNode,
50         doc = node1.document;
51     if(doc.areItemsOfSameList({node1: node1, node2: node2})) {
52         doc.extractItems({item1: node1, item2: node2});
53         canvas.setCurrentElement(selectionAnchor.element, {caretTo: selectionAnchor.offset});
54     } else if(!cursor.isSelecting()) {
55         var nodeToUnwrap = cursor.getPosition().element.wlxmlNode,
56             parentNode = nodeToUnwrap.unwrap();
57         if(parentNode) {
58             canvas.setCurrentElement(utils.findCanvasElement(parentNode));
59         }
60     }
61 });
62
63 commands.register('wrap-node', function(canvas) {
64     var cursor = canvas.getCursor(),
65         selectionStart = cursor.getSelectionStart(),
66         selectionEnd = cursor.getSelectionEnd(),
67         parent1 = selectionStart.element.parent() || undefined,
68         parent2 = selectionEnd.element.parent() || undefined;
69
70     var node1 = parent1.wlxmlNode,
71         node2 = parent2.wlxmlNode,
72         doc = node1.document;
73
74     if(doc.areItemsOfSameList({node1: node1, node2: node2})) {
75         doc.createList({node1: node1, node2: node2});
76     }
77 });
78
79 commands.register('list', function(canvas, params) {
80     void(params);
81     var cursor = canvas.getCursor(),
82         selectionStart = cursor.getSelectionStart(),
83         selectionEnd = cursor.getSelectionEnd(),
84         parent1 = selectionStart.element.parent() || undefined,
85         parent2 = selectionEnd.element.parent() || undefined;
86
87     var selectionFocus = cursor.getSelectionFocus();
88
89     if(selectionStart.element.isInsideList() || selectionEnd.element.isInsideList()) {
90         return;
91     }
92
93     var node1 = parent1.wlxmlNode,
94         node2 = parent2.wlxmlNode,
95         doc = node1.document;
96     
97     doc.transaction(function() {
98         doc.createList({node1: node1, node2: node2});
99     }, {
100         success: function() {
101             canvas.setCurrentElement(selectionFocus.element, {caretTo: selectionFocus.offset});
102         }
103     });
104 });
105
106 commands.register('toggle-grid', function(canvas, params) {
107     canvas.doc().dom().parent().toggleClass('grid-on', params.toggle);
108     gridToggled = params.toggle;
109 });
110
111 commands.register('newNodeRequested', function(canvas, params, user) {
112     var cursor = canvas.getCursor(),
113         selectionStart = cursor.getSelectionStart(),
114         selectionEnd = cursor.getSelectionEnd(),
115         wlxmlNode, caretTo, wrapperCanvasElement;
116
117     var insertNode = function(insertion, callback) {
118         var doc = canvas.wlxmlDocument,
119             metadata, creator, dialog;
120
121         var execCallback = function(node) {
122             if(callback) {
123                 callback(node);
124             }
125         };
126
127         if(params.wlxmlTag === 'aside' && params.wlxmlClass === 'comment') {
128             doc.transaction(function() {
129                 var node = insertion();
130                 if(user) {
131                     creator = user.name;
132                     if(user.email) {
133                         creator += ' (' + user.email + ')';
134                     }
135                 } else {
136                     creator = 'anonymous';
137                 }
138
139                 metadata = node.getMetadata();
140                 metadata.add({key: 'creator', value: creator});
141                 metadata.add({key: 'date', value: datetime.currentStrfmt()});
142                 return node;
143             }, {
144                 success: execCallback
145             });
146         } else if(params.wlxmlClass === 'link') {
147             dialog = Dialog.create({
148                 title: gettext('Create link'),
149                 executeButtonText: gettext('Apply'),
150                 cancelButtonText: gettext('Cancel'),
151                 fields: [
152                     {label: gettext('Link'), name: 'href', type: 'input'}
153                 ]
154             });
155             dialog.on('execute', function(event) {
156                 doc.transaction(function() {
157                     var node = insertion();
158                     node.setAttr('href', event.formData.href);
159                     event.success();
160                     return node;
161                 }, {
162                     success: execCallback
163                 });
164             });
165             dialog.show();
166         } else {
167             doc.transaction(function() {
168                 return insertion();
169             }, {success: execCallback});
170         }
171     };
172
173     if(cursor.isSelecting()) {
174         if(cursor.isSelectingSiblings()) {
175             if(cursor.isSelectingWithinElement()) {
176                 wlxmlNode = selectionStart.element.wlxmlNode;
177                 caretTo = selectionStart.offset < selectionEnd.offset ? 'start' : 'end';
178
179                 insertNode(
180                     function() {
181                         return wlxmlNode.wrapWith({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}, start: selectionStart.offset, end: selectionEnd.offset});
182                     },
183                     function(wrapper) {
184                         wrapperCanvasElement = utils.findCanvasElement(wrapper);
185                         canvas.setCurrentElement(wrapperCanvasElement.children()[0], {caretTo: caretTo});
186                     }
187                 );
188             }
189             else {
190                 wlxmlNode = selectionStart.element.wlxmlNode.parent();
191                 caretTo = selectionStart.element.sameNode(cursor.getSelectionAnchor().element) ? 'end' : 'start';
192
193                 insertNode(
194                     function() {
195                         return wlxmlNode.wrapText({
196                             _with: {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}},
197                             offsetStart: selectionStart.offset,
198                             offsetEnd: selectionEnd.offset,
199                             textNodeIdx: [wlxmlNode.indexOf(selectionStart.element.wlxmlNode), wlxmlNode.indexOf(selectionEnd.element.wlxmlNode)] //parent.childIndex(selectionEnd.element)]
200                         });
201                     },
202                     function(wrapper) {
203                         wrapperCanvasElement = utils.findCanvasElement(wrapper);
204                         canvas.setCurrentElement(wrapperCanvasElement.children()[caretTo === 0 ? 0 : wrapperCanvasElement.children().length - 1], {caretTo: caretTo});
205                     }
206                 );
207             }
208         } else {
209             var node1 = selectionStart.element.wlxmlNode,
210                 node2 = selectionEnd.element.wlxmlNode,
211                 siblingParents = canvas.wlxmlDocument.getSiblingParents({node1: node1, node2: node2});
212
213             if(siblingParents) {
214                 insertNode(
215                     function() {
216                         return canvas.wlxmlDocument.wrapNodes({
217                             node1: siblingParents.node1,
218                             node2: siblingParents.node2,
219                             _with: {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}}
220                         });
221                     }
222                 );
223             }
224         }
225     } else if(canvas.getCurrentNodeElement()) {
226         wlxmlNode = canvas.getCurrentNodeElement().wlxmlNode;
227
228         var linkFound = [wlxmlNode].concat(wlxmlNode.parents()).some(function(node) {
229             if(node.getClass() === 'link') {
230                 var dialog = Dialog.create({
231                     title: gettext('Edit link'),
232                     executeButtonText: gettext('Apply'),
233                     cancelButtonText: gettext('Cancel'),
234                     fields: [
235                         {label: gettext('Link'), name: 'href', type: 'input', initialValue: node.getAttr('href')},
236                     ]
237                 });
238                 dialog.on('execute', function(event) {
239                     canvas.wlxmlDocument.transaction(function() {
240                         node.setAttr('href', event.formData.href);
241                         event.success();
242                     });
243                     canvas.wlxmlDocument.endTransaction();
244                 });
245                 dialog.show();
246                 return true;
247             }
248         });
249         if(linkFound) {
250             return;
251         }
252
253         if(params.ctrlKey) {
254             insertNode(
255                 function() {
256                     return wlxmlNode.wrapWith({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}});
257                 },
258                 function(wrapper) {
259                     canvas.setCurrentElement(utils.findCanvasElement(wrapper));
260                 }
261             );
262         } else {
263             insertNode(
264                 function() {
265                     var node = wlxmlNode.after({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}});
266                     node.append({text:''});
267                     return node;
268                 }, function(wrapper) {
269                     canvas.setCurrentElement(utils.findCanvasElement(wrapper));
270                 }
271             );
272         }
273     }
274 });
275
276 commands.register('footnote', function(canvas, params) {
277     void(params);
278     var cursor = canvas.getCursor(),
279         position = cursor.getPosition(),
280         asideNode, asideElement, node;
281         
282
283     if(cursor.isSelectingWithinElement()) {
284         asideNode = position.element.wlxmlNode.wrapWith({tagName: 'aside', attrs:{'class': 'footnote'}, start: cursor.getSelectionStart().offset, end: cursor.getSelectionEnd().offset});
285     } else {
286         node = position.element.wlxmlNode;
287         node.document.transaction(function() {
288             asideNode = node.divideWithElementNode({tagName: 'aside', attrs:{'class': 'footnote'}}, {offset: position.offset});
289             asideNode.append({text: ''});
290         });
291     }
292
293     asideElement = utils.findCanvasElement(asideNode);
294     asideElement.toggle(true);
295     canvas.setCurrentElement(asideElement);
296 });
297
298 commands.register('take-away-node', function(canvas) {
299     var position = canvas.getCursor().getPosition(),
300         element = position.element,
301         nodeElement = element ? element.parent() : canvas.getCurrentNodeElement();
302
303     if(!nodeElement || !(nodeElement.parent())) {
304         return;
305     }
306
307     var range = nodeElement.wlxmlNode.unwrapContent();
308
309     if(element) {
310         var elementIsFirstChild = nodeElement.childIndex(element);
311         if(element.bound()) {
312             canvas.setCurrentElement(element, {caretTo: position.offset});
313         } else {
314             if(elementIsFirstChild) {
315                 canvas.setCurrentElement(utils.findCanvasElement(range.element1), {caretTo: 'end'});
316             } else {
317                 canvas.setCurrentElement(utils.findCanvasElement(range.element2), {caretTo: 'end'});
318             }
319         }
320     } else {
321         canvas.setCurrentElement(utils.findCanvasElement(range.element1), {caretTo: 'start'});
322     }
323
324 });
325
326
327 return {
328     run: function(name, params, canvas, user) {
329         return commands.run(name, params, canvas, user);
330     }
331 };
332
333 });