Skip to content

Vanilla JS version #14

@mcasimir

Description

@mcasimir

Hi, i was attempting to use your polyfill for Mobile Angular UI so I re-adapted it to be used without jquery.
Dropped the idea since I think nothing could be done for Opera, at least not with a lightweight solution.
Never tested, but should be correct and should work.

/*
 * Pointer Events Polyfill: Adds support for the style attribute "pointer-events: none" to browsers without this feature (namely, IE).
 * (c) 2013, Kent Mewhort, licensed under BSD. See LICENSE.txt for details.
 */
(function() {
  'use strict';

  // Prevents a drunk to use it twice
  if (!window.PointerEventsPolyfill) {
    var attachedOnce = false;
    var addEventListenerMethod = document.addEventListener ? 'addEventListener' : 'attachEvent';

    // Handle multiple events with same callback. Works with ms.
    var attachEvents = function(elem, events, callback) {
      for (var i = 0; i < events.length; i++) {
        elem[addEventListenerMethod](events[i], callback);
      }
    };

    // Detects if pointer-events is supported
    var hasNativePointerEventsSupport = function () {
      var element = $document.createElement('span');
      element.style.cssText = 'pointer-events:auto';
      return element.style.pointerEvents === 'auto';    
    };

    // Cross browser `window.getComputedStyle`, taken from jquery .css() source
    var getElemComputedStyle = function(elem) {
      if ( elem.ownerDocument.defaultView.opener ) {
        return elem.ownerDocument.defaultView.getComputedStyle( elem, null );
      }
      return window.getComputedStyle( elem, null );
    };

    var polyfillCallback = function(e) {
      if (e.target.style.pointerEvents === 'none') {

        var origDisplayAttribute = e.target.style.display;
        e.target.style.display = 'none';
        var underneathElem = document.elementFromPoint(e.clientX, e.clientY);
        e.target.style.display = origDisplayAttribute;
        e.target = underneathElem;
        if (underneathElem.dispatchEvent) {
          underneathElem.dispatchEvent(e);
        } else if (underneathElem.fireEvent) {
          underneathElem.fireEvent('on'+e.eventType, e);
        }

        return false;
      } else {
        return true;
      }
    };

    PointerEventsPolyfill.attach = function(userOptions) {
      userOptions = userOptions || {};

      var options = {
        mouseEvents: userOptions.mouseEvents || ['click', 'dblclick', 'mousedown', 'mouseup'],
        usePolyfillIf: userOptions.usePolyfillIf || function(){ return !hasNativePointerEventsSupport(); }
      };

      if (attachedOnce || !options.usePolyfillIf()) { return false; } else {
        attachedOnce = true;
        attachEvents(document, userOptions.mouseEvents, polyfillCallback);
        return true;
      }
    };

    PointerEventsPolyfill.initialize = PointerEventsPolyfill.attach;
    window.PointerEventsPolyfill = PointerEventsPolyfill;
  }
}());

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions