From e35c2d161b9e213d5504c367b2dc2e3b0cec5b0e Mon Sep 17 00:00:00 2001 From: Cyrille Rossant Date: Tue, 12 Mar 2024 17:52:49 +0100 Subject: [PATCH] Store currently-selected listing in local storage --- scripts/selector.js | 5 ++++- scripts/storage.js | 12 ++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/scripts/selector.js b/scripts/selector.js index 5afdf35..560b239 100644 --- a/scripts/selector.js +++ b/scripts/selector.js @@ -24,8 +24,9 @@ class Selector { let names = this.model.storage.list(); let first = names ? names[0] : null; + let stored = this.model.storage.loadMetadata("__name__"); - this.state.name = this.state.name || first; + this.state.name = this.state.name || stored || first; // Set the selector list with those names. this.setNames(names, this.state.name); @@ -137,6 +138,8 @@ class Selector { // Update the state. this.state.name = name; + this.model.storage.storeMetadata("__name__", name); + // Emit the select event. this.dispatcher.select(this, name); } diff --git a/scripts/storage.js b/scripts/storage.js index f772af5..1a788fa 100644 --- a/scripts/storage.js +++ b/scripts/storage.js @@ -70,6 +70,18 @@ class Storage { /* Public functions */ /*********************************************************************************************/ + storeMetadata(key, value) { + if (key.startsWith("__")) { + localStorage.setItem(key, value); + } + } + + loadMetadata(key) { + if (key.startsWith("__")) { + return localStorage.getItem(key); + } + } + count() { return this.list().length; }