~andreserl/maas/packaging_precise_rebase

« back to all changes in this revision

Viewing changes to debian/extras/jslibs/yui/widget-stack/widget-stack.js

  • Committer: Andres Rodriguez
  • Date: 2013-03-20 18:12:30 UTC
  • mfrom: (145.2.22 precise.sru)
  • Revision ID: andreserl@ubuntu.com-20130320181230-6l5guc0nhlv2z4p7
Re-base againts latest quantal released branch towards SRU

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
YUI 3.5.1 (build 22)
 
3
Copyright 2012 Yahoo! Inc. All rights reserved.
 
4
Licensed under the BSD License.
 
5
http://yuilibrary.com/license/
 
6
*/
 
7
YUI.add('widget-stack', function(Y) {
 
8
 
 
9
/**
 
10
 * Provides stackable (z-index) support for Widgets through an extension.
 
11
 *
 
12
 * @module widget-stack
 
13
 */
 
14
    var L = Y.Lang,
 
15
        UA = Y.UA,
 
16
        Node = Y.Node,
 
17
        Widget = Y.Widget,
 
18
 
 
19
        ZINDEX = "zIndex",
 
20
        SHIM = "shim",
 
21
        VISIBLE = "visible",
 
22
 
 
23
        BOUNDING_BOX = "boundingBox",
 
24
 
 
25
        RENDER_UI = "renderUI",
 
26
        BIND_UI = "bindUI",
 
27
        SYNC_UI = "syncUI",
 
28
 
 
29
        OFFSET_WIDTH = "offsetWidth",
 
30
        OFFSET_HEIGHT = "offsetHeight",
 
31
        PARENT_NODE = "parentNode",
 
32
        FIRST_CHILD = "firstChild",
 
33
        OWNER_DOCUMENT = "ownerDocument",
 
34
 
 
35
        WIDTH = "width",
 
36
        HEIGHT = "height",
 
37
        PX = "px",
 
38
 
 
39
        // HANDLE KEYS
 
40
        SHIM_DEFERRED = "shimdeferred",
 
41
        SHIM_RESIZE = "shimresize",
 
42
 
 
43
        // Events
 
44
        VisibleChange = "visibleChange",
 
45
        WidthChange = "widthChange",
 
46
        HeightChange = "heightChange",
 
47
        ShimChange = "shimChange",
 
48
        ZIndexChange = "zIndexChange",
 
49
        ContentUpdate = "contentUpdate",
 
50
 
 
51
        // CSS
 
52
        STACKED = "stacked";
 
53
 
 
54
    /**
 
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.
 
58
     *
 
59
     * @class WidgetStack
 
60
     * @param {Object} User configuration object
 
61
     */
 
62
    function Stack(config) {
 
63
        this._stackNode = this.get(BOUNDING_BOX);
 
64
        this._stackHandles = {};
 
65
 
 
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);
 
70
    }
 
71
 
 
72
    // Static Properties
 
73
    /**
 
74
     * Static property used to define the default attribute
 
75
     * configuration introduced by WidgetStack.
 
76
     *
 
77
     * @property ATTRS
 
78
     * @type Object
 
79
     * @static
 
80
     */
 
81
    Stack.ATTRS = {
 
82
        /**
 
83
         * @attribute shim
 
84
         * @type boolean
 
85
         * @default false, for all browsers other than IE6, for which a shim is enabled by default.
 
86
         *
 
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.
 
89
         */
 
90
        shim: {
 
91
            value: (UA.ie == 6)
 
92
        },
 
93
 
 
94
        /**
 
95
         * @attribute zIndex
 
96
         * @type number
 
97
         * @default 0
 
98
         * @description The z-index to apply to the Widgets boundingBox. Non-numerical values for
 
99
         * zIndex will be converted to 0
 
100
         */
 
101
        zIndex: {
 
102
            value : 0,
 
103
            setter: '_setZIndex'
 
104
        }
 
105
    };
 
106
 
 
107
    /**
 
108
     * The HTML parsing rules for the WidgetStack class.
 
109
     *
 
110
     * @property HTML_PARSER
 
111
     * @static
 
112
     * @type Object
 
113
     */
 
114
    Stack.HTML_PARSER = {
 
115
        zIndex: function (srcNode) {
 
116
            return this._parseZIndex(srcNode);
 
117
        }
 
118
    };
 
119
 
 
120
    /**
 
121
     * Default class used to mark the shim element
 
122
     *
 
123
     * @property SHIM_CLASS_NAME
 
124
     * @type String
 
125
     * @static
 
126
     * @default "yui3-widget-shim"
 
127
     */
 
128
    Stack.SHIM_CLASS_NAME = Widget.getClassName(SHIM);
 
129
 
 
130
    /**
 
131
     * Default class used to mark the boundingBox of a stacked widget.
 
132
     *
 
133
     * @property STACKED_CLASS_NAME
 
134
     * @type String
 
135
     * @static
 
136
     * @default "yui3-widget-stacked"
 
137
     */
 
138
    Stack.STACKED_CLASS_NAME = Widget.getClassName(STACKED);
 
139
 
 
140
    /**
 
141
     * Default markup template used to generate the shim element.
 
142
     *
 
143
     * @property SHIM_TEMPLATE
 
144
     * @type String
 
145
     * @static
 
146
     */
 
147
    Stack.SHIM_TEMPLATE = '<iframe class="' + Stack.SHIM_CLASS_NAME + '" frameborder="0" title="Widget Stacking Shim" src="javascript:false" tabindex="-1" role="presentation"></iframe>';
 
148
 
 
149
    Stack.prototype = {
 
150
 
 
151
        /**
 
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.
 
154
         *
 
155
         * @method _syncUIStack
 
156
         * @protected
 
157
         */
 
158
        _syncUIStack: function() {
 
159
            this._uiSetShim(this.get(SHIM));
 
160
            this._uiSetZIndex(this.get(ZINDEX));
 
161
        },
 
162
 
 
163
        /**
 
164
         * Binds event listeners responsible for updating the UI state in response to
 
165
         * Widget stack related state changes.
 
166
         * <p>
 
167
         * This method is invoked after bindUI is invoked for the Widget class
 
168
         * using YUI's aop infrastructure.
 
169
         * </p>
 
170
         * @method _bindUIStack
 
171
         * @protected
 
172
         */
 
173
        _bindUIStack: function() {
 
174
            this.after(ShimChange, this._afterShimChange);
 
175
            this.after(ZIndexChange, this._afterZIndexChange);
 
176
        },
 
177
 
 
178
        /**
 
179
         * Creates/Initializes the DOM to support stackability.
 
180
         * <p>
 
181
         * This method in invoked after renderUI is invoked for the Widget class
 
182
         * using YUI's aop infrastructure.
 
183
         * </p>
 
184
         * @method _renderUIStack
 
185
         * @protected
 
186
         */
 
187
        _renderUIStack: function() {
 
188
            this._stackNode.addClass(Stack.STACKED_CLASS_NAME);
 
189
        },
 
190
 
 
191
        /**
 
192
        Parses a `zIndex` attribute value from this widget's `srcNode`.
 
193
 
 
194
        @method _parseZIndex
 
195
        @param {Node} srcNode The node to parse a `zIndex` value from.
 
196
        @return {Mixed} The parsed `zIndex` value.
 
197
        @protected
 
198
        **/
 
199
        _parseZIndex: function (srcNode) {
 
200
            var zIndex;
 
201
 
 
202
            // Prefers how WebKit handles `z-index` which better matches the
 
203
            // spec:
 
204
            //
 
205
            // * http://www.w3.org/TR/CSS2/visuren.html#z-index
 
206
            // * https://bugs.webkit.org/show_bug.cgi?id=15562
 
207
            //
 
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') {
 
212
                zIndex = 'auto';
 
213
            } else {
 
214
                // Uses `getComputedStyle()` because it has greater accuracy in
 
215
                // more browsers than `getStyle()` does for `z-index`.
 
216
                zIndex = srcNode.getComputedStyle('zIndex');
 
217
            }
 
218
 
 
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;
 
224
        },
 
225
 
 
226
        /**
 
227
         * Default setter for zIndex attribute changes. Normalizes zIndex values to
 
228
         * numbers, converting non-numerical values to 0.
 
229
         *
 
230
         * @method _setZIndex
 
231
         * @protected
 
232
         * @param {String | Number} zIndex
 
233
         * @return {Number} Normalized zIndex
 
234
         */
 
235
        _setZIndex: function(zIndex) {
 
236
            if (L.isString(zIndex)) {
 
237
                zIndex = parseInt(zIndex, 10);
 
238
            }
 
239
            if (!L.isNumber(zIndex)) {
 
240
                zIndex = 0;
 
241
            }
 
242
            return zIndex;
 
243
        },
 
244
 
 
245
        /**
 
246
         * Default attribute change listener for the shim attribute, responsible
 
247
         * for updating the UI, in response to attribute changes.
 
248
         *
 
249
         * @method _afterShimChange
 
250
         * @protected
 
251
         * @param {EventFacade} e The event facade for the attribute change
 
252
         */
 
253
        _afterShimChange : function(e) {
 
254
            this._uiSetShim(e.newVal);
 
255
        },
 
256
 
 
257
        /**
 
258
         * Default attribute change listener for the zIndex attribute, responsible
 
259
         * for updating the UI, in response to attribute changes.
 
260
         *
 
261
         * @method _afterZIndexChange
 
262
         * @protected
 
263
         * @param {EventFacade} e The event facade for the attribute change
 
264
         */
 
265
        _afterZIndexChange : function(e) {
 
266
            this._uiSetZIndex(e.newVal);
 
267
        },
 
268
 
 
269
        /**
 
270
         * Updates the UI to reflect the zIndex value passed in.
 
271
         *
 
272
         * @method _uiSetZIndex
 
273
         * @protected
 
274
         * @param {number} zIndex The zindex to be reflected in the UI
 
275
         */
 
276
        _uiSetZIndex: function (zIndex) {
 
277
            this._stackNode.setStyle(ZINDEX, zIndex);
 
278
        },
 
279
 
 
280
        /**
 
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.
 
283
         *
 
284
         * @method _uiSetShim
 
285
         * @protected
 
286
         * @param {boolean} enable If true, creates/renders the shim, if false, removes it.
 
287
         */
 
288
        _uiSetShim: function (enable) {
 
289
            if (enable) {
 
290
                // Lazy creation
 
291
                if (this.get(VISIBLE)) {
 
292
                    this._renderShim();
 
293
                } else {
 
294
                    this._renderShimDeferred();
 
295
                }
 
296
 
 
297
                // Eagerly attach resize handlers
 
298
                //
 
299
                // Required because of Event stack behavior, commit ref: cd8dddc
 
300
                // Should be revisted after Ticket #2531067 is resolved.
 
301
                if (UA.ie == 6) {
 
302
                    this._addShimResizeHandlers();
 
303
                }
 
304
            } else {
 
305
                this._destroyShim();
 
306
            }
 
307
        },
 
308
 
 
309
        /**
 
310
         * Sets up change handlers for the visible attribute, to defer shim creation/rendering
 
311
         * until the Widget is made visible.
 
312
         *
 
313
         * @method _renderShimDeferred
 
314
         * @private
 
315
         */
 
316
        _renderShimDeferred : function() {
 
317
 
 
318
            this._stackHandles[SHIM_DEFERRED] = this._stackHandles[SHIM_DEFERRED] || [];
 
319
 
 
320
            var handles = this._stackHandles[SHIM_DEFERRED],
 
321
                createBeforeVisible = function(e) {
 
322
                    if (e.newVal) {
 
323
                        this._renderShim();
 
324
                    }
 
325
                };
 
326
 
 
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:
 
331
            //
 
332
            // handles.push(this.after(VisibleChange, this.sizeShim));
 
333
        },
 
334
 
 
335
        /**
 
336
         * Sets up event listeners to resize the shim when the size of the Widget changes.
 
337
         * <p>
 
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
 
340
         * set.
 
341
         * </p>
 
342
         * @method _addShimResizeHandlers
 
343
         * @private
 
344
         */
 
345
        _addShimResizeHandlers : function() {
 
346
 
 
347
            this._stackHandles[SHIM_RESIZE] = this._stackHandles[SHIM_RESIZE] || [];
 
348
 
 
349
            var sizeShim = this.sizeShim,
 
350
                handles = this._stackHandles[SHIM_RESIZE];
 
351
 
 
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));
 
