]>
Commit | Line | Data |
---|---|---|
e945b184 | 1 | import { is18nLocale, isDefaultLocale } from '../../../../shared/models/i18n/i18n' |
6cca7360 | 2 | import { VideoFile } from '../../../../shared/models/videos' |
e945b184 | 3 | |
c6352f2c C |
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 | ||
a8462c8e C |
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 | ||
c6352f2c C |
54 | function saveVolumeInStore (value: number) { |
55 | return setLocalStorage('volume', value.toString()) | |
56 | } | |
57 | ||
58 | function saveMuteInStore (value: boolean) { | |
59 | return setLocalStorage('mute', value.toString()) | |
60 | } | |
61 | ||
a8462c8e C |
62 | function saveAverageBandwidth (value: number) { |
63 | return setLocalStorage('average-bandwidth', value.toString()) | |
64 | } | |
65 | ||
d1bd87e0 C |
66 | function isMobile () { |
67 | return /iPhone|iPad|iPod|Android/i.test(navigator.userAgent) | |
68 | } | |
69 | ||
960a11e8 C |
70 | function buildVideoLink (time?: number) { |
71 | let href = window.location.href.replace('/embed/', '/watch/') | |
72 | if (time) { | |
73 | const timeInt = Math.floor(time) | |
74 | ||
75 | if (window.location.search) href += '&start=' + timeInt | |
76 | else href += '?start=' + timeInt | |
77 | } | |
78 | ||
79 | return href | |
80 | } | |
81 | ||
82 | function buildVideoEmbed (embedUrl: string) { | |
83 | return '<iframe width="560" height="315" ' + | |
84 | 'src="' + embedUrl + '" ' + | |
85 | 'frameborder="0" allowfullscreen>' + | |
86 | '</iframe>' | |
87 | } | |
88 | ||
89 | function copyToClipboard (text: string) { | |
90 | const el = document.createElement('textarea') | |
91 | el.value = text | |
92 | el.setAttribute('readonly', '') | |
93 | el.style.position = 'absolute' | |
94 | el.style.left = '-9999px' | |
95 | document.body.appendChild(el) | |
96 | el.select() | |
97 | document.execCommand('copy') | |
98 | document.body.removeChild(el) | |
99 | } | |
100 | ||
6cca7360 C |
101 | function videoFileMaxByResolution (files: VideoFile[]) { |
102 | let max = files[0] | |
103 | ||
104 | for (let i = 1; i < files.length; i++) { | |
105 | const file = files[i] | |
106 | if (max.resolution.id < file.resolution.id) max = file | |
107 | } | |
108 | ||
109 | return max | |
110 | } | |
111 | ||
112 | function videoFileMinByResolution (files: VideoFile[]) { | |
113 | let min = files[0] | |
114 | ||
115 | for (let i = 1; i < files.length; i++) { | |
116 | const file = files[i] | |
117 | if (min.resolution.id > file.resolution.id) min = file | |
118 | } | |
119 | ||
120 | return min | |
121 | } | |
122 | ||
c6352f2c C |
123 | export { |
124 | toTitleCase, | |
960a11e8 | 125 | buildVideoLink, |
c6352f2c C |
126 | getStoredVolume, |
127 | saveVolumeInStore, | |
a8462c8e C |
128 | saveAverageBandwidth, |
129 | getAverageBandwidth, | |
c6352f2c | 130 | saveMuteInStore, |
960a11e8 | 131 | buildVideoEmbed, |
c6352f2c | 132 | getStoredMute, |
6cca7360 C |
133 | videoFileMaxByResolution, |
134 | videoFileMinByResolution, | |
960a11e8 | 135 | copyToClipboard, |
d1bd87e0 | 136 | isMobile, |
c6352f2c C |
137 | bytes |
138 | } | |
139 | ||
140 | // --------------------------------------------------------------------------- | |
141 | ||
142 | const KEY_PREFIX = 'peertube-videojs-' | |
143 | ||
144 | function getLocalStorage (key: string) { | |
145 | try { | |
146 | return localStorage.getItem(KEY_PREFIX + key) | |
147 | } catch { | |
148 | return undefined | |
149 | } | |
150 | } | |
151 | ||
152 | function setLocalStorage (key: string, value: string) { | |
153 | try { | |
154 | localStorage.setItem(KEY_PREFIX + key, value) | |
155 | } catch { /* empty */ } | |
156 | } |