From f74a9c7594d3d5564141a3c070269fc5426baf90 Mon Sep 17 00:00:00 2001 From: Matt Moran Date: Sat, 11 Nov 2017 10:45:56 +1300 Subject: [PATCH 01/11] Added ability to navigate with browser forward/backward buttons Added ability to navigate with browser forward/backward buttons Moved item/module loading from the mod (data set) loader. Now loadData only gets mod data and stores it appropriately. loadModules and loadItems have been added. Functions that change the hash now have an optional parameter called keepHash (which stops the function changing the hash) Added loadFullSettings which doesn't leave default settings blank --- display.js | 10 ++-- events.js | 43 ++++++++++++--- fragment.js | 10 ++++ init.js | 151 ++++++++++++++++++++++++++++------------------------ settings.js | 18 +++++++ 5 files changed, 152 insertions(+), 80 deletions(-) diff --git a/display.js b/display.js index fa7a6890..7183261e 100644 --- a/display.js +++ b/display.js @@ -63,7 +63,7 @@ var globalTotals // This function obtains the set of item-rates to solve for from build_targets, // the set of modules from spec, and obtains a solution from solver. The // factory counts are then obtained from the solution using the spec. -function itemUpdate() { +function itemUpdate(keepHash) { var rates = {} for (var i = 0; i < build_targets.length; i++) { var target = build_targets[i] @@ -71,7 +71,7 @@ function itemUpdate() { rates[target.itemName] = rate } globalTotals = solver.solve(rates, spec.ignore, spec) - display() + display(keepHash) } function Header(name, colSpan) { @@ -893,14 +893,16 @@ RecipeTable.prototype = { } // Re-renders the current solution, without re-computing it. -function display() { +function display(keepHash) { // Update the display of the target rate text boxes, if needed. for (var i = 0; i < build_targets.length; i++) { build_targets[i].getRate() } var totals = globalTotals - window.location.hash = "#" + formatSettings() + if (!keepHash) { + window.location.hash = "#" + formatSettings() + } if (currentTab == "graph_tab") { renderGraph(totals, spec.ignore) diff --git a/events.js b/events.js index 4d391ef8..ab109792 100644 --- a/events.js +++ b/events.js @@ -1,5 +1,37 @@ "use strict" +// correctly handle back/forward buttons + +var mouseOnPage = true +document.addEventListener("DOMContentLoaded", function() { + document.body.addEventListener("mouseenter", function() { + mouseOnPage = true + }); + document.body.addEventListener("mouseleave", function() { + mouseOnPage = false + }); +}); +window.addEventListener("hashchange", function() { + if (!mouseOnPage) { + var settings = loadFullSettings(window.location.hash) + if (settings.tab != DEFAULT_SETTINGS.tab) { + settings.tab += "_tab" + } + clickTab(settings.tab, "keepHash") + + if ("data" in settings && settings.data != currentMod()) { + document.getElementById("data_set").value = settings.data + changeMod(settings) + } else { + // changeMod >> loadData also does all this so don't repeat it + renderSettings(settings) + loadModules(settings) + loadItems(settings) + itemUpdate() + } + } +}); + // build target events // The "+" button to add a new target. @@ -48,11 +80,11 @@ function RateHandler(target) { // Obtains current data set from UI element, and resets the world with the new // data. -function changeMod() { +function changeMod(settings) { var modName = currentMod() - reset() - loadData(modName) + reset("keepHash") + loadData(modName, settings) } // Triggered when the display rate is changed. @@ -321,7 +353,7 @@ var tabMap = { } // Triggered when a tab is clicked on. -function clickTab(tabName) { +function clickTab(tabName, keepHash) { currentTab = tabName var tabs = document.getElementsByClassName("tab") for (var i=0; i < tabs.length; i++) { @@ -336,7 +368,7 @@ function clickTab(tabName) { document.getElementById(tabName).style.display = "block" var button = document.getElementById(tabMap[tabName]) button.classList.add("active") - if (initDone) { + if (!keepHash) { window.location.hash = "#" + formatSettings() } } @@ -357,4 +389,3 @@ function toggleVisible(targetID) { target.style.display = "none" } } - diff --git a/fragment.js b/fragment.js index 0ad1a4a4..e2b0fcb6 100644 --- a/fragment.js +++ b/fragment.js @@ -134,3 +134,13 @@ function loadSettings(fragment) { } return settings } + +function loadFullSettings(fragment) { + var settings = loadSettings(fragment) + for (var name in DEFAULT_SETTINGS) { + if (!(name in settings)) { + settings[name] = DEFAULT_SETTINGS[name] + } + } + return settings +} diff --git a/init.js b/init.js index 3043c543..e1f02571 100644 --- a/init.js +++ b/init.js @@ -18,15 +18,15 @@ var shortModules // Array of item groups, in turn divided into subgroups. For display purposes. var itemGroups -var initDone = false - // Set the page back to a state immediately following initial setup, but before // the dataset is loaded for the first time. // // This is intended to be called when the top-level dataset is changed. // Therefore, it also resets the fragment and settings. -function reset() { - window.location.hash = "" +function reset(keepHash) { + if (!keepHash) { + window.location.hash = "" + } build_targets = [] var targetList = document.getElementById("targets") @@ -48,6 +48,80 @@ function reset() { oldTotals.parentNode.replaceChild(newTotals, oldTotals) } +function loadItems(settings) { + // clear current targets + build_targets = [] + var targetList = document.getElementById("targets") + targetList.innerHTML = targetList.lastChild.outerHTML + + if ("items" in settings && settings.items != "") { + var targets = settings.items.split(",") + for (var i=0; i < targets.length; i++) { + var targetString = targets[i] + var parts = targetString.split(":") + var name = parts[0] + var target = addTarget(name) + var type = parts[1] + if (type == "f") { + target.setFactories(parts[2]) + } else if (type == "r") { + target.setRate(parts[2]) + } else { + throw new Error("unknown target type") + } + } + } else { + addTarget() + } +} + +function loadModules(settings) { + if ("modules" in settings && settings.modules != "") { + var moduleSettings = settings.modules.split(",") + for (var i=0; i < moduleSettings.length; i++) { + var bothSettings = moduleSettings[i].split(";") + var factoryModuleSettings = bothSettings[0] + var beaconSettings = bothSettings[1] + + var singleModuleSettings = factoryModuleSettings.split(":") + var recipeName = singleModuleSettings[0] + var recipe = solver.recipes[recipeName] + var moduleNameList = singleModuleSettings.slice(1) + for (var j=0; j < moduleNameList.length; j++) { + var moduleName = moduleNameList[j] + if (moduleName && moduleName != "null") { + var module + if (moduleName in modules) { + module = modules[moduleName] + } else if (moduleName in shortModules) { + module = shortModules[moduleName] + } + if (module) { + spec.setModule(recipe, j, module) + } + } + } + if (beaconSettings) { + beaconSettings = beaconSettings.split(":") + var moduleName = beaconSettings[0] + var module = null + if (moduleName in modules) { + module = modules[moduleName] + } else if (moduleName in shortModules) { + module = shortModules[moduleName] + } + var factory = spec.getFactory(recipe) + var count = RationalFromFloat(Number(beaconSettings[1])) + if (module === spec.defaultBeacon) { + module = null + } + factory.beaconModule = module + factory.beaconCount = count + } + } + } +} + function loadDataRunner(modName, callback) { var xobj = new XMLHttpRequest() var mod = MODIFICATIONS[modName] @@ -96,71 +170,8 @@ function loadData(modName, settings) { solver = new Solver(items, recipes) renderSettings(settings) - - if ("items" in settings && settings.items != "") { - var targets = settings.items.split(",") - for (var i=0; i < targets.length; i++) { - var targetString = targets[i] - var parts = targetString.split(":") - var name = parts[0] - var target = addTarget(name) - var type = parts[1] - if (type == "f") { - target.setFactories(parts[2]) - } else if (type == "r") { - target.setRate(parts[2]) - } else { - throw new Error("unknown target type") - } - } - } else { - addTarget() - } - if ("modules" in settings && settings.modules != "") { - var moduleSettings = settings.modules.split(",") - for (var i=0; i < moduleSettings.length; i++) { - var bothSettings = moduleSettings[i].split(";") - var factoryModuleSettings = bothSettings[0] - var beaconSettings = bothSettings[1] - - var singleModuleSettings = factoryModuleSettings.split(":") - var recipeName = singleModuleSettings[0] - var recipe = recipes[recipeName] - var moduleNameList = singleModuleSettings.slice(1) - for (var j=0; j < moduleNameList.length; j++) { - var moduleName = moduleNameList[j] - if (moduleName && moduleName != "null") { - var module - if (moduleName in modules) { - module = modules[moduleName] - } else if (moduleName in shortModules) { - module = shortModules[moduleName] - } - if (module) { - spec.setModule(recipe, j, module) - } - } - } - if (beaconSettings) { - beaconSettings = beaconSettings.split(":") - var moduleName = beaconSettings[0] - var module = null - if (moduleName in modules) { - module = modules[moduleName] - } else if (moduleName in shortModules) { - module = shortModules[moduleName] - } - var factory = spec.getFactory(recipe) - var count = RationalFromFloat(Number(beaconSettings[1])) - if (module === spec.defaultBeacon) { - module = null - } - factory.beaconModule = module - factory.beaconCount = count - } - } - } - initDone = true + loadModules(settings) + loadItems(settings) itemUpdate() }) } @@ -174,5 +185,5 @@ function init() { loadData(currentMod(), settings) // We don't need to call clickVisualize here, as we will properly render // the graph when we call itemUpdate() at the end of initialization. - clickTab(currentTab) + clickTab(currentTab, "keepHash") } diff --git a/settings.js b/settings.js index 82fc459e..700bc710 100644 --- a/settings.js +++ b/settings.js @@ -226,6 +226,8 @@ function setMinPipe(lengthString) { } // mining productivity bonus +var DEFAULT_MINING_PROD = 0 + function renderMiningProd(settings) { if ("mprod" in settings) { var mprod = document.getElementById("mprod") @@ -342,3 +344,19 @@ function renderSettings(settings) { renderDefaultBeacon(settings) renderValueFormat(settings) } + +// this is easy to loop through to get missing settings +var DEFAULT_SETTINGS = { + "tab": DEFAULT_TAB, + "data": DEFAULT_MODIFICATION, + "rate": DEFAULT_RATE, + "rp": DEFAULT_RATE_PRECISION, + "cp": DEFAULT_COUNT_PRECISION, + "min": DEFAULT_MINIMUM, + // DEFAULT_FURNACE isn't set till later :( hacky + "furnace": "electric-furnace", + "belt": DEFAULT_BELT, + "pipe": DEFAULT_PIPE.toString(), + "mprod": DEFAULT_MINING_PROD, + "vf": DEFAULT_FORMAT[0] +} From f2d3fdf0fdc1f314aa98d67ccc9004f19af50682 Mon Sep 17 00:00:00 2001 From: Matt Moran Date: Sun, 12 Nov 2017 08:48:53 +1300 Subject: [PATCH 02/11] Removed keepHash, loadFullSettings changed hashchange to not trigger if it is already being handled moved `clickTab` in `init` to inside `loadDataRunner` callback to make it click the tab after the furnace has been set (otherwise `spec` is undefined when `clickTab` tries to `formatSettings`) --- display.js | 10 ++++------ events.js | 38 ++++++++++++++++++-------------------- fragment.js | 10 ---------- init.js | 11 +++-------- settings.js | 16 ---------------- 5 files changed, 25 insertions(+), 60 deletions(-) diff --git a/display.js b/display.js index 7183261e..fa7a6890 100644 --- a/display.js +++ b/display.js @@ -63,7 +63,7 @@ var globalTotals // This function obtains the set of item-rates to solve for from build_targets, // the set of modules from spec, and obtains a solution from solver. The // factory counts are then obtained from the solution using the spec. -function itemUpdate(keepHash) { +function itemUpdate() { var rates = {} for (var i = 0; i < build_targets.length; i++) { var target = build_targets[i] @@ -71,7 +71,7 @@ function itemUpdate(keepHash) { rates[target.itemName] = rate } globalTotals = solver.solve(rates, spec.ignore, spec) - display(keepHash) + display() } function Header(name, colSpan) { @@ -893,16 +893,14 @@ RecipeTable.prototype = { } // Re-renders the current solution, without re-computing it. -function display(keepHash) { +function display() { // Update the display of the target rate text boxes, if needed. for (var i = 0; i < build_targets.length; i++) { build_targets[i].getRate() } var totals = globalTotals - if (!keepHash) { - window.location.hash = "#" + formatSettings() - } + window.location.hash = "#" + formatSettings() if (currentTab == "graph_tab") { renderGraph(totals, spec.ignore) diff --git a/events.js b/events.js index ab109792..8aef4b72 100644 --- a/events.js +++ b/events.js @@ -2,22 +2,15 @@ // correctly handle back/forward buttons -var mouseOnPage = true +var navigationInProgress = false document.addEventListener("DOMContentLoaded", function() { - document.body.addEventListener("mouseenter", function() { - mouseOnPage = true - }); - document.body.addEventListener("mouseleave", function() { - mouseOnPage = false - }); -}); -window.addEventListener("hashchange", function() { - if (!mouseOnPage) { - var settings = loadFullSettings(window.location.hash) - if (settings.tab != DEFAULT_SETTINGS.tab) { - settings.tab += "_tab" + window.addEventListener("hashchange", function() { + if (navigationInProgress) { + return } - clickTab(settings.tab, "keepHash") + navigationInProgress = true + var settings = loadSettings(window.location.hash) + clickTab(settings.tab) if ("data" in settings && settings.data != currentMod()) { document.getElementById("data_set").value = settings.data @@ -29,7 +22,8 @@ window.addEventListener("hashchange", function() { loadItems(settings) itemUpdate() } - } + navigationInProgress = false + }); }); // build target events @@ -83,7 +77,7 @@ function RateHandler(target) { function changeMod(settings) { var modName = currentMod() - reset("keepHash") + reset() loadData(modName, settings) } @@ -353,7 +347,13 @@ var tabMap = { } // Triggered when a tab is clicked on. -function clickTab(tabName, keepHash) { +function clickTab(tabName) { + if (!tabName) { + tabName = DEFAULT_TAB + } + if (!tabName.endsWith("_tab")) { + tabName += "_tab" + } currentTab = tabName var tabs = document.getElementsByClassName("tab") for (var i=0; i < tabs.length; i++) { @@ -368,9 +368,7 @@ function clickTab(tabName, keepHash) { document.getElementById(tabName).style.display = "block" var button = document.getElementById(tabMap[tabName]) button.classList.add("active") - if (!keepHash) { - window.location.hash = "#" + formatSettings() - } + window.location.hash = "#" + formatSettings() } // Triggered when the "Visualize" tab is clicked on. diff --git a/fragment.js b/fragment.js index e2b0fcb6..0ad1a4a4 100644 --- a/fragment.js +++ b/fragment.js @@ -134,13 +134,3 @@ function loadSettings(fragment) { } return settings } - -function loadFullSettings(fragment) { - var settings = loadSettings(fragment) - for (var name in DEFAULT_SETTINGS) { - if (!(name in settings)) { - settings[name] = DEFAULT_SETTINGS[name] - } - } - return settings -} diff --git a/init.js b/init.js index e1f02571..7b0a5998 100644 --- a/init.js +++ b/init.js @@ -23,10 +23,8 @@ var itemGroups // // This is intended to be called when the top-level dataset is changed. // Therefore, it also resets the fragment and settings. -function reset(keepHash) { - if (!keepHash) { - window.location.hash = "" - } +function reset() { + window.location.hash = "" build_targets = [] var targetList = document.getElementById("targets") @@ -172,6 +170,7 @@ function loadData(modName, settings) { renderSettings(settings) loadModules(settings) loadItems(settings) + clickTab(settings.tab) itemUpdate() }) } @@ -179,11 +178,7 @@ function loadData(modName, settings) { function init() { var settings = loadSettings(window.location.hash) renderDataSetOptions(settings) - if ("tab" in settings) { - currentTab = settings.tab + "_tab" - } loadData(currentMod(), settings) // We don't need to call clickVisualize here, as we will properly render // the graph when we call itemUpdate() at the end of initialization. - clickTab(currentTab, "keepHash") } diff --git a/settings.js b/settings.js index aad9ad49..b6e6764d 100644 --- a/settings.js +++ b/settings.js @@ -356,19 +356,3 @@ function renderSettings(settings) { renderDefaultBeacon(settings) renderValueFormat(settings) } - -// this is easy to loop through to get missing settings -var DEFAULT_SETTINGS = { - "tab": DEFAULT_TAB, - "data": DEFAULT_MODIFICATION, - "rate": DEFAULT_RATE, - "rp": DEFAULT_RATE_PRECISION, - "cp": DEFAULT_COUNT_PRECISION, - "min": DEFAULT_MINIMUM, - // DEFAULT_FURNACE isn't set till later :( hacky - "furnace": "electric-furnace", - "belt": DEFAULT_BELT, - "pipe": DEFAULT_PIPE.toString(), - "mprod": DEFAULT_MINING_PROD, - "vf": DEFAULT_FORMAT[0] -} From 639a3d371a860d3ceffe5b8842219beb61b87ec5 Mon Sep 17 00:00:00 2001 From: Matt Moran Date: Tue, 14 Nov 2017 12:02:10 +1300 Subject: [PATCH 03/11] Changed formatSettings to updatehash `formatSettings` hash been renamed `updateHash` and directly modifies `window.location.hash`. `updateHash` does not run when the back/forward browser navigation button have been clicked. --- display.js | 2 +- events.js | 2 +- fragment.js | 10 +++++++--- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/display.js b/display.js index fa7a6890..1a4c0535 100644 --- a/display.js +++ b/display.js @@ -900,7 +900,7 @@ function display() { } var totals = globalTotals - window.location.hash = "#" + formatSettings() + updateHash() if (currentTab == "graph_tab") { renderGraph(totals, spec.ignore) diff --git a/events.js b/events.js index 8aef4b72..f6c12a0e 100644 --- a/events.js +++ b/events.js @@ -368,7 +368,7 @@ function clickTab(tabName) { document.getElementById(tabName).style.display = "block" var button = document.getElementById(tabMap[tabName]) button.classList.add("active") - window.location.hash = "#" + formatSettings() + updateHash() } // Triggered when the "Visualize" tab is clicked on. diff --git a/fragment.js b/fragment.js index 0ad1a4a4..26c79843 100644 --- a/fragment.js +++ b/fragment.js @@ -1,7 +1,11 @@ "use strict" -function formatSettings() { - var settings = "" +function updateHash() { + if (navigationInProgress) { + return + } + + var settings = "#" if (currentTab != DEFAULT_TAB) { settings += "tab=" + currentTab.slice(0, currentTab.indexOf("_")) + "&" } @@ -112,7 +116,7 @@ function formatSettings() { if (zip.length < settings.length) { return zip } - return settings + window.location.hash = settings } function loadSettings(fragment) { From 9f9455c210876b5d288508d7b6d05be9bae2d0bf Mon Sep 17 00:00:00 2001 From: Matt Moran Date: Tue, 14 Nov 2017 12:25:21 +1300 Subject: [PATCH 04/11] All tab names in JavaScript do not end in _tab Changed for consistency --- calc.html | 12 ++++++------ display.js | 2 +- events.js | 31 ++++++++++++++----------------- fragment.js | 2 +- 4 files changed, 22 insertions(+), 25 deletions(-) diff --git a/calc.html b/calc.html index 8e396c9a..8434b28b 100644 --- a/calc.html +++ b/calc.html @@ -62,13 +62,13 @@
- + - - - - - + + + + +
diff --git a/display.js b/display.js index 1a4c0535..45e78e1b 100644 --- a/display.js +++ b/display.js @@ -902,7 +902,7 @@ function display() { updateHash() - if (currentTab == "graph_tab") { + if (currentTab == "graph") { renderGraph(totals, spec.ignore) } recipeTable.displaySolution(totals) diff --git a/events.js b/events.js index f6c12a0e..54e3798b 100644 --- a/events.js +++ b/events.js @@ -10,8 +10,9 @@ document.addEventListener("DOMContentLoaded", function() { } navigationInProgress = true var settings = loadSettings(window.location.hash) - clickTab(settings.tab) - + if ("tab" in settings && settings.tab != currentTab) { + clickTab(settings.tab) + } if ("data" in settings && settings.data != currentMod()) { document.getElementById("data_set").value = settings.data changeMod(settings) @@ -332,18 +333,18 @@ function GraphClickHandler(node) { // tab events -var DEFAULT_TAB = "totals_tab" +var DEFAULT_TAB = "totals" var currentTab = DEFAULT_TAB var tabMap = { - "totals_tab": "totals_button", - "steps_tab": "steps_button", - "graph_tab": "graph_button", - "settings_tab": "settings_button", - "about_tab": "about_button", - "faq_tab": "faq_button", - "debug_tab": "debug_button", + "totals": "totals_button", + "steps": "steps_button", + "graph": "graph_button", + "settings": "settings_button", + "about": "about_button", + "faq": "faq_button", + "debug": "debug_button", } // Triggered when a tab is clicked on. @@ -351,9 +352,6 @@ function clickTab(tabName) { if (!tabName) { tabName = DEFAULT_TAB } - if (!tabName.endsWith("_tab")) { - tabName += "_tab" - } currentTab = tabName var tabs = document.getElementsByClassName("tab") for (var i=0; i < tabs.length; i++) { @@ -364,16 +362,15 @@ function clickTab(tabName) { for (var i=0; i < buttons.length; i++) { buttons[i].classList.remove("active") } - - document.getElementById(tabName).style.display = "block" + document.getElementById(tabName + "_tab").style.display = "block" var button = document.getElementById(tabMap[tabName]) button.classList.add("active") updateHash() } // Triggered when the "Visualize" tab is clicked on. -function clickVisualize(event, tabName) { - clickTab(event, tabName) +function clickVisualize(tabName) { + clickTab(tabName) renderGraph(globalTotals, spec.ignore) } diff --git a/fragment.js b/fragment.js index 26c79843..a67b7972 100644 --- a/fragment.js +++ b/fragment.js @@ -7,7 +7,7 @@ function updateHash() { var settings = "#" if (currentTab != DEFAULT_TAB) { - settings += "tab=" + currentTab.slice(0, currentTab.indexOf("_")) + "&" + settings += "tab=" + currentTab + "&" } var mod = currentMod() if (mod != DEFAULT_MODIFICATION) { From 48e13589c7bb03bc547b973e9961f549fab5280c Mon Sep 17 00:00:00 2001 From: Matt Moran Date: Tue, 14 Nov 2017 13:15:15 +1300 Subject: [PATCH 05/11] Removed comment --- init.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/init.js b/init.js index 7b0a5998..117ab80c 100644 --- a/init.js +++ b/init.js @@ -179,6 +179,4 @@ function init() { var settings = loadSettings(window.location.hash) renderDataSetOptions(settings) loadData(currentMod(), settings) - // We don't need to call clickVisualize here, as we will properly render - // the graph when we call itemUpdate() at the end of initialization. } From daa5cebe649c848d1dd0d2d34f0324a0c7d5f369 Mon Sep 17 00:00:00 2001 From: Matt Moran Date: Tue, 14 Nov 2017 21:59:36 +1300 Subject: [PATCH 06/11] Stopped double processing for each action Added a flag if it is a `userTriggeredHashUpdate` which makes the `hashchange` handler do nothing. The flag is reset on each hashchange --- events.js | 6 ++++++ fragment.js | 1 + 2 files changed, 7 insertions(+) diff --git a/events.js b/events.js index 54e3798b..de06c1dc 100644 --- a/events.js +++ b/events.js @@ -2,12 +2,18 @@ // correctly handle back/forward buttons +var userTriggeredHashUpdate = false var navigationInProgress = false document.addEventListener("DOMContentLoaded", function() { window.addEventListener("hashchange", function() { if (navigationInProgress) { return } + if (userTriggeredHashUpdate) { + // reset flag for next hashChange + userTriggeredHashUpdate = false + return + } navigationInProgress = true var settings = loadSettings(window.location.hash) if ("tab" in settings && settings.tab != currentTab) { diff --git a/fragment.js b/fragment.js index a67b7972..360f9212 100644 --- a/fragment.js +++ b/fragment.js @@ -4,6 +4,7 @@ function updateHash() { if (navigationInProgress) { return } + userTriggeredHashUpdate = true; var settings = "#" if (currentTab != DEFAULT_TAB) { From 0f0b3309f3b0ad043eb1b103e5fee9c2dea0cc54 Mon Sep 17 00:00:00 2001 From: Matt Moran Date: Wed, 15 Nov 2017 09:22:10 +1300 Subject: [PATCH 07/11] Removed unnecessary navigationInProgress flag --- events.js | 5 ----- 1 file changed, 5 deletions(-) diff --git a/events.js b/events.js index de06c1dc..e29c22d4 100644 --- a/events.js +++ b/events.js @@ -3,12 +3,8 @@ // correctly handle back/forward buttons var userTriggeredHashUpdate = false -var navigationInProgress = false document.addEventListener("DOMContentLoaded", function() { window.addEventListener("hashchange", function() { - if (navigationInProgress) { - return - } if (userTriggeredHashUpdate) { // reset flag for next hashChange userTriggeredHashUpdate = false @@ -29,7 +25,6 @@ document.addEventListener("DOMContentLoaded", function() { loadItems(settings) itemUpdate() } - navigationInProgress = false }); }); From 32edcb02803d4a88e526e7d9c252d2b29e930f9c Mon Sep 17 00:00:00 2001 From: Matt Moran Date: Wed, 15 Nov 2017 09:55:06 +1300 Subject: [PATCH 08/11] Fixed bug referencing undefined variable I didn't test it before committing (previously)... --- events.js | 7 +++---- fragment.js | 5 +---- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/events.js b/events.js index e29c22d4..6fd24ceb 100644 --- a/events.js +++ b/events.js @@ -2,15 +2,14 @@ // correctly handle back/forward buttons -var userTriggeredHashUpdate = false +var plannedHashUpdate = false document.addEventListener("DOMContentLoaded", function() { window.addEventListener("hashchange", function() { - if (userTriggeredHashUpdate) { + if (plannedHashUpdate) { // reset flag for next hashChange - userTriggeredHashUpdate = false + plannedHashUpdate = false return } - navigationInProgress = true var settings = loadSettings(window.location.hash) if ("tab" in settings && settings.tab != currentTab) { clickTab(settings.tab) diff --git a/fragment.js b/fragment.js index 360f9212..bcbf93d2 100644 --- a/fragment.js +++ b/fragment.js @@ -1,10 +1,7 @@ "use strict" function updateHash() { - if (navigationInProgress) { - return - } - userTriggeredHashUpdate = true; + plannedHashUpdate = true; var settings = "#" if (currentTab != DEFAULT_TAB) { From 2885741aa3040cdc9b3428c97446bfa69a5b40c4 Mon Sep 17 00:00:00 2001 From: Matt Moran Date: Wed, 15 Nov 2017 10:28:52 +1300 Subject: [PATCH 09/11] Reinstated navigationInProgress Also changed `clickTab` to only click the tab if it is different to the current tab --- events.js | 12 ++++++++---- fragment.js | 3 +++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/events.js b/events.js index 6fd24ceb..e456bc59 100644 --- a/events.js +++ b/events.js @@ -3,6 +3,7 @@ // correctly handle back/forward buttons var plannedHashUpdate = false +var navigationInProgress = false document.addEventListener("DOMContentLoaded", function() { window.addEventListener("hashchange", function() { if (plannedHashUpdate) { @@ -10,10 +11,8 @@ document.addEventListener("DOMContentLoaded", function() { plannedHashUpdate = false return } + navigationInProgress = true var settings = loadSettings(window.location.hash) - if ("tab" in settings && settings.tab != currentTab) { - clickTab(settings.tab) - } if ("data" in settings && settings.data != currentMod()) { document.getElementById("data_set").value = settings.data changeMod(settings) @@ -24,6 +23,8 @@ document.addEventListener("DOMContentLoaded", function() { loadItems(settings) itemUpdate() } + clickTab(settings.tab) + navigationInProgress = false }); }); @@ -335,7 +336,7 @@ function GraphClickHandler(node) { var DEFAULT_TAB = "totals" -var currentTab = DEFAULT_TAB +var currentTab var tabMap = { "totals": "totals_button", @@ -352,6 +353,9 @@ function clickTab(tabName) { if (!tabName) { tabName = DEFAULT_TAB } + if (tabName == currentTab) { + return + } currentTab = tabName var tabs = document.getElementsByClassName("tab") for (var i=0; i < tabs.length; i++) { diff --git a/fragment.js b/fragment.js index bcbf93d2..6672dc39 100644 --- a/fragment.js +++ b/fragment.js @@ -1,6 +1,9 @@ "use strict" function updateHash() { + if (navigationInProgress) { + return + } plannedHashUpdate = true; var settings = "#" From 771371246154d25f2c1463c59722236063cbe065 Mon Sep 17 00:00:00 2001 From: Matt Moran Date: Wed, 15 Nov 2017 12:07:40 +1300 Subject: [PATCH 10/11] plannedHashUpdate flag is only set if the hash actually will be updated --- fragment.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/fragment.js b/fragment.js index 6672dc39..604eb34a 100644 --- a/fragment.js +++ b/fragment.js @@ -4,7 +4,6 @@ function updateHash() { if (navigationInProgress) { return } - plannedHashUpdate = true; var settings = "#" if (currentTab != DEFAULT_TAB) { @@ -115,9 +114,12 @@ function updateHash() { } var zip = "zip=" + window.btoa(pako.deflateRaw(settings, {to: "string"})) if (zip.length < settings.length) { - return zip + settings = zip + } + if (window.location.hash != settings) { + plannedHashUpdate = true; + window.location.hash = settings } - window.location.hash = settings } function loadSettings(fragment) { From da93afb1014880a4d3e8cfff140c962c09211c40 Mon Sep 17 00:00:00 2001 From: Matt Moran Date: Sun, 26 Nov 2017 14:34:59 +1300 Subject: [PATCH 11/11] Changed updateSettings Changed updateSettings to add the hash last, just before assignment to window.location.hash. This fixes unwanted behaviour with the zip'ed hash. --- fragment.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/fragment.js b/fragment.js index 604eb34a..339c38fb 100644 --- a/fragment.js +++ b/fragment.js @@ -5,7 +5,7 @@ function updateHash() { return } - var settings = "#" + var settings = "" if (currentTab != DEFAULT_TAB) { settings += "tab=" + currentTab + "&" } @@ -116,6 +116,7 @@ function updateHash() { if (zip.length < settings.length) { settings = zip } + settings = "#" + settings if (window.location.hash != settings) { plannedHashUpdate = true; window.location.hash = settings