aboutsummaryrefslogtreecommitdiff
path: root/js/controllers/selection-controller.js
blob: f50762f3eff55014aa2be0865fcd33670330a82e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* <copyright>
This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
(c) Copyright 2011 Motorola Mobility, Inc.  All Rights Reserved.
</copyright> */


var Montage = require("montage/core/core").Montage,
    Component = require("montage/ui/component").Component;

exports.SelectionController = Montage.create(Component, {

    _isDocument: {
        value: true
    },

    isDocument: {
        get: function() {
            return this._isDocument;
        }
    },

    /*
     * Bound property to the ninja currentSelectedContainer
     */
    _selectionContainer: {
        value: null
    },

    selectionContainer: {
        get: function() {
            return this._selectionContainer
        },
        set: function(value) {
            if(this._selectionContainer && this._selectionContainer !== value) {
                this.executeSelectElement();
            }

            this._selectionContainer = value;
        }
    },

    deserializedFromTemplate: {
        value: function() {
            this.eventManager.addEventListener("openDocument", this, false);
            this.eventManager.addEventListener("elementAdded", this, false);
            this.eventManager.addEventListener("elementDeleted", this, false);
            this.eventManager.addEventListener("selectAll", this, false);
            this.eventManager.addEventListener("deleteSelection", this, false);
//            defaultEventManager.addEventListener( "undo", this, false);
//            defaultEventManager.addEventListener( "redo", this, false);
        }
    },

    /**
     * Get the current document selection array. If nothing is selected the currentSelectionArray should be null
     */
    handleOpenDocument: {
        value: function() {
            // Handle initializing the selection array here.
            this.initWithDocument([]);
        }
    },
    
    initWithDocument: {
        value: function(currentSelectionArray) {
            this._selectedItems = [];
            this._isDocument = true;

            if(currentSelectionArray) {
                if(currentSelectionArray.length >= 1) {
                    this._selectedItems = currentSelectionArray;
                    this._isDocument = false;



                    this.application.ninja.selectedElements = currentSelectionArray;
                    NJevent("selectionChange", {"elements": this.application.ninja.selectedElements, "isDocument": this._isDocument} );



                }
            }

            //
            this._selectionContainer = this.application.ninja.currentSelectedContainer;

        }
    },

    handleElementAdded: {
        value: function(event) {
            this.executeSelectElement(event.detail);
        }
    },

    handleElementDeleted: {
        value: function(event) {
            if(!this._isDocument) {
                if(this.findSelectedElement(event.detail) !== -1) {
                    this.executeSelectElement();
                }
            }
        }
    },

    handleSelectAll: {
        value: function(event) {
            var selected = [], childNodes = [];

            childNodes = this.application.ninja.currentDocument.documentRoot.childNodes;
            childNodes = Array.prototype.slice.call(childNodes, 0);
            childNodes.forEach(function(item) {
                if(item.nodeType == 1) {
                    selected.push(item);
                }
            });

            this.selectElements(selected);
        }
    },

    handleDeleteSelection: {
        value: function(event) {
            this.application.ninja.selectedElements = [];
            this._isDocument = true;
            NJevent("selectionChange", {"elements": this.application.ninja.selectedElements, "isDocument": this._isDocument} );
        }
    },

    /**
     * Select Element. This function will not check that element, it will simply add it to the selection array.
     */
    executeSelectElement: {
        value: function(item) {
            this.application.ninja.selectedElements = [];

            if(item) {
                this.application.ninja.selectedElements.push({_element: item, uuid: item.uuid});
                this._isDocument = false;
            } else {
                this._isDocument = true;
            }

            NJevent("selectionChange", {"elements": this.application.ninja.selectedElements, "isDocument": this._isDocument} );

        }

    },

    selectElement: {
        value: function(item) {

            if(this.findSelectedElement(item) === -1) {

                if(this.application.ninja.currentDocument.inExclusion(item) !== -1){
                    if(this.isDocument) return;     // If the stage is already selected do nothing.
                    this.executeSelectElement();    // Else execute selection with no item
                } else {

//                    if(item.parentNode.id === "UserContent") {
                    if(item.parentNode.uuid === this.selectionContainer.uuid) {
                        this.executeSelectElement(item);
                    } else {
                        var outerElement = item.parentNode;

                        while(outerElement.parentNode && outerElement.parentNode.uuid !== this.selectionContainer.uuid) {
                        //while(outerElement.parentNode && outerElement.parentNode.id !== "UserContent") {
                            // If element is higher up than current container then return
                            if(outerElement.id === "UserContent") return;
                            // else keep going up the chain
                            outerElement = outerElement.parentNode;
                        }

                        this.executeSelectElement(outerElement);
                    }
                }
            }
        }
    },

    selectElements: {
        value: function(items) {
            if(items && items.length > 0) {
                var that = this;
                this.application.ninja.selectedElements = [];

                items.forEach(function(item) {
                    that.application.ninja.selectedElements.push({_element: item, uuid: item.uuid});
                    that._isDocument = false;
                });

                NJevent("selectionChange", {"elements": this.application.ninja.selectedElements, "isDocument": this._isDocument} );
            }
        }
    },

    shiftSelectElement: {
        value: function(item) {
            if(this.application.ninja.currentDocument.inExclusion(item) !== -1) return;

            (this.findSelectedElement(item) !== -1 ) ? this.removeElement(item) : this.insertElement(item);
        }
    },

    insertElement: {
        value: function(item) {
            if(item) {
                if(this._isDocument) {
                    this.application.ninja.selectedElements = [];
                    this._isDocument = false;
                }

                this.application.ninja.selectedElements.push({_element: item, uuid: item.uuid});

                NJevent("selectionChange", {"elements": this.application.ninja.selectedElements, "isDocument": this._isDocument} );
            }
        }
    },

    removeElement: {
        value: function(item) {
            if(item){
                try{
                    if(this.application.ninja.selectedElements.length > 1) {
                        var idx = this.findSelectedElement(item);
                        if(idx != -1){
                            this.application.ninja.selectedElements.splice(idx, 1);
                        }
                    } else {
                        this.application.ninja.selectedElements = [];
                        this._isDocument = true;
                    }

                    NJevent("selectionChange", {"elements": this.application.ninja.selectedElements, "isDocument": this._isDocument} );

                } catch (err) {
                    console.log("Fault: " + err);
                }
            }

        }
    },





    handleUndo: {
        value: function(event) {
            this._applySelectionAfterUndoRedo(event.detail);
        }
    },

    handleRedo: {
        value: function(event) {
            this._applySelectionAfterUndoRedo(event.detail);
        }
    },

    _applySelectionAfterUndoRedo: {
        value: function(items) {
            if(items) {
                if(items instanceof Array) {
                    if(items.length > 1)
                    {
                        this.clearSelection();
                        this.setMultipleObjects(items);
                        documentControllerModule.DocumentController.DispatchElementChangedEvent(items);
                    }
                    else if(this._selectedItems.length === 0 || this.findSelectedElement(items) === -1) {
                        this.setSingleSelection(items[0]);
                        documentControllerModule.DocumentController.DispatchElementChangedEvent(items[0]);
                    }
                } else {
                    if(this._selectedItems.length === 0 || this.findSelectedElement(items) === -1) {
                        this.setSingleSelection(items);
                        //documentControllerModule.DocumentController.DispatchElementChangedEvent([items]);
                    }
                }

            } else {
                this.clearSelection();
            }
        }
    },

	isObjectSelected:
	{
		value: function( elt )
		{
			return this.findSelectedElement(elt) > -1;
		}
	},

    /**
     * Looks into the selectionObject for the item to be found using it's id
     *
     * @return: Item index in the selectionObject if found
     *          -1 if not found
     */
    findSelectedElement: {
        value: function(item) {
            var itemUUID;

            for(var i=0, uuid; this.application.ninja.selectedElements[i];i++) {
                // Check for multiple selection and excluding inner elements
                if(item.parentNode && item.parentNode.id !== "UserContent") {
                    var outerElement = item.parentNode;

                    while(outerElement.parentNode && outerElement.parentNode.id !== "UserContent") {
                        outerElement = outerElement.parentNode;
                    }

                    itemUUID = outerElement.uuid;
                } else {
                    itemUUID = item.uuid;
                }

                if(this.application.ninja.selectedElements[i].uuid === itemUUID) {
                    return i;
                }
            }

            return -1;

            // TODO: Not a true object because of the _element.
            //return this.application.ninja.selectedElements.indexOf(item);
        }
    }

});