diff --git a/Logic/Web/NavigatorBackButtonHandler.ts b/Logic/Web/ThemeViewStateHashActor.ts similarity index 87% rename from Logic/Web/NavigatorBackButtonHandler.ts rename to Logic/Web/ThemeViewStateHashActor.ts index 244d192322..c6e18b3542 100644 --- a/Logic/Web/NavigatorBackButtonHandler.ts +++ b/Logic/Web/ThemeViewStateHashActor.ts @@ -1,11 +1,13 @@ import ThemeViewState from "../../Models/ThemeViewState"; import Hash from "./Hash"; -export default class NavigatorBackButtonHandler { +export default class ThemeViewStateHashActor { private readonly _state: ThemeViewState; /** - * Handles the 'back'-button events. + * Converts the hash to the appropriate themeview state and, vice versa, sets the hash. + * + * As the navigator-back-button changes the hash first, this class thus also handles the 'back'-button events. * * Note that there is no "real" way to intercept the back button, we can only detect the removal of the hash. * As such, we use a change in the hash to close the appropriate windows @@ -15,9 +17,20 @@ export default class NavigatorBackButtonHandler { constructor(state: ThemeViewState) { this._state = state; + // First of all, try to recover the selected element + if (Hash.hash.data) { + const hash = Hash.hash.data + this.loadStateFromHash(hash) + Hash.hash.setData(hash) // reapply the previous hash + state.indexedFeatures.featuresById.addCallbackAndRunD(_ => { + let unregister = this.loadSelectedElementFromHash(hash); + // once that we have found a matching element, we can be sure the indexedFeaturesource was popuplated and that the job is done + return unregister + }) + } + // Register a hash change listener to correctly handle the back button Hash.hash.addCallback(hash => { - console.log("Current hash", hash) if (!!hash) { // There is still a hash // We _only_ have to (at most) close the overlays in this case @@ -31,22 +44,18 @@ export default class NavigatorBackButtonHandler { } }) - state.selectedElement.addCallbackAndRun(_ => this.setHash()) + // At last, register callbacks on the state to update the hash when they change. + // Note: these should use 'addCallback', not 'addCallbackAndRun' + state.selectedElement.addCallback(_ => this.setHash()) state.guistate.allToggles.forEach(({toggle, submenu}) => { submenu?.addCallback(_ => this.setHash()) toggle.addCallback(_ => this.setHash()); }) - if (Hash.hash.data) { - const hash = Hash.hash.data - this.loadStateFromHash(hash) - Hash.hash.setData(hash) // reapply the previous hash - state.indexedFeatures.featuresById.addCallbackAndRunD(_ => { - let unregister = this.loadSelectedElementFromHash(hash); - // once that we have found a matching element, we can be sure the indexedFeaturesource was popuplated and that the job is done - return unregister - }) - } + // When all is done, set the hash. This must happen last to give the code above correct info + this.setHash() + + } /** diff --git a/Models/MenuState.ts b/Models/MenuState.ts index c1ec9b4e76..1c264e17dd 100644 --- a/Models/MenuState.ts +++ b/Models/MenuState.ts @@ -55,7 +55,6 @@ export class MenuState { [], (str) => MenuState._menuviewTabs.indexOf(str) ) - this.menuViewTab.addCallbackAndRunD(s => console.trace("Menu view tab state is", s, this.menuIsOpened.data)) this.menuIsOpened.addCallbackAndRun((isOpen) => { if (!isOpen) { this.highlightedUserSetting.setData(undefined) diff --git a/Models/ThemeViewState.ts b/Models/ThemeViewState.ts index f4e0ba707f..ffdcdaad6f 100644 --- a/Models/ThemeViewState.ts +++ b/Models/ThemeViewState.ts @@ -45,7 +45,7 @@ import {EliCategory} from "./RasterLayerProperties" import BackgroundLayerResetter from "../Logic/Actors/BackgroundLayerResetter" import SaveFeatureSourceToLocalStorage from "../Logic/FeatureSource/Actors/SaveFeatureSourceToLocalStorage" import BBoxFeatureSource from "../Logic/FeatureSource/Sources/TouchesBboxFeatureSource" -import NavigatorBackButtonHandler from "../Logic/Web/NavigatorBackButtonHandler"; +import ThemeViewStateHashActor from "../Logic/Web/ThemeViewStateHashActor"; /** * @@ -545,7 +545,7 @@ export default class ThemeViewState implements SpecialVisualizationState { } }) } - new NavigatorBackButtonHandler(this) + new ThemeViewStateHashActor(this) new MetaTagging(this) new TitleHandler(this.selectedElement, this.selectedLayer, this.featureProperties, this) new ChangeToElementsActor(this.changes, this.featureProperties)