aboutsummaryrefslogtreecommitdiff
path: root/js/controllers/elements/element-controller.js
diff options
context:
space:
mode:
authorPierre Frisch2011-12-22 07:25:50 -0800
committerValerio Virgillito2012-01-27 11:18:17 -0800
commitb89a7ee8b956c96a1dcee995ea840feddc5d4b27 (patch)
tree0f3136ab0ecdbbbed6a83576581af0a53124d6f1 /js/controllers/elements/element-controller.js
parent2401f05d1f4b94d45e4568b81fc73e67b969d980 (diff)
downloadninja-b89a7ee8b956c96a1dcee995ea840feddc5d4b27.tar.gz
First commit of Ninja to ninja-internal
Signed-off-by: Valerio Virgillito <rmwh84@motorola.com>
Diffstat (limited to 'js/controllers/elements/element-controller.js')
-rw-r--r--js/controllers/elements/element-controller.js263
1 files changed, 263 insertions, 0 deletions
diff --git a/js/controllers/elements/element-controller.js b/js/controllers/elements/element-controller.js
new file mode 100644
index 00000000..f254220c
--- /dev/null
+++ b/js/controllers/elements/element-controller.js
@@ -0,0 +1,263 @@
1/* <copyright>
2This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
3No 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
7var Montage = require("montage/core/core").Montage,
8 NJComponent = require("js/lib/nj-base").NJComponent;
9
10var ElementController = exports.ElementController = Montage.create(NJComponent, {
11
12 addElement: {
13 value: function(el, styles) {
14 this.application.ninja.currentDocument.documentRoot.appendChild(el);
15 this.application.ninja.stylesController.setElementStyles(el, styles);
16 }
17 },
18
19 removeElement: {
20 value: function(el) {
21 el.parentNode.removeChild(el);
22 }
23 },
24
25 getProperty: {
26 value: function(el, prop, fallbackOnComputed, isStageElement) {
27 return this.application.ninja.stylesController.getElementStyle(el, prop, fallbackOnComputed, isStageElement);
28 }
29 },
30
31 setProperty: {
32 value: function(el, p, value) {
33 this.application.ninja.stylesController.setElementStyle(el, p, value);
34 }
35 },
36
37 setProperties: {
38 value: function(el, props, index) {
39 for(var p in props) {
40 this.application.ninja.stylesController.setElementStyle(el, p, props[p][index]);
41 }
42 }
43 },
44
45 setAttribute: {
46 value: function(el, att, value) {
47 if(att === "id") {
48 if(value === "") {
49 el.setAttribute(att, value);
50 return;
51 }
52
53 // Then check if this is a valid id by the following spec: http://www.w3.org/TR/REC-html40/types.html#h-6.2
54 var regexID = /^([a-zA-Z])+([a-zA-Z0-9_\.\:\-])+/;
55 if(!regexID.test(value)) {
56 alert("Invalid ID");
57 return;
58 } else if (this.application.ninja.currentDocument._document.getElementById(value) !== null) {
59 alert("The following ID: " + value + " is already in Use");
60 }
61
62 }
63
64 el.setAttribute(att, value);
65 }
66 },
67
68 //--------------------------------------------------------------------------------------------------------
69 // Routines to get/set color properties
70 getColor: {
71 value: function(el, isFill) {
72 if(isFill)
73 {
74 return this.application.ninja.stylesController.getElementStyle(el, "background-color");
75 }
76 else
77 {
78 // TODO - Need to figure out which border side user wants
79 return this.application.ninja.stylesController.getElementStyle(el, "border-color");
80 }
81 }
82 },
83
84 setColor: {
85 value: function(el, color, isFill) {
86 if(isFill)
87 {
88 this.application.ninja.stylesController.setElementStyle(el, "background-color", color.color.css);
89 }
90 else
91 {
92 this.application.ninja.stylesController.setElementStyle(el, "border-color", color.color.css);
93 }
94 }
95 },
96
97 getStroke: {
98 value: function(el) {
99 // TODO - Need to figure out which border side user wants
100 return this.application.ninja.stylesController.getElementStyle(el, "border");
101 }
102 },
103
104 setStroke: {
105 value: function(el, stroke) {
106 var border = stroke.borderWidth + stroke.borderUnits + " " + stroke.borderStyle + " " + stroke.color.color.css;
107 this.application.ninja.stylesController.setElementStyle(el, "border", border);
108 }
109 },
110
111 //--------------------------------------------------------------------------------------------------------
112 // Routines to get/set 3D properties
113 get3DProperty: {
114 value: function(el, prop) {
115 if(el.elementModel && el.elementModel.props3D)
116 {
117 return el.elementModel.props3D[prop];
118 }
119 }
120 },
121
122 getMatrix: {
123 value: function(el) {
124 if(el.elementModel && el.elementModel.props3D && el.elementModel.props3D.matrix3d)
125 {
126 return el.elementModel.props3D.matrix3d.slice(0);
127 }
128 else
129 {
130 // TODO - for now, just return the identity matrix
131 return Matrix.I(4);
132// var mat;
133//
134// if (elt)
135// {
136// var xformStr = ElementsMediator.getProperty(elt, "-webkit-transform");
137// if (xformStr)
138// mat = this.transformStringToMat( xformStr );
139// if (!mat)
140// mat = Matrix.I(4);
141//
142// if (elt.style && elt.style.zoom)
143// {
144// var zoom = Number(elt.style.zoom);
145// if (zoom != 1)
146// {
147// var zoomMat = Matrix.create( [
148// [ zoom, 0, 0, 0],
149// [ 0, zoom, 0, 0],
150// [ 0, 0, zoom, 0],
151// [ 0, 0, 0, 1]
152// ] );
153// glmat4.multiply( zoomMat, mat, mat );
154// }
155// }
156// }
157//
158// elt.elementModel.props3D.matrix3d = mat;
159// return mat;
160 }
161 }
162 },
163
164 getPerspectiveDist: {
165 value: function(el) {
166 if(el.elementModel && el.elementModel.props3D && el.elementModel.props3D.perspectiveDist)
167 {
168 return el.elementModel.props3D.perspectiveDist;
169 }
170 else
171 {
172 var dist = 1400;
173
174 var str = this.getProperty(el, "-webkit-transform");
175 if (str)
176 {
177 var index1 = str.indexOf( "perspective(");
178 if (index1 >= 0)
179 {
180 index1 += 12; // do not include 'perspective('
181 var index2 = str.indexOf( ")", index1 );
182 if (index2 >= 0)
183 {
184 var substr = str.substr( index1, (index2-index1));
185 if (substr && (substr.length > 0))
186 dist = MathUtils.styleToNumber( substr );
187 }
188 }
189 }
190
191 el.elementModel.props3D.perspectiveDist = dist;
192 return dist;
193 }
194 }
195 },
196
197 // TODO - perspective distance needs to be passed in as "dist" and matrix3d needs to be passed in as "mat"
198 set3DProperties: {
199 value: function(el, props, index, update3DModel) {
200 var dist = props[index]["dist"],
201 mat = props[index]["mat"];
202 this.application.ninja.stylesController.setElementStyle(el,
203 "-webkit-transform",
204 "perspective(" + dist + ") " +
205 "matrix3d(" + MathUtils.scientificToDecimal(mat, 5) + ")");
206
207 el.elementModel.props3D.matrix3d = mat;
208 el.elementModel.props3D.perspectiveDist = dist;
209
210// if(update3DModel)
211 {
212 this._update3DProperties(el, mat, dist);
213 }
214 }
215 },
216
217 _update3DProperties: {
218 value: function(elt, mat, dist) {
219 var elt3DInfo = MathUtils.decomposeMatrix2(mat);