MapComplete/src/Models/MenuState.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

121 lines
3.9 KiB
TypeScript
Raw Normal View History

import LayerConfig from "./ThemeConfig/LayerConfig"
import { UIEventSource } from "../Logic/UIEventSource"
2023-04-07 03:54:11 +02:00
import UserRelatedState from "../Logic/State/UserRelatedState"
import { Utils } from "../Utils"
import Zoomcontrol from "../UI/Zoomcontrol"
import { LocalStorageSource } from "../Logic/Web/LocalStorageSource"
export type PageType = (typeof MenuState.pageNames)[number]
/**
* Indicates if a menu is open, and if so, which tab is selected;
* Some tabs allow to highlight an element.
*
* Some convenience methods are provided for this as well
*/
export class MenuState {
2023-05-18 15:44:54 +02:00
public static readonly pageNames = [
"copyright", "copyright_icons", "community_index", "hotkeys",
"privacy", "filter", "background", "about_theme", "download", "favourites",
2024-09-02 00:26:02 +02:00
"usersettings", "share", "menu",
] as const
2024-06-27 03:39:04 +02:00
public readonly pageStates: Record<PageType, UIEventSource<boolean>>
2023-06-07 02:42:49 +02:00
public readonly highlightedLayerInFilters: UIEventSource<string> = new UIEventSource<string>(
2024-09-02 00:26:02 +02:00
undefined,
)
2023-04-07 02:13:57 +02:00
public highlightedUserSetting: UIEventSource<string> = new UIEventSource<string>(undefined)
2023-06-07 02:42:49 +02:00
constructor(shouldShowWelcomeMessage: boolean, themeid: string) {
// Note: this class is _not_ responsible to update the Hash, @see ThemeViewStateHashActor for this
const states = {}
for (const pageName of MenuState.pageNames) {
const toggle = new UIEventSource(false)
states[pageName] = toggle
2024-08-31 16:25:41 +02:00
}
this.pageStates = <Record<PageType, UIEventSource<boolean>>>states
2024-09-02 00:26:02 +02:00
for (const pageName of MenuState.pageNames) {
if(pageName === "menu"){
continue
}
2024-08-31 16:43:25 +02:00
this.pageStates[pageName].addCallback(enabled => {
if (enabled) {
2024-09-02 00:26:02 +02:00
this.pageStates.menu.set(false)
}
})
}
2024-09-02 00:26:02 +02:00
const visitedBefore = LocalStorageSource.GetParsed<boolean>(
2024-09-02 00:26:02 +02:00
themeid + "thememenuisopened", false,
)
if (!visitedBefore.data && shouldShowWelcomeMessage) {
this.pageStates.about_theme.set(true)
visitedBefore.set(true)
}
}
private resetZoomIfAllClosed() {
if (this.isSomethingOpen()) {
return
}
Zoomcontrol.resetzoom()
}
2023-06-07 02:42:49 +02:00
public openFilterView(highlightLayer?: LayerConfig | string) {
this.pageStates.filter.setData(true)
if (highlightLayer) {
if (typeof highlightLayer !== "string") {
highlightLayer = highlightLayer.id
}
this.highlightedLayerInFilters.setData(highlightLayer)
}
}
2023-04-07 02:13:57 +02:00
public openUsersettings(highlightTagRendering?: string) {
2023-04-07 03:54:11 +02:00
if (
highlightTagRendering !== undefined &&
!UserRelatedState.availableUserSettingsIds.some((tr) => tr === highlightTagRendering)
) {
console.error(
"No tagRendering with id '" + highlightTagRendering + "'; maybe you meant:",
Utils.sortedByLevenshteinDistance(
highlightTagRendering,
UserRelatedState.availableUserSettingsIds,
2024-09-02 00:26:02 +02:00
(x) => x,
),
2023-04-07 03:54:11 +02:00
)
}
2023-04-07 02:13:57 +02:00
this.highlightedUserSetting.setData(highlightTagRendering)
this.pageStates.usersettings.set(true)
2023-04-07 02:13:57 +02:00
}
public isSomethingOpen(): boolean {
return Object.values(this.pageStates).some((t) => t.data)
}
2023-06-07 02:42:49 +02:00
/**
* Close all floatOvers.
* Returns 'true' if at least one menu was opened
*/
public closeAll(): boolean {
2024-09-02 00:26:02 +02:00
const ps = this.pageStates
for (const key in ps) {
const toggle = ps[key]
const wasOpen = toggle.data
toggle.setData(false)
if (wasOpen) {
return true
}
}
2024-09-02 00:26:02 +02:00
if (ps.menu.data) {
ps.menu.set(false)
return true
}
}
}