aboutsummaryrefslogtreecommitdiffhomepage
path: root/client
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-06-18 09:46:58 +0200
committerChocobozzz <me@florianbigard.com>2018-06-18 10:26:19 +0200
commit7b3a99d51716e404bdea0cef8d1f994aab0e8aac (patch)
treef1a07e610a1db0afd9d08083a148381ae1eaf0cf /client
parentf5a2dc48eb80ab7842cec1da7269b5d153208689 (diff)
downloadPeerTube-7b3a99d51716e404bdea0cef8d1f994aab0e8aac.tar.gz
PeerTube-7b3a99d51716e404bdea0cef8d1f994aab0e8aac.tar.zst
PeerTube-7b3a99d51716e404bdea0cef8d1f994aab0e8aac.zip
Move player local storage functions in their own file
Diffstat (limited to 'client')
-rw-r--r--client/src/assets/player/peertube-player-local-storage.ts86
-rw-r--r--client/src/assets/player/peertube-videojs-plugin.ts17
-rw-r--r--client/src/assets/player/theater-button.ts3
-rw-r--r--client/src/assets/player/utils.ts83
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 @@
1function 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
13function getStoredMute () {
14 const value = getLocalStorage('mute')
15 if (value !== null && value !== undefined) return value === 'true'
16
17 return undefined
18}
19
20function getStoredTheater () {
21 const value = getLocalStorage('theater-enabled')
22 if (value !== null && value !== undefined) return value === 'true'
23
24 return undefined
25}
26
27function saveVolumeInStore (value: number) {
28 return setLocalStorage('volume', value.toString())
29}
30
31function saveMuteInStore (value: boolean) {
32 return setLocalStorage('mute', value.toString())
33}
34
35function saveTheaterInStore (enabled: boolean) {
36 return setLocalStorage('theater-enabled', enabled.toString())
37}
38
39function saveAverageBandwidth (value: number) {
40 return setLocalStorage('average-bandwidth', value.toString())
41}
42
43function 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
58export {
59 getStoredVolume,
60 getStoredMute,
61 getStoredTheater,
62 saveVolumeInStore,
63 saveMuteInStore,
64 saveTheaterInStore,
65 saveAverageBandwidth,
66 getAverageBandwidthInStore
67}
68
69// ---------------------------------------------------------------------------
70
71const KEY_PREFIX = 'peertube-videojs-'
72
73function getLocalStorage (key: string) {
74 try {
75 return localStorage.getItem(KEY_PREFIX + key)
76 } catch {
77 return undefined
78 }
79}
80
81function 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'
4import { renderVideo } from './video-renderer' 4import { renderVideo } from './video-renderer'
5import './settings-menu-button' 5import './settings-menu-button'
6import { PeertubePluginOptions, VideoJSComponentInterface, videojsUntyped } from './peertube-videojs-typings' 6import { PeertubePluginOptions, VideoJSComponentInterface, videojsUntyped } from './peertube-videojs-typings'
7import { isMobile, videoFileMaxByResolution, videoFileMinByResolution } from './utils'
8import * as CacheChunkStore from 'cache-chunk-store'
9import { PeertubeChunkStore } from './peertube-chunk-store'
7import { 10import {
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'
17import * as CacheChunkStore from 'cache-chunk-store'
18import { PeertubeChunkStore } from './peertube-chunk-store'
19 18
20const Plugin: VideoJSComponentInterface = videojs.getPlugin('plugin') 19const Plugin: VideoJSComponentInterface = videojs.getPlugin('plugin')
21class PeerTubePlugin extends Plugin { 20class 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 @@
1import { VideoJSComponentInterface, videojsUntyped } from './peertube-videojs-typings' 1import { VideoJSComponentInterface, videojsUntyped } from './peertube-videojs-typings'
2import { getStoredTheater, saveTheaterInStore } from './utils' 2import { saveTheaterInStore } from './peertube-player-local-storage'
3import { getStoredTheater } from './peertube-player-local-storage'
3 4
4const Button: VideoJSComponentInterface = videojsUntyped.getComponent('Button') 5const Button: VideoJSComponentInterface = videojsUntyped.getComponent('Button')
5class TheaterButton extends Button { 6class 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 @@
1import { is18nLocale, isDefaultLocale } from '../../../../shared/models/i18n/i18n'
2import { VideoFile } from '../../../../shared/models/videos' 1import { VideoFile } from '../../../../shared/models/videos'
3 2
4function toTitleCase (str: string) { 3function toTitleCase (str: string) {
@@ -20,60 +19,6 @@ function bytes (value) {
20 return [ calc, format.type ] 19 return [ calc, format.type ]
21} 20}
22 21
23function 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
35function getStoredMute () {
36 const value = getLocalStorage('mute')
37 if (value !== null && value !== undefined) return value === 'true'
38
39 return undefined
40}
41
42function 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
54function getStoredTheater () {
55 const value = getLocalStorage('theater-enabled')
56 if (value !== null && value !== undefined) return value === 'true'
57
58 return undefined
59}
60
61function saveVolumeInStore (value: number) {
62 return setLocalStorage('volume', value.toString())
63}
64
65function saveMuteInStore (value: boolean) {
66 return setLocalStorage('mute', value.toString())
67}
68
69function saveTheaterInStore (enabled: boolean) {
70 return setLocalStorage('theater-enabled', enabled.toString())
71}
72
73function saveAverageBandwidth (value: number) {
74 return setLocalStorage('average-bandwidth', value.toString())
75}
76
77function isMobile () { 22function 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
135export { 82export {
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
156const KEY_PREFIX = 'peertube-videojs-'
157
158function getLocalStorage (key: string) {
159 try {
160 return localStorage.getItem(KEY_PREFIX + key)
161 } catch {
162 return undefined
163 }
164}
165
166function setLocalStorage (key: string, value: string) {
167 try {
168 localStorage.setItem(KEY_PREFIX + key, value)
169 } catch { /* empty */ }
170}