toast.js 9.90 KiB
/*!
  * Bootstrap toast.js v5.0.0 (https://getbootstrap.com/)
  * Copyright 2011-2021 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
  * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
  */
(function (global, factory) {
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('./dom/data.js'), require('./dom/event-handler.js'), require('./dom/manipulator.js'), require('./base-component.js')) :
  typeof define === 'function' && define.amd ? define(['./dom/data', './dom/event-handler', './dom/manipulator', './base-component'], factory) :
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Toast = factory(global.Data, global.EventHandler, global.Manipulator, global.Base));
}(this, (function (Data, EventHandler, Manipulator, BaseComponent) { 'use strict';
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
  var Data__default = /*#__PURE__*/_interopDefaultLegacy(Data);
  var EventHandler__default = /*#__PURE__*/_interopDefaultLegacy(EventHandler);
  var Manipulator__default = /*#__PURE__*/_interopDefaultLegacy(Manipulator);
  var BaseComponent__default = /*#__PURE__*/_interopDefaultLegacy(BaseComponent);
  /**
   * --------------------------------------------------------------------------
   * Bootstrap (v5.0.0): util/index.js
   * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
   * --------------------------------------------------------------------------
  const MILLISECONDS_MULTIPLIER = 1000;
  const TRANSITION_END = 'transitionend'; // Shoutout AngusCroll (https://goo.gl/pxwQGp)
  const toType = obj => {
    if (obj === null || obj === undefined) {
      return `${obj}`;
    return {}.toString.call(obj).match(/\s([a-z]+)/i)[1].toLowerCase();
  const getTransitionDurationFromElement = element => {
    if (!element) {
      return 0;
    } // Get transition-duration of the element
    let {
      transitionDuration,
      transitionDelay
    } = window.getComputedStyle(element);
    const floatTransitionDuration = Number.parseFloat(transitionDuration);
    const floatTransitionDelay = Number.parseFloat(transitionDelay); // Return 0 if element or transition duration is not found
    if (!floatTransitionDuration && !floatTransitionDelay) {
      return 0;
    } // If multiple durations are defined, take the first
    transitionDuration = transitionDuration.split(',')[0];
    transitionDelay = transitionDelay.split(',')[0];
    return (Number.parseFloat(transitionDuration) + Number.parseFloat(transitionDelay)) * MILLISECONDS_MULTIPLIER;
  const triggerTransitionEnd = element => {
    element.dispatchEvent(new Event(TRANSITION_END));
  const isElement = obj => (obj[0] || obj).nodeType;
  const emulateTransitionEnd = (element, duration) => {
    let called = false;
    const durationPadding = 5;
    const emulatedDuration = duration + durationPadding;
    function listener() {
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
called = true; element.removeEventListener(TRANSITION_END, listener); } element.addEventListener(TRANSITION_END, listener); setTimeout(() => { if (!called) { triggerTransitionEnd(element); } }, emulatedDuration); }; const typeCheckConfig = (componentName, config, configTypes) => { Object.keys(configTypes).forEach(property => { const expectedTypes = configTypes[property]; const value = config[property]; const valueType = value && isElement(value) ? 'element' : toType(value); if (!new RegExp(expectedTypes).test(valueType)) { throw new TypeError(`${componentName.toUpperCase()}: Option "${property}" provided type "${valueType}" but expected type "${expectedTypes}".`); } }); }; const reflow = element => element.offsetHeight; const getjQuery = () => { const { jQuery } = window; if (jQuery && !document.body.hasAttribute('data-bs-no-jquery')) { return jQuery; } return null; }; const onDOMContentLoaded = callback => { if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', callback); } else { callback(); } }; const defineJQueryPlugin = (name, plugin) => { onDOMContentLoaded(() => { const $ = getjQuery(); /* istanbul ignore if */ if ($) { const JQUERY_NO_CONFLICT = $.fn[name]; $.fn[name] = plugin.jQueryInterface; $.fn[name].Constructor = plugin; $.fn[name].noConflict = () => { $.fn[name] = JQUERY_NO_CONFLICT; return plugin.jQueryInterface; }; } }); }; /** * -------------------------------------------------------------------------- * Bootstrap (v5.0.0): toast.js * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) * -------------------------------------------------------------------------- */
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
/** * ------------------------------------------------------------------------ * Constants * ------------------------------------------------------------------------ */ const NAME = 'toast'; const DATA_KEY = 'bs.toast'; const EVENT_KEY = `.${DATA_KEY}`; const EVENT_CLICK_DISMISS = `click.dismiss${EVENT_KEY}`; const EVENT_HIDE = `hide${EVENT_KEY}`; const EVENT_HIDDEN = `hidden${EVENT_KEY}`; const EVENT_SHOW = `show${EVENT_KEY}`; const EVENT_SHOWN = `shown${EVENT_KEY}`; const CLASS_NAME_FADE = 'fade'; const CLASS_NAME_HIDE = 'hide'; const CLASS_NAME_SHOW = 'show'; const CLASS_NAME_SHOWING = 'showing'; const DefaultType = { animation: 'boolean', autohide: 'boolean', delay: 'number' }; const Default = { animation: true, autohide: true, delay: 5000 }; const SELECTOR_DATA_DISMISS = '[data-bs-dismiss="toast"]'; /** * ------------------------------------------------------------------------ * Class Definition * ------------------------------------------------------------------------ */ class Toast extends BaseComponent__default['default'] { constructor(element, config) { super(element); this._config = this._getConfig(config); this._timeout = null; this._setListeners(); } // Getters static get DefaultType() { return DefaultType; } static get Default() { return Default; } static get DATA_KEY() { return DATA_KEY; } // Public show() { const showEvent = EventHandler__default['default'].trigger(this._element, EVENT_SHOW); if (showEvent.defaultPrevented) { return; } this._clearTimeout(); if (this._config.animation) { this._element.classList.add(CLASS_NAME_FADE); }
211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
const complete = () => { this._element.classList.remove(CLASS_NAME_SHOWING); this._element.classList.add(CLASS_NAME_SHOW); EventHandler__default['default'].trigger(this._element, EVENT_SHOWN); if (this._config.autohide) { this._timeout = setTimeout(() => { this.hide(); }, this._config.delay); } }; this._element.classList.remove(CLASS_NAME_HIDE); reflow(this._element); this._element.classList.add(CLASS_NAME_SHOWING); if (this._config.animation) { const transitionDuration = getTransitionDurationFromElement(this._element); EventHandler__default['default'].one(this._element, 'transitionend', complete); emulateTransitionEnd(this._element, transitionDuration); } else { complete(); } } hide() { if (!this._element.classList.contains(CLASS_NAME_SHOW)) { return; } const hideEvent = EventHandler__default['default'].trigger(this._element, EVENT_HIDE); if (hideEvent.defaultPrevented) { return; } const complete = () => { this._element.classList.add(CLASS_NAME_HIDE); EventHandler__default['default'].trigger(this._element, EVENT_HIDDEN); }; this._element.classList.remove(CLASS_NAME_SHOW); if (this._config.animation) { const transitionDuration = getTransitionDurationFromElement(this._element); EventHandler__default['default'].one(this._element, 'transitionend', complete); emulateTransitionEnd(this._element, transitionDuration); } else { complete(); } } dispose() { this._clearTimeout(); if (this._element.classList.contains(CLASS_NAME_SHOW)) { this._element.classList.remove(CLASS_NAME_SHOW); } super.dispose(); this._config = null; } // Private
281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
_getConfig(config) { config = { ...Default, ...Manipulator__default['default'].getDataAttributes(this._element), ...(typeof config === 'object' && config ? config : {}) }; typeCheckConfig(NAME, config, this.constructor.DefaultType); return config; } _setListeners() { EventHandler__default['default'].on(this._element, EVENT_CLICK_DISMISS, SELECTOR_DATA_DISMISS, () => this.hide()); } _clearTimeout() { clearTimeout(this._timeout); this._timeout = null; } // Static static jQueryInterface(config) { return this.each(function () { let data = Data__default['default'].get(this, DATA_KEY); const _config = typeof config === 'object' && config; if (!data) { data = new Toast(this, _config); } if (typeof config === 'string') { if (typeof data[config] === 'undefined') { throw new TypeError(`No method named "${config}"`); } data[config](this); } }); } } /** * ------------------------------------------------------------------------ * jQuery * ------------------------------------------------------------------------ * add .Toast to jQuery only if jQuery is present */ defineJQueryPlugin(NAME, Toast); return Toast; }))); //# sourceMappingURL=toast.js.map