2 Copyright (c) 2008, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.net/yui/license.txt
9 * The CustomEvent class lets you define events for your application
10 * that can be subscribed to by one or more independent component.
12 * @param {String} type The type of event, which is passed to the callback
13 * when the event fires
14 * @param {Object} oScope The context the event will fire from. "this" will
15 * refer to this object in the callback. Default value:
16 * the window object. The listener can override this.
17 * @param {boolean} silent pass true to prevent the event from writing to
19 * @param {int} signature the signature that the custom event subscriber
20 * will receive. YAHOO.util.CustomEvent.LIST or
21 * YAHOO.util.CustomEvent.FLAT. The default is
22 * YAHOO.util.CustomEvent.LIST.
23 * @namespace YAHOO.util
27 YAHOO
.util
.CustomEvent = function(type
, oScope
, silent
, signature
) {
30 * The type of event, returned to subscribers when the event fires
37 * The scope the the event will fire from by default. Defaults to the window
42 this.scope
= oScope
|| window
;
45 * By default all custom events are logged in the debug build, set silent
46 * to true to disable debug outpu for this event.
53 * Custom events support two styles of arguments provided to the event
56 * <li>YAHOO.util.CustomEvent.LIST:
58 * <li>param1: event name</li>
59 * <li>param2: array of arguments sent to fire</li>
60 * <li>param3: <optional> a custom object supplied by the subscriber</li>
63 * <li>YAHOO.util.CustomEvent.FLAT
65 * <li>param1: the first argument passed to fire. If you need to
66 * pass multiple parameters, use and array or object literal</li>
67 * <li>param2: <optional> a custom object supplied by the subscriber</li>
74 this.signature
= signature
|| YAHOO
.util
.CustomEvent
.LIST
;
77 * The subscribers to this event
78 * @property subscribers
81 this.subscribers
= [];
86 var onsubscribeType
= "_YUICEOnSubscribe";
88 // Only add subscribe events for events that are not generated by
90 if (type
!== onsubscribeType
) {
93 * Custom events provide a custom event that fires whenever there is
94 * a new subscriber to the event. This provides an opportunity to
95 * handle the case where there is a non-repeating event that has
96 * already fired has a new subscriber.
98 * @event subscribeEvent
99 * @type YAHOO.util.CustomEvent
100 * @param {Function} fn The function to execute
101 * @param {Object} obj An object to be passed along when the event
103 * @param {boolean|Object} override If true, the obj passed in becomes
104 * the execution scope of the listener.
105 * if an object, that object becomes the
106 * the execution scope.
108 this.subscribeEvent
=
109 new YAHOO
.util
.CustomEvent(onsubscribeType
, this, true);
115 * In order to make it possible to execute the rest of the subscriber
116 * stack when one thows an exception, the subscribers exceptions are
117 * caught. The most recent exception is stored in this property
118 * @property lastError
121 this.lastError
= null;
125 * Subscriber listener sigature constant. The LIST type returns three
126 * parameters: the event type, the array of args passed to fire, and
127 * the optional custom object
128 * @property YAHOO.util.CustomEvent.LIST
132 YAHOO
.util
.CustomEvent
.LIST
= 0;
135 * Subscriber listener sigature constant. The FLAT type returns two
136 * parameters: the first argument passed to fire and the optional
138 * @property YAHOO.util.CustomEvent.FLAT
142 YAHOO
.util
.CustomEvent
.FLAT
= 1;
144 YAHOO
.util
.CustomEvent
.prototype = {
147 * Subscribes the caller to this event
149 * @param {Function} fn The function to execute
150 * @param {Object} obj An object to be passed along when the event
152 * @param {boolean|Object} override If true, the obj passed in becomes
153 * the execution scope of the listener.
154 * if an object, that object becomes the
155 * the execution scope.
157 subscribe: function(fn
, obj
, override
) {
160 throw new Error("Invalid callback for subscriber to '" + this.type
+ "'");
163 if (this.subscribeEvent
) {
164 this.subscribeEvent
.fire(fn
, obj
, override
);
167 this.subscribers
.push( new YAHOO
.util
.Subscriber(fn
, obj
, override
) );
171 * Unsubscribes subscribers.
172 * @method unsubscribe
173 * @param {Function} fn The subscribed function to remove, if not supplied
174 * all will be removed
175 * @param {Object} obj The custom object passed to subscribe. This is
176 * optional, but if supplied will be used to
177 * disambiguate multiple listeners that are the same
178 * (e.g., you subscribe many object using a function
179 * that lives on the prototype)
180 * @return {boolean} True if the subscriber was found and detached.
182 unsubscribe: function(fn
, obj
) {
185 return this.unsubscribeAll();
189 for (var i
=0, len
=this.subscribers
.length
; i
<len
; ++i
) {
190 var s
= this.subscribers
[i
];
191 if (s
&& s
.contains(fn
, obj
)) {
201 * Notifies the subscribers. The callback functions will be executed
202 * from the scope specified when the event was created, and with the
203 * following parameters:
205 * <li>The type of event</li>
206 * <li>All of the arguments fire() was executed with as an array</li>
207 * <li>The custom object (if any) that was passed into the subscribe()
211 * @param {Object*} arguments an arbitrary set of parameters to pass to
213 * @return {boolean} false if one of the subscribers returned false,
218 this.lastError
= null;
221 len
=this.subscribers
.length
;
223 if (!len
&& this.silent
) {
227 var args
=[].slice
.call(arguments
, 0), ret
=true, i
, rebuild
=false;
232 // make a copy of the subscribers so that there are
233 // no index problems if one subscriber removes another.
234 var subs
= this.subscribers
.slice(), throwErrors
= YAHOO
.util
.Event
.throwErrors
;
236 for (i
=0; i
<len
; ++i
) {
244 var scope
= s
.getScope(this.scope
);
246 if (this.signature
== YAHOO
.util
.CustomEvent
.FLAT
) {
248 if (args
.length
> 0) {
253 ret
= s
.fn
.call(scope
, param
, s
.obj
);
263 ret
= s
.fn
.call(scope
, this.type
, args
, s
.obj
);
282 return (ret
!== false);
286 * Removes all listeners
287 * @method unsubscribeAll
288 * @return {int} The number of listeners unsubscribed
290 unsubscribeAll: function() {
291 for (var i
=this.subscribers
.length
-1; i
>-1; i
--) {
304 _delete: function(index
) {
305 var s
= this.subscribers
[index
];
311 // this.subscribers[index]=null;
312 this.subscribers
.splice(index
, 1);
318 toString: function() {
319 return "CustomEvent: " + "'" + this.type
+ "', " +
320 "scope: " + this.scope
;
325 /////////////////////////////////////////////////////////////////////
328 * Stores the subscriber information to be used when the event fires.
329 * @param {Function} fn The function to execute
330 * @param {Object} obj An object to be passed along when the event fires
331 * @param {boolean} override If true, the obj passed in becomes the execution
332 * scope of the listener
336 YAHOO
.util
.Subscriber = function(fn
, obj
, override
) {
339 * The callback that will be execute when the event fires
346 * An optional custom object that will passed to the callback when
351 this.obj
= YAHOO
.lang
.isUndefined(obj
) ? null : obj
;
354 * The default execution scope for the event listener is defined when the
355 * event is created (usually the object which contains the event).
356 * By setting override to true, the execution scope becomes the custom
357 * object passed in by the subscriber. If override is an object, that
358 * object becomes the scope.
360 * @type boolean|object
362 this.override
= override
;
367 * Returns the execution scope for this listener. If override was set to true
368 * the custom obj will be the scope. If override is an object, that is the
369 * scope, otherwise the default scope will be used.
371 * @param {Object} defaultScope the scope to use if this listener does not
374 YAHOO
.util
.Subscriber
.prototype.getScope = function(defaultScope
) {
376 if (this.override
=== true) {
379 return this.override
;
386 * Returns true if the fn and obj match this objects properties.
387 * Used by the unsubscribe method to match the right subscriber.
390 * @param {Function} fn the function to execute
391 * @param {Object} obj an object to be passed along when the event fires
392 * @return {boolean} true if the supplied arguments match this
393 * subscriber's signature.
395 YAHOO
.util
.Subscriber
.prototype.contains = function(fn
, obj
) {
397 return (this.fn
== fn
&& this.obj
== obj
);
399 return (this.fn
== fn
);
406 YAHOO
.util
.Subscriber
.prototype.toString = function() {
407 return "Subscriber { obj: " + this.obj
+
408 ", override: " + (this.override
|| "no") + " }";
412 * The Event Utility provides utilities for managing DOM Events and tools
413 * for building event systems
416 * @title Event Utility
417 * @namespace YAHOO.util
421 // The first instance of Event will win if it is loaded more than once.
422 // @TODO this needs to be changed so that only the state data that needs to
423 // be preserved is kept, while methods are overwritten/added as needed.
424 // This means that the module pattern can't be used.
425 if (!YAHOO
.util
.Event
) {
428 * The event utility provides functions to add and remove event listeners,
429 * event cleansing. It also tries to automatically remove listeners it
430 * registers during the unload event.
435 YAHOO
.util
.Event = function() {
438 * True after the onload event has fired
439 * @property loadComplete
444 var loadComplete
= false;
447 * Cache of wrapped listeners
448 * @property listeners
456 * User-defined unload function that will be fired before all events
458 * @property unloadListeners
463 var unloadListeners
= [];
466 * Cache of DOM0 event handlers to work around issues with DOM2 events
468 * @property legacyEvents
472 var legacyEvents
= [];
475 * Listener stack for DOM0 events
476 * @property legacyHandlers
480 var legacyHandlers
= [];
483 * The number of times to poll after window.onload. This number is
484 * increased if additional late-bound handlers are requested after
486 * @property retryCount
493 * onAvailable listeners
494 * @property onAvailStack
498 var onAvailStack
= [];
501 * Lookup table for legacy events
502 * @property legacyMap
509 * Counter for auto id generation
517 * Normalized keycodes for webkit/safari
518 * @property webkitKeymap
529 63276: 33, // page up
530 63277: 34, // page down
531 25: 9 // SHIFT-TAB (Safari provides a different key code in
532 // this case, even though the shiftKey modifier is set)
535 // String constants used by the addFocusListener and removeFocusListener methods
536 var _FOCUS
= YAHOO
.env
.ua
.ie
? "focusin" : "focus";
537 var _BLUR
= YAHOO
.env
.ua
.ie
? "focusout" : "blur";
542 * The number of times we should look for elements that are not
543 * in the DOM at the time the event is requested after the document
544 * has been loaded. The default is 2000@amp;20 ms, so it will poll
545 * for 40 seconds or until all outstanding handlers are bound
546 * (whichever comes first).
547 * @property POLL_RETRYS
555 * The poll interval in milliseconds
556 * @property POLL_INTERVAL
564 * Element to bind, int constant
573 * Type of event, int constant
582 * Function to execute, int constant
591 * Function wrapped for scope correction and cleanup, int constant
600 * Object passed in by the user that will be returned as a
601 * parameter to the callback, int constant. Specific to
611 * Adjusted scope, either the element we are registering the event
612 * on or the custom object passed in by the listener, int constant
613 * @property ADJ_SCOPE
621 * The original obj passed into addListener
630 * The original scope parameter passed into addListener
639 * The original capture parameter passed into _addListener
649 * addListener/removeListener can throw errors in unexpected scenarios.
650 * These errors are suppressed, the method returns false, and this property
652 * @property lastError
663 * @deprecated use YAHOO.env.ua.webkit
665 isSafari
: YAHOO
.env
.ua
.webkit
,
673 * @deprecated use YAHOO.env.ua.webkit
675 webkit
: YAHOO
.env
.ua
.webkit
,
682 * @deprecated use YAHOO.env.ua.ie
684 isIE
: YAHOO
.env
.ua
.ie
,
688 * @property _interval
695 * document readystate poll handle
703 * True when the document is initially usable
711 * Errors thrown by subscribers of custom events are caught
712 * and the error message is written to the debug console. If
713 * this property is set to true, it will also re-throw the
715 * @property throwErrors
722 * @method startInterval
726 startInterval: function() {
727 if (!this._interval
) {
729 var callback = function() { self
._tryPreloadAttach(); };
730 this._interval
= setInterval(callback
, this.POLL_INTERVAL
);
735 * Executes the supplied callback when the item with the supplied
736 * id is found. This is meant to be used to execute behavior as
737 * soon as possible as the page loads. If you use this after the
738 * initial page load it will poll for a fixed time for the element.
739 * The number of times it will poll and the frequency are
740 * configurable. By default it will poll for 10 seconds.
742 * <p>The callback is executed with a single parameter:
743 * the custom object parameter, if provided.</p>
745 * @method onAvailable
747 * @param {string||string[]} p_id the id of the element, or an array
748 * of ids to look for.
749 * @param {function} p_fn what to execute when the element is found.
750 * @param {object} p_obj an optional object to be passed back as
751 * a parameter to p_fn.
752 * @param {boolean|object} p_override If set to true, p_fn will execute
753 * in the scope of p_obj, if set to an object it
754 * will execute in the scope of that object
755 * @param checkContent {boolean} check child node readiness (onContentReady)
758 onAvailable: function(p_id
, p_fn
, p_obj
, p_override
, checkContent
) {
760 var a
= (YAHOO
.lang
.isString(p_id
)) ? [p_id
] : p_id
;
762 for (var i
=0; i
<a
.length
; i
=i
+1) {
763 onAvailStack
.push({id
: a
[i
],
766 override
: p_override
,
767 checkReady
: checkContent
});
770 retryCount
= this.POLL_RETRYS
;
772 this.startInterval();
776 * Works the same way as onAvailable, but additionally checks the
777 * state of sibling elements to determine if the content of the
778 * available element is safe to modify.
780 * <p>The callback is executed with a single parameter:
781 * the custom object parameter, if provided.</p>
783 * @method onContentReady
785 * @param {string} p_id the id of the element to look for.
786 * @param {function} p_fn what to execute when the element is ready.
787 * @param {object} p_obj an optional object to be passed back as
788 * a parameter to p_fn.
789 * @param {boolean|object} p_override If set to true, p_fn will execute
790 * in the scope of p_obj. If an object, p_fn will
791 * exectute in the scope of that object
795 onContentReady: function(p_id
, p_fn
, p_obj
, p_override
) {
796 this.onAvailable(p_id
, p_fn
, p_obj
, p_override
, true);
800 * Executes the supplied callback when the DOM is first usable. This
801 * will execute immediately if called after the DOMReady event has
802 * fired. @todo the DOMContentReady event does not fire when the
803 * script is dynamically injected into the page. This means the
804 * DOMReady custom event will never fire in FireFox or Opera when the
805 * library is injected. It _will_ fire in Safari, and the IE
806 * implementation would allow for us to fire it if the defered script
807 * is not available. We want this to behave the same in all browsers.
808 * Is there a way to identify when the script has been injected
809 * instead of included inline? Is there a way to know whether the
810 * window onload event has fired without having had a listener attached
811 * to it when it did so?
813 * <p>The callback is a CustomEvent, so the signature is:</p>
814 * <p>type <string>, args <array>, customobject <object></p>
815 * <p>For DOMReady events, there are no fire argments, so the
817 * <p>"DOMReady", [], obj</p>
822 * @param {function} p_fn what to execute when the element is found.
823 * @param {object} p_obj an optional object to be passed back as
824 * a parameter to p_fn.
825 * @param {boolean|object} p_scope If set to true, p_fn will execute
826 * in the scope of p_obj, if set to an object it
827 * will execute in the scope of that object
831 onDOMReady: function(p_fn
, p_obj
, p_override
) {
833 setTimeout(function() {
836 if (p_override
=== true) {
842 p_fn
.call(s
, "DOMReady", [], p_obj
);
845 this.DOMReadyEvent
.subscribe(p_fn
, p_obj
, p_override
);
851 * Appends an event handler
853 * @method _addListener
855 * @param {String|HTMLElement|Array|NodeList} el An id, an element
856 * reference, or a collection of ids and/or elements to assign the
858 * @param {String} sType The type of event to append
859 * @param {Function} fn The method the event invokes
860 * @param {Object} obj An arbitrary object that will be
861 * passed as a parameter to the handler
862 * @param {Boolean|object} override If true, the obj passed in becomes
863 * the execution scope of the listener. If an
864 * object, this object becomes the execution
866 * @param {boolen} capture capture or bubble phase
867 * @return {Boolean} True if the action was successful or defered,
868 * false if one or more of the elements
869 * could not have the listener attached,
870 * or if the operation throws an exception.
874 _addListener: function(el
, sType
, fn
, obj
, override
, capture
) {
876 if (!fn
|| !fn
.call
) {
880 // The el argument can be an array of elements or element ids.
881 if ( this._isValidCollection(el
)) {
883 for (var i
=0,len
=el
.length
; i
<len
; ++i
) {
884 ok
= this._addListener(el
[i
],
893 } else if (YAHOO
.lang
.isString(el
)) {
894 var oEl
= this.getEl(el
);
895 // If the el argument is a string, we assume it is
896 // actually the id of the element. If the page is loaded
897 // we convert el to the actual element, otherwise we
898 // defer attaching the event until onload event fires
900 // check to see if we need to delay hooking up the event
901 // until after the page loads.
905 // defer adding the event until the element is available
906 this.onAvailable(el
, function() {
907 YAHOO
.util
.Event
._addListener(el
, sType
, fn
, obj
, override
, capture
);
914 // Element should be an html element or an array if we get
920 // we need to make sure we fire registered unload events
921 // prior to automatically unhooking them. So we hang on to
922 // these instead of attaching them to the window and fire the
923 // handles explicitly during our one unload event.
924 if ("unload" == sType
&& obj
!== this) {
925 unloadListeners
[unloadListeners
.length
] =
926 [el
, sType
, fn
, obj
, override
, capture
];
931 // if the user chooses to override the scope, we use the custom
932 // object passed in, otherwise the executing scope will be the
933 // HTML element that the event is registered on
936 if (override
=== true) {
943 // wrap the function so we can return the obj object when
945 var wrappedFn = function(e
) {
946 return fn
.call(scope
, YAHOO
.util
.Event
.getEvent(e
, el
),
950 var li
= [el
, sType
, fn
, wrappedFn
, scope
, obj
, override
, capture
];
951 var index
= listeners
.length
;
952 // cache the listener so we can try to automatically unload
953 listeners
[index
] = li
;
955 if (this.useLegacyEvent(el
, sType
)) {
956 var legacyIndex
= this.getLegacyIndex(el
, sType
);
958 // Add a new dom0 wrapper if one is not detected for this
960 if ( legacyIndex
== -1 ||
961 el
!= legacyEvents
[legacyIndex
][0] ) {
963 legacyIndex
= legacyEvents
.length
;
964 legacyMap
[el
.id
+ sType
] = legacyIndex
;
966 // cache the signature for the DOM0 event, and
967 // include the existing handler for the event, if any
968 legacyEvents
[legacyIndex
] =
969 [el
, sType
, el
["on" + sType
]];
970 legacyHandlers
[legacyIndex
] = [];
974 YAHOO
.util
.Event
.fireLegacyEvent(
975 YAHOO
.util
.Event
.getEvent(e
), legacyIndex
);
979 // add a reference to the wrapped listener to our custom
981 //legacyHandlers[legacyIndex].push(index);
982 legacyHandlers
[legacyIndex
].push(li
);
986 this._simpleAdd(el
, sType
, wrappedFn
, capture
);
988 // handle an error trying to attach an event. If it fails
989 // we need to clean up the cache
991 this._removeListener(el
, sType
, fn
, capture
);
1002 * Appends an event handler
1004 * @method addListener
1006 * @param {String|HTMLElement|Array|NodeList} el An id, an element
1007 * reference, or a collection of ids and/or elements to assign the
1009 * @param {String} sType The type of event to append
1010 * @param {Function} fn The method the event invokes
1011 * @param {Object} obj An arbitrary object that will be
1012 * passed as a parameter to the handler
1013 * @param {Boolean|object} override If true, the obj passed in becomes
1014 * the execution scope of the listener. If an
1015 * object, this object becomes the execution
1017 * @return {Boolean} True if the action was successful or defered,
1018 * false if one or more of the elements
1019 * could not have the listener attached,
1020 * or if the operation throws an exception.
1023 addListener: function (el
, sType
, fn
, obj
, override
) {
1024 return this._addListener(el
, sType
, fn
, obj
, override
, false);
1028 * Appends a focus event handler. (The focusin event is used for Internet Explorer,
1029 * the focus, capture-event for Opera, WebKit, and Gecko.)
1031 * @method addFocusListener
1033 * @param {String|HTMLElement|Array|NodeList} el An id, an element
1034 * reference, or a collection of ids and/or elements to assign the
1036 * @param {Function} fn The method the event invokes
1037 * @param {Object} obj An arbitrary object that will be
1038 * passed as a parameter to the handler
1039 * @param {Boolean|object} override If true, the obj passed in becomes
1040 * the execution scope of the listener. If an
1041 * object, this object becomes the execution
1043 * @return {Boolean} True if the action was successful or defered,
1044 * false if one or more of the elements
1045 * could not have the listener attached,
1046 * or if the operation throws an exception.
1049 addFocusListener: function (el
, fn
, obj
, override
) {
1050 return this._addListener(el
, _FOCUS
, fn
, obj
, override
, true);
1055 * Removes a focus event listener
1057 * @method removeFocusListener
1059 * @param {String|HTMLElement|Array|NodeList} el An id, an element
1060 * reference, or a collection of ids and/or elements to remove
1061 * the listener from.
1062 * @param {Function} fn the method the event invokes. If fn is
1063 * undefined, then all event handlers for the type of event are
1065 * @return {boolean} true if the unbind was successful, false
1069 removeFocusListener: function (el
, fn
) {
1070 return this._removeListener(el
, _FOCUS
, fn
, true);
1074 * Appends a blur event handler. (The focusout event is used for Internet Explorer,
1075 * the focusout, capture-event for Opera, WebKit, and Gecko.)
1077 * @method addBlurListener
1079 * @param {String|HTMLElement|Array|NodeList} el An id, an element
1080 * reference, or a collection of ids and/or elements to assign the
1082 * @param {Function} fn The method the event invokes
1083 * @param {Object} obj An arbitrary object that will be
1084 * passed as a parameter to the handler
1085 * @param {Boolean|object} override If true, the obj passed in becomes
1086 * the execution scope of the listener. If an
1087 * object, this object becomes the execution
1089 * @return {Boolean} True if the action was successful or defered,
1090 * false if one or more of the elements
1091 * could not have the listener attached,
1092 * or if the operation throws an exception.
1095 addBlurListener: function (el
, fn
, obj
, override
) {
1096 return this._addListener(el
, _BLUR
, fn
, obj
, override
, true);
1100 * Removes a blur event listener
1102 * @method removeBlurListener
1104 * @param {String|HTMLElement|Array|NodeList} el An id, an element
1105 * reference, or a collection of ids and/or elements to remove
1106 * the listener from.
1107 * @param {Function} fn the method the event invokes. If fn is
1108 * undefined, then all event handlers for the type of event are
1110 * @return {boolean} true if the unbind was successful, false
1114 removeBlurListener: function (el
, fn
) {
1116 return this._removeListener(el
, _BLUR
, fn
, true);
1121 * When using legacy events, the handler is routed to this object
1122 * so we can fire our custom listener stack.
1123 * @method fireLegacyEvent
1127 fireLegacyEvent: function(e
, legacyIndex
) {
1128 var ok
=true, le
, lh
, li
, scope
, ret
;
1130 lh
= legacyHandlers
[legacyIndex
].slice();
1131 for (var i
=0, len
=lh
.length
; i
<len
; ++i
) {
1132 // for (var i in lh.length) {
1134 if ( li
&& li
[this.WFN
] ) {
1135 scope
= li
[this.ADJ_SCOPE
];
1136 ret
= li
[this.WFN
].call(scope
, e
);
1141 // Fire the original handler if we replaced one. We fire this
1142 // after the other events to keep stopPropagation/preventDefault
1143 // that happened in the DOM0 handler from touching our DOM2
1145 le
= legacyEvents
[legacyIndex
];
1154 * Returns the legacy event index that matches the supplied
1156 * @method getLegacyIndex
1160 getLegacyIndex: function(el
, sType
) {
1161 var key
= this.generateId(el
) + sType
;
1162 if (typeof legacyMap
[key
] == "undefined") {
1165 return legacyMap
[key
];
1170 * Logic that determines when we should automatically use legacy
1171 * events instead of DOM2 events. Currently this is limited to old
1172 * Safari browsers with a broken preventDefault
1173 * @method useLegacyEvent
1177 useLegacyEvent: function(el
, sType
) {
1178 return (this.webkit
&& this.webkit
< 419 && ("click"==sType
|| "dblclick"==sType
));
1182 * Removes an event listener
1184 * @method _removeListener
1186 * @param {String|HTMLElement|Array|NodeList} el An id, an element
1187 * reference, or a collection of ids and/or elements to remove
1188 * the listener from.
1189 * @param {String} sType the type of event to remove.
1190 * @param {Function} fn the method the event invokes. If fn is
1191 * undefined, then all event handlers for the type of event are
1193 * @param {boolen} capture capture or bubble phase
1194 * @return {boolean} true if the unbind was successful, false
1199 _removeListener: function(el
, sType
, fn
, capture
) {
1202 // The el argument can be a string
1203 if (typeof el
== "string") {
1204 el
= this.getEl(el
);
1205 // The el argument can be an array of elements or element ids.
1206 } else if ( this._isValidCollection(el
)) {
1208 for (i
=el
.length
-1; i
>-1; i
--) {
1209 ok
= ( this._removeListener(el
[i
], sType
, fn
, capture
) && ok
);
1214 if (!fn
|| !fn
.call
) {
1216 return this.purgeElement(el
, false, sType
);
1219 if ("unload" == sType
) {
1221 for (i
=unloadListeners
.length
-1; i
>-1; i
--) {
1222 li
= unloadListeners
[i
];
1227 unloadListeners
.splice(i
, 1);
1228 // unloadListeners[i]=null;
1236 var cacheItem
= null;
1238 // The index is a hidden parameter; needed to remove it from
1239 // the method signature because it was tempting users to
1240 // try and take advantage of it, which is not possible.
1241 var index
= arguments
[4];
1243 if ("undefined" === typeof index
) {
1244 index
= this._getCacheIndex(el
, sType
, fn
);
1248 cacheItem
= listeners
[index
];
1251 if (!el
|| !cacheItem
) {
1256 if (this.useLegacyEvent(el
, sType
)) {
1257 var legacyIndex
= this.getLegacyIndex(el
, sType
);
1258 var llist
= legacyHandlers
[legacyIndex
];
1260 for (i
=0, len
=llist
.length
; i
<len
; ++i
) {
1261 // for (i in llist.length) {
1264 li
[this.EL
] == el
&&
1265 li
[this.TYPE
] == sType
&&
1266 li
[this.FN
] == fn
) {
1276 this._simpleRemove(el
, sType
, cacheItem
[this.WFN
], capture
);
1278 this.lastError
= ex
;
1283 // removed the wrapped handler
1284 delete listeners
[index
][this.WFN
];
1285 delete listeners
[index
][this.FN
];
1286 listeners
.splice(index
, 1);
1287 // listeners[index]=null;
1295 * Removes an event listener
1297 * @method removeListener
1299 * @param {String|HTMLElement|Array|NodeList} el An id, an element
1300 * reference, or a collection of ids and/or elements to remove
1301 * the listener from.
1302 * @param {String} sType the type of event to remove.
1303 * @param {Function} fn the method the event invokes. If fn is
1304 * undefined, then all event handlers for the type of event are
1306 * @return {boolean} true if the unbind was successful, false
1310 removeListener: function(el
, sType
, fn
) {
1312 return this._removeListener(el
, sType
, fn
, false);
1318 * Returns the event's target element. Safari sometimes provides
1319 * a text node, and this is automatically resolved to the text
1320 * node's parent so that it behaves like other browsers.
1322 * @param {Event} ev the event
1323 * @param {boolean} resolveTextNode when set to true the target's
1324 * parent will be returned if the target is a
1325 * text node. @deprecated, the text node is
1326 * now resolved automatically
1327 * @return {HTMLElement} the event's target
1330 getTarget: function(ev
, resolveTextNode
) {
1331 var t
= ev
.target
|| ev
.srcElement
;
1332 return this.resolveTextNode(t
);
1336 * In some cases, some browsers will return a text node inside
1337 * the actual element that was targeted. This normalizes the
1338 * return value for getTarget and getRelatedTarget.
1339 * @method resolveTextNode
1340 * @param {HTMLElement} node node to resolve
1341 * @return {HTMLElement} the normized node
1344 resolveTextNode: function(n
) {
1346 if (n
&& 3 == n
.nodeType
) {
1347 return n
.parentNode
;
1355 * Returns the event's pageX
1357 * @param {Event} ev the event
1358 * @return {int} the event's pageX
1361 getPageX: function(ev
) {
1363 if (!x
&& 0 !== x
) {
1364 x
= ev
.clientX
|| 0;
1367 x
+= this._getScrollLeft();
1375 * Returns the event's pageY
1377 * @param {Event} ev the event
1378 * @return {int} the event's pageY
1381 getPageY: function(ev
) {
1383 if (!y
&& 0 !== y
) {
1384 y
= ev
.clientY
|| 0;
1387 y
+= this._getScrollTop();
1396 * Returns the pageX and pageY properties as an indexed array.
1398 * @param {Event} ev the event
1399 * @return {[x, y]} the pageX and pageY properties of the event
1402 getXY: function(ev
) {
1403 return [this.getPageX(ev
), this.getPageY(ev
)];
1407 * Returns the event's related target
1408 * @method getRelatedTarget
1409 * @param {Event} ev the event
1410 * @return {HTMLElement} the event's relatedTarget
1413 getRelatedTarget: function(ev
) {
1414 var t
= ev
.relatedTarget
;
1416 if (ev
.type
== "mouseout") {
1418 } else if (ev
.type
== "mouseover") {
1423 return this.resolveTextNode(t
);
1427 * Returns the time of the event. If the time is not included, the
1428 * event is modified using the current time.
1430 * @param {Event} ev the event
1431 * @return {Date} the time of the event
1434 getTime: function(ev
) {
1436 var t
= new Date().getTime();
1440 this.lastError
= ex
;
1449 * Convenience method for stopPropagation + preventDefault
1451 * @param {Event} ev the event
1454 stopEvent: function(ev
) {
1455 this.stopPropagation(ev
);
1456 this.preventDefault(ev
);
1460 * Stops event propagation
1461 * @method stopPropagation
1462 * @param {Event} ev the event
1465 stopPropagation: function(ev
) {
1466 if (ev
.stopPropagation
) {
1467 ev
.stopPropagation();
1469 ev
.cancelBubble
= true;
1474 * Prevents the default behavior of the event
1475 * @method preventDefault
1476 * @param {Event} ev the event
1479 preventDefault: function(ev
) {
1480 if (ev
.preventDefault
) {
1481 ev
.preventDefault();
1483 ev
.returnValue
= false;
1488 * Finds the event in the window object, the caller's arguments, or
1489 * in the arguments of another method in the callstack. This is
1490 * executed automatically for events registered through the event
1491 * manager, so the implementer should not normally need to execute
1492 * this function at all.
1494 * @param {Event} e the event parameter from the handler
1495 * @param {HTMLElement} boundEl the element the listener is attached to
1496 * @return {Event} the event
1499 getEvent: function(e
, boundEl
) {
1500 var ev
= e
|| window
.event
;
1503 var c
= this.getEvent
.caller
;
1505 ev
= c
.arguments
[0];
1506 if (ev
&& Event
== ev
.constructor) {
1517 * Returns the charcode for an event
1518 * @method getCharCode
1519 * @param {Event} ev the event
1520 * @return {int} the event's charCode
1523 getCharCode: function(ev
) {
1524 var code
= ev
.keyCode
|| ev
.charCode
|| 0;
1526 // webkit key normalization
1527 if (YAHOO
.env
.ua
.webkit
&& (code
in webkitKeymap
)) {
1528 code
= webkitKeymap
[code
];
1534 * Locating the saved event handler data by function ref
1536 * @method _getCacheIndex
1540 _getCacheIndex: function(el
, sType
, fn
) {
1541 for (var i
=0, l
=listeners
.length
; i
<l
; i
=i
+1) {
1542 var li
= listeners
[i
];
1544 li
[this.FN
] == fn
&&
1545 li
[this.EL
] == el
&&
1546 li
[this.TYPE
] == sType
) {
1555 * Generates an unique ID for the element if it does not already
1557 * @method generateId
1558 * @param el the element to create the id for
1559 * @return {string} the resulting id of the element
1562 generateId: function(el
) {
1566 id
= "yuievtautoid-" + counter
;
1576 * We want to be able to use getElementsByTagName as a collection
1577 * to attach a group of events to. Unfortunately, different
1578 * browsers return different types of collections. This function
1579 * tests to determine if the object is array-like. It will also
1580 * fail if the object is an array, but is empty.
1581 * @method _isValidCollection
1582 * @param o the object to test
1583 * @return {boolean} true if the object is array-like and populated
1587 _isValidCollection: function(o
) {
1589 return ( o
&& // o is something
1590 typeof o
!== "string" && // o is not a string
1591 o
.length
&& // o is indexed
1592 !o
.tagName
&& // o is not an HTML element
1593 !o
.alert
&& // o is not a window
1594 typeof o
[0] !== "undefined" );
1606 * @deprecated Elements are not cached due to issues that arise when
1607 * elements are removed and re-added
1612 * We cache elements bound by id because when the unload event
1613 * fires, we can no longer use document.getElementById
1617 * @deprecated Elements are not cached any longer
1619 getEl: function(id
) {
1620 return (typeof id
=== "string") ? document
.getElementById(id
) : id
;
1624 * Clears the element cache
1625 * @deprecated Elements are not cached any longer
1626 * @method clearCache
1630 clearCache: function() { },
1633 * Custom event the fires when the dom is initially usable
1634 * @event DOMReadyEvent
1636 DOMReadyEvent
: new YAHOO
.util
.CustomEvent("DOMReady", this),
1639 * hook up any deferred listeners
1644 _load: function(e
) {
1646 if (!loadComplete
) {
1647 loadComplete
= true;
1648 var EU
= YAHOO
.util
.Event
;
1650 // Just in case DOMReady did not go off for some reason
1653 // Available elements may not have been detected before the
1654 // window load event fires. Try to find them now so that the
1655 // the user is more likely to get the onAvailable notifications
1656 // before the window load notification
1657 EU
._tryPreloadAttach();
1663 * Fires the DOMReady event listeners the first time the document is
1669 _ready: function(e
) {
1670 var EU
= YAHOO
.util
.Event
;
1674 // Fire the content ready custom event
1675 EU
.DOMReadyEvent
.fire();
1677 // Remove the DOMContentLoaded (FF/Opera)
1678 EU
._simpleRemove(document
, "DOMContentLoaded", EU
._ready
);
1683 * Polling function that runs before the onload event fires,
1684 * attempting to attach to DOM Nodes as soon as they are
1686 * @method _tryPreloadAttach
1690 _tryPreloadAttach: function() {
1692 if (onAvailStack
.length
=== 0) {
1694 clearInterval(this._interval
);
1695 this._interval
= null;
1704 // Hold off if DOMReady has not fired and check current
1705 // readyState to protect against the IE operation aborted
1707 if (!this.DOMReady
) {
1708 this.startInterval();
1716 // keep trying until after the page is loaded. We need to
1717 // check the page load state prior to trying to bind the
1718 // elements so that we can be certain all elements have been
1719 // tested appropriately
1720 var tryAgain
= !loadComplete
;
1722 tryAgain
= (retryCount
> 0 && onAvailStack
.length
> 0);
1728 var executeItem = function (el
, item
) {
1730 if (item
.override
) {
1731 if (item
.override
=== true) {
1734 scope
= item
.override
;
1737 item
.fn
.call(scope
, item
.obj
);
1740 var i
, len
, item
, el
, ready
=[];
1742 // onAvailable onContentReady
1743 for (i
=0, len
=onAvailStack
.length
; i
<len
; i
=i
+1) {
1744 item
= onAvailStack
[i
];
1746 el
= this.getEl(item
.id
);
1748 if (item
.checkReady
) {
1749 if (loadComplete
|| el
.nextSibling
|| !tryAgain
) {
1751 onAvailStack
[i
] = null;
1754 executeItem(el
, item
);
1755 onAvailStack
[i
] = null;
1758 notAvail
.push(item
);
1763 // make sure onContentReady fires after onAvailable
1764 for (i
=0, len
=ready
.length
; i
<len
; i
=i
+1) {
1766 executeItem(this.getEl(item
.id
), item
);
1773 for (i
=onAvailStack
.length
-1; i
>-1; i
--) {
1774 item
= onAvailStack
[i
];
1775 if (!item
|| !item
.id
) {
1776 onAvailStack
.splice(i
, 1);
1780 this.startInterval();
1782 clearInterval(this._interval
);
1783 this._interval
= null;
1786 this.locked
= false;
1791 * Removes all listeners attached to the given element via addListener.
1792 * Optionally, the node's children can also be purged.
1793 * Optionally, you can specify a specific type of event to remove.
1794 * @method purgeElement
1795 * @param {HTMLElement} el the element to purge
1796 * @param {boolean} recurse recursively purge this element's children
1797 * as well. Use with caution.
1798 * @param {string} sType optional type of listener to purge. If
1799 * left out, all listeners will be removed
1802 purgeElement: function(el
, recurse
, sType
) {
1803 var oEl
= (YAHOO
.lang
.isString(el
)) ? this.getEl(el
) : el
;
1804 var elListeners
= this.getListeners(oEl
, sType
), i
, len
;
1806 for (i
=elListeners
.length
-1; i
>-1; i
--) {
1807 var l
= elListeners
[i
];
1808 this._removeListener(oEl
, l
.type
, l
.fn
, l
.capture
);
1812 if (recurse
&& oEl
&& oEl
.childNodes
) {
1813 for (i
=0,len
=oEl
.childNodes
.length
; i
<len
; ++i
) {
1814 this.purgeElement(oEl
.childNodes
[i
], recurse
, sType
);
1820 * Returns all listeners attached to the given element via addListener.
1821 * Optionally, you can specify a specific type of event to return.
1822 * @method getListeners
1823 * @param el {HTMLElement|string} the element or element id to inspect
1824 * @param sType {string} optional type of listener to return. If
1825 * left out, all listeners will be returned
1826 * @return {Object} the listener. Contains the following fields:
1827 * type: (string) the type of event
1828 * fn: (function) the callback supplied to addListener
1829 * obj: (object) the custom object supplied to addListener
1830 * adjust: (boolean|object) whether or not to adjust the default scope
1831 * scope: (boolean) the derived scope based on the adjust parameter
1832 * scope: (capture) the capture parameter supplied to addListener
1833 * index: (int) its position in the Event util listener cache
1836 getListeners: function(el
, sType
) {
1837 var results
=[], searchLists
;
1839 searchLists
= [listeners
, unloadListeners
];
1840 } else if (sType
=== "unload") {
1841 searchLists
= [unloadListeners
];
1843 searchLists
= [listeners
];
1846 var oEl
= (YAHOO
.lang
.isString(el
)) ? this.getEl(el
) : el
;
1848 for (var j
=0;j
<searchLists
.length
; j
=j
+1) {
1849 var searchList
= searchLists
[j
];
1851 for (var i
=0,len
=searchList
.length
; i
<len
; ++i
) {
1852 var l
= searchList
[i
];
1853 if ( l
&& l
[this.EL
] === oEl
&&
1854 (!sType
|| sType
=== l
[this.TYPE
]) ) {
1859 adjust
: l
[this.OVERRIDE
],
1860 scope
: l
[this.ADJ_SCOPE
],
1861 capture
: l
[this.CAPTURE
],
1869 return (results
.length
) ? results
: null;
1873 * Removes all listeners registered by pe.event. Called
1874 * automatically during the unload event.
1879 _unload: function(e
) {
1881 var EU
= YAHOO
.util
.Event
, i
, j
, l
, len
, index
,
1882 ul
= unloadListeners
.slice();
1884 // execute and clear stored unload listeners
1885 for (i
=0,len
=unloadListeners
.length
; i
<len
; ++i
) {
1889 if (l
[EU
.ADJ_SCOPE
]) {
1890 if (l
[EU
.ADJ_SCOPE
] === true) {
1891 scope
= l
[EU
.UNLOAD_OBJ
];
1893 scope
= l
[EU
.ADJ_SCOPE
];
1896 l
[EU
.FN
].call(scope
, EU
.getEvent(e
, l
[EU
.EL
]), l
[EU
.UNLOAD_OBJ
] );
1903 unloadListeners
= null;
1905 // Remove listeners to handle IE memory leaks
1906 //if (YAHOO.env.ua.ie && listeners && listeners.length > 0) {
1908 // 2.5.0 listeners are removed for all browsers again. FireFox preserves
1909 // at least some listeners between page refreshes, potentially causing
1910 // errors during page load (mouseover listeners firing before they
1911 // should if the user moves the mouse at the correct moment).
1913 for (j
=listeners
.length
-1; j
>-1; j
--) {
1916 EU
._removeListener(l
[EU
.EL
], l
[EU
.TYPE
], l
[EU
.FN
], l
[EU
.CAPTURE
], j
);
1922 legacyEvents
= null;
1924 EU
._simpleRemove(window
, "unload", EU
._unload
);
1929 * Returns scrollLeft
1930 * @method _getScrollLeft
1934 _getScrollLeft: function() {
1935 return this._getScroll()[1];
1940 * @method _getScrollTop
1944 _getScrollTop: function() {
1945 return this._getScroll()[0];
1949 * Returns the scrollTop and scrollLeft. Used to calculate the
1950 * pageX and pageY in Internet Explorer
1951 * @method _getScroll
1955 _getScroll: function() {
1956 var dd
= document
.documentElement
, db
= document
.body
;
1957 if (dd
&& (dd
.scrollTop
|| dd
.scrollLeft
)) {
1958 return [dd
.scrollTop
, dd
.scrollLeft
];
1960 return [db
.scrollTop
, db
.scrollLeft
];
1967 * Used by old versions of CustomEvent, restored for backwards
1972 * @deprecated still here for backwards compatibility
1979 * Adds a DOM event directly without the caching, cleanup, scope adj, etc
1981 * @method _simpleAdd
1982 * @param {HTMLElement} el the element to bind the handler to
1983 * @param {string} sType the type of event handler
1984 * @param {function} fn the callback to invoke
1985 * @param {boolen} capture capture or bubble phase
1989 _simpleAdd: function () {
1990 if (window
.addEventListener
) {
1991 return function(el
, sType
, fn
, capture
) {
1992 el
.addEventListener(sType
, fn
, (capture
));
1994 } else if (window
.attachEvent
) {
1995 return function(el
, sType
, fn
, capture
) {
1996 el
.attachEvent("on" + sType
, fn
);
1999 return function(){};
2004 * Basic remove listener
2006 * @method _simpleRemove
2007 * @param {HTMLElement} el the element to bind the handler to
2008 * @param {string} sType the type of event handler
2009 * @param {function} fn the callback to invoke
2010 * @param {boolen} capture capture or bubble phase
2014 _simpleRemove: function() {
2015 if (window
.removeEventListener
) {
2016 return function (el
, sType
, fn
, capture
) {
2017 el
.removeEventListener(sType
, fn
, (capture
));
2019 } else if (window
.detachEvent
) {
2020 return function (el
, sType
, fn
) {
2021 el
.detachEvent("on" + sType
, fn
);
2024 return function(){};
2032 var EU
= YAHOO
.util
.Event
;
2035 * YAHOO.util.Event.on is an alias for addListener
2040 EU
.on
= EU
.addListener
;
2043 * YAHOO.util.Event.onFocus is an alias for addFocusListener
2045 * @see addFocusListener
2048 EU
.onFocus
= EU
.addFocusListener
;
2051 * YAHOO.util.Event.onBlur is an alias for addBlurListener
2053 * @see addBlurListener
2056 EU
.onBlur
= EU
.addBlurListener
;
2059 /*! DOMReady: based on work by: Dean Edwards/John Resig/Matthias Miller */
2061 // Internet Explorer: use the readyState of a defered script.
2062 // This isolates what appears to be a safe moment to manipulate
2063 // the DOM prior to when the document's readyState suggests
2064 // it is safe to do so.
2067 // Process onAvailable/onContentReady items when the
2069 YAHOO
.util
.Event
.onDOMReady(
2070 YAHOO
.util
.Event
._tryPreloadAttach
,
2071 YAHOO
.util
.Event
, true);
2073 var n
= document
.createElement('p');
2075 EU
._dri
= setInterval(function() {
2077 // throws an error if doc is not ready
2079 clearInterval(EU
._dri
);
2085 }, EU
.POLL_INTERVAL
);
2088 // The document's readyState in Safari currently will
2089 // change to loaded/complete before images are loaded.
2090 } else if (EU
.webkit
&& EU
.webkit
< 525) {
2092 EU
._dri
= setInterval(function() {
2093 var rs
=document
.readyState
;
2094 if ("loaded" == rs
|| "complete" == rs
) {
2095 clearInterval(EU
._dri
);
2099 }, EU
.POLL_INTERVAL
);
2101 // FireFox and Opera: These browsers provide a event for this
2102 // moment. The latest WebKit releases now support this event.
2105 EU
._simpleAdd(document
, "DOMContentLoaded", EU
._ready
);
2108 /////////////////////////////////////////////////////////////
2111 EU
._simpleAdd(window
, "load", EU
._load
);
2112 EU
._simpleAdd(window
, "unload", EU
._unload
);
2113 EU
._tryPreloadAttach();
2118 * EventProvider is designed to be used with YAHOO.augment to wrap
2119 * CustomEvents in an interface that allows events to be subscribed to
2120 * and fired by name. This makes it possible for implementing code to
2121 * subscribe to an event that either has not been created yet, or will
2122 * not be created at all.
2124 * @Class EventProvider
2126 YAHOO
.util
.EventProvider = function() { };
2128 YAHOO
.util
.EventProvider
.prototype = {
2131 * Private storage of custom events
2132 * @property __yui_events
2139 * Private storage of custom event subscribers
2140 * @property __yui_subscribers
2144 __yui_subscribers
: null,
2147 * Subscribe to a CustomEvent by event type
2150 * @param p_type {string} the type, or name of the event
2151 * @param p_fn {function} the function to exectute when the event fires
2152 * @param p_obj {Object} An object to be passed along when the event
2154 * @param p_override {boolean} If true, the obj passed in becomes the
2155 * execution scope of the listener
2157 subscribe: function(p_type
, p_fn
, p_obj
, p_override
) {
2159 this.__yui_events
= this.__yui_events
|| {};
2160 var ce
= this.__yui_events
[p_type
];
2163 ce
.subscribe(p_fn
, p_obj
, p_override
);
2165 this.__yui_subscribers
= this.__yui_subscribers
|| {};
2166 var subs
= this.__yui_subscribers
;
2167 if (!subs
[p_type
]) {
2171 { fn
: p_fn
, obj
: p_obj
, override
: p_override
} );
2176 * Unsubscribes one or more listeners the from the specified event
2177 * @method unsubscribe
2178 * @param p_type {string} The type, or name of the event. If the type
2179 * is not specified, it will attempt to remove
2180 * the listener from all hosted events.
2181 * @param p_fn {Function} The subscribed function to unsubscribe, if not
2182 * supplied, all subscribers will be removed.
2183 * @param p_obj {Object} The custom object passed to subscribe. This is
2184 * optional, but if supplied will be used to
2185 * disambiguate multiple listeners that are the same
2186 * (e.g., you subscribe many object using a function
2187 * that lives on the prototype)
2188 * @return {boolean} true if the subscriber was found and detached.
2190 unsubscribe: function(p_type
, p_fn
, p_obj
) {
2191 this.__yui_events
= this.__yui_events
|| {};
2192 var evts
= this.__yui_events
;
2194 var ce
= evts
[p_type
];
2196 return ce
.unsubscribe(p_fn
, p_obj
);
2200 for (var i
in evts
) {
2201 if (YAHOO
.lang
.hasOwnProperty(evts
, i
)) {
2202 ret
= ret
&& evts
[i
].unsubscribe(p_fn
, p_obj
);
2212 * Removes all listeners from the specified event. If the event type
2213 * is not specified, all listeners from all hosted custom events will
2215 * @method unsubscribeAll
2216 * @param p_type {string} The type, or name of the event
2218 unsubscribeAll: function(p_type
) {
2219 return this.unsubscribe(p_type
);
2223 * Creates a new custom event of the specified type. If a custom event
2224 * by that name already exists, it will not be re-created. In either
2225 * case the custom event is returned.
2227 * @method createEvent
2229 * @param p_type {string} the type, or name of the event
2230 * @param p_config {object} optional config params. Valid properties are:
2234 * scope: defines the default execution scope. If not defined
2235 * the default scope will be this instance.
2238 * silent: if true, the custom event will not generate log messages.
2239 * This is false by default.
2242 * onSubscribeCallback: specifies a callback to execute when the
2243 * event has a new subscriber. This will fire immediately for
2244 * each queued subscriber if any exist prior to the creation of
2249 * @return {CustomEvent} the custom event
2252 createEvent: function(p_type
, p_config
) {
2254 this.__yui_events
= this.__yui_events
|| {};
2255 var opts
= p_config
|| {};
2256 var events
= this.__yui_events
;
2258 if (events
[p_type
]) {
2261 var scope
= opts
.scope
|| this;
2262 var silent
= (opts
.silent
);
2264 var ce
= new YAHOO
.util
.CustomEvent(p_type
, scope
, silent
,
2265 YAHOO
.util
.CustomEvent
.FLAT
);
2266 events
[p_type
] = ce
;
2268 if (opts
.onSubscribeCallback
) {
2269 ce
.subscribeEvent
.subscribe(opts
.onSubscribeCallback
);
2272 this.__yui_subscribers
= this.__yui_subscribers
|| {};
2273 var qs
= this.__yui_subscribers
[p_type
];
2276 for (var i
=0; i
<qs
.length
; ++i
) {
2277 ce
.subscribe(qs
[i
].fn
, qs
[i
].obj
, qs
[i
].override
);
2282 return events
[p_type
];
2287 * Fire a custom event by name. The callback functions will be executed
2288 * from the scope specified when the event was created, and with the
2289 * following parameters:
2291 * <li>The first argument fire() was executed with</li>
2292 * <li>The custom object (if any) that was passed into the subscribe()
2296 * @param p_type {string} the type, or name of the event
2297 * @param arguments {Object*} an arbitrary set of parameters to pass to
2299 * @return {boolean} the return value from CustomEvent.fire
2302 fireEvent: function(p_type
, arg1
, arg2
, etc
) {
2304 this.__yui_events
= this.__yui_events
|| {};
2305 var ce
= this.__yui_events
[p_type
];
2312 for (var i
=1; i
<arguments
.length
; ++i
) {
2313 args
.push(arguments
[i
]);
2315 return ce
.fire
.apply(ce
, args
);
2319 * Returns true if the custom event of the provided type has been created
2322 * @param type {string} the type, or name of the event
2324 hasEvent: function(type
) {
2325 if (this.__yui_events
) {
2326 if (this.__yui_events
[type
]) {
2336 //@TODO use event utility, lang abstractions
2340 * KeyListener is a utility that provides an easy interface for listening for
2341 * keydown/keyup events fired against DOM elements.
2342 * @namespace YAHOO.util
2343 * @class KeyListener
2345 * @param {HTMLElement} attachTo The element or element ID to which the key
2346 * event should be attached
2347 * @param {String} attachTo The element or element ID to which the key
2348 * event should be attached
2349 * @param {Object} keyData The object literal representing the key(s)
2350 * to detect. Possible attributes are
2351 * shift(boolean), alt(boolean), ctrl(boolean)
2352 * and keys(either an int or an array of ints
2353 * representing keycodes).
2354 * @param {Function} handler The CustomEvent handler to fire when the
2355 * key event is detected
2356 * @param {Object} handler An object literal representing the handler.
2357 * @param {String} event Optional. The event (keydown or keyup) to
2358 * listen for. Defaults automatically to keydown.
2360 * @knownissue the "keypress" event is completely broken in Safari 2.x and below.
2361 * the workaround is use "keydown" for key listening. However, if
2362 * it is desired to prevent the default behavior of the keystroke,
2363 * that can only be done on the keypress event. This makes key
2364 * handling quite ugly.
2365 * @knownissue keydown is also broken in Safari 2.x and below for the ESC key.
2366 * There currently is no workaround other than choosing another
2367 * key to listen for.
2369 YAHOO
.util
.KeyListener = function(attachTo
, keyData
, handler
, event
) {
2371 } else if (!keyData
) {
2372 } else if (!handler
) {
2376 event
= YAHOO
.util
.KeyListener
.KEYDOWN
;
2380 * The CustomEvent fired internally when a key is pressed
2383 * @param {Object} keyData The object literal representing the key(s) to
2384 * detect. Possible attributes are shift(boolean),
2385 * alt(boolean), ctrl(boolean) and keys(either an
2386 * int or an array of ints representing keycodes).
2388 var keyEvent
= new YAHOO
.util
.CustomEvent("keyPressed");
2391 * The CustomEvent fired when the KeyListener is enabled via the enable()
2393 * @event enabledEvent
2394 * @param {Object} keyData The object literal representing the key(s) to
2395 * detect. Possible attributes are shift(boolean),
2396 * alt(boolean), ctrl(boolean) and keys(either an
2397 * int or an array of ints representing keycodes).
2399 this.enabledEvent
= new YAHOO
.util
.CustomEvent("enabled");
2402 * The CustomEvent fired when the KeyListener is disabled via the
2403 * disable() function
2404 * @event disabledEvent
2405 * @param {Object} keyData The object literal representing the key(s) to
2406 * detect. Possible attributes are shift(boolean),
2407 * alt(boolean), ctrl(boolean) and keys(either an
2408 * int or an array of ints representing keycodes).
2410 this.disabledEvent
= new YAHOO
.util
.CustomEvent("disabled");
2412 if (typeof attachTo
== 'string') {
2413 attachTo
= document
.getElementById(attachTo
);
2416 if (typeof handler
== 'function') {
2417 keyEvent
.subscribe(handler
);
2419 keyEvent
.subscribe(handler
.fn
, handler
.scope
, handler
.correctScope
);
2423 * Handles the key event when a key is pressed.
2424 * @method handleKeyPress
2425 * @param {DOMEvent} e The keypress DOM event
2426 * @param {Object} obj The DOM event scope object
2429 function handleKeyPress(e
, obj
) {
2430 if (! keyData
.shift
) {
2431 keyData
.shift
= false;
2433 if (! keyData
.alt
) {
2434 keyData
.alt
= false;
2436 if (! keyData
.ctrl
) {
2437 keyData
.ctrl
= false;
2440 // check held down modifying keys first
2441 if (e
.shiftKey
== keyData
.shift
&&
2442 e
.altKey
== keyData
.alt
&&
2443 e
.ctrlKey
== keyData
.ctrl
) { // if we pass this, all modifiers match
2447 if (keyData
.keys
instanceof Array
) {
2448 for (var i
=0;i
<keyData
.keys
.length
;i
++) {
2449 dataItem
= keyData
.keys
[i
];
2451 if (dataItem
== e
.charCode
) {
2452 keyEvent
.fire(e
.charCode
, e
);
2454 } else if (dataItem
== e
.keyCode
) {
2455 keyEvent
.fire(e
.keyCode
, e
);
2460 dataItem
= keyData
.keys
;
2461 if (dataItem
== e
.charCode
) {
2462 keyEvent
.fire(e
.charCode
, e
);
2463 } else if (dataItem
== e
.keyCode
) {
2464 keyEvent
.fire(e
.keyCode
, e
);
2471 * Enables the KeyListener by attaching the DOM event listeners to the
2472 * target DOM element
2475 this.enable = function() {
2476 if (! this.enabled
) {
2477 YAHOO
.util
.Event
.addListener(attachTo
, event
, handleKeyPress
);
2478 this.enabledEvent
.fire(keyData
);
2481 * Boolean indicating the enabled/disabled state of the Tooltip
2485 this.enabled
= true;
2489 * Disables the KeyListener by removing the DOM event listeners from the
2490 * target DOM element
2493 this.disable = function() {
2495 YAHOO
.util
.Event
.removeListener(attachTo
, event
, handleKeyPress
);
2496 this.disabledEvent
.fire(keyData
);
2498 this.enabled
= false;
2502 * Returns a String representation of the object.
2504 * @return {String} The string representation of the KeyListener
2506 this.toString = function() {
2507 return "KeyListener [" + keyData
.keys
+ "] " + attachTo
.tagName
+
2508 (attachTo
.id
? "[" + attachTo
.id
+ "]" : "");
2514 * Constant representing the DOM "keydown" event.
2515 * @property YAHOO.util.KeyListener.KEYDOWN
2520 YAHOO
.util
.KeyListener
.KEYDOWN
= "keydown";
2523 * Constant representing the DOM "keyup" event.
2524 * @property YAHOO.util.KeyListener.KEYUP
2529 YAHOO
.util
.KeyListener
.KEYUP
= "keyup";
2532 * keycode constants for a subset of the special keys
2537 YAHOO
.util
.KeyListener
.KEY
= {
2562 YAHOO
.register("event", YAHOO
.util
.Event
, {version
: "2.6.0", build
: "1321"});