diff options
-rw-r--r-- | client/src/app/shared/misc/screen.service.ts | 34 |
1 files changed, 24 insertions, 10 deletions
diff --git a/client/src/app/shared/misc/screen.service.ts b/client/src/app/shared/misc/screen.service.ts index 5b17a914a..2e01839b2 100644 --- a/client/src/app/shared/misc/screen.service.ts +++ b/client/src/app/shared/misc/screen.service.ts | |||
@@ -1,23 +1,37 @@ | |||
1 | import { Injectable, NgZone } from '@angular/core' | 1 | import { Injectable } from '@angular/core' |
2 | 2 | ||
3 | @Injectable() | 3 | @Injectable() |
4 | export class ScreenService { | 4 | export class ScreenService { |
5 | private windowInnerWidth: number | 5 | private windowInnerWidth: number |
6 | private lastFunctionCallTime: number | ||
7 | private cacheForMs = 500 | ||
6 | 8 | ||
7 | constructor (private zone: NgZone) { | 9 | constructor () { |
8 | this.windowInnerWidth = window.innerWidth | 10 | this.refreshWindowInnerWidth() |
9 | |||
10 | // Try to cache a little bit window.innerWidth | ||
11 | this.zone.runOutsideAngular(() => { | ||
12 | setInterval(() => this.windowInnerWidth = window.innerWidth, 500) | ||
13 | }) | ||
14 | } | 11 | } |
15 | 12 | ||
16 | isInSmallView () { | 13 | isInSmallView () { |
17 | return this.windowInnerWidth < 600 | 14 | return this.getWindowInnerWidth() < 600 |
18 | } | 15 | } |
19 | 16 | ||
20 | isInMobileView () { | 17 | isInMobileView () { |
21 | return this.windowInnerWidth < 500 | 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) | ||
22 | } | 36 | } |
23 | } | 37 | } |