mirror of
https://github.com/twbs/bootstrap.git
synced 2025-10-01 15:56:45 +02:00
Switch to string constants. (#30490)
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 22.09 kB 21.13 kB -0.96 kB (-4.35 %) bootstrap.min.js 15.4 kB 14.46 kB -0.94 kB (-3.86 %)
This commit is contained in:
152
js/src/modal.js
152
js/src/modal.js
@@ -36,39 +36,33 @@ const DefaultType = {
|
||||
show : 'boolean'
|
||||
}
|
||||
|
||||
const Event = {
|
||||
HIDE : `hide${EVENT_KEY}`,
|
||||
HIDE_PREVENTED : `hidePrevented${EVENT_KEY}`,
|
||||
HIDDEN : `hidden${EVENT_KEY}`,
|
||||
SHOW : `show${EVENT_KEY}`,
|
||||
SHOWN : `shown${EVENT_KEY}`,
|
||||
FOCUSIN : `focusin${EVENT_KEY}`,
|
||||
RESIZE : `resize${EVENT_KEY}`,
|
||||
CLICK_DISMISS : `click.dismiss${EVENT_KEY}`,
|
||||
KEYDOWN_DISMISS : `keydown.dismiss${EVENT_KEY}`,
|
||||
MOUSEUP_DISMISS : `mouseup.dismiss${EVENT_KEY}`,
|
||||
MOUSEDOWN_DISMISS : `mousedown.dismiss${EVENT_KEY}`,
|
||||
CLICK_DATA_API : `click${EVENT_KEY}${DATA_API_KEY}`
|
||||
}
|
||||
const EVENT_HIDE = `hide${EVENT_KEY}`
|
||||
const EVENT_HIDE_PREVENTED = `hidePrevented${EVENT_KEY}`
|
||||
const EVENT_HIDDEN = `hidden${EVENT_KEY}`
|
||||
const EVENT_SHOW = `show${EVENT_KEY}`
|
||||
const EVENT_SHOWN = `shown${EVENT_KEY}`
|
||||
const EVENT_FOCUSIN = `focusin${EVENT_KEY}`
|
||||
const EVENT_RESIZE = `resize${EVENT_KEY}`
|
||||
const EVENT_CLICK_DISMISS = `click.dismiss${EVENT_KEY}`
|
||||
const EVENT_KEYDOWN_DISMISS = `keydown.dismiss${EVENT_KEY}`
|
||||
const EVENT_MOUSEUP_DISMISS = `mouseup.dismiss${EVENT_KEY}`
|
||||
const EVENT_MOUSEDOWN_DISMISS = `mousedown.dismiss${EVENT_KEY}`
|
||||
const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`
|
||||
|
||||
const ClassName = {
|
||||
SCROLLABLE : 'modal-dialog-scrollable',
|
||||
SCROLLBAR_MEASURER : 'modal-scrollbar-measure',
|
||||
BACKDROP : 'modal-backdrop',
|
||||
OPEN : 'modal-open',
|
||||
FADE : 'fade',
|
||||
SHOW : 'show',
|
||||
STATIC : 'modal-static'
|
||||
}
|
||||
const CLASS_NAME_SCROLLABLE = 'modal-dialog-scrollable'
|
||||
const CLASS_NAME_SCROLLBAR_MEASURER = 'modal-scrollbar-measure'
|
||||
const CLASS_NAME_BACKDROP = 'modal-backdrop'
|
||||
const CLASS_NAME_OPEN = 'modal-open'
|
||||
const CLASS_NAME_FADE = 'fade'
|
||||
const CLASS_NAME_SHOW = 'show'
|
||||
const CLASS_NAME_STATIC = 'modal-static'
|
||||
|
||||
const Selector = {
|
||||
DIALOG : '.modal-dialog',
|
||||
MODAL_BODY : '.modal-body',
|
||||
DATA_TOGGLE : '[data-toggle="modal"]',
|
||||
DATA_DISMISS : '[data-dismiss="modal"]',
|
||||
FIXED_CONTENT : '.fixed-top, .fixed-bottom, .is-fixed, .sticky-top',
|
||||
STICKY_CONTENT : '.sticky-top'
|
||||
}
|
||||
const SELECTOR_DIALOG = '.modal-dialog'
|
||||
const SELECTOR_MODAL_BODY = '.modal-body'
|
||||
const SELECTOR_DATA_TOGGLE = '[data-toggle="modal"]'
|
||||
const SELECTOR_DATA_DISMISS = '[data-dismiss="modal"]'
|
||||
const SELECTOR_FIXED_CONTENT = '.fixed-top, .fixed-bottom, .is-fixed, .sticky-top'
|
||||
const SELECTOR_STICKY_CONTENT = '.sticky-top'
|
||||
|
||||
/**
|
||||
* ------------------------------------------------------------------------
|
||||
@@ -80,7 +74,7 @@ class Modal {
|
||||
constructor(element, config) {
|
||||
this._config = this._getConfig(config)
|
||||
this._element = element
|
||||
this._dialog = element.querySelector(Selector.DIALOG)
|
||||
this._dialog = element.querySelector(SELECTOR_DIALOG)
|
||||
this._backdrop = null
|
||||
this._isShown = false
|
||||
this._isBodyOverflowing = false
|
||||
@@ -110,11 +104,11 @@ class Modal {
|
||||
return
|
||||
}
|
||||
|
||||
if ($(this._element).hasClass(ClassName.FADE)) {
|
||||
if ($(this._element).hasClass(CLASS_NAME_FADE)) {
|
||||
this._isTransitioning = true
|
||||
}
|
||||
|
||||
const showEvent = $.Event(Event.SHOW, {
|
||||
const showEvent = $.Event(EVENT_SHOW, {
|
||||
relatedTarget
|
||||
})
|
||||
|
||||
@@ -135,13 +129,13 @@ class Modal {
|
||||
this._setResizeEvent()
|
||||
|
||||
$(this._element).on(
|
||||
Event.CLICK_DISMISS,
|
||||
Selector.DATA_DISMISS,
|
||||
EVENT_CLICK_DISMISS,
|
||||
SELECTOR_DATA_DISMISS,
|
||||
(event) => this.hide(event)
|
||||
)
|
||||
|
||||
$(this._dialog).on(Event.MOUSEDOWN_DISMISS, () => {
|
||||
$(this._element).one(Event.MOUSEUP_DISMISS, (event) => {
|
||||
$(this._dialog).on(EVENT_MOUSEDOWN_DISMISS, () => {
|
||||
$(this._element).one(EVENT_MOUSEUP_DISMISS, (event) => {
|
||||
if ($(event.target).is(this._element)) {
|
||||
this._ignoreBackdropClick = true
|
||||
}
|
||||
@@ -160,7 +154,7 @@ class Modal {
|
||||
return
|
||||
}
|
||||
|
||||
const hideEvent = $.Event(Event.HIDE)
|
||||
const hideEvent = $.Event(EVENT_HIDE)
|
||||
|
||||
$(this._element).trigger(hideEvent)
|
||||
|
||||
@@ -169,7 +163,7 @@ class Modal {
|
||||
}
|
||||
|
||||
this._isShown = false
|
||||
const transition = $(this._element).hasClass(ClassName.FADE)
|
||||
const transition = $(this._element).hasClass(CLASS_NAME_FADE)
|
||||
|
||||
if (transition) {
|
||||
this._isTransitioning = true
|
||||
@@ -178,12 +172,12 @@ class Modal {
|
||||
this._setEscapeEvent()
|
||||
this._setResizeEvent()
|
||||
|
||||
$(document).off(Event.FOCUSIN)
|
||||
$(document).off(EVENT_FOCUSIN)
|
||||
|
||||
$(this._element).removeClass(ClassName.SHOW)
|
||||
$(this._element).removeClass(CLASS_NAME_SHOW)
|
||||
|
||||
$(this._element).off(Event.CLICK_DISMISS)
|
||||
$(this._dialog).off(Event.MOUSEDOWN_DISMISS)
|
||||
$(this._element).off(EVENT_CLICK_DISMISS)
|
||||
$(this._dialog).off(EVENT_MOUSEDOWN_DISMISS)
|
||||
|
||||
|
||||
if (transition) {
|
||||
@@ -202,11 +196,11 @@ class Modal {
|
||||
.forEach((htmlElement) => $(htmlElement).off(EVENT_KEY))
|
||||
|
||||
/**
|
||||
* `document` has 2 events `Event.FOCUSIN` and `Event.CLICK_DATA_API`
|
||||
* `document` has 2 events `EVENT_FOCUSIN` and `EVENT_CLICK_DATA_API`
|
||||
* Do not move `document` in `htmlElements` array
|
||||
* It will remove `Event.CLICK_DATA_API` event that should remain
|
||||
* It will remove `EVENT_CLICK_DATA_API` event that should remain
|
||||
*/
|
||||
$(document).off(Event.FOCUSIN)
|
||||
$(document).off(EVENT_FOCUSIN)
|
||||
|
||||
$.removeData(this._element, DATA_KEY)
|
||||
|
||||
@@ -238,19 +232,19 @@ class Modal {
|
||||
|
||||
_triggerBackdropTransition() {
|
||||
if (this._config.backdrop === 'static') {
|
||||
const hideEventPrevented = $.Event(Event.HIDE_PREVENTED)
|
||||
const hideEventPrevented = $.Event(EVENT_HIDE_PREVENTED)
|
||||
|
||||
$(this._element).trigger(hideEventPrevented)
|
||||
if (hideEventPrevented.defaultPrevented) {
|
||||
return
|
||||
}
|
||||
|
||||
this._element.classList.add(ClassName.STATIC)
|
||||
this._element.classList.add(CLASS_NAME_STATIC)
|
||||
|
||||
const modalTransitionDuration = Util.getTransitionDurationFromElement(this._element)
|
||||
|
||||
$(this._element).one(Util.TRANSITION_END, () => {
|
||||
this._element.classList.remove(ClassName.STATIC)
|
||||
this._element.classList.remove(CLASS_NAME_STATIC)
|
||||
})
|
||||
.emulateTransitionEnd(modalTransitionDuration)
|
||||
this._element.focus()
|
||||
@@ -260,8 +254,8 @@ class Modal {
|
||||
}
|
||||
|
||||
_showElement(relatedTarget) {
|
||||
const transition = $(this._element).hasClass(ClassName.FADE)
|
||||
const modalBody = this._dialog ? this._dialog.querySelector(Selector.MODAL_BODY) : null
|
||||
const transition = $(this._element).hasClass(CLASS_NAME_FADE)
|
||||
const modalBody = this._dialog ? this._dialog.querySelector(SELECTOR_MODAL_BODY) : null
|
||||
|
||||
if (!this._element.parentNode ||
|
||||
this._element.parentNode.nodeType !== Node.ELEMENT_NODE) {
|
||||
@@ -273,7 +267,7 @@ class Modal {
|
||||
this._element.removeAttribute('aria-hidden')
|
||||
this._element.setAttribute('aria-modal', true)
|
||||
|
||||
if ($(this._dialog).hasClass(ClassName.SCROLLABLE) && modalBody) {
|
||||
if ($(this._dialog).hasClass(CLASS_NAME_SCROLLABLE) && modalBody) {
|
||||
modalBody.scrollTop = 0
|
||||
} else {
|
||||
this._element.scrollTop = 0
|
||||
@@ -283,13 +277,13 @@ class Modal {
|
||||
Util.reflow(this._element)
|
||||
}
|
||||
|
||||
$(this._element).addClass(ClassName.SHOW)
|
||||
$(this._element).addClass(CLASS_NAME_SHOW)
|
||||
|
||||
if (this._config.focus) {
|
||||
this._enforceFocus()
|
||||
}
|
||||
|
||||
const shownEvent = $.Event(Event.SHOWN, {
|
||||
const shownEvent = $.Event(EVENT_SHOWN, {
|
||||
relatedTarget
|
||||
})
|
||||
|
||||
@@ -314,8 +308,8 @@ class Modal {
|
||||
|
||||
_enforceFocus() {
|
||||
$(document)
|
||||
.off(Event.FOCUSIN) // Guard against infinite focus loop
|
||||
.on(Event.FOCUSIN, (event) => {
|
||||
.off(EVENT_FOCUSIN) // Guard against infinite focus loop
|
||||
.on(EVENT_FOCUSIN, (event) => {
|
||||
if (document !== event.target &&
|
||||
this._element !== event.target &&
|
||||
$(this._element).has(event.target).length === 0) {
|
||||
@@ -326,7 +320,7 @@ class Modal {
|
||||
|
||||
_setEscapeEvent() {
|
||||
if (this._isShown) {
|
||||
$(this._element).on(Event.KEYDOWN_DISMISS, (event) => {
|
||||
$(this._element).on(EVENT_KEYDOWN_DISMISS, (event) => {
|
||||
if (this._config.keyboard && event.which === ESCAPE_KEYCODE) {
|
||||
event.preventDefault()
|
||||
this.hide()
|
||||
@@ -335,15 +329,15 @@ class Modal {
|
||||
}
|
||||
})
|
||||
} else if (!this._isShown) {
|
||||
$(this._element).off(Event.KEYDOWN_DISMISS)
|
||||
$(this._element).off(EVENT_KEYDOWN_DISMISS)
|
||||
}
|
||||
}
|
||||
|
||||
_setResizeEvent() {
|
||||
if (this._isShown) {
|
||||
$(window).on(Event.RESIZE, (event) => this.handleUpdate(event))
|
||||
$(window).on(EVENT_RESIZE, (event) => this.handleUpdate(event))
|
||||
} else {
|
||||
$(window).off(Event.RESIZE)
|
||||
$(window).off(EVENT_RESIZE)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -353,10 +347,10 @@ class Modal {
|
||||
this._element.removeAttribute('aria-modal')
|
||||
this._isTransitioning = false
|
||||
this._showBackdrop(() => {
|
||||
$(document.body).removeClass(ClassName.OPEN)
|
||||
$(document.body).removeClass(CLASS_NAME_OPEN)
|
||||
this._resetAdjustments()
|
||||
this._resetScrollbar()
|
||||
$(this._element).trigger(Event.HIDDEN)
|
||||
$(this._element).trigger(EVENT_HIDDEN)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -368,12 +362,12 @@ class Modal {
|
||||
}
|
||||
|
||||
_showBackdrop(callback) {
|
||||
const animate = $(this._element).hasClass(ClassName.FADE)
|
||||
? ClassName.FADE : ''
|
||||
const animate = $(this._element).hasClass(CLASS_NAME_FADE)
|
||||
? CLASS_NAME_FADE : ''
|
||||
|
||||
if (this._isShown && this._config.backdrop) {
|
||||
this._backdrop = document.createElement('div')
|
||||
this._backdrop.className = ClassName.BACKDROP
|
||||
this._backdrop.className = CLASS_NAME_BACKDROP
|
||||
|
||||
if (animate) {
|
||||
this._backdrop.classList.add(animate)
|
||||
@@ -381,7 +375,7 @@ class Modal {
|
||||
|
||||
$(this._backdrop).appendTo(document.body)
|
||||
|
||||
$(this._element).on(Event.CLICK_DISMISS, (event) => {
|
||||
$(this._element).on(EVENT_CLICK_DISMISS, (event) => {
|
||||
if (this._ignoreBackdropClick) {
|
||||
this._ignoreBackdropClick = false
|
||||
return
|
||||
@@ -397,7 +391,7 @@ class Modal {
|
||||
Util.reflow(this._backdrop)
|
||||
}
|
||||
|
||||
$(this._backdrop).addClass(ClassName.SHOW)
|
||||
$(this._backdrop).addClass(CLASS_NAME_SHOW)
|
||||
|
||||
if (!callback) {
|
||||
return
|
||||
@@ -414,7 +408,7 @@ class Modal {
|
||||
.one(Util.TRANSITION_END, callback)
|
||||
.emulateTransitionEnd(backdropTransitionDuration)
|
||||
} else if (!this._isShown && this._backdrop) {
|
||||
$(this._backdrop).removeClass(ClassName.SHOW)
|
||||
$(this._backdrop).removeClass(CLASS_NAME_SHOW)
|
||||
|
||||
const callbackRemove = () => {
|
||||
this._removeBackdrop()
|
||||
@@ -423,7 +417,7 @@ class Modal {
|
||||
}
|
||||
}
|
||||
|
||||
if ($(this._element).hasClass(ClassName.FADE)) {
|
||||
if ($(this._element).hasClass(CLASS_NAME_FADE)) {
|
||||
const backdropTransitionDuration = Util.getTransitionDurationFromElement(this._backdrop)
|
||||
|
||||
$(this._backdrop)
|
||||
@@ -470,8 +464,8 @@ class Modal {
|
||||
if (this._isBodyOverflowing) {
|
||||
// Note: DOMNode.style.paddingRight returns the actual value or '' if not set
|
||||
// while $(DOMNode).css('padding-right') returns the calculated value or 0 if not set
|
||||
const fixedContent = [].slice.call(document.querySelectorAll(Selector.FIXED_CONTENT))
|
||||
const stickyContent = [].slice.call(document.querySelectorAll(Selector.STICKY_CONTENT))
|
||||
const fixedContent = [].slice.call(document.querySelectorAll(SELECTOR_FIXED_CONTENT))
|
||||
const stickyContent = [].slice.call(document.querySelectorAll(SELECTOR_STICKY_CONTENT))
|
||||
|
||||
// Adjust fixed content padding
|
||||
$(fixedContent).each((index, element) => {
|
||||
@@ -499,12 +493,12 @@ class Modal {
|
||||
.css('padding-right', `${parseFloat(calculatedPadding) + this._scrollbarWidth}px`)
|
||||
}
|
||||
|
||||
$(document.body).addClass(ClassName.OPEN)
|
||||
$(document.body).addClass(CLASS_NAME_OPEN)
|
||||
}
|
||||
|
||||
_resetScrollbar() {
|
||||
// Restore fixed content padding
|
||||
const fixedContent = [].slice.call(document.querySelectorAll(Selector.FIXED_CONTENT))
|
||||
const fixedContent = [].slice.call(document.querySelectorAll(SELECTOR_FIXED_CONTENT))
|
||||
$(fixedContent).each((index, element) => {
|
||||
const padding = $(element).data('padding-right')
|
||||
$(element).removeData('padding-right')
|
||||
@@ -512,7 +506,7 @@ class Modal {
|
||||
})
|
||||
|
||||
// Restore sticky content
|
||||
const elements = [].slice.call(document.querySelectorAll(`${Selector.STICKY_CONTENT}`))
|
||||
const elements = [].slice.call(document.querySelectorAll(`${SELECTOR_STICKY_CONTENT}`))
|
||||
$(elements).each((index, element) => {
|
||||
const margin = $(element).data('margin-right')
|
||||
if (typeof margin !== 'undefined') {
|
||||
@@ -528,7 +522,7 @@ class Modal {
|
||||
|
||||
_getScrollbarWidth() { // thx d.walsh
|
||||
const scrollDiv = document.createElement('div')
|
||||
scrollDiv.className = ClassName.SCROLLBAR_MEASURER
|
||||
scrollDiv.className = CLASS_NAME_SCROLLBAR_MEASURER
|
||||
document.body.appendChild(scrollDiv)
|
||||
const scrollbarWidth = scrollDiv.getBoundingClientRect().width - scrollDiv.clientWidth
|
||||
document.body.removeChild(scrollDiv)
|
||||
@@ -569,7 +563,7 @@ class Modal {
|
||||
* ------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
$(document).on(Event.CLICK_DATA_API, Selector.DATA_TOGGLE, function (event) {
|
||||
$(document).on(EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) {
|
||||
let target
|
||||
const selector = Util.getSelectorFromElement(this)
|
||||
|
||||
@@ -587,13 +581,13 @@ $(document).on(Event.CLICK_DATA_API, Selector.DATA_TOGGLE, function (event) {
|
||||
event.preventDefault()
|
||||
}
|
||||
|
||||
const $target = $(target).one(Event.SHOW, (showEvent) => {
|
||||
const $target = $(target).one(EVENT_SHOW, (showEvent) => {
|
||||
if (showEvent.isDefaultPrevented()) {
|
||||
// Only register focus restorer if modal will actually get shown
|
||||
return
|
||||
}
|
||||
|
||||
$target.one(Event.HIDDEN, () => {
|
||||
$target.one(EVENT_HIDDEN, () => {
|
||||
if ($(this).is(':visible')) {
|
||||
this.focus()
|
||||
}
|
||||
|
Reference in New Issue
Block a user