keys: mode switches in page location hash
[sheet.git] / keys.js
diff --git a/keys.js b/keys.js
index dba04a1426b05ee980b7f08262501064a5d1beac..e1b7038e197d66d298c5b04710b099de0bb38e97 100644 (file)
--- a/keys.js
+++ b/keys.js
@@ -1,4 +1,4 @@
-function setmode(classname) {
+function setmode(classname, restore) {
        // set style for each #rows>li>ul>li to display:none unless it matches classname
        var showclass = classname ? '^mode '+classname+'(?!\\w)' : '^(?!mode)';
        var rows = document.getElementById('rows').getElementsByTagName('TR');
@@ -13,12 +13,17 @@ function setmode(classname) {
                if (h3s[i].parentNode.style.display != 'block') continue;
                var header = h3s[i].firstChild.data;
                document.getElementsByTagName('H2')[0].innerHTML = header;
+               if (restore) break;
+               history.pushState(null, header, classname ? '#'+classname : '#');
                break;
        }
 }
 
+window.addEventListener('hashchange', function(e) {
+       setmode(location.hash.slice(1), true);
+});
 if (location.hash) {
-       setmode(location.hash.slice(1));
+       setmode(location.hash.slice(1), true);
 }
 
 var keyfocus = undefined;