0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-12-03 15:24:19 +01:00

Wrap our objects into IIFE

This commit is contained in:
Johann-S 2017-09-20 14:19:10 +02:00 committed by XhmikosR
parent bcbea02886
commit 7c1d0a1097
3 changed files with 419 additions and 377 deletions

View File

@ -5,7 +5,16 @@
* -------------------------------------------------------------------------- * --------------------------------------------------------------------------
*/ */
const mapData = (() => { const Data = (() => {
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
const mapData = (() => {
const storeData = {} const storeData = {}
let id = 1 let id = 1
return { return {
@ -43,9 +52,9 @@ const mapData = (() => {
} }
} }
} }
})() })()
const Data = { return {
setData(instance, key, data) { setData(instance, key, data) {
mapData.set(instance, key, data) mapData.set(instance, key, data)
}, },
@ -55,6 +64,7 @@ const Data = {
removeData(instance, key) { removeData(instance, key) {
mapData.delete(instance, key) mapData.delete(instance, key)
} }
} }
})()
export default Data export default Data

View File

@ -7,19 +7,27 @@ import Util from '../util'
* -------------------------------------------------------------------------- * --------------------------------------------------------------------------
*/ */
// defaultPrevented is broken in IE. const EventHandler = (() => {
// https://connect.microsoft.com/IE/feedback/details/790389/event-defaultprevented-returns-false-after-preventdefault-was-called
const workingDefaultPrevented = (() => { /**
* ------------------------------------------------------------------------
* Polyfills
* ------------------------------------------------------------------------
*/
// defaultPrevented is broken in IE.
// https://connect.microsoft.com/IE/feedback/details/790389/event-defaultprevented-returns-false-after-preventdefault-was-called
const workingDefaultPrevented = (() => {
const e = document.createEvent('CustomEvent') const e = document.createEvent('CustomEvent')
e.initEvent('Bootstrap', true, true) e.initEvent('Bootstrap', true, true)
e.preventDefault() e.preventDefault()
return e.defaultPrevented return e.defaultPrevented
})() })()
let defaultPreventedPreservedOnDispatch = true let defaultPreventedPreservedOnDispatch = true
// CustomEvent polyfill for IE (see: https://mzl.la/2v76Zvn) // CustomEvent polyfill for IE (see: https://mzl.la/2v76Zvn)
if (typeof window.CustomEvent !== 'function') { if (typeof window.CustomEvent !== 'function') {
window.CustomEvent = (event, params) => { window.CustomEvent = (event, params) => {
params = params || { params = params || {
bubbles: false, bubbles: false,
@ -48,7 +56,7 @@ if (typeof window.CustomEvent !== 'function') {
} }
window.CustomEvent.prototype = window.Event.prototype window.CustomEvent.prototype = window.Event.prototype
} else { } else {
// MSEdge resets defaultPrevented flag upon dispatchEvent call if at least one listener is attached // MSEdge resets defaultPrevented flag upon dispatchEvent call if at least one listener is attached
defaultPreventedPreservedOnDispatch = (() => { defaultPreventedPreservedOnDispatch = (() => {
const e = new CustomEvent('Bootstrap', { const e = new CustomEvent('Bootstrap', {
@ -62,10 +70,10 @@ if (typeof window.CustomEvent !== 'function') {
element.dispatchEvent(e) element.dispatchEvent(e)
return e.defaultPrevented return e.defaultPrevented
})() })()
} }
// Event constructor shim // Event constructor shim
if (!window.Event || typeof window.Event !== 'function') { if (!window.Event || typeof window.Event !== 'function') {
const origEvent = window.Event const origEvent = window.Event
window.Event = (inType, params) => { window.Event = (inType, params) => {
params = params || {} params = params || {}
@ -74,27 +82,29 @@ if (!window.Event || typeof window.Event !== 'function') {
return e return e
} }
window.Event.prototype = origEvent.prototype window.Event.prototype = origEvent.prototype
} }
const namespaceRegex = /[^.]*(?=\..*)\.|.*/ /**
const stripNameRegex = /\..*/ * ------------------------------------------------------------------------
const keyEventRegex = /^key/ * Constants
const stripUidRegex = /::\d+$/ * ------------------------------------------------------------------------
*/
// Events storage const TransitionEndEvent = {
const eventRegistry = {} WebkitTransition : 'webkitTransitionEnd',
let uidEvent = 1 transition : 'transitionend'
}
function getUidEvent(element, uid) { const namespaceRegex = /[^.]*(?=\..*)\.|.*/
return element.uidEvent = uid && `${uid}::${uidEvent++}` || element.uidEvent || uidEvent++ const stripNameRegex = /\..*/
} const keyEventRegex = /^key/
const stripUidRegex = /::\d+$/
function getEvent(element) { const eventRegistry = {} // Events storage
const uid = getUidEvent(element) let uidEvent = 1
return eventRegistry[uid] = eventRegistry[uid] || {} const customEvents = {
} mouseenter: 'mouseover',
mouseleave: 'mouseout'
const nativeEvents = [ }
const nativeEvents = [
'click', 'dblclick', 'mouseup', 'mousedown', 'contextmenu', 'click', 'dblclick', 'mouseup', 'mousedown', 'contextmenu',
'mousewheel', 'DOMMouseScroll', 'mousewheel', 'DOMMouseScroll',
'mouseover', 'mouseout', 'mousemove', 'selectstart', 'selectend', 'mouseover', 'mouseout', 'mousemove', 'selectstart', 'selectend',
@ -105,29 +115,40 @@ const nativeEvents = [
'focus', 'blur', 'change', 'reset', 'select', 'submit', 'focusin', 'focusout', 'focus', 'blur', 'change', 'reset', 'select', 'submit', 'focusin', 'focusout',
'load', 'unload', 'beforeunload', 'resize', 'move', 'DOMContentLoaded', 'readystatechange', 'load', 'unload', 'beforeunload', 'resize', 'move', 'DOMContentLoaded', 'readystatechange',
'error', 'abort', 'scroll' 'error', 'abort', 'scroll'
] ]
const customEvents = { /**
mouseenter: 'mouseover', * ------------------------------------------------------------------------
mouseleave: 'mouseout' * Private methods
} * ------------------------------------------------------------------------
*/
function fixEvent(event) {
function getUidEvent(element, uid) {
return element.uidEvent = uid && `${uid}::${uidEvent++}` || element.uidEvent || uidEvent++
}
function getEvent(element) {
const uid = getUidEvent(element)
return eventRegistry[uid] = eventRegistry[uid] || {}
}
function fixEvent(event) {
// Add which for key events // Add which for key events
if (event.which === null && keyEventRegex.test(event.type)) { if (event.which === null && keyEventRegex.test(event.type)) {
event.which = event.charCode !== null ? event.charCode : event.keyCode event.which = event.charCode !== null ? event.charCode : event.keyCode
} }
return event return event
} }
function bootstrapHandler(element, fn) { function bootstrapHandler(element, fn) {
return function (event) { return function (event) {
event = fixEvent(event) event = fixEvent(event)
return fn.apply(element, [event]) return fn.apply(element, [event])
} }
} }
function bootstrapDelegationHandler(element, selector, fn) { function bootstrapDelegationHandler(element, selector, fn) {
return function (event) { return function (event) {
event = fixEvent(event) event = fixEvent(event)
const domElements = element.querySelectorAll(selector) const domElements = element.querySelectorAll(selector)
@ -141,16 +162,16 @@ function bootstrapDelegationHandler(element, selector, fn) {
// To please ESLint // To please ESLint
return null return null
} }
} }
function removeHandler(element, events, typeEvent, handler) { function removeHandler(element, events, typeEvent, handler) {
const uidEvent = handler.uidEvent const uidEvent = handler.uidEvent
const fn = events[typeEvent][uidEvent] const fn = events[typeEvent][uidEvent]
element.removeEventListener(typeEvent, fn, fn.delegation) element.removeEventListener(typeEvent, fn, fn.delegation)
delete events[typeEvent][uidEvent] delete events[typeEvent][uidEvent]
} }
function removeNamespacedHandlers(element, events, typeEvent, namespace) { function removeNamespacedHandlers(element, events, typeEvent, namespace) {
const storeElementEvent = events[typeEvent] || {} const storeElementEvent = events[typeEvent] || {}
for (const handlerKey in storeElementEvent) { for (const handlerKey in storeElementEvent) {
if (!Object.prototype.hasOwnProperty.call(storeElementEvent, handlerKey)) { if (!Object.prototype.hasOwnProperty.call(storeElementEvent, handlerKey)) {
@ -161,9 +182,9 @@ function removeNamespacedHandlers(element, events, typeEvent, namespace) {
removeHandler(element, events, typeEvent, storeElementEvent[handlerKey].originalHandler) removeHandler(element, events, typeEvent, storeElementEvent[handlerKey].originalHandler)
} }
} }
} }
const EventHandler = { return {
on(element, originalTypeEvent, handler, delegationFn) { on(element, originalTypeEvent, handler, delegationFn) {
if (typeof originalTypeEvent !== 'string' || if (typeof originalTypeEvent !== 'string' ||
(typeof element === 'undefined' || element === null)) { (typeof element === 'undefined' || element === null)) {
@ -325,7 +346,8 @@ const EventHandler = {
return evt return evt
} }
} }
})()
// focusin and focusout polyfill // focusin and focusout polyfill
if (typeof window.onfocusin === 'undefined') { if (typeof window.onfocusin === 'undefined') {

View File

@ -5,19 +5,28 @@
* -------------------------------------------------------------------------- * --------------------------------------------------------------------------
*/ */
// matches polyfill (see: https://mzl.la/2ikXneG) const SelectorEngine = (() => {
let fnMatches = null
if (!Element.prototype.matches) {
/**
* ------------------------------------------------------------------------
* Polyfills
* ------------------------------------------------------------------------
*/
// matches polyfill (see: https://mzl.la/2ikXneG)
let fnMatches = null
if (!Element.prototype.matches) {
fnMatches = fnMatches =
Element.prototype.msMatchesSelector || Element.prototype.msMatchesSelector ||
Element.prototype.webkitMatchesSelector Element.prototype.webkitMatchesSelector
} else { } else {
fnMatches = Element.prototype.matches fnMatches = Element.prototype.matches
} }
// closest polyfill (see: https://mzl.la/2vXggaI) // closest polyfill (see: https://mzl.la/2vXggaI)
let fnClosest = null let fnClosest = null
if (!Element.prototype.closest) { if (!Element.prototype.closest) {
fnClosest = (element, selector) => { fnClosest = (element, selector) => {
let ancestor = element let ancestor = element
if (!document.documentElement.contains(element)) { if (!document.documentElement.contains(element)) {
@ -34,14 +43,14 @@ if (!Element.prototype.closest) {
return null return null
} }
} else { } else {
// eslint-disable-next-line arrow-body-style // eslint-disable-next-line arrow-body-style
fnClosest = (element, selector) => { fnClosest = (element, selector) => {
return element.closest(selector) return element.closest(selector)
} }
} }
const SelectorEngine = { return {
matches(element, selector) { matches(element, selector) {
return fnMatches.call(element, selector) return fnMatches.call(element, selector)
}, },
@ -75,6 +84,7 @@ const SelectorEngine = {
closest(element, selector) { closest(element, selector) {
return fnClosest(element, selector) return fnClosest(element, selector)
} }
} }
})()
export default SelectorEngine export default SelectorEngine