2021-08-08 19:26:34 +03: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 16:15:52 +03:00
|
|
|
constructor() {
|
2021-08-08 19:26:34 +03:00
|
|
|
super();
|
|
|
|
|
2021-07-28 16:15:52 +03:00
|
|
|
this.themes = ['', 'silver-theme', 'dark-theme', 'butter-theme'];
|
|
|
|
this.themeIndex = this.getThemeIndex();
|
2021-08-08 19:26:34 +03:00
|
|
|
|
2022-01-03 15:16:43 +02:00
|
|
|
this.useSystemTheme = this.getUseSystemTheme();
|
2021-08-08 19:26:34 +03:00
|
|
|
this.isMarkdown = this.getIsMarkdown();
|
2021-11-15 05:53:59 +02:00
|
|
|
this.isPeopleDrawer = this.getIsPeopleDrawer();
|
2021-12-12 17:23:32 +02:00
|
|
|
this.hideMembershipEvents = this.getHideMembershipEvents();
|
|
|
|
this.hideNickAvatarEvents = this.getHideNickAvatarEvents();
|
2022-01-29 16:20:51 +02:00
|
|
|
this._showNotifications = this.getShowNotifications();
|
2022-03-18 05:37:11 +02:00
|
|
|
this.isNotificationSounds = this.getIsNotificationSounds();
|
2021-08-25 11:10:38 +03:00
|
|
|
|
|
|
|
this.isTouchScreenDevice = ('ontouchstart' in window) || (navigator.maxTouchPoints > 0) || (navigator.msMaxTouchPoints > 0);
|
2021-07-28 16:15:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
getThemeIndex() {
|
|
|
|
if (typeof this.themeIndex === 'number') return this.themeIndex;
|
|
|
|
|
2021-08-08 19:26:34 +03:00
|
|
|
const settings = getSettings();
|
2021-07-28 16:15:52 +03: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];
|
|
|
|
}
|
|
|
|
|
2022-07-09 15:38:35 +03:00
|
|
|
_clearTheme() {
|
|
|
|
document.body.classList.remove('system-theme');
|
2021-07-28 16:15:52 +03:00
|
|
|
this.themes.forEach((themeName) => {
|
|
|
|
if (themeName === '') return;
|
2022-07-09 15:38:35 +03:00
|
|
|
document.body.classList.remove(themeName);
|
2021-07-28 16:15:52 +03:00
|
|
|
});
|
2022-07-09 15:38:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
applyTheme() {
|
|
|
|
this._clearTheme();
|
2022-01-07 06:51:35 +02:00
|
|
|
if (this.useSystemTheme) {
|
2022-07-09 15:38:35 +03:00
|
|
|
document.body.classList.add('system-theme');
|
|
|
|
} else if (this.themes[this.themeIndex]) {
|
|
|
|
document.body.classList.add(this.themes[this.themeIndex]);
|
2022-01-07 06:51:35 +02:00
|
|
|
}
|
2022-07-09 15:38:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
setTheme(themeIndex) {
|
2021-07-28 16:15:52 +03:00
|
|
|
this.themeIndex = themeIndex;
|
2022-07-09 15:38:35 +03:00
|
|
|
setSettings('themeIndex', this.themeIndex);
|
|
|
|
this.applyTheme();
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleUseSystemTheme() {
|
|
|
|
this.useSystemTheme = !this.useSystemTheme;
|
|
|
|
setSettings('useSystemTheme', this.useSystemTheme);
|
|
|
|
this.applyTheme();
|
|
|
|
|
|
|
|
this.emit(cons.events.settings.SYSTEM_THEME_TOGGLED, this.useSystemTheme);
|
2021-07-28 16:15:52 +03:00
|
|
|
}
|
2021-08-08 19:26:34 +03:00
|
|
|
|
2022-01-03 15:16:43 +02:00
|
|
|
getUseSystemTheme() {
|
|
|
|
if (typeof this.useSystemTheme === 'boolean') return this.useSystemTheme;
|
|
|
|
|
|
|
|
const settings = getSettings();
|
2022-07-09 15:38:35 +03:00
|
|
|
if (settings === null) return true;
|
|
|
|
if (typeof settings.useSystemTheme === 'undefined') return true;
|
2022-01-03 15:16:43 +02:00
|
|
|
return settings.useSystemTheme;
|
|
|
|
}
|
|
|
|
|
2021-08-08 19:26:34 +03:00
|
|
|
getIsMarkdown() {
|
|
|
|
if (typeof this.isMarkdown === 'boolean') return this.isMarkdown;
|
|
|
|
|
|
|
|
const settings = getSettings();
|
2022-01-29 16:24:39 +02:00
|
|
|
if (settings === null) return true;
|
|
|
|
if (typeof settings.isMarkdown === 'undefined') return true;
|
2021-08-08 19:26:34 +03:00
|
|
|
return settings.isMarkdown;
|
|
|
|
}
|
|
|
|
|
2021-12-12 17:23:32 +02:00
|
|
|
getHideMembershipEvents() {
|
|
|
|
if (typeof this.hideMembershipEvents === 'boolean') return this.hideMembershipEvents;
|
|
|
|
|
|
|
|
const settings = getSettings();
|
|
|
|
if (settings === null) return false;
|
|
|
|
if (typeof settings.hideMembershipEvents === 'undefined') return false;
|
|
|
|
return settings.hideMembershipEvents;
|
|
|
|
}
|
|
|
|
|
|
|
|
getHideNickAvatarEvents() {
|
|
|
|
if (typeof this.hideNickAvatarEvents === 'boolean') return this.hideNickAvatarEvents;
|
|
|
|
|
|
|
|
const settings = getSettings();
|
2022-03-14 14:18:27 +02:00
|
|
|
if (settings === null) return true;
|
|
|
|
if (typeof settings.hideNickAvatarEvents === 'undefined') return true;
|
2021-12-12 17:23:32 +02:00
|
|
|
return settings.hideNickAvatarEvents;
|
|
|
|
}
|
|
|
|
|
2021-11-15 05:53:59 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-01-29 16:20:51 +02:00
|
|
|
get showNotifications() {
|
|
|
|
if (window.Notification?.permission !== 'granted') return false;
|
|
|
|
return this._showNotifications;
|
|
|
|
}
|
|
|
|
|
|
|
|
getShowNotifications() {
|
|
|
|
if (typeof this._showNotifications === 'boolean') return this._showNotifications;
|
|
|
|
|
|
|
|
const settings = getSettings();
|
|
|
|
if (settings === null) return true;
|
|
|
|
if (typeof settings.showNotifications === 'undefined') return true;
|
|
|
|
return settings.showNotifications;
|
|
|
|
}
|
|
|
|
|
2022-03-18 05:37:11 +02:00
|
|
|
getIsNotificationSounds() {
|
|
|
|
if (typeof this.isNotificationSounds === 'boolean') return this.isNotificationSounds;
|
|
|
|
|
|
|
|
const settings = getSettings();
|
|
|
|
if (settings === null) return true;
|
|
|
|
if (typeof settings.isNotificationSounds === 'undefined') return true;
|
|
|
|
return settings.isNotificationSounds;
|
|
|
|
}
|
|
|
|
|
2021-08-08 19:26:34 +03:00
|
|
|
setter(action) {
|
|
|
|
const actions = {
|
2022-01-03 15:16:43 +02:00
|
|
|
[cons.actions.settings.TOGGLE_SYSTEM_THEME]: () => {
|
2022-07-09 15:38:35 +03:00
|
|
|
this.toggleUseSystemTheme();
|
2022-01-03 15:16:43 +02:00
|
|
|
},
|
2021-08-08 19:26:34 +03:00
|
|
|
[cons.actions.settings.TOGGLE_MARKDOWN]: () => {
|
|
|
|
this.isMarkdown = !this.isMarkdown;
|
|
|
|
setSettings('isMarkdown', this.isMarkdown);
|
|
|
|
this.emit(cons.events.settings.MARKDOWN_TOGGLED, this.isMarkdown);
|
|
|
|
},
|
2021-11-15 05:53:59 +02:00
|
|
|
[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-12-12 17:23:32 +02:00
|
|
|
[cons.actions.settings.TOGGLE_MEMBERSHIP_EVENT]: () => {
|
|
|
|
this.hideMembershipEvents = !this.hideMembershipEvents;
|
|
|
|
setSettings('hideMembershipEvents', this.hideMembershipEvents);
|
|
|
|
this.emit(cons.events.settings.MEMBERSHIP_EVENTS_TOGGLED, this.hideMembershipEvents);
|
|
|
|
},
|
|
|
|
[cons.actions.settings.TOGGLE_NICKAVATAR_EVENT]: () => {
|
|
|
|
this.hideNickAvatarEvents = !this.hideNickAvatarEvents;
|
|
|
|
setSettings('hideNickAvatarEvents', this.hideNickAvatarEvents);
|
|
|
|
this.emit(cons.events.settings.NICKAVATAR_EVENTS_TOGGLED, this.hideNickAvatarEvents);
|
|
|
|
},
|
2022-01-29 16:20:51 +02:00
|
|
|
[cons.actions.settings.TOGGLE_NOTIFICATIONS]: async () => {
|
|
|
|
if (window.Notification?.permission !== 'granted') {
|
|
|
|
this._showNotifications = false;
|
|
|
|
} else {
|
|
|
|
this._showNotifications = !this._showNotifications;
|
|
|
|
}
|
|
|
|
setSettings('showNotifications', this._showNotifications);
|
|
|
|
this.emit(cons.events.settings.NOTIFICATIONS_TOGGLED, this._showNotifications);
|
|
|
|
},
|
2022-03-18 05:37:11 +02:00
|
|
|
[cons.actions.settings.TOGGLE_NOTIFICATION_SOUNDS]: () => {
|
|
|
|
this.isNotificationSounds = !this.isNotificationSounds;
|
|
|
|
setSettings('isNotificationSounds', this.isNotificationSounds);
|
|
|
|
this.emit(cons.events.settings.NOTIFICATION_SOUNDS_TOGGLED, this.isNotificationSounds);
|
|
|
|
},
|
2021-08-08 19:26:34 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
actions[action.type]?.();
|
|
|
|
}
|
2021-07-28 16:15:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const settings = new Settings();
|
2021-08-08 19:26:34 +03:00
|
|
|
appDispatcher.register(settings.setter.bind(settings));
|
2021-07-28 16:15:52 +03:00
|
|
|
|
|
|
|
export default settings;
|