]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/assets/player/utils.ts
Localize player
[github/Chocobozzz/PeerTube.git] / client / src / assets / player / utils.ts
CommitLineData
e945b184
C
1import { is18nLocale, isDefaultLocale } from '../../../../shared/models/i18n/i18n'
2
c6352f2c
C
3function 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
9const 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]
15function 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
22function 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
34function getStoredMute () {
35 const value = getLocalStorage('mute')
36 if (value !== null && value !== undefined) return value === 'true'
37
38 return undefined
39}
40
a8462c8e
C
41function 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
c6352f2c
C
53function saveVolumeInStore (value: number) {
54 return setLocalStorage('volume', value.toString())
55}
56
57function saveMuteInStore (value: boolean) {
58 return setLocalStorage('mute', value.toString())
59}
60
a8462c8e
C
61function saveAverageBandwidth (value: number) {
62 return setLocalStorage('average-bandwidth', value.toString())
63}
64
d1bd87e0
C
65function isMobile () {
66 return /iPhone|iPad|iPod|Android/i.test(navigator.userAgent)
67}
68
960a11e8
C
69function 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
81function buildVideoEmbed (embedUrl: string) {
82 return '<iframe width="560" height="315" ' +
83 'src="' + embedUrl + '" ' +
84 'frameborder="0" allowfullscreen>' +
85 '</iframe>'
86}
87
88function 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
c6352f2c
C
100export {
101 toTitleCase,
960a11e8 102 buildVideoLink,
c6352f2c
C
103 getStoredVolume,
104 saveVolumeInStore,
a8462c8e
C
105 saveAverageBandwidth,
106 getAverageBandwidth,
c6352f2c 107 saveMuteInStore,
960a11e8 108 buildVideoEmbed,
c6352f2c 109 getStoredMute,
960a11e8 110 copyToClipboard,
d1bd87e0 111 isMobile,
c6352f2c
C
112 bytes
113}
114
115// ---------------------------------------------------------------------------
116
117const KEY_PREFIX = 'peertube-videojs-'
118
119function getLocalStorage (key: string) {
120 try {
121 return localStorage.getItem(KEY_PREFIX + key)
122 } catch {
123 return undefined
124 }
125}
126
127function setLocalStorage (key: string, value: string) {
128 try {
129 localStorage.setItem(KEY_PREFIX + key, value)
130 } catch { /* empty */ }
131}