Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
toast.js221 linesDownload Raw Back to src
1/**2 * --------------------------------------------------------------------------3 * Bootstrap (v4.6.2): toast.js4 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)5 * --------------------------------------------------------------------------6 */7 8import $ from 'jquery'9import Util from './util'10 11/**12 * Constants13 */14 15const NAME = 'toast'16const VERSION = '4.6.2'17const DATA_KEY = 'bs.toast'18const EVENT_KEY = `.${DATA_KEY}`19const JQUERY_NO_CONFLICT = $.fn[NAME]20 21const CLASS_NAME_FADE = 'fade'22const CLASS_NAME_HIDE = 'hide'23const CLASS_NAME_SHOW = 'show'24const CLASS_NAME_SHOWING = 'showing'25 26const EVENT_CLICK_DISMISS = `click.dismiss${EVENT_KEY}`27const EVENT_HIDE = `hide${EVENT_KEY}`28const EVENT_HIDDEN = `hidden${EVENT_KEY}`29const EVENT_SHOW = `show${EVENT_KEY}`30const EVENT_SHOWN = `shown${EVENT_KEY}`31 32const SELECTOR_DATA_DISMISS = '[data-dismiss="toast"]'33 34const Default = {35  animation: true,36  autohide: true,37  delay: 50038}39 40const DefaultType = {41  animation: 'boolean',42  autohide: 'boolean',43  delay: 'number'44}45 46/**47 * Class definition48 */49 50class Toast {51  constructor(element, config) {52    this._element = element53    this._config = this._getConfig(config)54    this._timeout = null55    this._setListeners()56  }57 58  // Getters59  static get VERSION() {60    return VERSION61  }62 63  static get DefaultType() {64    return DefaultType65  }66 67  static get Default() {68    return Default69  }70 71  // Public72  show() {73    const showEvent = $.Event(EVENT_SHOW)74 75    $(this._element).trigger(showEvent)76    if (showEvent.isDefaultPrevented()) {77      return78    }79 80    this._clearTimeout()81 82    if (this._config.animation) {83      this._element.classList.add(CLASS_NAME_FADE)84    }85 86    const complete = () => {87      this._element.classList.remove(CLASS_NAME_SHOWING)88      this._element.classList.add(CLASS_NAME_SHOW)89 90      $(this._element).trigger(EVENT_SHOWN)91 92      if (this._config.autohide) {93        this._timeout = setTimeout(() => {94          this.hide()95        }, this._config.delay)96      }97    }98 99    this._element.classList.remove(CLASS_NAME_HIDE)100    Util.reflow(this._element)101    this._element.classList.add(CLASS_NAME_SHOWING)102    if (this._config.animation) {103      const transitionDuration = Util.getTransitionDurationFromElement(this._element)104 105      $(this._element)106        .one(Util.TRANSITION_END, complete)107        .emulateTransitionEnd(transitionDuration)108    } else {109      complete()110    }111  }112 113  hide() {114    if (!this._element.classList.contains(CLASS_NAME_SHOW)) {115      return116    }117 118    const hideEvent = $.Event(EVENT_HIDE)119 120    $(this._element).trigger(hideEvent)121    if (hideEvent.isDefaultPrevented()) {122      return123    }124 125    this._close()126  }127 128  dispose() {129    this._clearTimeout()130 131    if (this._element.classList.contains(CLASS_NAME_SHOW)) {132      this._element.classList.remove(CLASS_NAME_SHOW)133    }134 135    $(this._element).off(EVENT_CLICK_DISMISS)136 137    $.removeData(this._element, DATA_KEY)138    this._element = null139    this._config = null140  }141 142  // Private143  _getConfig(config) {144    config = {145      ...Default,146      ...$(this._element).data(),147      ...(typeof config === 'object' && config ? config : {})148    }149 150    Util.typeCheckConfig(151      NAME,152      config,153      this.constructor.DefaultType154    )155 156    return config157  }158 159  _setListeners() {160    $(this._element).on(EVENT_CLICK_DISMISS, SELECTOR_DATA_DISMISS, () => this.hide())161  }162 163  _close() {164    const complete = () => {165      this._element.classList.add(CLASS_NAME_HIDE)166      $(this._element).trigger(EVENT_HIDDEN)167    }168 169    this._element.classList.remove(CLASS_NAME_SHOW)170    if (this._config.animation) {171      const transitionDuration = Util.getTransitionDurationFromElement(this._element)172 173      $(this._element)174        .one(Util.TRANSITION_END, complete)175        .emulateTransitionEnd(transitionDuration)176    } else {177      complete()178    }179  }180 181  _clearTimeout() {182    clearTimeout(this._timeout)183    this._timeout = null184  }185 186  // Static187  static _jQueryInterface(config) {188    return this.each(function () {189      const $element = $(this)190      let data = $element.data(DATA_KEY)191      const _config = typeof config === 'object' && config192 193      if (!data) {194        data = new Toast(this, _config)195        $element.data(DATA_KEY, data)196      }197 198      if (typeof config === 'string') {199        if (typeof data[config] === 'undefined') {200          throw new TypeError(`No method named "${config}"`)201        }202 203        data[config](this)204      }205    })206  }207}208 209/**210 * jQuery211 */212 213$.fn[NAME] = Toast._jQueryInterface214$.fn[NAME].Constructor = Toast215$.fn[NAME].noConflict = () => {216  $.fn[NAME] = JQUERY_NO_CONFLICT217  return Toast._jQueryInterface218}219 220export default Toast221 
codekingpro/portable-devtools · Team Ai