]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/misc/utils.ts
Add ability to search available plugins
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / misc / utils.ts
CommitLineData
f3aaa9a9
C
1// Thanks: https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript
2
61bbc727 3import { DatePipe } from '@angular/common'
c5911fd3 4import { environment } from '../../../environments/environment'
15a7387d
C
5import { AuthService } from '../../core/auth'
6
f3aaa9a9
C
7function 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
830b4faf 20function 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
39function 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
49const datePipe = new DatePipe('en')
50function dateToHuman (date: string) {
51 return datePipe.transform(date, 'medium')
52}
53
11b8762f
C
54function durationToString (duration: number) {
55 const hours = Math.floor(duration / 3600)
56 const minutes = Math.floor((duration % 3600) / 60)
57 const seconds = duration % 60
58
59 const minutesPadding = minutes >= 10 ? '' : '0'
60 const secondsPadding = seconds >= 10 ? '' : '0'
61 const displayedHours = hours > 0 ? hours.toString() + ':' : ''
62
63 return displayedHours + minutesPadding + minutes.toString() + ':' + secondsPadding + seconds.toString()
64}
65
0cd4344f
C
66function immutableAssign <A, B> (target: A, source: B) {
67 return Object.assign({}, target, source)
68}
69
cd4d7a2c
C
70function objectToUrlEncoded (obj: any) {
71 const str: string[] = []
72 for (const key of Object.keys(obj)) {
73 str.push(encodeURIComponent(key) + '=' + encodeURIComponent(obj[key]))
74 }
75
76 return str.join('&')
77}
78
6de36768
C
79// Thanks: https://gist.github.com/ghinda/8442a57f22099bdb2e34
80function objectToFormData (obj: any, form?: FormData, namespace?: string) {
c4710631 81 const fd = form || new FormData()
6de36768
C
82 let formKey
83
c4710631 84 for (const key of Object.keys(obj)) {
6de36768
C
85 if (namespace) formKey = `${namespace}[${key}]`
86 else formKey = key
87
88 if (obj[key] === undefined) continue
89
2efd32f6
C
90 if (Array.isArray(obj[key]) && obj[key].length === 0) {
91 fd.append(key, null)
92 continue
93 }
94
360329cc 95 if (obj[key] !== null && typeof obj[ key ] === 'object' && !(obj[ key ] instanceof File)) {
40e87e9e 96 objectToFormData(obj[ key ], fd, formKey)
6de36768
C
97 } else {
98 fd.append(formKey, obj[ key ])
99 }
100 }
101
102 return fd
103}
104
1506307f 105function objectLineFeedToHtml (obj: any, keyToNormalize: string) {
5de8a55a 106 return immutableAssign(obj, {
1506307f 107 [keyToNormalize]: lineFeedToHtml(obj[keyToNormalize])
5de8a55a
C
108 })
109}
110
1506307f
C
111function lineFeedToHtml (text: string) {
112 if (!text) return text
113
114 return text.replace(/\r?\n|\r/g, '<br />')
115}
116
40e87e9e
C
117function removeElementFromArray <T> (arr: T[], elem: T) {
118 const index = arr.indexOf(elem)
119 if (index !== -1) arr.splice(index, 1)
120}
121
ad774752
C
122function 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]
126
127 if (elem1 < elem2) return -1
128 if (elem1 === elem2) return 0
129 return 1
130 })
131}
132
7373507f
C
133function scrollToTop () {
134 window.scroll(0, 0)
135}
136
f3aaa9a9 137export {
ad774752 138 sortBy,
11b8762f 139 durationToString,
1506307f 140 lineFeedToHtml,
cd4d7a2c 141 objectToUrlEncoded,
15a7387d 142 getParameterByName,
c5911fd3 143 populateAsyncUserVideoChannels,
61bbc727
C
144 getAbsoluteAPIUrl,
145 dateToHuman,
6de36768 146 immutableAssign,
5de8a55a 147 objectToFormData,
1506307f 148 objectLineFeedToHtml,
7373507f
C
149 removeElementFromArray,
150 scrollToTop
f3aaa9a9 151}