From 938c209869dc46ea751e20b9fcfda372a4404942 Mon Sep 17 00:00:00 2001 From: the-djmaze <> Date: Tue, 21 Feb 2023 12:59:56 +0100 Subject: [PATCH] Cleanup a bit of javascript --- dev/Settings/User/Themes.js | 31 +++++++++--------- dev/Stores/Theme.js | 63 ++++++++++++++++++++----------------- dev/prototype.js | 8 ++--- 3 files changed, 54 insertions(+), 48 deletions(-) diff --git a/dev/Settings/User/Themes.js b/dev/Settings/User/Themes.js index 37134950d..041680901 100644 --- a/dev/Settings/User/Themes.js +++ b/dev/Settings/User/Themes.js @@ -6,6 +6,7 @@ import { i18n } from 'Common/Translator'; import { SettingsCapa } from 'Common/Globals'; import { ThemeStore, convertThemeName, changeTheme } from 'Stores/Theme'; +import { addSubscribablesTo } from 'External/ko'; import Remote from 'Remote/User/Fetch'; @@ -24,20 +25,22 @@ export class UserSettingsThemes /*extends AbstractViewSettings*/ { this.fontSansSerif = ThemeStore.fontSansSerif; this.fontSerif = ThemeStore.fontSerif; this.fontMono = ThemeStore.fontMono; - ThemeStore.fontSansSerif.subscribe(value => { - Remote.saveSettings(null, { - fontSansSerif: value - }); - }); - ThemeStore.fontSerif.subscribe(value => { - Remote.saveSettings(null, { - fontSerif: value - }); - }); - ThemeStore.fontMono.subscribe(value => { - Remote.saveSettings(null, { - fontMono: value - }); + addSubscribablesTo(ThemeStore, { + fontSansSerif: value => { + Remote.saveSettings(null, { + fontSansSerif: value + }); + }, + fontSerif: value => { + Remote.saveSettings(null, { + fontSerif: value + }); + }, + fontMono: value => { + Remote.saveSettings(null, { + fontMono: value + }); + } }); this.theme = ThemeStore.theme; diff --git a/dev/Stores/Theme.js b/dev/Stores/Theme.js index 6914347a0..b5a05976f 100644 --- a/dev/Stores/Theme.js +++ b/dev/Stores/Theme.js @@ -3,6 +3,7 @@ import { $htmlCL, appEl, elementById, leftPanelDisabled, Settings, SettingsGet } import { isArray, arrayLength } from 'Common/Utils'; import { cssLink, serverRequestRaw } from 'Common/Links'; import { SaveSettingStatus } from 'Common/Enums'; +import { addSubscribablesTo } from 'External/ko'; let __themeTimer = 0; @@ -60,35 +61,39 @@ export const convertThemeName = theme => theme.replace(/@[a-z]+$/, '').replace(/([A-Z])/g, ' $1').trim(); -ThemeStore.isMobile.subscribe(value => $htmlCL.toggle('rl-mobile', value)); +addSubscribablesTo(ThemeStore, { + isMobile: value => $htmlCL.toggle('rl-mobile', value), -ThemeStore.fontSansSerif.subscribe(value => { - if (null != value) { - let cl = appEl.classList; - cl.forEach(name => { - if (name.startsWith('font') && !/font(Serif|Mono)/.test(name)) { - cl.remove(name); - } - }); - value && cl.add('font'+value); - } -}); -ThemeStore.fontSerif.subscribe(value => { - if (null != value) { - let cl = appEl.classList; - cl.forEach(name => name.startsWith('fontSerif') && cl.remove(name)); - value && cl.add('fontSerif'+value); - } -}); -ThemeStore.fontMono.subscribe(value => { - if (null != value) { - let cl = appEl.classList; - cl.forEach(name => name.startsWith('fontMono') && cl.remove(name)); - value && cl.add('fontMono'+value); - } -}); + fontSansSerif: value => { + if (null != value) { + let cl = appEl.classList; + cl.forEach(name => { + if (name.startsWith('font') && !/font(Serif|Mono)/.test(name)) { + cl.remove(name); + } + }); + value && cl.add('font'+value); + } + }, -ThemeStore.userBackgroundHash.subscribe(value => { - appEl.classList.toggle('UserBackground', !!value); - appEl.style.backgroundImage = value ? "url("+serverRequestRaw('UserBackground', value)+")" : null; + fontSerif: value => { + if (null != value) { + let cl = appEl.classList; + cl.forEach(name => name.startsWith('fontSerif') && cl.remove(name)); + value && cl.add('fontSerif'+value); + } + }, + + fontMono: value => { + if (null != value) { + let cl = appEl.classList; + cl.forEach(name => name.startsWith('fontMono') && cl.remove(name)); + value && cl.add('fontMono'+value); + } + }, + + userBackgroundHash: value => { + appEl.classList.toggle('UserBackground', !!value); + appEl.style.backgroundImage = value ? "url("+serverRequestRaw('UserBackground', value)+")" : null; + } }); diff --git a/dev/prototype.js b/dev/prototype.js index 78f5c57a1..cec93864d 100644 --- a/dev/prototype.js +++ b/dev/prototype.js @@ -53,7 +53,7 @@ return function(...args) { timer && clearTimeout(timer); timer = setTimeout(()=>{ - func.apply(this, args) + func.apply(this, args); timer = 0; }, ms); }; @@ -68,12 +68,10 @@ Function.prototype.throttle = function(ms) { let func = this, timer; return function(...args) { - if (!timer) { - timer = setTimeout(()=>{ - func.apply(this, args) + timer = timer || setTimeout(()=>{ + func.apply(this, args); timer = 0; }, ms); - } }; }; }