editor: dialog forms - better initial focus
[fnpeditor.git] / src / editor / modules / documentCanvas / commands.js
1 define([
2 './canvas/utils',
3 'fnpjs/datetime'
4 ], function(utils, datetime) {
5     
6 'use strict';
7
8
9 var gridToggled = false;
10
11 var commands = {
12     _cmds: {},
13     register: function(name, command) {
14         this._cmds[name] = command;
15     },
16
17     run: function(name, params, canvas, user) {
18         return this._cmds[name](canvas, params, user);
19     }
20 };
21
22 commands.register('undo', function(canvas) {
23     var doc = canvas.wlxmlDocument;
24
25     doc.undo();
26 });
27
28 commands.register('redo', function(canvas) {
29     var doc = canvas.wlxmlDocument;
30
31     doc.redo();
32 });
33
34 commands.register('remove-node', function(canvas) {
35     canvas.getCurrentNodeElement().data('wlxmlNode').detach();
36 });
37
38 commands.register('unwrap-node', function(canvas) {
39     var cursor = canvas.getCursor(),
40         selectionStart = cursor.getSelectionStart(),
41         selectionEnd = cursor.getSelectionEnd(),
42         parent1 = selectionStart.element.parent() || undefined,
43         parent2 = selectionEnd.element.parent() || undefined;
44
45     var selectionAnchor = cursor.getSelectionAnchor(),
46         node1 = parent1.data('wlxmlNode'),
47         node2 = parent2.data('wlxmlNode'),
48         doc = node1.document;
49     if(doc.areItemsOfSameList({node1: node1, node2: node2})) {
50         doc.extractItems({item1: node1, item2: node2});
51         canvas.setCurrentElement(selectionAnchor.element, {caretTo: selectionAnchor.offset});
52     } else if(!cursor.isSelecting()) {
53         var nodeToUnwrap = cursor.getPosition().element.data('wlxmlNode'),
54             parentNode = nodeToUnwrap.unwrap();
55         if(parentNode) {
56             canvas.setCurrentElement(utils.findCanvasElement(parentNode));
57         }
58     }
59 });
60
61 commands.register('wrap-node', function(canvas) {
62     var cursor = canvas.getCursor(),
63         selectionStart = cursor.getSelectionStart(),
64         selectionEnd = cursor.getSelectionEnd(),
65         parent1 = selectionStart.element.parent() || undefined,
66         parent2 = selectionEnd.element.parent() || undefined;
67
68     var node1 = parent1.data('wlxmlNode'),
69         node2 = parent2.data('wlxmlNode'),
70         doc = node1.document;
71
72     if(doc.areItemsOfSameList({node1: node1, node2: node2})) {
73         doc.createList({node1: node1, node2: node2});
74     }
75 });
76
77 commands.register('list', function(canvas, params) {
78     void(params);
79     var cursor = canvas.getCursor(),
80         selectionStart = cursor.getSelectionStart(),
81         selectionEnd = cursor.getSelectionEnd(),
82         parent1 = selectionStart.element.parent() || undefined,
83         parent2 = selectionEnd.element.parent() || undefined;
84
85     var selectionFocus = cursor.getSelectionFocus();
86
87     if(selectionStart.element.isInsideList() || selectionEnd.element.isInsideList()) {
88         return;
89     }
90
91     var node1 = parent1.data('wlxmlNode'),
92         node2 = parent2.data('wlxmlNode'),
93         doc = node1.document;
94     
95     doc.createList({node1: node1, node2: node2});
96     canvas.setCurrentElement(selectionFocus.element, {caretTo: selectionFocus.offset});
97 });
98
99 commands.register('toggle-grid', function(canvas, params) {
100     canvas.doc().dom().parent().toggleClass('grid-on', params.toggle);
101     gridToggled = params.toggle;
102 });
103
104 commands.register('newNodeRequested', function(canvas, params, user) {
105     var cursor = canvas.getCursor(),
106         selectionStart = cursor.getSelectionStart(),
107         selectionEnd = cursor.getSelectionEnd(),
108         wlxmlNode, caretTo, wrapper, wrapperCanvasElement;
109
110     var insertNode = function(insertion) {
111         var doc = canvas.wlxmlDocument,
112             node, metadata, creator;
113
114         doc.startTransaction();
115         node = insertion();
116         if(node.getTagName() === 'aside' && node.getClass() === 'comment') {
117             if(user) {
118                 creator = user.name;
119                 if(user.email) {
120                     creator += ' (' + user.email + ')';
121                 }
122             } else {
123                 creator = 'anonymous';
124             }
125
126             metadata = node.getMetadata();
127             metadata.add({key: 'creator', value: creator});
128             metadata.add({key: 'date', value: datetime.currentStrfmt()});
129         }
130         doc.endTransaction();
131         return node;
132     };
133
134
135     if(cursor.isSelecting()) {
136         if(cursor.isSelectingSiblings()) {
137             if(cursor.isSelectingWithinElement()) {
138                 wlxmlNode = selectionStart.element.data('wlxmlNode');
139                 caretTo = selectionStart.offset < selectionEnd.offset ? 'start' : 'end';
140
141                 wrapper = insertNode(function() {
142                     return wlxmlNode.wrapWith({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}, start: selectionStart.offset, end: selectionEnd.offset});
143                 });
144                 wrapperCanvasElement = utils.findCanvasElement(wrapper);
145                 canvas.setCurrentElement(wrapperCanvasElement.children()[0], {caretTo: caretTo});
146             }
147             else {
148                 wlxmlNode = selectionStart.element.data('wlxmlNode').parent();
149                 caretTo = selectionStart.element.sameNode(cursor.getSelectionAnchor().element) ? 'end' : 'start';
150
151                 wrapper = insertNode(function() {
152                     return wlxmlNode.wrapText({
153                         _with: {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}},
154                         offsetStart: selectionStart.offset,
155                         offsetEnd: selectionEnd.offset,
156                         textNodeIdx: [wlxmlNode.indexOf(selectionStart.element.data('wlxmlNode')), wlxmlNode.indexOf(selectionEnd.element.data('wlxmlNode'))] //parent.childIndex(selectionEnd.element)]
157                     });
158                 });
159                 wrapperCanvasElement = utils.findCanvasElement(wrapper);
160                 canvas.setCurrentElement(wrapperCanvasElement.children()[caretTo === 0 ? 0 : wrapperCanvasElement.children().length - 1], {caretTo: caretTo});
161             }
162         } else {
163             var node1 = selectionStart.element.data('wlxmlNode'),
164                 node2 = selectionEnd.element.data('wlxmlNode'),
165                 siblingParents = canvas.wlxmlDocument.getSiblingParents({node1: node1, node2: node2});
166
167             if(siblingParents) {
168                 insertNode(function() {
169                     return canvas.wlxmlDocument.wrapNodes({
170                         node1: siblingParents.node1,
171                         node2: siblingParents.node2,
172                         _with: {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}}
173                     });
174                 });
175             }
176         }
177     } else if(canvas.getCurrentNodeElement()) {
178         wlxmlNode = canvas.getCurrentNodeElement().data('wlxmlNode');
179         if(params.ctrlKey) {
180             wrapper = insertNode(function() {
181                 return wlxmlNode.wrapWith({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}});
182             });
183         } else {
184             wrapper = insertNode(function() {
185                 var node = wlxmlNode.after({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}});
186                 node.append({text:''});
187                 return node;
188             });
189         }
190         canvas.setCurrentElement(utils.findCanvasElement(wrapper));
191     }
192
193
194 });
195
196 commands.register('footnote', function(canvas, params) {
197     void(params);
198     var cursor = canvas.getCursor(),
199         position = cursor.getPosition(),
200         asideNode, asideElement, node;
201         
202
203     if(cursor.isSelectingWithinElement()) {
204         asideNode = position.element.data('wlxmlNode').wrapWith({tagName: 'aside', attrs:{'class': 'footnote'}, start: cursor.getSelectionStart().offset, end: cursor.getSelectionEnd().offset});
205     } else {
206         node = position.element.data('wlxmlNode');
207         node.document.transaction(function() {
208             asideNode = node.divideWithElementNode({tagName: 'aside', attrs:{'class': 'footnote'}}, {offset: position.offset});
209             asideNode.append({text: ''});
210         });
211     }
212
213     asideElement = utils.findCanvasElement(asideNode);
214     asideElement.toggle(true);
215     canvas.setCurrentElement(asideElement);
216 });
217
218 commands.register('take-away-node', function(canvas) {
219     var position = canvas.getCursor().getPosition(),
220         element = position.element,
221         nodeElement = element ? element.parent() : canvas.getCurrentNodeElement();
222
223     if(!nodeElement || !(nodeElement.parent())) {
224         return;
225     }
226
227     var range = nodeElement.data('wlxmlNode').unwrapContent();
228
229     if(element) {
230         var elementIsFirstChild = nodeElement.childIndex(element);
231         if(element.bound()) {
232             canvas.setCurrentElement(element, {caretTo: position.offset});
233         } else {
234             if(elementIsFirstChild) {
235                 canvas.setCurrentElement(utils.findCanvasElement(range.element1), {caretTo: 'end'});
236             } else {
237                 canvas.setCurrentElement(utils.findCanvasElement(range.element2), {caretTo: 'end'});
238             }
239         }
240     } else {
241         canvas.setCurrentElement(utils.findCanvasElement(range.element1), {caretTo: 'start'});
242     }
243
244 });
245
246
247 return {
248     run: function(name, params, canvas, user) {
249         return commands.run(name, params, canvas, user);
250     }
251 };
252
253 });