diff --git a/assets/js/components/tree.js b/assets/js/components/tree.js index 1b2b0e7..fa50c91 100644 --- a/assets/js/components/tree.js +++ b/assets/js/components/tree.js @@ -16,7 +16,7 @@ class Tree { constructor(el) { this._el = $(el); this._id = this._el[0].id; - this._state = storage.get(`tree.${this._id}.state`, []); + this._state = storage.get(`tree.${this._id}.state`, [], 'session'); this._collections = $.map(this._el.find('[data-behaviour="collection"]'), (c) => new TreeCollection(c, this)); this._collapseButton = this._el.find('[data-behaviour="collapse-tree"]'); @@ -61,7 +61,7 @@ class Tree { saveState() { this._state = this._collections.filter((c) => c.isOpen).map((c) => c.id); - storage.set(`tree.${this._id}.state`, this._state); + storage.set(`tree.${this._id}.state`, this._state, 'session'); this._updateCollapseButtonVisibility(); } diff --git a/assets/js/storage.js b/assets/js/storage.js index ddbadde..f75af19 100644 --- a/assets/js/storage.js +++ b/assets/js/storage.js @@ -1,12 +1,17 @@ 'use strict'; +const storages = { + local: localStorage, + session: sessionStorage, +}; + module.exports = { - get(name, fallback) { - const result = localStorage.getItem(name); + get(name, fallback, storage = 'local') { + const result = storages[storage].getItem(name); return result ? JSON.parse(result) : fallback; }, - set(name, value) { - localStorage.setItem(name, JSON.stringify(value)); + set(name, value, storage = 'local') { + storages[storage].setItem(name, JSON.stringify(value)); }, };