📄 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.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<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>
*
* @param {Array} an arbitrary set of parameters to pass to the handler
*/
fire: function() {
for (var i=0, len=this.subscribers.length; i<len; ++i) {
var s = this.subscribers[i];
if (s) {
var scope = (s.override) ? s.obj : this.scope;
s.fn.call(scope, this.type, arguments, s.obj);
}
}
},
/**
* Removes all listeners
*/
unsubscribeAll: function() {
for (var i=0, len=this.subscribers.length; i<len; ++i) {
this._delete(i);
}
},
/**
* @private
*/
_delete: function(index) {
var s = this.subscribers[index];
if (s) {
delete s.fn;
delete s.obj;
}
delete this.subscribers[index];
}
};
/////////////////////////////////////////////////////////////////////
/**
* @class 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
* @constructor
*/
YAHOO.util.Subscriber = function(fn, obj, bOverride) {
/**
* The callback that will be execute when the event fires
* @type function
*/
this.fn = fn;
/**
* An optional custom object that will passed to the callback when
* the event fires
* @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
* @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.
*
* @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);
};
/* Copyright (c) 2006 Yahoo! Inc. All rights reserved. */
// Only load this library once. If it is loaded a second time, existing
// events cannot be detached.
if (!YAHOO.util.Event) {
/**
* @class
* 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.
* @constructor
*/
YAHOO.util.Event = function() {
/**
* True after the onload event has fired
* @type boolean
* @private
*/
var loadComplete = false;
/**
* Cache of wrapped listeners
* @type array
* @private
*/
var listeners = [];
/**
* Listeners that will be attached during the onload event
* @type array
* @private
*/
var delayedListeners = [];
/**
* User-defined unload function that will be fired before all events
* are detached
* @type array
* @private
*/
var unloadListeners = [];
/**
* Cache of the custom events that have been defined. Used for
* automatic cleanup
* @type array
* @private
*/
var customEvents = [];
/**
* Cache of DOM0 event handlers to work around issues with DOM2 events
* in Safari
* @private
*/
var legacyEvents = [];
/**
* Listener stack for DOM0 events
* @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.
* @private
*/
var retryCount = 0;
/**
* onAvailable listeners
* @private
*/
var onAvailStack = [];
/**
* Lookup table for legacy events
* @private
*/
var legacyMap = [];
/**
* Counter for auto id generation
* @private
*/
var counter = 0;
return { // PREPROCESS
/**
* The number of times we should look for elements that are not
* in the DOM at the time the event is requested after the document
* has been loaded. The default is 200@50 ms, so it will poll
* for 10 seconds or until all outstanding handlers are bound
* (whichever comes first).
* @type int
*/
POLL_RETRYS: 200,
/**
* The poll interval in milliseconds
* @type int
*/
POLL_INTERVAL: 50,
/**
* Element to bind, int constant
* @type int
*/
EL: 0,
/**
* Type of event, int constant
* @type int
*/
TYPE: 1,
/**
* Function to execute, int constant
* @type int
*/
FN: 2,
/**
* Function wrapped for scope correction and cleanup, int constant
* @type int
*/
WFN: 3,
/**
* Object passed in by the user that will be returned as a
* parameter to the callback, int constant
* @type int
*/
SCOPE: 3,
/**
* Adjusted scope, either the element we are registering the event
* on or the custom object passed in by the listener, int constant
* @type int
*/
ADJ_SCOPE: 4,
/**
* Safari detection is necessary to work around the preventDefault
* bug that makes it so you can't cancel a href click from the
* handler. There is not a capabilities check we can use here.
* @private
*/
isSafari: (/Safari|Konqueror|KHTML/gi).test(navigator.userAgent),
/**
* IE detection needed to properly calculate pageX and pageY.
* capabilities checking didn't seem to work because another
* browser that does not provide the properties have the values
* calculated in a different manner than IE.
* @private
*/
isIE: (!this.isSafari && !navigator.userAgent.match(/opera/gi) &&
navigator.userAgent.match(/msie/gi)),
/**
* @private
*/
addDelayedListener: function(el, sType, fn, oScope, bOverride) {
delayedListeners[delayedListeners.length] =
[el, sType, fn, oScope, bOverride];
// If this happens after the inital page load, we need to
// reset the poll counter so that we continue to search for
// the element for a fixed period of time.
if (loadComplete) {
retryCount = this.POLL_RETRYS;
this.startTimeout(0);
// this._tryPreloadAttach();
}
},
/**
* @private
*/
startTimeout: function(interval) {
var i = (interval || interval === 0) ? interval : this.POLL_INTERVAL;
var self = this;
var callback = function() { self._tryPreloadAttach(); };
this.timeout = setTimeout(callback, i);
},
/**
* Executes the supplied callback when the item with the supplied
* id is found. This is meant to be used to execute behavior as
* soon as possible as the page loads. If you use this after the
* initial page load it will poll for a fixed time for the element.
* The number of times it will poll and the frequency are
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -