forked from lojjic/PIE
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Element.js
310 lines (268 loc) · 11.3 KB
/
Element.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
PIE.Element = (function() {
var wrappers = {};
function Element( el ) {
var lastW, lastH, lastX, lastY,
renderers,
styleInfos,
ancestors,
initializing,
initialized,
delayed;
/**
* Initialize PIE for this element.
*/
function init() {
if( !initialized ) {
var doc = el.document,
docEl = doc.documentElement || doc.body,
rect,
rootRenderer;
// Force layout so move/resize events will fire. Set this as soon as possible to avoid layout changes
// after load, but make sure it only gets called the first time through to avoid recursive calls to init().
if( !initializing ) {
initializing = 1;
el.runtimeStyle.zoom = 1;
initFirstChildPseudoClass();
el.attachEvent( 'onmove', update );
el.attachEvent( 'onresize', update );
el.attachEvent( 'onpropertychange', propChanged );
el.attachEvent( 'onmouseenter', mouseEntered );
el.attachEvent( 'onmouseleave', mouseLeft );
}
rect = el.getBoundingClientRect();
// Check if the element is in the viewport; if not, delay initialization
if( rect.top > docEl.clientHeight || rect.left > docEl.clientWidth || rect.bottom < 0 || rect.right < 0 ) {
if( !delayed ) {
delayed = 1;
PIE.OnScroll.observe( init );
}
} else {
initialized = 1;
delayed = initializing = 0;
PIE.OnScroll.unobserve( init );
// Create the style infos and renderers
styleInfos = {
backgroundInfo: new PIE.BackgroundStyleInfo( el ),
borderInfo: new PIE.BorderStyleInfo( el ),
borderImageInfo: new PIE.BorderImageStyleInfo( el ),
borderRadiusInfo: new PIE.BorderRadiusStyleInfo( el ),
boxShadowInfo: new PIE.BoxShadowStyleInfo( el ),
visibilityInfo: new PIE.VisibilityStyleInfo( el )
};
rootRenderer = new PIE.RootRenderer( el, styleInfos );
renderers = [
rootRenderer,
new PIE.BoxShadowOutsetRenderer( el, styleInfos, rootRenderer ),
new PIE.BackgroundRenderer( el, styleInfos, rootRenderer ),
new PIE.BoxShadowInsetRenderer( el, styleInfos, rootRenderer ),
new PIE.BorderRenderer( el, styleInfos, rootRenderer ),
new PIE.BorderImageRenderer( el, styleInfos, rootRenderer )
];
// Add property change listeners to ancestors if requested
initAncestorPropChangeListeners();
// Add to list of polled elements in IE8
if( PIE.ie8DocMode === 8 ) {
PIE.Heartbeat.observe( update );
}
// Listen for window resize events
PIE.OnResize.observe( update );
// Trigger rendering
update();
}
}
}
/**
* Update position and/or size as necessary. Both move and resize events call
* this rather than the updatePos/Size functions because sometimes, particularly
* during page load, one will fire but the other won't.
*/
function update() {
if( initialized ) {
/* TODO just using getBoundingClientRect may not always be accurate; it's possible that
an element will actually move relative to its positioning parent, but its position
relative to the viewport will stay the same. Need to come up with a better way to
track movement. The most accurate would be the same logic used in RootRenderer.updatePos()
but that is a more expensive operation since it does some DOM walking, and we want this
check to be as fast as possible. */
var rect = el.getBoundingClientRect(),
x = rect.left,
y = rect.top,
w = rect.right - x,
h = rect.bottom - y,
i, len;
if( x !== lastX || y !== lastY ) {
for( i = 0, len = renderers.length; i < len; i++ ) {
renderers[i].updatePos();
}
lastX = x;
lastY = y;
}
if( w !== lastW || h !== lastH ) {
for( i = 0, len = renderers.length; i < len; i++ ) {
renderers[i].updateSize();
}
lastW = w;
lastH = h;
}
}
else if( !initializing ) {
init();
}
}
/**
* Handle property changes to trigger update when appropriate.
*/
function propChanged() {
if( initialized ) {
var i, len,
toUpdate = [];
for( i = 0, len = renderers.length; i < len; i++ ) {
if( renderers[i].needsUpdate() ) {
toUpdate.push( renderers[i] );
}
}
for( i = 0, len = toUpdate.length; i < len; i++ ) {
toUpdate[i].updateProps();
}
}
else if( !initializing ) {
init();
}
}
/**
* Handle mouseenter events. Adds a custom class to the element to allow IE6 to add
* hover styles to non-link elements.
*/
function mouseEntered() {
if( PIE.ieVersion === 6 && el.tagName !== 'A' ) {
el.className += ' ' + PIE.CLASS_PREFIX + 'hover';
}
//must delay this because the mouseleave event fires before the :hover styles are added.
setTimeout( propChanged, 0 );
}
/**
* Handle mouseleave events
*/
function mouseLeft() {
if( PIE.ieVersion === 6 && el.tagName !== 'A' ) {
el.className = el.className.replace( PIE.hoverClassRE, '' );
}
//must delay this because the mouseleave event fires before the :hover styles are removed.
setTimeout( propChanged, 0 );
}
/**
* Handle property changes on ancestors of the element; see initAncestorPropChangeListeners()
* which adds these listeners as requested with the -pie-watch-ancestors CSS property.
*/
function ancestorPropChanged() {
var name = event.propertyName;
if( name === 'className' || name === 'id' ) {
propChanged();
}
}
/**
* Clean everything up when the behavior is removed from the element, or the element
* is destroyed.
*/
function destroy() {
var i, len;
// destroy any active renderers
if( renderers ) {
for( i = 0, len = renderers.length; i < len; i++ ) {
renderers[i].destroy();
}
renderers = null;
}
styleInfos = null;
// remove any ancestor propertychange listeners
if( ancestors ) {
for( i = 0, len = ancestors.length; i < len; i++ ) {
ancestors[i].detachEvent( 'onpropertychange', ancestorPropChanged );
ancestors[i].detachEvent( 'onmouseenter', mouseEntered );
ancestors[i].detachEvent( 'onmouseleave', mouseLeft );
}
ancestors = null;
}
// Remove from list of polled elements in IE8
if( PIE.ie8DocMode === 8 ) {
PIE.Heartbeat.unobserve( update );
}
// Stop onscroll/onresize listening
PIE.OnScroll.unobserve( update );
PIE.OnResize.unobserve( update );
el.detachEvent( 'onmove', update );
el.detachEvent( 'onresize', update );
el.detachEvent( 'onpropertychange', propChanged );
el.detachEvent( 'onmouseenter', mouseEntered );
el.detachEvent( 'onmouseleave', mouseLeft );
}
/**
* If requested via the custom -pie-watch-ancestors CSS property, add onpropertychange listeners
* to ancestor(s) of the element so we can pick up style changes based on CSS rules using
* descendant selectors.
*/
function initAncestorPropChangeListeners() {
var watch = el.currentStyle.getAttribute( PIE.CSS_PREFIX + 'watch-ancestors' ),
i, a;
if( watch ) {
ancestors = [];
watch = parseInt( watch, 10 );
i = 0;
a = el.parentNode;
while( a && ( watch === 'NaN' || i++ < watch ) ) {
ancestors.push( a );
a.attachEvent( 'onpropertychange', ancestorPropChanged );
a.attachEvent( 'onmouseenter', mouseEntered );
a.attachEvent( 'onmouseleave', mouseLeft );
a = a.parentNode;
}
}
}
/**
* If the target element is a first child, add a pie_first-child class to it. This allows using
* the added class as a workaround for the fact that PIE's rendering element breaks the :first-child
* pseudo-class selector.
*/
function initFirstChildPseudoClass() {
var tmpEl = el,
isFirst = 1;
while( tmpEl = tmpEl.previousSibling ) {
if( tmpEl.nodeType === 1 ) {
isFirst = 0;
break;
}
}
if( isFirst ) {
el.className += ' ' + PIE.CLASS_PREFIX + 'first-child';
}
}
// Public methods - these are all already bound to this instance so there's no need to wrap them
// in a closure to maintain the 'this' scope object.
this.init = init;
this.update = update;
this.destroy = destroy;
}
Element.getInstance = function( el ) {
var id = PIE.Util.getUID( el );
return wrappers[ id ] || ( wrappers[ id ] = new Element( el ) );
};
Element.destroy = function( el ) {
var id = PIE.Util.getUID( el ),
wrapper = wrappers[ id ];
if( wrapper ) {
wrapper.destroy();
delete wrappers[ id ];
}
};
// Destroy all Element objects when leaving the page
window.attachEvent( 'onbeforeunload', function() {
if( wrappers ) {
for( var w in wrappers ) {
if( wrappers.hasOwnProperty( w ) ) {
wrappers[ w ].destroy();
}
}
}
} );
return Element;
})();