|
| 1 | +import { REQUEST } from '@nguniversal/express-engine/tokens' |
| 2 | +import { Inject, Injectable, PLATFORM_ID } from '@angular/core' |
| 3 | +import { UAParser } from 'ua-parser-js' |
| 4 | +import { Request } from 'express' |
| 5 | +import { isPlatformServer } from '@angular/common' |
| 6 | +import { WINDOW } from './tokens' |
| 7 | + |
| 8 | +export interface IUserAgentService { |
| 9 | + readonly userAgent: () => IUAParser.IResult |
| 10 | + readonly isiPhone: () => boolean |
| 11 | + readonly isiPad: () => boolean |
| 12 | + readonly isMobile: () => boolean |
| 13 | + readonly isTablet: () => boolean |
| 14 | + readonly isDesktop: () => boolean |
| 15 | + readonly isChrome: () => boolean |
| 16 | + readonly isFirefox: () => boolean |
| 17 | + readonly isSafari: () => boolean |
| 18 | + readonly isIE: () => boolean |
| 19 | + readonly isIE7: () => boolean |
| 20 | + readonly isIE8: () => boolean |
| 21 | + readonly isIE9: () => boolean |
| 22 | + readonly isIE10: () => boolean |
| 23 | + readonly isIE11: () => boolean |
| 24 | + readonly isWindows: () => boolean |
| 25 | + readonly isWindowsXP: () => boolean |
| 26 | + readonly isWindows7: () => boolean |
| 27 | + readonly isWindows8: () => boolean |
| 28 | + readonly isMac: () => boolean |
| 29 | + readonly isChromeOS: () => boolean |
| 30 | + readonly isiOS: () => boolean |
| 31 | + readonly isAndroid: () => boolean |
| 32 | +} |
| 33 | + |
| 34 | +// tslint:disable:no-class |
| 35 | +// tslint:disable:no-this |
| 36 | +@Injectable() |
| 37 | +export class UserAgentService implements IUserAgentService { |
| 38 | + constructor( |
| 39 | + @Inject(PLATFORM_ID) private platformId: any, |
| 40 | + @Inject(REQUEST) private req: Request, |
| 41 | + @Inject(WINDOW) private _window: Window |
| 42 | + ) {} |
| 43 | + |
| 44 | + public userAgent(): IUAParser.IResult { |
| 45 | + const ua = isPlatformServer(this.platformId) |
| 46 | + ? new UAParser(this.req.headers['user-agent'] as string | undefined) |
| 47 | + : new UAParser(this._window.navigator.userAgent) |
| 48 | + |
| 49 | + return ua.getResult() |
| 50 | + } |
| 51 | + |
| 52 | + isiPhone(): boolean { |
| 53 | + return this.userAgent().device.type === 'iPhone' |
| 54 | + } |
| 55 | + |
| 56 | + isiPad(): boolean { |
| 57 | + return this.userAgent().device.type === 'iPad' |
| 58 | + } |
| 59 | + |
| 60 | + isMobile(): boolean { |
| 61 | + return this.userAgent().device.type === 'mobile' |
| 62 | + } |
| 63 | + |
| 64 | + isTablet(): boolean { |
| 65 | + return this.userAgent().device.type === 'tablet' |
| 66 | + } |
| 67 | + |
| 68 | + isDesktop(): boolean { |
| 69 | + return !this.isTablet && !this.isMobile |
| 70 | + } |
| 71 | + |
| 72 | + isChrome(): boolean { |
| 73 | + return this.userAgent().browser.name === 'Chrome' |
| 74 | + } |
| 75 | + |
| 76 | + isFirefox(): boolean { |
| 77 | + return this.userAgent().browser.name === 'Firefox' |
| 78 | + } |
| 79 | + |
| 80 | + isSafari(): boolean { |
| 81 | + return this.userAgent().browser.name === 'Safari' |
| 82 | + } |
| 83 | + |
| 84 | + isIE(): boolean { |
| 85 | + return this.userAgent().browser.name === 'IE' |
| 86 | + } |
| 87 | + |
| 88 | + isIE7(): boolean { |
| 89 | + return this.isIE && this.userAgent().browser.major === '7' |
| 90 | + } |
| 91 | + |
| 92 | + isIE8(): boolean { |
| 93 | + return this.isIE && this.userAgent().browser.major === '8' |
| 94 | + } |
| 95 | + |
| 96 | + isIE9(): boolean { |
| 97 | + return this.isIE && this.userAgent().browser.major === '9' |
| 98 | + } |
| 99 | + |
| 100 | + isIE10(): boolean { |
| 101 | + return this.isIE && this.userAgent().browser.major === '10' |
| 102 | + } |
| 103 | + |
| 104 | + isIE11(): boolean { |
| 105 | + return this.isIE && this.userAgent().browser.major === '11' |
| 106 | + } |
| 107 | + |
| 108 | + isWindows(): boolean { |
| 109 | + return this.userAgent().os.name === 'Windows' |
| 110 | + } |
| 111 | + |
| 112 | + isWindowsXP(): boolean { |
| 113 | + return this.isWindows && this.userAgent().os.version === 'XP' |
| 114 | + } |
| 115 | + |
| 116 | + isWindows7(): boolean { |
| 117 | + return this.isWindows && this.userAgent().os.version === '7' |
| 118 | + } |
| 119 | + |
| 120 | + isWindows8(): boolean { |
| 121 | + return this.isWindows && this.userAgent().os.version === '8' |
| 122 | + } |
| 123 | + |
| 124 | + isMac(): boolean { |
| 125 | + return this.userAgent().os.name === 'Mac OS X' |
| 126 | + } |
| 127 | + |
| 128 | + isChromeOS(): boolean { |
| 129 | + return this.userAgent().os.name === 'Chromium OS' |
| 130 | + } |
| 131 | + |
| 132 | + isiOS(): boolean { |
| 133 | + return this.userAgent().os.name === 'iOS' |
| 134 | + } |
| 135 | + |
| 136 | + isAndroid(): boolean { |
| 137 | + return this.userAgent().os.name === 'Android' |
| 138 | + } |
| 139 | +} |
0 commit comments