1 import { DatePipe } from '@angular/common'
2 import { environment } from '../../environments/environment'
3 import { AuthService } from '../core/auth'
5 // Thanks: https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript
6 function getParameterByName (name: string, url: string) {
7 if (!url) url = window.location.href
8 name = name.replace(/[\[\]]/g, '\\$&')
10 const regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)')
11 const results = regex.exec(url)
13 if (!results) return null
14 if (!results[2]) return ''
16 return decodeURIComponent(results[2].replace(/\+/g, ' '))
19 function populateAsyncUserVideoChannels (
20 authService: AuthService,
21 channel: { id: number, label: string, support?: string, avatarPath?: string, recent?: boolean }[]
23 return new Promise(res => {
24 authService.userInformationLoaded
27 const user = authService.getUser()
30 const videoChannels = user.videoChannels
31 if (Array.isArray(videoChannels) === false) return
33 videoChannels.forEach(c => channel.push({
37 avatarPath: c.avatar?.path
46 function getAbsoluteAPIUrl () {
47 let absoluteAPIUrl = environment.hmr === true
48 ? 'http://localhost:9000'
51 if (!absoluteAPIUrl) {
52 // The API is on the same domain
53 absoluteAPIUrl = window.location.origin
59 function getAbsoluteEmbedUrl () {
60 let absoluteEmbedUrl = environment.embedUrl
61 if (!absoluteEmbedUrl) {
62 // The Embed is on the same domain
63 absoluteEmbedUrl = window.location.origin
66 return absoluteEmbedUrl
69 const datePipe = new DatePipe('en')
70 function dateToHuman (date: string) {
71 return datePipe.transform(date, 'medium')
74 function durationToString (duration: number) {
75 const hours = Math.floor(duration / 3600)
76 const minutes = Math.floor((duration % 3600) / 60)
77 const seconds = duration % 60
79 const minutesPadding = minutes >= 10 ? '' : '0'
80 const secondsPadding = seconds >= 10 ? '' : '0'
81 const displayedHours = hours > 0 ? hours.toString() + ':' : ''
84 displayedHours + minutesPadding + minutes.toString() + ':' + secondsPadding + seconds.toString()
88 function immutableAssign <A, B> (target: A, source: B) {
89 return Object.assign({}, target, source)
92 // Thanks: https://gist.github.com/ghinda/8442a57f22099bdb2e34
93 function objectToFormData (obj: any, form?: FormData, namespace?: string) {
94 const fd = form || new FormData()
97 for (const key of Object.keys(obj)) {
98 if (namespace) formKey = `${namespace}[${key}]`
101 if (obj[key] === undefined) continue
103 if (Array.isArray(obj[key]) && obj[key].length === 0) {
108 if (obj[key] !== null && typeof obj[ key ] === 'object' && !(obj[ key ] instanceof File)) {
109 objectToFormData(obj[ key ], fd, formKey)
111 fd.append(formKey, obj[ key ])
118 function objectLineFeedToHtml (obj: any, keyToNormalize: string) {
119 return immutableAssign(obj, {
120 [keyToNormalize]: lineFeedToHtml(obj[keyToNormalize])
124 function lineFeedToHtml (text: string) {
125 if (!text) return text
127 return text.replace(/\r?\n|\r/g, '<br />')
130 function removeElementFromArray <T> (arr: T[], elem: T) {
131 const index = arr.indexOf(elem)
132 if (index !== -1) arr.splice(index, 1)
135 function sortBy (obj: any[], key1: string, key2?: string) {
136 return obj.sort((a, b) => {
137 const elem1 = key2 ? a[key1][key2] : a[key1]
138 const elem2 = key2 ? b[key1][key2] : b[key1]
140 if (elem1 < elem2) return -1
141 if (elem1 === elem2) return 0
146 function scrollToTop () {
150 // Thanks: https://github.com/uupaa/dynamic-import-polyfill
151 function importModule (path: string) {
152 return new Promise((resolve, reject) => {
153 const vector = '$importModule$' + Math.random().toString(32).slice(2)
154 const script = document.createElement('script')
156 const destructor = () => {
157 delete window[ vector ]
158 script.onerror = null
161 URL.revokeObjectURL(script.src)
166 script.type = 'module'
168 script.onerror = () => {
169 reject(new Error(`Failed to import: ${path}`))
172 script.onload = () => {
173 resolve(window[ vector ])
176 const absURL = (environment.apiUrl || window.location.origin) + path
177 const loader = `import * as m from "${absURL}"; window.${vector} = m;` // export Module
178 const blob = new Blob([ loader ], { type: 'text/javascript' })
179 script.src = URL.createObjectURL(blob)
181 document.head.appendChild(script)
185 function isInViewport (el: HTMLElement) {
186 const bounding = el.getBoundingClientRect()
189 bounding.left >= 0 &&
190 bounding.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
191 bounding.right <= (window.innerWidth || document.documentElement.clientWidth)
195 function isXPercentInViewport (el: HTMLElement, percentVisible: number) {
196 const rect = el.getBoundingClientRect()
197 const windowHeight = (window.innerHeight || document.documentElement.clientHeight)
200 Math.floor(100 - (((rect.top >= 0 ? 0 : rect.top) / +-(rect.height / 1)) * 100)) < percentVisible ||
201 Math.floor(100 - ((rect.bottom - windowHeight) / rect.height) * 100) < percentVisible
210 populateAsyncUserVideoChannels,
216 objectLineFeedToHtml,
217 removeElementFromArray,