Skip to content
This repository has been archived by the owner on Aug 1, 2020. It is now read-only.

Commit

Permalink
feat: persist navigation state only for the current session (#110)
Browse files Browse the repository at this point in the history
Fixes #78
  • Loading branch information
LeBenLeBen authored May 15, 2020
1 parent cfe6b96 commit f993983
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
4 changes: 2 additions & 2 deletions assets/js/components/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"]');

Expand Down Expand Up @@ -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();
}

Expand Down
13 changes: 9 additions & 4 deletions assets/js/storage.js
Original file line number Diff line number Diff line change
@@ -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));
},
};

0 comments on commit f993983

Please sign in to comment.