Skip to content

Commit

Permalink
⚡ Refactors class names in collapsible
Browse files Browse the repository at this point in the history
  • Loading branch information
Lissy93 committed Nov 5, 2021
1 parent 5f9d165 commit 453a3c1
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions src/components/LinkItems/Collapsable.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<template>
<div
:class="`collapsable ${checkSpanNum(cols, 'col')} ${checkSpanNum(rows, 'row')}`"
:class="`collapsable ${rowColSpanClass} ${collapseClass}`"
:style="`${color ? 'background: '+color : ''}; ${sanitizeCustomStyles(customStyles)};`"
>
<input
:id="sectionKey"
class="toggle"
type="checkbox"
:checked="getCollapseState()"
:checked="isExpanded"
@change="collapseChanged"
tabIndex="-1"
>
Expand Down Expand Up @@ -56,6 +56,19 @@ export default {
if (this.isEditMode) return undefined;
return `collapsible-${this.uniqueKey}`;
},
collapseClass() {
return !this.isExpanded ? ' is-collapsed' : 'is-open';
},
rowColSpanClass() {
const { rows, cols, checkSpanNum } = this;
return `${checkSpanNum(cols, 'col')} ${checkSpanNum(rows, 'row')}`;
},
},
data: () => ({
isExpanded: false,
}),
mounted() {
this.isExpanded = this.getCollapseState();
},
methods: {
/* Check that row & column span is valid, and not over the max */
Expand All @@ -69,29 +82,28 @@ export default {
sanitizeCustomStyles(userCss) {
return userCss ? userCss.replace(/[^a-zA-Z0-9- :;.]/g, '') : '';
},
/* If not already done, then add object structure to local storage */
/* Returns local storage collapse state data, and if not yet set then initialized is */
initialiseStorage() {
const storageKey = localStorageKeys.COLLAPSE_STATE;
/* Initialize function will create and set a blank object to storage */
const initStorage = () => localStorage.setItem(
localStorageKeys.COLLAPSE_STATE, JSON.stringify({}),
);
if (!localStorage[localStorageKeys.COLLAPSE_STATE]) initStorage(); // Initialise if not set
try { // Check storage is valid JSON, and has not been corrupted
JSON.parse(localStorage[localStorageKeys.COLLAPSE_STATE]);
} catch {
const initStorage = () => localStorage.setItem(storageKey, JSON.stringify({}));
// If not yet set, then call initialize
if (!localStorage[storageKey]) {
initStorage();
return {};
}
return JSON.parse(localStorage[localStorageKeys.COLLAPSE_STATE]);
// Otherwise, return value of local storage
return JSON.parse(localStorage[storageKey]);
},
/* If not specified by user, get last state from local storage */
/* If specified by user, return conf collapse state, otherwise check local storage */
getCollapseState() {
if (this.collapsed !== undefined) return !this.collapsed;
const collapseStateObject = this.initialiseStorage();
let collapseState = !this.collapsed;
if (this.collapsed !== undefined) return !this.collapsed; // Check users config
const collapseStateObject = this.initialiseStorage(); // Check local storage
if (collapseStateObject[this.uniqueKey] !== undefined) {
collapseState = collapseStateObject[this.uniqueKey];
return collapseStateObject[this.uniqueKey];
}
return collapseState;
// Nothing specified, return Open
return true;
},
/* When section collapsed, update local storage, to remember for next time */
setCollapseState(id, newState) {
Expand All @@ -104,9 +116,10 @@ export default {
},
/* Called when collapse state changes, trigger local storage update if needed */
collapseChanged(whatChanged) {
this.isExpanded = 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);
this.setCollapseState(this.uniqueKey.toString(), this.isExpanded);
}
},
openEditModal() {
Expand Down

0 comments on commit 453a3c1

Please sign in to comment.