aboutsummaryrefslogtreecommitdiff
path: root/js/controllers/elements/element-controller.js
blob: e40a646ae66ce697633c0e0da30b4edf5ca6eafe (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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/* <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,
    njModule = require("js/lib/NJUtils");

exports.ElementController = Montage.create(Component, {

    addElement: {
        value: function(el, styles) {
        
        	if (el.getAttribute) el.setAttribute('data-ninja-node', 'true');
        	
            if(this.application.ninja.timeline.currentLayerSelected) {
                var selectedLayerIndex = this.application.ninja.timeline.getLayerIndexByID(this.application.ninja.timeline.currentLayerSelected.layerData.layerID);

                if(selectedLayerIndex === 0) {
                    this.application.ninja.currentDocument.model.domContainer.appendChild(el);
                } else {
                    var element = this.application.ninja.timeline.arrLayers[selectedLayerIndex].layerData.elementsList[0];
                    element.parentNode.insertBefore(el, element.nextSibling);
                }
            } else {
                this.application.ninja.currentDocument.model.domContainer.appendChild(el);
            }

            if(styles) {
                this.application.ninja.stylesController.setElementStyles(el, styles);
            }
        }
    },

    // Remove the element from the DOM and clear the GLWord.
    removeElement: {
        value: function(el) {
            if(el.elementModel.shapeModel && el.elementModel.shapeModel.GLWorld) {
                el.elementModel.shapeModel.GLWorld.clearTree();
            }
            el.parentNode.removeChild(el);
        }
    },
    
    getProperty: {
        value: function(el, prop, fallbackOnComputed, isStageElement) {
            if(el.nodeType !== 3){
                return this.application.ninja.stylesController.getElementStyle(el, prop, fallbackOnComputed, isStageElement);
            }else{
                return null;
            }
        }
    },

    setProperty: {
        value: function(el, p, value) {
            this.application.ninja.stylesController.setElementStyle(el, p, value);
        }
    },

    setProperties: {
        value: function(element, properties) {
            for(var property in properties) {
                this.application.ninja.stylesController.setElementStyle(element, property, properties[property]);
        }
        }
    },

    setAttribute: {
        value: function(el, att, value) {
            el.setAttribute(att, value);
        }
    },

    //--------------------------------------------------------------------------------------------------------
    // Routines to get/set color properties
    // borderSide : "top", "right", "bottom", or "left"
    getColor: {
        value: function(el, isFill, borderSide) {
            var colorObj, color, image;

            // Return cached value if one exists
            if(isFill) {
                if(el.elementModel.fill) {
                    return el.elementModel.fill;
                }
                //TODO: Once logic for color and gradient is established, this needs to be revised
                color = this.getProperty(el, "background-color");
                image = this.getProperty(el, "background-image");
            } else {
                // Try getting border color from specific side first
                if(borderSide) {
                    color = this.getProperty(el, "border-" + borderSide + "-color");
                    image = this.getProperty(el, "border-" + borderSide + "-image");
                }

                // If no color was found, look up the shared border color
                if(!color && !image) {
                    if(el.elementModel.stroke) {
                        return el.elementModel.stroke;
                    }

                    color = this.getProperty(el, "border-color");
                    image = this.getProperty(el, "border-image");
                }
            }

            if(color || image) {
                if (image && image !== 'none' && image.indexOf('-webkit') >= 0) {
                    //Gradient
                    colorObj = this.application.ninja.colorController.getColorObjFromCss(image);
                } else {
                    //Solid
                    colorObj = this.application.ninja.colorController.getColorObjFromCss(color);
                }
            }

            // Update cache
            if(isFill) {
                el.elementModel.fill = colorObj;
            } else if(!borderSide) {
                // TODO - Need to update border style and width also
                el.elementModel.stroke = colorObj;
            } else {
                // TODO - Should update specific border sides too
            }

            return colorObj;
        }
    },

    setColor: {
        value: function(el, color, isFill) {
            var mode = color.mode;

            if(isFill) {
                if(mode) {
                    switch (mode) {
                        case 'nocolor':
                            this.setProperty(el, "background-image", "none");
                            this.setProperty(el, "background-color", "none");
                            el.elementModel.fill = null;
                            return;
                        case 'gradient':
                            this.setProperty(el, "background-image", color.color.css);
                            this.setProperty(el, "background-color", "none");
                            break;
                        default:
                            this.setProperty(el, "background-image", "none");
                            this.setProperty(el, "background-color", color.color.css);
                    }
                }

                el.elementModel.fill = color;
            } else {
                if(mode) {
                    switch (mode) {
                        case 'nocolor':
                            this.setProperty(el, "border-image", "none");
                            this.setProperty(el, "border-color", "none");
                            el.elementModel.stroke = null;
                            return;
                        case 'gradient':
                            this.setGradientBorder(el, color.color.gradientMode, color.color.css);
                            break;
                        default:
                            this.setProperty(el, "border-image", "none");
                            this.setProperty(el, "border-image-slice", "");
                            this.setProperty(el, "border-color", color.color.css);
                    }
                }
                el.elementModel.stroke = color;
            }
        }
    },

    setGradientBorder: {
        value: function(el, gradientMode, css) {
            if(gradientMode === "radial") {
                this.setProperty(el, "border-image", css.replace("ellipse", "circle"));
            } else {
                this.setProperty(el, "border-image", css);
            }
            this.setProperty(el, "border-color", "none");
            // gradient slice = borderWidth/totalWidth
            var b = parseInt(this.getProperty(el, "border-left-width", true)),
                w = parseInt(this.getProperty(el, "width", true)),
                h = parseInt(this.getProperty(el, "height", true));
            if(h > w) {
                w = h;
            }
            this.setProperty(el, "border-image-slice", Math.floor(b/(w+b+b) * 100) + "%");
        }
    },

    getStroke: {
        value: function(el, stroke) {
            var strokeInfo = {},
                color,
                borderWidth,
                border;
            if(stroke.colorInfo) {
                strokeInfo.colorInfo = {};
                color = this.getColor(el, false);
                if(color && color.color) {
                    strokeInfo.colorInfo.mode = color.mode;
                    strokeInfo.colorInfo.color = color.color;
                } else {
                    strokeInfo.colorInfo.mode = "nocolor";
                    strokeInfo.colorInfo.color = null;
                }
            }
            if(stroke.borderInfo) {
                // TODO - Need to figure out which border side user wants
                strokeInfo.borderInfo = {};
                if(stroke.borderInfo.borderWidth) {
                    borderWidth = this.getProperty(el, "border-width");
                    if(borderWidth) {
                        border = njModule.NJUtils.getValueAndUnits(borderWidth);
                        strokeInfo.borderInfo.borderWidth = border[0];
                        strokeInfo.borderInfo.borderUnits = border[1];
                    }
                }
                if(stroke.borderInfo.borderStyle) {
                    strokeInfo.borderInfo.borderStyle = this.getProperty(el, "border-style");
                }
            }
            return strokeInfo;
        }
    },

    setStroke: {
        value: function(el, stroke) {
            if(stroke.borderInfo) {
                if(stroke.borderInfo.borderWidth) {
                    this.application.ninja.stylesController.setElementStyle(el, "border-width", stroke.borderInfo.borderWidth + stroke.borderInfo.borderUnits);
                }
                if(stroke.borderInfo.borderStyle) {
                    this.application.ninja.stylesController.setElementStyle(el, "border-style", stroke.borderInfo.borderStyle);
                }
            }
            if(stroke.colorInfo) {
                this.setColor(el, stroke.colorInfo, false);
            }
        }
    },

    getFill: {
        value: function(el, fill) {
            var fillInfo = {},
                color;
            if(fill.colorInfo) {
                fillInfo.colorInfo = {};
                color = this.getColor(el, true);
                if(color && color.color) {
                    fillInfo.colorInfo.mode = color.mode;
                    fillInfo.colorInfo.color = color.color;
                } else {
                    fillInfo.colorInfo.mode = "nocolor";
                    fillInfo.colorInfo.color = null;
                }
            }
            return fillInfo;
        }
    },

    setFill: {
        value: function(el, fill) {
            if(fill.colorInfo) {
                this.setColor(el, fill.colorInfo, true);
            }
        }
    },
    //--------------------------------------------------------------------------------------------------------
    // Routines to get/set 3D properties
    get3DProperty: {
        value: function(el, prop) {
            if(el.elementModel.props3D) {
                return el.elementModel.props3D[prop];
            }
        }
    },

    getMatrix: {
        value: function(el) {
            if(el.elementModel.props3D && el.elementModel.props3D.matrix3d) {
                return el.elementModel.props3D.matrix3d.slice(0);
            } else {
                var mat;

                if (el) {
                    mat = this.application.ninja.stylesController.getMatrixFromElement(el, false);
                    if (!mat) {
                        mat = Matrix.I(4);
                    }
                }

                el.elementModel.props3D.matrix3d = mat;
                return mat;
            }
        }
    },

    getPerspectiveDist: {
        value: function(el) {
            if(el.elementModel.props3D && el.elementModel.props3D.perspectiveDist) {
                return el.elementModel.props3D.perspectiveDist;
            } else {
                var dist = this.application.ninja.stylesController.getPerspectiveDistFromElement(el, false);
                el.elementModel.props3D.perspectiveDist = dist;
                return dist;
            }
        }
    },

    // TODO - perspective distance needs to be passed in as "dist" and matrix3d needs to be passed in as "mat"
    set3DProperties: {
        value: function(el, props, update3DModel) {
            var dist = props["dist"],
                mat = props["mat"];

            this.application.ninja.stylesController.setElementStyle(el, "-webkit-transform", "matrix3d(" + MathUtils.scientificToDecimal(mat, 5) + ")");

            this.application.ninja.stylesController.setElementStyle(el, "-webkit-transform-style", "preserve-3d");

            // TODO - We don't support perspective on individual elements yet
            // this.application.ninja.stylesController.setElementStyle(el, "-webkit-perspective", dist);

            el.elementModel.props3D.matrix3d = mat;
            el.elementModel.props3D.perspectiveDist = dist;

            if(update3DModel) {
                this._update3DProperties(el, mat, dist);
            }
        }
    },

    _update3DProperties: {
        value: function(elt, mat, dist) {
            var elt3DInfo = MathUtils.decomposeMatrix2(mat);
            if(elt3DInfo)
            {
                elt.elementModel.props3D.xAngle = elt3DInfo.rotation[0] * MathUtils.RAD_TO_DEG;
                elt.elementModel.props3D.yAngle = elt3DInfo.rotation[1] * MathUtils.RAD_TO_DEG;
                elt.elementModel.props3D.zAngle = elt3DInfo.rotation[2] * MathUtils.RAD_TO_DEG;

                elt.elementModel.props3D.x3D = ~~(elt3DInfo.translation[0]);
                elt.elementModel.props3D.y3D = ~~(elt3DInfo.translation[1]);
                elt.elementModel.props3D.z3D = ~~(elt3DInfo.translation[2]);
            }
        }
    }

});