mirror of
https://github.com/twbs/bootstrap.git
synced 2025-09-28 06:20:15 +02:00
Switch to strings constants.
This allows the minifier to mangle the constants. It also allows the linter to find unused strings properly. While at it, remove a few unused properties. File Before After Diff -------------------------------------------------------- bootstrap.bundle.min.js 23.61 kB 22.61 kB -1.00 kB (-4.23 %) bootstrap.min.js 17.04 kB 16.08 kB -0.96 kB (-5.63 %)
This commit is contained in:
@@ -44,30 +44,22 @@ const DefaultType = {
|
||||
parent: '(string|element)'
|
||||
}
|
||||
|
||||
const Event = {
|
||||
SHOW: `show${EVENT_KEY}`,
|
||||
SHOWN: `shown${EVENT_KEY}`,
|
||||
HIDE: `hide${EVENT_KEY}`,
|
||||
HIDDEN: `hidden${EVENT_KEY}`,
|
||||
CLICK_DATA_API: `click${EVENT_KEY}${DATA_API_KEY}`
|
||||
}
|
||||
const EVENT_SHOW = `show${EVENT_KEY}`
|
||||
const EVENT_SHOWN = `shown${EVENT_KEY}`
|
||||
const EVENT_HIDE = `hide${EVENT_KEY}`
|
||||
const EVENT_HIDDEN = `hidden${EVENT_KEY}`
|
||||
const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`
|
||||
|
||||
const ClassName = {
|
||||
SHOW: 'show',
|
||||
COLLAPSE: 'collapse',
|
||||
COLLAPSING: 'collapsing',
|
||||
COLLAPSED: 'collapsed'
|
||||
}
|
||||
const CLASS_NAME_SHOW = 'show'
|
||||
const CLASS_NAME_COLLAPSE = 'collapse'
|
||||
const CLASS_NAME_COLLAPSING = 'collapsing'
|
||||
const CLASS_NAME_COLLAPSED = 'collapsed'
|
||||
|
||||
const Dimension = {
|
||||
WIDTH: 'width',
|
||||
HEIGHT: 'height'
|
||||
}
|
||||
const WIDTH = 'width'
|
||||
const HEIGHT = 'height'
|
||||
|
||||
const Selector = {
|
||||
ACTIVES: '.show, .collapsing',
|
||||
DATA_TOGGLE: '[data-toggle="collapse"]'
|
||||
}
|
||||
const SELECTOR_ACTIVES = '.show, .collapsing'
|
||||
const SELECTOR_DATA_TOGGLE = '[data-toggle="collapse"]'
|
||||
|
||||
/**
|
||||
* ------------------------------------------------------------------------
|
||||
@@ -81,11 +73,11 @@ class Collapse {
|
||||
this._element = element
|
||||
this._config = this._getConfig(config)
|
||||
this._triggerArray = makeArray(SelectorEngine.find(
|
||||
`${Selector.DATA_TOGGLE}[href="#${element.id}"],` +
|
||||
`${Selector.DATA_TOGGLE}[data-target="#${element.id}"]`
|
||||
`${SELECTOR_DATA_TOGGLE}[href="#${element.id}"],` +
|
||||
`${SELECTOR_DATA_TOGGLE}[data-target="#${element.id}"]`
|
||||
))
|
||||
|
||||
const toggleList = makeArray(SelectorEngine.find(Selector.DATA_TOGGLE))
|
||||
const toggleList = makeArray(SelectorEngine.find(SELECTOR_DATA_TOGGLE))
|
||||
for (let i = 0, len = toggleList.length; i < len; i++) {
|
||||
const elem = toggleList[i]
|
||||
const selector = getSelectorFromElement(elem)
|
||||
@@ -124,7 +116,7 @@ class Collapse {
|
||||
// Public
|
||||
|
||||
toggle() {
|
||||
if (this._element.classList.contains(ClassName.SHOW)) {
|
||||
if (this._element.classList.contains(CLASS_NAME_SHOW)) {
|
||||
this.hide()
|
||||
} else {
|
||||
this.show()
|
||||
@@ -133,7 +125,7 @@ class Collapse {
|
||||
|
||||
show() {
|
||||
if (this._isTransitioning ||
|
||||
this._element.classList.contains(ClassName.SHOW)) {
|
||||
this._element.classList.contains(CLASS_NAME_SHOW)) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -141,13 +133,13 @@ class Collapse {
|
||||
let activesData
|
||||
|
||||
if (this._parent) {
|
||||
actives = makeArray(SelectorEngine.find(Selector.ACTIVES, this._parent))
|
||||
actives = makeArray(SelectorEngine.find(SELECTOR_ACTIVES, this._parent))
|
||||
.filter(elem => {
|
||||
if (typeof this._config.parent === 'string') {
|
||||
return elem.getAttribute('data-parent') === this._config.parent
|
||||
}
|
||||
|
||||
return elem.classList.contains(ClassName.COLLAPSE)
|
||||
return elem.classList.contains(CLASS_NAME_COLLAPSE)
|
||||
})
|
||||
|
||||
if (actives.length === 0) {
|
||||
@@ -165,7 +157,7 @@ class Collapse {
|
||||
}
|
||||
}
|
||||
|
||||
const startEvent = EventHandler.trigger(this._element, Event.SHOW)
|
||||
const startEvent = EventHandler.trigger(this._element, EVENT_SHOW)
|
||||
if (startEvent.defaultPrevented) {
|
||||
return
|
||||
}
|
||||
@@ -184,14 +176,14 @@ class Collapse {
|
||||
|
||||
const dimension = this._getDimension()
|
||||
|
||||
this._element.classList.remove(ClassName.COLLAPSE)
|
||||
this._element.classList.add(ClassName.COLLAPSING)
|
||||
this._element.classList.remove(CLASS_NAME_COLLAPSE)
|
||||
this._element.classList.add(CLASS_NAME_COLLAPSING)
|
||||
|
||||
this._element.style[dimension] = 0
|
||||
|
||||
if (this._triggerArray.length) {
|
||||
this._triggerArray.forEach(element => {
|
||||
element.classList.remove(ClassName.COLLAPSED)
|
||||
element.classList.remove(CLASS_NAME_COLLAPSED)
|
||||
element.setAttribute('aria-expanded', true)
|
||||
})
|
||||
}
|
||||
@@ -199,15 +191,15 @@ class Collapse {
|
||||
this.setTransitioning(true)
|
||||
|
||||
const complete = () => {
|
||||
this._element.classList.remove(ClassName.COLLAPSING)
|
||||
this._element.classList.add(ClassName.COLLAPSE)
|
||||
this._element.classList.add(ClassName.SHOW)
|
||||
this._element.classList.remove(CLASS_NAME_COLLAPSING)
|
||||
this._element.classList.add(CLASS_NAME_COLLAPSE)
|
||||
this._element.classList.add(CLASS_NAME_SHOW)
|
||||
|
||||
this._element.style[dimension] = ''
|
||||
|
||||
this.setTransitioning(false)
|
||||
|
||||
EventHandler.trigger(this._element, Event.SHOWN)
|
||||
EventHandler.trigger(this._element, EVENT_SHOWN)
|
||||
}
|
||||
|
||||
const capitalizedDimension = dimension[0].toUpperCase() + dimension.slice(1)
|
||||
@@ -222,11 +214,11 @@ class Collapse {
|
||||
|
||||
hide() {
|
||||
if (this._isTransitioning ||
|
||||
!this._element.classList.contains(ClassName.SHOW)) {
|
||||
!this._element.classList.contains(CLASS_NAME_SHOW)) {
|
||||
return
|
||||
}
|
||||
|
||||
const startEvent = EventHandler.trigger(this._element, Event.HIDE)
|
||||
const startEvent = EventHandler.trigger(this._element, EVENT_HIDE)
|
||||
if (startEvent.defaultPrevented) {
|
||||
return
|
||||
}
|
||||
@@ -237,9 +229,9 @@ class Collapse {
|
||||
|
||||
reflow(this._element)
|
||||
|
||||
this._element.classList.add(ClassName.COLLAPSING)
|
||||
this._element.classList.remove(ClassName.COLLAPSE)
|
||||
this._element.classList.remove(ClassName.SHOW)
|
||||
this._element.classList.add(CLASS_NAME_COLLAPSING)
|
||||
this._element.classList.remove(CLASS_NAME_COLLAPSE)
|
||||
this._element.classList.remove(CLASS_NAME_SHOW)
|
||||
|
||||
const triggerArrayLength = this._triggerArray.length
|
||||
if (triggerArrayLength > 0) {
|
||||
@@ -247,8 +239,8 @@ class Collapse {
|
||||
const trigger = this._triggerArray[i]
|
||||
const elem = getElementFromSelector(trigger)
|
||||
|
||||
if (elem && !elem.classList.contains(ClassName.SHOW)) {
|
||||
trigger.classList.add(ClassName.COLLAPSED)
|
||||
if (elem && !elem.classList.contains(CLASS_NAME_SHOW)) {
|
||||
trigger.classList.add(CLASS_NAME_COLLAPSED)
|
||||
trigger.setAttribute('aria-expanded', false)
|
||||
}
|
||||
}
|
||||
@@ -258,9 +250,9 @@ class Collapse {
|
||||
|
||||
const complete = () => {
|
||||
this.setTransitioning(false)
|
||||
this._element.classList.remove(ClassName.COLLAPSING)
|
||||
this._element.classList.add(ClassName.COLLAPSE)
|
||||
EventHandler.trigger(this._element, Event.HIDDEN)
|
||||
this._element.classList.remove(CLASS_NAME_COLLAPSING)
|
||||
this._element.classList.add(CLASS_NAME_COLLAPSE)
|
||||
EventHandler.trigger(this._element, EVENT_HIDDEN)
|
||||
}
|
||||
|
||||
this._element.style[dimension] = ''
|
||||
@@ -297,8 +289,8 @@ class Collapse {
|
||||
}
|
||||
|
||||
_getDimension() {
|
||||
const hasWidth = this._element.classList.contains(Dimension.WIDTH)
|
||||
return hasWidth ? Dimension.WIDTH : Dimension.HEIGHT
|
||||
const hasWidth = this._element.classList.contains(WIDTH)
|
||||
return hasWidth ? WIDTH : HEIGHT
|
||||
}
|
||||
|
||||
_getParent() {
|
||||
@@ -313,7 +305,7 @@ class Collapse {
|
||||
parent = SelectorEngine.findOne(parent)
|
||||
}
|
||||
|
||||
const selector = `${Selector.DATA_TOGGLE}[data-parent="${parent}"]`
|
||||
const selector = `${SELECTOR_DATA_TOGGLE}[data-parent="${parent}"]`
|
||||
|
||||
makeArray(SelectorEngine.find(selector, parent))
|
||||
.forEach(element => {
|
||||
@@ -330,14 +322,14 @@ class Collapse {
|
||||
|
||||
_addAriaAndCollapsedClass(element, triggerArray) {
|
||||
if (element) {
|
||||
const isOpen = element.classList.contains(ClassName.SHOW)
|
||||
const isOpen = element.classList.contains(CLASS_NAME_SHOW)
|
||||
|
||||
if (triggerArray.length) {
|
||||
triggerArray.forEach(elem => {
|
||||
if (isOpen) {
|
||||
elem.classList.remove(ClassName.COLLAPSED)
|
||||
elem.classList.remove(CLASS_NAME_COLLAPSED)
|
||||
} else {
|
||||
elem.classList.add(ClassName.COLLAPSED)
|
||||
elem.classList.add(CLASS_NAME_COLLAPSED)
|
||||
}
|
||||
|
||||
elem.setAttribute('aria-expanded', isOpen)
|
||||
@@ -390,7 +382,7 @@ class Collapse {
|
||||
* ------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
EventHandler.on(document, Event.CLICK_DATA_API, Selector.DATA_TOGGLE, function (event) {
|
||||
EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) {
|
||||
// preventDefault only for <a> elements (which change the URL) not inside the collapsible element
|
||||
if (event.target.tagName === 'A') {
|
||||
event.preventDefault()
|
||||
|
Reference in New Issue
Block a user