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)
538 * The number of times we should look for elements that are not
539 * in the DOM at the time the event is requested after the document
540 * has been loaded. The default is 2000@amp;20 ms, so it will poll
541 * for 40 seconds or until all outstanding handlers are bound
542 * (whichever comes first).
543 * @property POLL_RETRYS
551 * The poll interval in milliseconds
552 * @property POLL_INTERVAL
560 * Element to bind, int constant
569 * Type of event, int constant
578 * Function to execute, int constant
587 * Function wrapped for scope correction and cleanup, int constant
596 * Object passed in by the user that will be returned as a
597 * parameter to the callback, int constant. Specific to
607 * Adjusted scope, either the element we are registering the event
608 * on or the custom object passed in by the listener, int constant
609 * @property ADJ_SCOPE
617 * The original obj passed into addListener
626 * The original scope parameter passed into addListener
635 * addListener/removeListener can throw errors in unexpected scenarios.
636 * These errors are suppressed, the method returns false, and this property
638 * @property lastError
649 * @deprecated use YAHOO.env.ua.webkit
651 isSafari: YAHOO.env.ua.webkit,
659 * @deprecated use YAHOO.env.ua.webkit
661 webkit: YAHOO.env.ua.webkit,
668 * @deprecated use YAHOO.env.ua.ie
670 isIE: YAHOO.env.ua.ie,
674 * @property _interval
681 * document readystate poll handle
689 * True when the document is initially usable
697 * Errors thrown by subscribers of custom events are caught
698 * and the error message is written to the debug console. If
699 * this property is set to true, it will also re-throw the
701 * @property throwErrors
708 * @method startInterval
712 startInterval: function() {
713 if (!this._interval) {
715 var callback = function() { self._tryPreloadAttach(); };
716 this._interval = setInterval(callback, this.POLL_INTERVAL);
721 * Executes the supplied callback when the item with the supplied
722 * id is found. This is meant to be used to execute behavior as
723 * soon as possible as the page loads. If you use this after the
724 * initial page load it will poll for a fixed time for the element.
725 * The number of times it will poll and the frequency are
726 * configurable. By default it will poll for 10 seconds.
728 * <p>The callback is executed with a single parameter:
729 * the custom object parameter, if provided.</p>
731 * @method onAvailable
733 * @param {string||string[]} p_id the id of the element, or an array
734 * of ids to look for.
735 * @param {function} p_fn what to execute when the element is found.
736 * @param {object} p_obj an optional object to be passed back as
737 * a parameter to p_fn.
738 * @param {boolean|object} p_override If set to true, p_fn will execute
739 * in the scope of p_obj, if set to an object it
740 * will execute in the scope of that object
741 * @param checkContent {boolean} check child node readiness (onContentReady)
744 onAvailable: function(p_id, p_fn, p_obj, p_override, checkContent) {
746 var a = (YAHOO.lang.isString(p_id)) ? [p_id] : p_id;
748 for (var i=0; i<a.length; i=i+1) {
749 onAvailStack.push({id: a[i],
752 override: p_override,
753 checkReady: checkContent });
756 retryCount = this.POLL_RETRYS;
758 this.startInterval();
762 * Works the same way as onAvailable, but additionally checks the
763 * state of sibling elements to determine if the content of the
764 * available element is safe to modify.
766 * <p>The callback is executed with a single parameter:
767 * the custom object parameter, if provided.</p>
769 * @method onContentReady
771 * @param {string} p_id the id of the element to look for.
772 * @param {function} p_fn what to execute when the element is ready.
773 * @param {object} p_obj an optional object to be passed back as
774 * a parameter to p_fn.
775 * @param {boolean|object} p_override If set to true, p_fn will execute
776 * in the scope of p_obj. If an object, p_fn will
777 * exectute in the scope of that object
781 onContentReady: function(p_id, p_fn, p_obj, p_override) {
782 this.onAvailable(p_id, p_fn, p_obj, p_override, true);
786 * Executes the supplied callback when the DOM is first usable. This
787 * will execute immediately if called after the DOMReady event has
788 * fired. @todo the DOMContentReady event does not fire when the
789 * script is dynamically injected into the page. This means the
790 * DOMReady custom event will never fire in FireFox or Opera when the
791 * library is injected. It _will_ fire in Safari, and the IE
792 * implementation would allow for us to fire it if the defered script
793 * is not available. We want this to behave the same in all browsers.
794 * Is there a way to identify when the script has been injected
795 * instead of included inline? Is there a way to know whether the
796 * window onload event has fired without having had a listener attached
797 * to it when it did so?
799 * <p>The callback is a CustomEvent, so the signature is:</p>
800 * <p>type <string>, args <array>, customobject <object></p>
801 * <p>For DOMReady events, there are no fire argments, so the
803 * <p>"DOMReady", [], obj</p>
808 * @param {function} p_fn what to execute when the element is found.
809 * @param {object} p_obj an optional object to be passed back as
810 * a parameter to p_fn.
811 * @param {boolean|object} p_scope If set to true, p_fn will execute
812 * in the scope of p_obj, if set to an object it
813 * will execute in the scope of that object
817 onDOMReady: function(p_fn, p_obj, p_override) {
819 setTimeout(function() {
822 if (p_override === true) {
828 p_fn.call(s, "DOMReady", [], p_obj);
831 this.DOMReadyEvent.subscribe(p_fn, p_obj, p_override);
836 * Appends an event handler
838 * @method addListener
840 * @param {String|HTMLElement|Array|NodeList} el An id, an element
841 * reference, or a collection of ids and/or elements to assign the
843 * @param {String} sType The type of event to append
844 * @param {Function} fn The method the event invokes
845 * @param {Object} obj An arbitrary object that will be
846 * passed as a parameter to the handler
847 * @param {Boolean|object} override If true, the obj passed in becomes
848 * the execution scope of the listener. If an
849 * object, this object becomes the execution
851 * @return {Boolean} True if the action was successful or defered,
852 * false if one or more of the elements
853 * could not have the listener attached,
854 * or if the operation throws an exception.
857 addListener: function(el, sType, fn, obj, override) {
859 if (!fn || !fn.call) {
863 // The el argument can be an array of elements or element ids.
864 if ( this._isValidCollection(el)) {
866 for (var i=0,len=el.length; i<len; ++i) {
875 } else if (YAHOO.lang.isString(el)) {
876 var oEl = this.getEl(el);
877 // If the el argument is a string, we assume it is
878 // actually the id of the element. If the page is loaded
879 // we convert el to the actual element, otherwise we
880 // defer attaching the event until onload event fires
882 // check to see if we need to delay hooking up the event
883 // until after the page loads.
887 // defer adding the event until the element is available
888 this.onAvailable(el, function() {
889 YAHOO.util.Event.on(el, sType, fn, obj, override);
896 // Element should be an html element or an array if we get
902 // we need to make sure we fire registered unload events
903 // prior to automatically unhooking them. So we hang on to
904 // these instead of attaching them to the window and fire the
905 // handles explicitly during our one unload event.
906 if ("unload" == sType && obj !== this) {
907 unloadListeners[unloadListeners.length] =
908 [el, sType, fn, obj, override];
913 // if the user chooses to override the scope, we use the custom
914 // object passed in, otherwise the executing scope will be the
915 // HTML element that the event is registered on
918 if (override === true) {
925 // wrap the function so we can return the obj object when
927 var wrappedFn = function(e) {
928 return fn.call(scope, YAHOO.util.Event.getEvent(e, el),
932 var li = [el, sType, fn, wrappedFn, scope, obj, override];
933 var index = listeners.length;
934 // cache the listener so we can try to automatically unload
935 listeners[index] = li;
937 if (this.useLegacyEvent(el, sType)) {
938 var legacyIndex = this.getLegacyIndex(el, sType);
940 // Add a new dom0 wrapper if one is not detected for this
942 if ( legacyIndex == -1 ||
943 el != legacyEvents[legacyIndex][0] ) {
945 legacyIndex = legacyEvents.length;
946 legacyMap[el.id + sType] = legacyIndex;
948 // cache the signature for the DOM0 event, and
949 // include the existing handler for the event, if any
950 legacyEvents[legacyIndex] =
951 [el, sType, el["on" + sType]];
952 legacyHandlers[legacyIndex] = [];
956 YAHOO.util.Event.fireLegacyEvent(
957 YAHOO.util.Event.getEvent(e), legacyIndex);
961 // add a reference to the wrapped listener to our custom
963 //legacyHandlers[legacyIndex].push(index);
964 legacyHandlers[legacyIndex].push(li);
968 this._simpleAdd(el, sType, wrappedFn, false);
970 // handle an error trying to attach an event. If it fails
971 // we need to clean up the cache
973 this.removeListener(el, sType, fn);
983 * When using legacy events, the handler is routed to this object
984 * so we can fire our custom listener stack.
985 * @method fireLegacyEvent
989 fireLegacyEvent: function(e, legacyIndex) {
990 var ok=true, le, lh, li, scope, ret;
992 lh = legacyHandlers[legacyIndex].slice();
993 for (var i=0, len=lh.length; i<len; ++i) {
994 // for (var i in lh.length) {
996 if ( li && li[this.WFN] ) {
997 scope = li[this.ADJ_SCOPE];
998 ret = li[this.WFN].call(scope, e);
1003 // Fire the original handler if we replaced one. We fire this
1004 // after the other events to keep stopPropagation/preventDefault
1005 // that happened in the DOM0 handler from touching our DOM2
1007 le = legacyEvents[legacyIndex];
1016 * Returns the legacy event index that matches the supplied
1018 * @method getLegacyIndex
1022 getLegacyIndex: function(el, sType) {
1023 var key = this.generateId(el) + sType;
1024 if (typeof legacyMap[key] == "undefined") {
1027 return legacyMap[key];
1032 * Logic that determines when we should automatically use legacy
1033 * events instead of DOM2 events. Currently this is limited to old
1034 * Safari browsers with a broken preventDefault
1035 * @method useLegacyEvent
1039 useLegacyEvent: function(el, sType) {
1040 if (this.webkit && ("click"==sType || "dblclick"==sType)) {
1041 var v = parseInt(this.webkit, 10);
1042 if (!isNaN(v) && v<418) {
1050 * Removes an event listener
1052 * @method removeListener
1054 * @param {String|HTMLElement|Array|NodeList} el An id, an element
1055 * reference, or a collection of ids and/or elements to remove
1056 * the listener from.
1057 * @param {String} sType the type of event to remove.
1058 * @param {Function} fn the method the event invokes. If fn is
1059 * undefined, then all event handlers for the type of event are
1061 * @return {boolean} true if the unbind was successful, false
1065 removeListener: function(el, sType, fn) {
1068 // The el argument can be a string
1069 if (typeof el == "string") {
1070 el = this.getEl(el);
1071 // The el argument can be an array of elements or element ids.
1072 } else if ( this._isValidCollection(el)) {
1074 for (i=el.length-1; i>-1; i--) {
1075 ok = ( this.removeListener(el[i], sType, fn) && ok );
1080 if (!fn || !fn.call) {
1082 return this.purgeElement(el, false, sType);
1085 if ("unload" == sType) {
1087 for (i=unloadListeners.length-1; i>-1; i--) {
1088 li = unloadListeners[i];
1093 unloadListeners.splice(i, 1);
1094 // unloadListeners[i]=null;
1102 var cacheItem = null;
1104 // The index is a hidden parameter; needed to remove it from
1105 // the method signature because it was tempting users to
1106 // try and take advantage of it, which is not possible.
1107 var index = arguments[3];
1109 if ("undefined" === typeof index) {
1110 index = this._getCacheIndex(el, sType, fn);
1114 cacheItem = listeners[index];
1117 if (!el || !cacheItem) {
1122 if (this.useLegacyEvent(el, sType)) {
1123 var legacyIndex = this.getLegacyIndex(el, sType);
1124 var llist = legacyHandlers[legacyIndex];
1126 for (i=0, len=llist.length; i<len; ++i) {
1127 // for (i in llist.length) {
1130 li[this.EL] == el &&
1131 li[this.TYPE] == sType &&
1132 li[this.FN] == fn) {
1142 this._simpleRemove(el, sType, cacheItem[this.WFN], false);
1144 this.lastError = ex;
1149 // removed the wrapped handler
1150 delete listeners[index][this.WFN];
1151 delete listeners[index][this.FN];
1152 listeners.splice(index, 1);
1153 // listeners[index]=null;
1160 * Returns the event's target element. Safari sometimes provides
1161 * a text node, and this is automatically resolved to the text
1162 * node's parent so that it behaves like other browsers.
1164 * @param {Event} ev the event
1165 * @param {boolean} resolveTextNode when set to true the target's
1166 * parent will be returned if the target is a
1167 * text node. @deprecated, the text node is
1168 * now resolved automatically
1169 * @return {HTMLElement} the event's target
1172 getTarget: function(ev, resolveTextNode) {
1173 var t = ev.target || ev.srcElement;
1174 return this.resolveTextNode(t);
1178 * In some cases, some browsers will return a text node inside
1179 * the actual element that was targeted. This normalizes the
1180 * return value for getTarget and getRelatedTarget.
1181 * @method resolveTextNode
1182 * @param {HTMLElement} node node to resolve
1183 * @return {HTMLElement} the normized node
1186 resolveTextNode: function(n) {
1188 if (n && 3 == n.nodeType) {
1189 return n.parentNode;
1197 * Returns the event's pageX
1199 * @param {Event} ev the event
1200 * @return {int} the event's pageX
1203 getPageX: function(ev) {
1205 if (!x && 0 !== x) {
1206 x = ev.clientX || 0;
1209 x += this._getScrollLeft();
1217 * Returns the event's pageY
1219 * @param {Event} ev the event
1220 * @return {int} the event's pageY
1223 getPageY: function(ev) {
1225 if (!y && 0 !== y) {
1226 y = ev.clientY || 0;
1229 y += this._getScrollTop();
1238 * Returns the pageX and pageY properties as an indexed array.
1240 * @param {Event} ev the event
1241 * @return {[x, y]} the pageX and pageY properties of the event
1244 getXY: function(ev) {
1245 return [this.getPageX(ev), this.getPageY(ev)];
1249 * Returns the event's related target
1250 * @method getRelatedTarget
1251 * @param {Event} ev the event
1252 * @return {HTMLElement} the event's relatedTarget
1255 getRelatedTarget: function(ev) {
1256 var t = ev.relatedTarget;
1258 if (ev.type == "mouseout") {
1260 } else if (ev.type == "mouseover") {
1265 return this.resolveTextNode(t);
1269 * Returns the time of the event. If the time is not included, the
1270 * event is modified using the current time.
1272 * @param {Event} ev the event
1273 * @return {Date} the time of the event
1276 getTime: function(ev) {
1278 var t = new Date().getTime();
1282 this.lastError = ex;
1291 * Convenience method for stopPropagation + preventDefault
1293 * @param {Event} ev the event
1296 stopEvent: function(ev) {
1297 this.stopPropagation(ev);
1298 this.preventDefault(ev);
1302 * Stops event propagation
1303 * @method stopPropagation
1304 * @param {Event} ev the event
1307 stopPropagation: function(ev) {
1308 if (ev.stopPropagation) {
1309 ev.stopPropagation();
1311 ev.cancelBubble = true;
1316 * Prevents the default behavior of the event
1317 * @method preventDefault
1318 * @param {Event} ev the event
1321 preventDefault: function(ev) {
1322 if (ev.preventDefault) {
1323 ev.preventDefault();
1325 ev.returnValue = false;
1330 * Finds the event in the window object, the caller's arguments, or
1331 * in the arguments of another method in the callstack. This is
1332 * executed automatically for events registered through the event
1333 * manager, so the implementer should not normally need to execute
1334 * this function at all.
1336 * @param {Event} e the event parameter from the handler
1337 * @param {HTMLElement} boundEl the element the listener is attached to
1338 * @return {Event} the event
1341 getEvent: function(e, boundEl) {
1342 var ev = e || window.event;
1345 var c = this.getEvent.caller;
1347 ev = c.arguments[0];
1348 if (ev && Event == ev.constructor) {
1359 * Returns the charcode for an event
1360 * @method getCharCode
1361 * @param {Event} ev the event
1362 * @return {int} the event's charCode
1365 getCharCode: function(ev) {
1366 var code = ev.keyCode || ev.charCode || 0;
1368 // webkit key normalization
1369 if (YAHOO.env.ua.webkit && (code in webkitKeymap)) {
1370 code = webkitKeymap[code];
1376 * Locating the saved event handler data by function ref
1378 * @method _getCacheIndex
1382 _getCacheIndex: function(el, sType, fn) {
1383 for (var i=0, l=listeners.length; i<l; i=i+1) {
1384 var li = listeners[i];
1386 li[this.FN] == fn &&
1387 li[this.EL] == el &&
1388 li[this.TYPE] == sType ) {
1397 * Generates an unique ID for the element if it does not already
1399 * @method generateId
1400 * @param el the element to create the id for
1401 * @return {string} the resulting id of the element
1404 generateId: function(el) {
1408 id = "yuievtautoid-" + counter;
1418 * We want to be able to use getElementsByTagName as a collection
1419 * to attach a group of events to. Unfortunately, different
1420 * browsers return different types of collections. This function
1421 * tests to determine if the object is array-like. It will also
1422 * fail if the object is an array, but is empty.
1423 * @method _isValidCollection
1424 * @param o the object to test
1425 * @return {boolean} true if the object is array-like and populated
1429 _isValidCollection: function(o) {
1431 return ( o && // o is something
1432 typeof o !== "string" && // o is not a string
1433 o.length && // o is indexed
1434 !o.tagName && // o is not an HTML element
1435 !o.alert && // o is not a window
1436 typeof o[0] !== "undefined" );
1448 * @deprecated Elements are not cached due to issues that arise when
1449 * elements are removed and re-added
1454 * We cache elements bound by id because when the unload event
1455 * fires, we can no longer use document.getElementById
1459 * @deprecated Elements are not cached any longer
1461 getEl: function(id) {
1462 return (typeof id === "string") ? document.getElementById(id) : id;
1466 * Clears the element cache
1467 * @deprecated Elements are not cached any longer
1468 * @method clearCache
1472 clearCache: function() { },
1475 * Custom event the fires when the dom is initially usable
1476 * @event DOMReadyEvent
1478 DOMReadyEvent: new YAHOO.util.CustomEvent("DOMReady", this),
1481 * hook up any deferred listeners
1486 _load: function(e) {
1488 if (!loadComplete) {
1489 loadComplete = true;
1490 var EU = YAHOO.util.Event;
1492 // Just in case DOMReady did not go off for some reason
1495 // Available elements may not have been detected before the
1496 // window load event fires. Try to find them now so that the
1497 // the user is more likely to get the onAvailable notifications
1498 // before the window load notification
1499 EU._tryPreloadAttach();
1505 * Fires the DOMReady event listeners the first time the document is
1511 _ready: function(e) {
1512 var EU = YAHOO.util.Event;
1516 // Fire the content ready custom event
1517 EU.DOMReadyEvent.fire();
1519 // Remove the DOMContentLoaded (FF/Opera)
1520 EU._simpleRemove(document, "DOMContentLoaded", EU._ready);
1525 * Polling function that runs before the onload event fires,
1526 * attempting to attach to DOM Nodes as soon as they are
1528 * @method _tryPreloadAttach
1532 _tryPreloadAttach: function() {
1534 if (onAvailStack.length === 0) {
1536 clearInterval(this._interval);
1537 this._interval = null;
1546 // Hold off if DOMReady has not fired and check current
1547 // readyState to protect against the IE operation aborted
1549 if (!this.DOMReady) {
1550 this.startInterval();
1558 // keep trying until after the page is loaded. We need to
1559 // check the page load state prior to trying to bind the
1560 // elements so that we can be certain all elements have been
1561 // tested appropriately
1562 var tryAgain = !loadComplete;
1564 tryAgain = (retryCount > 0 && onAvailStack.length > 0);
1570 var executeItem = function (el, item) {
1572 if (item.override) {
1573 if (item.override === true) {
1576 scope = item.override;
1579 item.fn.call(scope, item.obj);
1582 var i, len, item, el, ready=[];
1584 // onAvailable onContentReady
1585 for (i=0, len=onAvailStack.length; i<len; i=i+1) {
1586 item = onAvailStack[i];
1588 el = this.getEl(item.id);
1590 if (item.checkReady) {
1591 if (loadComplete || el.nextSibling || !tryAgain) {
1593 onAvailStack[i] = null;
1596 executeItem(el, item);
1597 onAvailStack[i] = null;
1600 notAvail.push(item);
1605 // make sure onContentReady fires after onAvailable
1606 for (i=0, len=ready.length; i<len; i=i+1) {
1608 executeItem(this.getEl(item.id), item);
1615 for (i=onAvailStack.length-1; i>-1; i--) {
1616 item = onAvailStack[i];
1617 if (!item || !item.id) {
1618 onAvailStack.splice(i, 1);
1622 this.startInterval();
1624 clearInterval(this._interval);
1625 this._interval = null;
1628 this.locked = false;
1633 * Removes all listeners attached to the given element via addListener.
1634 * Optionally, the node's children can also be purged.
1635 * Optionally, you can specify a specific type of event to remove.
1636 * @method purgeElement
1637 * @param {HTMLElement} el the element to purge
1638 * @param {boolean} recurse recursively purge this element's children
1639 * as well. Use with caution.
1640 * @param {string} sType optional type of listener to purge. If
1641 * left out, all listeners will be removed
1644 purgeElement: function(el, recurse, sType) {
1645 var oEl = (YAHOO.lang.isString(el)) ? this.getEl(el) : el;
1646 var elListeners = this.getListeners(oEl, sType), i, len;
1648 for (i=elListeners.length-1; i>-1; i--) {
1649 var l = elListeners[i];
1650 this.removeListener(oEl, l.type, l.fn);
1654 if (recurse && oEl && oEl.childNodes) {
1655 for (i=0,len=oEl.childNodes.length; i<len ; ++i) {
1656 this.purgeElement(oEl.childNodes[i], recurse, sType);
1662 * Returns all listeners attached to the given element via addListener.
1663 * Optionally, you can specify a specific type of event to return.
1664 * @method getListeners
1665 * @param el {HTMLElement|string} the element or element id to inspect
1666 * @param sType {string} optional type of listener to return. If
1667 * left out, all listeners will be returned
1668 * @return {Object} the listener. Contains the following fields:
1669 * type: (string) the type of event
1670 * fn: (function) the callback supplied to addListener
1671 * obj: (object) the custom object supplied to addListener
1672 * adjust: (boolean|object) whether or not to adjust the default scope
1673 * scope: (boolean) the derived scope based on the adjust parameter
1674 * index: (int) its position in the Event util listener cache
1677 getListeners: function(el, sType) {
1678 var results=[], searchLists;
1680 searchLists = [listeners, unloadListeners];
1681 } else if (sType === "unload") {
1682 searchLists = [unloadListeners];
1684 searchLists = [listeners];
1687 var oEl = (YAHOO.lang.isString(el)) ? this.getEl(el) : el;
1689 for (var j=0;j<searchLists.length; j=j+1) {
1690 var searchList = searchLists[j];
1692 for (var i=0,len=searchList.length; i<len ; ++i) {
1693 var l = searchList[i];
1694 if ( l && l[this.EL] === oEl &&
1695 (!sType || sType === l[this.TYPE]) ) {
1700 adjust: l[this.OVERRIDE],
1701 scope: l[this.ADJ_SCOPE],
1709 return (results.length) ? results : null;
1713 * Removes all listeners registered by pe.event. Called
1714 * automatically during the unload event.
1719 _unload: function(e) {
1721 var EU = YAHOO.util.Event, i, j, l, len, index,
1722 ul = unloadListeners.slice();
1724 // execute and clear stored unload listeners
1725 for (i=0,len=unloadListeners.length; i<len; ++i) {
1729 if (l[EU.ADJ_SCOPE]) {
1730 if (l[EU.ADJ_SCOPE] === true) {
1731 scope = l[EU.UNLOAD_OBJ];
1733 scope = l[EU.ADJ_SCOPE];
1736 l[EU.FN].call(scope, EU.getEvent(e, l[EU.EL]), l[EU.UNLOAD_OBJ] );
1743 unloadListeners = null;
1745 // Remove listeners to handle IE memory leaks
1746 //if (YAHOO.env.ua.ie && listeners && listeners.length > 0) {
1748 // 2.5.0 listeners are removed for all browsers again. FireFox preserves
1749 // at least some listeners between page refreshes, potentially causing
1750 // errors during page load (mouseover listeners firing before they
1751 // should if the user moves the mouse at the correct moment).
1753 for (j=listeners.length-1; j>-1; j--) {
1756 EU.removeListener(l[EU.EL], l[EU.TYPE], l[EU.FN], j);
1762 legacyEvents = null;
1764 EU._simpleRemove(window, "unload", EU._unload);
1769 * Returns scrollLeft
1770 * @method _getScrollLeft
1774 _getScrollLeft: function() {
1775 return this._getScroll()[1];
1780 * @method _getScrollTop
1784 _getScrollTop: function() {
1785 return this._getScroll()[0];
1789 * Returns the scrollTop and scrollLeft. Used to calculate the
1790 * pageX and pageY in Internet Explorer
1791 * @method _getScroll
1795 _getScroll: function() {
1796 var dd = document.documentElement, db = document.body;
1797 if (dd && (dd.scrollTop || dd.scrollLeft)) {
1798 return [dd.scrollTop, dd.scrollLeft];
1800 return [db.scrollTop, db.scrollLeft];
1807 * Used by old versions of CustomEvent, restored for backwards
1812 * @deprecated still here for backwards compatibility
1819 * Adds a DOM event directly without the caching, cleanup, scope adj, etc
1821 * @method _simpleAdd
1822 * @param {HTMLElement} el the element to bind the handler to
1823 * @param {string} sType the type of event handler
1824 * @param {function} fn the callback to invoke
1825 * @param {boolen} capture capture or bubble phase
1829 _simpleAdd: function () {
1830 if (window.addEventListener) {
1831 return function(el, sType, fn, capture) {
1832 el.addEventListener(sType, fn, (capture));
1834 } else if (window.attachEvent) {
1835 return function(el, sType, fn, capture) {
1836 el.attachEvent("on" + sType, fn);
1839 return function(){};
1844 * Basic remove listener
1846 * @method _simpleRemove
1847 * @param {HTMLElement} el the element to bind the handler to
1848 * @param {string} sType the type of event handler
1849 * @param {function} fn the callback to invoke
1850 * @param {boolen} capture capture or bubble phase
1854 _simpleRemove: function() {
1855 if (window.removeEventListener) {
1856 return function (el, sType, fn, capture) {
1857 el.removeEventListener(sType, fn, (capture));
1859 } else if (window.detachEvent) {
1860 return function (el, sType, fn) {
1861 el.detachEvent("on" + sType, fn);
1864 return function(){};
1872 var EU = YAHOO.util.Event;
1875 * YAHOO.util.Event.on is an alias for addListener
1880 EU.on = EU.addListener;
1882 /*! DOMReady: based on work by: Dean Edwards/John Resig/Matthias Miller */
1884 // Internet Explorer: use the readyState of a defered script.
1885 // This isolates what appears to be a safe moment to manipulate
1886 // the DOM prior to when the document's readyState suggests
1887 // it is safe to do so.
1890 // Process onAvailable/onContentReady items when the
1892 YAHOO.util.Event.onDOMReady(
1893 YAHOO.util.Event._tryPreloadAttach,
1894 YAHOO.util.Event, true);
1896 var n = document.createElement('p');
1898 EU._dri = setInterval(function() {
1900 // throws an error if doc is not ready
1902 clearInterval(EU._dri);
1908 }, EU.POLL_INTERVAL);
1911 // The document's readyState in Safari currently will
1912 // change to loaded/complete before images are loaded.
1913 } else if (EU.webkit && EU.webkit < 525) {
1915 EU._dri = setInterval(function() {
1916 var rs=document.readyState;
1917 if ("loaded" == rs || "complete" == rs) {
1918 clearInterval(EU._dri);
1922 }, EU.POLL_INTERVAL);
1924 // FireFox and Opera: These browsers provide a event for this
1925 // moment. The latest WebKit releases now support this event.
1928 EU._simpleAdd(document, "DOMContentLoaded", EU._ready);
1931 /////////////////////////////////////////////////////////////
1934 EU._simpleAdd(window, "load", EU._load);
1935 EU._simpleAdd(window, "unload", EU._unload);
1936 EU._tryPreloadAttach();
1941 * EventProvider is designed to be used with YAHOO.augment to wrap
1942 * CustomEvents in an interface that allows events to be subscribed to
1943 * and fired by name. This makes it possible for implementing code to
1944 * subscribe to an event that either has not been created yet, or will
1945 * not be created at all.
1947 * @Class EventProvider
1949 YAHOO.util.EventProvider = function() { };
1951 YAHOO.util.EventProvider.prototype = {
1954 * Private storage of custom events
1955 * @property __yui_events
1962 * Private storage of custom event subscribers
1963 * @property __yui_subscribers
1967 __yui_subscribers: null,
1970 * Subscribe to a CustomEvent by event type
1973 * @param p_type {string} the type, or name of the event
1974 * @param p_fn {function} the function to exectute when the event fires
1975 * @param p_obj {Object} An object to be passed along when the event
1977 * @param p_override {boolean} If true, the obj passed in becomes the
1978 * execution scope of the listener
1980 subscribe: function(p_type, p_fn, p_obj, p_override) {
1982 this.__yui_events = this.__yui_events || {};
1983 var ce = this.__yui_events[p_type];
1986 ce.subscribe(p_fn, p_obj, p_override);
1988 this.__yui_subscribers = this.__yui_subscribers || {};
1989 var subs = this.__yui_subscribers;
1990 if (!subs[p_type]) {
1994 { fn: p_fn, obj: p_obj, override: p_override } );
1999 * Unsubscribes one or more listeners the from the specified event
2000 * @method unsubscribe
2001 * @param p_type {string} The type, or name of the event. If the type
2002 * is not specified, it will attempt to remove
2003 * the listener from all hosted events.
2004 * @param p_fn {Function} The subscribed function to unsubscribe, if not
2005 * supplied, all subscribers will be removed.
2006 * @param p_obj {Object} The custom object passed to subscribe. This is
2007 * optional, but if supplied will be used to
2008 * disambiguate multiple listeners that are the same
2009 * (e.g., you subscribe many object using a function
2010 * that lives on the prototype)
2011 * @return {boolean} true if the subscriber was found and detached.
2013 unsubscribe: function(p_type, p_fn, p_obj) {
2014 this.__yui_events = this.__yui_events || {};
2015 var evts = this.__yui_events;
2017 var ce = evts[p_type];
2019 return ce.unsubscribe(p_fn, p_obj);
2023 for (var i in evts) {
2024 if (YAHOO.lang.hasOwnProperty(evts, i)) {
2025 ret = ret && evts[i].unsubscribe(p_fn, p_obj);
2035 * Removes all listeners from the specified event. If the event type
2036 * is not specified, all listeners from all hosted custom events will
2038 * @method unsubscribeAll
2039 * @param p_type {string} The type, or name of the event
2041 unsubscribeAll: function(p_type) {
2042 return this.unsubscribe(p_type);
2046 * Creates a new custom event of the specified type. If a custom event
2047 * by that name already exists, it will not be re-created. In either
2048 * case the custom event is returned.
2050 * @method createEvent
2052 * @param p_type {string} the type, or name of the event
2053 * @param p_config {object} optional config params. Valid properties are:
2057 * scope: defines the default execution scope. If not defined
2058 * the default scope will be this instance.
2061 * silent: if true, the custom event will not generate log messages.
2062 * This is false by default.
2065 * onSubscribeCallback: specifies a callback to execute when the
2066 * event has a new subscriber. This will fire immediately for
2067 * each queued subscriber if any exist prior to the creation of
2072 * @return {CustomEvent} the custom event
2075 createEvent: function(p_type, p_config) {
2077 this.__yui_events = this.__yui_events || {};
2078 var opts = p_config || {};
2079 var events = this.__yui_events;
2081 if (events[p_type]) {
2084 var scope = opts.scope || this;
2085 var silent = (opts.silent);
2087 var ce = new YAHOO.util.CustomEvent(p_type, scope, silent,
2088 YAHOO.util.CustomEvent.FLAT);
2089 events[p_type] = ce;
2091 if (opts.onSubscribeCallback) {
2092 ce.subscribeEvent.subscribe(opts.onSubscribeCallback);
2095 this.__yui_subscribers = this.__yui_subscribers || {};
2096 var qs = this.__yui_subscribers[p_type];
2099 for (var i=0; i<qs.length; ++i) {
2100 ce.subscribe(qs[i].fn, qs[i].obj, qs[i].override);
2105 return events[p_type];
2110 * Fire a custom event by name. The callback functions will be executed
2111 * from the scope specified when the event was created, and with the
2112 * following parameters:
2114 * <li>The first argument fire() was executed with</li>
2115 * <li>The custom object (if any) that was passed into the subscribe()
2119 * @param p_type {string} the type, or name of the event
2120 * @param arguments {Object*} an arbitrary set of parameters to pass to
2122 * @return {boolean} the return value from CustomEvent.fire
2125 fireEvent: function(p_type, arg1, arg2, etc) {
2127 this.__yui_events = this.__yui_events || {};
2128 var ce = this.__yui_events[p_type];
2135 for (var i=1; i<arguments.length; ++i) {
2136 args.push(arguments[i]);
2138 return ce.fire.apply(ce, args);
2142 * Returns true if the custom event of the provided type has been created
2145 * @param type {string} the type, or name of the event
2147 hasEvent: function(type) {
2148 if (this.__yui_events) {
2149 if (this.__yui_events[type]) {
2159 * KeyListener is a utility that provides an easy interface for listening for
2160 * keydown/keyup events fired against DOM elements.
2161 * @namespace YAHOO.util
2162 * @class KeyListener
2164 * @param {HTMLElement} attachTo The element or element ID to which the key
2165 * event should be attached
2166 * @param {String} attachTo The element or element ID to which the key
2167 * event should be attached
2168 * @param {Object} keyData The object literal representing the key(s)
2169 * to detect. Possible attributes are
2170 * shift(boolean), alt(boolean), ctrl(boolean)
2171 * and keys(either an int or an array of ints
2172 * representing keycodes).
2173 * @param {Function} handler The CustomEvent handler to fire when the
2174 * key event is detected
2175 * @param {Object} handler An object literal representing the handler.
2176 * @param {String} event Optional. The event (keydown or keyup) to
2177 * listen for. Defaults automatically to keydown.
2179 * @knownissue the "keypress" event is completely broken in Safari 2.x and below.
2180 * the workaround is use "keydown" for key listening. However, if
2181 * it is desired to prevent the default behavior of the keystroke,
2182 * that can only be done on the keypress event. This makes key
2183 * handling quite ugly.
2184 * @knownissue keydown is also broken in Safari 2.x and below for the ESC key.
2185 * There currently is no workaround other than choosing another
2186 * key to listen for.
2188 YAHOO.util.KeyListener = function(attachTo, keyData, handler, event) {
2190 } else if (!keyData) {
2191 } else if (!handler) {
2195 event = YAHOO.util.KeyListener.KEYDOWN;
2199 * The CustomEvent fired internally when a key is pressed
2202 * @param {Object} keyData The object literal representing the key(s) to
2203 * detect. Possible attributes are shift(boolean),
2204 * alt(boolean), ctrl(boolean) and keys(either an
2205 * int or an array of ints representing keycodes).
2207 var keyEvent = new YAHOO.util.CustomEvent("keyPressed");
2210 * The CustomEvent fired when the KeyListener is enabled via the enable()
2212 * @event enabledEvent
2213 * @param {Object} keyData The object literal representing the key(s) to
2214 * detect. Possible attributes are shift(boolean),
2215 * alt(boolean), ctrl(boolean) and keys(either an
2216 * int or an array of ints representing keycodes).
2218 this.enabledEvent = new YAHOO.util.CustomEvent("enabled");
2221 * The CustomEvent fired when the KeyListener is disabled via the
2222 * disable() function
2223 * @event disabledEvent
2224 * @param {Object} keyData The object literal representing the key(s) to
2225 * detect. Possible attributes are shift(boolean),
2226 * alt(boolean), ctrl(boolean) and keys(either an
2227 * int or an array of ints representing keycodes).
2229 this.disabledEvent = new YAHOO.util.CustomEvent("disabled");
2231 if (typeof attachTo == 'string') {
2232 attachTo = document.getElementById(attachTo);
2235 if (typeof handler == 'function') {
2236 keyEvent.subscribe(handler);
2238 keyEvent.subscribe(handler.fn, handler.scope, handler.correctScope);
2242 * Handles the key event when a key is pressed.
2243 * @method handleKeyPress
2244 * @param {DOMEvent} e The keypress DOM event
2245 * @param {Object} obj The DOM event scope object
2248 function handleKeyPress(e, obj) {
2249 if (! keyData.shift) {
2250 keyData.shift = false;
2252 if (! keyData.alt) {
2253 keyData.alt = false;
2255 if (! keyData.ctrl) {
2256 keyData.ctrl = false;
2259 // check held down modifying keys first
2260 if (e.shiftKey == keyData.shift &&
2261 e.altKey == keyData.alt &&
2262 e.ctrlKey == keyData.ctrl) { // if we pass this, all modifiers match
2266 if (keyData.keys instanceof Array) {
2267 for (var i=0;i<keyData.keys.length;i++) {
2268 dataItem = keyData.keys[i];
2270 if (dataItem == e.charCode ) {
2271 keyEvent.fire(e.charCode, e);
2273 } else if (dataItem == e.keyCode) {
2274 keyEvent.fire(e.keyCode, e);
2279 dataItem = keyData.keys;
2280 if (dataItem == e.charCode ) {
2281 keyEvent.fire(e.charCode, e);
2282 } else if (dataItem == e.keyCode) {
2283 keyEvent.fire(e.keyCode, e);
2290 * Enables the KeyListener by attaching the DOM event listeners to the
2291 * target DOM element
2294 this.enable = function() {
2295 if (! this.enabled) {
2296 YAHOO.util.Event.addListener(attachTo, event, handleKeyPress);
2297 this.enabledEvent.fire(keyData);
2300 * Boolean indicating the enabled/disabled state of the Tooltip
2304 this.enabled = true;
2308 * Disables the KeyListener by removing the DOM event listeners from the
2309 * target DOM element
2312 this.disable = function() {
2314 YAHOO.util.Event.removeListener(attachTo, event, handleKeyPress);
2315 this.disabledEvent.fire(keyData);
2317 this.enabled = false;
2321 * Returns a String representation of the object.
2323 * @return {String} The string representation of the KeyListener
2325 this.toString = function() {
2326 return "KeyListener [" + keyData.keys + "] " + attachTo.tagName +
2327 (attachTo.id ? "[" + attachTo.id + "]" : "");
2333 * Constant representing the DOM "keydown" event.
2334 * @property YAHOO.util.KeyListener.KEYDOWN
2339 YAHOO.util.KeyListener.KEYDOWN = "keydown";
2342 * Constant representing the DOM "keyup" event.
2343 * @property YAHOO.util.KeyListener.KEYUP
2348 YAHOO.util.KeyListener.KEYUP = "keyup";
2351 * keycode constants for a subset of the special keys
2356 YAHOO.util.KeyListener.KEY = {
2381 YAHOO.register("event", YAHOO.util.Event, {version: "2.5.2", build: "1076"});