🔀 Merge pull request #270 from Lissy93/FIX/252-collapse-state-ignored

[FIX] Don't remember collapse state, when specified in config
Fixes #252
This commit is contained in:
Alicia Sykes 2021-10-09 13:08:54 +01:00 committed by GitHub
commit 5d8748c4e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 30 additions and 19 deletions

View File

@ -1,14 +1,15 @@
<template>
<div :class="`collapsable ${checkSpanNum(cols, 'col')} ${checkSpanNum(rows, 'row')}`"
<div
:class="`collapsable ${checkSpanNum(cols, 'col')} ${checkSpanNum(rows, 'row')}`"
:style="`${color ? 'background: '+color : ''}; ${sanitizeCustomStyles(customStyles)};`"
>
<input
:id="`collapsible-${uniqueKey}`"
class="toggle"
type="checkbox"
:checked="getCollapseState()"
@change="collapseChanged"
tabIndex="-1"
:id="`collapsible-${uniqueKey}`"
class="toggle"
type="checkbox"
:checked="getCollapseState()"
@change="collapseChanged"
tabIndex="-1"
>
<label :for="`collapsible-${uniqueKey}`" class="lbl-toggle" tabindex="-1">
<Icon v-if="icon" :icon="icon" size="small" :url="title" class="section-icon" />
@ -30,14 +31,14 @@ import Icon from '@/components/LinkItems/ItemIcon.vue';
export default {
name: 'CollapsableContainer',
props: {
uniqueKey: String,
title: String,
icon: String,
collapsed: Boolean,
cols: Number,
rows: Number,
color: String,
customStyles: String,
uniqueKey: String, // Generated unique ID
title: String, // The section title
icon: String, // An optional section icon
collapsed: Boolean, // Optional override collapse state
cols: Number, // Set section horizontal col span / width
rows: Number, // Set section vertical row span / height
color: String, // Optional color override
customStyles: String, // Optional custom stylings
},
components: {
Icon,
@ -45,7 +46,7 @@ export default {
methods: {
/* Check that row & column span is valid, and not over the max */
checkSpanNum(span, classPrefix) {
const maxSpan = 4;
const maxSpan = 5;
let numSpan = /^\d*$/.test(span) ? parseInt(span, 10) : 1;
numSpan = (numSpan > maxSpan) ? maxSpan : numSpan;
return `${classPrefix}-${numSpan}`;
@ -68,7 +69,9 @@ export default {
}
return JSON.parse(localStorage[localStorageKeys.COLLAPSE_STATE]);
},
/* If not specified by user, get last state from local storage */
getCollapseState() {
if (this.collapsed !== undefined) return !this.collapsed;
const collapseStateObject = this.initialiseStorage();
let collapseState = !this.collapsed;
if (collapseStateObject[this.uniqueKey] !== undefined) {
@ -76,6 +79,7 @@ export default {
}
return collapseState;
},
/* When section collapsed, update local storage, to remember for next time */
setCollapseState(id, newState) {
// Get the current localstorage collapse state object
const collapseState = JSON.parse(localStorage[localStorageKeys.COLLAPSE_STATE]);
@ -84,9 +88,12 @@ export default {
// Stringify, and set the new object into local storage
localStorage.setItem(localStorageKeys.COLLAPSE_STATE, JSON.stringify(collapseState));
},
/* Called when collapse state changes, trigger local storage update if needed */
collapseChanged(whatChanged) {
this.initialiseStorage();
this.setCollapseState(this.uniqueKey.toString(), whatChanged.srcElement.checked);
if (this.collapseState === undefined) { // Only run, if user hasn't manually set prop
this.initialiseStorage();
this.setCollapseState(this.uniqueKey.toString(), whatChanged.srcElement.checked);
}
},
},
};
@ -110,22 +117,26 @@ export default {
&.row-2 { grid-row-start: span 2; }
&.row-3 { grid-row-start: span 3; }
&.row-4 { grid-row-start: span 4; }
&.row-5 { grid-row-start: span 5; }
grid-column-start: span 1;
@include tablet-up {
&.col-2 { grid-column-start: span 2; }
&.col-3 { grid-column-start: span 2; }
&.col-4 { grid-column-start: span 2; }
&.col-5 { grid-column-start: span 2; }
}
@include laptop-up {
&.col-2 { grid-column-start: span 2; }
&.col-3 { grid-column-start: span 3; }
&.col-4 { grid-column-start: span 3; }
&.col-5 { grid-column-start: span 3; }
}
@include monitor-up {
&.col-2 { grid-column-start: span 2; }
&.col-3 { grid-column-start: span 3; }
&.col-4 { grid-column-start: span 4; }
&.col-5 { grid-column-start: span 5; }
}
.wrap-collabsible {
@ -144,7 +155,7 @@ export default {
border-radius: var(--curve-factor);
transition: all 0.25s ease-out;
text-align: left;
color: var(--item-group-heading-text-color); //var(--item-group-background);
color: var(--item-group-heading-text-color);
h3 {
margin: 0;
padding: 0;