UNPKG

@geogirafe/lib-geoportal

Version:

GeoGirafe is a flexible application to build online geoportals.

55 lines (54 loc) • 2.13 kB
// SPDX-License-Identifier: Apache-2.0 import GirafeSingleton from '../../base/GirafeSingleton.js'; import ThemeLayer from '../../models/layers/themelayer.js'; class ThemeFavoritesManager extends GirafeSingleton { themeFavorites = []; storagePath = 'themeFavorites'; async initializeSingleton() { await super.initializeSingleton(); await this.loadThemeFavorites(); } async saveThemeFavorites() { const serializedThemeFavorites = JSON.stringify(this.themeFavorites); await this.context.userDataManager.saveUserData(this.storagePath, serializedThemeFavorites); } async loadThemeFavorites() { const serializedThemeFavorites = (await this.context.userDataManager.getUserData(this.storagePath)); if (serializedThemeFavorites) { this.themeFavorites = JSON.parse(serializedThemeFavorites); } } async addThemeToFavorites(theme) { this.themeFavorites.push(theme instanceof ThemeLayer ? theme.id : theme.name); await this.saveThemeFavorites(); } async removeThemeFromFavorites(theme) { const index = this.themeFavorites.findIndex((themeIdOrName) => theme instanceof ThemeLayer ? themeIdOrName === theme.id : themeIdOrName === theme.name); if (index >= 0) { this.themeFavorites.splice(index, 1); await this.saveThemeFavorites(); } else { throw new Error('The theme to be removed cannot be found in the list of favorites'); } } async addOrRemoveThemeFromFavorites(theme) { if (this.isThemeInFavorites(theme)) { await this.removeThemeFromFavorites(theme); } else { await this.addThemeToFavorites(theme); } } async clearThemeFavorites() { this.themeFavorites = []; await this.saveThemeFavorites(); } isThemeInFavorites(theme) { return this.themeFavorites.includes(theme instanceof ThemeLayer ? theme.id : theme.name); } getThemeFavorites() { return this.themeFavorites; } } export default ThemeFavoritesManager;