forked from lojjic/PIE
/
RootRenderer.js
199 lines (178 loc) · 6.63 KB
/
RootRenderer.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
/**
* Root renderer; creates the outermost container element and handles keeping it aligned
* with the target element's size and position.
* @param {Element} el The target element
* @param {Object} styleInfos The StyleInfo objects
*/
PIE.RootRenderer = PIE.RendererBase.newRenderer( {
/**
* Flag indicating the element has already been positioned at least once.
* @type {boolean}
*/
isPositioned: false,
isActive: function() {
var children = this.childRenderers;
for( var i in children ) {
if( children.hasOwnProperty( i ) && children[ i ].isActive() ) {
return true;
}
}
return false;
},
needsUpdate: function() {
return this.styleInfos.visibilityInfo.changed();
},
/**
* Tell the renderer to update based on modified element position
*/
updatePos: function() {
if( this.isActive() && this.getBoxEl() ) {
var el = this.getPositioningElement(),
par = el,
docEl,
parRect,
tgtCS = el.currentStyle,
tgtPos = tgtCS.position,
boxPos,
s = this.getBoxEl().style, cs,
x = 0, y = 0,
elBounds = this.boundsInfo.getBounds(),
logicalZoomRatio = elBounds.logicalZoomRatio;
if( tgtPos === 'fixed' && PIE.ieVersion > 6 ) {
x = elBounds.x * logicalZoomRatio;
y = elBounds.y * logicalZoomRatio;
boxPos = tgtPos;
} else {
// Get the element's offsets from its nearest positioned ancestor. Uses
// getBoundingClientRect for accuracy and speed.
do {
par = par.offsetParent;
} while( par && ( par.currentStyle.position === 'static' ) );
if( par ) {
parRect = par.getBoundingClientRect();
cs = par.currentStyle;
x = ( elBounds.x - parRect.left ) * logicalZoomRatio - ( parseFloat(cs.borderLeftWidth) || 0 );
y = ( elBounds.y - parRect.top ) * logicalZoomRatio - ( parseFloat(cs.borderTopWidth) || 0 );
} else {
docEl = doc.documentElement;
x = ( elBounds.x + docEl.scrollLeft - docEl.clientLeft ) * logicalZoomRatio;
y = ( elBounds.y + docEl.scrollTop - docEl.clientTop ) * logicalZoomRatio;
}
boxPos = 'absolute';
}
s.position = boxPos;
s.left = x;
s.top = y;
s.zIndex = tgtPos === 'static' ? -1 : tgtCS.zIndex;
this.isPositioned = true;
}
},
updateVisibility: function() {
var vis = this.styleInfos.visibilityInfo,
box = this._box;
if ( box && vis.changed() ) {
vis = vis.getProps();
box.style.display = ( vis.visible && vis.displayed ) ? '' : 'none';
}
},
updateRendering: function() {
if( this.isActive() ) {
this.updateVisibility();
} else {
this.destroy();
}
},
getPositioningElement: function() {
var el = this.targetElement;
return el.tagName in PIE.tableCellTags ? el.offsetParent : el;
},
/**
* Get a reference to the css3pie container element that contains the VML shapes,
* if it has been inserted.
*/
getBoxEl: function() {
var box = this._box;
if( !box ) {
box = this._box = doc.getElementById( '_pie' + PIE.Util.getUID( this ) );
}
return box;
},
/**
* Render any child rendrerer shapes which have not already been rendered into the DOM.
*/
finishUpdate: function() {
var me = this,
queue = me._shapeRenderQueue,
renderedShapes, markup, i, len, j,
ref, pos;
if( queue ) {
// We've already rendered something once, so do incremental insertion of new shapes
renderedShapes = me._renderedShapes;
if( renderedShapes ) {
for( i = 0, len = queue.length; i < len; i++ ) {
for( j = renderedShapes.length; j--; ) {
if( renderedShapes[ j ].ordinalGroup < queue[ i ].ordinalGroup ) {
break;
}
}
if ( j < 0 ) {
ref = me.getBoxEl();
pos = 'afterBegin';
} else {
ref = renderedShapes[ j ].getShape();
pos = 'afterEnd';
}
ref.insertAdjacentHTML( pos, queue[ i ].getMarkup() );
renderedShapes.splice( j < 0 ? 0 : j, 0, queue[ i ] );
}
}
// This is the first render, so build up a single markup string and insert it all at once
else {
queue.sort( me.shapeSorter );
markup = [ '<css3pie id="_pie' + PIE.Util.getUID( me ) + '" style="direction:ltr;position:absolute;">' ];
for( i = 0, len = queue.length; i < len; i++ ) {
markup.push( queue[ i ].getMarkup() );
}
markup.push( '</css3pie>' );
me.getPositioningElement().insertAdjacentHTML( 'beforeBegin', markup.join( '' ) );
me._renderedShapes = queue;
}
me._shapeRenderQueue = 0;
}
},
shapeSorter: function( shape1, shape2 ) {
return shape1.ordinalGroup - shape2.ordinalGroup;
},
/**
* Add a VmlShape into the queue to get rendered in finishUpdate
*/
enqueueShapeForRender: function( shape ) {
var me = this,
queue = me._shapeRenderQueue || ( me._shapeRenderQueue = [] );
queue.push( shape );
},
/**
* Remove a VmlShape from the DOM and also from the internal list of rendered shapes.
*/
removeShape: function( shape ) {
var shapes = this._renderedShapes, i;
if ( shapes ) {
for( i = shapes.length; i--; ) {
if( shapes[ i ] === shape ) {
shapes.splice( i, 1 );
break;
}
}
}
},
destroy: function() {
var box = this._box, par;
if( box && ( par = box.parentNode ) ) {
par.removeChild( box );
}
delete this._box;
delete this._renderedShapes;
}
} );
// Prime IE for recognizing the custom <css3pie> element
doc.createElement( 'css3pie' );