diff options
author | XhmikosR <xhmikosr@gmail.com> | 2019-04-18 14:47:52 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-18 14:47:52 +0300 |
commit | 091aa1e9fd411440c3ff3c95042a5aa1502e6978 (patch) | |
tree | 9343d4a164ad6e2c064d0305ba28e6d8870029e7 /js/dist/dom/eventhandler.js | |
parent | 5c5b15a077b796510fce745129612ee940b13b89 (diff) |
v5 dist (#28657)
Diffstat (limited to 'js/dist/dom/eventhandler.js')
-rw-r--r-- | js/dist/dom/eventhandler.js | 175 |
1 files changed, 163 insertions, 12 deletions
diff --git a/js/dist/dom/eventhandler.js b/js/dist/dom/eventhandler.js index 18cbf23567..5c8baaeed9 100644 --- a/js/dist/dom/eventhandler.js +++ b/js/dist/dom/eventhandler.js @@ -4,12 +4,10 @@ * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE) */ (function (global, factory) { - typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('./polyfill.js')) : - typeof define === 'function' && define.amd ? define(['./polyfill.js'], factory) : - (global = global || self, global.EventHandler = factory(global.Polyfill)); -}(this, function (Polyfill) { 'use strict'; - - Polyfill = Polyfill && Polyfill.hasOwnProperty('default') ? Polyfill['default'] : Polyfill; + typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : + typeof define === 'function' && define.amd ? define(factory) : + (global = global || self, global.EventHandler = factory()); +}(this, function () { 'use strict'; /** * -------------------------------------------------------------------------- @@ -17,8 +15,163 @@ * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE) * -------------------------------------------------------------------------- */ + var MAX_UID = 1000000; var _window = window, jQuery = _window.jQuery; // Shoutout AngusCroll (https://goo.gl/pxwQGp) + /** + * -------------------------------------------------------------------------- + * Public Util Api + * -------------------------------------------------------------------------- + */ + + + var getUID = function getUID(prefix) { + do { + // eslint-disable-next-line no-bitwise + prefix += ~~(Math.random() * MAX_UID); // "~~" acts like a faster Math.floor() here + } while (document.getElementById(prefix)); + + return prefix; + }; + + /* istanbul ignore file */ + var _Element$prototype = Element.prototype, + matches = _Element$prototype.matches, + closest = _Element$prototype.closest; + var find = Element.prototype.querySelectorAll; + var findOne = Element.prototype.querySelector; + + var createCustomEvent = function createCustomEvent(eventName, params) { + var cEvent = new CustomEvent(eventName, params); + return cEvent; + }; + + if (typeof window.CustomEvent !== 'function') { + createCustomEvent = function createCustomEvent(eventName, params) { + params = params || { + bubbles: false, + cancelable: false, + detail: null + }; + var evt = document.createEvent('CustomEvent'); + evt.initCustomEvent(eventName, params.bubbles, params.cancelable, params.detail); + return evt; + }; + } + + var workingDefaultPrevented = function () { + var e = document.createEvent('CustomEvent'); + e.initEvent('Bootstrap', true, true); + e.preventDefault(); + return e.defaultPrevented; + }(); + + if (!workingDefaultPrevented) { + var origPreventDefault = Event.prototype.preventDefault; + + Event.prototype.preventDefault = function () { + if (!this.cancelable) { + return; + } + + origPreventDefault.call(this); + Object.defineProperty(this, 'defaultPrevented', { + get: function get() { + return true; + }, + configurable: true + }); + }; + } // MSEdge resets defaultPrevented flag upon dispatchEvent call if at least one listener is attached + + + var defaultPreventedPreservedOnDispatch = function () { + var e = createCustomEvent('Bootstrap', { + cancelable: true + }); + var element = document.createElement('div'); + element.addEventListener('Bootstrap', function () { + return null; + }); + e.preventDefault(); + element.dispatchEvent(e); + return e.defaultPrevented; + }(); + + if (!matches) { + matches = Element.prototype.msMatchesSelector || Element.prototype.webkitMatchesSelector; + } + + if (!closest) { + closest = function closest(selector) { + var element = this; + + do { + if (matches.call(element, selector)) { + return element; + } + + element = element.parentElement || element.parentNode; + } while (element !== null && element.nodeType === 1); + + return null; + }; + } + + var scopeSelectorRegex = /:scope\b/; + + var supportScopeQuery = function () { + var element = document.createElement('div'); + + try { + element.querySelectorAll(':scope *'); + } catch (error) { + return false; + } + + return true; + }(); + + if (!supportScopeQuery) { + find = function find(selector) { + if (!scopeSelectorRegex.test(selector)) { + return this.querySelectorAll(selector); + } + + var hasId = Boolean(this.id); + + if (!hasId) { + this.id = getUID('scope'); + } + + var nodeList = null; + + try { + selector = selector.replace(scopeSelectorRegex, "#" + this.id); + nodeList = this.querySelectorAll(selector); + } finally { + if (!hasId) { + this.removeAttribute('id'); + } + } + + return nodeList; + }; + + findOne = function findOne(selector) { + if (!scopeSelectorRegex.test(selector)) { + return this.querySelector(selector); + } + + var matches = find.call(this, selector); + + if (typeof matches[0] !== 'undefined') { + return matches[0]; + } + + return null; + }; + } /** * -------------------------------------------------------------------------- @@ -110,10 +263,8 @@ delegationSelector = null; } - var uidList = Object.keys(events); - - for (var i = 0; i < uidList.length; i++) { - var uid = uidList[i]; + for (var _i = 0, _Object$keys = Object.keys(events); _i < _Object$keys.length; _i++) { + var uid = _Object$keys[_i]; var event = events[uid]; if (event.originalHandler === handler && event.delegationSelector === delegationSelector) { @@ -272,7 +423,7 @@ evt = document.createEvent('HTMLEvents'); evt.initEvent(typeEvent, bubbles, true); } else { - evt = new CustomEvent(event, { + evt = createCustomEvent(event, { bubbles: bubbles, cancelable: true }); @@ -292,7 +443,7 @@ if (defaultPrevented) { evt.preventDefault(); - if (!Polyfill.defaultPreventedPreservedOnDispatch) { + if (!defaultPreventedPreservedOnDispatch) { Object.defineProperty(evt, 'defaultPrevented', { get: function get() { return true; |