- 已打开页面标签栏只在最外层窗口渲染,iframe 子页面不再出现同一条标签栏, 避免点标签时在 iframe 内部再套一层快递报价工作区(页面上出现两份同样的页签+标签行) - 快递报价工作区注入的样式同时隐藏 .open-tabs,兜住浏览器缓存的旧子页面
204 lines
8.3 KiB
PHP
204 lines
8.3 KiB
PHP
{{--
|
||
已打开页面标签栏:点开过的业务页面固定在顶部并排显示,方便来回切换;
|
||
只要没退出登录就一直保留(按账号存在浏览器本地,不外传)。
|
||
--}}
|
||
<div class="open-tabs" data-open-tabs data-user-id="{{ user()->id }}" hidden>
|
||
<div class="open-tabs__scroll">
|
||
<div class="open-tabs__list" data-open-tabs-list role="tablist" aria-label="已打开页面"></div>
|
||
</div>
|
||
<button type="button" class="open-tabs__clear" data-open-tabs-clear title="清空已打开页面(当前页面保留)">清空</button>
|
||
</div>
|
||
|
||
<style>
|
||
.open-tabs {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 0.5rem;
|
||
margin: 0 0 0.75rem;
|
||
padding: 0.35rem 0.5rem;
|
||
border: 1px solid var(--vip-border, #e7e0d5);
|
||
border-radius: 0.625rem;
|
||
background: var(--vip-surface, #fff);
|
||
}
|
||
.open-tabs[hidden] { display: none; }
|
||
.open-tabs__scroll { flex: 1 1 auto; min-width: 0; overflow-x: auto; }
|
||
.open-tabs__list { display: flex; align-items: center; gap: 0.35rem; white-space: nowrap; }
|
||
.open-tabs__tab {
|
||
display: inline-flex;
|
||
align-items: center;
|
||
gap: 0.35rem;
|
||
max-width: 15rem;
|
||
padding: 0.3rem 0.5rem 0.3rem 0.65rem;
|
||
border: 1px solid var(--vip-border, #e7e0d5);
|
||
border-radius: 0.5rem;
|
||
background: #fbf8f0;
|
||
color: var(--vip-content-ink, #171717);
|
||
font-size: 0.75rem;
|
||
text-decoration: none;
|
||
}
|
||
.open-tabs__tab:hover { border-color: var(--vip-content-accent, #b9954f); color: var(--vip-content-accent-dark, #856632); }
|
||
.open-tabs__tab.is-active {
|
||
border-color: var(--vip-content-accent-dark, #856632);
|
||
background: var(--vip-content-accent-soft, #f7f0de);
|
||
color: var(--vip-content-accent-dark, #856632);
|
||
font-weight: 600;
|
||
}
|
||
.open-tabs__label { overflow: hidden; text-overflow: ellipsis; }
|
||
.open-tabs__close {
|
||
flex: 0 0 auto;
|
||
width: 1rem;
|
||
height: 1rem;
|
||
padding: 0;
|
||
border: 0;
|
||
border-radius: 50%;
|
||
background: transparent;
|
||
color: inherit;
|
||
font-size: 0.875rem;
|
||
line-height: 1;
|
||
cursor: pointer;
|
||
opacity: 0.55;
|
||
}
|
||
.open-tabs__close:hover { background: rgba(133, 102, 50, 0.15); opacity: 1; }
|
||
.open-tabs__clear {
|
||
flex: 0 0 auto;
|
||
height: 1.6rem;
|
||
padding: 0 0.5rem;
|
||
border: 1px solid var(--vip-border, #e7e0d5);
|
||
border-radius: 0.5rem;
|
||
background: #fff;
|
||
color: var(--vip-content-muted, #595959);
|
||
font-size: 0.6875rem;
|
||
cursor: pointer;
|
||
}
|
||
.open-tabs__clear:hover { border-color: var(--vip-content-accent-dark, #856632); color: var(--vip-content-accent-dark, #856632); }
|
||
</style>
|
||
|
||
<script>
|
||
(() => {
|
||
const root = document.querySelector('[data-open-tabs]');
|
||
if (!root) {
|
||
return;
|
||
}
|
||
// 目录里有用 iframe 打开子页面的页面(例如快递报价)。iframe 内不再显示标签栏:
|
||
// 它既和外层那条重复,点标签也只会在 iframe 内部再套一层同样的页面。
|
||
if (window.self !== window.top) {
|
||
return;
|
||
}
|
||
const list = root.querySelector('[data-open-tabs-list]');
|
||
const userId = root.dataset.userId || 'anon';
|
||
const storageKey = 'caiwu.open-pages.v1.' + userId;
|
||
// 只在"本次浏览器会话"里保留:同一个浏览器窗口内刷新/来回切换都在,
|
||
// 关掉浏览器或换一个窗口重新进来就是干净的,不会再把前几天的旧标签带出来。
|
||
const storage = window.sessionStorage;
|
||
const maxTabs = 15;
|
||
|
||
// 清理早期版本写在 localStorage 里的记录(那份会跨浏览器一直保留)。
|
||
try {
|
||
window.localStorage.removeItem(storageKey);
|
||
} catch (_) { /* 隐私模式下忽略 */ }
|
||
|
||
const read = () => {
|
||
try {
|
||
const parsed = JSON.parse(storage.getItem(storageKey) || '[]');
|
||
return Array.isArray(parsed) ? parsed.filter((item) => item && typeof item.url === 'string' && typeof item.path === 'string') : [];
|
||
} catch (_) {
|
||
return [];
|
||
}
|
||
};
|
||
const save = (items) => {
|
||
try {
|
||
storage.setItem(storageKey, JSON.stringify(items));
|
||
} catch (_) { /* 隐私模式下忽略 */ }
|
||
};
|
||
|
||
const currentUrl = window.location.pathname + window.location.search;
|
||
const currentPath = window.location.pathname;
|
||
const isBusinessPage = /^\/\d+\/workspace\//.test(window.location.pathname);
|
||
const pageTitle = () => (document.title || '').split(' - ')[0].trim() || window.location.pathname;
|
||
// 只有"从系统里点进去"的页面才自动固定:浏览器恢复会话、直接输入地址、
|
||
// 从其它网站点进来时没有同源来源,不会被记进标签栏。
|
||
const openedFromApp = (() => {
|
||
try {
|
||
return !!document.referrer && new URL(document.referrer).origin === window.location.origin;
|
||
} catch (_) {
|
||
return false;
|
||
}
|
||
})();
|
||
|
||
let items = read();
|
||
if (isBusinessPage) {
|
||
// 同一张页面(按路径去重)只保留一个标签,点击标签回到最近一次带筛选的地址。
|
||
const existing = items.find((item) => item.path === currentPath);
|
||
if (existing) {
|
||
existing.title = pageTitle();
|
||
existing.url = currentUrl;
|
||
save(items);
|
||
} else if (openedFromApp || items.length === 0) {
|
||
// 从系统内点进来的页面,或者当前会话还没有任何标签(例如刷新后),
|
||
// 把当前页作为第一个标签,避免标签栏空着。
|
||
items.push({ path: currentPath, url: currentUrl, title: pageTitle() });
|
||
items = items.slice(-maxTabs);
|
||
save(items);
|
||
}
|
||
}
|
||
|
||
if (!items.length) {
|
||
return;
|
||
}
|
||
root.hidden = false;
|
||
|
||
const render = () => {
|
||
list.innerHTML = items.map((item) => {
|
||
const active = item.path === currentPath ? ' is-active' : '';
|
||
const title = String(item.title || item.url).replace(/[&<>"']/g, (ch) => ({
|
||
'&': '&', '<': '<', '>': '>', '"': '"', "'": ''',
|
||
}[ch]));
|
||
const href = item.url.replace(/[&<>"']/g, (ch) => ({
|
||
'&': '&', '<': '<', '>': '>', '"': '"', "'": ''',
|
||
}[ch]));
|
||
|
||
return '<a class="open-tabs__tab' + active + '" href="' + href + '" role="tab"'
|
||
+ (active ? ' aria-current="page"' : '') + ' title="' + title + '">'
|
||
+ '<span class="open-tabs__label">' + title + '</span>'
|
||
+ '<button type="button" class="open-tabs__close" data-open-tab-close="' + href + '" aria-label="关闭 ' + title + '">×</button>'
|
||
+ '</a>';
|
||
}).join('');
|
||
};
|
||
|
||
render();
|
||
|
||
list.addEventListener('click', (event) => {
|
||
const button = event.target.closest('[data-open-tab-close]');
|
||
if (!button) {
|
||
return;
|
||
}
|
||
event.preventDefault();
|
||
event.stopPropagation();
|
||
const url = button.getAttribute('data-open-tab-close');
|
||
const index = items.findIndex((item) => item.url === url);
|
||
const wasActive = items[index] ? items[index].path === currentPath : false;
|
||
items = items.filter((item) => item.url !== url);
|
||
save(items);
|
||
if (wasActive) {
|
||
const fallback = items[Math.max(0, index - 1)] || items[0];
|
||
if (fallback && fallback.url) {
|
||
window.location.assign(fallback.url);
|
||
return;
|
||
}
|
||
}
|
||
render();
|
||
});
|
||
|
||
root.querySelector('[data-open-tabs-clear]')?.addEventListener('click', () => {
|
||
items = items.filter((item) => item.path === currentPath);
|
||
save(items);
|
||
if (!items.length) {
|
||
root.hidden = true;
|
||
list.innerHTML = '';
|
||
return;
|
||
}
|
||
render();
|
||
});
|
||
})();
|
||
</script>
|