(function () { 'use strict'; var preferredLabels = { 'fit-orders-multiplatform': '多平台销售订单', 'shopify-payouts': 'Payouts 打款记录', 'shopify-payout-detail': 'Payouts 打款记录明细' }; var tabs = []; var activeId = ''; function appWrap() { return document.getElementById('appWrap'); } function pageLabel(id) { var section = document.getElementById(id); var heading = section && section.querySelector('.section-title h2, h2'); var nav = document.querySelector('.nav[data-s="' + id + '"], .nav[data-sps="' + id + '"]'); return preferredLabels[id] || (heading && heading.textContent.trim()) || (nav && nav.textContent.trim()) || id; } function escapeHtml(value) { return String(value == null ? '' : value).replace(/[&<>"']/g, function (char) { return {'&': '&', '<': '<', '>': '>', '"': '"', "'": '''}[char]; }); } function uniqueText(items) { var seen = {}; return items.map(function (item) { return String(item || '').replace(/\s+/g, ' ').trim(); }) .filter(function (item) { if (!item || seen[item]) return false; seen[item] = true; return true; }); } function helpNotice(notice, section) { var text = String(notice.textContent || '').replace(/\s+/g, ' ').trim(); return !notice.id || section.id === 'mapping' || /^(说明|提示|同步说明|导入说明|唯一性规则)[::]/.test(text); } function preparePageHelp(section) { if (!section) return; section.querySelectorAll('.section-title p').forEach(function (item) { if (!item.classList.contains('workspace-help-source')) item.classList.add('workspace-help-source'); }); section.querySelectorAll('.notice').forEach(function (notice) { if (helpNotice(notice, section) && !notice.classList.contains('workspace-help-source')) { notice.classList.add('workspace-help-source'); } }); } function removeUniqueMappingUi() { var button = document.querySelector('#mapping button[onclick="showUniqueValues()"]'); if (button && button.parentElement) { var filterRow = button.closest('.row4'); button.parentElement.remove(); if (filterRow) { if (filterRow.classList.contains('row4')) filterRow.classList.remove('row4'); if (!filterRow.classList.contains('row3')) filterRow.classList.add('row3'); } } var grid = document.getElementById('jUnique'); grid = grid && grid.closest('.grid'); if (!grid) return; var heading = grid.previousElementSibling; if (heading && /唯一值/.test(heading.textContent || '')) heading.remove(); grid.remove(); } function prepareAllPages() { document.querySelectorAll('.section').forEach(function (section) { enhanceDataPage(section); preparePageHelp(section); }); removeUniqueMappingUi(); } function helpChips(items) { if (!items.length) return '当前页面没有可列出的字段'; return '
' + items.map(function (item) { return '' + escapeHtml(item) + ''; }).join('') + '
'; } function currentPageHelp() { var section = document.querySelector('main.main > .section.active'); if (!section) return; preparePageHelp(section); var descriptions = uniqueText(Array.prototype.map.call( section.querySelectorAll('.section-title p, .workspace-help-source'), function (item) { return item.textContent; } )); var fields = uniqueText(Array.prototype.map.call(section.querySelectorAll('label'), function (label) { var control = label.parentElement && label.parentElement.querySelector('select, input, textarea'); if (!control || control.tagName !== 'SELECT') return label.textContent; var values = uniqueText(Array.prototype.map.call(control.options || [], function (option) { return option.textContent; })).slice(0, 10); return label.textContent + (values.length ? ':' + values.join('、') : ''); })).slice(0, 36); var columns = uniqueText(Array.prototype.map.call(section.querySelectorAll('table thead th'), function (cell) { return cell.textContent; })).filter(function (item) { return item !== '操作'; }).slice(0, 40); var actions = uniqueText(Array.prototype.map.call( section.querySelectorAll(':scope > .section-title button, :scope > .card > button'), function (button) { return button.textContent; } )).slice(0, 20); var extra = { 'mapping': '映射关系的唯一性在保存时检查;页面不再单独展示“积加唯一值/金蝶唯一值”。冲突数据会在保存时直接提示。', 'shopify-payouts': 'Payout date 是 Shopify 打款日期;交易日期是该笔打款包含的交易范围;推送金蝶前可按店铺、日期、状态和单据编号筛选。', 'shopify-payout-detail': '明细来自当前 Payout 的 balance transactions;Type、Amount、Fee、Net 按映射配置生成金蝶分录。', 'fit-orders-multiplatform': '订单日期取 orderingMarketTime 市场订购时间;定时任务会滚动补拉最近 7 个数据日期,避免延迟导入遗漏。' }[section.id] || ''; var purpose = descriptions[0] || ('当前页面用于“' + pageLabel(section.id) + '”相关数据的查询、维护和处理。'); var rules = uniqueText(descriptions.slice(1).concat(extra ? [extra] : [])); var html = '
' + '

页面用途

' + escapeHtml(purpose) + '

' + (rules.length ? '

业务口径

' + rules.map(function (item) { return '

' + escapeHtml(item) + '

'; }).join('') + '
' : '') + '

查询与录入字段

' + helpChips(fields) + '
' + (columns.length ? '

明细结果字段

' + helpChips(columns) + '
' : '') + (actions.length ? '

页面操作

' + helpChips(actions) + '
' : '') + '
'; if (typeof window.openSimpleModal === 'function') window.openSimpleModal('帮助 · ' + pageLabel(section.id), html); } function enhanceDataPage(section) { if (!section || !section.querySelector('.table-wrap table')) return false; if (!section.classList.contains('k3-data-page')) section.classList.add('k3-data-page'); var pageInfo = section.querySelector('[id*="PageInfo"], [id$="Page"]'); var pager = pageInfo && (pageInfo.classList.contains('card') ? pageInfo : pageInfo.closest('.card')); if (!pager) { pager = Array.prototype.find.call(section.children, function (child) { return child.classList && child.classList.contains('card') && !child.classList.contains('table-wrap') && /上一页|下一页|每页/.test(child.textContent || ''); }); } if (pager && pager.closest('.section') === section) { if (!pager.classList.contains('k3-pager')) pager.classList.add('k3-pager'); if (!section.classList.contains('k3-has-pager')) section.classList.add('k3-has-pager'); } return true; } function isWorkspacePage(id) { var section = document.getElementById(id); if (!section || id === 'dashboard') return false; enhanceDataPage(section); return true; } function ensureGlobalHeader() { var app = appWrap(); var main = document.querySelector('main.main'); var top = document.querySelector('.top'); if (!app || !main || !top) return null; if (top.parentElement !== app) app.insertBefore(top, app.firstChild); var brand = document.querySelector('.side .brand, .top .brand'); if (brand) { brand.classList.add('global-brand'); var name = brand.querySelector('b'); if (name) name.textContent = '数据核对中台'; if (brand.parentElement !== top) top.insertBefore(brand, top.firstChild); } var titleBlock = Array.prototype.find.call(top.children, function (child) { return child !== brand && child.querySelector && child.querySelector('#title'); }); if (titleBlock) titleBlock.classList.add('global-page-title'); var auth = top.querySelector('.auth-bar'); if (auth && !auth.querySelector('.workspace-help-btn')) { var help = document.createElement('button'); help.type = 'button'; help.className = 'workspace-help-btn'; help.innerHTML = '📖'; help.title = '当前页面帮助'; help.setAttribute('aria-label', '当前页面帮助'); help.onclick = currentPageHelp; var bell = auth.querySelector('.bell-btn'); auth.insertBefore(help, bell || auth.firstChild); } return top; } function ensureTabBar() { var app = appWrap(); var top = ensureGlobalHeader(); if (!app || !top) return null; var bar = document.getElementById('workspaceTabs'); if (bar) return bar; bar = document.createElement('div'); bar.id = 'workspaceTabs'; bar.className = 'workspace-tabs'; bar.setAttribute('role', 'tablist'); top.insertAdjacentElement('afterend', bar); return bar; } function showDashboard() { var dashboard = document.querySelector('.nav[data-s="dashboard"]'); if (dashboard && dashboard.style.display !== 'none') dashboard.click(); } function renderTabs() { var bar = ensureTabBar(); if (!bar) return; bar.innerHTML = ''; var menu = document.createElement('button'); menu.type = 'button'; menu.className = 'workspace-menu-btn'; menu.innerHTML = '☰'; menu.title = '展开或收起左侧导航'; menu.setAttribute('aria-label', '展开或收起左侧导航'); var currentApp = appWrap(); var menuExpanded = currentApp && (currentApp.classList.contains('workspace-focus') ? currentApp.classList.contains('workspace-nav-open') : !currentApp.classList.contains('workspace-nav-collapsed')); menu.setAttribute('aria-expanded', menuExpanded ? 'true' : 'false'); menu.onclick = function (event) { event.stopPropagation(); var app = appWrap(); if (!app) return; var expanded; if (app.classList.contains('workspace-focus')) { expanded = app.classList.toggle('workspace-nav-open'); } else { expanded = !app.classList.toggle('workspace-nav-collapsed'); } menu.setAttribute('aria-expanded', expanded ? 'true' : 'false'); }; bar.appendChild(menu); var home = document.createElement('button'); home.type = 'button'; home.className = 'workspace-home-btn' + (activeId ? '' : ' active'); home.innerHTML = '⌂'; home.title = '总览驾驶舱'; home.setAttribute('aria-label', '总览驾驶舱'); home.onclick = showDashboard; bar.appendChild(home); tabs.forEach(function (id) { var tab = document.createElement('div'); tab.className = 'workspace-tab' + (id === activeId ? ' active' : ''); tab.setAttribute('role', 'tab'); tab.setAttribute('aria-selected', id === activeId ? 'true' : 'false'); tab.onclick = function () { openWorkspacePage(id); }; var label = document.createElement('span'); label.className = 'workspace-tab-label'; label.textContent = pageLabel(id); label.title = pageLabel(id); tab.appendChild(label); var close = document.createElement('button'); close.type = 'button'; close.className = 'workspace-tab-close'; close.innerHTML = '×'; close.title = '关闭页面'; close.setAttribute('aria-label', '关闭' + pageLabel(id)); close.onclick = function (event) { event.stopPropagation(); closeWorkspacePage(id); }; tab.appendChild(close); bar.appendChild(tab); }); } function activateWorkspacePage(id) { var app = appWrap(); if (!app || !isWorkspacePage(id)) return; var pageChanged = activeId !== id; var enteringWorkspace = !app.classList.contains('workspace-focus'); var changed = tabs.indexOf(id) < 0 || pageChanged || enteringWorkspace; if (tabs.indexOf(id) < 0) tabs.push(id); activeId = id; app.classList.add('workspace-focus'); if (pageChanged || enteringWorkspace) { app.classList.remove('workspace-nav-open', 'workspace-nav-collapsed'); } preparePageHelp(document.getElementById(id)); if (changed) renderTabs(); if (id === 'shopify-payouts' && typeof window.ensureShopifyStores === 'function') { window.ensureShopifyStores().catch(function () {}); } if (id.indexOf('walmart-') === 0 && typeof window.loadWalmartPageData === 'function') { window.loadWalmartPageData(id); } } function leaveWorkspace() { var app = appWrap(); if (!app) return; var changed = activeId || app.classList.contains('workspace-focus'); app.classList.remove('workspace-focus', 'workspace-nav-open', 'workspace-nav-collapsed'); activeId = ''; if (changed) renderTabs(); } function openWorkspacePage(id) { var section = document.getElementById(id); if (!section || !isWorkspacePage(id)) return; document.querySelectorAll('.section').forEach(function (item) { item.classList.remove('active'); }); document.querySelectorAll('.nav[data-s], .nav[data-sps]').forEach(function (item) { item.classList.toggle('active', (item.dataset.s || item.dataset.sps) === id); }); section.classList.add('active'); var title = document.getElementById('title'); if (title) title.textContent = pageLabel(id); activateWorkspacePage(id); } function closeWorkspacePage(id) { var index = tabs.indexOf(id); if (index < 0) return; var wasActive = activeId === id; tabs.splice(index, 1); if (wasActive && tabs.length) { openWorkspacePage(tabs[Math.max(0, index - 1)]); return; } if (!tabs.length) { leaveWorkspace(); showDashboard(); } renderTabs(); } function resetWorkspaceShell() { tabs = []; activeId = ''; leaveWorkspace(); renderTabs(); } function syncActiveSection() { var section = document.querySelector('main.main > .section.active'); if (!section) return; if (isWorkspacePage(section.id)) activateWorkspacePage(section.id); else if (section.id === 'dashboard') leaveWorkspace(); } function installNavigationHooks() { var originalShowSection = window.showSection; if (typeof originalShowSection === 'function') { window.showSection = function (id) { var result = originalShowSection.apply(this, arguments); if (isWorkspacePage(id)) activateWorkspacePage(id); else leaveWorkspace(); return result; }; } var originalOpenFitSection = window.openFitSection; if (typeof originalOpenFitSection === 'function') { window.openFitSection = function (id) { var result = originalOpenFitSection.apply(this, arguments); if (isWorkspacePage(id)) activateWorkspacePage(id); else leaveWorkspace(); return result; }; } document.addEventListener('click', function (event) { var nav = event.target.closest && event.target.closest('.nav[data-s], .nav[data-sps]'); if (nav) { var id = nav.dataset.s || nav.dataset.sps; setTimeout(function () { if (isWorkspacePage(id)) activateWorkspacePage(id); else leaveWorkspace(); }, 0); return; } var action = event.target.closest && event.target.closest('button, a, [onclick]'); if (!action) return; if (action.closest('.workspace-tabs, .top')) return; setTimeout(function () { syncActiveSection(); }, 0); }); } prepareAllPages(); new MutationObserver(function (mutations) { var addedSection = mutations.some(function (mutation) { return mutation.type === 'childList' && Array.prototype.some.call(mutation.addedNodes, function (node) { return node.nodeType === 1 && ((node.matches && node.matches('.section')) || (node.querySelector && node.querySelector('.section'))); }); }); if (!addedSection) return; prepareAllPages(); syncActiveSection(); }).observe(document.querySelector('main.main'), {childList: true}); ensureGlobalHeader(); ensureTabBar(); renderTabs(); installNavigationHooks(); window.activateWorkspacePage = activateWorkspacePage; window.openWorkspacePage = openWorkspacePage; window.openWorkspaceHelp = currentPageHelp; window.resetWorkspaceShell = resetWorkspaceShell; })();