]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/assets/player/utils.ts
(doc) note about PeerTube for YunoHost (#624)
[github/Chocobozzz/PeerTube.git] / client / src / assets / player / utils.ts
CommitLineData
c6352f2c
C
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
a8462c8e
C
39function getAverageBandwidth () {
40 const value = getLocalStorage('average-bandwidth')
41 if (value !== null && value !== undefined) {
42 const valueNumber = parseInt(value, 10)
43 if (isNaN(valueNumber)) return undefined
44
45 return valueNumber
46 }
47
48 return undefined
49}
50
c6352f2c
C
51function saveVolumeInStore (value: number) {
52 return setLocalStorage('volume', value.toString())
53}
54
55function saveMuteInStore (value: boolean) {
56 return setLocalStorage('mute', value.toString())
57}
58
a8462c8e
C
59function saveAverageBandwidth (value: number) {
60 return setLocalStorage('average-bandwidth', value.toString())
61}
62
d1bd87e0
C
63function isMobile () {
64 return /iPhone|iPad|iPod|Android/i.test(navigator.userAgent)
65}
66
960a11e8
C
67function buildVideoLink (time?: number) {
68 let href = window.location.href.replace('/embed/', '/watch/')
69 if (time) {
70 const timeInt = Math.floor(time)
71
72 if (window.location.search) href += '&start=' + timeInt
73 else href += '?start=' + timeInt
74 }
75
76 return href
77}
78
79function buildVideoEmbed (embedUrl: string) {
80 return '<iframe width="560" height="315" ' +
81 'src="' + embedUrl + '" ' +
82 'frameborder="0" allowfullscreen>' +
83 '</iframe>'
84}
85
86function copyToClipboard (text: string) {
87 const el = document.createElement('textarea')
88 el.value = text
89 el.setAttribute('readonly', '')
90 el.style.position = 'absolute'
91 el.style.left = '-9999px'
92 document.body.appendChild(el)
93 el.select()
94 document.execCommand('copy')
95 document.body.removeChild(el)
96}
97
c6352f2c
C
98export {
99 toTitleCase,
960a11e8 100 buildVideoLink,
c6352f2c
C
101 getStoredVolume,
102 saveVolumeInStore,
a8462c8e
C
103 saveAverageBandwidth,
104 getAverageBandwidth,
c6352f2c 105 saveMuteInStore,
960a11e8 106 buildVideoEmbed,
c6352f2c 107 getStoredMute,
960a11e8 108 copyToClipboard,
d1bd87e0 109 isMobile,
c6352f2c
C
110 bytes
111}
112
113// ---------------------------------------------------------------------------
114
115const KEY_PREFIX = 'peertube-videojs-'
116
117function getLocalStorage (key: string) {
118 try {
119 return localStorage.getItem(KEY_PREFIX + key)
120 } catch {
121 return undefined
122 }
123}
124
125function setLocalStorage (key: string, value: string) {
126 try {
127 localStorage.setItem(KEY_PREFIX + key, value)
128 } catch { /* empty */ }
129}