diff options
| author | Owen Jacobson <owen@grimoire.ca> | 2024-10-05 20:12:25 -0400 |
|---|---|---|
| committer | Owen Jacobson <owen@grimoire.ca> | 2024-10-05 20:23:54 -0400 |
| commit | 8edd5625ad5dde0ef1637d5c89e9901b3ee65d73 (patch) | |
| tree | 2dfa09776511bdae04e116a478a605957d2db2fe /hi-ui/src/store/channels.js | |
| parent | bc514e0ea5f0a553f15ab8275961907877181520 (diff) | |
Provide named operations for stored state.
Diffstat (limited to 'hi-ui/src/store/channels.js')
| -rw-r--r-- | hi-ui/src/store/channels.js | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/hi-ui/src/store/channels.js b/hi-ui/src/store/channels.js new file mode 100644 index 0000000..20702cc --- /dev/null +++ b/hi-ui/src/store/channels.js @@ -0,0 +1,71 @@ +export class Channels { + constructor() { + this.channels = []; + } + + setChannels(channels) { + this.channels = [...channels]; + this.sort(); + return this; + } + + addChannel(channel) { + this.channels = [...this.channels, channel]; + this.sort(); + return this; + } + + deleteChannel(id) { + const channelIndex = this.channels.map((e) => e.id).indexOf(id); + if (channelIndex !== -1) { + this.channels.splice(channelIndex, 1); + } + return this; + } + + sort() { + this.channels.sort((a, b) => { + if (a.name < b.name) { + return -1; + } else if (a.name > b.name) { + return 1; + } + return 0; + }); + } +} + +export class ActiveChannel { + constructor() { + this.channel = null; + } + + isSet() { + return this.channel !== null; + } + + get() { + return this.channel; + } + + is(id) { + return this.channel === id; + } + + set(id) { + this.channel = id; + return this; + } + + deleteChannel(id) { + if (this.is(id)) { + return this.clear(); + } + return this; + } + + clear() { + this.channel = null; + return this; + } +} |
