]>
Commit | Line | Data |
---|---|---|
fc11a44e | 1 | import { Injectable } from '@angular/core' |
bbe0f064 C |
2 | |
3 | @Injectable() | |
4 | export 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 | ||
13 | isInSmallView () { | |
8ff3f883 | 14 | return this.getWindowInnerWidth() < 800 |
bbe0f064 C |
15 | } |
16 | ||
3b20bdd6 RK |
17 | isInMediumView () { |
18 | return this.getWindowInnerWidth() < 1100 | |
19 | } | |
20 | ||
bbe0f064 | 21 | isInMobileView () { |
fc11a44e C |
22 | return this.getWindowInnerWidth() < 500 |
23 | } | |
24 | ||
8dfceec4 C |
25 | isInTouchScreen () { |
26 | return 'ontouchstart' in window || navigator.msMaxTouchPoints | |
27 | } | |
28 | ||
6eb62c33 C |
29 | getNumberOfAvailableMiniatures () { |
30 | const screenWidth = this.getWindowInnerWidth() | |
31 | ||
32 | let numberOfVideos = 1 | |
33 | ||
34 | if (screenWidth > 1850) numberOfVideos = 6 | |
35 | else if (screenWidth > 1600) numberOfVideos = 5 | |
36 | else if (screenWidth > 1370) numberOfVideos = 4 | |
37 | else if (screenWidth > 1100) numberOfVideos = 3 | |
38 | else if (screenWidth > 850) numberOfVideos = 2 | |
39 | ||
40 | return numberOfVideos | |
41 | } | |
42 | ||
fc11a44e | 43 | // Cache window inner width, because it's an expensive call |
6eb62c33 | 44 | getWindowInnerWidth () { |
fc11a44e C |
45 | if (this.cacheWindowInnerWidthExpired()) this.refreshWindowInnerWidth() |
46 | ||
47 | return this.windowInnerWidth | |
48 | } | |
49 | ||
50 | private refreshWindowInnerWidth () { | |
51 | this.lastFunctionCallTime = new Date().getTime() | |
52 | ||
53 | this.windowInnerWidth = window.innerWidth | |
54 | } | |
55 | ||
56 | private cacheWindowInnerWidthExpired () { | |
3a0fb65c C |
57 | if (!this.lastFunctionCallTime) return true |
58 | ||
fc11a44e | 59 | return new Date().getTime() > (this.lastFunctionCallTime + this.cacheForMs) |
bbe0f064 C |
60 | } |
61 | } |