/** * 统一进度条(实时百分比 + 「第X条/共Y条」) * 配合 templates/macros/progress.html、static/css/iar_progress.css * 默认:progress-bar-striped;进行中(pct<100 / indeterminate)progress-bar-animated; * 仅当 options.striped===false / options.animated===false 时关闭。 * * IARProgress.show(id, { message, percent, indeterminate, color, current, total, unit }) * IARProgress.set(id, { message, percent, indeterminate, countText, itemsText, current, total }) * IARProgress.setItems(id, current, total, { detail, message, label, unit }) * → 右侧实时提示「第X条/共Y条」,条宽 = current/total,条上显示 N% * IARProgress.done(id, { message, hideAfterMs, keepVisible, current, total, unit }) * IARProgress.hide(id) * IARProgress.isVisible(id) * IARProgress.bind(id, { wrapId, msgId, barId, doneId, countId, itemsId }) * IARProgress.formatItemsLabel(current, total, unit) * IARProgress.startAuto(id, { message, maxPercent, intervalMs, step }) * IARProgress.stopAuto(id) */ (function (global) { 'use strict'; var _bindings = Object.create(null); var _hideTimers = Object.create(null); var _autoTimers = Object.create(null); var _autoState = Object.create(null); function _qs(id) { return id ? document.getElementById(id) : null; } function _clearHideTimer(id) { if (_hideTimers[id]) { clearTimeout(_hideTimers[id]); delete _hideTimers[id]; } } function _clearAutoTimer(id) { if (_autoTimers[id]) { clearInterval(_autoTimers[id]); delete _autoTimers[id]; } delete _autoState[id]; } function bind(id, opts) { opts = opts || {}; var key = String(id || '').trim(); if (!key) return null; _bindings[key] = { wrapId: opts.wrapId || (key + 'ProgressWrap'), msgId: opts.msgId || opts.textId || (key + 'ProgressMsg'), barId: opts.barId || (key + 'ProgressBar'), doneId: opts.doneId || (key + 'ProgressDone'), countId: opts.countId || (key + 'ProgressCount'), itemsId: opts.itemsId || (key + 'ProgressItems'), }; return _bindings[key]; } function _resolve(id) { var key = String(id || '').trim(); if (!key) return null; if (_bindings[key]) return { id: key, ids: _bindings[key] }; var wrapByData = document.querySelector('[data-iar-progress-id="' + key + '"]'); if (wrapByData && wrapByData.id) { var auto = { wrapId: wrapByData.id, msgId: (wrapByData.querySelector('.iar-progress-msg') || {}).id || (key + 'ProgressMsg'), barId: (wrapByData.querySelector('.iar-progress-bar') || {}).id || (key + 'ProgressBar'), doneId: (wrapByData.querySelector('.iar-progress-done') || {}).id || (key + 'ProgressDone'), countId: (wrapByData.querySelector('.iar-progress-count') || {}).id || (key + 'ProgressCount'), itemsId: (wrapByData.querySelector('.iar-progress-items') || {}).id || (key + 'ProgressItems'), }; _bindings[key] = auto; return { id: key, ids: auto }; } var guessed = { wrapId: key + 'ProgressWrap', msgId: key + 'ProgressMsg', barId: key + 'ProgressBar', doneId: key + 'ProgressDone', countId: key + 'ProgressCount', itemsId: key + 'ProgressItems', }; if (!_qs(guessed.wrapId) && _qs(key + 'ProgressText')) { guessed.msgId = key + 'ProgressText'; } if (!_qs(guessed.wrapId)) { if (_qs(key)) { guessed.wrapId = key; var wrapEl = _qs(key); var msgEl = wrapEl.querySelector('.iar-progress-msg, [id$="ProgressMsg"], [id$="ProgressText"]'); var barEl = wrapEl.querySelector('.iar-progress-bar, .progress-bar'); var doneEl = wrapEl.querySelector('.iar-progress-done, [id$="DoneMsg"], [id$="ProgressDone"]'); var countEl = wrapEl.querySelector('.iar-progress-count, [id$="ProgressCount"]'); var itemsEl = wrapEl.querySelector('.iar-progress-items, [id$="ProgressItems"]'); if (msgEl && msgEl.id) guessed.msgId = msgEl.id; if (barEl && barEl.id) guessed.barId = barEl.id; if (doneEl && doneEl.id) guessed.doneId = doneEl.id; if (countEl && countEl.id) guessed.countId = countEl.id; if (itemsEl && itemsEl.id) guessed.itemsId = itemsEl.id; } } _bindings[key] = guessed; return { id: key, ids: guessed }; } function _els(id) { var ref = _resolve(id); if (!ref) return null; return { id: ref.id, wrap: _qs(ref.ids.wrapId), msg: _qs(ref.ids.msgId), bar: _qs(ref.ids.barId), done: _qs(ref.ids.doneId), count: _qs(ref.ids.countId), items: _qs(ref.ids.itemsId), }; } function _showPercentEnabled(wrap) { if (!wrap) return true; var flag = wrap.getAttribute('data-iar-progress-show-percent'); if (flag == null || flag === '') return true; return flag !== '0' && flag !== 'false'; } function _showItemsEnabled(wrap) { if (!wrap) return true; var flag = wrap.getAttribute('data-iar-progress-show-items'); if (flag == null || flag === '') return true; return flag !== '0' && flag !== 'false'; } function _itemUnit(wrap, override) { if (override != null && String(override).trim() !== '') return String(override).trim(); if (wrap) { var u = wrap.getAttribute('data-iar-progress-item-unit'); if (u != null && String(u).trim() !== '') return String(u).trim(); } return '条'; } function _setVisible(wrap, visible) { if (!wrap) return; wrap.classList.toggle('d-none', !visible); wrap.classList.toggle('hidden', !visible); if (visible) { wrap.removeAttribute('hidden'); } else { wrap.setAttribute('hidden', 'hidden'); } } function _applyBar(bar, opts, wrap) { if (!bar) return; opts = opts || {}; var indeterminate = opts.indeterminate; if (indeterminate == null) { if (opts.percent != null && !isNaN(Number(opts.percent))) { indeterminate = false; } else { indeterminate = bar.getAttribute('data-iar-progress-indeterminate') === '1'; } } var pct = opts.percent; if (pct == null || isNaN(Number(pct))) { pct = indeterminate ? 100 : 0; } pct = Math.max(0, Math.min(100, Math.round(Number(pct)))); var showPct = opts.showPercent; if (showPct == null) showPct = _showPercentEnabled(wrap); // 默认条纹;仅显式 striped: false 时取消 var wantStriped = opts.striped !== false; // 进行中(不确定进度,或未到 100%)默认动画;仅显式 animated: false 时取消 var inProgress = !!indeterminate || pct < 100; var wantAnimated = opts.animated === true || (opts.animated !== false && inProgress); if (indeterminate) { bar.classList.add('progress-bar-striped', 'progress-bar-animated', 'is-indeterminate'); if (!wantStriped) bar.classList.remove('progress-bar-striped'); if (!wantAnimated) bar.classList.remove('progress-bar-animated'); bar.setAttribute('data-iar-progress-indeterminate', '1'); bar.style.width = '100%'; bar.setAttribute('aria-valuenow', '100'); bar.textContent = ''; } else { bar.classList.remove('is-indeterminate'); bar.setAttribute('data-iar-progress-indeterminate', '0'); bar.style.width = pct + '%'; bar.setAttribute('aria-valuenow', String(pct)); bar.classList.toggle('progress-bar-striped', wantStriped); bar.classList.toggle('progress-bar-animated', wantAnimated); if (showPct) { bar.textContent = pct + '%'; bar.classList.toggle('iar-progress-bar-empty', pct < 8); } else { bar.textContent = ''; bar.classList.remove('iar-progress-bar-empty'); } } if (opts.color) { bar.className = bar.className .split(/\s+/) .filter(function (c) { return c && !/^bg-/.test(c); }) .join(' '); bar.classList.add('progress-bar', 'iar-progress-bar', 'bg-' + opts.color); if (wantStriped) bar.classList.add('progress-bar-striped'); if (wantAnimated) bar.classList.add('progress-bar-animated'); if (indeterminate) bar.classList.add('is-indeterminate'); } } /** 「第X条/共Y条」;unit 默认「条」,可传「项」「行」「文件」等 */ function formatItemsLabel(current, total, unit) { var cur = Math.max(0, Number(current) || 0); var tot = Math.max(0, Number(total) || 0); var u = (unit != null && String(unit).trim() !== '') ? String(unit).trim() : '条'; return '第' + cur + u + '/共' + tot + u; } function set(id, options) { options = options || {}; var els = _els(id); if (!els || !els.wrap) return false; if (options.message != null && els.msg) { els.msg.textContent = String(options.message); } var hasItems = options.current != null && options.total != null; var unit = _itemUnit(els.wrap, options.unit); var itemsLabel = null; if (options.itemsText != null) { itemsLabel = String(options.itemsText); } else if (hasItems) { itemsLabel = formatItemsLabel(options.current, options.total, unit); } if (els.items && _showItemsEnabled(els.wrap)) { if (itemsLabel != null) { els.items.textContent = itemsLabel; els.items.classList.remove('d-none'); } else if (options.clearItems) { els.items.textContent = ''; } } var pctForCount = options.percent; if (pctForCount != null && !isNaN(Number(pctForCount))) { pctForCount = Math.max(0, Math.min(100, Math.round(Number(pctForCount)))); } else { pctForCount = null; } if (els.count) { if (options.countText != null) { els.count.textContent = String(options.countText); } else if (els.items && _showItemsEnabled(els.wrap) && itemsLabel != null) { // 有独立「第X条/共Y条」时,count 仅显示百分比 if ( options.indeterminate !== true && pctForCount != null && _showPercentEnabled(els.wrap) && options.syncCountPercent !== false ) { els.count.textContent = pctForCount + '%'; } } else if (hasItems) { // 无 items 节点:count 显示「第X条/共Y条」或带百分比 if ( pctForCount != null && _showPercentEnabled(els.wrap) && options.includePercentInCount !== false ) { els.count.textContent = itemsLabel + ' · ' + pctForCount + '%'; } else { els.count.textContent = itemsLabel; } } else if ( options.indeterminate !== true && pctForCount != null && _showPercentEnabled(els.wrap) && options.syncCountPercent !== false ) { els.count.textContent = pctForCount + '%'; } } if (els.done && options.hideDone !== false && options.done !== true) { els.done.classList.add('d-none'); } _applyBar(els.bar, options, els.wrap); return true; } /** * 按条目进度实时更新:「第 current 条/共 total 条」,条宽 = current/total。 * options.detail / options.label 追加到主文案;options.message 可完全覆盖主文案。 * options.unit 覆盖计量单位(默认宏上的 item_unit,一般为「条」)。 */ function setItems(id, current, total, options) { options = options || {}; var els = _els(id); var unit = _itemUnit(els && els.wrap, options.unit); var tot = Math.max(0, Number(total) || 0); var cur = Math.max(0, Number(current) || 0); if (tot <= 0) { // 总数未知时仍展示「第0条/共0条」,便于实时提示占位 if (els && els.wrap) { _clearHideTimer(els.id); if (els.done) els.done.classList.add('d-none'); _setVisible(els.wrap, true); } return set(id, { message: options.message || '请稍候…', current: 0, total: 0, unit: unit, percent: 0, indeterminate: false, color: options.color, }); } cur = Math.min(cur, tot); var pct = Math.round((cur / tot) * 100); var label = formatItemsLabel(cur, tot, unit); var message = options.message; if (message == null) { message = label; if (options.detail) message += ' · ' + String(options.detail); else if (options.label) message += ' · ' + String(options.label); } if (els && els.wrap && !isVisible(id)) { _clearHideTimer(els.id); if (els.done) els.done.classList.add('d-none'); _setVisible(els.wrap, true); } return set(id, { message: message, current: cur, total: tot, unit: unit, percent: pct, indeterminate: false, animated: options.animated !== false && cur < tot, color: options.color, syncCountPercent: true, }); } function show(id, options) { options = options || {}; var els = _els(id); if (!els || !els.wrap) return false; _clearHideTimer(els.id); if (els.done) els.done.classList.add('d-none'); if (els.count && options.countText == null && options.current == null && options.percent == null) { els.count.textContent = ''; } if (els.items && options.current == null && options.total == null && options.itemsText == null) { els.items.textContent = ''; } _setVisible(els.wrap, true); if (options.current != null && options.total != null) { return setItems(id, options.current, options.total, options); } if (options.indeterminate == null && options.percent == null) { options.percent = 0; options.indeterminate = false; } set(els.id, options); return true; } function startAuto(id, options) { options = options || {}; var key = String(id || '').trim(); if (!key) return false; _clearAutoTimer(key); var maxPct = options.maxPercent != null ? Number(options.maxPercent) : 90; maxPct = Math.max(1, Math.min(99, maxPct)); var step = options.step != null ? Number(options.step) : 2; step = Math.max(0.5, step); var intervalMs = options.intervalMs != null ? Number(options.intervalMs) : 400; intervalMs = Math.max(100, intervalMs); var startPct = options.percent != null ? Number(options.percent) : 0; startPct = Math.max(0, Math.min(maxPct, startPct)); show(key, { message: options.message, percent: startPct, indeterminate: false, color: options.color, current: options.current, total: options.total, unit: options.unit, }); _autoState[key] = { percent: startPct, maxPercent: maxPct, message: options.message }; _autoTimers[key] = setInterval(function () { var st = _autoState[key]; if (!st) return; if (st.percent >= st.maxPercent) return; var remain = st.maxPercent - st.percent; var inc = Math.min(step, Math.max(0.3, remain * 0.08 + step * 0.35)); st.percent = Math.min(st.maxPercent, st.percent + inc); set(key, { message: st.message != null ? st.message : undefined, percent: st.percent, indeterminate: false, animated: true, }); }, intervalMs); return true; } function stopAuto(id) { var key = String(id || '').trim(); if (!key) return false; _clearAutoTimer(key); return true; } function done(id, options) { options = options || {}; var els = _els(id); if (!els || !els.wrap) return false; _clearHideTimer(els.id); _clearAutoTimer(els.id); _setVisible(els.wrap, true); if (options.message != null && els.msg) { els.msg.textContent = String(options.message); } var unit = _itemUnit(els.wrap, options.unit); if (options.total != null || options.current != null) { var tot = Math.max(0, Number(options.total) || 0); var cur = options.current != null ? Number(options.current) : tot; var itemsLabel = formatItemsLabel(cur, tot, unit); if (els.items && _showItemsEnabled(els.wrap)) { els.items.textContent = itemsLabel; els.items.classList.remove('d-none'); if (els.count && _showPercentEnabled(els.wrap)) { els.count.textContent = '100%'; } } else if (els.count) { els.count.textContent = itemsLabel + ' · 100%'; } } else if (els.count) { if (options.countText != null) { els.count.textContent = String(options.countText); } else if (_showPercentEnabled(els.wrap)) { els.count.textContent = '100%'; } } _applyBar(els.bar, { percent: 100, indeterminate: false, animated: false, striped: options.striped !== false, color: options.color, showPercent: options.showPercent, }, els.wrap); if (els.bar) { els.bar.classList.remove('progress-bar-animated'); } if (els.done) { if (options.doneMessage != null) { var icon = els.done.querySelector('i.fas, i.far'); els.done.innerHTML = (icon ? icon.outerHTML + ' ' : ' ') + String(options.doneMessage); } els.done.classList.remove('d-none'); } var hideAfter = options.hideAfterMs; if (hideAfter == null && options.keepVisible) hideAfter = 0; if (hideAfter == null) hideAfter = 0; if (hideAfter > 0) { _hideTimers[els.id] = setTimeout(function () { hide(els.id); _hideTimers[els.id] = null; }, hideAfter); } return true; } function hide(id) { var els = _els(id); if (!els || !els.wrap) return false; _clearHideTimer(els.id); _clearAutoTimer(els.id); _setVisible(els.wrap, false); if (els.done) els.done.classList.add('d-none'); if (els.count) els.count.textContent = ''; if (els.items) els.items.textContent = ''; if (els.bar) { els.bar.textContent = ''; els.bar.classList.remove('iar-progress-bar-empty'); } return true; } function isVisible(id) { var els = _els(id); if (!els || !els.wrap) return false; if (els.wrap.hasAttribute('hidden')) return false; if (els.wrap.classList.contains('d-none') || els.wrap.classList.contains('hidden')) return false; return true; } function showBusy(id, message) { return show(id, { message: message || '请稍候…', indeterminate: true }); } global.IARProgress = { bind: bind, show: show, set: set, setItems: setItems, formatItemsLabel: formatItemsLabel, startAuto: startAuto, stopAuto: stopAuto, done: done, hide: hide, isVisible: isVisible, showBusy: showBusy, }; })(typeof window !== 'undefined' ? window : this);