1 import { map } from 'rxjs/operators'
2 import { SelectChannelItem } from 'src/types/select-options-item.model'
3 import { DatePipe } from '@angular/common'
4 import { HttpErrorResponse } from '@angular/common/http'
5 import { Notifier } from '@app/core'
6 import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
7 import { environment } from '../../environments/environment'
8 import { AuthService } from '../core/auth'
10 // Thanks: https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript
11 function getParameterByName (name: string, url: string) {
12 if (!url) url = window.location.href
13 name = name.replace(/[\[\]]/g, '\\$&')
15 const regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)')
16 const results = regex.exec(url)
18 if (!results) return null
19 if (!results[2]) return ''
21 return decodeURIComponent(results[2].replace(/\+/g, ' '))
24 function listUserChannels (authService: AuthService) {
25 return authService.userInformationLoaded
27 const user = authService.getUser()
28 if (!user) return undefined
30 const videoChannels = user.videoChannels
31 if (Array.isArray(videoChannels) === false) return undefined
33 return videoChannels.map(c => ({
37 avatarPath: c.avatar?.path
38 }) as SelectChannelItem)
42 function getAbsoluteAPIUrl () {
43 let absoluteAPIUrl = environment.hmr === true
44 ? 'http://localhost:9000'
47 if (!absoluteAPIUrl) {
48 // The API is on the same domain
49 absoluteAPIUrl = window.location.origin
55 function getAbsoluteEmbedUrl () {
56 let absoluteEmbedUrl = environment.originServerUrl
57 if (!absoluteEmbedUrl) {
58 // The Embed is on the same domain
59 absoluteEmbedUrl = window.location.origin
62 return absoluteEmbedUrl
65 const datePipe = new DatePipe('en')
66 function dateToHuman (date: string) {
67 return datePipe.transform(date, 'medium')
70 function durationToString (duration: number) {
71 const hours = Math.floor(duration / 3600)
72 const minutes = Math.floor((duration % 3600) / 60)
73 const seconds = duration % 60
75 const minutesPadding = minutes >= 10 ? '' : '0'
76 const secondsPadding = seconds >= 10 ? '' : '0'
77 const displayedHours = hours > 0 ? hours.toString() + ':' : ''
80 displayedHours + minutesPadding + minutes.toString() + ':' + secondsPadding + seconds.toString()
84 function immutableAssign <A, B> (target: A, source: B) {
85 return Object.assign({}, target, source)
88 // Thanks: https://gist.github.com/ghinda/8442a57f22099bdb2e34
89 function objectToFormData (obj: any, form?: FormData, namespace?: string) {
90 const fd = form || new FormData()
93 for (const key of Object.keys(obj)) {
94 if (namespace) formKey = `${namespace}[${key}]`
97 if (obj[key] === undefined) continue
99 if (Array.isArray(obj[key]) && obj[key].length === 0) {
104 if (obj[key] !== null && typeof obj[ key ] === 'object' && !(obj[ key ] instanceof File)) {
105 objectToFormData(obj[ key ], fd, formKey)
107 fd.append(formKey, obj[ key ])
114 function objectLineFeedToHtml (obj: any, keyToNormalize: string) {
115 return immutableAssign(obj, {
116 [keyToNormalize]: lineFeedToHtml(obj[keyToNormalize])
120 function lineFeedToHtml (text: string) {
121 if (!text) return text
123 return text.replace(/\r?\n|\r/g, '<br />')
126 function removeElementFromArray <T> (arr: T[], elem: T) {
127 const index = arr.indexOf(elem)
128 if (index !== -1) arr.splice(index, 1)
131 function sortBy (obj: any[], key1: string, key2?: string) {
132 return obj.sort((a, b) => {
133 const elem1 = key2 ? a[key1][key2] : a[key1]
134 const elem2 = key2 ? b[key1][key2] : b[key1]
136 if (elem1 < elem2) return -1
137 if (elem1 === elem2) return 0
142 function scrollToTop (behavior: 'auto' | 'smooth' = 'auto') {
150 function isInViewport (el: HTMLElement) {
151 const bounding = el.getBoundingClientRect()
154 bounding.left >= 0 &&
155 bounding.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
156 bounding.right <= (window.innerWidth || document.documentElement.clientWidth)
160 function isXPercentInViewport (el: HTMLElement, percentVisible: number) {
161 const rect = el.getBoundingClientRect()
162 const windowHeight = (window.innerHeight || document.documentElement.clientHeight)
165 Math.floor(100 - (((rect.top >= 0 ? 0 : rect.top) / +-(rect.height / 1)) * 100)) < percentVisible ||
166 Math.floor(100 - ((rect.bottom - windowHeight) / rect.height) * 100) < percentVisible
170 function uploadErrorHandler (parameters: {
171 err: HttpErrorResponse
176 const { err, name, notifier, sticky } = { sticky: false, ...parameters }
177 const title = $localize`The upload failed`
178 let message = err.message
180 if (err instanceof ErrorEvent) { // network error
181 message = $localize`The connection was interrupted`
182 notifier.error(message, title, null, sticky)
183 } else if (err.status === HttpStatusCode.REQUEST_TIMEOUT_408) {
184 message = $localize`Your ${name} file couldn't be transferred before the set timeout (usually 10min)`
185 notifier.error(message, title, null, sticky)
186 } else if (err.status === HttpStatusCode.PAYLOAD_TOO_LARGE_413) {
187 const maxFileSize = err.headers?.get('X-File-Maximum-Size') || '8G'
188 message = $localize`Your ${name} file was too large (max. size: ${maxFileSize})`
189 notifier.error(message, title, null, sticky)
191 notifier.error(err.message, title)
207 objectLineFeedToHtml,
208 removeElementFromArray,
211 isXPercentInViewport,