1 // Thanks: https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript
3 import { DatePipe } from '@angular/common'
4 import { environment } from '../../../environments/environment'
5 import { AuthService } from '../../core/auth'
7 function getParameterByName (name: string, url: string) {
8 if (!url) url = window.location.href
9 name = name.replace(/[\[\]]/g, '\\$&')
11 const regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)')
12 const results = regex.exec(url)
14 if (!results) return null
15 if (!results[2]) return ''
17 return decodeURIComponent(results[2].replace(/\+/g, ' '))
20 function populateAsyncUserVideoChannels (authService: AuthService, channel: { id: number, label: string, support?: string }[]) {
21 return new Promise(res => {
22 authService.userInformationLoaded
25 const user = authService.getUser()
28 const videoChannels = user.videoChannels
29 if (Array.isArray(videoChannels) === false) return
31 videoChannels.forEach(c => channel.push({ id: c.id, label: c.displayName, support: c.support }))
39 function getAbsoluteAPIUrl () {
40 let absoluteAPIUrl = environment.apiUrl
41 if (!absoluteAPIUrl) {
42 // The API is on the same domain
43 absoluteAPIUrl = window.location.origin
49 const datePipe = new DatePipe('en')
50 function dateToHuman (date: string) {
51 return datePipe.transform(date, 'medium')
54 function durationToString (duration: number) {
55 const hours = Math.floor(duration / 3600)
56 const minutes = Math.floor((duration % 3600) / 60)
57 const seconds = duration % 60
59 const minutesPadding = minutes >= 10 ? '' : '0'
60 const secondsPadding = seconds >= 10 ? '' : '0'
61 const displayedHours = hours > 0 ? hours.toString() + ':' : ''
63 return displayedHours + minutesPadding + minutes.toString() + ':' + secondsPadding + seconds.toString()
66 function immutableAssign <A, B> (target: A, source: B) {
67 return Object.assign({}, target, source)
70 function objectToUrlEncoded (obj: any) {
71 const str: string[] = []
72 for (const key of Object.keys(obj)) {
73 str.push(encodeURIComponent(key) + '=' + encodeURIComponent(obj[key]))
79 // Thanks: https://gist.github.com/ghinda/8442a57f22099bdb2e34
80 function objectToFormData (obj: any, form?: FormData, namespace?: string) {
81 const fd = form || new FormData()
84 for (const key of Object.keys(obj)) {
85 if (namespace) formKey = `${namespace}[${key}]`
88 if (obj[key] === undefined) continue
90 if (Array.isArray(obj[key]) && obj[key].length === 0) {
95 if (obj[key] !== null && typeof obj[ key ] === 'object' && !(obj[ key ] instanceof File)) {
96 objectToFormData(obj[ key ], fd, formKey)
98 fd.append(formKey, obj[ key ])
105 function objectLineFeedToHtml (obj: any, keyToNormalize: string) {
106 return immutableAssign(obj, {
107 [keyToNormalize]: lineFeedToHtml(obj[keyToNormalize])
111 function lineFeedToHtml (text: string) {
112 if (!text) return text
114 return text.replace(/\r?\n|\r/g, '<br />')
117 function removeElementFromArray <T> (arr: T[], elem: T) {
118 const index = arr.indexOf(elem)
119 if (index !== -1) arr.splice(index, 1)
122 function sortBy (obj: any[], key1: string, key2?: string) {
123 return obj.sort((a, b) => {
124 const elem1 = key2 ? a[key1][key2] : a[key1]
125 const elem2 = key2 ? b[key1][key2] : b[key1]
127 if (elem1 < elem2) return -1
128 if (elem1 === elem2) return 0
133 function scrollToTop () {
143 populateAsyncUserVideoChannels,
148 objectLineFeedToHtml,
149 removeElementFromArray,