cinny/src/client/state/settings.js

97 lines
2.8 KiB
JavaScript
Raw Normal View History

2021-08-08 16:26:34 +00:00
import EventEmitter from 'events';
import appDispatcher from '../dispatcher';
import cons from './cons';
function getSettings() {
const settings = localStorage.getItem('settings');
if (settings === null) return null;
return JSON.parse(settings);
}
function setSettings(key, value) {
let settings = getSettings();
if (settings === null) settings = {};
settings[key] = value;
localStorage.setItem('settings', JSON.stringify(settings));
}
class Settings extends EventEmitter {
2021-07-28 13:15:52 +00:00
constructor() {
2021-08-08 16:26:34 +00:00
super();
2021-07-28 13:15:52 +00:00
this.themes = ['', 'silver-theme', 'dark-theme', 'butter-theme'];
this.themeIndex = this.getThemeIndex();
2021-08-08 16:26:34 +00:00
this.isMarkdown = this.getIsMarkdown();
this.isPeopleDrawer = this.getIsPeopleDrawer();
this.isTouchScreenDevice = ('ontouchstart' in window) || (navigator.maxTouchPoints > 0) || (navigator.msMaxTouchPoints > 0);
2021-07-28 13:15:52 +00:00
}
getThemeIndex() {
if (typeof this.themeIndex === 'number') return this.themeIndex;
2021-08-08 16:26:34 +00:00
const settings = getSettings();
2021-07-28 13:15:52 +00:00
if (settings === null) return 0;
if (typeof settings.themeIndex === 'undefined') return 0;
// eslint-disable-next-line radix
return parseInt(settings.themeIndex);
}
getThemeName() {
return this.themes[this.themeIndex];
}
setTheme(themeIndex) {
const appBody = document.getElementById('appBody');
this.themes.forEach((themeName) => {
if (themeName === '') return;
appBody.classList.remove(themeName);
});
if (this.themes[themeIndex] !== '') appBody.classList.add(this.themes[themeIndex]);
2021-08-08 16:26:34 +00:00
setSettings('themeIndex', themeIndex);
2021-07-28 13:15:52 +00:00
this.themeIndex = themeIndex;
}
2021-08-08 16:26:34 +00:00
getIsMarkdown() {
if (typeof this.isMarkdown === 'boolean') return this.isMarkdown;
const settings = getSettings();
if (settings === null) return false;
if (typeof settings.isMarkdown === 'undefined') return false;
return settings.isMarkdown;
}
getIsPeopleDrawer() {
if (typeof this.isPeopleDrawer === 'boolean') return this.isPeopleDrawer;
const settings = getSettings();
if (settings === null) return true;
if (typeof settings.isPeopleDrawer === 'undefined') return true;
return settings.isPeopleDrawer;
}
2021-08-08 16:26:34 +00:00
setter(action) {
const actions = {
[cons.actions.settings.TOGGLE_MARKDOWN]: () => {
this.isMarkdown = !this.isMarkdown;
setSettings('isMarkdown', this.isMarkdown);
this.emit(cons.events.settings.MARKDOWN_TOGGLED, this.isMarkdown);
},
[cons.actions.settings.TOGGLE_PEOPLE_DRAWER]: () => {
this.isPeopleDrawer = !this.isPeopleDrawer;
setSettings('isPeopleDrawer', this.isPeopleDrawer);
this.emit(cons.events.settings.PEOPLE_DRAWER_TOGGLED, this.isPeopleDrawer);
},
2021-08-08 16:26:34 +00:00
};
actions[action.type]?.();
}
2021-07-28 13:15:52 +00:00
}
const settings = new Settings();
2021-08-08 16:26:34 +00:00
appDispatcher.register(settings.setter.bind(settings));
2021-07-28 13:15:52 +00:00
export default settings;