]>
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 | ||
17 | isInMobileView () { | |
fc11a44e C |
18 | return this.getWindowInnerWidth() < 500 |
19 | } | |
20 | ||
21 | // Cache window inner width, because it's an expensive call | |
22 | private getWindowInnerWidth () { | |
23 | if (this.cacheWindowInnerWidthExpired()) this.refreshWindowInnerWidth() | |
24 | ||
25 | return this.windowInnerWidth | |
26 | } | |
27 | ||
28 | private refreshWindowInnerWidth () { | |
29 | this.lastFunctionCallTime = new Date().getTime() | |
30 | ||
31 | this.windowInnerWidth = window.innerWidth | |
32 | } | |
33 | ||
34 | private cacheWindowInnerWidthExpired () { | |
35 | return new Date().getTime() > (this.lastFunctionCallTime + this.cacheForMs) | |
bbe0f064 C |
36 | } |
37 | } |