From c3c8c3959dc2eff83fbe7c3d70e8623331afa070 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tu=C4=9F=C3=A7e=20K=C3=BC=C3=A7=C3=BCko=C4=9Flu?= Date: Wed, 16 Feb 2022 10:21:31 +0300 Subject: [PATCH] fix theme switching --- src/App.vue | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/src/App.vue b/src/App.vue index a33ec045e..cf8b29cf2 100755 --- a/src/App.vue +++ b/src/App.vue @@ -49,7 +49,6 @@ export default { } this.newsActive = this.newsActive && sessionStorage.getItem('primevue-news-hidden') == null; - this.initTheme(); }, watch: { $route: { @@ -69,22 +68,6 @@ export default { } }, methods: { - initTheme() { - let appTheme; - const queryString = window.location.search; - if (queryString) - appTheme = new URLSearchParams(queryString.substring(1)).get('theme'); - else - appTheme = localStorage.getItem('primevue-theme'); - - if (appTheme) { - let darkTheme = this.isDarkTheme(appTheme); - this.changeTheme({ - theme: appTheme, - dark: darkTheme - }); - } - }, onMenuButtonClick() { if (this.sidebarActive) { this.sidebarActive = false; @@ -105,10 +88,12 @@ export default { event.stopPropagation(); }, changeTheme(event) { - let themeElement = document.getElementById('theme-link'); - themeElement.setAttribute('href', themeElement.getAttribute('href').replace(this.theme, event.theme)); + let themeLink = document.getElementById('theme-link'); + let href = 'themes/' + event.theme + '/theme.css'; this.theme = event.theme; + this.replaceLink(themeLink, href); + this.activeMenuIndex = null; EventBus.emit('change-theme', event); @@ -117,8 +102,20 @@ export default { if (event.theme.startsWith('md')) { this.$primevue.config.ripple = true; } + }, + replaceLink(linkElement, href) { + const id = linkElement.getAttribute('id'); + const cloneLinkElement = linkElement.cloneNode(true); - localStorage.setItem('primevue-theme', this.theme); + cloneLinkElement.setAttribute('href', href); + cloneLinkElement.setAttribute('id', id + '-clone'); + + linkElement.parentNode.insertBefore(cloneLinkElement, linkElement.nextSibling); + + cloneLinkElement.addEventListener('load', () => { + linkElement.remove(); + cloneLinkElement.setAttribute('id', id); + }); }, addClass(element, className) { if (!this.hasClass(element, className)) {