ui-navigation.js 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Page routing only. Changing the visible page never changes a device session.
  2. (() => {
  3. const pages = new Set(['overview', 'thermal', 'connections', 'plan', 'uip', 'amt', 'testing', 'records', 'queue']);
  4. function show(value, updateHash = true) {
  5. const normalized = value === 'plans' ? 'plan' : value;
  6. const page = pages.has(normalized) ? normalized : 'overview';
  7. document.body.dataset.currentPage = page;
  8. if (page !== 'overview') {
  9. const split = document.querySelector('.workspace-split.console-expanded');
  10. if (split) {
  11. split.classList.remove('console-expanded');
  12. const expandBtn = document.getElementById('btn-console-expand');
  13. if (expandBtn) expandBtn.textContent = '放大终端';
  14. }
  15. }
  16. window.stlStore?.set?.('page', page, 'nav');
  17. document.querySelectorAll('[data-pages]').forEach(node => {
  18. node.hidden = !node.dataset.pages.split(' ').includes(page);
  19. });
  20. document.querySelectorAll('.workspace-tab').forEach(button => {
  21. const selected = button.dataset.page === page;
  22. button.classList.toggle('selected', selected);
  23. button.setAttribute('aria-selected', String(selected));
  24. button.tabIndex = selected ? 0 : -1;
  25. });
  26. if (updateHash && location.hash !== '#' + page) history.replaceState(null, '', '#' + page);
  27. window.dispatchEvent(new CustomEvent('stl:page-change', {detail:{page}}));
  28. }
  29. document.querySelectorAll('.workspace-tab').forEach(button => {
  30. button.addEventListener('click', () => show(button.dataset.page));
  31. button.addEventListener('keydown', event => {
  32. if (!['ArrowLeft', 'ArrowRight', 'Home', 'End'].includes(event.key)) return;
  33. event.preventDefault();
  34. const buttons = [...document.querySelectorAll('.workspace-tab')];
  35. const i = buttons.indexOf(button);
  36. const next = event.key === 'Home' ? 0 : event.key === 'End' ? buttons.length - 1 : (i + (event.key === 'ArrowRight' ? 1 : -1) + buttons.length) % buttons.length;
  37. show(buttons[next].dataset.page); buttons[next].focus();
  38. });
  39. });
  40. document.querySelectorAll('[data-nav-page]').forEach(button => button.addEventListener('click', () => show(button.dataset.navPage)));
  41. document.getElementById('nav-station').addEventListener('change', event => {
  42. window.dispatchEvent(new CustomEvent('stl:station-select', {detail:{stationId:event.target.value}}));
  43. });
  44. window.addEventListener('hashchange', () => show(location.hash.slice(1), false));
  45. window.stlNavigation = {
  46. show,
  47. current: () => document.body.dataset.currentPage,
  48. updateStations(stations, selected) {
  49. const select = document.getElementById('nav-station');
  50. const signature = JSON.stringify(stations.map(s => [s.station_id, s.name]));
  51. if (select.dataset.signature !== signature) {
  52. select.replaceChildren(...stations.map(s => {
  53. const option = document.createElement('option');
  54. option.value = s.station_id; option.textContent = `${s.station_id} · ${s.name || s.station_id}`;
  55. return option;
  56. }));
  57. select.dataset.signature = signature;
  58. document.querySelectorAll('select[data-dynamic-stations="all"]').forEach(sel => {
  59. const prev = sel.value;
  60. const allOpt = document.createElement('option');
  61. allOpt.value = '';
  62. allOpt.textContent = '全部工位';
  63. sel.replaceChildren(allOpt, ...stations.map(s => {
  64. const o = document.createElement('option');
  65. o.value = s.station_id;
  66. o.textContent = s.station_id;
  67. return o;
  68. }));
  69. sel.value = stations.some(s => s.station_id === prev) ? prev : '';
  70. });
  71. }
  72. select.value = selected;
  73. },
  74. };
  75. show(location.hash.slice(1), false);
  76. })();