Add theatre mode
[oweals/peertube.git] / client / src / assets / player / utils.ts
1 import { is18nLocale, isDefaultLocale } from '../../../../shared/models/i18n/i18n'
2 import { VideoFile } from '../../../../shared/models/videos'
3
4 function toTitleCase (str: string) {
5   return str.charAt(0).toUpperCase() + str.slice(1)
6 }
7
8 // https://github.com/danrevah/ngx-pipes/blob/master/src/pipes/math/bytes.ts
9 // Don't import all Angular stuff, just copy the code with shame
10 const dictionaryBytes: Array<{max: number, type: string}> = [
11   { max: 1024, type: 'B' },
12   { max: 1048576, type: 'KB' },
13   { max: 1073741824, type: 'MB' },
14   { max: 1.0995116e12, type: 'GB' }
15 ]
16 function bytes (value) {
17   const format = dictionaryBytes.find(d => value < d.max) || dictionaryBytes[dictionaryBytes.length - 1]
18   const calc = Math.floor(value / (format.max / 1024)).toString()
19
20   return [ calc, format.type ]
21 }
22
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 () {
78   return /iPhone|iPad|iPod|Android/i.test(navigator.userAgent)
79 }
80
81 function buildVideoLink (time?: number) {
82   let href = window.location.href.replace('/embed/', '/watch/')
83   if (time) {
84     const timeInt = Math.floor(time)
85
86     if (window.location.search) href += '&start=' + timeInt
87     else href += '?start=' + timeInt
88   }
89
90   return href
91 }
92
93 function buildVideoEmbed (embedUrl: string) {
94   return '<iframe width="560" height="315" ' +
95     'src="' + embedUrl + '" ' +
96     'frameborder="0" allowfullscreen>' +
97     '</iframe>'
98 }
99
100 function copyToClipboard (text: string) {
101   const el = document.createElement('textarea')
102   el.value = text
103   el.setAttribute('readonly', '')
104   el.style.position = 'absolute'
105   el.style.left = '-9999px'
106   document.body.appendChild(el)
107   el.select()
108   document.execCommand('copy')
109   document.body.removeChild(el)
110 }
111
112 function videoFileMaxByResolution (files: VideoFile[]) {
113   let max = files[0]
114
115   for (let i = 1; i < files.length; i++) {
116     const file = files[i]
117     if (max.resolution.id < file.resolution.id) max = file
118   }
119
120   return max
121 }
122
123 function videoFileMinByResolution (files: VideoFile[]) {
124   let min = files[0]
125
126   for (let i = 1; i < files.length; i++) {
127     const file = files[i]
128     if (min.resolution.id > file.resolution.id) min = file
129   }
130
131   return min
132 }
133
134 export {
135   toTitleCase,
136   buildVideoLink,
137   getStoredVolume,
138   saveVolumeInStore,
139   saveAverageBandwidth,
140   getAverageBandwidth,
141   saveMuteInStore,
142   buildVideoEmbed,
143   getStoredMute,
144   videoFileMaxByResolution,
145   videoFileMinByResolution,
146   copyToClipboard,
147   getStoredTheater,
148   saveTheaterInStore,
149   isMobile,
150   bytes
151 }
152
153 // ---------------------------------------------------------------------------
154
155 const KEY_PREFIX = 'peertube-videojs-'
156
157 function getLocalStorage (key: string) {
158   try {
159     return localStorage.getItem(KEY_PREFIX + key)
160   } catch {
161     return undefined
162   }
163 }
164
165 function setLocalStorage (key: string, value: string) {
166   try {
167     localStorage.setItem(KEY_PREFIX + key, value)
168   } catch { /* empty */ }
169 }