b5bf99be2336a93ece30ba9e78c1a8fdb942a5cb
[oweals/peertube.git] / client / src / app / shared / misc / utils.ts
1 // Thanks: https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript
2
3 import { DatePipe } from '@angular/common'
4 import { environment } from '../../../environments/environment'
5 import { AuthService } from '../../core/auth'
6
7 function getParameterByName (name: string, url: string) {
8   if (!url) url = window.location.href
9   name = name.replace(/[\[\]]/g, '\\$&')
10
11   const regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)')
12   const results = regex.exec(url)
13
14   if (!results) return null
15   if (!results[2]) return ''
16
17   return decodeURIComponent(results[2].replace(/\+/g, ' '))
18 }
19
20 function populateAsyncUserVideoChannels (authService: AuthService, channel: any[]) {
21   return new Promise(res => {
22     authService.userInformationLoaded
23       .subscribe(
24         () => {
25           const user = authService.getUser()
26           if (!user) return
27
28           const videoChannels = user.videoChannels
29           if (Array.isArray(videoChannels) === false) return
30
31           videoChannels.forEach(c => channel.push({ id: c.id, label: c.displayName }))
32
33           return res()
34         }
35       )
36   })
37 }
38
39 function getAbsoluteAPIUrl () {
40   let absoluteAPIUrl = environment.apiUrl
41   if (!absoluteAPIUrl) {
42     // The API is on the same domain
43     absoluteAPIUrl = window.location.origin
44   }
45
46   return absoluteAPIUrl
47 }
48
49 const datePipe = new DatePipe('en')
50 function dateToHuman (date: string) {
51   return datePipe.transform(date, 'medium')
52 }
53
54 function immutableAssign <A, B> (target: A, source: B) {
55   return Object.assign({}, target, source)
56 }
57
58 // Thanks: https://gist.github.com/ghinda/8442a57f22099bdb2e34
59 function objectToFormData (obj: any, form?: FormData, namespace?: string) {
60   let fd = form || new FormData()
61   let formKey
62
63   for (let key of Object.keys(obj)) {
64     if (namespace) formKey = `${namespace}[${key}]`
65     else formKey = key
66
67     if (obj[key] === undefined) continue
68
69     if (obj[key] !== null && typeof obj[ key ] === 'object' && !(obj[ key ] instanceof File)) {
70       objectToFormData(obj[ key ], fd, key)
71     } else {
72       fd.append(formKey, obj[ key ])
73     }
74   }
75
76   return fd
77 }
78
79 function lineFeedToHtml (obj: object, keyToNormalize: string) {
80   return immutableAssign(obj, {
81     [keyToNormalize]: obj[keyToNormalize].replace(/\r?\n|\r/g, '<br />')
82   })
83 }
84
85 // Try to cache a little bit window.innerWidth
86 let windowInnerWidth = window.innerWidth
87 setInterval(() => windowInnerWidth = window.innerWidth, 500)
88
89 function isInSmallView () {
90   return windowInnerWidth < 600
91 }
92
93 function isInMobileView () {
94   return windowInnerWidth < 500
95 }
96
97 export {
98   getParameterByName,
99   populateAsyncUserVideoChannels,
100   getAbsoluteAPIUrl,
101   dateToHuman,
102   isInSmallView,
103   isInMobileView,
104   immutableAssign,
105   objectToFormData,
106   lineFeedToHtml
107 }