2021-12-09 14:18:38 +01:00
|
|
|
|
|
|
|
|
import { defineStore } from 'pinia';
|
2021-12-15 14:36:45 +01:00
|
|
|
import { UiFlavorProvider, UiIconSet, UiSection, UiSettingsGroup, UiStoreState } from 'zero/ui';
|
2021-12-09 14:18:38 +01:00
|
|
|
import api from './api';
|
|
|
|
|
|
2021-12-16 20:01:43 +01:00
|
|
|
const THEME_KEY = 'zero.theme';
|
|
|
|
|
|
2022-01-05 16:46:32 +01:00
|
|
|
export let sectionMods = [];
|
|
|
|
|
export let settingsMods = [];
|
|
|
|
|
|
2021-12-09 14:18:38 +01:00
|
|
|
export const useUiStore = defineStore('zero.ui', {
|
|
|
|
|
state: () => ({
|
2021-12-16 20:01:43 +01:00
|
|
|
preferences: {
|
|
|
|
|
theme: 'dark'
|
|
|
|
|
},
|
|
|
|
|
|
2021-12-09 14:18:38 +01:00
|
|
|
sections: [],
|
|
|
|
|
settingGroups: [],
|
2021-12-15 14:36:45 +01:00
|
|
|
iconSets: [],
|
2021-12-15 15:34:05 +01:00
|
|
|
flavors: [],
|
|
|
|
|
blueprints: []
|
2021-12-09 14:18:38 +01:00
|
|
|
} as UiStoreState),
|
|
|
|
|
|
|
|
|
|
actions: {
|
2021-12-16 20:01:43 +01:00
|
|
|
async setup(anonymous)
|
2021-12-09 14:18:38 +01:00
|
|
|
{
|
2021-12-16 20:01:43 +01:00
|
|
|
this.setTheme(localStorage.getItem(THEME_KEY) || 'default');
|
|
|
|
|
|
|
|
|
|
if (anonymous)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-09 14:18:38 +01:00
|
|
|
const values = await Promise.all([
|
|
|
|
|
api.getSections(),
|
|
|
|
|
api.getSettingGroups(),
|
2021-12-15 14:36:45 +01:00
|
|
|
api.getIconSets(),
|
2021-12-15 15:34:05 +01:00
|
|
|
api.getFlavors(),
|
|
|
|
|
api.getBlueprints()
|
2021-12-09 14:18:38 +01:00
|
|
|
]);
|
|
|
|
|
|
2021-12-14 12:16:27 +01:00
|
|
|
this.sections = values[0].data as UiSection[];
|
|
|
|
|
this.settingGroups = values[1].data as UiSettingsGroup[];
|
|
|
|
|
this.iconSets = values[2].data as UiIconSet[];
|
2021-12-15 14:36:45 +01:00
|
|
|
this.flavors = values[3].data as Record<string, UiFlavorProvider>;
|
2021-12-15 15:34:05 +01:00
|
|
|
this.blueprints = values[4].data as string[];
|
2022-01-05 16:46:32 +01:00
|
|
|
|
|
|
|
|
sectionMods.forEach(mod =>
|
|
|
|
|
{
|
|
|
|
|
mod(this.sections);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
settingsMods.forEach(mod =>
|
|
|
|
|
{
|
|
|
|
|
mod(this.settingGroups);
|
|
|
|
|
});
|
2021-12-16 20:01:43 +01:00
|
|
|
},
|
|
|
|
|
|
2022-01-12 14:11:41 +01:00
|
|
|
getFlavors(alias: string)
|
|
|
|
|
{
|
|
|
|
|
let flavorConfig = this.flavors[alias] || {
|
|
|
|
|
canUseWithoutFlavors: true,
|
|
|
|
|
flavors: []
|
|
|
|
|
} as UiFlavorProvider;
|
|
|
|
|
|
|
|
|
|
let flavors = JSON.parse(JSON.stringify(flavorConfig.flavors));
|
|
|
|
|
|
|
|
|
|
if (flavorConfig.canUseWithoutFlavors && flavors.length > 0)
|
|
|
|
|
{
|
|
|
|
|
flavors.splice(0, 0, {
|
|
|
|
|
name: '@flavors.default.name',
|
|
|
|
|
description: '@flavors.default.text',
|
|
|
|
|
alias: null,
|
|
|
|
|
icon: 'fth-box'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return flavors;
|
|
|
|
|
},
|
|
|
|
|
|
2021-12-16 20:01:43 +01:00
|
|
|
setTheme(theme: 'default' | 'light' | 'dark')
|
|
|
|
|
{
|
|
|
|
|
const dark = theme === 'dark';
|
|
|
|
|
this.preferences.theme = theme;
|
|
|
|
|
localStorage.setItem(THEME_KEY, theme);
|
|
|
|
|
|
|
|
|
|
document.body.classList.toggle('theme-light', !dark);
|
|
|
|
|
document.body.classList.toggle('theme-dark', dark);
|
2021-12-09 14:18:38 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|