| | @@ -16,27 +16,37 @@ |
| 16 | 16 | ** This file contains the JS code specific to the Fossil default skin. |
| 17 | 17 | ** Currently, the only thing this does is handle clicks on its hamburger |
| 18 | 18 | ** menu button. |
| 19 | 19 | */ |
| 20 | 20 | (function() { |
| 21 | + if (!document.getElementById("hbbtn")) return; // no hamburger button |
| 21 | 22 | var panel = document.getElementById("hbdrop"); |
| 22 | 23 | if (!panel) return; // site admin might've nuked it |
| 23 | 24 | if (!panel.style) return; // shouldn't happen, but be sure |
| 24 | 25 | var panelBorder = panel.style.border; |
| 26 | + var panelInitialized = false; // track first panel display |
| 25 | 27 | |
| 26 | 28 | // Disable animation if this browser doesn't support CSS transitions. |
| 27 | 29 | // |
| 28 | 30 | // We need this ugly calling form for old browsers that don't allow |
| 29 | 31 | // panel.style.hasOwnProperty('transition'); catering to old browsers |
| 30 | 32 | // is the whole point here. |
| 31 | 33 | var animate = panel.style.transition !== null && (typeof(panel.style.transition) == "string"); |
| 32 | | - var animMS = 400; |
| 34 | + |
| 35 | + // The duration of the animation can be overridden from the default skin |
| 36 | + // header.txt by setting the "data-anim-ms" attribute of the panel. |
| 37 | + var animMS = panel.getAttribute("data-anim-ms"); |
| 38 | + if (animMS === "0") |
| 39 | + animate = false; // disable animation if "data-anim-ms" === "0" |
| 40 | + else if (!animMS || animMS>>0 !== Number(animMS) || animMS < 0) |
| 41 | + animMS = 400; // set default if missing, empty, non-integer, or negative |
| 42 | + |
| 33 | 43 | var originalEventHandlers = { }; // original event handlers to be restored |
| 34 | 44 | |
| 35 | 45 | // Calculate panel height despite its being hidden at call time. |
| 36 | 46 | // Based on https://stackoverflow.com/a/29047447/142454 |
| 37 | | - var panelHeight; // computed on sitemap load |
| 47 | + var panelHeight; // computed on first panel display |
| 38 | 48 | function calculatePanelHeight() { |
| 39 | 49 | // Get initial panel styles so we can restore them below. |
| 40 | 50 | var es = window.getComputedStyle(panel), |
| 41 | 51 | edis = es.display, |
| 42 | 52 | epos = es.position, |
| | @@ -64,18 +74,27 @@ |
| 64 | 74 | // change as a state transition, so it'd skip the CSS transition: |
| 65 | 75 | // |
| 66 | 76 | // https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Transitions/Using_CSS_transitions#JavaScript_examples |
| 67 | 77 | function showPanel() { |
| 68 | 78 | if (animate) { |
| 79 | + if (!panelInitialized) { |
| 80 | + panelInitialized = true; |
| 81 | + // Set up a CSS transition to animate the panel open and |
| 82 | + // closed. Only needs to be done once per page load. |
| 83 | + // Based on https://stackoverflow.com/a/29047447/142454 |
| 84 | + calculatePanelHeight(); |
| 85 | + panel.style.transition = 'max-height ' + animMS + |
| 86 | + 'ms ease-in-out'; |
| 87 | + panel.style.overflowY = 'hidden'; |
| 88 | + panel.style.maxHeight = '0'; |
| 89 | + } |
| 69 | 90 | setTimeout(function() { |
| 70 | 91 | panel.style.maxHeight = panelHeight; |
| 71 | 92 | panel.style.border = panelBorder; |
| 72 | 93 | }, 40); // 25ms is insufficient with Firefox 62 |
| 73 | 94 | } |
| 74 | | - else { |
| 75 | | - panel.style.display = 'block'; |
| 76 | | - } |
| 95 | + panel.style.display = 'block'; |
| 77 | 96 | originalEventHandlers.onkeydown = document.onkeydown; |
| 78 | 97 | document.onkeydown = function(event) { |
| 79 | 98 | event = event || window.event; |
| 80 | 99 | var key = event.which || event.keyCode; |
| 81 | 100 | if (key == 27) { |
| | @@ -99,14 +118,25 @@ |
| 99 | 118 | } |
| 100 | 119 | else { |
| 101 | 120 | return panel.style.display == 'block'; |
| 102 | 121 | } |
| 103 | 122 | } |
| 123 | + |
| 124 | + // Check if the specified HTML element has any child elements. Note that plain |
| 125 | + // text nodes, comments, and any spaces (presentational or not) are ignored. |
| 126 | + function hasChildren(element) { |
| 127 | + var childElement = element.firstChild; |
| 128 | + while (childElement) { |
| 129 | + if (childElement.nodeType == 1) // Node.ELEMENT_NODE == 1 |
| 130 | + return true; |
| 131 | + childElement = childElement.nextSibling; |
| 132 | + } |
| 133 | + return false; |
| 134 | + } |
| 104 | 135 | |
| 105 | 136 | // Click handler for the hamburger button. |
| 106 | | - var needSitemapHTML = true; |
| 107 | | - document.querySelector("div.mainmenu > a").onclick = function(event) { |
| 137 | + document.getElementById("hbbtn").onclick = function(event) { |
| 108 | 138 | // Break the event handler chain, or the handler for document.onclick |
| 109 | 139 | // (about to be installed) may already be triggered by the current event. |
| 110 | 140 | if (event.stopPropagation) |
| 111 | 141 | event.stopPropagation(); |
| 112 | 142 | else |
| | @@ -142,44 +172,33 @@ |
| 142 | 172 | } |
| 143 | 173 | else { |
| 144 | 174 | panel.style.display = 'none'; |
| 145 | 175 | } |
| 146 | 176 | } |
| 147 | | - else if (needSitemapHTML) { |
| 148 | | - // Only get it once per page load: it isn't likely to |
| 149 | | - // change on us. |
| 150 | | - var xhr = new XMLHttpRequest(); |
| 151 | | - xhr.onload = function() { |
| 152 | | - var doc = xhr.responseXML; |
| 153 | | - if (doc) { |
| 154 | | - var sm = doc.querySelector("ul#sitemap"); |
| 155 | | - if (sm && xhr.status == 200) { |
| 156 | | - // Got sitemap. Insert it into the drop-down panel. |
| 157 | | - needSitemapHTML = false; |
| 158 | | - panel.innerHTML = sm.outerHTML; |
| 159 | | - |
| 160 | | - // Display the panel |
| 161 | | - if (animate) { |
| 162 | | - // Set up a CSS transition to animate the panel open and |
| 163 | | - // closed. Only needs to be done once per page load. |
| 164 | | - // Based on https://stackoverflow.com/a/29047447/142454 |
| 165 | | - calculatePanelHeight(); |
| 166 | | - panel.style.transition = 'max-height ' + animMS + |
| 167 | | - 'ms ease-in-out'; |
| 168 | | - panel.style.overflowY = 'hidden'; |
| 169 | | - panel.style.maxHeight = '0'; |
| 177 | + else { |
| 178 | + if (!hasChildren(panel)) { |
| 179 | + // Only get the sitemap once per page load: it isn't likely to |
| 180 | + // change on us. |
| 181 | + var xhr = new XMLHttpRequest(); |
| 182 | + xhr.onload = function() { |
| 183 | + var doc = xhr.responseXML; |
| 184 | + if (doc) { |
| 185 | + var sm = doc.querySelector("ul#sitemap"); |
| 186 | + if (sm && xhr.status == 200) { |
| 187 | + // Got sitemap. Insert it into the drop-down panel. |
| 188 | + panel.innerHTML = sm.outerHTML; |
| 189 | + // Display the panel |
| 170 | 190 | showPanel(); |
| 171 | 191 | } |
| 172 | | - panel.style.display = 'block'; |
| 173 | | - } |
| 174 | | - } |
| 175 | | - // else, can't parse response as HTML or XML |
| 176 | | - } |
| 177 | | - xhr.open("GET", "$home/sitemap?popup"); // note the TH1 substitution! |
| 178 | | - xhr.responseType = "document"; |
| 179 | | - xhr.send(); |
| 180 | | - } |
| 181 | | - else { |
| 182 | | - showPanel(); // just show what we built above |
| 192 | + } |
| 193 | + // else, can't parse response as HTML or XML |
| 194 | + } |
| 195 | + xhr.open("GET", "$home/sitemap?popup"); // note the TH1 substitution! |
| 196 | + xhr.responseType = "document"; |
| 197 | + xhr.send(); |
| 198 | + } |
| 199 | + else { |
| 200 | + showPanel(); // just show what we built above |
| 201 | + } |
| 183 | 202 | } |
| 184 | 203 | } |
| 185 | 204 | })(); |
| 186 | 205 | |