]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/assets/player/utils.ts
Localize player
[github/Chocobozzz/PeerTube.git] / client / src / assets / player / utils.ts
1 import { is18nLocale, isDefaultLocale } from '../../../../shared/models/i18n/i18n'
2
3 function toTitleCase (str: string) {
4 return str.charAt(0).toUpperCase() + str.slice(1)
5 }
6
7 // https://github.com/danrevah/ngx-pipes/blob/master/src/pipes/math/bytes.ts
8 // Don't import all Angular stuff, just copy the code with shame
9 const dictionaryBytes: Array<{max: number, type: string}> = [
10 { max: 1024, type: 'B' },
11 { max: 1048576, type: 'KB' },
12 { max: 1073741824, type: 'MB' },
13 { max: 1.0995116e12, type: 'GB' }
14 ]
15 function bytes (value) {
16 const format = dictionaryBytes.find(d => value < d.max) || dictionaryBytes[dictionaryBytes.length - 1]
17 const calc = Math.floor(value / (format.max / 1024)).toString()
18
19 return [ calc, format.type ]
20 }
21
22 function getStoredVolume () {
23 const value = getLocalStorage('volume')
24 if (value !== null && value !== undefined) {
25 const valueNumber = parseFloat(value)
26 if (isNaN(valueNumber)) return undefined
27
28 return valueNumber
29 }
30
31 return undefined
32 }
33
34 function getStoredMute () {
35 const value = getLocalStorage('mute')
36 if (value !== null && value !== undefined) return value === 'true'
37
38 return undefined
39 }
40
41 function getAverageBandwidth () {
42 const value = getLocalStorage('average-bandwidth')
43 if (value !== null && value !== undefined) {
44 const valueNumber = parseInt(value, 10)
45 if (isNaN(valueNumber)) return undefined
46
47 return valueNumber
48 }
49
50 return undefined
51 }
52
53 function saveVolumeInStore (value: number) {
54 return setLocalStorage('volume', value.toString())
55 }
56
57 function saveMuteInStore (value: boolean) {
58 return setLocalStorage('mute', value.toString())
59 }
60
61 function saveAverageBandwidth (value: number) {
62 return setLocalStorage('average-bandwidth', value.toString())
63 }
64
65 function isMobile () {
66 return /iPhone|iPad|iPod|Android/i.test(navigator.userAgent)
67 }
68
69 function buildVideoLink (time?: number) {
70 let href = window.location.href.replace('/embed/', '/watch/')
71 if (time) {
72 const timeInt = Math.floor(time)
73
74 if (window.location.search) href += '&start=' + timeInt
75 else href += '?start=' + timeInt
76 }
77
78 return href
79 }
80
81 function buildVideoEmbed (embedUrl: string) {
82 return '<iframe width="560" height="315" ' +
83 'src="' + embedUrl + '" ' +
84 'frameborder="0" allowfullscreen>' +
85 '</iframe>'
86 }
87
88 function copyToClipboard (text: string) {
89 const el = document.createElement('textarea')
90 el.value = text
91 el.setAttribute('readonly', '')
92 el.style.position = 'absolute'
93 el.style.left = '-9999px'
94 document.body.appendChild(el)
95 el.select()
96 document.execCommand('copy')
97 document.body.removeChild(el)
98 }
99
100 export {
101 toTitleCase,
102 buildVideoLink,
103 getStoredVolume,
104 saveVolumeInStore,
105 saveAverageBandwidth,
106 getAverageBandwidth,
107 saveMuteInStore,
108 buildVideoEmbed,
109 getStoredMute,
110 copyToClipboard,
111 isMobile,
112 bytes
113 }
114
115 // ---------------------------------------------------------------------------
116
117 const KEY_PREFIX = 'peertube-videojs-'
118
119 function getLocalStorage (key: string) {
120 try {
121 return localStorage.getItem(KEY_PREFIX + key)
122 } catch {
123 return undefined
124 }
125 }
126
127 function setLocalStorage (key: string, value: string) {
128 try {
129 localStorage.setItem(KEY_PREFIX + key, value)
130 } catch { /* empty */ }
131 }