]>
Commit | Line | Data |
---|---|---|
f3aaa9a9 C |
1 | // Thanks: https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript |
2 | ||
61bbc727 | 3 | import { DatePipe } from '@angular/common' |
c5911fd3 | 4 | import { environment } from '../../../environments/environment' |
15a7387d C |
5 | import { AuthService } from '../../core/auth' |
6 | ||
f3aaa9a9 C |
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 | ||
74af5145 | 20 | function populateAsyncUserVideoChannels (authService: AuthService, channel: { id: number, label: string, support: string }[]) { |
15a7387d C |
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 | ||
74af5145 | 31 | videoChannels.forEach(c => channel.push({ id: c.id, label: c.displayName, support: c.support })) |
15a7387d C |
32 | |
33 | return res() | |
34 | } | |
35 | ) | |
36 | }) | |
37 | } | |
38 | ||
c5911fd3 C |
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 | ||
61bbc727 C |
49 | const datePipe = new DatePipe('en') |
50 | function dateToHuman (date: string) { | |
51 | return datePipe.transform(date, 'medium') | |
52 | } | |
53 | ||
0cd4344f C |
54 | function immutableAssign <A, B> (target: A, source: B) { |
55 | return Object.assign({}, target, source) | |
56 | } | |
57 | ||
cd4d7a2c C |
58 | function objectToUrlEncoded (obj: any) { |
59 | const str: string[] = [] | |
60 | for (const key of Object.keys(obj)) { | |
61 | str.push(encodeURIComponent(key) + '=' + encodeURIComponent(obj[key])) | |
62 | } | |
63 | ||
64 | return str.join('&') | |
65 | } | |
66 | ||
6de36768 C |
67 | // Thanks: https://gist.github.com/ghinda/8442a57f22099bdb2e34 |
68 | function objectToFormData (obj: any, form?: FormData, namespace?: string) { | |
69 | let fd = form || new FormData() | |
70 | let formKey | |
71 | ||
72 | for (let key of Object.keys(obj)) { | |
73 | if (namespace) formKey = `${namespace}[${key}]` | |
74 | else formKey = key | |
75 | ||
76 | if (obj[key] === undefined) continue | |
77 | ||
2efd32f6 C |
78 | if (Array.isArray(obj[key]) && obj[key].length === 0) { |
79 | fd.append(key, null) | |
80 | continue | |
81 | } | |
82 | ||
360329cc | 83 | if (obj[key] !== null && typeof obj[ key ] === 'object' && !(obj[ key ] instanceof File)) { |
40e87e9e | 84 | objectToFormData(obj[ key ], fd, formKey) |
6de36768 C |
85 | } else { |
86 | fd.append(formKey, obj[ key ]) | |
87 | } | |
88 | } | |
89 | ||
90 | return fd | |
91 | } | |
92 | ||
5de8a55a C |
93 | function lineFeedToHtml (obj: object, keyToNormalize: string) { |
94 | return immutableAssign(obj, { | |
95 | [keyToNormalize]: obj[keyToNormalize].replace(/\r?\n|\r/g, '<br />') | |
96 | }) | |
97 | } | |
98 | ||
40e87e9e C |
99 | function removeElementFromArray <T> (arr: T[], elem: T) { |
100 | const index = arr.indexOf(elem) | |
101 | if (index !== -1) arr.splice(index, 1) | |
102 | } | |
103 | ||
ad774752 C |
104 | function sortBy (obj: any[], key1: string, key2?: string) { |
105 | return obj.sort((a, b) => { | |
106 | const elem1 = key2 ? a[key1][key2] : a[key1] | |
107 | const elem2 = key2 ? b[key1][key2] : b[key1] | |
108 | ||
109 | if (elem1 < elem2) return -1 | |
110 | if (elem1 === elem2) return 0 | |
111 | return 1 | |
112 | }) | |
113 | } | |
114 | ||
f3aaa9a9 | 115 | export { |
ad774752 | 116 | sortBy, |
cd4d7a2c | 117 | objectToUrlEncoded, |
15a7387d | 118 | getParameterByName, |
c5911fd3 | 119 | populateAsyncUserVideoChannels, |
61bbc727 C |
120 | getAbsoluteAPIUrl, |
121 | dateToHuman, | |
6de36768 | 122 | immutableAssign, |
5de8a55a | 123 | objectToFormData, |
40e87e9e C |
124 | lineFeedToHtml, |
125 | removeElementFromArray | |
f3aaa9a9 | 126 | } |