logging: pass logger name to the handler
[fnpeditor.git] / src / editor / modules / documentCanvas / commands.js
1 define([
2 'modules/documentCanvas/canvas/documentElement',
3 './canvas/utils'
4 ], function(documentElement, utils) {
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) {
18         return this._cmds[name](canvas, params);
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
51
52         doc.extractItems({item1: node1, item2: node2});
53
54         //canvas.list.extractItems({element1: parent1, element2: parent2});
55         canvas.setCurrentElement(selectionAnchor.element, {caretTo: selectionAnchor.offset});
56     } else if(!cursor.isSelecting()) {
57         var nodeToUnwrap = cursor.getPosition().element.data('wlxmlNode'),
58             parentNode = nodeToUnwrap.unwrap();
59         if(parentNode) {
60             canvas.setCurrentElement(utils.findCanvasElement(parentNode));
61         }
62     }
63 });
64
65 commands.register('wrap-node', function(canvas) {
66     var cursor = canvas.getCursor(),
67         selectionStart = cursor.getSelectionStart(),
68         selectionEnd = cursor.getSelectionEnd(),
69         parent1 = selectionStart.element.parent() || undefined,
70         parent2 = selectionEnd.element.parent() || undefined;
71
72     var node1 = parent1.data('wlxmlNode'),
73         node2 = parent2.data('wlxmlNode'),
74         doc = node1.document;
75
76     if(doc.areItemsOfSameList({node1: node1, node2: node2})) {
77         //canvas.list.create({element1: parent1, element2: parent2});
78         //doc.transform('createList', {node1: node1, node2: node2});
79         doc.createList({node1: node1, node2: node2});
80     }
81 });
82
83 commands.register('list', function(canvas, params) {
84     void(params);
85     var cursor = canvas.getCursor(),
86         selectionStart = cursor.getSelectionStart(),
87         selectionEnd = cursor.getSelectionEnd(),
88         parent1 = selectionStart.element.parent() || undefined,
89         parent2 = selectionEnd.element.parent() || undefined;
90
91     var selectionFocus = cursor.getSelectionFocus();
92
93     if(selectionStart.element.isInsideList() || selectionEnd.element.isInsideList()) {
94         return;
95     }
96
97     var node1 = parent1.data('wlxmlNode'),
98         node2 = parent2.data('wlxmlNode'),
99         doc = node1.document;
100     
101     //doc.transform('createList', {node1: node1, node2: node2});
102     doc.createList({node1: node1, node2: node2});
103
104
105     canvas.setCurrentElement(selectionFocus.element, {caretTo: selectionFocus.offset});
106 });
107
108 commands.register('toggle-grid', function(canvas, params) {
109     canvas.doc().dom().find('[wlxml-tag]').toggleClass('rng-common-hoveredNode', params.toggle);
110     gridToggled = params.toggle;
111 });
112
113 commands.register('newNodeRequested', function(canvas, params) {
114     var cursor = canvas.getCursor(),
115         selectionStart = cursor.getSelectionStart(),
116         selectionEnd = cursor.getSelectionEnd(),
117         wlxmlNode, caretTo, wrapper, wrapperCanvasElement;
118
119     if(cursor.isSelecting()) {
120         if(cursor.isSelectingSiblings()) {
121             if(cursor.isSelectingWithinElement()) {
122                 wlxmlNode = selectionStart.element.data('wlxmlNode');
123                 caretTo = selectionStart.offset < selectionEnd.offset ? 'start' : 'end';
124                     //wrapper = wlxmlNode.wrapWith({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}, start: selectionStart.offset, end: selectionEnd.offset}),
125                     //wrapper = wlxmlNode.transform('smartxml.wrapWith', {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}, start: selectionStart.offset, end: selectionEnd.offset})
126                 wrapper = wlxmlNode.wrapWith({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}, start: selectionStart.offset, end: selectionEnd.offset});
127                 wrapperCanvasElement = utils.findCanvasElement(wrapper);
128                 canvas.setCurrentElement(wrapperCanvasElement.children()[0], {caretTo: caretTo});
129             }
130             else {
131                 wlxmlNode = selectionStart.element.data('wlxmlNode').parent();
132                 caretTo = selectionStart.element.sameNode(cursor.getSelectionAnchor().element) ? 'end' : 'start';
133
134                 // var wrapper = wlxmlNode.wrapText({
135                 //     _with: {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}},
136                 //     offsetStart: selectionStart.offset,
137                 //     offsetEnd: selectionEnd.offset,
138                 //     textNodeIdx: [wlxmlNode.indexOf(selectionStart.element.data('wlxmlNode')), wlxmlNode.indexOf(selectionEnd.element.data('wlxmlNode'))] //parent.childIndex(selectionEnd.element)]
139                 // }),
140                 wrapper = wlxmlNode.wrapText({
141                     _with: {tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}},
142                     offsetStart: selectionStart.offset,
143                     offsetEnd: selectionEnd.offset,
144                     textNodeIdx: [wlxmlNode.indexOf(selectionStart.element.data('wlxmlNode')), wlxmlNode.indexOf(selectionEnd.element.data('wlxmlNode'))] //parent.childIndex(selectionEnd.element)]
145                 });
146                 wrapperCanvasElement = utils.findCanvasElement(wrapper);
147                 canvas.setCurrentElement(wrapperCanvasElement.children()[caretTo === 0 ? 0 : wrapperCanvasElement.children().length - 1], {caretTo: caretTo});
148             }
149         } else {
150             var node1 = selectionStart.element.data('wlxmlNode'),
151                 node2 = selectionEnd.element.data('wlxmlNode'),
152                 siblingParents = canvas.wlxmlDocument.getSiblingParents({node1: node1, node2: node2});
153
154             if(siblingParents) {
155                 // canvas.wlxmlDocument.wrapNodes({
156                 //     element1: siblingParents.node1,
157                 //     element2: siblingParents.node2,
158                 //     _with: {tagName: params.wlxmlTag, attrs: {klass: params.wlxmlClass}}
159                 // });
160                 canvas.wlxmlDocument.wrapNodes({
161                     node1: siblingParents.node1,
162                     node2: siblingParents.node2,
163                     _with: {tagName: params.wlxmlTag, attrs: {klass: params.wlxmlClass}}
164                 });
165             }
166         }
167     } else if(canvas.getCurrentNodeElement()) {
168         wlxmlNode = canvas.getCurrentNodeElement().data('wlxmlNode');
169             // wrapper = node.wrapWith({tagName: params.wlxmlTag, attrs: {klass: params.wlxmlClass}});
170         wrapper = wlxmlNode.wrapWith({tagName: params.wlxmlTag, attrs: {'class': params.wlxmlClass}});
171         canvas.setCurrentElement(utils.findCanvasElement(wrapper));
172     }
173
174
175 });
176
177 commands.register('footnote', function(canvas, params) {
178     void(params);
179     var cursor = canvas.getCursor(),
180         position = cursor.getPosition(),
181         asideNode, asideElement;
182         
183
184     if(cursor.isSelectingWithinElement()) {
185         asideNode = position.element.data('wlxmlNode').wrapWith({tagName: 'aside', attrs:{'class': 'footnote'}, start: cursor.getSelectionStart().offset, end: cursor.getSelectionEnd().offset});
186     } else {
187         asideNode = position.element.data('wlxmlNode').divideWithElementNode({tagName: 'aside', attrs:{'class': 'footnote'}}, {offset: position.offset});
188         asideNode.append({text: ''});
189     }
190
191     asideElement = utils.findCanvasElement(asideNode);
192     asideElement.toggle(true);
193     canvas.setCurrentElement(asideElement);
194 });
195
196 commands.register('take-away-node', function(canvas) {
197     var position = canvas.getCursor().getPosition(),
198         element = position.element,
199         nodeElement = element ? element.parent() : canvas.getCurrentNodeElement();
200
201     if(!nodeElement || !(nodeElement.parent())) {
202         return;
203     }
204
205
206     var range = nodeElement.data('wlxmlNode').unwrapContent();
207
208     if(element) {
209         var elementIsFirstChild = nodeElement.childIndex(element);
210         if(element.bound()) {
211             canvas.setCurrentElement(element, {caretTo: position.offset});
212         } else {
213             if(elementIsFirstChild) {
214                 canvas.setCurrentElement(utils.findCanvasElement(range.element1), {caretTo: 'end'});
215             } else {
216                 canvas.setCurrentElement(utils.findCanvasElement(range.element2), {caretTo: 'end'});
217             }
218         }
219     } else {
220         canvas.setCurrentElement(utils.findCanvasElement(range.element1), {caretTo: 'start'});
221     }
222
223 });
224
225
226 return {
227     run: function(name, params, canvas) {
228         return commands.run(name, params, canvas);
229     }
230 };
231
232 });