diff options
Diffstat (limited to 'js/controllers/selection-controller.js')
-rw-r--r-- | js/controllers/selection-controller.js | 299 |
1 files changed, 299 insertions, 0 deletions
diff --git a/js/controllers/selection-controller.js b/js/controllers/selection-controller.js new file mode 100644 index 00000000..833e6f04 --- /dev/null +++ b/js/controllers/selection-controller.js | |||
@@ -0,0 +1,299 @@ | |||
1 | /* <copyright> | ||
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | ||
3 | No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/> | ||
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | ||
5 | </copyright> */ | ||
6 | |||
7 | |||
8 | var Montage = require("montage/core/core").Montage, | ||
9 | Component = require("montage/ui/component").Component; | ||
10 | |||
11 | exports.SelectionController = Montage.create(Component, { | ||
12 | |||
13 | _isDocument: { | ||
14 | value: true | ||
15 | }, | ||
16 | |||
17 | isDocument: { | ||
18 | get: function() { | ||
19 | return this._isDocument; | ||
20 | } | ||
21 | }, | ||
22 | |||
23 | deserializedFromTemplate: { | ||
24 | value: function() { | ||
25 | this.eventManager.addEventListener("openDocument", this, false); | ||
26 | this.eventManager.addEventListener("elementAdded", this, false); | ||
27 | this.eventManager.addEventListener("elementDeleted", this, false); | ||
28 | this.eventManager.addEventListener("selectAll", this, false); | ||
29 | this.eventManager.addEventListener("deleteSelection", this, false); | ||
30 | // defaultEventManager.addEventListener( "undo", this, false); | ||
31 | // defaultEventManager.addEventListener( "redo", this, false); | ||
32 | } | ||
33 | }, | ||
34 | |||
35 | /** | ||
36 | * Get the current document selection array. If nothing is selected the currentSelectionArray should be null | ||
37 | */ | ||
38 | handleOpenDocument: { | ||
39 | value: function() { | ||
40 | // Handle initializing the selection array here. | ||
41 | } | ||
42 | }, | ||
43 | |||
44 | initWithDocument: { | ||
45 | value: function(currentSelectionArray) { | ||
46 | this._selectedItems = []; | ||
47 | this._isDocument = true; | ||
48 | |||
49 | if(currentSelectionArray) { | ||
50 | if(currentSelectionArray.length >= 1) { | ||
51 | this._selectedItems = currentSelectionArray; | ||
52 | this._isDocument = false; | ||
53 | } | ||
54 | } | ||
55 | |||
56 | this.dispatchEvent(selectionEvent.event); | ||
57 | } | ||
58 | }, | ||
59 | |||
60 | handleElementAdded: { | ||
61 | value: function(event) { | ||
62 | this.executeSelectElement(event.detail); | ||
63 | } | ||
64 | }, | ||
65 | |||
66 | handleElementDeleted: { | ||
67 | value: function(event) { | ||
68 | if(!this._isDocument) { | ||
69 | |||
70 | if(this.findSelectedElement(event.detail) !== -1) { | ||
71 | this.executeSelectElement(); | ||
72 | } | ||
73 | |||
74 | } | ||
75 | |||
76 | } | ||
77 | }, | ||
78 | |||
79 | handleSelectAll: { | ||
80 | value: function(event) { | ||
81 | var selected = [], childNodes = []; | ||
82 | |||
83 | childNodes = this.application.ninja.currentDocument.documentRoot.childNodes; | ||
84 | childNodes = Array.prototype.slice.call(childNodes, 0); | ||
85 | childNodes.forEach(function(item) { | ||
86 | if(item.nodeType == 1) { | ||
87 | selected.push(item); | ||
88 | } | ||
89 | }); | ||
90 | |||
91 | this.selectElements(selected); | ||
92 | } | ||
93 | }, | ||
94 | |||
95 | handleDeleteSelection: { | ||
96 | value: function(event) { | ||
97 | this.application.ninja.selectedElements = []; | ||
98 | this._isDocument = true; | ||
99 | NJevent("selectionChange", {"elements": this.application.ninja.selectedElements, "isDocument": this._isDocument} ); | ||
100 | } | ||
101 | }, | ||
102 | |||
103 | /** | ||
104 | * Select Element. This function will not check that element, it will simply add it to the selection array. | ||
105 | */ | ||
106 | executeSelectElement: { | ||
107 | value: function(item) { | ||
108 | this.application.ninja.selectedElements = []; | ||
109 | |||
110 | if(item) { | ||
111 | this.application.ninja.selectedElements.push({_element: item, uuid: item.uuid}); | ||
112 | this._isDocument = false; | ||
113 | } else { | ||
114 | this._isDocument = true; | ||
115 | } | ||
116 | |||
117 | NJevent("selectionChange", {"elements": this.application.ninja.selectedElements, "isDocument": this._isDocument} ); | ||
118 | |||
119 | } | ||
120 | |||
121 | }, | ||
122 | |||
123 | selectElement: { | ||
124 | value: function(item) { | ||
125 | |||
126 | if(this.findSelectedElement(item) === -1) { | ||
127 | |||
128 | if(this.application.ninja.currentDocument.inExclusion(item) !== -1){ | ||
129 | if(this.isDocument) return; // If the stage is already selected do nothing. | ||
130 | this.executeSelectElement(); // Else execute selection with no item | ||
131 | } else { | ||
132 | |||
133 | if(item.parentNode.id == "UserContent") { | ||
134 | this.executeSelectElement(item); | ||
135 | } else { | ||
136 | var outerElement = item.parentNode; | ||
137 | |||
138 | while(outerElement.parentNode && outerElement.parentNode.id !== "UserContent") { | ||
139 | outerElement = outerElement.parentNode; | ||
140 | } | ||
141 | |||
142 | this.executeSelectElement(outerElement); | ||
143 | } | ||
144 | } | ||
145 | } | ||
146 | } | ||
147 | }, | ||
148 | |||
149 | selectElements: { | ||
150 | value: function(items) { | ||
151 | if(items && items.length > 0) { | ||
152 | var that = this; | ||
153 | this.application.ninja.selectedElements = []; | ||
154 | |||
155 | items.forEach(function(item) { | ||
156 | that.application.ninja.selectedElements.push({_element: item, uuid: item.uuid}); | ||
157 | that._isDocument = false; | ||
158 | }); | ||
159 | |||
160 | NJevent("selectionChange", {"elements": this.application.ninja.selectedElements, "isDocument": this._isDocument} ); | ||
161 | } | ||
162 | } | ||
163 | }, | ||
164 | |||
165 | shiftSelectElement: { | ||
166 | value: function(item) { | ||
167 | if(this.application.ninja.currentDocument.inExclusion(item) !== -1) return; | ||
168 | |||
169 | (this.findSelectedElement(item) !== -1 ) ? this.removeElement(item) : this.insertElement(item); | ||
170 | } | ||
171 | }, | ||
172 | |||
173 | insertElement: { | ||
174 | value: function(item) { | ||
175 | if(item) { | ||
176 | if(this._isDocument) { | ||
177 | this.application.ninja.selectedElements = []; | ||
178 | this._isDocument = false; | ||
179 | } | ||
180 | |||
181 | this.application.ninja.selectedElements.push({_element: item, uuid: item.uuid}); | ||
182 | |||
183 | NJevent("selectionChange", {"elements": this.application.ninja.selectedElements, "isDocument": this._isDocument} ); | ||
184 | } | ||
185 | } | ||
186 | }, | ||
187 | |||
188 | removeElement: { | ||
189 | value: function(item) { | ||
190 | if(item){ | ||
191 | try{ | ||
192 | if(this.application.ninja.selectedElements.length > 1) { | ||
193 | var idx = this.findSelectedElement(item); | ||
194 | if(idx != -1){ | ||
195 | this.application.ninja.selectedElements.splice(idx, 1); | ||
196 | } | ||
197 | } else { | ||
198 | this.application.ninja.selectedElements = []; | ||
199 | this._isDocument = true; | ||
200 | } | ||
201 | |||
202 | NJevent("selectionChange", {"elements": this.application.ninja.selectedElements, "isDocument": this._isDocument} ); | ||
203 | |||
204 | } catch (err) { | ||
205 | console.log("Fault: " + err); | ||
206 | } | ||
207 | } | ||
208 | |||
209 | } | ||
210 | }, | ||
211 | |||
212 | |||
213 | |||
214 | |||
215 | |||
216 | handleUndo: { | ||
217 | value: function(event) { | ||
218 | this._applySelectionAfterUndoRedo(event.detail); | ||
219 | } | ||
220 | }, | ||
221 | |||
222 | handleRedo: { | ||
223 | value: function(event) { | ||
224 | this._applySelectionAfterUndoRedo(event.detail); | ||
225 | } | ||
226 | }, | ||
227 | |||
228 | _applySelectionAfterUndoRedo: { | ||
229 | value: function(items) { | ||
230 | if(items) { | ||
231 | if(items instanceof Array) { | ||
232 | if(items.length > 1) | ||
233 | { | ||