diff options
author | Chocobozzz <me@florianbigard.com> | 2018-06-18 09:46:58 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-06-18 10:26:19 +0200 |
commit | 7b3a99d51716e404bdea0cef8d1f994aab0e8aac (patch) | |
tree | f1a07e610a1db0afd9d08083a148381ae1eaf0cf /client/src/assets/player/peertube-player-local-storage.ts | |
parent | f5a2dc48eb80ab7842cec1da7269b5d153208689 (diff) | |
download | PeerTube-7b3a99d51716e404bdea0cef8d1f994aab0e8aac.tar.gz PeerTube-7b3a99d51716e404bdea0cef8d1f994aab0e8aac.tar.zst PeerTube-7b3a99d51716e404bdea0cef8d1f994aab0e8aac.zip |
Move player local storage functions in their own file
Diffstat (limited to 'client/src/assets/player/peertube-player-local-storage.ts')
-rw-r--r-- | client/src/assets/player/peertube-player-local-storage.ts | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/client/src/assets/player/peertube-player-local-storage.ts b/client/src/assets/player/peertube-player-local-storage.ts new file mode 100644 index 000000000..6882f68a6 --- /dev/null +++ b/client/src/assets/player/peertube-player-local-storage.ts | |||
@@ -0,0 +1,86 @@ | |||
1 | function getStoredVolume () { | ||
2 | const value = getLocalStorage('volume') | ||
3 | if (value !== null && value !== undefined) { | ||
4 | const valueNumber = parseFloat(value) | ||
5 | if (isNaN(valueNumber)) return undefined | ||
6 | |||
7 | return valueNumber | ||
8 | } | ||
9 | |||
10 | return undefined | ||
11 | } | ||
12 | |||
13 | function getStoredMute () { | ||
14 | const value = getLocalStorage('mute') | ||
15 | if (value !== null && value !== undefined) return value === 'true' | ||
16 | |||
17 | return undefined | ||
18 | } | ||
19 | |||
20 | function getStoredTheater () { | ||
21 | const value = getLocalStorage('theater-enabled') | ||
22 | if (value !== null && value !== undefined) return value === 'true' | ||
23 | |||
24 | return undefined | ||
25 | } | ||
26 | |||
27 | function saveVolumeInStore (value: number) { | ||
28 | return setLocalStorage('volume', value.toString()) | ||
29 | } | ||
30 | |||
31 | function saveMuteInStore (value: boolean) { | ||
32 | return setLocalStorage('mute', value.toString()) | ||
33 | } | ||
34 | |||
35 | function saveTheaterInStore (enabled: boolean) { | ||
36 | return setLocalStorage('theater-enabled', enabled.toString()) | ||
37 | } | ||
38 | |||
39 | function saveAverageBandwidth (value: number) { | ||
40 | return setLocalStorage('average-bandwidth', value.toString()) | ||
41 | } | ||
42 | |||
43 | function getAverageBandwidthInStore () { | ||
44 | const value = getLocalStorage('average-bandwidth') | ||
45 | if (value !== null && value !== undefined) { | ||
46 | const valueNumber = parseInt(value, 10) | ||
47 | if (isNaN(valueNumber)) return undefined | ||
48 | |||
49 | return valueNumber | ||
50 | } | ||
51 | |||
52 | return undefined | ||
53 | } | ||
54 | |||
55 | |||
56 | // --------------------------------------------------------------------------- | ||
57 | |||
58 | export { | ||
59 | getStoredVolume, | ||
60 | getStoredMute, | ||
61 | getStoredTheater, | ||
62 | saveVolumeInStore, | ||
63 | saveMuteInStore, | ||
64 | saveTheaterInStore, | ||
65 | saveAverageBandwidth, | ||
66 | getAverageBandwidthInStore | ||
67 | } | ||
68 | |||
69 | // --------------------------------------------------------------------------- | ||
70 | |||
71 | const KEY_PREFIX = 'peertube-videojs-' | ||
72 | |||
73 | function getLocalStorage (key: string) { | ||
74 | try { | ||
75 | return localStorage.getItem(KEY_PREFIX + key) | ||
76 | } catch { | ||
77 | return undefined | ||
78 | } | ||
79 | } | ||
80 | |||
81 | function setLocalStorage (key: string, value: string) { | ||
82 | try { | ||
83 | localStorage.setItem(KEY_PREFIX + key, value) | ||
84 | } catch { /* empty */ | ||
85 | } | ||
86 | } | ||