356
        },
 
357
 
 
358
        /**
 
359
         * Detaches any handles stored for the provided key
 
360
         *
 
361
         * @method _detachStackHandles
 
362
         * @param String handleKey The key defining the group of handles which should be detached
 
363
         * @private
 
364
         */
 
365
        _detachStackHandles : function(handleKey) {
 
366
            var handles = this._stackHandles[handleKey],
 
367
                handle;
 
368
 
 
369
            if (handles && handles.length > 0) {
 
370
                while((handle = handles.pop())) {
 
371
                    handle.detach();
 
372
                }
 
373
            }
 
374
        },
 
375
 
 
376
        /**
 
377
         * Creates the shim element and adds it to the DOM
 
378
         *
 
379
         * @method _renderShim
 
380
         * @private
 
381
         */
 
382
        _renderShim : function() {
 
383
            var shimEl = this._shimNode,
 
384
                stackEl = this._stackNode;
 
385
 
 
386
            if (!shimEl) {
 
387
                shimEl = this._shimNode = this._getShimTemplate();
 
388
                stackEl.insertBefore(shimEl, stackEl.get(FIRST_CHILD));
 
389
 
 
390
                this._detachStackHandles(SHIM_DEFERRED);
 
391
                this.sizeShim();
 
392
            }
 
393
        },
 
