0
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-03-15 15:29:22 +01:00

rewrite toast plugin without jquery

This commit is contained in:
Johann-S 2018-11-14 12:02:18 +01:00 committed by XhmikosR
parent 5e068eeda9
commit 661db08eeb
4 changed files with 80 additions and 40 deletions

View File

@ -136,6 +136,23 @@ function getConfigByPluginKey(pluginKey) {
} }
} }
} }
if (pluginKey === 'Toast') {
return {
external: [
bsPlugins.Data,
bsPlugins.EventHandler,
bsPlugins.Manipulator,
bsPlugins.Util
],
globals: {
[bsPlugins.Data]: 'Data',
[bsPlugins.EventHandler]: 'EventHandler',
[bsPlugins.Manipulator]: 'Manipulator',
[bsPlugins.Util]: 'Util'
}
}
}
} }
function build(plugin) { function build(plugin) {

View File

@ -5,7 +5,9 @@
* -------------------------------------------------------------------------- * --------------------------------------------------------------------------
*/ */
import $ from 'jquery' import Data from './dom/data'
import EventHandler from './dom/eventHandler'
import Manipulator from './dom/manipulator'
import Util from './util' import Util from './util'
/** /**
@ -14,11 +16,10 @@ import Util from './util'
* ------------------------------------------------------------------------ * ------------------------------------------------------------------------
*/ */
const NAME = 'toast' const NAME = 'toast'
const VERSION = '4.3.1' const VERSION = '4.3.1'
const DATA_KEY = 'bs.toast' const DATA_KEY = 'bs.toast'
const EVENT_KEY = `.${DATA_KEY}` const EVENT_KEY = `.${DATA_KEY}`
const JQUERY_NO_CONFLICT = $.fn[NAME]
const Event = { const Event = {
CLICK_DISMISS : `click.dismiss${EVENT_KEY}`, CLICK_DISMISS : `click.dismiss${EVENT_KEY}`,
@ -63,6 +64,7 @@ class Toast {
this._config = this._getConfig(config) this._config = this._getConfig(config)
this._timeout = null this._timeout = null
this._setListeners() this._setListeners()
Data.setData(element, DATA_KEY, this)
} }
// Getters // Getters
@ -82,7 +84,7 @@ class Toast {
// Public // Public
show() { show() {
$(this._element).trigger(Event.SHOW) EventHandler.trigger(this._element, Event.SHOW)
if (this._config.animation) { if (this._config.animation) {
this._element.classList.add(ClassName.FADE) this._element.classList.add(ClassName.FADE)
@ -92,7 +94,7 @@ class Toast {
this._element.classList.remove(ClassName.SHOWING) this._element.classList.remove(ClassName.SHOWING)
this._element.classList.add(ClassName.SHOW) this._element.classList.add(ClassName.SHOW)
$(this._element).trigger(Event.SHOWN) EventHandler.trigger(this._element, Event.SHOWN)
if (this._config.autohide) { if (this._config.autohide) {
this.hide() this.hide()
@ -104,9 +106,8 @@ class Toast {
if (this._config.animation) { if (this._config.animation) {
const transitionDuration = Util.getTransitionDurationFromElement(this._element) const transitionDuration = Util.getTransitionDurationFromElement(this._element)
$(this._element) EventHandler.one(this._element, Util.TRANSITION_END, complete)
.one(Util.TRANSITION_END, complete) Util.emulateTransitionEnd(this._element, transitionDuration)
.emulateTransitionEnd(transitionDuration)
} else { } else {
complete() complete()
} }
@ -117,7 +118,7 @@ class Toast {
return return
} }
$(this._element).trigger(Event.HIDE) EventHandler.trigger(this._element, Event.HIDE)
if (withoutTimeout) { if (withoutTimeout) {
this._close() this._close()
@ -136,9 +137,9 @@ class Toast {
this._element.classList.remove(ClassName.SHOW) this._element.classList.remove(ClassName.SHOW)
} }
$(this._element).off(Event.CLICK_DISMISS) EventHandler.off(this._element, Event.CLICK_DISMISS)
Data.removeData(this._element, DATA_KEY)
$.removeData(this._element, DATA_KEY)
this._element = null this._element = null
this._config = null this._config = null
} }
@ -148,7 +149,7 @@ class Toast {
_getConfig(config) { _getConfig(config) {
config = { config = {
...Default, ...Default,
...$(this._element).data(), ...Manipulator.getDataAttributes(this._element),
...typeof config === 'object' && config ? config : {} ...typeof config === 'object' && config ? config : {}
} }
@ -162,7 +163,8 @@ class Toast {
} }
_setListeners() { _setListeners() {
$(this._element).on( EventHandler.on(
this._element,
Event.CLICK_DISMISS, Event.CLICK_DISMISS,
Selector.DATA_DISMISS, Selector.DATA_DISMISS,
() => this.hide(true) () => this.hide(true)
@ -172,16 +174,15 @@ class Toast {
_close() { _close() {
const complete = () => { const complete = () => {
this._element.classList.add(ClassName.HIDE) this._element.classList.add(ClassName.HIDE)
$(this._element).trigger(Event.HIDDEN) EventHandler.trigger(this._element, Event.HIDDEN)
} }
this._element.classList.remove(ClassName.SHOW) this._element.classList.remove(ClassName.SHOW)
if (this._config.animation) { if (this._config.animation) {
const transitionDuration = Util.getTransitionDurationFromElement(this._element) const transitionDuration = Util.getTransitionDurationFromElement(this._element)
$(this._element) EventHandler.one(this._element, Util.TRANSITION_END, complete)
.one(Util.TRANSITION_END, complete) Util.emulateTransitionEnd(this._element, transitionDuration)
.emulateTransitionEnd(transitionDuration)
} else { } else {
complete() complete()
} }
@ -191,13 +192,11 @@ class Toast {
static _jQueryInterface(config) { static _jQueryInterface(config) {
return this.each(function () { return this.each(function () {
const $element = $(this) let data = Data.getData(this, DATA_KEY)
let data = $element.data(DATA_KEY)
const _config = typeof config === 'object' && config const _config = typeof config === 'object' && config
if (!data) { if (!data) {
data = new Toast(this, _config) data = new Toast(this, _config)
$element.data(DATA_KEY, data)
} }
if (typeof config === 'string') { if (typeof config === 'string') {
@ -209,19 +208,28 @@ class Toast {
} }
}) })
} }
static _getInstance(element) {
return Data.getData(element, DATA_KEY)
}
} }
/** /**
* ------------------------------------------------------------------------ * ------------------------------------------------------------------------
* jQuery * jQuery
* ------------------------------------------------------------------------ * ------------------------------------------------------------------------
* add .toast to jQuery only if jQuery is present
*/ */
$.fn[NAME] = Toast._jQueryInterface const $ = Util.jQuery
$.fn[NAME].Constructor = Toast if (typeof $ !== 'undefined') {
$.fn[NAME].noConflict = () => { const JQUERY_NO_CONFLICT = $.fn[NAME]
$.fn[NAME] = JQUERY_NO_CONFLICT $.fn[NAME] = Toast._jQueryInterface
return Toast._jQueryInterface $.fn[NAME].Constructor = Toast
$.fn[NAME].noConflict = () => {
$.fn[NAME] = JQUERY_NO_CONFLICT
return Toast._jQueryInterface
}
} }
export default Toast export default Toast

View File

@ -146,11 +146,11 @@ $(function () {
.bootstrapToast() .bootstrapToast()
.appendTo($('#qunit-fixture')) .appendTo($('#qunit-fixture'))
assert.ok(typeof $toast.data('bs.toast') !== 'undefined') assert.ok(typeof Toast._getInstance($toast[0]) !== 'undefined')
$toast.bootstrapToast('dispose') $toast.bootstrapToast('dispose')
assert.ok(typeof $toast.data('bs.toast') === 'undefined') assert.ok(Toast._getInstance($toast[0]) === null)
}) })
QUnit.test('should allow to destroy toast and hide it before that', function (assert) { QUnit.test('should allow to destroy toast and hide it before that', function (assert) {
@ -171,11 +171,11 @@ $(function () {
$toast.one('shown.bs.toast', function () { $toast.one('shown.bs.toast', function () {
setTimeout(function () { setTimeout(function () {
assert.ok($toast.hasClass('show')) assert.ok($toast.hasClass('show'))
assert.ok(typeof $toast.data('bs.toast') !== 'undefined') assert.ok(typeof Toast._getInstance($toast[0]) !== 'undefined')
$toast.bootstrapToast('dispose') $toast.bootstrapToast('dispose')
assert.ok(typeof $toast.data('bs.toast') === 'undefined') assert.ok(Toast._getInstance($toast[0]) === null)
assert.ok($toast.hasClass('show') === false) assert.ok($toast.hasClass('show') === false)
done() done()

View File

@ -52,19 +52,34 @@
</div> </div>
</div> </div>
<script src="../../dist/dom/polyfill.js"></script>
<script src="../../dist/util.js"></script> <script src="../../dist/util.js"></script>
<script src="../../dist/dom/manipulator.js"></script>
<script src="../../dist/dom/data.js"></script>
<script src="../../dist/dom/eventHandler.js"></script>
<script src="../../dist/toast.js"></script> <script src="../../dist/toast.js"></script>
<script> <script>
$(function () { window.addEventListener('load', function () {
$('.toast').toast() Util.makeArray(document.querySelectorAll('.toast'))
.forEach(function (toastNode) {
new Toast(toastNode)
})
$('#btnShowToast').on('click', function () { document.getElementById('btnShowToast').addEventListener('click', function () {
$('.toast').toast('show') Util.makeArray(document.querySelectorAll('.toast'))
}) .forEach(function (toastNode) {
var toast = Toast._getInstance(toastNode)
toast.show()
})
})
$('#btnHideToast').on('click', function () { document.getElementById('btnHideToast').addEventListener('click', function () {
$('.toast').toast('hide') Util.makeArray(document.querySelectorAll('.toast'))
}) .forEach(function (toastNode) {
var toast = Toast._getInstance(toastNode)
toast.hide()
})
})
}) })
</script> </script>
</body> </body>