PageSourceSearch

https://rocokingdom.com/web/assets/js/components/toast.js?v=4758f89e

js rocokingdom.com collected 2026-10-04 07:06:48 UTC 2,552 bytes, 78 lines download raw bytes

1// 轻提示组件:底部胶囊浮层,节流防抖、自动消失、支持主动关闭。
2// 用法:
3//   window.showToast('提示文案')           // 显示 2000ms 后自动消失
4//   window.hideToast()                     // 主动关闭
5//   window.showToast('提示文案', 3000)     // 自定义显示时长(ms)
6;(function () {
7  var DEFAULT_DURATION = 2000
8  var FADE_DURATION = 250
9
10  var toastEl = null
11  var textEl = null
12  var toastLock = false
13  var showTimer = null
14  var hideTimer = null
15  var unlockTimer = null
16
17  // 懒加载查找 DOM,避免在 partial 未引入时报错。
18  function ensureNodes() {
19    if (toastEl) return toastEl
20    toastEl = document.querySelector('.js-site-toast')
21    if (!toastEl) return null
22    textEl = toastEl.querySelector('.js-site-toast-text')
23    return toastEl
24  }
25
26  // 清理所有定时器,便于重启或主动关闭。
27  function clearTimers() {
28    if (showTimer) { clearTimeout(showTimer); showTimer = null }
29    if (hideTimer) { clearTimeout(hideTimer); hideTimer = null }
30    if (unlockTimer) { clearTimeout(unlockTimer); unlockTimer = null }
31  }
32
33  // 主动关闭:立即触发淡出,并在过渡结束后解锁。
34  window.hideToast = function () {
35    if (!toastEl) return
36    clearTimers()
37    toastEl.classList.remove('is-active')
38    unlockTimer = setTimeout(function () {
39      toastLock = false
40    }, FADE_DURATION)
41  }
42
43  /**
44   * 显示轻提示。
45   * @param {string} text    提示文案;为空时使用默认文案。
46   * @param {number} duration 显示时长(ms),默认 2000。
47   */
48  window.showToast = function (text, duration) {
49    if (!ensureNodes() || !textEl) return
50    // 节流:提示显示期间再次调用直接丢弃,避免闪烁。
51    if (toastLock) return
52    toastLock = true
53
54    clearTimers()
55
56    // 用 textContent 防 XSS;undefined 时给默认文案。
57    textEl.textContent = (text === undefined || text === null || text === '')
58      ? 'Loading, please wait...'
59      : String(text)
60
61    // 下一帧激活,确保过渡生效。
62    window.requestAnimationFrame(function () {
63      toastEl.classList.add('is-active')
64    })
65
66    var showDuration = (typeof duration === 'number' && duration > 0) ? duration : DEFAULT_DURATION
67
68    // 到点淡出。
69    hideTimer = setTimeout(function () {
70      toastEl.classList.remove('is-active')
71    }, showDuration)
72
73    // 过渡结束后解锁并清理。
74    unlockTimer = setTimeout(function () {
75      toastLock = false
76    }, showDuration + FADE_DURATION)
77  }
78})()

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.