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
|
/* <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,
NJComponent = require("js/lib/nj-base").NJComponent;
var ElementController = exports.ElementController = Montage.create(NJComponent, {
addElement: {
value: function(el, styles) {
this.application.ninja.currentDocument.documentRoot.appendChild(el);
// Nested elements -
// TODO make sure the CSS is correct before nesting elements
// this.application.ninja.currentSelectedContainer.appendChild(el);
this.application.ninja.stylesController.setElementStyles(el, styles);
}
},
removeElement: {
value: function(el) {
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(el, props, index) {
for(var p in props) {
this.application.ninja.stylesController.setElementStyle(el, p, props[p][index]);
}
}
},
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.setProperty(el, "border-image", color.color.css);
this.setProperty(el, "border-color", "none");
if(color.borderInfo)
{
this.setProperty(el, "border-width", color.borderInfo.borderWidth +
color.borderInfo.borderUnits);
this.setProperty(el, "border-style", color.borderInfo.borderStyle);
}
break;
default:
this.setProperty(el, "border-image", "none");
this.setProperty(el, "border-color", color.color.css);
if(color.borderInfo)
{
this.setProperty(el, "border-width", color.borderInfo.borderWidth +
color.borderInfo.borderUnits);
this.setProperty(el, "border-style", color.borderInfo.borderStyle);
}
}
}
el.elementModel.stroke = color;
}
}
},
getStroke: {
value: function(el) {
// TODO - Need to figure out which border side user wants
return this.application.ninja.stylesController.getElementStyle(el, "border");
}
},
setStroke: {
value: function(el, stroke) {
this.application.ninja.stylesController.setElementStyle(el, "border-width", stroke.borderWidth + stroke.borderUnits);
this.application.ninja.stylesController.setElementStyle(el, "border-style", stroke.borderStyle);
this.setColor(el, stroke.color, false);
}
},
//--------------------------------------------------------------------------------------------------------
// Routines to get/set 3D properties
get3DProperty: {
value: function(el, prop) {
if(el.elementModel && el.elementModel.props3D)
{
return el.elementModel.props3D[prop];
}
}
},
getMatrix: {
value: function(el) {
if(el.elementModel && 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 && el.elementModel.props3D && el.elementModel.props3D.perspectiveDist)
{
return el.elementModel.props3D.perspectiveDist;
}
else
{
var dist = this.application.ninja.stylesController.getPerspectiveDistFromElement(el, false);
if(dist == null) {
dist = 1400;
}
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, index, update3DModel) {
var dist = props[index]["dist"],
mat = props[index]["mat"];
this.application.ninja.stylesController.setElementStyle(el,
"-webkit-transform",
"perspective(" + dist + ") " +
"matrix3d(" + MathUtils.scientificToDecimal(mat, 5) + ")");
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]);
}
}
}
});
|