]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/misc/utils.ts
6620ac9737f5fe5678c125d32cd6d36d26b4811f
[github/Chocobozzz/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 viewportHeight () {
21 return Math.max(document.documentElement.clientHeight, window.innerHeight || 0)
22 }
23
24 function 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
35 videoChannels.forEach(c => channel.push({ id: c.id, label: c.displayName }))
36
37 return res()
38 }
39 )
40 })
41 }
42
43 function 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
53 const datePipe = new DatePipe('en')
54 function dateToHuman (date: string) {
55 return datePipe.transform(date, 'medium')
56 }
57
58 function isInSmallView () {
59 return window.innerWidth < 600
60 }
61
62 function isInMobileView () {
63 return window.innerWidth < 500
64 }
65
66 export {
67 viewportHeight,
68 getParameterByName,
69 populateAsyncUserVideoChannels,
70 getAbsoluteAPIUrl,
71 dateToHuman,
72 isInSmallView,
73 isInMobileView
74 }