]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/misc/utils.ts
Throttle infinite scroller
[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
cd83ea1b
C
20function viewportHeight () {
21 return Math.max(document.documentElement.clientHeight, window.innerHeight || 0)
22}
23
15a7387d
C
24function populateAsyncUserVideoChannels (authService: AuthService, channel: any[]) {
25 return new Promise(res => {
26 authService.userInformationLoaded
27 .subscribe(
28 () => {
29 const user = authService.getUser()
30 if (!user) return
31
32 const videoChannels = user.videoChannels
33 if (Array.isArray(videoChannels) === false) return
34
60650c77 35 videoChannels.forEach(c => channel.push({ id: c.id, label: c.displayName }))
15a7387d
C
36
37 return res()
38 }
39 )
40 })
41}
42
c5911fd3
C
43function getAbsoluteAPIUrl () {
44 let absoluteAPIUrl = environment.apiUrl
45 if (!absoluteAPIUrl) {
46 // The API is on the same domain
47 absoluteAPIUrl = window.location.origin
48 }
49
50 return absoluteAPIUrl
51}
52
61bbc727
C
53const datePipe = new DatePipe('en')
54function dateToHuman (date: string) {
55 return datePipe.transform(date, 'medium')
56}
57
0cd4344f
C
58function immutableAssign <A, B> (target: A, source: B) {
59 return Object.assign({}, target, source)
60}
61
6de36768
C
62// Thanks: https://gist.github.com/ghinda/8442a57f22099bdb2e34
63function objectToFormData (obj: any, form?: FormData, namespace?: string) {
64 let fd = form || new FormData()
65 let formKey
66
67 for (let key of Object.keys(obj)) {
68 if (namespace) formKey = `${namespace}[${key}]`
69 else formKey = key
70
71 if (obj[key] === undefined) continue
72
73 if (typeof obj[ key ] === 'object' && !(obj[ key ] instanceof File)) {
74 objectToFormData(obj[ key ], fd, key)
75 } else {
76 fd.append(formKey, obj[ key ])
77 }
78 }
79
80 return fd
81}
82
5de8a55a
C
83function lineFeedToHtml (obj: object, keyToNormalize: string) {
84 return immutableAssign(obj, {
85 [keyToNormalize]: obj[keyToNormalize].replace(/\r?\n|\r/g, '<br />')
86 })
87}
88
a9ca764e
C
89// Try to cache a little bit window.innerWidth
90let windowInnerWidth = window.innerWidth
91setInterval(() => windowInnerWidth = window.innerWidth, 500)
92
93function isInSmallView () {
94 return windowInnerWidth < 600
95}
96
97function isInMobileView () {
98 return windowInnerWidth < 500
99}
100
f3aaa9a9 101export {
cd83ea1b 102 viewportHeight,
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}