ui-navigation.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 page = pages.has(value) ? value : 'overview';
  6. document.body.dataset.currentPage = page;
  7. document.querySelectorAll('[data-pages]').forEach(node => {
  8. node.hidden = !node.dataset.pages.split(' ').includes(page);
  9. });
  10. document.querySelectorAll('.workspace-tab').forEach(button => {
  11. const selected = button.dataset.page === page;
  12. button.classList.toggle('selected', selected);
  13. button.setAttribute('aria-selected', String(selected));
  14. button.tabIndex = selected ? 0 : -1;
  15. });
  16. if (updateHash && location.hash !== '#' + page) history.replaceState(null, '', '#' + page);
  17. window.dispatchEvent(new CustomEvent('stl:page-change', {detail:{page}}));
  18. }
  19. document.querySelectorAll('.workspace-tab').forEach(button => {
  20. button.addEventListener('click', () => show(button.dataset.page));
  21. button.addEventListener('keydown', event => {
  22. if (!['ArrowLeft', 'ArrowRight', 'Home', 'End'].includes(event.key)) return;
  23. event.preventDefault();
  24. const buttons = [...document.querySelectorAll('.workspace-tab')];
  25. const i = buttons.indexOf(button);
  26. const next = event.key === 'Home' ? 0 : event.key === 'End' ? buttons.length - 1 : (i + (event.key === 'ArrowRight' ? 1 : -1) + buttons.length) % buttons.length;
  27. show(buttons[next].dataset.page); buttons[next].focus();
  28. });
  29. });
  30. document.querySelectorAll('[data-nav-page]').forEach(button => button.addEventListener('click', () => show(button.dataset.navPage)));
  31. document.getElementById('nav-station').addEventListener('change', event => {
  32. window.dispatchEvent(new CustomEvent('stl:station-select', {detail:{stationId:event.target.value}}));
  33. });
  34. window.addEventListener('hashchange', () => show(location.hash.slice(1), false));
  35. window.stlNavigation = {
  36. show,
  37. current: () => document.body.dataset.currentPage,
  38. updateStations(stations, selected) {
  39. const select = document.getElementById('nav-station');
  40. const signature = JSON.stringify(stations.map(s => [s.station_id, s.name]));
  41. if (select.dataset.signature !== signature) {
  42. select.replaceChildren(...stations.map(s => {
  43. const option = document.createElement('option');
  44. option.value = s.station_id; option.textContent = `${s.station_id} · ${s.name || s.station_id}`;
  45. return option;
  46. }));
  47. select.dataset.signature = signature;
  48. }
  49. select.value = selected;
  50. },
  51. };
  52. show(location.hash.slice(1), false);
  53. })();