* --------------------------------------------------------------------------
*/
-const EventHandler = (() => {
- /**
- * ------------------------------------------------------------------------
- * Constants
- * ------------------------------------------------------------------------
- */
-
- const namespaceRegex = /[^.]*(?=\..*)\.|.*/
- const stripNameRegex = /\..*/
- const keyEventRegex = /^key/
- const stripUidRegex = /::\d+$/
- const eventRegistry = {} // Events storage
- let uidEvent = 1
- const customEvents = {
- mouseenter: 'mouseover',
- mouseleave: 'mouseout'
- }
- const nativeEvents = [
- 'click', 'dblclick', 'mouseup', 'mousedown', 'contextmenu',
- 'mousewheel', 'DOMMouseScroll',
- 'mouseover', 'mouseout', 'mousemove', 'selectstart', 'selectend',
- 'keydown', 'keypress', 'keyup',
- 'orientationchange',
- 'touchstart', 'touchmove', 'touchend', 'touchcancel',
- 'pointerdown', 'pointermove', 'pointerup', 'pointerleave', 'pointercancel',
- 'gesturestart', 'gesturechange', 'gestureend',
- 'focus', 'blur', 'change', 'reset', 'select', 'submit', 'focusin', 'focusout',
- 'load', 'unload', 'beforeunload', 'resize', 'move', 'DOMContentLoaded', 'readystatechange',
- 'error', 'abort', 'scroll'
- ]
-
- /**
- * ------------------------------------------------------------------------
- * Private methods
- * ------------------------------------------------------------------------
- */
-
- function getUidEvent(element, uid) {
- return uid && `${uid}::${uidEvent++}` || element.uidEvent || uidEvent++
- }
+/**
+ * ------------------------------------------------------------------------
+ * Constants
+ * ------------------------------------------------------------------------
+ */
- function getEvent(element) {
- const uid = getUidEvent(element)
- element.uidEvent = uid
+const namespaceRegex = /[^.]*(?=\..*)\.|.*/
+const stripNameRegex = /\..*/
+const keyEventRegex = /^key/
+const stripUidRegex = /::\d+$/
+const eventRegistry = {} // Events storage
+let uidEvent = 1
+const customEvents = {
+ mouseenter: 'mouseover',
+ mouseleave: 'mouseout'
+}
+const nativeEvents = [
+ 'click', 'dblclick', 'mouseup', 'mousedown', 'contextmenu',
+ 'mousewheel', 'DOMMouseScroll',
+ 'mouseover', 'mouseout', 'mousemove', 'selectstart', 'selectend',
+ 'keydown', 'keypress', 'keyup',
+ 'orientationchange',
+ 'touchstart', 'touchmove', 'touchend', 'touchcancel',
+ 'pointerdown', 'pointermove', 'pointerup', 'pointerleave', 'pointercancel',
+ 'gesturestart', 'gesturechange', 'gestureend',
+ 'focus', 'blur', 'change', 'reset', 'select', 'submit', 'focusin', 'focusout',
+ 'load', 'unload', 'beforeunload', 'resize', 'move', 'DOMContentLoaded', 'readystatechange',
+ 'error', 'abort', 'scroll'
+]
- return eventRegistry[uid] = eventRegistry[uid] || {}
- }
+/**
+ * ------------------------------------------------------------------------
+ * Private methods
+ * ------------------------------------------------------------------------
+ */
- function fixEvent(event, element) {
- // Add which for key events
- if (event.which === null && keyEventRegex.test(event.type)) {
- event.which = event.charCode !== null ? event.charCode : event.keyCode
- }
+function getUidEvent(element, uid) {
+ return uid && `${uid}::${uidEvent++}` || element.uidEvent || uidEvent++
+}
+
+function getEvent(element) {
+ const uid = getUidEvent(element)
+ element.uidEvent = uid
+
+ return eventRegistry[uid] = eventRegistry[uid] || {}
+}
- event.delegateTarget = element
+function fixEvent(event, element) {
+ // Add which for key events
+ if (event.which === null && keyEventRegex.test(event.type)) {
+ event.which = event.charCode !== null ? event.charCode : event.keyCode
}
- function bootstrapHandler(element, fn) {
- return function handler(event) {
- fixEvent(event, element)
- if (handler.oneOff) {
- EventHandler.off(element, event.type, fn)
- }
+ event.delegateTarget = element
+}
- return fn.apply(element, [event])
+function bootstrapHandler(element, fn) {
+ return function handler(event) {
+ fixEvent(event, element)
+ if (handler.oneOff) {
+ EventHandler.off(element, event.type, fn)
}
- }
- function bootstrapDelegationHandler(element, selector, fn) {
- return function handler(event) {
- const domElements = element.querySelectorAll(selector)
- for (let target = event.target; target && target !== this; target = target.parentNode) {
- for (let i = domElements.length; i--;) {
- if (domElements[i] === target) {
- fixEvent(event, target)
- if (handler.oneOff) {
- EventHandler.off(element, event.type, fn)
- }
+ return fn.apply(element, [event])
+ }
+}
- return fn.apply(target, [event])
+function bootstrapDelegationHandler(element, selector, fn) {
+ return function handler(event) {
+ const domElements = element.querySelectorAll(selector)
+ for (let target = event.target; target && target !== this; target = target.parentNode) {
+ for (let i = domElements.length; i--;) {
+ if (domElements[i] === target) {
+ fixEvent(event, target)
+ if (handler.oneOff) {
+ EventHandler.off(element, event.type, fn)
}
+
+ return fn.apply(target, [event])
}
}
-
- // To please ESLint
- return null
}
- }
- function findHandler(events, handler, delegationSelector = null) {
- for (const uid in events) {
- if (!Object.prototype.hasOwnProperty.call(events, uid)) {
- continue
- }
+ // To please ESLint
+ return null
+ }
+}
- const event = events[uid]
- if (event.originalHandler === handler && event.delegationSelector === delegationSelector) {
- return events[uid]
- }
+function findHandler(events, handler, delegationSelector = null) {
+ for (const uid in events) {
+ if (!Object.prototype.hasOwnProperty.call(events, uid)) {
+ continue
}
- return null
+ const event = events[uid]
+ if (event.originalHandler === handler && event.delegationSelector === delegationSelector) {
+ return events[uid]
+ }
}
- function normalizeParams(originalTypeEvent, handler, delegationFn) {
- const delegation = typeof handler === 'string'
- const originalHandler = delegation ? delegationFn : handler
+ return null
+}
- // allow to get the native events from namespaced events ('click.bs.button' --> 'click')
- let typeEvent = originalTypeEvent.replace(stripNameRegex, '')
+function normalizeParams(originalTypeEvent, handler, delegationFn) {
+ const delegation = typeof handler === 'string'
+ const originalHandler = delegation ? delegationFn : handler
- const custom = customEvents[typeEvent]
- if (custom) {
- typeEvent = custom
- }
+ // allow to get the native events from namespaced events ('click.bs.button' --> 'click')
+ let typeEvent = originalTypeEvent.replace(stripNameRegex, '')
- const isNative = nativeEvents.indexOf(typeEvent) > -1
- if (!isNative) {
- typeEvent = originalTypeEvent
- }
+ const custom = customEvents[typeEvent]
+ if (custom) {
+ typeEvent = custom
+ }
- return [delegation, originalHandler, typeEvent]
+ const isNative = nativeEvents.indexOf(typeEvent) > -1
+ if (!isNative) {
+ typeEvent = originalTypeEvent
}
- function addHandler(element, originalTypeEvent, handler, delegationFn, oneOff) {
- if (typeof originalTypeEvent !== 'string' || (typeof element === 'undefined' || element === null)) {
- return
- }
+ return [delegation, originalHandler, typeEvent]
+}
- if (!handler) {
- handler = delegationFn
- delegationFn = null
- }
+function addHandler(element, originalTypeEvent, handler, delegationFn, oneOff) {
+ if (typeof originalTypeEvent !== 'string' || (typeof element === 'undefined' || element === null)) {
+ return
+ }
- const [delegation, originalHandler, typeEvent] = normalizeParams(originalTypeEvent, handler, delegationFn)
+ if (!handler) {
+ handler = delegationFn
+ delegationFn = null
+ }
- const events = getEvent(element)
- const handlers = events[typeEvent] || (events[typeEvent] = {})
- const previousFn = findHandler(handlers, originalHandler, delegation ? handler : null)
+ const [delegation, originalHandler, typeEvent] = normalizeParams(originalTypeEvent, handler, delegationFn)
- if (previousFn) {
- previousFn.oneOff = previousFn.oneOff && oneOff
- return
- }
+ const events = getEvent(element)
+ const handlers = events[typeEvent] || (events[typeEvent] = {})
+ const previousFn = findHandler(handlers, originalHandler, delegation ? handler : null)
- const uid = getUidEvent(originalHandler, originalTypeEvent.replace(namespaceRegex, ''))
- const fn = !delegation ? bootstrapHandler(element, handler) : bootstrapDelegationHandler(element, handler, delegationFn)
+ if (previousFn) {
+ previousFn.oneOff = previousFn.oneOff && oneOff
+ return
+ }
- fn.delegationSelector = delegation ? handler : null
- fn.originalHandler = originalHandler
- fn.oneOff = oneOff
- fn.uidEvent = uid
- handlers[uid] = fn
+ const uid = getUidEvent(originalHandler, originalTypeEvent.replace(namespaceRegex, ''))
+ const fn = !delegation ? bootstrapHandler(element, handler) : bootstrapDelegationHandler(element, handler, delegationFn)
- element.addEventListener(typeEvent, fn, delegation)
- }
+ fn.delegationSelector = delegation ? handler : null
+ fn.originalHandler = originalHandler
+ fn.oneOff = oneOff
+ fn.uidEvent = uid
+ handlers[uid] = fn
- function removeHandler(element, events, typeEvent, handler, delegationSelector) {
- const fn = findHandler(events[typeEvent], handler, delegationSelector)
- if (fn === null) {
- return
- }
+ element.addEventListener(typeEvent, fn, delegation)
+}
- element.removeEventListener(typeEvent, fn, Boolean(delegationSelector))
- delete events[typeEvent][fn.uidEvent]
+function removeHandler(element, events, typeEvent, handler, delegationSelector) {
+ const fn = findHandler(events[typeEvent], handler, delegationSelector)
+ if (fn === null) {
+ return
}
- function removeNamespacedHandlers(element, events, typeEvent, namespace) {
- const storeElementEvent = events[typeEvent] || {}
- for (const handlerKey in storeElementEvent) {
- if (!Object.prototype.hasOwnProperty.call(storeElementEvent, handlerKey)) {
- continue
- }
+ element.removeEventListener(typeEvent, fn, Boolean(delegationSelector))
+ delete events[typeEvent][fn.uidEvent]
+}
- if (handlerKey.indexOf(namespace) > -1) {
- const event = storeElementEvent[handlerKey]
- removeHandler(element, events, typeEvent, event.originalHandler, event.delegationSelector)
- }
+function removeNamespacedHandlers(element, events, typeEvent, namespace) {
+ const storeElementEvent = events[typeEvent] || {}
+ for (const handlerKey in storeElementEvent) {
+ if (!Object.prototype.hasOwnProperty.call(storeElementEvent, handlerKey)) {
+ continue
}
- }
- return {
- on(element, event, handler, delegationFn) {
- addHandler(element, event, handler, delegationFn, false)
- },
+ if (handlerKey.indexOf(namespace) > -1) {
+ const event = storeElementEvent[handlerKey]
+ removeHandler(element, events, typeEvent, event.originalHandler, event.delegationSelector)
+ }
+ }
+}
- one(element, event, handler, delegationFn) {
- addHandler(element, event, handler, delegationFn, true)
- },
+const EventHandler = {
+ on(element, event, handler, delegationFn) {
+ addHandler(element, event, handler, delegationFn, false)
+ },
- off(element, originalTypeEvent, handler, delegationFn) {
- if (typeof originalTypeEvent !== 'string' || (typeof element === 'undefined' || element === null)) {
- return
- }
+ one(element, event, handler, delegationFn) {
+ addHandler(element, event, handler, delegationFn, true)
+ },
- const [delegation, originalHandler, typeEvent] = normalizeParams(originalTypeEvent, handler, delegationFn)
+ off(element, originalTypeEvent, handler, delegationFn) {
+ if (typeof originalTypeEvent !== 'string' || (typeof element === 'undefined' || element === null)) {
+ return
+ }
- const inNamespace = typeEvent !== originalTypeEvent
- const events = getEvent(element)
+ const [delegation, originalHandler, typeEvent] = normalizeParams(originalTypeEvent, handler, delegationFn)
- if (typeof originalHandler !== 'undefined') {
- // Simplest case: handler is passed, remove that listener ONLY.
- if (!events || !events[typeEvent]) {
- return
- }
+ const inNamespace = typeEvent !== originalTypeEvent
+ const events = getEvent(element)
- removeHandler(element, events, typeEvent, originalHandler, delegation ? handler : null)
+ if (typeof originalHandler !== 'undefined') {
+ // Simplest case: handler is passed, remove that listener ONLY.
+ if (!events || !events[typeEvent]) {
return
}
- const isNamespace = originalTypeEvent.charAt(0) === '.'
- if (isNamespace) {
- for (const elementEvent in events) {
- if (!Object.prototype.hasOwnProperty.call(events, elementEvent)) {
- continue
- }
-
- removeNamespacedHandlers(element, events, elementEvent, originalTypeEvent.substr(1))
- }
- }
+ removeHandler(element, events, typeEvent, originalHandler, delegation ? handler : null)
+ return
+ }
- const storeElementEvent = events[typeEvent] || {}
- for (const keyHandlers in storeElementEvent) {
- if (!Object.prototype.hasOwnProperty.call(storeElementEvent, keyHandlers)) {
+ const isNamespace = originalTypeEvent.charAt(0) === '.'
+ if (isNamespace) {
+ for (const elementEvent in events) {
+ if (!Object.prototype.hasOwnProperty.call(events, elementEvent)) {
continue
}
- const handlerKey = keyHandlers.replace(stripUidRegex, '')
- if (!inNamespace || originalTypeEvent.indexOf(handlerKey) > -1) {
- const event = storeElementEvent[keyHandlers]
- removeHandler(element, events, typeEvent, event.originalHandler, event.delegationSelector)
- }
+ removeNamespacedHandlers(element, events, elementEvent, originalTypeEvent.substr(1))
}
- },
+ }
- trigger(element, event, args) {
- if (typeof event !== 'string' ||
- (typeof element === 'undefined' || element === null)) {
- return null
+ const storeElementEvent = events[typeEvent] || {}
+ for (const keyHandlers in storeElementEvent) {
+ if (!Object.prototype.hasOwnProperty.call(storeElementEvent, keyHandlers)) {
+ continue
}
- const typeEvent = event.replace(stripNameRegex, '')
- const inNamespace = event !== typeEvent
- const isNative = nativeEvents.indexOf(typeEvent) > -1
+ const handlerKey = keyHandlers.replace(stripUidRegex, '')
+ if (!inNamespace || originalTypeEvent.indexOf(handlerKey) > -1) {
+ const event = storeElementEvent[keyHandlers]
+ removeHandler(element, events, typeEvent, event.originalHandler, event.delegationSelector)
+ }
+ }
+ },
- const $ = Util.jQuery
- let jQueryEvent
+ trigger(element, event, args) {
+ if (typeof event !== 'string' ||
+ (typeof element === 'undefined' || element === null)) {
+ return null
+ }
- let bubbles = true
- let nativeDispatch = true
- let defaultPrevented = false
+ const typeEvent = event.replace(stripNameRegex, '')
+ const inNamespace = event !== typeEvent
+ const isNative = nativeEvents.indexOf(typeEvent) > -1
- if (inNamespace && typeof $ !== 'undefined') {
- jQueryEvent = $.Event(event, args)
+ const $ = Util.jQuery
+ let jQueryEvent
- $(element).trigger(jQueryEvent)
- bubbles = !jQueryEvent.isPropagationStopped()
- nativeDispatch = !jQueryEvent.isImmediatePropagationStopped()
- defaultPrevented = jQueryEvent.isDefaultPrevented()
- }
+ let bubbles = true
+ let nativeDispatch = true
+ let defaultPrevented = false
- let evt = null
- if (isNative) {
- evt = document.createEvent('HTMLEvents')
- evt.initEvent(typeEvent, bubbles, true)
- } else {
- evt = new CustomEvent(event, {
- bubbles,
- cancelable: true
- })
- }
+ if (inNamespace && typeof $ !== 'undefined') {
+ jQueryEvent = $.Event(event, args)
- // merge custom informations in our event
- if (typeof args !== 'undefined') {
- Object.keys(args)
- .forEach((key) => {
- Object.defineProperty(evt, key, {
- get() {
- return args[key]
- }
- })
- })
- }
+ $(element).trigger(jQueryEvent)
+ bubbles = !jQueryEvent.isPropagationStopped()
+ nativeDispatch = !jQueryEvent.isImmediatePropagationStopped()
+ defaultPrevented = jQueryEvent.isDefaultPrevented()
+ }
- if (defaultPrevented) {
- evt.preventDefault()
+ let evt = null
+ if (isNative) {
+ evt = document.createEvent('HTMLEvents')
+ evt.initEvent(typeEvent, bubbles, true)
+ } else {
+ evt = new CustomEvent(event, {
+ bubbles,
+ cancelable: true
+ })
+ }
- if (!Polyfill.defaultPreventedPreservedOnDispatch) {
- Object.defineProperty(evt, 'defaultPrevented', {
- get: () => true
+ // merge custom informations in our event
+ if (typeof args !== 'undefined') {
+ Object.keys(args)
+ .forEach((key) => {
+ Object.defineProperty(evt, key, {
+ get() {
+ return args[key]
+ }
})
- }
- }
+ })
+ }
- if (nativeDispatch) {
- element.dispatchEvent(evt)
- }
+ if (defaultPrevented) {
+ evt.preventDefault()
- if (evt.defaultPrevented && typeof jQueryEvent !== 'undefined') {
- jQueryEvent.preventDefault()
+ if (!Polyfill.defaultPreventedPreservedOnDispatch) {
+ Object.defineProperty(evt, 'defaultPrevented', {
+ get: () => true
+ })
}
+ }
+
+ if (nativeDispatch) {
+ element.dispatchEvent(evt)
+ }
- return evt
+ if (evt.defaultPrevented && typeof jQueryEvent !== 'undefined') {
+ jQueryEvent.preventDefault()
}
+
+ return evt
}
-})()
+}
/* istanbul ignore next */
// focusin and focusout polyfill
* --------------------------------------------------------------------------
*/
-const SelectorEngine = (() => {
- /**
- * ------------------------------------------------------------------------
- * Constants
- * ------------------------------------------------------------------------
- */
-
- const closest = Polyfill.closest
- const find = Polyfill.find
- const findOne = Polyfill.findOne
-
- return {
- matches(element, selector) {
- return element.matches(selector)
- },
-
- find(selector, element = document.documentElement) {
- if (typeof selector !== 'string') {
- return null
- }
+/**
+ * ------------------------------------------------------------------------
+ * Constants
+ * ------------------------------------------------------------------------
+ */
- return find.call(element, selector)
- },
+const closest = Polyfill.closest
+const matchesFn = Polyfill.matches
+const find = Polyfill.find
+const findOne = Polyfill.findOne
+const nodeText = 3
- findOne(selector, element = document.documentElement) {
- if (typeof selector !== 'string') {
- return null
- }
+const SelectorEngine = {
+ matches(element, selector) {
+ return matchesFn.call(element, selector)
+ },
- return findOne.call(element, selector)
- },
+ find(selector, element = document.documentElement) {
+ if (typeof selector !== 'string') {
+ return null
+ }
- children(element, selector) {
- if (typeof selector !== 'string') {
- return null
- }
+ return find.call(element, selector)
+ },
+
+ findOne(selector, element = document.documentElement) {
+ if (typeof selector !== 'string') {
+ return null
+ }
- const children = Util.makeArray(element.children)
- return children.filter((child) => this.matches(child, selector))
- },
+ return findOne.call(element, selector)
+ },
- parents(element, selector) {
- if (typeof selector !== 'string') {
- return null
- }
+ children(element, selector) {
+ if (typeof selector !== 'string') {
+ return null
+ }
+
+ const children = Util.makeArray(element.children)
+ return children.filter((child) => this.matches(child, selector))
+ },
- const parents = []
+ parents(element, selector) {
+ if (typeof selector !== 'string') {
+ return null
+ }
- let ancestor = element.parentNode
- while (ancestor && ancestor.nodeType === Node.ELEMENT_NODE) {
- if (ancestor.matches(selector)) {
- parents.push(ancestor)
- }
+ const parents = []
- ancestor = ancestor.parentNode
+ let ancestor = element.parentNode
+ while (ancestor && ancestor.nodeType === Node.ELEMENT_NODE && ancestor.nodeType !== nodeText) {
+ if (this.matches(ancestor, selector)) {
+ parents.push(ancestor)
}
- return parents
- },
+ ancestor = ancestor.parentNode
+ }
- closest(element, selector) {
- if (typeof selector !== 'string') {
- return null
- }
+ return parents
+ },
- return closest(element, selector)
- },
+ closest(element, selector) {
+ if (typeof selector !== 'string') {
+ return null
+ }
- prev(element, selector) {
- if (typeof selector !== 'string') {
- return null
- }
+ return closest(element, selector)
+ },
- const siblings = []
+ prev(element, selector) {
+ if (typeof selector !== 'string') {
+ return null
+ }
- let previous = element.previousSibling
- while (previous) {
- if (previous.matches(selector)) {
- siblings.push(previous)
- }
+ const siblings = []
- previous = previous.previousSibling
+ let previous = element.previousSibling
+ while (previous && previous.nodeType === Node.ELEMENT_NODE && previous.nodeType !== nodeText) {
+ if (this.matches(previous, selector)) {
+ siblings.push(previous)
}
- return siblings
+ previous = previous.previousSibling
}
+
+ return siblings
}
-})()
+}
export default SelectorEngine