frequi_origin/src/components/BootswatchThemeSelect.vue

131 lines
4.3 KiB
Vue
Raw Normal View History

2020-05-22 21:02:17 +00:00
<template>
<div>
<b-nav-item-dropdown
v-if="!simple"
2020-05-22 21:02:17 +00:00
id="my-nav-dropdown"
text="Theme"
toggle-class="nav-link-custom"
right
lazy
>
2023-03-24 05:49:50 +00:00
<b-dropdown-item v-if="themeList1.length === 0">
<b-spinner small></b-spinner> Loading Themes...
</b-dropdown-item>
<!-- TODO Add v-b-tooltip.hover.right=="{ variant: 'className' }" for tooltip class rendered from bootswatch-->
2020-05-22 21:02:17 +00:00
<b-dropdown-item-button
2023-03-24 05:49:50 +00:00
v-for="(theme, key) in themeList1"
2020-05-22 21:02:17 +00:00
:key="key"
2020-08-31 15:43:44 +00:00
:active="activeTheme === theme.name"
:title="theme.description"
2023-03-24 19:13:17 +00:00
@click="handleClick(theme.name)"
>{{ theme.name }}{{ theme.dark ? ' [dark]' : '' }}</b-dropdown-item-button
>
2020-05-22 21:02:17 +00:00
</b-nav-item-dropdown>
<b-link v-else variant="outline-primary" class="nav-link" @click="toggleNight">
<ThemeLightDark :size="16" />
</b-link>
2020-05-22 21:02:17 +00:00
</div>
</template>
2023-03-23 17:23:14 +00:00
<script setup lang="ts">
import { ref, onMounted } from 'vue';
2020-05-22 21:02:17 +00:00
import axios from 'axios';
import ThemeLightDark from 'vue-material-design-icons/Brightness6.vue';
2021-03-08 06:46:40 +00:00
import { FTHTMLStyleElement } from '@/types/styleElement';
import { useSettingsStore } from '@/stores/settings';
2023-03-24 05:49:50 +00:00
import { themeList, ThemeType } from '@/shared/themes';
2020-05-22 21:02:17 +00:00
2023-03-23 17:23:14 +00:00
const props = defineProps({
simple: {
type: Boolean,
default: true,
},
2023-03-23 17:23:14 +00:00
});
const activeTheme = ref('');
2023-03-24 05:49:50 +00:00
const themeList1 = ref<ThemeType[]>(themeList);
2023-03-23 17:23:14 +00:00
const settingsStore = useSettingsStore();
2022-04-17 08:07:48 +00:00
2023-03-23 17:23:14 +00:00
const setTheme = (themeName) => {
// If theme is already active, do nothing.
if (activeTheme.value === themeName) {
return;
}
if (themeName.toLowerCase() === 'bootstrap' || themeName.toLowerCase() === 'bootstrap_dark') {
const styles = document.getElementsByTagName('style');
const bw = Array.from(styles).filter((w) => w.textContent?.includes('bootswatch'));
document.documentElement.setAttribute(
'data-theme',
themeName.toLowerCase() === 'bootstrap' ? 'light' : 'dark',
);
// Reset all bootswatch styles
bw.forEach((style, index) => {
(bw[index] as FTHTMLStyleElement).disabled = true;
});
if (props.simple && activeTheme.value) {
// Only transition if simple mode is active
document.documentElement.classList.add('ft-theme-transition');
window.setTimeout(() => {
document.documentElement.classList.remove('ft-theme-transition');
}, 1000);
}
} else {
// Dynamic import for a different theme, to avoid loading ALL themes.
import(`bootswatch/dist/${themeName.toLowerCase()}/bootstrap.min.css`).then((mod) => {
console.log('theme', mod);
document.documentElement.removeAttribute('data-theme');
const styles = document.getElementsByTagName('style');
const bw = Array.from(styles).filter((w) => w.textContent?.includes('bootswatch'));
bw.forEach((style, index) => {
if (!style.id) {
// If its a style that was just imported and hasn't been assigned an id.
bw[index].id = themeName;
} else if (style.id === themeName) {
// If it's a style that has been imported already.
(bw[index] as FTHTMLStyleElement).disabled = false;
} else {
// All other style themes should be disabled.
2021-03-08 06:46:40 +00:00
(bw[index] as FTHTMLStyleElement).disabled = true;
}
2023-03-23 17:23:14 +00:00
});
2022-04-17 08:07:48 +00:00
});
2023-03-23 17:23:14 +00:00
}
// Save the theme as localstorage
settingsStore.currentTheme = themeName;
activeTheme.value = themeName;
};
2022-04-17 08:07:48 +00:00
2023-03-23 17:23:14 +00:00
onMounted(() => {
if (settingsStore.currentTheme) setTheme(settingsStore.currentTheme);
});
2023-03-23 17:23:14 +00:00
2023-03-24 19:13:17 +00:00
const handleClick = (name: string) => {
setTheme(name.trim());
2023-03-23 17:23:14 +00:00
};
const toggleNight = () => {
setTheme(activeTheme.value === 'bootstrap' ? 'bootstrap_dark' : 'bootstrap');
};
const fetchApi = () => {
// Fetches boostswatch api and dynamically sets themes.
// Not used, but useful for updating the static array of themes if bootswatch dependency is outdated.
axios
.get('https://bootswatch.com/api/4.json')
// .then((res) => {
// const { themes } = res.data;
// this.themes = themes;
// Use this code in the browser console and copy and paste the filteredThemes into this.themes
// console.log(themes);
// const filteredThemes = [];
// themes.forEach((item) =>
// filteredThemes.push({ name: item.name, description: item.description }),
// );
// })
.catch((error) => {
console.error(error);
});
};
2020-05-22 21:02:17 +00:00
</script>
<style scoped></style>