]>
Commit | Line | Data |
---|---|---|
6527f429 DM |
1 | /**\r |
2 | * @private\r | |
3 | */\r | |
4 | Ext.define('Ext.fx.runner.Css', {\r | |
5 | extend: 'Ext.Evented',\r | |
6 | \r | |
7 | requires: [\r | |
8 | 'Ext.fx.Animation'\r | |
9 | ],\r | |
10 | \r | |
11 | prefixedProperties: {\r | |
12 | 'transform' : true,\r | |
13 | 'transform-origin' : true,\r | |
14 | 'perspective' : true,\r | |
15 | 'transform-style' : true,\r | |
16 | 'transition' : true,\r | |
17 | 'transition-property' : true,\r | |
18 | 'transition-duration' : true,\r | |
19 | 'transition-timing-function': true,\r | |
20 | 'transition-delay' : true,\r | |
21 | 'animation' : true,\r | |
22 | 'animation-name' : true,\r | |
23 | 'animation-duration' : true,\r | |
24 | 'animation-iteration-count' : true,\r | |
25 | 'animation-direction' : true,\r | |
26 | 'animation-timing-function' : true,\r | |
27 | 'animation-delay' : true\r | |
28 | },\r | |
29 | \r | |
30 | lengthProperties: {\r | |
31 | 'top' : true,\r | |
32 | 'right' : true,\r | |
33 | 'bottom' : true,\r | |
34 | 'left' : true,\r | |
35 | 'width' : true,\r | |
36 | 'height' : true,\r | |
37 | 'max-height' : true,\r | |
38 | 'max-width' : true,\r | |
39 | 'min-height' : true,\r | |
40 | 'min-width' : true,\r | |
41 | 'margin-bottom' : true,\r | |
42 | 'margin-left' : true,\r | |
43 | 'margin-right' : true,\r | |
44 | 'margin-top' : true,\r | |
45 | 'padding-bottom' : true,\r | |
46 | 'padding-left' : true,\r | |
47 | 'padding-right' : true,\r | |
48 | 'padding-top' : true,\r | |
49 | 'border-bottom-width': true,\r | |
50 | 'border-left-width' : true,\r | |
51 | 'border-right-width' : true,\r | |
52 | 'border-spacing' : true,\r | |
53 | 'border-top-width' : true,\r | |
54 | 'border-width' : true,\r | |
55 | 'outline-width' : true,\r | |
56 | 'letter-spacing' : true,\r | |
57 | 'line-height' : true,\r | |
58 | 'text-indent' : true,\r | |
59 | 'word-spacing' : true,\r | |
60 | 'font-size' : true,\r | |
61 | 'translate' : true,\r | |
62 | 'translateX' : true,\r | |
63 | 'translateY' : true,\r | |
64 | 'translateZ' : true,\r | |
65 | 'translate3d' : true\r | |
66 | },\r | |
67 | \r | |
68 | durationProperties: {\r | |
69 | 'transition-duration' : true,\r | |
70 | 'transition-delay' : true,\r | |
71 | 'animation-duration' : true,\r | |
72 | 'animation-delay' : true\r | |
73 | },\r | |
74 | \r | |
75 | angleProperties: {\r | |
76 | rotate : true,\r | |
77 | rotateX : true,\r | |
78 | rotateY : true,\r | |
79 | rotateZ : true,\r | |
80 | skew : true,\r | |
81 | skewX : true,\r | |
82 | skewY : true\r | |
83 | },\r | |
84 | \r | |
85 | lengthUnitRegex: /([a-z%]*)$/,\r | |
86 | \r | |
87 | DEFAULT_UNIT_LENGTH: 'px',\r | |
88 | \r | |
89 | DEFAULT_UNIT_ANGLE: 'deg',\r | |
90 | \r | |
91 | DEFAULT_UNIT_DURATION: 'ms',\r | |
92 | \r | |
93 | formattedNameCache: {},\r | |
94 | \r | |
95 | transformMethods3d: [\r | |
96 | 'translateX', \r | |
97 | 'translateY', \r | |
98 | 'translateZ', \r | |
99 | 'rotate', \r | |
100 | 'rotateX', \r | |
101 | 'rotateY', \r | |
102 | 'rotateZ', \r | |
103 | 'skewX', \r | |
104 | 'skewY', \r | |
105 | 'scaleX', \r | |
106 | 'scaleY', \r | |
107 | 'scaleZ'\r | |
108 | ],\r | |
109 | \r | |
110 | transformMethodsNo3d: [\r | |
111 | 'translateX', \r | |
112 | 'translateY', \r | |
113 | 'rotate', \r | |
114 | 'skewX', \r | |
115 | 'skewY', \r | |
116 | 'scaleX', \r | |
117 | 'scaleY'\r | |
118 | ],\r | |
119 | \r | |
120 | constructor: function() {\r | |
121 | var me = this;\r | |
122 | \r | |
123 | me.transformMethods = Ext.feature.has.Css3dTransforms ? me.transformMethods3d : me.transformMethodsNo3d;\r | |
124 | me.vendorPrefix = Ext.browser.getStyleDashPrefix();\r | |
125 | me.ruleStylesCache = {};\r | |
126 | \r | |
127 | me.callParent();\r | |
128 | },\r | |
129 | \r | |
130 | getStyleSheet: function() {\r | |
131 | var styleSheet = this.styleSheet,\r | |
132 | styleElement, styleSheets;\r | |
133 | \r | |
134 | if (!styleSheet) {\r | |
135 | styleElement = document.createElement('style');\r | |
136 | styleElement.type = 'text/css';\r | |
137 | \r | |
138 | (document.head || document.getElementsByTagName('head')[0]).appendChild(styleElement);\r | |
139 | \r | |
140 | styleSheets = document.styleSheets;\r | |
141 | \r | |
142 | this.styleSheet = styleSheet = styleSheets[styleSheets.length - 1];\r | |
143 | }\r | |
144 | \r | |
145 | return styleSheet;\r | |
146 | },\r | |
147 | \r | |
148 | applyRules: function(selectors) {\r | |
149 | var styleSheet = this.getStyleSheet(),\r | |
150 | ruleStylesCache = this.ruleStylesCache,\r | |
151 | rules = styleSheet.cssRules,\r | |
152 | selector, properties, ruleStyle,\r | |
153 | ruleStyleCache, rulesLength, name, value;\r | |
154 | \r | |
155 | for (selector in selectors) {\r | |
156 | properties = selectors[selector];\r | |
157 | \r | |
158 | ruleStyle = ruleStylesCache[selector];\r | |
159 | \r | |
160 | if (ruleStyle === undefined) {\r | |
161 | rulesLength = rules.length;\r | |
162 | styleSheet.insertRule(selector + '{}', rulesLength);\r | |
163 | ruleStyle = ruleStylesCache[selector] = rules.item(rulesLength).style;\r | |
164 | }\r | |
165 | \r | |
166 | ruleStyleCache = ruleStyle.$cache;\r | |
167 | \r | |
168 | if (!ruleStyleCache) {\r | |
169 | ruleStyleCache = ruleStyle.$cache = {};\r | |
170 | }\r | |
171 | \r | |
172 | for (name in properties) {\r | |
173 | value = this.formatValue(properties[name], name);\r | |
174 | name = this.formatName(name);\r | |
175 | \r | |
176 | if (ruleStyleCache[name] !== value) {\r | |
177 | ruleStyleCache[name] = value;\r | |
178 | \r | |
179 | if (value === null) {\r | |
180 | ruleStyle.removeProperty(name);\r | |
181 | }\r | |
182 | else {\r | |
183 | ruleStyle.setProperty(name, value, 'important');\r | |
184 | }\r | |
185 | }\r | |
186 | }\r | |
187 | }\r | |
188 | \r | |
189 | return this;\r | |
190 | },\r | |
191 | \r | |
192 | applyStyles: function(styles) {\r | |
193 | var id, element, elementStyle, properties, name, value;\r | |
194 | \r | |
195 | for (id in styles) {\r | |
196 | if (styles.hasOwnProperty(id)) {\r | |
197 | element = document.getElementById(id);\r | |
198 | \r | |
199 | if (!element) {\r | |
200 | continue;\r | |
201 | }\r | |
202 | \r | |
203 | elementStyle = element.style;\r | |
204 | \r | |
205 | properties = styles[id];\r | |
206 | for (name in properties) {\r | |
207 | if (properties.hasOwnProperty(name)) {\r | |
208 | value = this.formatValue(properties[name], name);\r | |
209 | name = this.formatName(name);\r | |
210 | \r | |
211 | if (value === null) {\r | |
212 | elementStyle.removeProperty(name);\r | |
213 | }\r | |
214 | else {\r | |
215 | elementStyle.setProperty(name, value, 'important');\r | |
216 | }\r | |
217 | }\r | |
218 | }\r | |
219 | }\r | |
220 | }\r | |
221 | \r | |
222 | return this;\r | |
223 | },\r | |
224 | \r | |
225 | formatName: function(name) {\r | |
226 | var cache = this.formattedNameCache,\r | |
227 | formattedName = cache[name];\r | |
228 | \r | |
229 | if (!formattedName) {\r | |
230 | if ((Ext.os.is.Tizen || !Ext.feature.has.CssTransformNoPrefix) && this.prefixedProperties[name]) {\r | |
231 | formattedName = this.vendorPrefix + name;\r | |
232 | }\r | |
233 | else {\r | |
234 | formattedName = name;\r | |
235 | }\r | |
236 | \r | |
237 | cache[name] = formattedName;\r | |
238 | }\r | |
239 | \r | |
240 | return formattedName;\r | |
241 | },\r | |
242 | \r | |
243 | formatValue: function(value, name) {\r | |
244 | var type = typeof value,\r | |
245 | lengthUnit = this.DEFAULT_UNIT_LENGTH,\r | |
246 | transformMethods,\r | |
247 | method, i, ln,\r | |
248 | transformValues, values, unit;\r | |
249 | \r | |
250 | if (value === null) {\r | |
251 | return '';\r | |
252 | }\r | |
253 | \r | |
254 | if (type == 'string') {\r | |
255 | if (this.lengthProperties[name]) {\r | |
256 | unit = value.match(this.lengthUnitRegex)[1];\r | |
257 | \r | |
258 | if (unit.length > 0) {\r | |
259 | //<debug>\r | |
260 | if (unit !== lengthUnit) {\r | |
261 | Ext.Logger.error("Length unit: '" + unit + "' in value: '" + value + "' of property: '" + name + "' is not " +\r | |
262 | "valid for animation. Only 'px' is allowed");\r | |
263 | }\r | |
264 | //</debug>\r | |
265 | }\r | |
266 | else {\r | |
267 | return value + lengthUnit;\r | |
268 | }\r | |
269 | }\r | |
270 | \r | |
271 | return value;\r | |
272 | }\r | |
273 | else if (type == 'number') {\r | |
274 | if (value == 0) {\r | |
275 | return '0';\r | |
276 | }\r | |
277 | \r | |
278 | if (this.lengthProperties[name]) {\r | |
279 | return value + lengthUnit;\r | |
280 | }\r | |
281 | \r | |
282 | if (this.angleProperties[name]) {\r | |
283 | return value + this.DEFAULT_UNIT_ANGLE;\r | |
284 | }\r | |
285 | \r | |
286 | if (this.durationProperties[name]) {\r | |
287 | return value + this.DEFAULT_UNIT_DURATION;\r | |
288 | }\r | |
289 | }\r | |
290 | else if (name === 'transform') {\r | |
291 | transformMethods = this.transformMethods;\r | |
292 | transformValues = [];\r | |
293 | \r | |
294 | for (i = 0,ln = transformMethods.length; i < ln; i++) {\r | |
295 | method = transformMethods[i];\r | |
296 | \r | |
297 | transformValues.push(method + '(' + this.formatValue(value[method], method) + ')');\r | |
298 | }\r | |
299 | \r | |
300 | return transformValues.join(' ');\r | |
301 | }\r | |
302 | else if (Ext.isArray(value)) {\r | |
303 | values = [];\r | |
304 | \r | |
305 | for (i = 0,ln = value.length; i < ln; i++) {\r | |
306 | values.push(this.formatValue(value[i], name));\r | |
307 | }\r | |
308 | \r | |
309 | return (values.length > 0) ? values.join(', ') : 'none';\r | |
310 | }\r | |
311 | \r | |
312 | return value;\r | |
313 | }\r | |
314 | });\r |