📄 event.js
字号:
return true;
},
/**
* 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 elListeners = this.getListeners(el, sType);
if (elListeners) {
for (var i=0,len=elListeners.length; i<len ; ++i) {
var l = elListeners[i];
// can't use the index on the changing collection
//this.removeListener(el, l.type, l.fn, l.index);
this.removeListener(el, l.type, l.fn);
}
}
if (recurse && el && el.childNodes) {
for (i=0,len=el.childNodes.length; i<len ; ++i) {
this.purgeElement(el.childNodes[i], recurse, sType);
}
}
},
/**
* Returns all listeners attached to the given element via addListener.
* Optionally, you can specify a specific type of event to return.
* @method getListeners
* @param el {HTMLElement} the element to inspect
* @param sType {string} optional type of listener to return. If
* left out, all listeners will be returned
* @return {Object} the listener. Contains the following fields:
* type: (string) the type of event
* fn: (function) the callback supplied to addListener
* obj: (object) the custom object supplied to addListener
* adjust: (boolean) whether or not to adjust the default scope
* index: (int) its position in the Event util listener cache
* @static
*/
getListeners: function(el, sType) {
var elListeners = [];
if (listeners && listeners.length > 0) {
for (var i=0,len=listeners.length; i<len ; ++i) {
var l = listeners[i];
if ( l && l[this.EL] === el &&
(!sType || sType === l[this.TYPE]) ) {
elListeners.push({
type: l[this.TYPE],
fn: l[this.FN],
obj: l[this.OBJ],
adjust: l[this.ADJ_SCOPE],
index: i
});
}
}
}
return (elListeners.length) ? elListeners : null;
},
/**
* Removes all listeners registered by pe.event. Called
* automatically during the unload event.
* @method _unload
* @static
* @private
*/
_unload: function(e) {
var EU = YAHOO.util.Event, i, j, l, len, index;
for (i=0,len=unloadListeners.length; i<len; ++i) {
l = unloadListeners[i];
if (l) {
var scope = window;
if (l[EU.ADJ_SCOPE]) {
if (l[EU.ADJ_SCOPE] === true) {
scope = l[EU.OBJ];
} else {
scope = l[EU.ADJ_SCOPE];
}
}
l[EU.FN].call(scope, EU.getEvent(e), l[EU.OBJ] );
delete unloadListeners[i];
l=null;
scope=null;
}
}
if (listeners && listeners.length > 0) {
j = listeners.length;
while (j) {
index = j-1;
l = listeners[index];
if (l) {
EU.removeListener(l[EU.EL], l[EU.TYPE],
l[EU.FN], index);
}
j = j - 1;
}
l=null;
EU.clearCache();
}
for (i=0,len=legacyEvents.length; i<len; ++i) {
// dereference the element
delete legacyEvents[i][0];
// delete the array item
delete legacyEvents[i];
}
EU._simpleRemove(window, "unload", EU._unload);
},
/**
* 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];
}
},
/**
* Adds a DOM event directly without the caching, cleanup, scope 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;
/**
* YAHOO.util.Event.on is an alias for addListener
* @method on
* @see addListener
* @static
*/
EU.on = EU.addListener;
// YAHOO.mix(EU, YAHOO.util.EventProvider.prototype);
// EU.createEvent("DOMContentReady");
// EU.subscribe("DOMContentReady", EU._load);
if (document && document.body) {
EU._load();
} else {
// EU._simpleAdd(document, "DOMContentLoaded", EU._load);
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
* @param p_obj {Object} An object to be passed along when the event
* fires
* @param p_override {boolean} If true, the obj passed in becomes the
* execution scope of the listener
*/
subscribe: function(p_type, p_fn, p_obj, p_override) {
this.__yui_events = this.__yui_events || {};
var ce = this.__yui_events[p_type];
if (ce) {
ce.subscribe(p_fn, p_obj, p_override);
} 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, override: p_override } );
}
},
/**
* Unsubscribes the from the specified event
* @method unsubscribe
* @param p_type {string} The type, or name of the event
* @param p_fn {Function} The function to execute
* @param p_obj {Object} The custom object passed to subscribe (optional)
* @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 ce = this.__yui_events[p_type];
if (ce) {
return ce.unsubscribe(p_fn, p_obj);
} else {
return false;
}
},
/**
* 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:
*
* <ul>
* <li>
* scope: defines the default execution scope. If not defined
* the default scope will be this instance.
* </li>
* <li>
* silent: if true, the custom event will not generate log messages.
* This is false by default.
* </li>
* <li>
* 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.
* </li>
* </ul>
*
* @return {CustomEvent} the custom event
*
*/
createEvent: function(p_type, p_config) {
this.__yui_events = this.__yui_events || {};
var opts = p_config || {};
var events = this.__yui_events;
if (events[p_type]) {
} else {
var scope = opts.scope || this;
var silent = opts.silent || null;
var ce = new YAHOO.util.CustomEvent(p_type, scope, silent,
YAHOO.util.CustomEvent.FLAT);
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<qs.length; ++i) {
ce.subscribe(qs[i].fn, qs[i].obj, qs[i].override);
}
}
}
return events[p_type];
},
/**
* Fire a custom event by name. The callback functions will be executed
* from the scope specified when the event was created, and with the
* following parameters:
* <ul>
* <li>The first argument fire() was executed with</li>
* <li>The custom object (if any) that was passed into the subscribe()
* method</li>
* </ul>
* @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, or null if
* the custom event does not exist.
*/
fireEvent: function(p_type, arg1, arg2, etc) {
this.__yui_events = this.__yui_events || {};
var ce = this.__yui_events[p_type];
if (ce) {
var args = [];
for (var i=1; i<arguments.length; ++i) {
args.push(arguments[i]);
}
return ce.fire.apply(ce, args);
} else {
return null;
}
},
/**
* Returns true if the custom event of the provided type has been created
* with createEvent.
* @method hasEvent
* @param type {string} the type, or name of the event
*/
hasEvent: function(type) {
if (this.__yui_events) {
if (this.__yui_events[type]) {
return true;
}
}
return false;
}
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -