Files
xbcr/static/js/settings-pages.js

101 lines
5.0 KiB
JavaScript

/* One full-page settings hierarchy; browser Back and the header use the same route. */
(function () {
'use strict';
const routes = {
'settings-modal': { path: '', title: '设置', load: 'openSettingsPage' },
'auth-modal': { path: '/credentials', title: '微信凭证', load: 'openAuthModal' },
'devices-modal': { path: '/devices', title: '登录设备', load: 'openDevicesModal' },
'rental-settings-modal': { path: '/rental', title: '提醒与自动还车', load: 'openRentalSettingsModal' },
'packages-modal': { path: '/cards', title: '我的优惠卡', load: 'openPackagesModal' },
'orders-modal': { path: '/orders', title: '订单历史', load: 'openOrdersModal' },
'order-traj-modal': { path: '/orders/trajectory', title: '骑行轨迹', parent: 'orders-modal', fallback: 'openOrdersModal', close: 'closeOrderTrajModal' },
'subscribe-modal': { path: '/subscriptions', title: '车辆订阅', load: 'openSubscribeModal' },
'share-modal': { path: '/sharing', title: '共享电动车', load: 'openShareModal' },
'share-config-modal': { path: '/sharing/permissions', title: '共享权限', parent: 'share-modal', fallback: 'openShareModal', close: 'closeShareConfig' },
'share-remark-modal': { path: '/sharing/remark', title: '设置备注', parent: 'share-modal', fallback: 'openShareModal', close: 'closeShareRemark' }
};
const shell = document.getElementById('settings-page');
const content = document.getElementById('settings-page-main');
let active = null;
const scrollPositions = new Map();
const routeId = () => Object.keys(routes).find(id => location.hash === '#settings' + routes[id].path);
function hide() {
if (active) scrollPositions.set(active, content.scrollTop);
Object.keys(routes).forEach(id => {
const view = document.getElementById(id);
view.hidden = true;
view.classList.remove('show');
});
shell.hidden = true;
active = null;
}
function show(id) {
hide();
const view = document.getElementById(id);
view.hidden = false;
view.classList.add('show');
view.removeAttribute('aria-hidden');
shell.hidden = false;
active = id;
document.getElementById('settings-page-title').textContent = routes[id].title;
document.getElementById('settings-page-back').setAttribute('aria-label', id === 'settings-modal' ? '返回地图' : '返回上一页');
document.getElementById('settings-page-back-label').textContent = id === 'settings-modal' ? '地图' : '返回';
document.getElementById('settings-page-home').hidden = id === 'settings-modal';
content.scrollTop = scrollPositions.get(id) || 0;
document.getElementById('settings-page-title').focus({ preventScroll: true });
}
function open(id) {
if (!routes[id]) return;
const hash = '#settings' + routes[id].path;
if (location.hash !== hash) {
const depth = routeId() ? (history.state?.settingsDepth || 0) : 0;
history.pushState({ settingsDepth: depth + 1 }, '', hash);
}
show(id);
}
function close() {
const depth = history.state?.settingsDepth || 0;
hide();
if (depth > 0 && routeId()) history.go(-depth);
else history.replaceState(null, '', location.pathname + location.search);
document.getElementById('auth-btn')?.focus({ preventScroll: true });
}
function back() {
if (!active) return;
const route = routes[active];
if ((history.state?.settingsDepth || 0) > 0) history.back();
else if (active !== 'settings-modal') {
const parent = route.parent || 'settings-modal';
history.replaceState(null, '', '#settings' + routes[parent].path);
const load = routes[parent].load;
if (load && typeof window[load] === 'function') window[load]();
else show(parent);
} else close();
}
function requestBack() {
const cleanup = active && routes[active].close;
if (cleanup && typeof window[cleanup] === 'function') window[cleanup]();
else back();
}
function restore() {
const id = routeId();
if (!id) { hide(); return; }
if (!routes[id].load) {
const parent = routes[id].parent || 'settings-modal';
history.replaceState(null, '', '#settings' + routes[parent].path);
}
const load = routes[id].load || routes[id].fallback;
if (load && typeof window[load] === 'function') window[load]();
else show(id);
}
window.addEventListener('popstate', () => {
const id = routeId();
if (id) show(id); else hide();
});
shell.addEventListener('keydown', event => {
if (event.key === 'Escape') { event.preventDefault(); requestBack(); }
});
window.SettingsPages = { open, back, requestBack, close, hide, restore, isOpen: () => !shell.hidden };
})();