~bac/juju-gui/trunkcopy

« back to all changes in this revision

Viewing changes to lib/yui/build/event-base/event-base.js

  • Committer: kapil.foss at gmail
  • Date: 2012-07-13 18:45:59 UTC
  • Revision ID: kapil.foss@gmail.com-20120713184559-2xl7be17egsrz0c9
reshape

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
 
(function () {
8
 
var GLOBAL_ENV = YUI.Env;
9
 
 
10
 
if (!GLOBAL_ENV._ready) {
11
 
    GLOBAL_ENV._ready = function() {
12
 
        GLOBAL_ENV.DOMReady = true;
13
 
        GLOBAL_ENV.remove(YUI.config.doc, 'DOMContentLoaded', GLOBAL_ENV._ready);
14
 
    };
15
 
 
16
 
    GLOBAL_ENV.add(YUI.config.doc, 'DOMContentLoaded', GLOBAL_ENV._ready);
17
 
}
18
 
})();
19
 
YUI.add('event-base', function(Y) {
20
 
 
21
 
/*
22
 
 * DOM event listener abstraction layer
23
 
 * @module event
24
 
 * @submodule event-base
25
 
 */
26
 
 
27
 
/**
28
 
 * The domready event fires at the moment the browser's DOM is
29
 
 * usable. In most cases, this is before images are fully
30
 
 * downloaded, allowing you to provide a more responsive user
31
 
 * interface.
32
 
 *
33
 
 * In YUI 3, domready subscribers will be notified immediately if
34
 
 * that moment has already passed when the subscription is created.
35
 
 *
36
 
 * One exception is if the yui.js file is dynamically injected into
37
 
 * the page.  If this is done, you must tell the YUI instance that
38
 
 * you did this in order for DOMReady (and window load events) to
39
 
 * fire normally.  That configuration option is 'injected' -- set
40
 
 * it to true if the yui.js script is not included inline.
41
 
 *
42
 
 * This method is part of the 'event-ready' module, which is a
43
 
 * submodule of 'event'.
44
 
 *
45
 
 * @event domready
46
 
 * @for YUI
47
 
 */
48
 
Y.publish('domready', {
49
 
    fireOnce: true,
50
 
    async: true
51
 
});
52
 
 
53
 
if (YUI.Env.DOMReady) {
54
 
    Y.fire('domready');
55
 
} else {
56
 
    Y.Do.before(function() { Y.fire('domready'); }, YUI.Env, '_ready');
57
 
}
58
 
 
59
 
/**
60
 
 * Custom event engine, DOM event listener abstraction layer, synthetic DOM
61
 
 * events.
62
 
 * @module event
63
 
 * @submodule event-base
64
 
 */
65
 
 
66
 
/**
67
 
 * Wraps a DOM event, properties requiring browser abstraction are
68
 
 * fixed here.  Provids a security layer when required.
69
 
 * @class DOMEventFacade
70
 
 * @param ev {Event} the DOM event
71
 
 * @param currentTarget {HTMLElement} the element the listener was attached to
72
 
 * @param wrapper {Event.Custom} the custom event wrapper for this DOM event
73
 
 */
74
 
 
75
 
    var ua = Y.UA,
76
 
 
77
 
    EMPTY = {},
78
 
 
79
 
    /**
80
 
     * webkit key remapping required for Safari < 3.1
81
 
     * @property webkitKeymap
82
 
     * @private
83
 
     */
84
 
    webkitKeymap = {
85
 
        63232: 38, // up
86
 
        63233: 40, // down
87
 
        63234: 37, // left
88
 
        63235: 39, // right
89
 
        63276: 33, // page up
90
 
        63277: 34, // page down
91
 
        25:     9, // SHIFT-TAB (Safari provides a different key code in
92
 
                   // this case, even though the shiftKey modifier is set)
93
 
        63272: 46, // delete
94
 
        63273: 36, // home
95
 
        63275: 35  // end
96
 
    },
97
 
 
98
 
    /**
99
 
     * Returns a wrapped node.  Intended to be used on event targets,
100
 
     * so it will return the node's parent if the target is a text
101
 
     * node.
102
 
     *
103
 
     * If accessing a property of the node throws an error, this is
104
 
     * probably the anonymous div wrapper Gecko adds inside text
105
 
     * nodes.  This likely will only occur when attempting to access
106
 
     * the relatedTarget.  In this case, we now return null because
107
 
     * the anonymous div is completely useless and we do not know
108
 
     * what the related target was because we can't even get to
109
 
     * the element's parent node.
110
 
     *
111
 
     * @method resolve
112
 
     * @private
113
 
     */
114
 
    resolve = function(n) {
115
 
        if (!n) {
116
 
            return n;
117
 
        }
118
 
        try {
119
 
            if (n && 3 == n.nodeType) {
120
 
                n = n.parentNode;
121
 
            }
122
 
        } catch(e) {
123
 
            return null;
124
 
        }
125
 
 
126
 
        return Y.one(n);
127
 
    },
128
 
 
129
 
    DOMEventFacade = function(ev, currentTarget, wrapper) {
130
 
        this._event = ev;
131
 
        this._currentTarget = currentTarget;
132
 
        this._wrapper = wrapper || EMPTY;
133
 
 
134
 
        // if not lazy init
135
 
        this.init();
136
 
    };
137
 
 
138
 
Y.extend(DOMEventFacade, Object, {
139
 
 
140
 
    init: function() {
141
 
 
142
 
        var e = this._event,
143
 
            overrides = this._wrapper.overrides,
144
 
            x = e.pageX,
145
 
            y = e.pageY,
146
 
            c,
147
 
            currentTarget = this._currentTarget;
148
 
 
149
 
        this.altKey   = e.altKey;
150
 
        this.ctrlKey  = e.ctrlKey;
151
 
        this.metaKey  = e.metaKey;
152
 
        this.shiftKey = e.shiftKey;
153
 
        this.type     = (overrides && overrides.type) || e.type;
154
 
        this.clientX  = e.clientX;
155
 
        this.clientY  = e.clientY;
156
 
 
157
 
        this.pageX = x;
158
 
        this.pageY = y;
159
 
 
160
 
        // charCode is unknown in keyup, keydown. keyCode is unknown in keypress.
161
 
        // FF 3.6 - 8+? pass 0 for keyCode in keypress events.
162
 
        // Webkit, FF 3.6-8+?, and IE9+? pass 0 for charCode in keydown, keyup.
163
 
        // Webkit and IE9+? duplicate charCode in keyCode.
164
 
        // Opera never sets charCode, always keyCode (though with the charCode).
165
 
        // IE6-8 don't set charCode or which.
166
 
        // All browsers other than IE6-8 set which=keyCode in keydown, keyup, and 
167
 
        // which=charCode in keypress.
168
 
        //
169
 
        // Moral of the story: (e.which || e.keyCode) will always return the
170
 
        // known code for that key event phase. e.keyCode is often different in
171
 
        // keypress from keydown and keyup.
172
 
        c = e.keyCode || e.charCode;
173
 
 
174
 
        if (ua.webkit && (c in webkitKeymap)) {
175
 
            c = webkitKeymap[c];
176
 
        }
177
 
 
178
 
        this.keyCode = c;
179
 
        this.charCode = c;
180
 
        // Fill in e.which for IE - implementers should always use this over
181
 
        // e.keyCode or e.charCode.
182
 
        this.which = e.which || e.charCode || c;
183
 
        // this.button = e.button;
184
 
        this.button = this.which;
185
 
 
186
 
        this.target = resolve(e.target);
187
 
        this.currentTarget = resolve(currentTarget);
188
 
        this.relatedTarget = resolve(e.relatedTarget);
189
 
 
190
 
        if (e.type == "mousewheel" || e.type == "DOMMouseScroll") {
191
 
            this.wheelDelta = (e.detail) ? (e.detail * -1) : Math.round(e.wheelDelta / 80) || ((e.wheelDelta < 0) ? -1 : 1);
192
 
        }
193
 
 
194
 
        if (this._touch) {
195
 
            this._touch(e, currentTarget, this._wrapper);
196
 
        }
197
 
    },
198
 
 
199
 
    stopPropagation: function() {
200
 
        this._event.stopPropagation();
201
 
        this._wrapper.stopped = 1;
202
 
        this.stopped = 1;
203
 
    },
204
 
 
205
 
    stopImmediatePropagation: function() {
206
 
        var e = this._event;
207
 
        if (e.stopImmediatePropagation) {
208
 
            e.stopImmediatePropagation();
209
 
        } else {
210
 
            this.stopPropagation();
211
 
        }
212
 
        this._wrapper.stopped = 2;
213
 
        this.stopped = 2;
214
 
    },
215
 
 
216
 
    preventDefault: function(returnValue) {
217
 
        var e = this._event;
218
 
        e.preventDefault();
219
 
        e.returnValue = returnValue || false;
220
 
        this._wrapper.prevented = 1;
221
 
        this.prevented = 1;
222
 
    },
223
 
 
224
 
    halt: function(immediate) {
225
 
        if (immediate) {
226
 
            this.stopImmediatePropagation();
227
 
        } else {
228
 
            this.stopPropagation();
229
 
        }
230
 
 
231
 
        this.preventDefault();
232
 
    }
233
 
 
234
 
});
235
 
 
236
 
DOMEventFacade.resolve = resolve;
237
 
Y.DOM2EventFacade = DOMEventFacade;
238
 
Y.DOMEventFacade = DOMEventFacade;
239
 
 
240
 
    /**
241
 
     * The native event
242
 
     * @property _event
243
 
     * @type {Native DOM Event}
244
 
     * @private
245
 
     */
246
 
 
247
 
    /**
248
 
    The name of the event (e.g. "click")
249
 
 
250
 
    @property type
251
 
    @type {String}
252
 
    **/
253
 
 
254
 
    /**
255
 
    `true` if the "alt" or "option" key is pressed.
256
 
 
257
 
    @property altKey
258
 
    @type {Boolean}
259
 
    **/
260
 
 
261
 
    /**
262
 
    `true` if the shift key is pressed.
263
 
 
264
 
    @property shiftKey
265
 
    @type {Boolean}
266
 
    **/
267
 
 
268
 
    /**
269
 
    `true` if the "Windows" key on a Windows keyboard, "command" key on an
270
 
    Apple keyboard, or "meta" key on other keyboards is pressed.
271
 
 
272
 
    @property metaKey
273
 
    @type {Boolean}
274
 
    **/
275
 
 
276
 
    /**
277
 
    `true` if the "Ctrl" or "control" key is pressed.
278
 
 
279
 
    @property ctrlKey
280
 
    @type {Boolean}
281
 
    **/
282
 
 
283
 
    /**
284
 
     * The X location of the event on the page (including scroll)
285
 
     * @property pageX
286
 
     * @type {Number}
287
 
     */
288
 
 
289
 
    /**
290
 
     * The Y location of the event on the page (including scroll)
291
 
     * @property pageY
292
 
     * @type {Number}
293
 
     */
294
 
 
295
 
    /**
296
 
     * The X location of the event in the viewport
297
 
     * @property clientX
298
 
     * @type {Number}
299
 
     */
300
 
 
301
 
    /**
302
 
     * The Y location of the event in the viewport
303
 
     * @property clientY
304
 
     * @type {Number}
305
 
     */
306
 
 
307
 
    /**
308
 
     * The keyCode for key events.  Uses charCode if keyCode is not available
309
 
     * @property keyCode
310
 
     * @type {Number}
311
 
     */
312
 
 
313
 
    /**
314
 
     * The charCode for key events.  Same as keyCode
315
 
     * @property charCode
316
 
     * @type {Number}
317
 
     */
318
 
 
319
 
    /**
320
 
     * The button that was pushed. 1 for left click, 2 for middle click, 3 for
321
 
     * right click.  This is only reliably populated on `mouseup` events.
322
 
     * @property button
323
 
     * @type {Number}
324
 
     */
325
 
 
326
 
    /**
327
 
     * The button that was pushed.  Same as button.
328
 
     * @property which
329
 
     * @type {Number}
330
 
     */
331
 
 
332
 
    /**
333
 
     * Node reference for the targeted element
334
 
     * @property target
335
 
     * @type {Node}
336
 
     */
337
 
 
338
 
    /**
339
 
     * Node reference for the element that the listener was attached to.
340
 
     * @property currentTarget
341
 
     * @type {Node}
342
 
     */
343
 
 
344
 
    /**
345
 
     * Node reference to the relatedTarget
346
 
     * @property relatedTarget
347
 
     * @type {Node}
348
 
     */
349
 
 
350
 
    /**
351
 
     * Number representing the direction and velocity of the movement of the mousewheel.
352
 
     * Negative is down, the higher the number, the faster.  Applies to the mousewheel event.
353
 
     * @property wheelDelta
354
 
     * @type {Number}
355
 
     */
356
 
 
357
 
    /**
358
 
     * Stops the propagation to the next bubble target
359
 
     * @method stopPropagation
360
 
     */
361
 
 
362
 
    /**
363
 
     * Stops the propagation to the next bubble target and
364
 
     * prevents any additional listeners from being exectued
365
 
     * on the current target.
366
 
     * @method stopImmediatePropagation
367
 
     */
368
 
 
369
 
    /**
370
 
     * Prevents the event's default behavior
371
 
     * @method preventDefault
372
 
     * @param returnValue {string} sets the returnValue of the event to this value
373
 
     * (rather than the default false value).  This can be used to add a customized
374
 
     * confirmation query to the beforeunload event).
375
 
     */
376
 
 
377
 
    /**
378
 
     * Stops the event propagation and prevents the default
379
 
     * event behavior.
380
 
     * @method halt
381
 
     * @param immediate {boolean} if true additional listeners
382
 
     * on the current target will not be executed
383
 
     */
384
 
(function() {
385
 
/**
386
 
 * The event utility provides functions to add and remove event listeners,
387
 
 * event cleansing.  It also tries to automatically remove listeners it
388
 
 * registers during the unload event.
389
 
 * @module event
390
 
 * @main event
391
 
 * @submodule event-base
392
 
 */
393
 
 
394
 
/**
395
 
 * The event utility provides functions to add and remove event listeners,
396
 
 * event cleansing.  It also tries to automatically remove listeners it
397
 
 * registers during the unload event.
398
 
 *
399
 
 * @class Event
400
 
 * @static
401
 
 */
402
 
 
403
 
Y.Env.evt.dom_wrappers = {};
404
 
Y.Env.evt.dom_map = {};
405
 
 
406
 
var _eventenv = Y.Env.evt,
407
 
    config = Y.config,
408
 
    win = config.win,
409
 
    add = YUI.Env.add,
410
 
    remove = YUI.Env.remove,
411
 
 
412
 
    onLoad = function() {
413
 
        YUI.Env.windowLoaded = true;
414
 
        Y.Event._load();
415
 
        remove(win, "load", onLoad);
416
 
    },
417
 
 
418
 
    onUnload = function() {
419
 
        Y.Event._unload();
420
 
    },
421
 
 
422
 
    EVENT_READY = 'domready',
423
 
 
424
 
    COMPAT_ARG = '~yui|2|compat~',
425
 
 
426
 
    shouldIterate = function(o) {
427
 
        try {
428
 
            return (o && typeof o !== "string" && Y.Lang.isNumber(o.length) &&
429
 
                    !o.tagName && !o.alert);
430
 
        } catch(ex) {
431
 
            return false;
432
 
        }
433
 
 
434
 
    },
435
 
 
436
 
    // aliases to support DOM event subscription clean up when the last
437
 
    // subscriber is detached. deleteAndClean overrides the DOM event's wrapper
438
 
    // CustomEvent _delete method.
439
 
    _ceProtoDelete = Y.CustomEvent.prototype._delete,
440
 
    _deleteAndClean = function(s) {
441
 
        var ret = _ceProtoDelete.apply(this, arguments);
442
 
 
443
 
        if (!this.subCount && !this.afterCount) {
444
 
            Y.Event._clean(this);
445
 
        }
446
 
 
447
 
        return ret;
448
 
    },
449
 
 
450
 
Event = function() {
451
 
 
452
 
    /**
453
 
     * True after the onload event has fired
454
 
     * @property _loadComplete
455
 
     * @type boolean
456
 
     * @static
457
 
     * @private
458
 
     */
459
 
    var _loadComplete =  false,
460
 
 
461
 
    /**
462
 
     * The number of times to poll after window.onload.  This number is
463
 
     * increased if additional late-bound handlers are requested after
464
 
     * the page load.
465
 
     * @property _retryCount
466
 
     * @static
467
 
     * @private
468
 
     */
469
 
    _retryCount = 0,
470
 
 
471
 
    /**
472
 
     * onAvailable listeners
473
 
     * @property _avail
474
 
     * @static
475
 
     * @private
476
 
     */
477
 
    _avail = [],
478
 
 
479
 
    /**
480
 
     * Custom event wrappers for DOM events.  Key is
481
 
     * 'event:' + Element uid stamp + event type
482
 
     * @property _wrappers
483
 
     * @type Y.Event.Custom
484
 
     * @static
485
 
     * @private
486
 
     */
487
 
    _wrappers = _eventenv.dom_wrappers,
488
 
 
489
 
    _windowLoadKey = null,
490
 
 
491
 
    /**
492
 
     * Custom event wrapper map DOM events.  Key is
493
 
     * Element uid stamp.  Each item is a hash of custom event
494
 
     * wrappers as provided in the _wrappers collection.  This
495
 
     * provides the infrastructure for getListeners.
496
 
     * @property _el_events
497
 
     * @static
498
 
     * @private
499
 
     */
500
 
    _el_events = _eventenv.dom_map;
501
 
 
502
 
    return {
503
 
 
504
 
        /**
505
 
         * The number of times we should look for elements that are not
506
 
         * in the DOM at the time the event is requested after the document
507
 
         * has been loaded.  The default is 1000@amp;40 ms, so it will poll
508
 
         * for 40 seconds or until all outstanding handlers are bound
509
 
         * (whichever comes first).
510
 
         * @property POLL_RETRYS
511
 
         * @type int
512
 
         * @static
513
 
         * @final
514
 
         */
515
 
        POLL_RETRYS: 1000,
516
 
 
517
 
        /**
518
 
         * The poll interval in milliseconds
519
 
         * @property POLL_INTERVAL
520
 
         * @type int
521
 
         * @static
522
 
         * @final
523
 
         */
524
 
        POLL_INTERVAL: 40,
525
 
 
526
 
        /**
527
 
         * addListener/removeListener can throw errors in unexpected scenarios.
528
 
         * These errors are suppressed, the method returns false, and this property
529
 
         * is set
530
 
         * @property lastError
531
 
         * @static
532
 
         * @type Error
533
 
         */
534
 
        lastError: null,
535
 
 
536
 
 
537
 
        /**
538
 
         * poll handle
539
 
         * @property _interval
540
 
         * @static
541
 
         * @private
542
 
         */
543
 
        _interval: null,
544
 
 
545
 
        /**
546
 
         * document readystate poll handle
547
 
         * @property _dri
548
 
         * @static
549
 
         * @private
550
 
         */
551
 
         _dri: null,
552
 
 
553
 
        /**
554
 
         * True when the document is initially usable
555
 
         * @property DOMReady
556
 
         * @type boolean
557
 
         * @static
558
 
         */
559
 
        DOMReady: false,
560
 
 
561
 
        /**
562
 
         * @method startInterval
563
 
         * @static
564
 
         * @private
565
 
         */
566
 
        startInterval: function() {
567
 
            if (!Event._interval) {
568
 
Event._interval = setInterval(Event._poll, Event.POLL_INTERVAL);
569
 
            }
570
 
        },
571
 
 
572
 
        /**
573
 
         * Executes the supplied callback when the item with the supplied
574
 
         * id is found.  This is meant to be used to execute behavior as
575
 
         * soon as possible as the page loads.  If you use this after the
576
 
         * initial page load it will poll for a fixed time for the element.
577
 
         * The number of times it will poll and the frequency are
578
 
         * configurable.  By default it will poll for 10 seconds.
579
 
         *
580
 
         * <p>The callback is executed with a single parameter:
581
 
         * the custom object parameter, if provided.</p>
582
 
         *
583
 
         * @method onAvailable
584
 
         *
585
 
         * @param {string||string[]}   id the id of the element, or an array
586
 
         * of ids to look for.
587
 
         * @param {function} fn what to execute when the element is found.
588
 
         * @param {object}   p_obj an optional object to be passed back as
589
 
         *                   a parameter to fn.
590
 
         * @param {boolean|object}  p_override If set to true, fn will execute
591
 
         *                   in the context of p_obj, if set to an object it
592
 
         *                   will execute in the context of that object
593
 
         * @param checkContent {boolean} check child node readiness (onContentReady)
594
 
         * @static
595
 
         * @deprecated Use Y.on("available")
596
 
         */
597
 
        // @TODO fix arguments
598
 
        onAvailable: function(id, fn, p_obj, p_override, checkContent, compat) {
599
 
 
600
 
            var a = Y.Array(id), i, availHandle;
601
 
 
602
 
 
603
 
            for (i=0; i<a.length; i=i+1) {
604
 
                _avail.push({
605
 
                    id:         a[i],
606
 
                    fn:         fn,
607
 
                    obj:        p_obj,
608
 
                    override:   p_override,
609
 
                    checkReady: checkContent,
610
 
                    compat:     compat
611
 
                });
612
 
            }
613
 
            _retryCount = this.POLL_RETRYS;
614
 
 
615
 
            // We want the first test to be immediate, but async
616
 
            setTimeout(Event._poll, 0);
617
 
 
618
 
            availHandle = new Y.EventHandle({
619
 
 
620
 
                _delete: function() {
621
 
                    // set by the event system for lazy DOM listeners
622
 
                    if (availHandle.handle) {
623
 
                        availHandle.handle.detach();
624
 
                        return;
625
 
                    }
626
 
 
627
 
                    var i, j;
628
 
 
629
 
                    // otherwise try to remove the onAvailable listener(s)
630
 
                    for (i = 0; i < a.length; i++) {
631
 
                        for (j = 0; j < _avail.length; j++) {
632
 
                            if (a[i] === _avail[j].id) {
633
 
                                _avail.splice(j, 1);
634
 
                            }
635
 
                        }
636
 
                    }
637
 
                }
638
 
 
639
 
            });
640
 
 
641
 
            return availHandle;
642
 
        },
643
 
 
644
 
        /**
645
 
         * Works the same way as onAvailable, but additionally checks the
646
 
         * state of sibling elements to determine if the content of the
647
 
         * available element is safe to modify.
648
 
         *
649
 
         * <p>The callback is executed with a single parameter:
650
 
         * the custom object parameter, if provided.</p>
651
 
         *
652
 
         * @method onContentReady
653
 
         *
654
 
         * @param {string}   id the id of the element to look for.
655
 
         * @param {function} fn what to execute when the element is ready.
656
 
         * @param {object}   obj an optional object to be passed back as
657
 
         *                   a parameter to fn.
658
 
         * @param {boolean|object}  override If set to true, fn will execute
659
 
         *                   in the context of p_obj.  If an object, fn will
660
 
         *                   exectute in the context of that object
661
 
         *
662
 
         * @static
663
 
         * @deprecated Use Y.on("contentready")
664
 
         */
665
 
        // @TODO fix arguments
666
 
        onContentReady: function(id, fn, obj, override, compat) {
667
 
            return Event.onAvailable(id, fn, obj, override, true, compat);
668
 
        },
669
 
 
670
 
        /**
671
 
         * Adds an event listener
672
 
         *
673
 
         * @method attach
674
 
         *
675
 
         * @param {String}   type     The type of event to append
676
 
         * @param {Function} fn        The method the event invokes
677
 
         * @param {String|HTMLElement|Array|NodeList} el An id, an element
678
 
         *  reference, or a collection of ids and/or elements to assign the
679
 
         *  listener to.
680
 
         * @param {Object}   context optional context object
681
 
         * @param {Boolean|object}  args 0..n arguments to pass to the callback
682
 
         * @return {EventHandle} an object to that can be used to detach the listener
683
 
         *
684
 
         * @static
685
 
         */
686
 
 
687
 
        attach: function(type, fn, el, context) {
688
 
            return Event._attach(Y.Array(arguments, 0, true));
689
 
        },
690
 
 
691
 
        _createWrapper: function (el, type, capture, compat, facade) {
692
 
 
693
 
            var cewrapper,
694
 
                ek  = Y.stamp(el),
695
 
                key = 'event:' + ek + type;
696
 
 
697
 
            if (false === facade) {
698
 
                key += 'native';
699
 
            }
700
 
            if (capture) {
701
 
                key += 'capture';
702
 
            }
703
 
 
704
 
 
705
 
            cewrapper = _wrappers[key];
706
 
 
707
 
 
708
 
            if (!cewrapper) {
709
 
                // create CE wrapper
710
 
                cewrapper = Y.publish(key, {
711
 
                    silent: true,
712
 
                    bubbles: false,
713
 
                    contextFn: function() {
714
 
                        if (compat) {
715
 
                            return cewrapper.el;
716
 
                        } else {
717
 
                            cewrapper.nodeRef = cewrapper.nodeRef || Y.one(cewrapper.el);
718
 
                            return cewrapper.nodeRef;
719
 
                        }
720
 
                    }
721
 
                });
722
 
 
723
 
                cewrapper.overrides = {};
724
 
 
725
 
                // for later removeListener calls
726
 
                cewrapper.el = el;
727
 
                cewrapper.key = key;
728
 
                cewrapper.domkey = ek;
729
 
                cewrapper.type = type;
730
 
                cewrapper.fn = function(e) {
731
 
                    cewrapper.fire(Event.getEvent(e, el, (compat || (false === facade))));
732
 
                };
733
 
                cewrapper.capture = capture;
734
 
 
735
 
                if (el == win && type == "load") {
736
 
                    // window load happens once
737
 
                    cewrapper.fireOnce = true;
738
 
                    _windowLoadKey = key;
739
 
                }
740
 
                cewrapper._delete = _deleteAndClean;
741
 
 
742
 
                _wrappers[key] = cewrapper;
743
 
                _el_events[ek] = _el_events[ek] || {};
744
 
                _el_events[ek][key] = cewrapper;
745
 
 
746
 
                add(el, type, cewrapper.fn, capture);
747
 
            }
748
 
 
749
 
            return cewrapper;
750
 
 
751
 
        },
752
 
 
753
 
        _attach: function(args, conf) {
754
 
 
755
 
            var compat,
756
 
                handles, oEl, cewrapper, context,
757
 
                fireNow = false, ret,
758
 
                type = args[0],
759
 
                fn = args[1],
760
 
                el = args[2] || win,
761
 
                facade = conf && conf.facade,
762
 
                capture = conf && conf.capture,
763
 
                overrides = conf && conf.overrides;
764
 
 
765
 
            if (args[args.length-1] === COMPAT_ARG) {
766
 
                compat = true;
767
 
            }
768
 
 
769
 
            if (!fn || !fn.call) {
770
 
// throw new TypeError(type + " attach call failed, callback undefined");
771
 
                return false;
772
 
            }
773
 
 
774
 
            // The el argument can be an array of elements or element ids.
775
 
            if (shouldIterate(el)) {
776
 
 
777
 
                handles=[];
778
 
 
779
 
                Y.each(el, function(v, k) {
780
 
                    args[2] = v;
781
 
                    handles.push(Event._attach(args.slice(), conf));
782
 
                });
783
 
 
784
 
                // return (handles.length === 1) ? handles[0] : handles;
785
 
                return new Y.EventHandle(handles);
786
 
 
787
 
            // If the el argument is a string, we assume it is
788
 
            // actually the id of the element.  If the page is loaded
789
 
            // we convert el to the actual element, otherwise we
790
 
            // defer attaching the event until the element is
791
 
            // ready
792
 
            } else if (Y.Lang.isString(el)) {
793
 
 
794
 
                // oEl = (compat) ? Y.DOM.byId(el) : Y.Selector.query(el);
795
 
 
796
 
                if (compat) {
797
 
                    oEl = Y.DOM.byId(el);
798
 
                } else {
799
 
 
800
 
                    oEl = Y.Selector.query(el);
801
 
 
802
 
                    switch (oEl.length) {
803
 
                        case 0:
804
 
                            oEl = null;
805
 
                            break;
806
 
                        case 1:
807
 
                            oEl = oEl[0];
808
 
                            break;
809
 
                        default:
810
 
                            args[2] = oEl;
811
 
                            return Event._attach(args, conf);
812
 
                    }
813
 
                }
814
 
 
815
 
                if (oEl) {
816
 
 
817
 
                    el = oEl;
818
 
 
819
 
                // Not found = defer adding the event until the element is available
820
 
                } else {
821
 
 
822
 
                    ret = Event.onAvailable(el, function() {
823
 
 
824
 
                        ret.handle = Event._attach(args, conf);
825
 
 
826
 
                    }, Event, true, false, compat);
827
 
 
828
 
                    return ret;
829
 
 
830
 
                }
831
 
            }
832
 
 
833
 
            // Element should be an html element or node
834
 
            if (!el) {
835
 
                return false;
836
 
            }
837
 
 
838
 
            if (Y.Node && Y.instanceOf(el, Y.Node)) {
839
 
                el = Y.Node.getDOMNode(el);
840
 
            }
841
 
 
842
 
            cewrapper = Event._createWrapper(el, type, capture, compat, facade);
843
 
            if (overrides) {
844
 
                Y.mix(cewrapper.overrides, overrides);
845
 
            }
846
 
 
847
 
            if (el == win && type == "load") {
848
 
 
849
 
                // if the load is complete, fire immediately.
850
 
                // all subscribers, including the current one
851
 
                // will be notified.
852
 
                if (YUI.Env.windowLoaded) {
853
 
                    fireNow = true;
854
 
                }
855
 
            }
856
 
 
857
 
            if (compat) {
858
 
                args.pop();
859
 
            }
860
 
 
861
 
            context = args[3];
862
 
 
863
 
            // set context to the Node if not specified
864
 
            // ret = cewrapper.on.apply(cewrapper, trimmedArgs);
865
 
            ret = cewrapper._on(fn, context, (args.length > 4) ? args.slice(4) : null);
866
 
 
867
 
            if (fireNow) {
868
 
                cewrapper.fire();
869
 
            }
870
 
 
871
 
            return ret;
872
 
 
873
 
        },
874
 
 
875
 
        /**
876
 
         * Removes an event listener.  Supports the signature the event was bound
877
 
         * with, but the preferred way to remove listeners is using the handle
878
 
         * that is returned when using Y.on
879
 
         *
880
 
         * @method detach
881
 
         *
882
 
         * @param {String} type the type of event to remove.
883
 
         * @param {Function} fn the method the event invokes.  If fn is
884
 
         * undefined, then all event handlers for the type of event are
885
 
         * removed.
886
 
         * @param {String|HTMLElement|Array|NodeList|EventHandle} el An
887
 
         * event handle, an id, an element reference, or a collection
888
 
         * of ids and/or elements to remove the listener from.
889
 
         * @return {boolean} true if the unbind was successful, false otherwise.
890
 
         * @static
891
 
         */
892
 
        detach: function(type, fn, el, obj) {
893
 
 
894
 
            var args=Y.Array(arguments, 0, true), compat, l, ok, i,
895
 
                id, ce;
896
 
 
897
 
            if (args[args.length-1] === COMPAT_ARG) {
898
 
                compat = true;
899
 
                // args.pop();
900
 
            }
901
 
 
902
 
            if (type && type.detach) {
903
 
                return type.detach();
904
 
            }
905
 
 
906
 
            // The el argument can be a string
907
 
            if (typeof el == "string") {
908
 
 
909
 
                // el = (compat) ? Y.DOM.byId(el) : Y.all(el);
910
 
                if (compat) {
911
 
                    el = Y.DOM.byId(el);
912
 
                } else {
913
 
                    el = Y.Selector.query(el);
914
 
                    l = el.length;
915
 
                    if (l < 1) {
916
 
                        el = null;
917
 
                    } else if (l == 1) {
918
 
                        el = el[0];
919
 
                    }
920
 
                }
921
 
                // return Event.detach.apply(Event, args);
922
 
            }
923
 
 
924
 
            if (!el) {
925
 
                return false;
926
 
            }
927
 
 
928
 
            if (el.detach) {
929
 
                args.splice(2, 1);
930
 
                return el.detach.apply(el, args);
931
 
            // The el argument can be an array of elements or element ids.
932
 
            } else if (shouldIterate(el)) {
933
 
                ok = true;
934
 
                for (i=0, l=el.length; i<l; ++i) {
935
 
                    args[2] = el[i];
936
 
                    ok = ( Y.Event.detach.apply(Y.Event, args) && ok );
937
 
                }
938
 
 
939
 
                return ok;
940
 
            }
941
 
 
942
 
            if (!type || !fn || !fn.call) {
943
 
                return Event.purgeElement(el, false, type);
944
 
            }
945
 
 
946
 
            id = 'event:' + Y.stamp(el) + type;
947
 
            ce = _wrappers[id];
948
 
 
949
 
            if (ce) {
950
 
                return ce.detach(fn);
951
 
            } else {
952
 
                return false;
953
 
            }
954
 
 
955
 
        },
956
 
 
957
 
        /**
958
 
         * Finds the event in the window object, the caller's arguments, or
959
 
         * in the arguments of another method in the callstack.  This is
960
 
         * executed automatically for events registered through the event
961
 
         * manager, so the implementer should not normally need to execute
962
 
         * this function at all.
963
 
         * @method getEvent
964
 
         * @param {Event} e the event parameter from the handler
965
 
         * @param {HTMLElement} el the element the listener was attached to
966
 
         * @return {Event} the event
967
 
         * @static
968
 
         */
969
 
        getEvent: function(e, el, noFacade) {
970
 
            var ev = e || win.event;
971
 
 
972
 
            return (noFacade) ? ev :
973
 
                new Y.DOMEventFacade(ev, el, _wrappers['event:' + Y.stamp(el) + e.type]);
974
 
        },
975
 
 
976
 
        /**
977
 
         * Generates an unique ID for the element if it does not already
978
 
         * have one.
979
 
         * @method generateId
980
 
         * @param el the element to create the id for
981
 
         * @return {string} the resulting id of the element
982
 
         * @static
983
 
         */
984
 
        generateId: function(el) {
985
 
            return Y.DOM.generateID(el);
986
 
        },
987
 
 
988
 
        /**
989
 
         * We want to be able to use getElementsByTagName as a collection
990
 
         * to attach a group of events to.  Unfortunately, different
991
 
         * browsers return different types of collections.  This function
992
 
         * tests to determine if the object is array-like.  It will also
993
 
         * fail if the object is an array, but is empty.
994
 
         * @method _isValidCollection
995
 
         * @param o the object to test
996
 
         * @return {boolean} true if the object is array-like and populated
997
 
         * @deprecated was not meant to be used directly
998
 
         * @static
999
 
         * @private
1000
 
         */
1001
 
        _isValidCollection: shouldIterate,
1002
 
 
1003
 
        /**
1004
 
         * hook up any deferred listeners
1005
 
         * @method _load
1006
 
         * @static
1007
 
         * @private
1008
 
         */
1009
 
        _load: function(e) {
1010
 
            if (!_loadComplete) {
1011
 
                _loadComplete = true;
1012
 
 
1013
 
                // Just in case DOMReady did not go off for some reason
1014
 
                // E._ready();
1015
 
                if (Y.fire) {
1016
 
                    Y.fire(EVENT_READY);
1017
 
                }
1018
 
 
1019
 
                // Available elements may not have been detected before the
1020
 
                // window load event fires. Try to find them now so that the
1021
 
                // the user is more likely to get the onAvailable notifications
1022
 
                // before the window load notification
1023
 
                Event._poll();
1024
 
            }
1025
 
        },
1026
 
 
1027
 
        /**
1028
 
         * Polling function that runs before the onload event fires,
1029
 
         * attempting to attach to DOM Nodes as soon as they are
1030
 
         * available
1031
 
         * @method _poll
1032
 
         * @static
1033
 
         * @private
1034
 
         */
1035
 
        _poll: function() {
1036
 
            if (Event.locked) {
1037
 
                return;
1038
 
            }
1039
 
 
1040
 
            if (Y.UA.ie && !YUI.Env.DOMReady) {
1041
 
                // Hold off if DOMReady has not fired and check current
1042
 
                // readyState to protect against the IE operation aborted
1043
 
                // issue.
1044
 
                Event.startInterval();
1045
 
                return;
1046
 
            }
1047
 
 
1048
 
            Event.locked = true;
1049
 
 
1050
 
            // keep trying until after the page is loaded.  We need to
1051
 
            // check the page load state prior to trying to bind the
1052
 
            // elements so that we can be certain all elements have been
1053
 
            // tested appropriately
1054
 
            var i, len, item, el, notAvail, executeItem,
1055
 
                tryAgain = !_loadComplete;
1056
 
 
1057
 
            if (!tryAgain) {
1058
 
                tryAgain = (_retryCount > 0);
1059
 
            }
1060
 
 
1061
 
            // onAvailable
1062
 
            notAvail = [];
1063
 
 
1064
 
            executeItem = function (el, item) {
1065
 
                var context, ov = item.override;
1066
 
                try {
1067
 
                    if (item.compat) {
1068
 
                        if (item.override) {
1069
 
                            if (ov === true) {
1070
 
                                context = item.obj;
1071
 
                            } else {
1072
 
                                context = ov;
1073
 
                            }
1074
 
                        } else {
1075
 
                            context = el;
1076
 
                        }
1077
 
                        item.fn.call(context, item.obj);
1078
 
                    } else {
1079
 
                        context = item.obj || Y.one(el);
1080
 
                        item.fn.apply(context, (Y.Lang.isArray(ov)) ? ov : []);
1081
 
                    }
1082
 
                } catch (e) {
1083
 
                }
1084
 
            };
1085
 
 
1086
 
            // onAvailable
1087
 
            for (i=0,len=_avail.length; i<len; ++i) {
1088
 
                item = _avail[i];
1089
 
                if (item && !item.checkReady) {
1090
 
 
1091
 
                    // el = (item.compat) ? Y.DOM.byId(item.id) : Y.one(item.id);
1092
 
                    el = (item.compat) ? Y.DOM.byId(item.id) : Y.Selector.query(item.id, null, true);
1093
 
 
1094
 
                    if (el) {
1095
 
                        executeItem(el, item);
1096
 
                        _avail[i] = null;
1097
 
                    } else {
1098
 
                        notAvail.push(item);
1099
 
                    }
1100
 
                }
1101
 
            }
1102
 
 
1103
 
            // onContentReady
1104
 
            for (i=0,len=_avail.length; i<len; ++i) {
1105
 
                item = _avail[i];
1106
 
                if (item && item.checkReady) {
1107
 
 
1108
 
                    // el = (item.compat) ? Y.DOM.byId(item.id) : Y.one(item.id);
1109
 
                    el = (item.compat) ? Y.DOM.byId(item.id) : Y.Selector.query(item.id, null, true);
1110
 
 
1111
 
                    if (el) {
1112
 
                        // The element is available, but not necessarily ready
1113
 
                        // @todo should we test parentNode.nextSibling?
1114
 
                        if (_loadComplete || (el.get && el.get('nextSibling')) || el.nextSibling) {
1115
 
                            executeItem(el, item);
1116
 
                            _avail[i] = null;
1117
 
                        }
1118
 
                    } else {
1119
 
                        notAvail.push(item);
1120
 
                    }
1121
 
                }
1122
 
            }
1123
 
 
1124
 
            _retryCount = (notAvail.length === 0) ? 0 : _retryCount - 1;
1125
 
 
1126
 
            if (tryAgain) {
1127
 
                // we may need to strip the nulled out items here
1128
 
                Event.startInterval();
1129
 
            } else {
1130
 
                clearInterval(Event._interval);
1131
 
                Event._interval = null;
1132
 
            }
1133
 
 
1134
 
            Event.locked = false;
1135
 
 
1136
 
            return;
1137
 
 
1138
 
        },
1139
 
 
1140
 
        /**
1141
 
         * Removes all listeners attached to the given element via addListener.
1142
 
         * Optionally, the node's children can also be purged.
1143
 
         * Optionally, you can specify a specific type of event to remove.
1144
 
         * @method purgeElement
1145
 
         * @param {HTMLElement} el the element to purge
1146
 
         * @param {boolean} recurse recursively purge this element's children
1147
 
         * as well.  Use with caution.
1148
 
         * @param {string} type optional type of listener to purge. If
1149
 
         * left out, all listeners will be removed
1150
 
         * @static
1151
 
         */
1152
 
        purgeElement: function(el, recurse, type) {
1153
 
            // var oEl = (Y.Lang.isString(el)) ? Y.one(el) : el,
1154
 
            var oEl = (Y.Lang.isString(el)) ?  Y.Selector.query(el, null, true) : el,
1155
 
                lis = Event.getListeners(oEl, type), i, len, children, child;
1156
 
 
1157
 
            if (recurse && oEl) {
1158
 
                lis = lis || [];
1159
 
                children = Y.Selector.query('*', oEl);
1160
 
                i = 0;
1161
 
                len = children.length;
1162
 
                for (; i < len; ++i) {
1163
 
                    child = Event.getListeners(children[i], type);
1164
 
                    if (child) {
1165
 
                        lis = lis.concat(child);
1166
 
                    }
1167
 
                }
1168
 
            }
1169
 
 
1170
 
            if (lis) {
1171
 
                for (i = 0, len = lis.length; i < len; ++i) {
1172
 
                    lis[i].detachAll();
1173
 
                }
1174
 
            }
1175
 
 
1176
 
        },
1177
 
 
1178
 
        /**
1179
 
         * Removes all object references and the DOM proxy subscription for
1180
 
         * a given event for a DOM node.
1181
 
         *
1182
 
         * @method _clean
1183
 
         * @param wrapper {CustomEvent} Custom event proxy for the DOM
1184
 
         *                  subscription
1185
 
         * @private
1186
 
         * @static
1187
 
         * @since 3.4.0
1188
 
         */
1189
 
        _clean: function (wrapper) {
1190
 
            var key    = wrapper.key,
1191
 
                domkey = wrapper.domkey;
1192
 
 
1193
 
            remove(wrapper.el, wrapper.type, wrapper.fn, wrapper.capture);
1194
 
            delete _wrappers[key];
1195
 
            delete Y._yuievt.events[key];
1196
 
            if (_el_events[domkey]) {
1197
 
                delete _el_events[domkey][key];
1198
 
                if (!Y.Object.size(_el_events[domkey])) {
1199
 
                    delete _el_events[domkey];
1200
 
                }
1201
 
            }
1202
 
        },
1203
 
 
1204
 
        /**
1205
 
         * Returns all listeners attached to the given element via addListener.
1206
 
         * Optionally, you can specify a specific type of event to return.
1207
 
         * @method getListeners
1208
 
         * @param el {HTMLElement|string} the element or element id to inspect
1209
 
         * @param type {string} optional type of listener to return. If
1210
 
         * left out, all listeners will be returned
1211
 
         * @return {CustomEvent} the custom event wrapper for the DOM event(s)
1212
 
         * @static
1213
 
         */
1214
 
        getListeners: function(el, type) {
1215
 
            var ek = Y.stamp(el, true), evts = _el_events[ek],
1216
 
                results=[] , key = (type) ? 'event:' + ek + type : null,
1217
 
                adapters = _eventenv.plugins;
1218
 
 
1219
 
            if (!evts) {
1220
 
                return null;
1221
 
            }
1222
 
 
1223
 
            if (key) {
1224
 
                // look for synthetic events
1225
 
                if (adapters[type] && adapters[type].eventDef) {
1226
 
                    key += '_synth';
1227
 
                }
1228
 
 
1229
 
                if (evts[key]) {
1230
 
                    results.push(evts[key]);
1231
 
                }
1232
 
 
1233
 
                // get native events as well
1234
 
                key += 'native';
1235
 
                if (evts[key]) {
1236
 
                    results.push(evts[key]);
1237
 
                }
1238
 
 
1239
 
            } else {
1240
 
                Y.each(evts, function(v, k) {
1241
 
                    results.push(v);
1242
 
                });
1243
 
            }
1244
 
 
1245
 
            return (results.length) ? results : null;
1246
 
        },
1247
 
 
1248
 
        /**
1249
 
         * Removes all listeners registered by pe.event.  Called
1250
 
         * automatically during the unload event.
1251
 
         * @method _unload
1252
 
         * @static
1253
 
         * @private
1254
 
         */
1255
 
        _unload: function(e) {
1256
 
            Y.each(_wrappers, function(v, k) {
1257
 
                if (v.type == 'unload') {
1258
 
                    v.fire(e);
1259
 
                }
1260
 
                v.detachAll();
1261
 
            });
1262
 
            remove(win, "unload", onUnload);
1263
 
        },
1264
 
 
1265
 
        /**
1266
 
         * Adds a DOM event directly without the caching, cleanup, context adj, etc
1267
 
         *
1268
 
         * @method nativeAdd
1269
 
         * @param {HTMLElement} el      the element to bind the handler to
1270
 
         * @param {string}      type   the type of event handler
1271
 
         * @param {function}    fn      the callback to invoke
1272
 
         * @param {boolen}      capture capture or bubble phase
1273
 
         * @static
1274
 
         * @private
1275
 
         */
1276
 
        nativeAdd: add,
1277
 
 
1278
 
        /**
1279
 
         * Basic remove listener
1280
 
         *
1281
 
         * @method nativeRemove
1282
 
         * @param {HTMLElement} el      the element to bind the handler to
1283
 
         * @param {string}      type   the type of event handler
1284
 
         * @param {function}    fn      the callback to invoke
1285
 
         * @param {boolen}      capture capture or bubble phase
1286
 
         * @static
1287
 
         * @private
1288
 
         */
1289
 
        nativeRemove: remove
1290
 
    };
1291
 
 
1292
 
}();
1293
 
 
1294
 
Y.Event = Event;
1295
 
 
1296
 
if (config.injected || YUI.Env.windowLoaded) {
1297
 
    onLoad();
1298
 
} else {
1299
 
    add(win, "load", onLoad);
1300
 
}
1301
 
 
1302
 
// Process onAvailable/onContentReady items when when the DOM is ready in IE
1303
 
if (Y.UA.ie) {
1304
 
    Y.on(EVENT_READY, Event._poll);
1305
 
}
1306
 
 
1307
 
add(win, "unload", onUnload);
1308
 
 
1309
 
Event.Custom = Y.CustomEvent;
1310
 
Event.Subscriber = Y.Subscriber;
1311
 
Event.Target = Y.EventTarget;
1312
 
Event.Handle = Y.EventHandle;
1313
 
Event.Facade = Y.EventFacade;
1314
 
 
1315
 
Event._poll();
1316
 
 
1317
 
})();
1318
 
 
1319
 
/**
1320
 
 * DOM event listener abstraction layer
1321
 
 * @module event
1322
 
 * @submodule event-base
1323
 
 */
1324
 
 
1325
 
/**
1326
 
 * Executes the callback as soon as the specified element
1327
 
 * is detected in the DOM.  This function expects a selector
1328
 
 * string for the element(s) to detect.  If you already have
1329
 
 * an element reference, you don't need this event.
1330
 
 * @event available
1331
 
 * @param type {string} 'available'
1332
 
 * @param fn {function} the callback function to execute.
1333
 
 * @param el {string} an selector for the element(s) to attach
1334
 
 * @param context optional argument that specifies what 'this' refers to.
1335
 
 * @param args* 0..n additional arguments to pass on to the callback function.
1336
 
 * These arguments will be added after the event object.
1337
 
 * @return {EventHandle} the detach handle
1338
 
 * @for YUI
1339
 
 */
1340
 
Y.Env.evt.plugins.available = {
1341
 
    on: function(type, fn, id, o) {
1342
 
        var a = arguments.length > 4 ?  Y.Array(arguments, 4, true) : null;
1343
 
        return Y.Event.onAvailable.call(Y.Event, id, fn, o, a);
1344
 
    }
1345
 
};
1346
 
 
1347
 
/**
1348
 
 * Executes the callback as soon as the specified element
1349
 
 * is detected in the DOM with a nextSibling property
1350
 
 * (indicating that the element's children are available).
1351
 
 * This function expects a selector
1352
 
 * string for the element(s) to detect.  If you already have
1353
 
 * an element reference, you don't need this event.
1354
 
 * @event contentready
1355
 
 * @param type {string} 'contentready'
1356
 
 * @param fn {function} the callback function to execute.
1357
 
 * @param el {string} an selector for the element(s) to attach.
1358
 
 * @param context optional argument that specifies what 'this' refers to.
1359
 
 * @param args* 0..n additional arguments to pass on to the callback function.
1360
 
 * These arguments will be added after the event object.
1361
 
 * @return {EventHandle} the detach handle
1362
 
 * @for YUI
1363
 
 */
1364
 
Y.Env.evt.plugins.contentready = {
1365
 
    on: function(type, fn, id, o) {
1366
 
        var a = arguments.length > 4 ? Y.Array(arguments, 4, true) : null;
1367
 
        return Y.Event.onContentReady.call(Y.Event, id, fn, o, a);
1368
 
    }
1369
 
};
1370
 
 
1371
 
 
1372
 
}, '3.5.1' ,{requires:['event-custom-base']});