diff options
author | Johann-S <johann.servoire@gmail.com> | 2018-11-14 12:16:56 +0300 |
---|---|---|
committer | Johann-S <johann.servoire@gmail.com> | 2018-11-14 12:54:50 +0300 |
commit | 9201a805101943f9ec088639d520d7d2874bbed1 (patch) | |
tree | b103dea4a9d52bac3bb849c6c14277f4000a1f9c /js/src/toast.js | |
parent | fab1dea92773e796a75ac4a2fadb645714ac80ce (diff) |
some cleaning and changes for readability
Diffstat (limited to 'js/src/toast.js')
-rw-r--r-- | js/src/toast.js | 334 |
1 files changed, 165 insertions, 169 deletions
diff --git a/js/src/toast.js b/js/src/toast.js index 878ffe0296..23d482d35f 100644 --- a/js/src/toast.js +++ b/js/src/toast.js @@ -1,6 +1,3 @@ -import $ from 'jquery' -import Util from './util' - /** * -------------------------------------------------------------------------- * Bootstrap (v4.1.3): toast.js @@ -8,215 +5,214 @@ import Util from './util' * -------------------------------------------------------------------------- */ -const Toast = (($) => { - /** - * ------------------------------------------------------------------------ - * Constants - * ------------------------------------------------------------------------ - */ - - const NAME = 'toast' - const VERSION = '4.1.3' - const DATA_KEY = 'bs.toast' - const EVENT_KEY = `.${DATA_KEY}` - const JQUERY_NO_CONFLICT = $.fn[NAME] - - const Event = { - CLICK_DISMISS : `click.dismiss${EVENT_KEY}`, - HIDE : `hide${EVENT_KEY}`, - HIDDEN : `hidden${EVENT_KEY}`, - SHOW : `show${EVENT_KEY}`, - SHOWN : `shown${EVENT_KEY}` - } +import $ from 'jquery' +import Util from './util' - const ClassName = { - FADE : 'fade', - HIDE : 'hide', - SHOW : 'show' - } +/** + * ------------------------------------------------------------------------ + * Constants + * ------------------------------------------------------------------------ + */ - const DefaultType = { - animation : 'boolean', - autohide : 'boolean', - delay : 'number' - } +const NAME = 'toast' +const VERSION = '4.1.3' +const DATA_KEY = 'bs.toast' +const EVENT_KEY = `.${DATA_KEY}` +const JQUERY_NO_CONFLICT = $.fn[NAME] + +const Event = { + CLICK_DISMISS : `click.dismiss${EVENT_KEY}`, + HIDE : `hide${EVENT_KEY}`, + HIDDEN : `hidden${EVENT_KEY}`, + SHOW : `show${EVENT_KEY}`, + SHOWN : `shown${EVENT_KEY}` +} + +const ClassName = { + FADE : 'fade', + HIDE : 'hide', + SHOW : 'show' +} + +const DefaultType = { + animation : 'boolean', + autohide : 'boolean', + delay : 'number' +} + +const Default = { + animation : true, + autohide : true, + delay : 500 +} + +const Selector = { + DATA_DISMISS : '[data-dismiss="toast"]' +} - const Default = { - animation : true, - autohide : true, - delay : 500 - } +/** + * ------------------------------------------------------------------------ + * Class Definition + * ------------------------------------------------------------------------ + */ - const Selector = { - DATA_DISMISS : '[data-dismiss="toast"]' +class Toast { + constructor(element, config) { + this._element = element + this._config = this._getConfig(config) + this._timeout = null + this._setListeners() } - /** - * ------------------------------------------------------------------------ - * Class Definition - * ------------------------------------------------------------------------ - */ - - class Toast { - constructor(element, config) { - this._element = element - this._config = this._getConfig(config) - this._timeout = null - this._setListeners() - } + // Getters - // Getters - - static get VERSION() { - return VERSION - } + static get VERSION() { + return VERSION + } - static get DefaultType() { - return DefaultType - } + static get DefaultType() { + return DefaultType + } - // Public + // Public - show() { - $(this._element).trigger(Event.SHOW) + show() { + $(this._element).trigger(Event.SHOW) - if (this._config.animation) { - this._element.classList.add(ClassName.FADE) - } + if (this._config.animation) { + this._element.classList.add(ClassName.FADE) + } - const complete = () => { - $(this._element).trigger(Event.SHOWN) + const complete = () => { + $(this._element).trigger(Event.SHOWN) - if (this._config.autohide) { - this.hide() - } + if (this._config.autohide) { + this.hide() } + } - this._element.classList.add(ClassName.SHOW) - if (this._config.animation) { - const transitionDuration = Util.getTransitionDurationFromElement(this._element) + this._element.classList.add(ClassName.SHOW) + if (this._config.animation) { + const transitionDuration = Util.getTransitionDurationFromElement(this._element) - $(this._element) - .one(Util.TRANSITION_END, complete) - .emulateTransitionEnd(transitionDuration) - } else { - complete() - } + $(this._element) + .one(Util.TRANSITION_END, complete) + .emulateTransitionEnd(transitionDuration) + } else { + complete() } + } - hide(withoutTimeout) { - if (!this._element.classList.contains(ClassName.SHOW)) { - return - } + hide(withoutTimeout) { + if (!this._element.classList.contains(ClassName.SHOW)) { + return + } - $(this._element).trigger(Event.HIDE) + $(this._element).trigger(Event.HIDE) - if (withoutTimeout) { + if (withoutTimeout) { + this._close() + } else { + this._timeout = setTimeout(() => { this._close() - } else { - this._timeout = setTimeout(() => { - this._close() - }, this._config.delay) - } + }, this._config.delay) } + } - dispose() { - clearTimeout(this._timeout) - this._timeout = null - - if (this._element.classList.contains(ClassName.SHOW)) { - this._element.classList.remove(ClassName.SHOW) - } - - $(this._element).off(Event.CLICK_DISMISS) + dispose() { + clearTimeout(this._timeout) + this._timeout = null - $.removeData(this._element, DATA_KEY) - this._element = null - this._config = null + if (this._element.classList.contains(ClassName.SHOW)) { + this._element.classList.remove(ClassName.SHOW) } - // Private - - _getConfig(config) { - config = { - ...Default, - ...$(this._element).data(), - ...typeof config === 'object' && config ? config : {} - } + $(this._element).off(Event.CLICK_DISMISS) - Util.typeCheckConfig( - NAME, - config, - this.constructor.DefaultType - ) + $.removeData(this._element, DATA_KEY) + this._element = null + this._config = null + } - return config - } + // Private - _setListeners() { - $(this._element).on( - Event.CLICK_DISMISS, - Selector.DATA_DISMISS, - () => this.hide(true) - ) + _getConfig(config) { + config = { + ...Default, + ...$(this._element).data(), + ...typeof config === 'object' && config ? config : {} } - _close() { - const complete = () => { - $(this._element).trigger(Event.HIDDEN) - } + Util.typeCheckConfig( + NAME, + config, + this.constructor.DefaultType + ) - this._element.classList.remove(ClassName.SHOW) + return config + } - if (this._config.animation) { - const transitionDuration = Util.getTransitionDurationFromElement(this._element) + _setListeners() { + $(this._element).on( + Event.CLICK_DISMISS, + Selector.DATA_DISMISS, + () => this.hide(true) + ) + } - $(this._element) - .one(Util.TRANSITION_END, complete) - .emulateTransitionEnd(transitionDuration) - } else { - complete() - } + _close() { + const complete = () => { + $(this._element).trigger(Event.HIDDEN) } - // Static + this._element.classList.remove(ClassName.SHOW) - static _jQueryInterface(config) { - return this.each(function () { - const $element = $(this) - let data = $element.data(DATA_KEY) - const _config = typeof config === 'object' && config + if (this._config.animation) { + const transitionDuration = Util.getTransitionDurationFromElement(this._element) - if (!data) { - data = new Toast(this, _config) - $element.data(DATA_KEY, data) - } + $(this._element) + .one(Util.TRANSITION_END, complete) + .emulateTransitionEnd(transitionDuration) + } else { + complete() + } + } - if (typeof config === 'string') { - if (typeof data[config] === 'undefined') { - throw new TypeError(`No method named "${config}"`) - } + // Static + + static _jQueryInterface(config) { + return this.each(function () { + const $element = $(this) + let data = $element.data(DATA_KEY) + const _config = typeof config === 'object' && config + + if (!data) { + data = new Toast(this, _config) + $element.data(DATA_KEY, data) + } - data[config](this) + if (typeof config === 'string') { + if (typeof data[config] === 'undefined') { + throw new TypeError(`No method named "${config}"`) } - }) - } - } - /** - * ------------------------------------------------------------------------ - * jQuery - * ------------------------------------------------------------------------ - */ - - $.fn[NAME] = Toast._jQueryInterface - $.fn[NAME].Constructor = Toast - $.fn[NAME].noConflict = () => { - $.fn[NAME] = JQUERY_NO_CONFLICT - return Toast._jQueryInterface + data[config](this) + } + }) } +} + +/** + * ------------------------------------------------------------------------ + * jQuery + * ------------------------------------------------------------------------ + */ - return Toast -})($) +$.fn[NAME] = Toast._jQueryInterface +$.fn[NAME].Constructor = Toast +$.fn[NAME].noConflict = () => { + $.fn[NAME] = JQUERY_NO_CONFLICT + return Toast._jQueryInterface +} export default Toast |