优化用户 VPC 页签无刷新切换

go
codex 2 weeks ago
parent ebfccca608
commit f0bb99d5cd

@ -221,6 +221,10 @@ function membersHref(vpc, joining = false) {
return `/user/cloud/vpcs?vpc=${encodeURIComponent(vpcKey(vpc))}${joining ? '&view=join' : ''}`;
}
function membersRouteHref(vpc, joining = false) {
return `#/${encodeURIComponent(vpcKey(vpc))}/instances${joining ? '/join' : ''}`;
}
function currentVpc() {
const route = pageRoute();
return state.items.find(vpc => vpcKey(vpc) === route.key) || null;
@ -470,7 +474,7 @@ function renderInstanceManager(vpc, joining) {
section.append(joining
? emptyState('暂无可加入的实例', '只显示同地区且尚未加入 VPC 的实例。')
: emptyState('尚未加入实例', '将同地区实例连接到这张私网。',
membersHref(vpc, true), '加入实例 →'));
membersRouteHref(vpc, true), '加入实例 →'));
return section;
}
let search = null;
@ -607,12 +611,12 @@ function renderMembers(vpc, joining) {
const members = byId('cloud-vpc-members-link');
members.replaceChildren(element('span', '', '已加入实例'),
element('span', 'cloud-vpc-tab-count', instancesForVpc(vpc).length));
members.href = membersHref(vpc);
members.href = membersRouteHref(vpc);
const join = byId('cloud-vpc-join-link');
const joinable = candidatesForVpc(vpc).filter(instance => instance.can_attach === true).length;
join.replaceChildren(element('span', '', '加入实例'),
element('span', 'cloud-vpc-tab-count', joinable));
join.href = membersHref(vpc, true);
join.href = membersRouteHref(vpc, true);
for (const [link, active] of [[members, !joining], [join, joining]]) {
if (active) link.setAttribute('aria-current', 'page');
else link.removeAttribute('aria-current');

@ -1,2 +1,2 @@
{* Shared Venix CSS/JS cache key. Increment whenever a shared browser asset changes. *}
{assign var=venix_asset_version value='2026110109' scope='global'}
{assign var=venix_asset_version value='2026110110' scope='global'}

@ -47,8 +47,17 @@ class Node {
this.listeners.get(name).push(listener);
}
async fire(name) {
await Promise.all((this.listeners.get(name) || []).map(fn => fn({ target: this, preventDefault() {} })));
const event = {
target: this,
defaultPrevented: false,
preventDefault() { this.defaultPrevented = true; },
};
await Promise.all((this.listeners.get(name) || []).map(fn => fn(event)));
await tick();
if (name === 'click' && !event.defaultPrevented && this.tagName === 'A' && this.href) {
await this.document.navigate(this.href);
}
return event;
}
focus() { this.document.activeElement = this; }
scrollIntoView() {}
@ -96,6 +105,20 @@ async function setup(hash = '#/', payload = fixtures()) {
const location = new URL(`https://panel.example.test/user/cloud/vpcs${hash}`);
const window = { location, VenixDialog: { confirm: async () => true },
addEventListener: (name, listener) => events.set(name, listener) };
let fullPageNavigations = 0;
document.navigate = async target => {
const next = new URL(target, location.href);
if (next.origin === location.origin && next.pathname === location.pathname
&& next.search === location.search) {
if (next.hash !== location.hash) {
location.hash = next.hash;
events.get('hashchange')?.();
await tick();
}
return;
}
fullPageNavigations += 1;
};
let data = structuredClone(payload);
let respond = async (url, options) => {
if (!options.method) return structuredClone(data);
@ -127,6 +150,7 @@ async function setup(hash = '#/', payload = fixtures()) {
await context.page.initialize();
return {
nodes, calls, observations, storage, location, data: () => data,
fullPageNavigations: () => fullPageNavigations,
update: next => { data = structuredClone(next); context.page.apply(structuredClone(next)); },
respond: fn => { respond = fn; },
event: (name, event = {}) => events.get(name)?.(event),
@ -185,6 +209,20 @@ for (const [query, page, joining] of [
assert.equal(t.nodes.get('cloud-vpcs-home').hidden, false);
assert.equal(mutations(t).length, 0, 'Navigation must never mutate a VPC');
}
{
const t = await setup(`?vpc=${A}`);
await t.nodes.get('cloud-vpc-join-link').fire('click');
assert.equal(t.fullPageNavigations(), 0,
'Switching to joinable instances must not trigger a full-page navigation');
assert.equal(t.nodes.get('cloud-vpc-instance-manager').querySelector('section')
.classList.contains('joining'), true);
await t.nodes.get('cloud-vpc-members-link').fire('click');
assert.equal(t.fullPageNavigations(), 0,
'Switching to attached instances must not trigger a full-page navigation');
assert.equal(t.nodes.get('cloud-vpc-instance-manager').querySelector('section')
.classList.contains('joining'), false);
assert.equal(mutations(t).length, 0, 'Tab switching must not mutate a VPC');
}
{
const t = await setup(`${href(A)}/join`);
const search = control(t, 'instance-search');

Loading…
Cancel
Save