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 | |
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')
-rw-r--r-- | client/src/assets/player/peertube-player-local-storage.ts | 86 | ||||
-rw-r--r-- | client/src/assets/player/peertube-videojs-plugin.ts | 17 | ||||
-rw-r--r-- | client/src/assets/player/theater-button.ts | 3 | ||||
-rw-r--r-- | client/src/assets/player/utils.ts | 83 |
4 files changed, 98 insertions, 91 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 | } | ||
diff --git a/client/src/assets/player/peertube-videojs-plugin.ts b/client/src/assets/player/peertube-videojs-plugin.ts index b62dcb829..35a347e99 100644 --- a/client/src/assets/player/peertube-videojs-plugin.ts +++ b/client/src/assets/player/peertube-videojs-plugin.ts | |||
@@ -4,18 +4,17 @@ import { VideoFile } from '../../../../shared/models/videos/video.model' | |||
4 | import { renderVideo } from './video-renderer' | 4 | import { renderVideo } from './video-renderer' |
5 | import './settings-menu-button' | 5 | import './settings-menu-button' |
6 | import { PeertubePluginOptions, VideoJSComponentInterface, videojsUntyped } from './peertube-videojs-typings' | 6 | import { PeertubePluginOptions, VideoJSComponentInterface, videojsUntyped } from './peertube-videojs-typings' |
7 | import { isMobile, videoFileMaxByResolution, videoFileMinByResolution } from './utils' | ||
8 | import * as CacheChunkStore from 'cache-chunk-store' | ||
9 | import { PeertubeChunkStore } from './peertube-chunk-store' | ||
7 | import { | 10 | import { |
8 | getAverageBandwidth, | 11 | getAverageBandwidthInStore, |
9 | getStoredMute, | 12 | getStoredMute, |
10 | getStoredVolume, isMobile, | 13 | getStoredVolume, |
11 | saveAverageBandwidth, | 14 | saveAverageBandwidth, |
12 | saveMuteInStore, | 15 | saveMuteInStore, |
13 | saveVolumeInStore, | 16 | saveVolumeInStore |
14 | videoFileMaxByResolution, | 17 | } from './peertube-player-local-storage' |
15 | videoFileMinByResolution | ||
16 | } from './utils' | ||
17 | import * as CacheChunkStore from 'cache-chunk-store' | ||
18 | import { PeertubeChunkStore } from './peertube-chunk-store' | ||
19 | 18 | ||
20 | const Plugin: VideoJSComponentInterface = videojs.getPlugin('plugin') | 19 | const Plugin: VideoJSComponentInterface = videojs.getPlugin('plugin') |
21 | class PeerTubePlugin extends Plugin { | 20 | class PeerTubePlugin extends Plugin { |
@@ -148,7 +147,7 @@ class PeerTubePlugin extends Plugin { | |||
148 | ) { | 147 | ) { |
149 | // Automatically choose the adapted video file | 148 | // Automatically choose the adapted video file |
150 | if (videoFile === undefined) { | 149 | if (videoFile === undefined) { |
151 | const savedAverageBandwidth = getAverageBandwidth() | 150 | const savedAverageBandwidth = getAverageBandwidthInStore() |
152 | videoFile = savedAverageBandwidth | 151 | videoFile = savedAverageBandwidth |
153 | ? this.getAppropriateFile(savedAverageBandwidth) | 152 | ? this.getAppropriateFile(savedAverageBandwidth) |
154 | : this.pickAverageVideoFile() | 153 | : this.pickAverageVideoFile() |
diff --git a/client/src/assets/player/theater-button.ts b/client/src/assets/player/theater-button.ts index 1d330e08f..bad737eda 100644 --- a/client/src/assets/player/theater-button.ts +++ b/client/src/assets/player/theater-button.ts | |||
@@ -1,5 +1,6 @@ | |||
1 | import { VideoJSComponentInterface, videojsUntyped } from './peertube-videojs-typings' | 1 | import { VideoJSComponentInterface, videojsUntyped } from './peertube-videojs-typings' |
2 | import { getStoredTheater, saveTheaterInStore } from './utils' | 2 | import { saveTheaterInStore } from './peertube-player-local-storage' |
3 | import { getStoredTheater } from './peertube-player-local-storage' | ||
3 | 4 | ||
4 | const Button: VideoJSComponentInterface = videojsUntyped.getComponent('Button') | 5 | const Button: VideoJSComponentInterface = videojsUntyped.getComponent('Button') |
5 | class TheaterButton extends Button { | 6 | class TheaterButton extends Button { |
diff --git a/client/src/assets/player/utils.ts b/client/src/assets/player/utils.ts index 18a6b4dfa..c27e630e5 100644 --- a/client/src/assets/player/utils.ts +++ b/client/src/assets/player/utils.ts | |||
@@ -1,4 +1,3 @@ | |||
1 | import { is18nLocale, isDefaultLocale } from '../../../../shared/models/i18n/i18n' | ||
2 | import { VideoFile } from '../../../../shared/models/videos' | 1 | import { VideoFile } from '../../../../shared/models/videos' |
3 | 2 | ||
4 | function toTitleCase (str: string) { | 3 | function toTitleCase (str: string) { |
@@ -20,60 +19,6 @@ function bytes (value) { | |||
20 | return [ calc, format.type ] | 19 | return [ calc, format.type ] |
21 | } | 20 | } |
22 | 21 | ||
23 | function getStoredVolume () { | ||
24 | const value = getLocalStorage('volume') | ||
25 | if (value !== null && value !== undefined) { | ||
26 | const valueNumber = parseFloat(value) | ||
27 | if (isNaN(valueNumber)) return undefined | ||
28 | |||
29 | return valueNumber | ||
30 | } | ||
31 | |||
32 | return undefined | ||
33 | } | ||
34 | |||
35 | function getStoredMute () { | ||
36 | const value = getLocalStorage('mute') | ||
37 | if (value !== null && value !== undefined) return value === 'true' | ||
38 | |||
39 | return undefined | ||
40 | } | ||
41 | |||
42 | function getAverageBandwidth () { | ||
43 | const value = getLocalStorage('average-bandwidth') | ||
44 | if (value !== null && value !== undefined) { | ||
45 | const valueNumber = parseInt(value, 10) | ||
46 | if (isNaN(valueNumber)) return undefined | ||
47 | |||
48 | return valueNumber | ||
49 | } | ||
50 | |||
51 | return undefined | ||
52 | } | ||
53 | |||
54 | function getStoredTheater () { | ||
55 | const value = getLocalStorage('theater-enabled') | ||
56 | if (value !== null && value !== undefined) return value === 'true' | ||
57 | |||
58 | return undefined | ||
59 | } | ||
60 | |||
61 | function saveVolumeInStore (value: number) { | ||
62 | return setLocalStorage('volume', value.toString()) | ||
63 | } | ||
64 | |||
65 | function saveMuteInStore (value: boolean) { | ||
66 | return setLocalStorage('mute', value.toString()) | ||
67 | } | ||
68 | |||
69 | function saveTheaterInStore (enabled: boolean) { | ||
70 | return setLocalStorage('theater-enabled', enabled.toString()) | ||
71 | } | ||
72 | |||
73 | function saveAverageBandwidth (value: number) { | ||
74 | return setLocalStorage('average-bandwidth', value.toString()) | ||
75 | } | ||
76 | |||
77 | function isMobile () { | 22 | function isMobile () { |
78 | return /iPhone|iPad|iPod|Android/i.test(navigator.userAgent) | 23 | return /iPhone|iPad|iPod|Android/i.test(navigator.userAgent) |
79 | } | 24 | } |
@@ -132,39 +77,15 @@ function videoFileMinByResolution (files: VideoFile[]) { | |||
132 | return min | 77 | return min |
133 | } | 78 | } |
134 | 79 | ||
80 | // --------------------------------------------------------------------------- | ||
81 | |||
135 | export { | 82 | export { |
136 | toTitleCase, | 83 | toTitleCase, |
137 | buildVideoLink, | 84 | buildVideoLink, |
138 | getStoredVolume, | ||
139 | saveVolumeInStore, | ||
140 | saveAverageBandwidth, | ||
141 | getAverageBandwidth, | ||
142 | saveMuteInStore, | ||
143 | buildVideoEmbed, | 85 | buildVideoEmbed, |
144 | getStoredMute, | ||
145 | videoFileMaxByResolution, | 86 | videoFileMaxByResolution, |
146 | videoFileMinByResolution, | 87 | videoFileMinByResolution, |
147 | copyToClipboard, | 88 | copyToClipboard, |
148 | getStoredTheater, | ||
149 | saveTheaterInStore, | ||
150 | isMobile, | 89 | isMobile, |
151 | bytes | 90 | bytes |
152 | } | 91 | } |
153 | |||
154 | // --------------------------------------------------------------------------- | ||
155 | |||
156 | const KEY_PREFIX = 'peertube-videojs-' | ||
157 | |||
158 | function getLocalStorage (key: string) { | ||
159 | try { | ||
160 | return localStorage.getItem(KEY_PREFIX + key) | ||
161 | } catch { | ||
162 | return undefined | ||
163 | } | ||
164 | } | ||
165 | |||
166 | function setLocalStorage (key: string, value: string) { | ||
167 | try { | ||
168 | localStorage.setItem(KEY_PREFIX + key, value) | ||
169 | } catch { /* empty */ } | ||
170 | } | ||