1 import { Injectable } from '@angular/core'
4 export class ScreenService {
5 private windowInnerWidth: number
6 private lastFunctionCallTime: number
7 private cacheForMs = 500
10 this.refreshWindowInnerWidth()
13 isInSmallView (marginLeft = 0) {
15 const contentWidth = this.getWindowInnerWidth() - marginLeft
16 return contentWidth < 800
19 return this.getWindowInnerWidth() < 800
23 return this.getWindowInnerWidth() < 1100
27 return this.getWindowInnerWidth() < 500
31 return 'ontouchstart' in window || navigator.msMaxTouchPoints
34 getNumberOfAvailableMiniatures () {
35 const screenWidth = this.getWindowInnerWidth()
37 let numberOfVideos = 1
39 if (screenWidth > 1850) numberOfVideos = 6
40 else if (screenWidth > 1600) numberOfVideos = 5
41 else if (screenWidth > 1370) numberOfVideos = 4
42 else if (screenWidth > 1100) numberOfVideos = 3
43 else if (screenWidth > 850) numberOfVideos = 2
48 // Cache window inner width, because it's an expensive call
49 getWindowInnerWidth () {
50 if (this.cacheWindowInnerWidthExpired()) this.refreshWindowInnerWidth()
52 return this.windowInnerWidth
55 private refreshWindowInnerWidth () {
56 this.lastFunctionCallTime = new Date().getTime()
58 this.windowInnerWidth = window.innerWidth
61 private cacheWindowInnerWidthExpired () {
62 if (!this.lastFunctionCallTime) return true
64 return new Date().getTime() > (this.lastFunctionCallTime + this.cacheForMs)