/
dom.js
280 lines (253 loc) · 8.06 KB
/
dom.js
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
var Crafty = require('../core/core.js'),
document = window.document;
/**@
* #DOM
* @category Graphics
* @kind Component
*
* A component which renders entities as DOM nodes, specifically `<div>`s.
*/
Crafty.c("DOM", {
/**@
* #._element
* @comp DOM
* @kind Property
* The DOM element used to represent the entity.
*/
_element: null,
//holds current styles, so we can check if there are changes to be written to the DOM
_cssStyles: null,
/**@
* #.avoidCss3dTransforms
* @comp DOM
* @kind Property
*
* Avoids using of CSS 3D Transform for positioning when true. Default value is false.
*/
avoidCss3dTransforms: false,
init: function () {
this.requires("Renderable");
this._cssStyles = {
visibility: '',
left: '',
top: '',
width: '',
height: '',
zIndex: '',
opacity: '',
transformOrigin: '',
transform: ''
};
this._element = document.createElement("div");
// Attach the entity to the dom layer
if (!this._customLayer){
this._attachToLayer( Crafty.s("DefaultDOMLayer") );
}
this.bind("NewComponent", this._updateClass);
this.bind("RemoveComponent", this._removeClass);
},
remove: function(){
this._detachFromLayer();
this.unbind("NewComponent", this._updateClass);
this.unbind("RemoveComponent", this._removeClass);
},
/**@
* #.getDomId
* @comp DOM
* @kind Method
*
* @sign public this .getDomId()
*
* Get the Id of the DOM element used to represent the entity.
*/
getDomId: function () {
return this._element.id;
},
// removes a component on RemoveComponent events
_removeClass: function(removedComponent) {
var comp,
c = this.__c,
str = "";
for (comp in c) {
if (comp !== removedComponent) {
str += ' ' + comp;
}
}
str = str.substr(1);
this._element.className = str;
},
// adds a class on NewComponent events
_updateClass: function() {
var comp,
c = this.__c,
str = "";
for (comp in c) {
str += ' ' + comp;
}
str = str.substr(1);
this._element.className = str;
},
/**@
* #.DOM
* @comp DOM
* @kind Method
*
* @trigger Draw - when the entity is ready to be drawn to the stage - { style:String, type:"DOM", co}
* @sign public this .DOM(HTMLElement elem)
* @param elem - HTML element that will replace the dynamically created one
*
* Pass a DOM element to use rather than one created. Will set `._element` to this value. Removes the old element.
*
* Will reattach the entity to the current draw layer
*/
DOM: function (elem) {
if (elem && elem.nodeType) {
var layer = this._drawLayer;
this._detachFromLayer();
this._element = elem;
this._attachToLayer(layer);
}
return this;
},
/**@
* #.draw
* @comp DOM
* @kind Method
* @private
*
* @sign public this .draw(void)
*
* Updates the CSS properties of the node to draw on the stage.
*/
draw: function () {
var style = this._element.style,
coord = this.__coord || [0, 0, 0, 0],
co = {
x: coord[0],
y: coord[1],
w: coord[2],
h: coord[3]
},
prefix = Crafty.support.prefix,
trans = [];
if (this._cssStyles.visibility !== this._visible) {
this._cssStyles.visibility = this._visible;
if (!this._visible) {
style.visibility = "hidden";
} else {
style.visibility = "visible";
}
}
//utilize CSS3 if supported
if (Crafty.support.css3dtransform && !this.avoidCss3dTransforms) {
trans.push("translate3d(" + (~~this._x) + "px," + (~~this._y) + "px,0)");
} else {
if (this._cssStyles.left !== this._x) {
this._cssStyles.left = this._x;
style.left = ~~ (this._x) + "px";
}
if (this._cssStyles.top !== this._y) {
this._cssStyles.top = this._y;
style.top = ~~ (this._y) + "px";
}
}
if (this._cssStyles.width !== this._w) {
this._cssStyles.width = this._w;
style.width = ~~ (this._w) + "px";
}
if (this._cssStyles.height !== this._h) {
this._cssStyles.height = this._h;
style.height = ~~ (this._h) + "px";
}
if (this._cssStyles.zIndex !== this._z) {
this._cssStyles.zIndex = this._z;
style.zIndex = this._z;
}
if (this._cssStyles.opacity !== this._alpha) {
this._cssStyles.opacity = this._alpha;
style.opacity = this._alpha;
style[prefix + "Opacity"] = this._alpha;
}
if (this._mbr) {
var origin = this._origin.x + "px " + this._origin.y + "px";
style.transformOrigin = origin;
style[prefix + "TransformOrigin"] = origin;
if (Crafty.support.css3dtransform) trans.push("rotateZ(" + this._rotation + "deg)");
else trans.push("rotate(" + this._rotation + "deg)");
}
if (this._flipX) {
trans.push("scaleX(-1)");
}
if (this._flipY) {
trans.push("scaleY(-1)");
}
if (this._cssStyles.transform !== trans.join(" ")) {
this._cssStyles.transform = trans.join(" ");
style.transform = this._cssStyles.transform;
style[prefix + "Transform"] = this._cssStyles.transform;
}
this.trigger("Draw", {
style: style,
type: "DOM",
co: co
});
return this;
},
/**@
* #.css
* @comp DOM
* @kind Method
*
* @sign public css(String property, String value)
* @param property - CSS property to modify
* @param value - Value to give the CSS property
*
* @sign public css(Object map)
* @param map - Object where the key is the CSS property and the value is CSS value
*
* Apply CSS styles to the element.
*
* Can pass an object where the key is the style property and the value is style value.
*
* For setting one style, simply pass the style as the first argument and the value as the second.
*
* The notation can be CSS or JS (e.g. `border-radius` or `borderRadius`).
*
* To return a value, pass the property.
*
* Note: For entities with "Text" component, some css properties are controlled by separate functions
* `.textFont()` and `.textColor()`, and ignore `.css()` settings. See Text component for details.
*
* @example
* ~~~
* this.css({'border-radius': '5px', 'text-decoration': 'line-through'});
* this.css("borderRadius", "10px");
* this.css("border-radius"); //returns 10px
* ~~~
*/
css: function (obj, value) {
var key,
elem = this._element,
val,
style = elem.style;
//if an object passed
if (typeof obj === "object") {
for (key in obj) {
if (!obj.hasOwnProperty(key)) continue;
val = obj[key];
if (typeof val === "number") val += 'px';
style[Crafty.domHelper.camelize(key)] = val;
}
} else {
//if a value is passed, set the property
if (value) {
if (typeof value === "number") value += 'px';
style[Crafty.domHelper.camelize(obj)] = value;
} else { //otherwise return the computed property
return Crafty.domHelper.getStyle(elem, obj);
}
}
this.trigger("Invalidate");
return this;
}
});