]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/core/wrappers/screen.service.ts
Refactor video extensions logic in server
[github/Chocobozzz/PeerTube.git] / client / src / app / core / wrappers / screen.service.ts
CommitLineData
fc11a44e 1import { Injectable } from '@angular/core'
bbe0f064
C
2
3@Injectable()
4export class ScreenService {
5 private windowInnerWidth: number
fc11a44e
C
6 private lastFunctionCallTime: number
7 private cacheForMs = 500
bbe0f064 8
fc11a44e
C
9 constructor () {
10 this.refreshWindowInnerWidth()
bbe0f064
C
11 }
12
8544d8f5
K
13 isInSmallView (marginLeft = 0) {
14 if (marginLeft > 0) {
15 const contentWidth = this.getWindowInnerWidth() - marginLeft
16 return contentWidth < 800
17 }
18
8ff3f883 19 return this.getWindowInnerWidth() < 800
bbe0f064
C
20 }
21
3b20bdd6
RK
22 isInMediumView () {
23 return this.getWindowInnerWidth() < 1100
24 }
25
bbe0f064 26 isInMobileView () {
fc11a44e
C
27 return this.getWindowInnerWidth() < 500
28 }
29
8dfceec4
C
30 isInTouchScreen () {
31 return 'ontouchstart' in window || navigator.msMaxTouchPoints
32 }
33
6eb62c33
C
34 getNumberOfAvailableMiniatures () {
35 const screenWidth = this.getWindowInnerWidth()
36
37 let numberOfVideos = 1
38
cf78883c
C
39 if (screenWidth > 1850) numberOfVideos = 7
40 else if (screenWidth > 1600) numberOfVideos = 6
41 else if (screenWidth > 1370) numberOfVideos = 5
42 else if (screenWidth > 1100) numberOfVideos = 4
43 else if (screenWidth > 850) numberOfVideos = 3
6eb62c33
C
44
45 return numberOfVideos
46 }
47
fc11a44e 48 // Cache window inner width, because it's an expensive call
6eb62c33 49 getWindowInnerWidth () {
fc11a44e
C
50 if (this.cacheWindowInnerWidthExpired()) this.refreshWindowInnerWidth()
51
52 return this.windowInnerWidth
53 }
54
55 private refreshWindowInnerWidth () {
56 this.lastFunctionCallTime = new Date().getTime()
57
58 this.windowInnerWidth = window.innerWidth
59 }
60
61 private cacheWindowInnerWidthExpired () {
3a0fb65c
C
62 if (!this.lastFunctionCallTime) return true
63
fc11a44e 64 return new Date().getTime() > (this.lastFunctionCallTime + this.cacheForMs)
bbe0f064
C
65 }
66}