3
Copyright 2012 Yahoo! Inc. All rights reserved.
4
Licensed under the BSD License.
5
http://yuilibrary.com/license/
7
YUI.add('widget-stack', function(Y) {
10
* Provides stackable (z-index) support for Widgets through an extension.
12
* @module widget-stack
23
BOUNDING_BOX = "boundingBox",
25
RENDER_UI = "renderUI",
29
OFFSET_WIDTH = "offsetWidth",
30
OFFSET_HEIGHT = "offsetHeight",
31
PARENT_NODE = "parentNode",
32
FIRST_CHILD = "firstChild",
33
OWNER_DOCUMENT = "ownerDocument",
40
SHIM_DEFERRED = "shimdeferred",
41
SHIM_RESIZE = "shimresize",
44
VisibleChange = "visibleChange",
45
WidthChange = "widthChange",
46
HeightChange = "heightChange",
47
ShimChange = "shimChange",
48
ZIndexChange = "zIndexChange",
49
ContentUpdate = "contentUpdate",
55
* Widget extension, which can be used to add stackable (z-index) support to the
56
* base Widget class along with a shimming solution, through the
57
* <a href="Base.html#method_build">Base.build</a> method.
60
* @param {Object} User configuration object
62
function Stack(config) {
63
this._stackNode = this.get(BOUNDING_BOX);
64
this._stackHandles = {};
66
// WIDGET METHOD OVERLAP
67
Y.after(this._renderUIStack, this, RENDER_UI);
68
Y.after(this._syncUIStack, this, SYNC_UI);
69
Y.after(this._bindUIStack, this, BIND_UI);
74
* Static property used to define the default attribute
75
* configuration introduced by WidgetStack.
85
* @default false, for all browsers other than IE6, for which a shim is enabled by default.
87
* @description Boolean flag to indicate whether or not a shim should be added to the Widgets
88
* boundingBox, to protect it from select box bleedthrough.
98
* @description The z-index to apply to the Widgets boundingBox. Non-numerical values for
99
* zIndex will be converted to 0
108
* The HTML parsing rules for the WidgetStack class.
110
* @property HTML_PARSER
114
Stack.HTML_PARSER = {
115
zIndex: function (srcNode) {
116
return this._parseZIndex(srcNode);
121
* Default class used to mark the shim element
123
* @property SHIM_CLASS_NAME
126
* @default "yui3-widget-shim"
128
Stack.SHIM_CLASS_NAME = Widget.getClassName(SHIM);
131
* Default class used to mark the boundingBox of a stacked widget.
133
* @property STACKED_CLASS_NAME
136
* @default "yui3-widget-stacked"
138
Stack.STACKED_CLASS_NAME = Widget.getClassName(STACKED);
141
* Default markup template used to generate the shim element.
143
* @property SHIM_TEMPLATE
147
Stack.SHIM_TEMPLATE = '<iframe class="' + Stack.SHIM_CLASS_NAME + '" frameborder="0" title="Widget Stacking Shim" src="javascript:false" tabindex="-1" role="presentation"></iframe>';
152
* Synchronizes the UI to match the Widgets stack state. This method in
153
* invoked after syncUI is invoked for the Widget class using YUI's aop infrastructure.
155
* @method _syncUIStack
158
_syncUIStack: function() {
159
this._uiSetShim(this.get(SHIM));
160
this._uiSetZIndex(this.get(ZINDEX));
164
* Binds event listeners responsible for updating the UI state in response to
165
* Widget stack related state changes.
167
* This method is invoked after bindUI is invoked for the Widget class
168
* using YUI's aop infrastructure.
170
* @method _bindUIStack
173
_bindUIStack: function() {
174
this.after(ShimChange, this._afterShimChange);
175
this.after(ZIndexChange, this._afterZIndexChange);
179
* Creates/Initializes the DOM to support stackability.
181
* This method in invoked after renderUI is invoked for the Widget class
182
* using YUI's aop infrastructure.
184
* @method _renderUIStack
187
_renderUIStack: function() {
188
this._stackNode.addClass(Stack.STACKED_CLASS_NAME);
192
Parses a `zIndex` attribute value from this widget's `srcNode`.
195
@param {Node} srcNode The node to parse a `zIndex` value from.
196
@return {Mixed} The parsed `zIndex` value.
199
_parseZIndex: function (srcNode) {
202
// Prefers how WebKit handles `z-index` which better matches the
205
// * http://www.w3.org/TR/CSS2/visuren.html#z-index
206
// * https://bugs.webkit.org/show_bug.cgi?id=15562
208
// When a node isn't rendered in the document, and/or when a
209
// node is not positioned, then it doesn't have a context to derive
210
// a valid `z-index` value from.
211
if (!srcNode.inDoc() || srcNode.getStyle('position') === 'static') {
214
// Uses `getComputedStyle()` because it has greater accuracy in
215
// more browsers than `getStyle()` does for `z-index`.
216
zIndex = srcNode.getComputedStyle('zIndex');
219
// This extension adds a stacking context to widgets, therefore a
220
// `srcNode` witout a stacking context (i.e. "auto") will return
221
// `null` from this DOM parser. This way the widget's default or
222
// user provided value for `zIndex` will be used.
223
return zIndex === 'auto' ? null : zIndex;
227
* Default setter for zIndex attribute changes. Normalizes zIndex values to
228
* numbers, converting non-numerical values to 0.
232
* @param {String | Number} zIndex
233
* @return {Number} Normalized zIndex
235
_setZIndex: function(zIndex) {
236
if (L.isString(zIndex)) {
237
zIndex = parseInt(zIndex, 10);
239
if (!L.isNumber(zIndex)) {
246
* Default attribute change listener for the shim attribute, responsible
247
* for updating the UI, in response to attribute changes.
249
* @method _afterShimChange
251
* @param {EventFacade} e The event facade for the attribute change
253
_afterShimChange : function(e) {
254
this._uiSetShim(e.newVal);
258
* Default attribute change listener for the zIndex attribute, responsible
259
* for updating the UI, in response to attribute changes.
261
* @method _afterZIndexChange
263
* @param {EventFacade} e The event facade for the attribute change
265
_afterZIndexChange : function(e) {
266
this._uiSetZIndex(e.newVal);
270
* Updates the UI to reflect the zIndex value passed in.
272
* @method _uiSetZIndex
274
* @param {number} zIndex The zindex to be reflected in the UI
276
_uiSetZIndex: function (zIndex) {
277
this._stackNode.setStyle(ZINDEX, zIndex);
281
* Updates the UI to enable/disable the shim. If the widget is not currently visible,
282
* creation of the shim is deferred until it is made visible, for performance reasons.
286
* @param {boolean} enable If true, creates/renders the shim, if false, removes it.
288
_uiSetShim: function (enable) {
291
if (this.get(VISIBLE)) {
294
this._renderShimDeferred();
297
// Eagerly attach resize handlers
299
// Required because of Event stack behavior, commit ref: cd8dddc
300
// Should be revisted after Ticket #2531067 is resolved.
302
this._addShimResizeHandlers();
310
* Sets up change handlers for the visible attribute, to defer shim creation/rendering
311
* until the Widget is made visible.
313
* @method _renderShimDeferred
316
_renderShimDeferred : function() {
318
this._stackHandles[SHIM_DEFERRED] = this._stackHandles[SHIM_DEFERRED] || [];
320
var handles = this._stackHandles[SHIM_DEFERRED],
321
createBeforeVisible = function(e) {
327
handles.push(this.on(VisibleChange, createBeforeVisible));
328
// Depending how how Ticket #2531067 is resolved, a reversal of
329
// commit ref: cd8dddc could lead to a more elagent solution, with
330
// the addition of this line here:
332
// handles.push(this.after(VisibleChange, this.sizeShim));
336
* Sets up event listeners to resize the shim when the size of the Widget changes.
338
* NOTE: This method is only used for IE6 currently, since IE6 doesn't support a way to
339
* resize the shim purely through CSS, when the Widget does not have an explicit width/height
342
* @method _addShimResizeHandlers
345
_addShimResizeHandlers : function() {
347
this._stackHandles[SHIM_RESIZE] = this._stackHandles[SHIM_RESIZE] || [];
349
var sizeShim = this.sizeShim,
350
handles = this._stackHandles[SHIM_RESIZE];
352
handles.push(this.after(VisibleChange, sizeShim));
353
handles.push(this.after(WidthChange, sizeShim));
354
handles.push(this.after(HeightChange, sizeShim));
355
handles.push(this.after(ContentUpdate, sizeShim));
359
* Detaches any handles stored for the provided key
361
* @method _detachStackHandles
362
* @param String handleKey The key defining the group of handles which should be detached
365
_detachStackHandles : function(handleKey) {
366
var handles = this._stackHandles[handleKey],
369
if (handles && handles.length > 0) {
370
while((handle = handles.pop())) {
377
* Creates the shim element and adds it to the DOM
379
* @method _renderShim
382
_renderShim : function() {
383
var shimEl = this._shimNode,
384
stackEl = this._stackNode;
387
shimEl = this._shimNode = this._getShimTemplate();
388
stackEl.insertBefore(shimEl, stackEl.get(FIRST_CHILD));
390
this._detachStackHandles(SHIM_DEFERRED);
396
* Removes the shim from the DOM, and detaches any related event
399
* @method _destroyShim
402
_destroyShim : function() {
403
if (this._shimNode) {
404
this._shimNode.get(PARENT_NODE).removeChild(this._shimNode);
405
this._shimNode = null;
407
this._detachStackHandles(SHIM_DEFERRED);
408
this._detachStackHandles(SHIM_RESIZE);
413
* For IE6, synchronizes the size and position of iframe shim to that of
414
* Widget bounding box which it is protecting. For all other browsers,
415
* this method does not do anything.
419
sizeShim: function () {
420
var shim = this._shimNode,
421
node = this._stackNode;
423
if (shim && UA.ie === 6 && this.get(VISIBLE)) {
424
shim.setStyle(WIDTH, node.get(OFFSET_WIDTH) + PX);
425
shim.setStyle(HEIGHT, node.get(OFFSET_HEIGHT) + PX);
430
* Creates a cloned shim node, using the SHIM_TEMPLATE html template, for use on a new instance.
432
* @method _getShimTemplate
434
* @return {Node} node A new shim Node instance.
436
_getShimTemplate : function() {
437
return Node.create(Stack.SHIM_TEMPLATE, this._stackNode.get(OWNER_DOCUMENT));
441
Y.WidgetStack = Stack;
444
}, '3.5.1' ,{requires:['base-build', 'widget']});