/* This file is part of the DITA Open Toolkit project. See the accompanying LICENSE file for applicable license. Copyright (c) 2006, Yahoo! Inc. All rights reserved. Code licensed under the BSD License: http://developer.yahoo.net/yui/license.txt version: 0.10.0 */ /** * The CustomEvent class lets you define events for your application * that can be subscribed to by one or more independent component. * * @param {String} type The type of event, which is passed to the callback * when the event fires * @param {Object} oScope The context the event will fire from. "this" will * refer to this object in the callback. Default value: * the window object. The listener can override this. * @constructor */ YAHOO.util.CustomEvent = function(type, oScope) { /** * The type of event, returned to subscribers when the event fires * @type string */ this.type = type; /** * The scope the the event will fire from by default. Defaults to the window * obj * @type object */ this.scope = oScope || window; /** * The subscribers to this event * @type Subscriber[] */ this.subscribers = []; // Register with the event utility for automatic cleanup. Made optional // so that CustomEvent can be used independently of pe.event if (YAHOO.util.Event) { YAHOO.util.Event.regCE(this); } }; YAHOO.util.CustomEvent.prototype = { /** * Subscribes the caller to this event * @param {Function} fn The function to execute * @param {Object} obj An object to be passed along when the event fires * @param {boolean} bOverride If true, the obj passed in becomes the execution * scope of the listener */ subscribe: function(fn, obj, bOverride) { this.subscribers.push( new YAHOO.util.Subscriber(fn, obj, bOverride) ); }, /** * Unsubscribes the caller from this event * @param {Function} fn The function to execute * @param {Object} obj An object to be passed along when the event fires * @return {boolean} True if the subscriber was found and detached. */ unsubscribe: function(fn, obj) { var found = false; for (var i=0, len=this.subscribers.length; i * - The type of event * - All of the arguments fire() was executed with as an array * - The custom object (if any) that was passed into the subscribe() method * * * @param {Array} an arbitrary set of parameters to pass to the handler */ fire: function() { for (var i=0, len=this.subscribers.length; i= 0) { cacheItem = listeners[index]; } if (!el || !cacheItem) { return false; } if (el.removeEventListener) { el.removeEventListener(sType, cacheItem[this.WFN], false); } else if (el.detachEvent) { el.detachEvent("on" + sType, cacheItem[this.WFN]); } // removed the wrapped handler delete listeners[index][this.WFN]; delete listeners[index][this.FN]; delete listeners[index]; return true; }, /** * Returns the event's target element * @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 * @return {HTMLElement} the event's target */ getTarget: function(ev, resolveTextNode) { var t = ev.target || ev.srcElement; if (resolveTextNode && t && "#text" == t.nodeName) { return t.parentNode; } else { return t; } }, /** * Returns the event's pageX * @param {Event} ev the event * @return {int} the event's pageX */ 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 * @param {Event} ev the event * @return {int} the event's pageY */ 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. * @type int[] */ getXY: function(ev) { return [this.getPageX(ev), this.getPageY(ev)]; }, /** * Returns the event's related target * @param {Event} ev the event * @return {HTMLElement} the event's relatedTarget */ 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 t; }, /** * Returns the time of the event. If the time is not included, the * event is modified using the current time. * @param {Event} ev the event * @return {Date} the time of the event */ getTime: function(ev) { if (!ev.time) { var t = new Date().getTime(); try { ev.time = t; } catch(e) { // can't set the time property return t; } } return ev.time; }, /** * Convenience method for stopPropagation + preventDefault * @param {Event} ev the event */ stopEvent: function(ev) { this.stopPropagation(ev); this.preventDefault(ev); }, /** * Stops event propagation * @param {Event} ev the event */ stopPropagation: function(ev) { if (ev.stopPropagation) { ev.stopPropagation(); } else { ev.cancelBubble = true; } }, /** * Prevents the default behavior of the event * @param {Event} ev the event */ 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. * @param {Event} the event parameter from the handler * @return {Event} the event */ getEvent: function(e) { 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 * @param {Event} ev the event * @return {int} the event's charCode */ getCharCode: function(ev) { return ev.charCode || ((ev.type == "keypress") ? ev.keyCode : 0); }, /** * @private * Locating the saved event handler data by function ref */ _getCacheIndex: function(el, sType, fn) { for (var i=0,len=listeners.length; i 0); } // Delayed listeners var stillDelayed = []; for (var i=0,len=delayedListeners.length; i 0) { for (i=0,len=listeners.length; i