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 viewportHeight () {
21 return Math.max(document.documentElement.clientHeight, window.innerHeight || 0)
24 function populateAsyncUserVideoChannels (authService: AuthService, channel: any[]) {
25 return new Promise(res => {
26 authService.userInformationLoaded
29 const user = authService.getUser()
32 const videoChannels = user.videoChannels
33 if (Array.isArray(videoChannels) === false) return
35 videoChannels.forEach(c => channel.push({ id: c.id, label: c.displayName }))
43 function getAbsoluteAPIUrl () {
44 let absoluteAPIUrl = environment.apiUrl
45 if (!absoluteAPIUrl) {
46 // The API is on the same domain
47 absoluteAPIUrl = window.location.origin
53 const datePipe = new DatePipe('en')
54 function dateToHuman (date: string) {
55 return datePipe.transform(date, 'medium')
58 function immutableAssign <A, B> (target: A, source: B) {
59 return Object.assign({}, target, source)
62 // Thanks: https://gist.github.com/ghinda/8442a57f22099bdb2e34
63 function objectToFormData (obj: any, form?: FormData, namespace?: string) {
64 let fd = form || new FormData()
67 for (let key of Object.keys(obj)) {
68 if (namespace) formKey = `${namespace}[${key}]`
71 if (obj[key] === undefined) continue
73 if (typeof obj[ key ] === 'object' && !(obj[ key ] instanceof File)) {
74 objectToFormData(obj[ key ], fd, key)
76 fd.append(formKey, obj[ key ])
83 function lineFeedToHtml (obj: object, keyToNormalize: string) {
84 return immutableAssign(obj, {
85 [keyToNormalize]: obj[keyToNormalize].replace(/\r?\n|\r/g, '<br />')
89 // Try to cache a little bit window.innerWidth
90 let windowInnerWidth = window.innerWidth
91 setInterval(() => windowInnerWidth = window.innerWidth, 500)
93 function isInSmallView () {
94 return windowInnerWidth < 600
97 function isInMobileView () {
98 return windowInnerWidth < 500
104 populateAsyncUserVideoChannels,