aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/assets/player/utils.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-03-30 17:40:00 +0200
committerChocobozzz <me@florianbigard.com>2018-04-03 14:02:10 +0200
commitc6352f2c64f3c1ad54f8500f493587cdce3d33c9 (patch)
tree642a5b29b4d68ed8915e5e800232eab069303f79 /client/src/assets/player/utils.ts
parent6b9af1293621a81564296ead6f12f5e70eafbca2 (diff)
downloadPeerTube-c6352f2c64f3c1ad54f8500f493587cdce3d33c9.tar.gz
PeerTube-c6352f2c64f3c1ad54f8500f493587cdce3d33c9.tar.zst
PeerTube-c6352f2c64f3c1ad54f8500f493587cdce3d33c9.zip
Improve player
Add a settings dialog based on the work of Yanko Shterev (@yshterev): https://github.com/yshterev/videojs-settings-menu. Thanks!
Diffstat (limited to 'client/src/assets/player/utils.ts')
-rw-r--r--client/src/assets/player/utils.ts72
1 files changed, 72 insertions, 0 deletions
diff --git a/client/src/assets/player/utils.ts b/client/src/assets/player/utils.ts
new file mode 100644
index 000000000..7a99dba1a
--- /dev/null
+++ b/client/src/assets/player/utils.ts
@@ -0,0 +1,72 @@
1function toTitleCase (str: string) {
2 return str.charAt(0).toUpperCase() + str.slice(1)
3}
4
5// https://github.com/danrevah/ngx-pipes/blob/master/src/pipes/math/bytes.ts
6// Don't import all Angular stuff, just copy the code with shame
7const dictionaryBytes: Array<{max: number, type: string}> = [
8 { max: 1024, type: 'B' },
9 { max: 1048576, type: 'KB' },
10 { max: 1073741824, type: 'MB' },
11 { max: 1.0995116e12, type: 'GB' }
12]
13function bytes (value) {
14 const format = dictionaryBytes.find(d => value < d.max) || dictionaryBytes[dictionaryBytes.length - 1]
15 const calc = Math.floor(value / (format.max / 1024)).toString()
16
17 return [ calc, format.type ]
18}
19
20function getStoredVolume () {
21 const value = getLocalStorage('volume')
22 if (value !== null && value !== undefined) {
23 const valueNumber = parseFloat(value)
24 if (isNaN(valueNumber)) return undefined
25
26 return valueNumber
27 }
28
29 return undefined
30}
31
32function getStoredMute () {
33 const value = getLocalStorage('mute')
34 if (value !== null && value !== undefined) return value === 'true'
35
36 return undefined
37}
38
39function saveVolumeInStore (value: number) {
40 return setLocalStorage('volume', value.toString())
41}
42
43function saveMuteInStore (value: boolean) {
44 return setLocalStorage('mute', value.toString())
45}
46
47export {
48 toTitleCase,
49 getStoredVolume,
50 saveVolumeInStore,
51 saveMuteInStore,
52 getStoredMute,
53 bytes
54}
55
56// ---------------------------------------------------------------------------
57
58const KEY_PREFIX = 'peertube-videojs-'
59
60function getLocalStorage (key: string) {
61 try {
62 return localStorage.getItem(KEY_PREFIX + key)
63 } catch {
64 return undefined
65 }
66}
67
68function setLocalStorage (key: string, value: string) {
69 try {
70 localStorage.setItem(KEY_PREFIX + key, value)
71 } catch { /* empty */ }
72}