summaryrefslogtreecommitdiff
path: root/static/js/vendored/ua.ts
blob: 45ca33de0fccf4de59c792d3126aa71d7d4c95c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Venodred Platform Detection using Enums & Proper OOP
// Upstream: https://github.com/leondejong/platform-detection

export enum Browser {
  Chrome = 'chrome',
  Chromium = 'chrome',
  Firefox = 'firefox',
  Opera = 'opera',
  Edge = 'edge',
  Safari = 'safari',
  IE = 'explorer',
  Unknown = '',
}
export enum OS {
  Android = 'android',
  iOS = 'ios',
  Linux = 'linux',
  Darwin = 'darwin',
  Win32 = 'win32',
  Unknown = '',
}
export enum Device {
  Desktop = 'desktop',
  Tablet = 'tablet',
  Mobile = 'mobile',
  Unknown = '',
}

export default class Platform {
  public static readonly Device = Device;
  public static readonly OS = OS;
  public static readonly Browser = Browser;

  public device = Device.Desktop;
  public os = OS.Android;
  public browser = Browser.Firefox;

  public constructor(public userAgent = navigator.userAgent) {
    this.update(userAgent);
  }

  public update(userAgent = navigator.userAgent) {
    this.userAgent = userAgent;

    this.setDevice();
    this.setOS();
    this.setBrowser();
  }

  /** Adds OS, Device and Browser classes to the body */
  public addClasses() {
    if (this.device) document.body.classList.add(this.device);
    if (this.os) document.body.classList.add(this.os);
    if (this.browser) document.body.classList.add(this.browser);
  }

  public compareDevice(device: string): boolean {
    return this.device === device;
  }

  public compareOs(os: string): boolean {
    return this.os === os;
  }

  public compareBrowser(browser: string): boolean {
    return this.browser === browser;
  }

  public isDesktop(): boolean {
    return this.compareDevice(Device.Desktop);
  }

  public isTablet(): boolean {
    return this.compareDevice(Device.Tablet);
  }

  public isMobile(): boolean {
    return this.compareDevice(Device.Mobile);
  }

  public detectDesktop(): boolean {
    return !this.detectMobile() && !this.detectTablet();
  }

  public detectTablet(): boolean {
    return /tablet|ipad/i.test(this.userAgent);
  }

  public detectMobile(): boolean {
    return /mobile|iphone|ipod|android|windows *phone/i.test(this.userAgent);
  }

  public detectAndroid(): boolean {
    return /android/i.test(this.userAgent);
  }

  public detectIOS(): boolean {
    return /ipad|iphone|ipod/i.test(this.userAgent);
  }

  public detectLinux(): boolean {
    return /linux/i.test(this.userAgent);
  }

  public detectDarwin(): boolean {
    return /macintosh|os *x/i.test(this.userAgent);
  }

  public detectWin32(): boolean {
    return /windows|win64|win32/i.test(this.userAgent);
  }

  public detectChrome(): boolean {
    return (
      /chrome/i.test(this.userAgent) &&
      !this.detectOpera() &&
      !this.detectEdge()
    );
  }

  public detectFirefox(): boolean {
    return /firefox/i.test(this.userAgent);
  }

  public detectOpera(): boolean {
    return /opr/i.test(this.userAgent);
  }

  public detectEdge(): boolean {
    return /edge/i.test(this.userAgent);
  }

  public detectSafari(): boolean {
    return (
      /safari/i.test(this.userAgent) &&
      !this.detectChrome() &&
      !this.detectOpera() &&
      !this.detectEdge()
    );
  }

  public detectExplorer(): boolean {
    return /msie|trident/i.test(this.userAgent);
  }

  public setDevice(): void {
    switch (true) {
      case this.detectTablet():
        this.device = Device.Tablet;
        break;
      case this.detectMobile():
        this.device = Device.Mobile;
        break;
      case this.detectDesktop():
        this.device = Device.Desktop;
        break;
      default:
        this.device = Device.Unknown;
    }
  }

  public setOS(): void {
    switch (true) {
      case this.detectAndroid():
        this.os = OS.Android;
        break;
      case this.detectIOS():
        this.os = OS.iOS;
        break;
      case this.detectLinux():
        this.os = OS.Linux;
        break;
      case this.detectDarwin():
        this.os = OS.Darwin;
        break;
      case this.detectWin32():
        this.os = OS.Win32;
        break;
      default:
        this.os = OS.Unknown;
    }
  }

  public setBrowser(): void {
    switch (true) {
      case this.detectChrome():
        this.browser = Browser.Chromium;
        break;
      case this.detectFirefox():
        this.browser = Browser.Firefox;
        break;
      case this.detectOpera():
        this.browser = Browser.Opera;
        break;
      case this.detectEdge():
        this.browser = Browser.Edge;
        break;
      case this.detectSafari():
        this.browser = Browser.Safari;
        break;
      case this.detectExplorer():
        this.browser = Browser.IE;
        break;
      default:
        this.browser = Browser.Unknown;
    }
  }
}