📄 event.js
字号:
/*
Copyright (c) 2006, Yahoo! Inc. All rights reserved.
Code licensed under the BSD License:
http://developer.yahoo.net/yui/license.txt
Version: 0.11.4
*/
/**
* 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.
* @param {boolean} silent pass true to prevent the event from writing to
* the log system
* @namespace YAHOO.util
* @class CustomEvent
* @constructor
*/
YAHOO.util.CustomEvent = function(type, oScope, silent) {
/**
* The type of event, returned to subscribers when the event fires
* @property type
* @type string
*/
this.type = type;
/**
* The scope the the event will fire from by default. Defaults to the window
* obj
* @property scope
* @type object
*/
this.scope = oScope || window;
/**
* By default all custom events are logged in the debug build, set silent
* to true to disable logging for this event.
* @property silent
* @type boolean
*/
this.silent = silent;
/**
* The subscribers to this event
* @property subscribers
* @type Subscriber[]
*/
this.subscribers = [];
if (!this.silent) {
}
// Only add subscribe events for events that are not generated by CustomEvent
//if (oScope && (oScope.constructor != this.constructor)) {
/*
* Custom events provide a custom event that fires whenever there is
* a new subscriber to the event. This provides an opportunity to
* handle the case where there is a non-repeating event that has
* already fired has a new subscriber.
*
* type CustomEvent
*/
//this.subscribeEvent =
//new YAHOO.util.CustomEvent("subscribe", this, true);
//}
};
YAHOO.util.CustomEvent.prototype = {
/**
* Subscribes the caller to this event
* @method subscribe
* @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) {
//if (this.subscribeEvent) {
//this.subscribeEvent.fire(fn, obj, bOverride);
//}
this.subscribers.push( new YAHOO.util.Subscriber(fn, obj, bOverride) );
},
/**
* Unsubscribes the caller from this event
* @method unsubscribe
* @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<len; ++i) {
var s = this.subscribers[i];
if (s && s.contains(fn, obj)) {
this._delete(i);
found = true;
}
}
return found;
},
/**
* Notifies the subscribers. The callback functions will be executed
* from the scope specified when the event was created, and with the following
* parameters:
* <pre>
* - 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
* </pre>
* @method fire
* @param {Array} an arbitrary set of parameters to pass to the handler
*/
fire: function() {
var len=this.subscribers.length;
if (!len && this.silent) {
return;
}
var args = [];
for (var i=0; i<arguments.length; ++i) {
args.push(arguments[i]);
}
if (!this.silent) {
}
for (i=0; i<len; ++i) {
var s = this.subscribers[i];
if (s) {
if (!this.silent) {
}
var scope = (s.override) ? s.obj : this.scope;
s.fn.call(scope, this.type, args, s.obj);
}
}
},
/**
* Removes all listeners
* @method unsubscribeAll
*/
unsubscribeAll: function() {
for (var i=0, len=this.subscribers.length; i<len; ++i) {
this._delete(len - 1 - i);
}
},
/**
* @method _delete
* @private
*/
_delete: function(index) {
var s = this.subscribers[index];
if (s) {
delete s.fn;
delete s.obj;
}
// delete this.subscribers[index];
this.subscribers.splice(index, 1);
},
/**
* @method toString
*/
toString: function() {
return "CustomEvent: " + "'" + this.type + "', " +
"scope: " + this.scope;
}
};
/////////////////////////////////////////////////////////////////////
/**
* Stores the subscriber information to be used when the event fires.
* @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
* @class Subscriber
* @constructor
*/
YAHOO.util.Subscriber = function(fn, obj, bOverride) {
/**
* The callback that will be execute when the event fires
* @property fn
* @type function
*/
this.fn = fn;
/**
* An optional custom object that will passed to the callback when
* the event fires
* @property obj
* @type object
*/
this.obj = obj || null;
/**
* The default execution scope for the event listener is defined when the
* event is created (usually the object which contains the event).
* By setting override to true, the execution scope becomes the custom
* object passed in by the subscriber
* @property override
* @type boolean
*/
this.override = (bOverride);
};
/**
* Returns true if the fn and obj match this objects properties.
* Used by the unsubscribe method to match the right subscriber.
*
* @method contains
* @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 supplied arguments match this
* subscriber's signature.
*/
YAHOO.util.Subscriber.prototype.contains = function(fn, obj) {
return (this.fn == fn && this.obj == obj);
};
/**
* @method toString
*/
YAHOO.util.Subscriber.prototype.toString = function() {
return "Subscriber { obj: " + (this.obj || "") +
", override: " + (this.override || "no") + " }";
};
// The first instance of Event will win if it is loaded more than once.
if (!YAHOO.util.Event) {
/**
* The event utility provides functions to add and remove event listeners,
* event cleansing. It also tries to automatically remove listeners it
* registers during the unload event.
* @namespace YAHOO.util
* @class Event
*/
YAHOO.util.Event = function() {
/**
* True after the onload event has fired
* @property loadComplete
* @type boolean
* @private
*/
var loadComplete = false;
/**
* Cache of wrapped listeners
* @property listeners
* @type array
* @private
*/
var listeners = [];
/**
* Listeners that will be attached during the onload event
* @property delayedListeners
* @type array
* @private
*/
var delayedListeners = [];
/**
* User-defined unload function that will be fired before all events
* are detached
* @property unloadListeners
* @type array
* @private
*/
var unloadListeners = [];
/**
* Cache of DOM0 event handlers to work around issues with DOM2 events
* in Safari
* @property legacyEvents
* @private
*/
var legacyEvents = [];
/**
* Listener stack for DOM0 events
* @property legacyHandlers
* @private
*/
var legacyHandlers = [];
/**
* The number of times to poll after window.onload. This number is
* increased if additional late-bound handlers are requested after
* the page load.
* @property retryCount
* @private
*/
var retryCount = 0;
/**
* onAvailable listeners
* @property onAvailStack
* @private
*/
var onAvailStack = [];
/**
* Lookup table for legacy events
* @property legacyMap
* @private
*/
var legacyMap = [];
/**
* Counter for auto id generation
* @property counter
* @private
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -