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