Skip to content

armgit / dashy

Public Imported from GitHub
0

πŸš€ A self-hostable personal dashboard built for you. Includes status-checking, widgets, themes, icon packs, a UI editor and tons more!

πŸ› Loads sub-page external stylesheets on page change

Alicia Sykesde8a09789626
diff --git a/src/App.vue b/src/App.vue
index 79ef9bda..eff1b9b9 100644
--- a/src/App.vue
+++ b/src/App.vue
@@ -21,7 +21,7 @@ import { welcomeMsg } from '@/utils/logging/CoolConsole';
import ErrorHandler from '@/utils/logging/ErrorHandler';
import { syncPageMeta } from '@/utils/PageMeta';
import { viewFromPath } from '@/utils/config/ConfigHelpers';
-import { applyTheme } from '@/utils/Theming';
+import { applyTheme, loadExternalStyleSheets } from '@/utils/Theming';
import Keys from '@/utils/StoreMutations';
import { loadLocale } from '@/utils/languages';
import i18n from '@/utils/i18n';
@@ -61,6 +61,10 @@ export default {
immediate: true,
handler(t) { applyTheme(t, this.appConfig); },
},
+ /* Sub-pages may set their own external stylesheets */
+ '$store.state.currentConfigInfo.confId': {
+ handler() { loadExternalStyleSheets(this.appConfig); },
+ },
/* Keep i18n in sync with the active page's language (incl. sub-page overrides) */
'appConfig.language': {
immediate: true,
diff --git a/src/utils/Theming.js b/src/utils/Theming.js
index 9ffd52c2..7c7c51a9 100644
--- a/src/utils/Theming.js
+++ b/src/utils/Theming.js
@@ -23,15 +23,16 @@ export const getExternalThemes = (appConfig) => {
/* Inject every appConfig.externalStyleSheet, resolving once all have loaded/failed, or timed out */
export const loadExternalStyleSheets = (appConfig) => {
- const ext = appConfig?.externalStyleSheet;
- if (!ext) return Promise.resolve();
- document.querySelectorAll('[data-external-source]').forEach((el) => el.remove());
- const urls = Array.isArray(ext) ? ext : [ext];
+ const urls = [].concat(appConfig?.externalStyleSheet || []).filter((url) => {
+ const valid = typeof url === 'string' && (url.startsWith('http') || url.startsWith('/'));
+ if (!valid) ErrorHandler(`Invalid external stylesheet URL: ${url}`);
+ return valid;
+ });
+ const current = [...document.querySelectorAll('[data-external-source]')];
+ const currentUrls = current.map((el) => el.getAttribute('href'));
+ if (JSON.stringify(currentUrls) === JSON.stringify(urls)) return Promise.resolve();
+ current.forEach((el) => el.remove());
const loads = urls.map((url) => {
- if (typeof url !== 'string' || (!url.startsWith('http') && !url.startsWith('/'))) {
- ErrorHandler(`Invalid external stylesheet URL: ${url}`);
- return null;
- }
const link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';