web-apps/apps/common/mobile/lib/controller/Themes.js

58 lines
1.8 KiB
JavaScript
Raw Normal View History

2021-11-18 19:39:33 +00:00
import { Dom7 } from 'framework7'
import { LocalStorage } from "../../utils/LocalStorage";
class ThemesController {
constructor() {
this.themes_map = {
dark : {
id: 'theme-dark',
type:'dark'
},
light: {
id: 'theme-light',
type: 'light'
}};
2022-07-13 14:15:18 +00:00
}
2021-11-18 19:39:33 +00:00
2022-07-13 14:15:18 +00:00
init() {
2021-11-18 19:39:33 +00:00
const obj = LocalStorage.getItem("ui-theme");
let theme = this.themes_map.light;
if ( !!obj )
theme = this.themes_map[JSON.parse(obj).type];
else
if ( window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ) {
theme = this.themes_map['dark'];
LocalStorage.setItem("ui-theme", JSON.stringify(theme));
}
2022-07-13 14:15:18 +00:00
this.switchDarkTheme(theme, true);
2021-11-18 19:39:33 +00:00
}
get isCurrentDark() {
const obj = LocalStorage.getItem("ui-theme");
return !!obj ? JSON.parse(obj).type === 'dark' : false;
}
switchDarkTheme(dark) {
2022-07-13 14:15:18 +00:00
const theme = typeof dark == 'object' ? dark : this.themes_map[dark ? 'dark' : 'light'];
const refresh_only = !!arguments[1];
if ( !refresh_only )
LocalStorage.setItem("ui-theme", JSON.stringify(theme));
2021-11-18 19:39:33 +00:00
const $body = $$('body');
$body.attr('class') && $body.attr('class', $body.attr('class').replace(/\s?theme-type-(?:dark|light)/, ''));
$body.addClass(`theme-type-${theme.type}`);
2022-07-13 14:15:18 +00:00
const on_engine_created = api => {
api.asc_setSkin(theme.id);
};
const api = Common.EditorApi ? Common.EditorApi.get() : undefined;
if(!api) Common.Notifications.on('engineCreated', on_engine_created);
else on_engine_created(api);
2021-11-18 19:39:33 +00:00
}
}
const themes = new ThemesController()
export {themes as Themes}