394
 
 
395
        /**
 
396
         * Removes the shim from the DOM, and detaches any related event
 
397
         * listeners.
 
398
         *
 
399
         * @method _destroyShim
 
400
         * @private
 
401
         */
 
402
        _destroyShim : function() {
 
403
            if (this._shimNode) {
 
404
                this._shimNode.get(PARENT_NODE).removeChild(this._shimNode);
 
405
                this._shimNode = null;
 
406
 
 
407
                this._detachStackHandles(SHIM_DEFERRED);
 
408
                this._detachStackHandles(SHIM_RESIZE);
 
409
            }
 
410
        },
 
411
 
 
412
        /**
 
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.
 
416
         *
 
417
         * @method sizeShim
 
418
         */
 
419
        sizeShim: function () {
 
420
            var shim = this._shimNode,
 
421
                node = this._stackNode;
 
422
 
 
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);
 
426
            }
 
427
        },
 
428
 
 
429
        /**
 
430
         * Creates a cloned shim node, using the SHIM_TEMPLATE html template, for use on a new instance.
 
431
         *
 
432
         * @method _getShimTemplate
 
433
         * @private
 
434
         * @return {Node} node A new shim Node instance.
 
435
         */
 
436
        _getShimTemplate : function() {
 
437
            return Node.create(Stack.SHIM_TEMPLATE, this._stackNode.get(OWNER_DOCUMENT));
 
438
        }
 
439
    };
 
440
 
 
441
    Y.WidgetStack = Stack;
 
442
 
 
443
 
 
444
}, '3.5.1' ,{requires:['base-build', 'widget']});