1 import { is18nLocale, isDefaultLocale } from '../../../../shared/models/i18n/i18n'
2 import { VideoFile } from '../../../../shared/models/videos'
4 function toTitleCase (str: string) {
5 return str.charAt(0).toUpperCase() + str.slice(1)
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' }
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()
20 return [ calc, format.type ]
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
35 function getStoredMute () {
36 const value = getLocalStorage('mute')
37 if (value !== null && value !== undefined) return value === 'true'
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
54 function getStoredTheater () {
55 const value = getLocalStorage('theater-enabled')
56 if (value !== null && value !== undefined) return value === 'true'
61 function saveVolumeInStore (value: number) {
62 return setLocalStorage('volume', value.toString())
65 function saveMuteInStore (value: boolean) {
66 return setLocalStorage('mute', value.toString())
69 function saveTheaterInStore (enabled: boolean) {
70 return setLocalStorage('theater-enabled', enabled.toString())
73 function saveAverageBandwidth (value: number) {
74 return setLocalStorage('average-bandwidth', value.toString())
77 function isMobile () {
78 return /iPhone|iPad|iPod|Android/i.test(navigator.userAgent)
81 function buildVideoLink (time?: number) {
82 let href = window.location.href.replace('/embed/', '/watch/')
84 const timeInt = Math.floor(time)
86 if (window.location.search) href += '&start=' + timeInt
87 else href += '?start=' + timeInt
93 function buildVideoEmbed (embedUrl: string) {
94 return '<iframe width="560" height="315" ' +
95 'sandbox="allow-same-origin allow-scripts" ' +
96 'src="' + embedUrl + '" ' +
97 'frameborder="0" allowfullscreen>' +
101 function copyToClipboard (text: string) {
102 const el = document.createElement('textarea')
104 el.setAttribute('readonly', '')
105 el.style.position = 'absolute'
106 el.style.left = '-9999px'
107 document.body.appendChild(el)
109 document.execCommand('copy')
110 document.body.removeChild(el)
113 function videoFileMaxByResolution (files: VideoFile[]) {
116 for (let i = 1; i < files.length; i++) {
117 const file = files[i]
118 if (max.resolution.id < file.resolution.id) max = file
124 function videoFileMinByResolution (files: VideoFile[]) {
127 for (let i = 1; i < files.length; i++) {
128 const file = files[i]
129 if (min.resolution.id > file.resolution.id) min = file
140 saveAverageBandwidth,
145 videoFileMaxByResolution,
146 videoFileMinByResolution,
154 // ---------------------------------------------------------------------------
156 const KEY_PREFIX = 'peertube-videojs-'
158 function getLocalStorage (key: string) {
160 return localStorage.getItem(KEY_PREFIX + key)
166 function setLocalStorage (key: string, value: string) {
168 localStorage.setItem(KEY_PREFIX + key, value)
169 } catch { /* empty */ }