/** * 目录操作:主/备份目录来源切换(可复用封装) */ (function (global) { 'use strict'; const _registry = {}; const _rootState = {}; function _qs(id) { return document.getElementById(id); } function _cfg(opsId) { return _registry[opsId] || null; } function register(opsId, cfg) { _registry[opsId] = Object.assign({ mainValue: 'main', backupValue: 'backup', apiRootField: 'root', mainOnlyAttr: 'data-dir-main-only', backupBtnSelector: null, hintMain: '当前显示主目录。备份目录与「备份」功能写入的「上级目录 / backups / bak_文件夹名」镜像一致;切换后可只读查看备份内容。', hintBackup: '正在查看备份镜像目录。修改文件请切换回「主目录」。', blockedAlert: '当前为「备份目录」视图,仅可浏览、下载与在本地打开路径。请切换到「主目录」后再进行上传、删除、移动等操作。', }, cfg); if (!_rootState[opsId]) _rootState[opsId] = _registry[opsId].mainValue; } function isEnabled(opsId) { return !!_registry[opsId]; } function getRoot(opsId) { const cfg = _cfg(opsId); if (!cfg) return 'main'; const v = _rootState[opsId]; return v === cfg.backupValue ? cfg.backupValue : cfg.mainValue; } function isBackupView(opsId) { const cfg = _cfg(opsId); if (!cfg) return false; return getRoot(opsId) === cfg.backupValue; } function apiRootExtra(opsId) { const cfg = _cfg(opsId); if (!cfg) return {}; const out = {}; out[cfg.apiRootField || 'root'] = getRoot(opsId); return out; } function mainOnlyBlockedAlert(opsId) { const cfg = _cfg(opsId); alert((cfg && cfg.blockedAlert) || '备份目录视图下不可执行此操作,请切换到主目录。'); } function _updateHint(opsId) { const cfg = _cfg(opsId); if (!cfg || !cfg.hintId) return; const hint = _qs(cfg.hintId); if (!hint) return; hint.textContent = isBackupView(opsId) ? cfg.hintBackup : cfg.hintMain; } function syncRadios(opsId) { const cfg = _cfg(opsId); if (!cfg || !cfg.radioName) return; const v = getRoot(opsId); const target = v === cfg.backupValue ? cfg.backupValue : cfg.mainValue; const r = document.querySelector('input[name="' + cfg.radioName + '"][value="' + target + '"]'); if (r) r.checked = true; } function updateMainOnlyButtons(opsId) { const cfg = _cfg(opsId); if (!cfg || !cfg.modalId) return; const ro = isBackupView(opsId); const modal = _qs(cfg.modalId); if (!modal) return; const attr = cfg.mainOnlyAttr || 'data-project-dir-main-only'; modal.querySelectorAll('[' + attr + '="1"]').forEach(function (btn) { btn.disabled = ro; }); } function resetToMain(opsId) { const cfg = _cfg(opsId); if (!cfg) return; _rootState[opsId] = cfg.mainValue; syncRadios(opsId); _updateHint(opsId); updateMainOnlyButtons(opsId); } function prepareModalOpen(opsId) { resetToMain(opsId); } function onChange(opsId) { const cfg = _cfg(opsId); if (!cfg || !cfg.radioName) return; const el = document.querySelector('input[name="' + cfg.radioName + '"]:checked'); _rootState[opsId] = el && el.value === cfg.backupValue ? cfg.backupValue : cfg.mainValue; updateMainOnlyButtons(opsId); _updateHint(opsId); if (global.IARDirOpsColumns) { IARDirOpsColumns.invoke(opsId, 'refresh'); } } function setBackupProgress(opsId, visible, text) { const cfg = _cfg(opsId); if (!cfg) return; const progressKey = cfg.progressWrapId || opsId; if (global.IARProgress) { if (!cfg._iarProgressBound) { IARProgress.bind(progressKey, { wrapId: cfg.progressWrapId, msgId: cfg.progressTextId, barId: cfg.progressWrapId ? (cfg.progressWrapId + 'Bar') : undefined, }); cfg._iarProgressBound = true; } if (visible) { IARProgress.startAuto(progressKey, { message: text || '正在备份,请稍候…', }); } else { IARProgress.stopAuto(progressKey); IARProgress.hide(progressKey); } } else { const wrap = cfg.progressWrapId ? _qs(cfg.progressWrapId) : null; const txt = cfg.progressTextId ? _qs(cfg.progressTextId) : null; if (wrap) wrap.classList.toggle('d-none', !visible); if (txt && text) txt.textContent = text; } if (cfg.backupBtnSelector) { document.querySelectorAll(cfg.backupBtnSelector).forEach(function (btn) { btn.disabled = !!visible; }); } if (!visible) updateMainOnlyButtons(opsId); } function guardWrite(opsId) { if (!isBackupView(opsId)) return true; mainOnlyBlockedAlert(opsId); return false; } function invoke(opsId, method) { const args = Array.prototype.slice.call(arguments, 2); const fn = { getRoot: getRoot, isBackupView: isBackupView, onChange: onChange, resetToMain: resetToMain, prepareModalOpen: prepareModalOpen, syncRadios: syncRadios, updateMainOnlyButtons: updateMainOnlyButtons, setBackupProgress: setBackupProgress, mainOnlyBlockedAlert: mainOnlyBlockedAlert, guardWrite: guardWrite, apiRootExtra: apiRootExtra, }[method]; if (!fn) { console.error('IARDirOpsDirSource: unknown', opsId, method); return; } return fn.apply(null, [opsId].concat(args)); } global.IARDirOpsDirSource = { register: register, isEnabled: isEnabled, getRoot: getRoot, isBackupView: isBackupView, apiRootExtra: apiRootExtra, onChange: onChange, resetToMain: resetToMain, prepareModalOpen: prepareModalOpen, syncRadios: syncRadios, updateMainOnlyButtons: updateMainOnlyButtons, setBackupProgress: setBackupProgress, mainOnlyBlockedAlert: mainOnlyBlockedAlert, guardWrite: guardWrite, invoke: invoke, }; })(typeof window !== 'undefined' ? window : this);