Implemented pre-loading external style functionality

This commit is contained in:
Alicia Sykes 2021-04-08 19:53:04 +01:00
parent 99dc9299ed
commit 0cb6cc7d62
4 changed files with 177 additions and 18 deletions

View File

@ -15,6 +15,10 @@
@click="clearFilterInput"></i> @click="clearFilterInput"></i>
</form> </form>
<div class="options-container"> <div class="options-container">
<div class="theme-selector">
<ThemeSelector :themes="availableThemes" />
</div>
<div>
<span class="options-label">Layout</span> <span class="options-label">Layout</span>
<div class="display-options"> <div class="display-options">
<IconDeafault @click="updateDisplayLayout('default')" <IconDeafault @click="updateDisplayLayout('default')"
@ -25,12 +29,14 @@
:class="`layout-icon ${displayLayout === 'vertical' ? 'selected' : ''}`" /> :class="`layout-icon ${displayLayout === 'vertical' ? 'selected' : ''}`" />
</div> </div>
</div> </div>
</div>
<KeyboardShortcutInfo /> <KeyboardShortcutInfo />
</section> </section>
</template> </template>
<script> <script>
import KeyboardShortcutInfo from '@/components/KeyboardShortcutInfo'; import KeyboardShortcutInfo from '@/components/KeyboardShortcutInfo';
import ThemeSelector from '@/components/ThemeSelector';
import IconDeafault from '@/assets/icons/layout-default.svg'; import IconDeafault from '@/assets/icons/layout-default.svg';
import IconHorizontal from '@/assets/icons/layout-horizontal.svg'; import IconHorizontal from '@/assets/icons/layout-horizontal.svg';
@ -45,9 +51,11 @@ export default {
}, },
props: { props: {
displayLayout: String, displayLayout: String,
availableThemes: Object,
}, },
components: { components: {
KeyboardShortcutInfo, KeyboardShortcutInfo,
ThemeSelector,
IconDeafault, IconDeafault,
IconHorizontal, IconHorizontal,
IconVertical, IconVertical,
@ -92,7 +100,7 @@ export default {
display: flex; display: flex;
align-items: center; align-items: center;
align-items: stretch; align-items: stretch;
background: linear-gradient(0deg, $background 0%, $header-color 100%); background: linear-gradient(0deg, var(--background) 0%, $header-color 100%);
} }
form { form {
border-radius: 0 0 20px 0; border-radius: 0 0 20px 0;
@ -100,7 +108,7 @@ export default {
background: $header-color; background: $header-color;
label { label {
display: inline; display: inline;
color: $ascent; color: var(--primary);
margin: 0.5rem; margin: 0.5rem;
display: inline; display: inline;
} }
@ -112,8 +120,8 @@ export default {
outline: none; outline: none;
border: none; border: none;
border-radius: 12px; border-radius: 12px;
background: $background; background: var(--background);
color: $ascent; color: var(--primary);
&:focus { &:focus {
background: $bg-with-opacity; background: $bg-with-opacity;
} }
@ -121,7 +129,7 @@ export default {
.clear-search { .clear-search {
position: absolute; position: absolute;
margin: 1em 0 0 -2em; margin: 1em 0 0 -2em;
color: $ascent; color: var(--primary);
opacity: 0.5; opacity: 0.5;
border-radius: 50px; border-radius: 50px;
cursor: pointer; cursor: pointer;
@ -133,13 +141,13 @@ export default {
} }
.options-container { .options-container {
display: flex; display: flex;
flex-direction: column; flex-direction: row;
align-items: flex-end; align-items: flex-end;
justify-content: flex-end; justify-content: flex-end;
flex: 1; flex: 1;
padding: 0 1rem; padding: 0 1rem;
border-radius: 20px 0 0; border-radius: 20px 0 0;
background: $background; background: var(--background);
span.options-label { span.options-label {
font-size: 0.8rem; font-size: 0.8rem;
@ -148,6 +156,13 @@ export default {
text-align: left; text-align: left;
} }
.theme-selector {
align-items: center;
display: flex;
height: 100%;
padding: 0 1rem;
}
.display-options { .display-options {
color: $ascent-with-opacity; color: $ascent-with-opacity;
svg { svg {
@ -159,14 +174,14 @@ export default {
margin: 0.2rem; margin: 0.2rem;
padding: 0.2rem; padding: 0.2rem;
text-align: center; text-align: center;
background: $background; background: var(--background);
border: 1px solid currentColor; border: 1px solid currentColor;
border-radius: 4px; border-radius: 4px;
opacity: 0.8; opacity: 0.8;
cursor: pointer; cursor: pointer;
&:hover, &.selected { &:hover, &.selected {
background: $ascent-with-opacity; background: $ascent-with-opacity;
path { fill: $background; } path { fill: var(--background); }
} }
} }
} }

View File

@ -0,0 +1,86 @@
<template>
<div class="theme-selector-section" v-if="themes" >
<span class="theme-label">Themes</span>
<v-select :options="themeNames" v-model="selectedTheme" label="Theme" class="theme-dropdown" />
</div>
</template>
<script>
import ThemeHelper from '@/utils/ThemeHelper';
export default {
name: 'ThemeSelector',
props: {
themes: Object,
},
watch: {
selectedTheme(newTheme) {
this.themeHelper.theme = newTheme;
},
},
data() {
return {
selectedTheme: 'Default',
themeHelper: new ThemeHelper(),
loading: true,
};
},
computed: {
themeNames: function themeNames() { return Object.keys(this.themes); },
},
created() {
const added = Object.keys(this.themes).map(
name => this.themeHelper.add(name, this.themes[name]),
);
Promise.all(added).then(sheets => {
console.log(`${sheets.length} themes loaded`);
this.loading = false;
this.themeHelper.theme = 'Deafault';
});
},
};
</script>
<style lang="scss">
@import '../../src/styles/color-pallet.scss';
@import 'vue-select/src/scss/vue-select.scss';
.theme-dropdown {
div.vs__dropdown-toggle {
border-color: var(--primary);
min-width: 10rem;
height: 2rem;
}
span.vs__selected, li.vs__dropdown-option {
color: var(--primary);
}
button.vs__clear, svg.vs__open-indicator {
fill: var(--primary);
}
ul.vs__dropdown-menu {
width: auto;
background: var(--background);
}
li.vs__dropdown-option--highlight {
background: var(--primary);
color: var(--background);
}
}
.theme-selector-section {
display: flex;
flex-direction: column;
opacity: 0.8;
span.theme-label {
font-size: 0.8rem;
color: var(--primary);
margin: 1px 0 2px 0;
}
&:hover {
opacity: 1;
}
}
</style>

38
src/utils/ThemeHelper.js Normal file
View File

@ -0,0 +1,38 @@
/**
* A function for pre-loading, and easy switching of external stylesheets
*/
const ThemeHelper = function th() {
/* Preload content, to avoid FOUC */
const preloadTheme = (href) => {
const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = href;
document.head.appendChild(link);
return new Promise((resolve, reject) => {
link.onload = e => {
const { sheet } = e.target;
sheet.disabled = true;
resolve(sheet);
};
link.onerror = reject;
});
};
const selectTheme = (themes, name) => {
const themeResults = themes;
if (name && !themes[name]) {
throw new Error(`'${name}' has not been defined as a theme.`);
}
Object.keys(themeResults).forEach(n => { themeResults[n].disabled = (n !== name); });
};
const themes = {};
return {
add(name, href) { return preloadTheme(href).then(s => { themes[name] = s; }); },
set theme(name) { selectTheme(themes, name); },
get theme() { return Object.keys(themes).find(n => !themes[n].disabled); },
};
};
export default ThemeHelper;

View File

@ -5,6 +5,7 @@
@user-is-searchin="searching" @user-is-searchin="searching"
@change-display-layout="setLayoutOrientation" @change-display-layout="setLayoutOrientation"
:displayLayout="layout" :displayLayout="layout"
:availableThemes="getAvailibleThemes()"
class="filter-container" class="filter-container"
/> />
<!-- Main content, section for each group of items --> <!-- Main content, section for each group of items -->
@ -31,7 +32,8 @@ import ItemGroup from '@/components/ItemGroup.vue';
export default { export default {
name: 'home', name: 'home',
props: { props: {
sections: Array, // Main site configuration sections: Array, // Main site content
appConfig: Object, // Main site configuation (optional)
}, },
components: { components: {
FilterTile, FilterTile,
@ -94,6 +96,24 @@ export default {
getLayoutOrientation() { getLayoutOrientation() {
return localStorage.layoutOrientation || 'default'; return localStorage.layoutOrientation || 'default';
}, },
getAvailibleThemes() {
const availibleThemes = {};
if (this.appConfig) {
if (this.appConfig.externalStyleSheet) {
const externals = this.appConfig.externalStyleSheet;
console.log(externals);
if (Array.isArray(externals)) {
externals.forEach((ext, i) => {
availibleThemes[`External Stylesheet ${i + 1}`] = ext;
});
} else {
availibleThemes['External Stylesheet'] = this.appConfig.externalStyleSheet;
}
}
}
availibleThemes.Deafault = '#';
return availibleThemes;
},
/* Checks if any of the icons are Font Awesome glyphs */ /* Checks if any of the icons are Font Awesome glyphs */
checkIfFontAwesomeNeeded() { checkIfFontAwesomeNeeded() {
let isFound = false; let isFound = false;
@ -125,7 +145,7 @@ export default {
@import '../../src/styles/media-queries.scss'; @import '../../src/styles/media-queries.scss';
.home { .home {
background: $background; background: var(--background);
padding-bottom: 1px; padding-bottom: 1px;
min-height: 90%; min-height: 90%;
} }