Refactor: Removed API fetch call (Kept method, for future updates), Added static theme list, Removed localStorage Check, removed unused lifecyclehooks and other variables.

This commit is contained in:
Paul D. Mendes 2020-05-24 13:19:45 +04:00
parent aa2d1961af
commit 6a3a482b5d

View File

@ -1,30 +1,26 @@
<template> <template>
<div> <div>
<!-- <img :src="theme.thumbnail" style="height: 30px;" /> -->
<b-nav-item-dropdown <b-nav-item-dropdown
id="my-nav-dropdown" id="my-nav-dropdown"
text="Theme" text="Theme"
toggle-class="nav-link-custom" toggle-class="nav-link-custom"
@show.once="fetchApi"
right right
lazy lazy
> >
<b-dropdown-item v-if="themes.length === 0"> <b-dropdown-item v-if="themes.length === 0">
<b-spinner small></b-spinner> <b-spinner small></b-spinner> Loading Themes...
Loading Themes...
</b-dropdown-item> </b-dropdown-item>
<!-- TODO Add v-b-tooltip.hover.right=="{ variant: 'info' }" for tooltip class rendered from bootswatch--> <!-- TODO Add v-b-tooltip.hover.right=="{ variant: 'className' }" for tooltip class rendered from bootswatch-->
<b-dropdown-item-button <b-dropdown-item-button
v-for="(theme, key) in themes" v-for="(theme, key) in themes"
:key="key" :key="key"
@click="handleClick" @click="handleClick"
:value="theme.cssCdn"
:active="activeTheme === theme.name" :active="activeTheme === theme.name"
v-b-tooltip.hover.right v-b-tooltip.hover.right
:title="theme.description" :title="theme.description"
>{{ theme.name }} >{{ theme.name }}</b-dropdown-item-button
</b-dropdown-item-button> >
</b-nav-item-dropdown> </b-nav-item-dropdown>
</div> </div>
</template> </template>
@ -36,30 +32,110 @@ export default {
data() { data() {
return { return {
activeTheme: '', activeTheme: '',
themes: [], themes: [
supportsLocalStorage: false, {
name: 'Cerulean',
description: 'A calm blue sky',
},
{
name: 'Cosmo',
description: 'An ode to Metro',
},
{
name: 'Cyborg',
description: 'Jet black and electric blue',
},
{
name: 'Darkly',
description: 'Flatly in night mode',
},
{
name: 'Flatly',
description: 'Flat and modern',
},
{
name: 'Journal',
description: 'Crisp like a new sheet of paper',
},
{
name: 'Litera',
description: 'The medium is the message',
},
{
name: 'Lumen',
description: 'Light and shadow',
},
{
name: 'Lux',
description: 'A touch of class',
},
{
name: 'Materia',
description: 'Material is the metaphor',
},
{
name: 'Minty',
description: 'A fresh feel',
},
{
name: 'Pulse',
description: 'A trace of purple',
},
{
name: 'Sandstone',
description: 'A touch of warmth',
},
{
name: 'Simplex',
description: 'Mini and minimalist',
},
{
name: 'Sketchy',
description: 'A hand-drawn look for mockups and mirth',
},
{
name: 'Slate',
description: 'Shades of gunmetal gray',
},
{
name: 'Solar',
description: 'A spin on Solarized',
},
{
name: 'Spacelab',
description: 'Silvery and sleek',
},
{
name: 'Superhero',
description: 'The brave and the blue',
},
{
name: 'United',
description: 'Ubuntu orange and unique font',
},
{
name: 'Yeti',
description: 'A friendly foundation',
},
],
}; };
}, },
name: 'BootswatchThemeSelect', name: 'BootswatchThemeSelect',
created() {
// this.fetchApi();
// We don't need to call the api here because it will be called once the show event is triggered from clicking on the dropdown.
},
mounted() { mounted() {
this.supportsLocalStorage = window.localStorage.length > 0; // If a theme has been stored in localstorage, the theme will be set.
this.getTheme(); if (window.localStorage.theme) this.setTheme(window.localStorage.theme);
}, },
updated() {},
methods: { methods: {
handleClick(e) { handleClick(e) {
this.setTheme(e.target.innerText.trim()); this.setTheme(e.target.innerText.trim());
}, },
setTheme(themeName) { setTheme(themeName) {
// Change the stylesheet to a different theme // If theme is already active, do nothing.
if (this.activeTheme === themeName) { if (this.activeTheme === themeName) {
return; return;
} }
// Dynamic import for a different theme, to avoid loading ALL themes.
import(`bootswatch/dist/${themeName.toLowerCase()}/bootstrap.min.css`).then(() => { import(`bootswatch/dist/${themeName.toLowerCase()}/bootstrap.min.css`).then(() => {
const styles = document.getElementsByTagName('style'); const styles = document.getElementsByTagName('style');
const bw = Array.from(styles).filter((w) => w.textContent.includes('bootswatch')); const bw = Array.from(styles).filter((w) => w.textContent.includes('bootswatch'));
@ -76,34 +152,26 @@ export default {
} }
}); });
}); });
// Save the theme as localstorage // Save the theme as localstorage
if (this.supportsLocalStorage) { console.log('Setting theme as', themeName);
console.log('Setting new theme as ', themeName);
window.localStorage.theme = themeName; window.localStorage.theme = themeName;
}
this.activeTheme = themeName; this.activeTheme = themeName;
}, },
getTheme() {
// console.log('Looking in local storage for theme.');
if (this.supportsLocalStorage) {
const themeName = window.localStorage.theme;
if (themeName) {
console.log(`${themeName} theme found in localstorage`);
this.setTheme(themeName);
}
}
},
fetchApi() { fetchApi() {
// Fetches boostswatch api and dynamically sets themes. // Fetches boostswatch api and dynamically sets themes.
// console.log('Calling bootswatch api'); // Not used, but useful for updating the static array of themes if bootswatch dependency is outdated.
axios axios
.get('https://bootswatch.com/api/4.json') .get('https://bootswatch.com/api/4.json')
.then((res) => { .then((res) => {
const { themes } = res.data; const { themes } = res.data;
this.themes = themes; this.themes = themes;
// Use this code in the browser console and copy and paste the filteredThemes into this.themes
// console.log(themes); // console.log(themes);
// const filteredThemes = [];
// themes.forEach((item) =>
// filteredThemes.push({ name: item.name, description: item.description }),
// );
}) })
.catch((error) => { .catch((error) => {
console.log(error); console.log(error);