]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/misc/utils.ts
Fix updating video tags to empty field
[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
15a7387d
C
20function 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
60650c77 31 videoChannels.forEach(c => channel.push({ id: c.id, label: c.displayName }))
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
0cd4344f
C
54function immutableAssign <A, B> (target: A, source: B) {
55 return Object.assign({}, target, source)
56}
57
6de36768
C
58// Thanks: https://gist.github.com/ghinda/8442a57f22099bdb2e34
59function 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
2efd32f6
C
69 if (Array.isArray(obj[key]) && obj[key].length === 0) {
70 fd.append(key, null)
71 continue
72 }
73
360329cc 74 if (obj[key] !== null && typeof obj[ key ] === 'object' && !(obj[ key ] instanceof File)) {
6de36768
C
75 objectToFormData(obj[ key ], fd, key)
76 } else {
77 fd.append(formKey, obj[ key ])
78 }
79 }
80
81 return fd
82}
83
5de8a55a
C
84function lineFeedToHtml (obj: object, keyToNormalize: string) {
85 return immutableAssign(obj, {
86 [keyToNormalize]: obj[keyToNormalize].replace(/\r?\n|\r/g, '<br />')
87 })
88}
89
a9ca764e
C
90// Try to cache a little bit window.innerWidth
91let windowInnerWidth = window.innerWidth
92setInterval(() => windowInnerWidth = window.innerWidth, 500)
93
94function isInSmallView () {
95 return windowInnerWidth < 600
96}
97
98function isInMobileView () {
99 return windowInnerWidth < 500
100}
101
f3aaa9a9 102export {
15a7387d 103 getParameterByName,
c5911fd3 104 populateAsyncUserVideoChannels,
61bbc727
C
105 getAbsoluteAPIUrl,
106 dateToHuman,
3290f37c 107 isInSmallView,
0cd4344f 108 isInMobileView,
6de36768 109 immutableAssign,
5de8a55a
C
110 objectToFormData,
111 lineFeedToHtml
f3aaa9a9 112}