|
22 | 22 | picker.addEventListener("change", apply); |
23 | 23 | apply(); |
24 | 24 | })(); |
| 25 | + |
| 26 | +/* Tabs. The page is one long form, and nobody wants to scroll past sign in |
| 27 | + * to reach the reports. Only the cards on the chosen tab show; the rest are |
| 28 | + * hidden, not removed, so the form still sends every field when it is saved. |
| 29 | + * |
| 30 | + * The tab comes from the address (#tab-reports, or a section such as |
| 31 | + * #external), then from the last one used, then the first. A field the |
| 32 | + * browser refuses on a tab that is not showing would stop the save without |
| 33 | + * saying why, so its tab is opened first. */ |
| 34 | +(function () { |
| 35 | + "use strict"; |
| 36 | + |
| 37 | + var links = Array.prototype.slice.call( |
| 38 | + document.querySelectorAll(".settings-tabs [data-tab]")); |
| 39 | + if (!links.length) { return; } |
| 40 | + var owner = {}; |
| 41 | + links.forEach(function (link) { |
| 42 | + link.getAttribute("data-sections").split(" ").forEach(function (id) { |
| 43 | + owner[id] = link.getAttribute("data-tab"); |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + function remember(tab) { |
| 48 | + try { window.sessionStorage.setItem("gcr-settings-tab", tab); } catch (e) { /* private mode */ } |
| 49 | + } |
| 50 | + |
| 51 | + function recalled() { |
| 52 | + try { return window.sessionStorage.getItem("gcr-settings-tab"); } catch (e) { return null; } |
| 53 | + } |
| 54 | + |
| 55 | + function show(tab) { |
| 56 | + links.forEach(function (link) { |
| 57 | + var on = link.getAttribute("data-tab") === tab; |
| 58 | + link.classList.toggle("active", on); |
| 59 | + link.setAttribute("aria-selected", on ? "true" : "false"); |
| 60 | + }); |
| 61 | + Object.keys(owner).forEach(function (id) { |
| 62 | + var card = document.getElementById(id); |
| 63 | + if (card) { card.hidden = owner[id] !== tab; } |
| 64 | + }); |
| 65 | + document.querySelectorAll("[data-on-tab]").forEach(function (node) { |
| 66 | + node.hidden = node.getAttribute("data-on-tab") !== tab; |
| 67 | + }); |
| 68 | + remember(tab); |
| 69 | + } |
| 70 | + |
| 71 | + function fromHash() { |
| 72 | + var hash = (window.location.hash || "").replace(/^#/, ""); |
| 73 | + if (hash.indexOf("tab-") === 0 && links.some(function (l) { |
| 74 | + return l.getAttribute("data-tab") === hash.slice(4); })) { |
| 75 | + return hash.slice(4); |
| 76 | + } |
| 77 | + return owner[hash] || null; |
| 78 | + } |
| 79 | + |
| 80 | + links.forEach(function (link) { |
| 81 | + link.addEventListener("click", function (event) { |
| 82 | + event.preventDefault(); |
| 83 | + var tab = link.getAttribute("data-tab"); |
| 84 | + show(tab); |
| 85 | + if (window.history && window.history.replaceState) { |
| 86 | + window.history.replaceState(null, "", "#tab-" + tab); |
| 87 | + } |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + window.addEventListener("hashchange", function () { |
| 92 | + var tab = fromHash(); |
| 93 | + if (tab) { |
| 94 | + show(tab); |
| 95 | + var target = document.getElementById(window.location.hash.slice(1)); |
| 96 | + if (target && owner[target.id]) { target.scrollIntoView(); } |
| 97 | + } |
| 98 | + }); |
| 99 | + |
| 100 | + var form = document.getElementById("settings-form"); |
| 101 | + if (form) { |
| 102 | + form.addEventListener("invalid", function (event) { |
| 103 | + var card = event.target.closest(".card[id]"); |
| 104 | + if (card && owner[card.id] && card.hidden) { show(owner[card.id]); } |
| 105 | + }, true); |
| 106 | + } |
| 107 | + |
| 108 | + var start = fromHash(); |
| 109 | + var last = recalled(); |
| 110 | + if (!start && last && links.some(function (l) { return l.getAttribute("data-tab") === last; })) { |
| 111 | + start = last; |
| 112 | + } |
| 113 | + show(start || links[0].getAttribute("data-tab")); |
| 114 | + var direct = document.getElementById((window.location.hash || "").slice(1)); |
| 115 | + if (direct && owner[direct.id]) { direct.scrollIntoView(); } |
| 116 | +})(); |
0 commit comments