52f03d8f5b268028617910847bbafed5514f93ce
[fnpeditor.git] / modules / documentCanvas / commands.js
1 define([
2
3 ], function() {
4     
5 'use strict';
6
7
8 var gridToggled = false;
9
10 var commands = {
11     _cmds: {},
12     register: function(name, command) {
13         this._cmds[name] = command;
14     },
15
16     run: function(name, params, canvas) {
17         return this._cmds[name](canvas, params);
18     }
19 };
20
21 commands.register('unwrap-node', function(canvas) {
22     var cursor = canvas.getCursor(),
23         selectionStart = cursor.getSelectionStart(),
24         selectionEnd = cursor.getSelectionEnd(),
25         parent1 = selectionStart.element.parent() || undefined,
26         parent2 = selectionEnd.element.parent() || undefined;
27
28     if(canvas.list.areItemsOfTheSameList({element1: parent1, element2: parent2})) {
29         canvas.list.extractItems({element1: parent1, element2: parent2});
30     } else if(!cursor.isSelecting()) {
31         var toUnwrap = cursor.getPosition().element,
32             parent = toUnwrap.unwrap();
33         canvas.setCurrentElement(parent);
34     }
35 });
36
37 commands.register('wrap-node', function(canvas) {
38     var cursor = canvas.getCursor(),
39         selectionStart = cursor.getSelectionStart(),
40         selectionEnd = cursor.getSelectionEnd(),
41         parent1 = selectionStart.element.parent() || undefined,
42         parent2 = selectionEnd.element.parent() || undefined;
43
44     if(canvas.list.areItemsOfTheSameList({element1: parent1, element2: parent2})) {
45         canvas.list.create({element1: parent1, element2: parent2});
46     }
47 });
48
49 commands.register('toggle-list', function(canvas, params) {
50     var cursor = canvas.getCursor(),
51         selectionStart = cursor.getSelectionStart(),
52         selectionEnd = cursor.getSelectionEnd(),
53         parent1 = selectionStart.element.parent() || undefined,
54         parent2 = selectionEnd.element.parent() || undefined;
55
56     if(params.toggle) {
57         canvas.list.create({element1: parent1, element2: parent2});
58     } else {
59         if(canvas.list.areItemsOfTheSameList({element1: parent1, element2: parent2})) {
60             canvas.list.extractItems({element1: parent1, element2: parent2, merge: false});
61         } 
62     }
63 });
64
65 commands.register('toggle-grid', function(canvas, params) {
66     canvas.doc().dom().find('[wlxml-tag]').toggleClass('rng-common-hoveredNode', params.toggle);
67     gridToggled = params.toggle;
68 });
69
70 commands.register('newNodeRequested', function(canvas, params) {
71     var cursor = canvas.getCursor(),
72         selectionStart = cursor.getSelectionStart(),
73         selectionEnd = cursor.getSelectionEnd();
74
75     if(cursor.isSelecting() && cursor.isSelectingSiblings()) {
76         if(cursor.isSelectingWithinElement()) {
77             selectionStart.element.wrapWithNodeElement({tag: params.wlxmlTag, klass: params.wlxmlClass, start: selectionStart.offset, end: selectionEnd.offset});
78         }
79         else {
80             var parent = selectionStart.element.parent();
81             canvas.wrapText({
82                 inside: parent,
83                 _with: {tag: params.wlxmlTag, klass: params.wlxmlClass},
84                 offsetStart: selectionStart.offset,
85                 offsetEnd: selectionEnd.offset,
86                 textNodeIdx: [parent.childIndex(selectionStart.element), parent.childIndex(selectionEnd.element)]
87             });
88         }
89     }
90 });
91
92
93 return {
94     run: function(name, params, canvas) {
95         return commands.run(name, params, canvas);
96     }
97 };
98
99 });