The callback is executed with a single parameter:
* the custom object parameter, if provided.
*
* @method onAvailable
*
* @param {string||string[]} id the id of the element, or an array
* of ids to look for.
* @param {function} fn what to execute when the element is found.
* @param {object} obj an optional object to be passed back as
* a parameter to fn.
* @param {boolean|object} overrideContext If set to true, fn will execute
* in the context of obj, if set to an object it
* will execute in the context of that object
* @param checkContent {boolean} check child node readiness (onContentReady)
* @static
*/
onAvailable: function(id, fn, obj, overrideContext, checkContent) {
var a = (YAHOO.lang.isString(id)) ? [id] : id;
for (var i=0; iThe callback is executed with a single parameter:
* the custom object parameter, if provided.
*
* @method onContentReady
*
* @param {string} id the id of the element to look for.
* @param {function} fn what to execute when the element is ready.
* @param {object} obj an optional object to be passed back as
* a parameter to fn.
* @param {boolean|object} overrideContext If set to true, fn will execute
* in the context of obj. If an object, fn will
* exectute in the context of that object
*
* @static
*/
onContentReady: function(id, fn, obj, overrideContext) {
this.onAvailable(id, fn, obj, overrideContext, true);
},
/**
* Executes the supplied callback when the DOM is first usable. This
* will execute immediately if called after the DOMReady event has
* fired. @todo the DOMContentReady event does not fire when the
* script is dynamically injected into the page. This means the
* DOMReady custom event will never fire in FireFox or Opera when the
* library is injected. It _will_ fire in Safari, and the IE
* implementation would allow for us to fire it if the defered script
* is not available. We want this to behave the same in all browsers.
* Is there a way to identify when the script has been injected
* instead of included inline? Is there a way to know whether the
* window onload event has fired without having had a listener attached
* to it when it did so?
*
* The callback is a CustomEvent, so the signature is:
* type <string>, args <array>, customobject <object>
* For DOMReady events, there are no fire argments, so the
* signature is:
* "DOMReady", [], obj
*
*
* @method onDOMReady
*
* @param {function} fn what to execute when the element is found.
* @param {object} obj an optional object to be passed back as
* a parameter to fn.
* @param {boolean|object} overrideContext If set to true, fn will execute
* in the context of obj, if set to an object it
* will execute in the context of that object
*
* @static
*/
// onDOMReady: function(fn, obj, overrideContext) {
onDOMReady: function() {
this.DOMReadyEvent.subscribe.apply(this.DOMReadyEvent, arguments);
},
/**
* Appends an event handler
*
* @method _addListener
*
* @param {String|HTMLElement|Array|NodeList} el An id, an element
* reference, or a collection of ids and/or elements to assign the
* listener to.
* @param {String} sType The type of event to append
* @param {Function} fn The method the event invokes
* @param {Object} obj An arbitrary object that will be
* passed as a parameter to the handler
* @param {Boolean|object} overrideContext If true, the obj passed in becomes
* the execution context of the listener. If an
* object, this object becomes the execution
* context.
* @param {boolen} capture capture or bubble phase
* @return {Boolean} True if the action was successful or defered,
* false if one or more of the elements
* could not have the listener attached,
* or if the operation throws an exception.
* @private
* @static
*/
_addListener: function(el, sType, fn, obj, overrideContext, bCapture) {
if (!fn || !fn.call) {
YAHOO.log(sType + " addListener failed, invalid callback", "error", "Event");
return false;
}
// The el argument can be an array of elements or element ids.
if ( this._isValidCollection(el)) {
var ok = true;
for (var i=0,len=el.length; i-1; i--) {
ok = ( this.removeListener(el[i], sType, fn) && ok );
}
return ok;
}
if (!fn || !fn.call) {
// this.logger.debug("Error, function is not valid " + fn);
//return false;
return this.purgeElement(el, false, sType);
}
if ("unload" == sType) {
for (i=unloadListeners.length-1; i>-1; i--) {
li = unloadListeners[i];
if (li &&
li[0] == el &&
li[1] == sType &&
li[2] == fn) {
unloadListeners.splice(i, 1);
// unloadListeners[i]=null;
return true;
}
}
return false;
}
var cacheItem = null;
// The index is a hidden parameter; needed to remove it from
// the method signature because it was tempting users to
// try and take advantage of it, which is not possible.
var index = arguments[3];
if ("undefined" === typeof index) {
index = this._getCacheIndex(listeners, el, sType, fn);
}
if (index >= 0) {
cacheItem = listeners[index];
}
if (!el || !cacheItem) {
// this.logger.debug("cached listener not found");
return false;
}
// this.logger.debug("Removing handler: " + el + ", " + sType);
var bCapture = cacheItem[this.CAPTURE] === true ? true : false;
try {
this._simpleRemove(el, sType, cacheItem[this.WFN], bCapture);
} catch(ex) {
this.lastError = ex;
return false;
}
// removed the wrapped handler
delete listeners[index][this.WFN];
delete listeners[index][this.FN];
listeners.splice(index, 1);
// listeners[index]=null;
return true;
},
/**
* Returns the event's target element. Safari sometimes provides
* a text node, and this is automatically resolved to the text
* node's parent so that it behaves like other browsers.
* @method getTarget
* @param {Event} ev the event
* @param {boolean} resolveTextNode when set to true the target's
* parent will be returned if the target is a
* text node. @deprecated, the text node is
* now resolved automatically
* @return {HTMLElement} the event's target
* @static
*/
getTarget: function(ev, resolveTextNode) {
var t = ev.target || ev.srcElement;
return this.resolveTextNode(t);
},
/**
* In some cases, some browsers will return a text node inside
* the actual element that was targeted. This normalizes the
* return value for getTarget and getRelatedTarget.
*
* If accessing a property of the node throws an error, this is
* probably the anonymous div wrapper Gecko adds inside text
* nodes. This likely will only occur when attempting to access
* the relatedTarget. In this case, we now return null because
* the anonymous div is completely useless and we do not know
* what the related target was because we can't even get to
* the element's parent node.
*
* @method resolveTextNode
* @param {HTMLElement} node node to resolve
* @return {HTMLElement} the normized node
* @static
*/
resolveTextNode: function(n) {
try {
if (n && 3 == n.nodeType) {
return n.parentNode;
}
} catch(e) {
return null;
}
return n;
},
/**
* Returns the event's pageX
* @method getPageX
* @param {Event} ev the event
* @return {int} the event's pageX
* @static
*/
getPageX: function(ev) {
var x = ev.pageX;
if (!x && 0 !== x) {
x = ev.clientX || 0;
if ( this.isIE ) {
x += this._getScrollLeft();
}
}
return x;
},
/**
* Returns the event's pageY
* @method getPageY
* @param {Event} ev the event
* @return {int} the event's pageY
* @static
*/
getPageY: function(ev) {
var y = ev.pageY;
if (!y && 0 !== y) {
y = ev.clientY || 0;
if ( this.isIE ) {
y += this._getScrollTop();
}
}
return y;
},
/**
* Returns the pageX and pageY properties as an indexed array.
* @method getXY
* @param {Event} ev the event
* @return {[x, y]} the pageX and pageY properties of the event
* @static
*/
getXY: function(ev) {
return [this.getPageX(ev), this.getPageY(ev)];
},
/**
* Returns the event's related target
* @method getRelatedTarget
* @param {Event} ev the event
* @return {HTMLElement} the event's relatedTarget
* @static
*/
getRelatedTarget: function(ev) {
var t = ev.relatedTarget;
if (!t) {
if (ev.type == "mouseout") {
t = ev.toElement;
} else if (ev.type == "mouseover") {
t = ev.fromElement;
}
}
return this.resolveTextNode(t);
},
/**
* Returns the time of the event. If the time is not included, the
* event is modified using the current time.
* @method getTime
* @param {Event} ev the event
* @return {Date} the time of the event
* @static
*/
getTime: function(ev) {
if (!ev.time) {
var t = new Date().getTime();
try {
ev.time = t;
} catch(ex) {
this.lastError = ex;
return t;
}
}
return ev.time;
},
/**
* Convenience method for stopPropagation + preventDefault
* @method stopEvent
* @param {Event} ev the event
* @static
*/
stopEvent: function(ev) {
this.stopPropagation(ev);
this.preventDefault(ev);
},
/**
* Stops event propagation
* @method stopPropagation
* @param {Event} ev the event
* @static
*/
stopPropagation: function(ev) {
if (ev.stopPropagation) {
ev.stopPropagation();
} else {
ev.cancelBubble = true;
}
},
/**
* Prevents the default behavior of the event
* @method preventDefault
* @param {Event} ev the event
* @static
*/
preventDefault: function(ev) {
if (ev.preventDefault) {
ev.preventDefault();
} else {
ev.returnValue = false;
}
},
/**
* Finds the event in the window object, the caller's arguments, or
* in the arguments of another method in the callstack. This is
* executed automatically for events registered through the event
* manager, so the implementer should not normally need to execute
* this function at all.
* @method getEvent
* @param {Event} e the event parameter from the handler
* @param {HTMLElement} boundEl the element the listener is attached to
* @return {Event} the event
* @static
*/
getEvent: function(e, boundEl) {
var ev = e || window.event;
if (!ev) {
var c = this.getEvent.caller;
while (c) {
ev = c.arguments[0];
if (ev && Event == ev.constructor) {
break;
}
c = c.caller;
}
}
return ev;
},
/**
* Returns the charcode for an event
* @method getCharCode
* @param {Event} ev the event
* @return {int} the event's charCode
* @static
*/
getCharCode: function(ev) {
var code = ev.keyCode || ev.charCode || 0;
// webkit key normalization
if (YAHOO.env.ua.webkit && (code in webkitKeymap)) {
code = webkitKeymap[code];
}
return code;
},
/**
* Locating the saved event handler data by function ref
*
* @method _getCacheIndex
* @static
* @private
*/
_getCacheIndex: function(a, el, sType, fn) {
for (var i=0, l=a.length; i 0 && onAvailStack.length > 0);
}
// onAvailable
var notAvail = [];
var executeItem = function (el, item) {
var context = el;
if (item.overrideContext) {
if (item.overrideContext === true) {
context = item.obj;
} else {
context = item.overrideContext;
}
}
item.fn.call(context, item.obj);
};
var i, len, item, el, ready=[];
// onAvailable onContentReady
for (i=0, len=onAvailStack.length; i-1; i--) {
item = onAvailStack[i];
if (!item || !item.id) {
onAvailStack.splice(i, 1);
}
}
this.startInterval();
} else {
if (this._interval) {
// clearInterval(this._interval);
this._interval.cancel();
this._interval = null;
}
}
this.locked = false;
},
/**
* Removes all listeners attached to the given element via addListener.
* Optionally, the node's children can also be purged.
* Optionally, you can specify a specific type of event to remove.
* @method purgeElement
* @param {HTMLElement} el the element to purge
* @param {boolean} recurse recursively purge this element's children
* as well. Use with caution.
* @param {string} sType optional type of listener to purge. If
* left out, all listeners will be removed
* @static
*/
purgeElement: function(el, recurse, sType) {
var oEl = (YAHOO.lang.isString(el)) ? this.getEl(el) : el;
var elListeners = this.getListeners(oEl, sType), i, len;
if (elListeners) {
for (i=elListeners.length-1; i>-1; i--) {
var l = elListeners[i];
this.removeListener(oEl, l.type, l.fn);
}
}
if (recurse && oEl && oEl.childNodes) {
for (i=0,len=oEl.childNodes.length; i-1; j--) {
l = listeners[j];
if (l) {
try {
EU.removeListener(l[EU.EL], l[EU.TYPE], l[EU.FN], j);
} catch(e2) {}
}
}
l=null;
}
try {
EU._simpleRemove(window, "unload", EU._unload);
EU._simpleRemove(window, "load", EU._load);
} catch(e3) {}
},
/**
* Returns scrollLeft
* @method _getScrollLeft
* @static
* @private
*/
_getScrollLeft: function() {
return this._getScroll()[1];
},
/**
* Returns scrollTop
* @method _getScrollTop
* @static
* @private
*/
_getScrollTop: function() {
return this._getScroll()[0];
},
/**
* Returns the scrollTop and scrollLeft. Used to calculate the
* pageX and pageY in Internet Explorer
* @method _getScroll
* @static
* @private
*/
_getScroll: function() {
var dd = document.documentElement, db = document.body;
if (dd && (dd.scrollTop || dd.scrollLeft)) {
return [dd.scrollTop, dd.scrollLeft];
} else if (db) {
return [db.scrollTop, db.scrollLeft];
} else {
return [0, 0];
}
},
/**
* Used by old versions of CustomEvent, restored for backwards
* compatibility
* @method regCE
* @private
* @static
* @deprecated still here for backwards compatibility
*/
regCE: function() {},
/**
* Adds a DOM event directly without the caching, cleanup, context adj, etc
*
* @method _simpleAdd
* @param {HTMLElement} el the element to bind the handler to
* @param {string} sType the type of event handler
* @param {function} fn the callback to invoke
* @param {boolen} capture capture or bubble phase
* @static
* @private
*/
_simpleAdd: function () {
if (window.addEventListener) {
return function(el, sType, fn, capture) {
el.addEventListener(sType, fn, (capture));
};
} else if (window.attachEvent) {
return function(el, sType, fn, capture) {
el.attachEvent("on" + sType, fn);
};
} else {
return function(){};
}
}(),
/**
* Basic remove listener
*
* @method _simpleRemove
* @param {HTMLElement} el the element to bind the handler to
* @param {string} sType the type of event handler
* @param {function} fn the callback to invoke
* @param {boolen} capture capture or bubble phase
* @static
* @private
*/
_simpleRemove: function() {
if (window.removeEventListener) {
return function (el, sType, fn, capture) {
el.removeEventListener(sType, fn, (capture));
};
} else if (window.detachEvent) {
return function (el, sType, fn) {
el.detachEvent("on" + sType, fn);
};
} else {
return function(){};
}
}()
};
}();
(function() {
var EU = YAHOO.util.Event;
/**
* Appends an event handler. This is an alias for addListener
*
* @method on
*
* @param {String|HTMLElement|Array|NodeList} el An id, an element
* reference, or a collection of ids and/or elements to assign the
* listener to.
* @param {String} sType The type of event to append
* @param {Function} fn The method the event invokes
* @param {Object} obj An arbitrary object that will be
* passed as a parameter to the handler
* @param {Boolean|object} overrideContext If true, the obj passed in becomes
* the execution context of the listener. If an
* object, this object becomes the execution
* context.
* @return {Boolean} True if the action was successful or defered,
* false if one or more of the elements
* could not have the listener attached,
* or if the operation throws an exception.
* @static
*/
EU.on = EU.addListener;
/**
* YAHOO.util.Event.onFocus is an alias for addFocusListener
* @method onFocus
* @see addFocusListener
* @static
* @deprecated use YAHOO.util.Event.on and specify "focusin" as the event type.
*/
EU.onFocus = EU.addFocusListener;
/**
* YAHOO.util.Event.onBlur is an alias for addBlurListener
* @method onBlur
* @see addBlurListener
* @static
* @deprecated use YAHOO.util.Event.on and specify "focusout" as the event type.
*/
EU.onBlur = EU.addBlurListener;
/*! DOMReady: based on work by: Dean Edwards/John Resig/Matthias Miller/Diego Perini */
// Internet Explorer: use the readyState of a defered script.
// This isolates what appears to be a safe moment to manipulate
// the DOM prior to when the document's readyState suggests
// it is safe to do so.
if (EU.isIE) {
if (self !== self.top) {
document.onreadystatechange = function() {
if (document.readyState == 'complete') {
document.onreadystatechange = null;
EU._ready();
}
};
} else {
// Process onAvailable/onContentReady items when the
// DOM is ready.
YAHOO.util.Event.onDOMReady(
YAHOO.util.Event._tryPreloadAttach,
YAHOO.util.Event, true);
var n = document.createElement('p');
EU._dri = setInterval(function() {
try {
// throws an error if doc is not ready
n.doScroll('left');
clearInterval(EU._dri);
EU._dri = null;
EU._ready();
n = null;
} catch (ex) {
}
}, EU.POLL_INTERVAL);
}
// The document's readyState in Safari currently will
// change to loaded/complete before images are loaded.
} else if (EU.webkit && EU.webkit < 525) {
EU._dri = setInterval(function() {
var rs=document.readyState;
if ("loaded" == rs || "complete" == rs) {
clearInterval(EU._dri);
EU._dri = null;
EU._ready();
}
}, EU.POLL_INTERVAL);
// FireFox and Opera: These browsers provide a event for this
// moment. The latest WebKit releases now support this event.
} else {
EU._simpleAdd(document, "DOMContentLoaded", EU._ready);
}
/////////////////////////////////////////////////////////////
EU._simpleAdd(window, "load", EU._load);
EU._simpleAdd(window, "unload", EU._unload);
EU._tryPreloadAttach();
})();
}
/**
* EventProvider is designed to be used with YAHOO.augment to wrap
* CustomEvents in an interface that allows events to be subscribed to
* and fired by name. This makes it possible for implementing code to
* subscribe to an event that either has not been created yet, or will
* not be created at all.
*
* @Class EventProvider
*/
YAHOO.util.EventProvider = function() { };
YAHOO.util.EventProvider.prototype = {
/**
* Private storage of custom events
* @property __yui_events
* @type Object[]
* @private
*/
__yui_events: null,
/**
* Private storage of custom event subscribers
* @property __yui_subscribers
* @type Object[]
* @private
*/
__yui_subscribers: null,
/**
* Subscribe to a CustomEvent by event type
*
* @method subscribe
* @param p_type {string} the type, or name of the event
* @param p_fn {function} the function to exectute when the event fires
* @param p_obj {Object} An object to be passed along when the event
* fires
* @param overrideContext {boolean} If true, the obj passed in becomes the
* execution scope of the listener
*/
subscribe: function(p_type, p_fn, p_obj, overrideContext) {
this.__yui_events = this.__yui_events || {};
var ce = this.__yui_events[p_type];
if (ce) {
ce.subscribe(p_fn, p_obj, overrideContext);
} else {
this.__yui_subscribers = this.__yui_subscribers || {};
var subs = this.__yui_subscribers;
if (!subs[p_type]) {
subs[p_type] = [];
}
subs[p_type].push(
{ fn: p_fn, obj: p_obj, overrideContext: overrideContext } );
}
},
/**
* Unsubscribes one or more listeners the from the specified event
* @method unsubscribe
* @param p_type {string} The type, or name of the event. If the type
* is not specified, it will attempt to remove
* the listener from all hosted events.
* @param p_fn {Function} The subscribed function to unsubscribe, if not
* supplied, all subscribers will be removed.
* @param p_obj {Object} The custom object passed to subscribe. This is
* optional, but if supplied will be used to
* disambiguate multiple listeners that are the same
* (e.g., you subscribe many object using a function
* that lives on the prototype)
* @return {boolean} true if the subscriber was found and detached.
*/
unsubscribe: function(p_type, p_fn, p_obj) {
this.__yui_events = this.__yui_events || {};
var evts = this.__yui_events;
if (p_type) {
var ce = evts[p_type];
if (ce) {
return ce.unsubscribe(p_fn, p_obj);
}
} else {
var ret = true;
for (var i in evts) {
if (YAHOO.lang.hasOwnProperty(evts, i)) {
ret = ret && evts[i].unsubscribe(p_fn, p_obj);
}
}
return ret;
}
return false;
},
/**
* Removes all listeners from the specified event. If the event type
* is not specified, all listeners from all hosted custom events will
* be removed.
* @method unsubscribeAll
* @param p_type {string} The type, or name of the event
*/
unsubscribeAll: function(p_type) {
return this.unsubscribe(p_type);
},
/**
* Creates a new custom event of the specified type. If a custom event
* by that name already exists, it will not be re-created. In either
* case the custom event is returned.
*
* @method createEvent
*
* @param p_type {string} the type, or name of the event
* @param p_config {object} optional config params. Valid properties are:
*
*
* -
* scope: defines the default execution scope. If not defined
* the default scope will be this instance.
*
* -
* silent: if true, the custom event will not generate log messages.
* This is false by default.
*
* -
* fireOnce: if true, the custom event will only notify subscribers
* once regardless of the number of times the event is fired. In
* addition, new subscribers will be executed immediately if the
* event has already fired.
* This is false by default.
*
* -
* onSubscribeCallback: specifies a callback to execute when the
* event has a new subscriber. This will fire immediately for
* each queued subscriber if any exist prior to the creation of
* the event.
*
*
*
* @return {CustomEvent} the custom event
*
*/
createEvent: function(p_type, p_config) {
this.__yui_events = this.__yui_events || {};
var opts = p_config || {},
events = this.__yui_events, ce;
if (events[p_type]) {
YAHOO.log("EventProvider createEvent skipped: '"+p_type+"' already exists");
} else {
ce = new YAHOO.util.CustomEvent(p_type, opts.scope || this, opts.silent,
YAHOO.util.CustomEvent.FLAT, opts.fireOnce);
events[p_type] = ce;
if (opts.onSubscribeCallback) {
ce.subscribeEvent.subscribe(opts.onSubscribeCallback);
}
this.__yui_subscribers = this.__yui_subscribers || {};
var qs = this.__yui_subscribers[p_type];
if (qs) {
for (var i=0; i
* The first argument fire() was executed with
* The custom object (if any) that was passed into the subscribe()
* method
*
* @method fireEvent
* @param p_type {string} the type, or name of the event
* @param arguments {Object*} an arbitrary set of parameters to pass to
* the handler.
* @return {boolean} the return value from CustomEvent.fire
*
*/
fireEvent: function(p_type) {
this.__yui_events = this.__yui_events || {};
var ce = this.__yui_events[p_type];
if (!ce) {
YAHOO.log(p_type + "event fired before it was created.");
return null;
}
var args = [];
for (var i=1; i