aboutsummaryrefslogtreecommitdiff
path: root/js/src/modal.js
diff options
context:
space:
mode:
authorPierre-Denis Vanduynslager <[email protected]>2016-12-28 19:57:38 -0500
committerPierre-Denis Vanduynslager <[email protected]>2016-12-28 19:57:38 -0500
commit425d156df27fa6c18e979aa000bfe5a346ee3450 (patch)
tree4157dfcbdf8334e9d9fb2bb239f4ae78706bbc71 /js/src/modal.js
parentab2fc63d08b8c53d6f29bcfd73b7f2d5ceaacacd (diff)
parente1e621be046a4541a2fd36e445015ee44de3c67e (diff)
downloadbootstrap-425d156df27fa6c18e979aa000bfe5a346ee3450.tar.xz
bootstrap-425d156df27fa6c18e979aa000bfe5a346ee3450.zip
Merge branch 'twbs/v4-dev' into dropdown-keyboard
Diffstat (limited to 'js/src/modal.js')
-rw-r--r--js/src/modal.js98
1 files changed, 56 insertions, 42 deletions
diff --git a/js/src/modal.js b/js/src/modal.js
index f52af09a2..94abd19f4 100644
--- a/js/src/modal.js
+++ b/js/src/modal.js
@@ -3,7 +3,7 @@ import Util from './util'
/**
* --------------------------------------------------------------------------
- * Bootstrap (v4.0.0-alpha.2): modal.js
+ * Bootstrap (v4.0.0-alpha.5): modal.js
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* --------------------------------------------------------------------------
*/
@@ -18,13 +18,14 @@ const Modal = (($) => {
*/
const NAME = 'modal'
- const VERSION = '4.0.0-alpha.2'
+ const VERSION = '4.0.0-alpha.5'
const DATA_KEY = 'bs.modal'
const EVENT_KEY = `.${DATA_KEY}`
const DATA_API_KEY = '.data-api'
const JQUERY_NO_CONFLICT = $.fn[NAME]
const TRANSITION_DURATION = 300
const BACKDROP_TRANSITION_DURATION = 150
+ const ESCAPE_KEYCODE = 27 // KeyboardEvent.which value for Escape (Esc) key
const Default = {
backdrop : true,
@@ -59,7 +60,7 @@ const Modal = (($) => {
BACKDROP : 'modal-backdrop',
OPEN : 'modal-open',
FADE : 'fade',
- IN : 'in'
+ SHOW : 'show'
}
const Selector = {
@@ -86,6 +87,7 @@ const Modal = (($) => {
this._isShown = false
this._isBodyOverflowing = false
this._ignoreBackdropClick = false
+ this._isTransitioning = false
this._originalBodyPadding = 0
this._scrollbarWidth = 0
}
@@ -109,7 +111,15 @@ const Modal = (($) => {
}
show(relatedTarget) {
- let showEvent = $.Event(Event.SHOW, {
+ if (this._isTransitioning) {
+ throw new Error('Modal is transitioning')
+ }
+
+ if (Util.supportsTransitionEnd() &&
+ $(this._element).hasClass(ClassName.FADE)) {
+ this._isTransitioning = true
+ }
+ const showEvent = $.Event(Event.SHOW, {
relatedTarget
})
@@ -132,7 +142,7 @@ const Modal = (($) => {
$(this._element).on(
Event.CLICK_DISMISS,
Selector.DATA_DISMISS,
- $.proxy(this.hide, this)
+ (event) => this.hide(event)
)
$(this._dialog).on(Event.MOUSEDOWN_DISMISS, () => {
@@ -143,9 +153,7 @@ const Modal = (($) => {
})
})
- this._showBackdrop(
- $.proxy(this._showElement, this, relatedTarget)
- )
+ this._showBackdrop(() => this._showElement(relatedTarget))
}
hide(event) {
@@ -153,8 +161,17 @@ const Modal = (($) => {
event.preventDefault()
}
- let hideEvent = $.Event(Event.HIDE)
+ if (this._isTransitioning) {
+ throw new Error('Modal is transitioning')
+ }
+ const transition = Util.supportsTransitionEnd() &&
+ $(this._element).hasClass(ClassName.FADE)
+ if (transition) {
+ this._isTransitioning = true
+ }
+
+ const hideEvent = $.Event(Event.HIDE)
$(this._element).trigger(hideEvent)
if (!this._isShown || hideEvent.isDefaultPrevented()) {
@@ -168,16 +185,14 @@ const Modal = (($) => {
$(document).off(Event.FOCUSIN)
- $(this._element).removeClass(ClassName.IN)
+ $(this._element).removeClass(ClassName.SHOW)
$(this._element).off(Event.CLICK_DISMISS)
$(this._dialog).off(Event.MOUSEDOWN_DISMISS)
- if (Util.supportsTransitionEnd() &&
- ($(this._element).hasClass(ClassName.FADE))) {
-
+ if (transition) {
$(this._element)
- .one(Util.TRANSITION_END, $.proxy(this._hideModal, this))
+ .one(Util.TRANSITION_END, (event) => this._hideModal(event))
.emulateTransitionEnd(TRANSITION_DURATION)
} else {
this._hideModal()
@@ -187,10 +202,7 @@ const Modal = (($) => {
dispose() {
$.removeData(this._element, DATA_KEY)
- $(window).off(EVENT_KEY)
- $(document).off(EVENT_KEY)
- $(this._element).off(EVENT_KEY)
- $(this._backdrop).off(EVENT_KEY)
+ $(window, document, this._element, this._backdrop).off(EVENT_KEY)
this._config = null
this._element = null
@@ -213,11 +225,11 @@ const Modal = (($) => {
}
_showElement(relatedTarget) {
- let transition = Util.supportsTransitionEnd() &&
+ const transition = Util.supportsTransitionEnd() &&
$(this._element).hasClass(ClassName.FADE)
if (!this._element.parentNode ||
- (this._element.parentNode.nodeType !== Node.ELEMENT_NODE)) {
+ this._element.parentNode.nodeType !== Node.ELEMENT_NODE) {
// don't move modals dom position
document.body.appendChild(this._element)
}
@@ -230,20 +242,21 @@ const Modal = (($) => {
Util.reflow(this._element)
}
- $(this._element).addClass(ClassName.IN)
+ $(this._element).addClass(ClassName.SHOW)
if (this._config.focus) {
this._enforceFocus()
}
- let shownEvent = $.Event(Event.SHOWN, {
+ const shownEvent = $.Event(Event.SHOWN, {
relatedTarget
})
- let transitionComplete = () => {
+ const transitionComplete = () => {
if (this._config.focus) {
this._element.focus()
}
+ this._isTransitioning = false
$(this._element).trigger(shownEvent)
}
@@ -262,7 +275,7 @@ const Modal = (($) => {
.on(Event.FOCUSIN, (event) => {
if (document !== event.target &&
this._element !== event.target &&
- (!$(this._element).has(event.target).length)) {
+ !$(this._element).has(event.target).length) {
this._element.focus()
}
})
@@ -271,7 +284,7 @@ const Modal = (($) => {
_setEscapeEvent() {
if (this._isShown && this._config.keyboard) {
$(this._element).on(Event.KEYDOWN_DISMISS, (event) => {
- if (event.which === 27) {
+ if (event.which === ESCAPE_KEYCODE) {
this.hide()
}
})
@@ -283,7 +296,7 @@ const Modal = (($) => {
_setResizeEvent() {
if (this._isShown) {
- $(window).on(Event.RESIZE, $.proxy(this._handleUpdate, this))
+ $(window).on(Event.RESIZE, (event) => this._handleUpdate(event))
} else {
$(window).off(Event.RESIZE)
}
@@ -292,6 +305,7 @@ const Modal = (($) => {
_hideModal() {
this._element.style.display = 'none'
this._element.setAttribute('aria-hidden', 'true')
+ this._isTransitioning = false
this._showBackdrop(() => {
$(document.body).removeClass(ClassName.OPEN)
this._resetAdjustments()
@@ -308,11 +322,11 @@ const Modal = (($) => {
}
_showBackdrop(callback) {
- let animate = $(this._element).hasClass(ClassName.FADE) ?
+ const animate = $(this._element).hasClass(ClassName.FADE) ?
ClassName.FADE : ''
if (this._isShown && this._config.backdrop) {
- let doAnimate = Util.supportsTransitionEnd() && animate
+ const doAnimate = Util.supportsTransitionEnd() && animate
this._backdrop = document.createElement('div')
this._backdrop.className = ClassName.BACKDROP
@@ -342,7 +356,7 @@ const Modal = (($) => {
Util.reflow(this._backdrop)
}
- $(this._backdrop).addClass(ClassName.IN)
+ $(this._backdrop).addClass(ClassName.SHOW)
if (!callback) {
return
@@ -358,9 +372,9 @@ const Modal = (($) => {
.emulateTransitionEnd(BACKDROP_TRANSITION_DURATION)
} else if (!this._isShown && this._backdrop) {
- $(this._backdrop).removeClass(ClassName.IN)
+ $(this._backdrop).removeClass(ClassName.SHOW)
- let callbackRemove = () => {
+ const callbackRemove = () => {
this._removeBackdrop()
if (callback) {
callback()
@@ -368,7 +382,7 @@ const Modal = (($) => {
}
if (Util.supportsTransitionEnd() &&
- ($(this._element).hasClass(ClassName.FADE))) {
+ $(this._element).hasClass(ClassName.FADE)) {
$(this._backdrop)
.one(Util.TRANSITION_END, callbackRemove)
.emulateTransitionEnd(BACKDROP_TRANSITION_DURATION)
@@ -392,7 +406,7 @@ const Modal = (($) => {
}
_adjustDialog() {
- let isModalOverflowing =
+ const isModalOverflowing =
this._element.scrollHeight > document.documentElement.clientHeight
if (!this._isBodyOverflowing && isModalOverflowing) {
@@ -415,7 +429,7 @@ const Modal = (($) => {
}
_setScrollbar() {
- let bodyPadding = parseInt(
+ const bodyPadding = parseInt(
$(Selector.FIXED_CONTENT).css('padding-right') || 0,
10
)
@@ -433,10 +447,10 @@ const Modal = (($) => {
}
_getScrollbarWidth() { // thx d.walsh
- let scrollDiv = document.createElement('div')
+ const scrollDiv = document.createElement('div')
scrollDiv.className = ClassName.SCROLLBAR_MEASURER
document.body.appendChild(scrollDiv)
- let scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth
+ const scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth
document.body.removeChild(scrollDiv)
return scrollbarWidth
}
@@ -446,8 +460,8 @@ const Modal = (($) => {
static _jQueryInterface(config, relatedTarget) {
return this.each(function () {
- let data = $(this).data(DATA_KEY)
- let _config = $.extend(
+ let data = $(this).data(DATA_KEY)
+ const _config = $.extend(
{},
Modal.Default,
$(this).data(),
@@ -481,20 +495,20 @@ const Modal = (($) => {
$(document).on(Event.CLICK_DATA_API, Selector.DATA_TOGGLE, function (event) {
let target
- let selector = Util.getSelectorFromElement(this)
+ const selector = Util.getSelectorFromElement(this)
if (selector) {
target = $(selector)[0]
}
- let config = $(target).data(DATA_KEY) ?
+ const config = $(target).data(DATA_KEY) ?
'toggle' : $.extend({}, $(target).data(), $(this).data())
- if (this.tagName === 'A') {
+ if (this.tagName === 'A' || this.tagName === 'AREA') {
event.preventDefault()
}
- let $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