• Jump To … +
    breakpoints.js date-parse.js jquery.breakpoints.js jquery.calcRestrainedPos.js jquery.clientRect.js jquery.contentSize.js jquery.cookies.js jquery.css.js jquery.customEvent.js jquery.delimitedString.js jquery.disableEvent.js jquery.hostIframe.js jquery.hoverDelay.js jquery.htmlEncode.js jquery.imageSize.js jquery.isVisibleKeyCode.js jquery.menu.js jquery-menu-exampleskin.js jquery.modalDialog.deviceFixes.js jquery.modalDialog.getSettings.js jquery.modalDialog.header.js jquery.modalDialog.history.js jquery.modalDialog.js jquery.modalDialog.unobtrusive.js jquery.modalDialog.userAgent.js jquery.modalDialogContent.header.js jquery.modalDialogContent.js jquery.msAjax.js jquery.ns.js jquery.partialLoad.js jquery.postMessage.js jquery.proxyAll.js jquery.queryString.js jquery.richTooltip.js jquery.scrollAnchor.js jquery.uncomment.js jquery.url.js pointy.gestures.js pointy.js
  • jquery.disableEvent.js

  • ¶
    (function ($) {
  • ¶

    jQuery 1.8+ stores event data in the private $._data() method, whereas earlier versions stored using in the public fn.data() method.

        var _data = function ($el, key, value) {
            return $[$._data ? "_data" : "data"]($el[0], key, value);
        };
  • ¶

    Ensures that a data object is associated with a jQuery object If an object already exists, return it. If not, store and return the default object.

        function ensureData($el, key, defaultObject) {
            var data = _data($el, key);
            if (typeof data == "undefined") {
                data = defaultObject || {};
                _data($el, key, data);
            }
    
            return data;
        }
  • ¶

    Takes a space-delimited event type string and returns an array of individual event types.

        function parseEventTypes(types) {
            return $.trim(types).split(" "); // TODO: hoverHack in jQuery does something else here...
        }
  • ¶

    Returns true if the specified event has been disabled

        $.fn.isEventDisabled = function (eventType) {
            return eventType in (_data(this, "eventsDisabled") || {});
        };
    
        var _initialized = false;
        var _originalAddEvent;
  • ¶

    Lazily initialize. Pages that don’t use this plugin shouldn’t pay the overhead to monkey patch $.event.add().

        function initialize() {
            if (_initialized) {
                return;
            }
    
            _initialized = true;
    
            _originalAddEvent = $.event.add;
            $.event.add = addEvent;
        }
  • ¶

    Monkey patched version of $.event.add

        var addEvent = function (elem, types, handler, data, selector) {
            var eventsDisabled = _data($(elem), "eventsDisabled");
            if (eventsDisabled) {
                var eventTypes = parseEventTypes(types);
                for (var i = eventTypes.length - 1; i >= 0; i--) // loop backward- we need to remove elements from the array
                {
                    var eventType = eventTypes[i];
    
                    var disabledHandlers = eventsDisabled[eventType];
                    if (disabledHandlers) {
  • ¶

    This event type is disabled. Store the handler, and prevent it from being assigned via jQuery.

  • ¶

    $.event.add’s handler argument may be a handler object

                        if (handler.handler) {
                            disabledHandlers.push(handler);
                        } else {
                            disabledHandlers.push({
                                handler: handler,
                                data: data,
                                selector: selector,
                                type: eventType
                            });
                        }
  • ¶

    Handlers for this event type have been cached. Remove them so jQuery doesn’t assign them.

                        eventTypes.splice(i, 1);
                    }
                }
  • ¶

    Reconstruct the space-delimited “types” string.

                types = $.trim(eventTypes.join(" "));
                if (!types) {
                    return;
                }
            }
    
            _originalAddEvent.call(this, elem, types, handler, data, selector);
        };
  • ¶

    Disables all handlers for the specified event type(s) on the element Handlers added after this is called will be cached, but not actually added

        $.fn.disableEvent = function (eventType) {
            initialize();
  • ¶

    event types can be a space-delimited string (i.e. “click mouseup”)

            var eventTypes = parseEventTypes(eventType);
  • ¶

    Handle all jQuery elements

            this.each(function (i, el) {
                var $el = $(el);
  • ¶

    if the element has HTML attribute-based events, delete and re-add them with jQuery so the same mechanism can work on them.

                $.each(eventTypes, function (i, eventType) {
                    var attr = $el.attr("on" + eventType.toLowerCase());
                    if (attr) {
  • ¶

    Remove the native event

                        el["on" + eventType] = null;
  • ¶

    re-add with jQuery

                        /*jshint evil:true*/
                        $el.on(eventType, new Function("event", attr));
                    }
                });
  • ¶

    jQuery stores event handlers in an object associated with the element

                var events = _data($el, "events");
                if (events) {
                    $.each(eventTypes, function (i, eventType) {
  • ¶

    See if there are handlers for this particular event type

                        var handlers = events[eventType];
                        if (handlers) {
  • ¶

    Clone the array. When we call $.fn.off(), it will modify the array and mess up iteration.

                            handlers = handlers.slice(0);
    
                            var eventsDisabled = ensureData($el, "eventsDisabled");
  • ¶

    There are already some handlers disabled. Add them to the existing list.

                            var disabledHandlers = eventsDisabled[eventType];
                            if (!disabledHandlers) {
                                disabledHandlers = [];
                                eventsDisabled[eventType] = disabledHandlers;
                            }
    
                            for (var j = 0; j < handlers.length; j++) {
                                disabledHandlers.push(handlers[j]);
                                $el.off(eventType, handlers[j].handler);
                            }
                        }
                    });
                }
            });
        };
  • ¶

    Re-enables events disabled via $.fn.disableEvent

        $.fn.enableEvent = function (eventType, enable) {
            if (typeof enable != "undefined") {
                if (!enable) {
                    this.disableEvent(eventType);
                    return;
                }
            }
    
            this.each(function (i, el) {
                var $el = $(el);
                var eventsDisabled = _data($el, "eventsDisabled");
                if (eventsDisabled) {
                    var eventTypes = parseEventTypes(eventType);
                    $.each(eventTypes, function (i, eventType) {
                        var handlers = eventsDisabled[eventType];
                        if (handlers) {
                            $.each(handlers, function (j, handler) {
                                _originalAddEvent($el[0], eventType, handler);
                            });
    
                            delete eventsDisabled[eventType];
                        }
                    });
                }
            });
        };
    
    })(jQuery);