@geogirafe/lib-geoportal
Version:
GeoGirafe is a flexible application to build online geoportals.
55 lines (54 loc) • 2.03 kB
JavaScript
// 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';
initializeSingleton() {
super.initializeSingleton();
this.loadThemeFavorites();
}
saveThemeFavorites() {
const serializedThemeFavorites = JSON.stringify(this.themeFavorites);
this.context.userDataManager.saveUserData(this.storagePath, serializedThemeFavorites);
}
loadThemeFavorites() {
const serializedThemeFavorites = this.context.userDataManager.getUserData(this.storagePath);
if (serializedThemeFavorites) {
this.themeFavorites = JSON.parse(serializedThemeFavorites);
}
}
addThemeToFavorites(theme) {
this.themeFavorites.push(theme instanceof ThemeLayer ? theme.id : theme.name);
this.saveThemeFavorites();
}
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);
this.saveThemeFavorites();
}
else {
throw new Error('The theme to be removed cannot be found in the list of favorites');
}
}
addOrRemoveThemeFromFavorites(theme) {
if (this.isThemeInFavorites(theme)) {
this.removeThemeFromFavorites(theme);
}
else {
this.addThemeToFavorites(theme);
}
}
clearThemeFavorites() {
this.themeFavorites = [];
this.saveThemeFavorites();
}
isThemeInFavorites(theme) {
return this.themeFavorites.includes(theme instanceof ThemeLayer ? theme.id : theme.name);
}
getThemeFavorites() {
return this.themeFavorites;
}
}
export default ThemeFavoritesManager;