]>
Commit | Line | Data |
---|---|---|
1 | import { Injectable } from '@angular/core' | |
2 | ||
3 | @Injectable() | |
4 | export class ScreenService { | |
5 | isBroadcastMessageDisplayed = false | |
6 | ||
7 | private windowInnerWidth: number | |
8 | private lastFunctionCallTime: number | |
9 | private cacheForMs = 500 | |
10 | ||
11 | constructor () { | |
12 | this.refreshWindowInnerWidth() | |
13 | } | |
14 | ||
15 | isInSmallView (marginLeft = 0) { | |
16 | if (marginLeft > 0) { | |
17 | const contentWidth = this.getWindowInnerWidth() - marginLeft | |
18 | return contentWidth < 800 | |
19 | } | |
20 | ||
21 | return this.getWindowInnerWidth() < 800 | |
22 | } | |
23 | ||
24 | isInMediumView () { | |
25 | return this.getWindowInnerWidth() < 1100 | |
26 | } | |
27 | ||
28 | isInMobileView () { | |
29 | return this.getWindowInnerWidth() < 500 | |
30 | } | |
31 | ||
32 | isInTouchScreen () { | |
33 | return !!('ontouchstart' in window || navigator.msMaxTouchPoints) | |
34 | } | |
35 | ||
36 | getNumberOfAvailableMiniatures () { | |
37 | const screenWidth = this.getWindowInnerWidth() | |
38 | ||
39 | let numberOfVideos = 1 | |
40 | ||
41 | if (screenWidth > 1850) numberOfVideos = 7 | |
42 | else if (screenWidth > 1600) numberOfVideos = 6 | |
43 | else if (screenWidth > 1370) numberOfVideos = 5 | |
44 | else if (screenWidth > 1100) numberOfVideos = 4 | |
45 | else if (screenWidth > 850) numberOfVideos = 3 | |
46 | ||
47 | return numberOfVideos | |
48 | } | |
49 | ||
50 | // Cache window inner width, because it's an expensive call | |
51 | getWindowInnerWidth () { | |
52 | if (this.cacheWindowInnerWidthExpired()) this.refreshWindowInnerWidth() | |
53 | ||
54 | return this.windowInnerWidth | |
55 | } | |
56 | ||
57 | // https://stackoverflow.com/questions/2264072/detect-a-finger-swipe-through-javascript-on-the-iphone-and-android | |
58 | onFingerSwipe (direction: 'left' | 'right' | 'up' | 'down', action: () => void, removeEventOnEnd = true) { | |
59 | let touchDownClientX: number | |
60 | let touchDownClientY: number | |
61 | ||
62 | const onTouchStart = (event: TouchEvent) => { | |
63 | const firstTouch = event.touches[0] | |
64 | touchDownClientX = firstTouch.clientX | |
65 | touchDownClientY = firstTouch.clientY | |
66 | } | |
67 | ||
68 | const onTouchMove = (event: TouchEvent) => { | |
69 | if (!touchDownClientX || !touchDownClientY) { | |
70 | return | |
71 | } | |
72 | ||
73 | const touchUpClientX = event.touches[0].clientX | |
74 | const touchUpClientY = event.touches[0].clientY | |
75 | ||
76 | const touchClientX = Math.abs(touchDownClientX - touchUpClientX) | |
77 | const touchClientY = Math.abs(touchDownClientY - touchUpClientY) | |
78 | ||
79 | if (touchClientX > touchClientY) { | |
80 | if (touchClientX > 0) { | |
81 | if (direction === 'left') { | |
82 | if (removeEventOnEnd) this.removeFingerSwipeEventListener(onTouchStart, onTouchMove) | |
83 | action() | |
84 | } | |
85 | } else { | |
86 | if (direction === 'right') { | |
87 | if (removeEventOnEnd) this.removeFingerSwipeEventListener(onTouchStart, onTouchMove) | |
88 | action() | |
89 | } | |
90 | } | |
91 | } else { | |
92 | if (touchClientY > 0) { | |
93 | if (direction === 'up') { | |
94 | if (removeEventOnEnd) this.removeFingerSwipeEventListener(onTouchStart, onTouchMove) | |
95 | action() | |
96 | } | |
97 | } else { | |
98 | if (direction === 'down') { | |
99 | if (removeEventOnEnd) this.removeFingerSwipeEventListener(onTouchStart, onTouchMove) | |
100 | action() | |
101 | } | |
102 | } | |
103 | } | |
104 | } | |
105 | ||
106 | document.addEventListener('touchstart', onTouchStart, false) | |
107 | document.addEventListener('touchmove', onTouchMove, false) | |
108 | } | |
109 | ||
110 | private removeFingerSwipeEventListener (onTouchStart: (event: TouchEvent) => void, onTouchMove: (event: TouchEvent) => void) { | |
111 | document.removeEventListener('touchstart', onTouchStart) | |
112 | document.removeEventListener('touchmove', onTouchMove) | |
113 | } | |
114 | ||
115 | private refreshWindowInnerWidth () { | |
116 | this.lastFunctionCallTime = new Date().getTime() | |
117 | ||
118 | this.windowInnerWidth = window.innerWidth | |
119 | } | |
120 | ||
121 | private cacheWindowInnerWidthExpired () { | |
122 | if (!this.lastFunctionCallTime) return true | |
123 | ||
124 | return new Date().getTime() > (this.lastFunctionCallTime + this.cacheForMs) | |
125 | } | |
126 | } |