2 YUI 3.13.0 (build 508226d)
3 Copyright 2013 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
8 YUI.add('widget-stack', function (Y, NAME) {
11 * Provides stackable (z-index) support for Widgets through an extension.
13 * @module widget-stack
24 BOUNDING_BOX = "boundingBox",
26 RENDER_UI = "renderUI",
30 OFFSET_WIDTH = "offsetWidth",
31 OFFSET_HEIGHT = "offsetHeight",
32 PARENT_NODE = "parentNode",
33 FIRST_CHILD = "firstChild",
34 OWNER_DOCUMENT = "ownerDocument",
41 SHIM_DEFERRED = "shimdeferred",
42 SHIM_RESIZE = "shimresize",
45 VisibleChange = "visibleChange",
46 WidthChange = "widthChange",
47 HeightChange = "heightChange",
48 ShimChange = "shimChange",
49 ZIndexChange = "zIndexChange",
50 ContentUpdate = "contentUpdate",
56 * Widget extension, which can be used to add stackable (z-index) support to the
57 * base Widget class along with a shimming solution, through the
58 * <a href="Base.html#method_build">Base.build</a> method.
61 * @param {Object} User configuration object
63 function Stack(config) {}
67 * Static property used to define the default attribute
68 * configuration introduced by WidgetStack.
78 * @default false, for all browsers other than IE6, for which a shim is enabled by default.
80 * @description Boolean flag to indicate whether or not a shim should be added to the Widgets
81 * boundingBox, to protect it from select box bleedthrough.
91 * @description The z-index to apply to the Widgets boundingBox. Non-numerical values for
92 * zIndex will be converted to 0
101 * The HTML parsing rules for the WidgetStack class.
103 * @property HTML_PARSER
107 Stack.HTML_PARSER = {
108 zIndex: function (srcNode) {
109 return this._parseZIndex(srcNode);
114 * Default class used to mark the shim element
116 * @property SHIM_CLASS_NAME
119 * @default "yui3-widget-shim"
121 Stack.SHIM_CLASS_NAME = Widget.getClassName(SHIM);
124 * Default class used to mark the boundingBox of a stacked widget.
126 * @property STACKED_CLASS_NAME
129 * @default "yui3-widget-stacked"
131 Stack.STACKED_CLASS_NAME = Widget.getClassName(STACKED);
134 * Default markup template used to generate the shim element.
136 * @property SHIM_TEMPLATE
140 Stack.SHIM_TEMPLATE = '<iframe class="' + Stack.SHIM_CLASS_NAME + '" frameborder="0" title="Widget Stacking Shim" src="javascript:false" tabindex="-1" role="presentation"></iframe>';
144 initializer : function() {
145 this._stackNode = this.get(BOUNDING_BOX);
146 this._stackHandles = {};
148 // WIDGET METHOD OVERLAP
149 Y.after(this._renderUIStack, this, RENDER_UI);
150 Y.after(this._syncUIStack, this, SYNC_UI);
151 Y.after(this._bindUIStack, this, BIND_UI);
155 * Synchronizes the UI to match the Widgets stack state. This method in
156 * invoked after syncUI is invoked for the Widget class using YUI's aop infrastructure.
158 * @method _syncUIStack
161 _syncUIStack: function() {
162 this._uiSetShim(this.get(SHIM));
163 this._uiSetZIndex(this.get(ZINDEX));
167 * Binds event listeners responsible for updating the UI state in response to
168 * Widget stack related state changes.
170 * This method is invoked after bindUI is invoked for the Widget class
171 * using YUI's aop infrastructure.
173 * @method _bindUIStack
176 _bindUIStack: function() {
177 this.after(ShimChange, this._afterShimChange);
178 this.after(ZIndexChange, this._afterZIndexChange);
182 * Creates/Initializes the DOM to support stackability.
184 * This method in invoked after renderUI is invoked for the Widget class
185 * using YUI's aop infrastructure.
187 * @method _renderUIStack
190 _renderUIStack: function() {
191 this._stackNode.addClass(Stack.STACKED_CLASS_NAME);
195 Parses a `zIndex` attribute value from this widget's `srcNode`.
198 @param {Node} srcNode The node to parse a `zIndex` value from.
199 @return {Mixed} The parsed `zIndex` value.
202 _parseZIndex: function (srcNode) {
205 // Prefers how WebKit handles `z-index` which better matches the
208 // * http://www.w3.org/TR/CSS2/visuren.html#z-index
209 // * https://bugs.webkit.org/show_bug.cgi?id=15562
211 // When a node isn't rendered in the document, and/or when a
212 // node is not positioned, then it doesn't have a context to derive
213 // a valid `z-index` value from.
214 if (!srcNode.inDoc() || srcNode.getStyle('position') === 'static') {
217 // Uses `getComputedStyle()` because it has greater accuracy in
218 // more browsers than `getStyle()` does for `z-index`.
219 zIndex = srcNode.getComputedStyle('zIndex');
222 // This extension adds a stacking context to widgets, therefore a
223 // `srcNode` witout a stacking context (i.e. "auto") will return
224 // `null` from this DOM parser. This way the widget's default or
225 // user provided value for `zIndex` will be used.
226 return zIndex === 'auto' ? null : zIndex;
230 * Default setter for zIndex attribute changes. Normalizes zIndex values to
231 * numbers, converting non-numerical values to 0.
235 * @param {String | Number} zIndex
236 * @return {Number} Normalized zIndex
238 _setZIndex: function(zIndex) {
239 if (L.isString(zIndex)) {
240 zIndex = parseInt(zIndex, 10);
242 if (!L.isNumber(zIndex)) {
249 * Default attribute change listener for the shim attribute, responsible
250 * for updating the UI, in response to attribute changes.
252 * @method _afterShimChange
254 * @param {EventFacade} e The event facade for the attribute change
256 _afterShimChange : function(e) {
257 this._uiSetShim(e.newVal);
261 * Default attribute change listener for the zIndex attribute, responsible
262 * for updating the UI, in response to attribute changes.
264 * @method _afterZIndexChange
266 * @param {EventFacade} e The event facade for the attribute change
268 _afterZIndexChange : function(e) {
269 this._uiSetZIndex(e.newVal);
273 * Updates the UI to reflect the zIndex value passed in.
275 * @method _uiSetZIndex
277 * @param {number} zIndex The zindex to be reflected in the UI
279 _uiSetZIndex: function (zIndex) {
280 this._stackNode.setStyle(ZINDEX, zIndex);
284 * Updates the UI to enable/disable the shim. If the widget is not currently visible,
285 * creation of the shim is deferred until it is made visible, for performance reasons.
289 * @param {boolean} enable If true, creates/renders the shim, if false, removes it.
291 _uiSetShim: function (enable) {
294 if (this.get(VISIBLE)) {
297 this._renderShimDeferred();
300 // Eagerly attach resize handlers
302 // Required because of Event stack behavior, commit ref: cd8dddc
303 // Should be revisted after Ticket #2531067 is resolved.
305 this._addShimResizeHandlers();
313 * Sets up change handlers for the visible attribute, to defer shim creation/rendering
314 * until the Widget is made visible.
316 * @method _renderShimDeferred
319 _renderShimDeferred : function() {
321 this._stackHandles[SHIM_DEFERRED] = this._stackHandles[SHIM_DEFERRED] || [];
323 var handles = this._stackHandles[SHIM_DEFERRED],
324 createBeforeVisible = function(e) {
330 handles.push(this.on(VisibleChange, createBeforeVisible));
331 // Depending how how Ticket #2531067 is resolved, a reversal of
332 // commit ref: cd8dddc could lead to a more elagent solution, with
333 // the addition of this line here:
335 // handles.push(this.after(VisibleChange, this.sizeShim));
339 * Sets up event listeners to resize the shim when the size of the Widget changes.
341 * NOTE: This method is only used for IE6 currently, since IE6 doesn't support a way to
342 * resize the shim purely through CSS, when the Widget does not have an explicit width/height
345 * @method _addShimResizeHandlers
348 _addShimResizeHandlers : function() {
350 this._stackHandles[SHIM_RESIZE] = this._stackHandles[SHIM_RESIZE] || [];
352 var sizeShim = this.sizeShim,
353 handles = this._stackHandles[SHIM_RESIZE];
355 handles.push(this.after(VisibleChange, sizeShim));
356 handles.push(this.after(WidthChange, sizeShim));
357 handles.push(this.after(HeightChange, sizeShim));
358 handles.push(this.after(ContentUpdate, sizeShim));
362 * Detaches any handles stored for the provided key
364 * @method _detachStackHandles
365 * @param String handleKey The key defining the group of handles which should be detached
368 _detachStackHandles : function(handleKey) {
369 var handles = this._stackHandles[handleKey],
372 if (handles && handles.length > 0) {
373 while((handle = handles.pop())) {
380 * Creates the shim element and adds it to the DOM
382 * @method _renderShim
385 _renderShim : function() {
386 var shimEl = this._shimNode,
387 stackEl = this._stackNode;
390 shimEl = this._shimNode = this._getShimTemplate();
391 stackEl.insertBefore(shimEl, stackEl.get(FIRST_CHILD));
393 this._detachStackHandles(SHIM_DEFERRED);
399 * Removes the shim from the DOM, and detaches any related event
402 * @method _destroyShim
405 _destroyShim : function() {
406 if (this._shimNode) {
407 this._shimNode.get(PARENT_NODE).removeChild(this._shimNode);
408 this._shimNode = null;
410 this._detachStackHandles(SHIM_DEFERRED);
411 this._detachStackHandles(SHIM_RESIZE);
416 * For IE6, synchronizes the size and position of iframe shim to that of
417 * Widget bounding box which it is protecting. For all other browsers,
418 * this method does not do anything.
422 sizeShim: function () {
423 var shim = this._shimNode,
424 node = this._stackNode;
426 if (shim && UA.ie === 6 && this.get(VISIBLE)) {
427 shim.setStyle(WIDTH, node.get(OFFSET_WIDTH) + PX);
428 shim.setStyle(HEIGHT, node.get(OFFSET_HEIGHT) + PX);
433 * Creates a cloned shim node, using the SHIM_TEMPLATE html template, for use on a new instance.
435 * @method _getShimTemplate
437 * @return {Node} node A new shim Node instance.
439 _getShimTemplate : function() {
440 return Node.create(Stack.SHIM_TEMPLATE, this._stackNode.get(OWNER_DOCUMENT));
444 Y.WidgetStack = Stack;
447 }, '3.13.0', {"requires": ["base-build", "widget"], "skinnable": true});