diff options
author | Nullptrderef <nullptrderef@proton.me> | 2024-03-31 16:11:52 +0200 |
---|---|---|
committer | Nullptrderef <nullptrderef@proton.me> | 2024-03-31 16:11:52 +0200 |
commit | 5eed5768ec3d5fd6a85d3c4662da586e058c4a53 (patch) | |
tree | 5e25c41491a4cf55244492e728e82906920326e4 | |
parent | 00c8cdc6a152cd0194eda49baf679e48e5a2a2eb (diff) | |
download | taler-ops-www-5eed5768ec3d5fd6a85d3c4662da586e058c4a53.tar.gz taler-ops-www-5eed5768ec3d5fd6a85d3c4662da586e058c4a53.tar.bz2 taler-ops-www-5eed5768ec3d5fd6a85d3c4662da586e058c4a53.zip |
proper OOP and platform detection and alldat
-rw-r--r-- | static/js/vendored/ua.ts | 231 |
1 files changed, 125 insertions, 106 deletions
diff --git a/static/js/vendored/ua.ts b/static/js/vendored/ua.ts index e26b919..45ca33d 100644 --- a/static/js/vendored/ua.ts +++ b/static/js/vendored/ua.ts @@ -1,189 +1,208 @@ -// Venodred Platform Detection +// 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 desktop: string = 'desktop'; - public static readonly tablet: string = 'tablet'; - public static readonly mobile: string = 'mobile'; + public static readonly Device = Device; + public static readonly OS = OS; + public static readonly Browser = Browser; - public static readonly android: string = 'android'; - public static readonly ios: string = 'ios'; - public static readonly linux: string = 'linux'; - public static readonly osx: string = 'osx'; - public static readonly windows: string = 'windows'; + public device = Device.Desktop; + public os = OS.Android; + public browser = Browser.Firefox; - public static readonly chrome: string = 'chrome'; - public static readonly firefox: string = 'firefox'; - public static readonly opera: string = 'opera'; - public static readonly edge: string = 'edge'; - public static readonly safari: string = 'safari'; - public static readonly explorer: string = 'explorer'; + public constructor(public userAgent = navigator.userAgent) { + this.update(userAgent); + } - public static device: string = Platform.mobile; - public static os: string = Platform.android; - public static browser: string = Platform.firefox; + public update(userAgent = navigator.userAgent) { + this.userAgent = userAgent; - public static initialize(): void { - Platform.setDevice(); - Platform.setOs(); - Platform.setBrowser(); + this.setDevice(); + this.setOS(); + this.setBrowser(); + } - if (Platform.device) document.body.classList.add(Platform.device); - if (Platform.os) document.body.classList.add(Platform.os); - if (Platform.browser) document.body.classList.add(Platform.browser); + /** 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 static compareDevice(device: string): boolean { - return Platform.device === device; + public compareDevice(device: string): boolean { + return this.device === device; } - public static compareOs(os: string): boolean { - return Platform.os === os; + public compareOs(os: string): boolean { + return this.os === os; } - public static compareBrowser(browser: string): boolean { - return Platform.browser === browser; + public compareBrowser(browser: string): boolean { + return this.browser === browser; } - public static isDesktop(): boolean { - return Platform.compareDevice(Platform.desktop); + public isDesktop(): boolean { + return this.compareDevice(Device.Desktop); } - public static isTablet(): boolean { - return Platform.compareDevice(Platform.tablet); + public isTablet(): boolean { + return this.compareDevice(Device.Tablet); } - public static isMobile(): boolean { - return Platform.compareDevice(Platform.mobile); + public isMobile(): boolean { + return this.compareDevice(Device.Mobile); } - public static detectDesktop(): boolean { - return !Platform.detectMobile() && !Platform.detectTablet(); + public detectDesktop(): boolean { + return !this.detectMobile() && !this.detectTablet(); } - public static detectTablet(): boolean { - return /tablet|ipad/i.test(navigator.userAgent); + public detectTablet(): boolean { + return /tablet|ipad/i.test(this.userAgent); } - public static detectMobile(): boolean { - return /mobile|iphone|ipod|android|windows *phone/i.test( - navigator.userAgent, - ); + public detectMobile(): boolean { + return /mobile|iphone|ipod|android|windows *phone/i.test(this.userAgent); } - public static detectAndroid(): boolean { - return /android/i.test(navigator.userAgent); + public detectAndroid(): boolean { + return /android/i.test(this.userAgent); } - public static detectIos(): boolean { - return /ipad|iphone|ipod/i.test(navigator.userAgent); + public detectIOS(): boolean { + return /ipad|iphone|ipod/i.test(this.userAgent); } - public static detectLinux(): boolean { - return /linux/i.test(navigator.userAgent); + public detectLinux(): boolean { + return /linux/i.test(this.userAgent); } - public static detectOsx(): boolean { - return /macintosh|os *x/i.test(navigator.userAgent); + public detectDarwin(): boolean { + return /macintosh|os *x/i.test(this.userAgent); } - public static detectWindows(): boolean { - return /windows|win64|win32/i.test(navigator.userAgent); + public detectWin32(): boolean { + return /windows|win64|win32/i.test(this.userAgent); } - public static detectChrome(): boolean { + public detectChrome(): boolean { return ( - /chrome/i.test(navigator.userAgent) && - !Platform.detectOpera() && - !Platform.detectEdge() + /chrome/i.test(this.userAgent) && + !this.detectOpera() && + !this.detectEdge() ); } - public static detectFirefox(): boolean { - return /firefox/i.test(navigator.userAgent); + public detectFirefox(): boolean { + return /firefox/i.test(this.userAgent); } - public static detectOpera(): boolean { - return /opr/i.test(navigator.userAgent); + public detectOpera(): boolean { + return /opr/i.test(this.userAgent); } - public static detectEdge(): boolean { - return /edge/i.test(navigator.userAgent); + public detectEdge(): boolean { + return /edge/i.test(this.userAgent); } - public static detectSafari(): boolean { + public detectSafari(): boolean { return ( - /safari/i.test(navigator.userAgent) && - !Platform.detectChrome() && - !Platform.detectOpera() && - !Platform.detectEdge() + /safari/i.test(this.userAgent) && + !this.detectChrome() && + !this.detectOpera() && + !this.detectEdge() ); } - public static detectExplorer(): boolean { - return /msie|trident/i.test(navigator.userAgent); + public detectExplorer(): boolean { + return /msie|trident/i.test(this.userAgent); } - public static setDevice(): void { + public setDevice(): void { switch (true) { - case Platform.detectTablet(): - Platform.device = Platform.tablet; + case this.detectTablet(): + this.device = Device.Tablet; break; - case Platform.detectMobile(): - Platform.device = Platform.mobile; + case this.detectMobile(): + this.device = Device.Mobile; break; - case Platform.detectDesktop(): - Platform.device = Platform.desktop; + case this.detectDesktop(): + this.device = Device.Desktop; break; default: - Platform.device = ''; + this.device = Device.Unknown; } } - public static setOs(): void { + public setOS(): void { switch (true) { - case Platform.detectAndroid(): - Platform.os = Platform.android; + case this.detectAndroid(): + this.os = OS.Android; break; - case Platform.detectIos(): - Platform.os = Platform.ios; + case this.detectIOS(): + this.os = OS.iOS; break; - case Platform.detectLinux(): - Platform.os = Platform.linux; + case this.detectLinux(): + this.os = OS.Linux; break; - case Platform.detectOsx(): - Platform.os = Platform.osx; + case this.detectDarwin(): + this.os = OS.Darwin; break; - case Platform.detectWindows(): - Platform.os = Platform.windows; + case this.detectWin32(): + this.os = OS.Win32; break; default: - Platform.os = ''; + this.os = OS.Unknown; } } - public static setBrowser(): void { + public setBrowser(): void { switch (true) { - case Platform.detectChrome(): - Platform.browser = Platform.chrome; + case this.detectChrome(): + this.browser = Browser.Chromium; break; - case Platform.detectFirefox(): - Platform.browser = Platform.firefox; + case this.detectFirefox(): + this.browser = Browser.Firefox; break; - case Platform.detectOpera(): - Platform.browser = Platform.opera; + case this.detectOpera(): + this.browser = Browser.Opera; break; - case Platform.detectEdge(): - Platform.browser = Platform.edge; + case this.detectEdge(): + this.browser = Browser.Edge; break; - case Platform.detectSafari(): - Platform.browser = Platform.safari; + case this.detectSafari(): + this.browser = Browser.Safari; break; - case Platform.detectExplorer(): - Platform.browser = Platform.explorer; + case this.detectExplorer(): + this.browser = Browser.IE; break; default: - Platform.browser = ''; + this.browser = Browser.Unknown; } } } |