⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 event.js

📁 一个使用yui-ext库
💻 JS
📖 第 1 页 / 共 5 页
字号:
        if (!this.silent) {        }        for (i=0; i<len; ++i) {            var s = this.subscribers[i];            if (s) {                if (!this.silent) {                }                var scope = s.getScope(this.scope);                if (this.signature == YAHOO.util.CustomEvent.FLAT) {                    var param = null;                    if (args.length > 0) {                        param = args[0];                    }                    ret = s.fn.call(scope, param, s.obj);                } else {                    ret = s.fn.call(scope, this.type, args, s.obj);                }                if (false === ret) {                    if (!this.silent) {                    }                    //break;                    return false;                }            }        }        return true;    },    /**     * Removes all listeners     * @method unsubscribeAll     * @return {int} The number of listeners unsubscribed     */    unsubscribeAll: function() {        for (var i=0, len=this.subscribers.length; i<len; ++i) {            this._delete(len - 1 - i);        }        return 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}  override If true, the obj passed in becomes the execution *                            scope of the listener * @class Subscriber * @constructor */YAHOO.util.Subscriber = function(fn, obj, override) {    /**     * 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.  If override is an object, that      * object becomes the scope.     * @property override     * @type boolean|object     */    this.override = override;};/** * Returns the execution scope for this listener.  If override was set to true * the custom obj will be the scope.  If override is an object, that is the * scope, otherwise the default scope will be used. * @method getScope * @param {Object} defaultScope the scope to use if this listener does not *                              override it. */YAHOO.util.Subscriber.prototype.getScope = function(defaultScope) {    if (this.override) {        if (this.override === true) {            return this.obj;        } else {            return this.override;        }    }    return defaultScope;};/** * 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) {    if (obj) {        return (this.fn == fn && this.obj == obj);    } else {        return (this.fn == fn);    }};/** * @method toString */YAHOO.util.Subscriber.prototype.toString = function() {    return "Subscriber { obj: " + (this.obj || "")  +            ", override: " +  (this.override || "no") + " }";};/** * 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 one or more listeners the from the specified event     * @method unsubscribe     * @param p_type {string}   The type, or name of the event     * @param p_fn   {Function} The subscribed function to unsubscribe, if not     *                          supplied, all subscribers will be removed.     * @param p_obj  {Object}   The custom object passed to subscribe.  This is     *                        optional, but if supplied will be used to     *                        disambiguate multiple listeners that are the same     *                        (e.g., you subscribe many object using a function     *                        that lives on the prototype)     * @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;        }    },        /**     * Removes all listeners from the specified event     * @method unsubscribeAll     * @param p_type {string}   The type, or name of the event     */    unsubscribeAll: function(p_type) {        return this.unsubscribe(p_type);    },    /**     * 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;    }};/*** KeyListener is a utility that provides an easy interface for listening for* keydown/keyup events fired against DOM elements.* @namespace YAHOO.util* @class KeyListener* @constructor* @param {HTMLElement} attachTo The element or element ID to which the key *                               event should be attached* @param {String}      attachTo The element or element ID to which the key*                               event should be attached* @param {Object}      keyData  The object literal representing the key(s) *                               to detect. Possible attributes are *                               shift(boolean), alt(boolean), ctrl(boolean) *                               and keys(either an int or an array of ints *              

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -