📄 eventmanager.js
字号:
/*
* Ext JS Library 3.0 Pre-alpha
* Copyright(c) 2006-2008, Ext JS, LLC.
* licensing@extjs.com
*
* http://extjs.com/license
*/
/** * @class Ext.EventManager * Registers event handlers that want to receive a normalized EventObject instead of the standard browser event and provides * several useful events directly. * See {@link Ext.EventObject} for more details on normalized event objects. * @singleton */Ext.EventManager = function(){ var docReadyEvent, docReadyProcId, docReadyState = false, E = Ext.lib.Event, D = Ext.lib.Dom, DOC = document, WINDOW = window, IEDEFERED = "ie-deferred-loader", DOMCONTENTLOADED = "DOMContentLoaded", elHash = {}, propRe = /^(?:scope|delay|buffer|single|stopEvent|preventDefault|stopPropagation|normalized|args|delegate)$/; /// There is some jquery work around stuff here that isn't needed in Ext Core. function addListener(el, ename, fn, wrap, scope){ var id = Ext.id(el), es = elHash[id] = elHash[id] || {}; (es[ename] = es[ename] || []).push([fn, wrap, scope]); E.on(el, ename, wrap); // this is a workaround for jQuery and should somehow be removed from Ext Core in the future // without breaking ExtJS. if(ename == "mousewheel" && el.addEventListener){ // workaround for jQuery var args = ["DOMMouseScroll", wrap, false]; el.addEventListener.apply(el, args); E.on(window, 'unload', function(){ el.removeEventListener.apply(el, args); }); } if(ename == "mousedown" && el == document){ // fix stopped mousedowns on the document Ext.EventManager.stoppedMouseDownEvent.addListener(wrap); } }; function fireDocReady(){ if(!docReadyState){ Ext.isReady = docReadyState = true; if(docReadyProcId){ clearInterval(docReadyProcId); } if(Ext.isGecko || Ext.isOpera) { DOC.removeEventListener(DOMCONTENTLOADED, fireDocReady, false); } if(Ext.isIE){ var defer = DOC.getElementById(IEDEFERED); if(defer){ defer.onreadystatechange = null; defer.parentNode.removeChild(defer); } } if(docReadyEvent){ docReadyEvent.fire(); docReadyEvent.clearListeners(); } } }; function initDocReady(){ var COMPLETE = "complete"; docReadyEvent = new Ext.util.Event(); if (Ext.isGecko || Ext.isOpera) { DOC.addEventListener(DOMCONTENTLOADED, fireDocReady, false); } else if (Ext.isIE){ DOC.write("<s"+'cript id=' + IEDEFERED + ' defer="defer" src="/'+'/:"></s'+"cript>"); DOC.getElementById(IEDEFERED).onreadystatechange = function(){ if(this.readyState == COMPLETE){ fireDocReady(); } }; } else if (Ext.isWebKit){ docReadyProcId = setInterval(function(){ if(DOC.readyState == COMPLETE) { fireDocReady(); } }, 10); } // no matter what, make sure it fires on load E.on(WINDOW, "load", fireDocReady); }; function createTargeted(h, o){ return function(){ var args = Ext.toArray(arguments); if(o.target == Ext.EventObject.setEvent(args[0]).target){ h.apply(this, args); } }; }; function createBuffered(h, o){ var task = new Ext.util.DelayedTask(h); return function(e){ // create new event object impl so new events don't wipe out properties task.delay(o.buffer, h, null, [new Ext.EventObjectImpl(e)]); }; }; function createSingle(h, el, ename, fn, scope){ return function(e){ Ext.EventManager.removeListener(el, ename, fn, scope); h(e); }; }; function createDelayed(h, o){ return function(e){ // create new event object impl so new events don't wipe out properties e = new Ext.EventObjectImpl(e); setTimeout(function(){ h(e); }, o.delay || 10); }; }; function listen(element, ename, opt, fn, scope){ var o = !Ext.isObject(opt) ? {} : opt, el = Ext.getDom(element); fn = fn || o.fn; scope = scope || o.scope; if(!el){ throw "Error listening for \"" + ename + '\". Element "' + element + '" doesn\'t exist.'; } function h(e){ // prevent errors while unload occurring if(!Ext){// !window[xname]){ ==> can't we do this? return; } e = Ext.EventObject.setEvent(e); var t; if (o.delegate) { if(!(t = e.getTarget(o.delegate, el))){ return; } } else { t = e.target; } if (o.stopEvent) { e.stopEvent(); } if (o.preventDefault) { e.preventDefault(); } if (o.stopPropagation) { e.stopPropagation(); } if (o.normalized) { e = e.browserEvent; } fn.call(scope || el, e, t, o); }; if(o.target){ h = createTargeted(h, o); } if(o.delay){ h = createDelayed(h, o); } if(o.single){ h = createSingle(h, el, ename, fn, scope); } if(o.buffer){ h = createBuffered(h, o); } addListener(el, ename, fn, h, scope); return h; }; var pub = { /** * Appends an event handler to an element. The shorthand version {@link #on} is equivalent. Typically you will * use {@link Ext.Element#addListener} directly on an Element in favor of calling this version. * @param {String/HTMLElement} el The html element or id to assign the event handler to * @param {String} eventName The type of event to listen for * @param {Function} handler The handler function the event invokes This function is passed * the following parameters:<ul> * <li>evt : EventObject<div class="sub-desc">The {@link Ext.EventObject EventObject} describing the event.</div></li> * <li>t : Element<div class="sub-desc">The {@link Ext.Element Element} which was the target of the event. * Note that this may be filtered by using the <tt>delegate</tt> option.</div></li> * <li>o : Object<div class="sub-desc">The options object from the addListener call.</div></li> * </ul> * @param {Object} scope (optional) The scope in which to execute the handler * function (the handler function's "this" context) * @param {Object} options (optional) An object containing handler configuration properties. * This may contain any of the following properties:<ul> * <li>scope {Object} : The scope in which to execute the handler function. The handler function's "this" context.</li> * <li>delegate {String} : A simple selector to filter the target or look for a descendant of the target</li> * <li>stopEvent {Boolean} : True to stop the event. That is stop propagation, and prevent the default action.</li> * <li>preventDefault {Boolean} : True to prevent the default action</li> * <li>stopPropagation {Boolean} : True to prevent event propagation</li> * <li>normalized {Boolean} : False to pass a browser event to the handler function instead of an Ext.EventObject</li> * <li>delay {Number} : The number of milliseconds to delay the invocation of the handler after te event fires.</li> * <li>single {Boolean} : True to add a handler to handle just the next firing of the event, and then remove itself.</li> * <li>buffer {Number} : Causes the handler to be scheduled to run in an {@link Ext.util.DelayedTask} delayed * by the specified number of milliseconds. If the event fires again within that time, the original * handler is <em>not</em> invoked, but the new handler is scheduled in its place.</li> * <li>target {Element} : Only call the handler if the event was fired on the target Element, <i>not</i> * if the event was bubbled up from a child node.</li> * </ul><br> * <p>See {@link Ext.Element#addListener} for examples of how to use these options.</p> */ addListener : function(element, eventName, fn, scope, options){ if(Ext.isObject(eventName)){ var o = eventName, e, val; for(e in o){ val = o[e]; if(!propRe.test(e)){ if(Ext.isFunction(val)){ // shared options listen(element, e, o, val, o.scope); }else{ // individual options listen(element, e, val); } } } } else { listen(element, eventName, options, fn, scope); } }, /** * Removes an event handler from an element. The shorthand version {@link #un} is equivalent. Typically * you will use {@link Ext.Element#removeListener} directly on an Element in favor of calling this version. * @param {String/HTMLElement} el The id or html element from which to remove the event * @param {String} eventName The type of event * @param {Function} fn The handler function to remove */ removeListener : function(element, eventName, fn, scope){ var el = Ext.getDom(element), id = Ext.id(el), wrap; Ext.each((elHash[id] || {})[eventName], function (v,i,a) { if (Ext.isArray(v) && v[0] == fn && (!scope || v[2] == scope)) { E.un(el, eventName, wrap = v[1]); a.splice(i,1); return false; } }); // jQuery workaround that should be removed from Ext Core if(eventName == "mousewheel" && el.addEventListener && wrap){ el.removeEventListener("DOMMouseScroll", wrap, false); } if(eventName == "mousedown" && el == DOC && wrap){ // fix stopped mousedowns on the document Ext.EventManager.stoppedMouseDownEvent.removeListener(wrap); } }, /** * Removes all event handers from an element. Typically you will use {@link Ext.Element#removeAllListeners} * directly on an Element in favor of calling this version. * @param {String/HTMLElement} el The id or html element from which to remove the event */ removeAll : function(el){ var id = Ext.id(el = Ext.getDom(el)), es = elHash[id], ename; for(ename in es){ if(es.hasOwnProperty(ename)){ Ext.each(es[ename], function(v) { E.un(el, ename, v.wrap); }); } } elHash[id] = null; }, /** * Fires when the document is ready (before onload and before images are loaded). Can be * accessed shorthanded as Ext.onReady(). * @param {Function} fn The method the event invokes * @param {Object} scope (optional) An object that becomes the scope of the handler * @param {boolean} options (optional) An object containing standard {@link #addListener} options */ onDocumentReady : function(fn, scope, options){ if(docReadyState){ // if it already fired docReadyEvent.addListener(fn, scope, options); docReadyEvent.fire(); docReadyEvent.clearListeners(); } else { if(!docReadyEvent) initDocReady(); options = options || {}; options.delay = options.delay || 1; docReadyEvent.addListener(fn, scope, options); } }, elHash : elHash }; /** * Appends an event handler to an element. Shorthand for {@link #addListener}. * @param {String/HTMLElement} el The html element or id to assign the event handler to * @param {String} eventName The type of event to listen for * @param {Function} handler The handler function the event invokes
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -