summaryrefslogtreecommitdiff
path: root/hi-ui/src/store/channels.js
blob: bb6c86c5b4b88953d795723895318af371b4fb46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
export class Channels {
	constructor() {
		this.channels = [];
	}

	setChannels(channels) {
		this.channels = [...channels];
		this.sort();
		return this;
	}

	addChannel(id, name) {
		this.channels = [...this.channels, { id, name }];
		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;
	}
}