Improved collapsible logic

This commit is contained in:
Alicia Sykes 2022-05-19 01:31:02 +01:00
parent 0d66e54b59
commit 0ed084c147

View File

@ -10,8 +10,7 @@
:id="sectionKey" :id="sectionKey"
class="toggle" class="toggle"
type="checkbox" type="checkbox"
:checked="isExpanded" v-model="checkboxState"
@change="collapseChanged"
tabIndex="-1" tabIndex="-1"
> >
<label :for="sectionKey" class="lbl-toggle" tabindex="-1" <label :for="sectionKey" class="lbl-toggle" tabindex="-1"
@ -75,34 +74,42 @@ export default {
const { rows, cols, checkSpanNum } = this; const { rows, cols, checkSpanNum } = this;
return `${checkSpanNum(cols, 'col')} ${checkSpanNum(rows, 'row')}`; return `${checkSpanNum(cols, 'col')} ${checkSpanNum(rows, 'row')}`;
}, },
/* Used to fetch initial collapse state, and set new collapse state on change */
isExpanded: { isExpanded: {
// getter
get() { get() {
if (this.collapsed !== undefined) return !this.collapsed; if (this.collapsed !== undefined) return !this.collapsed;
const collapseStateObject = this.initialiseStorage(); // Check local storage const collapseStateObject = this.locallyStoredCollapseStates();
if (collapseStateObject[this.uniqueKey] !== undefined) { if (collapseStateObject[this.uniqueKey] !== undefined) {
return collapseStateObject[this.uniqueKey]; return collapseStateObject[this.uniqueKey];
} }
return true; // Nothing specified, return Open return true;
}, },
// setter
set(newState) { set(newState) {
// Get the current localstorage collapse state object const collapseState = this.locallyStoredCollapseStates();
const collapseState = JSON.parse(localStorage[localStorageKeys.COLLAPSE_STATE]);
// Add the new state to it
collapseState[this.uniqueKey] = newState; collapseState[this.uniqueKey] = newState;
// Stringify, and set the new object into local storage
localStorage.setItem(localStorageKeys.COLLAPSE_STATE, JSON.stringify(collapseState)); localStorage.setItem(localStorageKeys.COLLAPSE_STATE, JSON.stringify(collapseState));
}, },
}, },
}, },
data: () => ({ data: () => ({
// isExpanded: false, checkboxState: true,
}), }),
mounted() { mounted() {
// this.isExpanded = this.getCollapseState(); this.checkboxState = this.isExpanded;
},
watch: {
checkboxState(newState) {
this.isExpanded = newState;
},
uniqueKey() {
this.checkboxState = this.isExpanded;
},
}, },
methods: { methods: {
/* Either expand or collapse section, based on it's current state */
toggle() {
this.checkboxState = !this.checkboxState;
},
/* Check that row & column span is valid, and not over the max */ /* Check that row & column span is valid, and not over the max */
checkSpanNum(span, classPrefix) { checkSpanNum(span, classPrefix) {
const maxSpan = 6; const maxSpan = 6;
@ -115,7 +122,7 @@ export default {
return userCss ? userCss.replace(/[^a-zA-Z0-9- :;.]/g, '') : ''; return userCss ? userCss.replace(/[^a-zA-Z0-9- :;.]/g, '') : '';
}, },
/* Returns local storage collapse state data, and if not yet set then initialized is */ /* Returns local storage collapse state data, and if not yet set then initialized is */
initialiseStorage() { locallyStoredCollapseStates() {
// If not yet set, then call initialize // If not yet set, then call initialize
if (!localStorage[localStorageKeys.COLLAPSE_STATE]) { if (!localStorage[localStorageKeys.COLLAPSE_STATE]) {
localStorage.setItem(localStorageKeys.COLLAPSE_STATE, JSON.stringify({})); localStorage.setItem(localStorageKeys.COLLAPSE_STATE, JSON.stringify({}));
@ -124,10 +131,6 @@ export default {
// Otherwise, return value of local storage // Otherwise, return value of local storage
return JSON.parse(localStorage[localStorageKeys.COLLAPSE_STATE]); return JSON.parse(localStorage[localStorageKeys.COLLAPSE_STATE]);
}, },
// /* Called when collapse state changes, trigger local storage update if needed */
collapseChanged(whatChanged) {
this.isExpanded = whatChanged.srcElement.checked;
},
openEditModal() { openEditModal() {
this.$emit('openEditSection'); this.$emit('openEditSection');
}, },
@ -214,6 +217,8 @@ export default {
margin-right: .7rem; margin-right: .7rem;
transform: translateY(-2px); transform: translateY(-2px);
transition: transform .2s ease-out; transition: transform .2s ease-out;
opacity: 0.3;
transition: all 0.4s ease-in-out;
} }
} }
@ -251,6 +256,16 @@ export default {
top: 0.5rem; top: 0.5rem;
margin-left: 0.2rem; margin-left: 0.2rem;
margin-right: 0.2rem; margin-right: 0.2rem;
opacity: 0.3;
transition: all 0.4s ease-in-out;
}
/* On section hover, set interface icons to full visible */
&:hover {
.edit-mode-item, label.lbl-toggle::before {
opacity: 1;
transition: all 0.2s ease-out;
}
} }
/* Makes sections fill available space */ /* Makes sections fill available space */