@ngx-toolkit/device
Version:
Angular device user-agent detection with Universal support
252 lines (244 loc) • 6.45 kB
JavaScript
import * as i0 from '@angular/core';
import { InjectionToken, Optional, NgModule } from '@angular/core';
import { DOCUMENT } from '@angular/common';
var DeviceType;
(function (DeviceType) {
DeviceType[DeviceType["TABLET"] = 0] = "TABLET";
DeviceType[DeviceType["MOBILE"] = 1] = "MOBILE";
DeviceType[DeviceType["NORMAL"] = 2] = "NORMAL";
})(DeviceType || (DeviceType = {}));
var DevicePlatform;
(function (DevicePlatform) {
DevicePlatform[DevicePlatform["ANDROID"] = 0] = "ANDROID";
DevicePlatform[DevicePlatform["IOS"] = 1] = "IOS";
DevicePlatform[DevicePlatform["UNKNOWN"] = 2] = "UNKNOWN";
})(DevicePlatform || (DevicePlatform = {}));
class Device {
constructor(type = DeviceType.NORMAL, platform = DevicePlatform.UNKNOWN) {
this.type = type;
this.platform = platform;
}
isNormal() {
return this.type === DeviceType.NORMAL;
}
isMobile() {
return this.type === DeviceType.MOBILE;
}
isTablet() {
return this.type === DeviceType.TABLET;
}
}
const USER_AGENT = new InjectionToken('USER_AGENT');
const DEVICE = new InjectionToken('DEVICE');
/**
* see https://github.com/spring-projects/spring-mobile
*/
class DeviceService {
static resolveDevice(userAgent) {
if (!userAgent) {
return new Device(DeviceType.NORMAL, DevicePlatform.UNKNOWN);
}
userAgent = userAgent.toLowerCase();
/**
* Tablet Detection
*/
// Apple
if (userAgent.includes('ipad')) {
return new Device(DeviceType.TABLET, DevicePlatform.IOS);
}
const isMobile = userAgent.includes('mobile') ||
DeviceService.KNOWN_MOBILE_USER_AGENT_KEYWORDS.some(mobileUserAgent => userAgent.includes(mobileUserAgent));
if (!isMobile) {
// Android
if (userAgent.includes('android')) {
return new Device(DeviceType.TABLET, DevicePlatform.ANDROID);
}
// Kindle Fire
if (userAgent.includes('silk')) {
return new Device(DeviceType.TABLET, DevicePlatform.UNKNOWN);
}
}
// From keywords
if (DeviceService.KNOWN_TABLET_USER_AGENT_KEYWORDS.some(tabletUserAgent => userAgent.includes(tabletUserAgent))) {
return new Device(DeviceType.TABLET, DevicePlatform.UNKNOWN);
}
/**
* Mobile detection
*/
// From prefix
if (userAgent.length >= 4 &&
DeviceService.KNOWN_MOBILE_USER_AGENT_PREFIXES.indexOf(userAgent.substring(0, 4)) !== -1) {
return new Device(DeviceType.MOBILE, DevicePlatform.UNKNOWN);
}
// Android
if (userAgent.includes('android')) {
return new Device(DeviceType.MOBILE, DevicePlatform.ANDROID);
}
// Apple
if (userAgent.includes('iphone') || userAgent.includes('ipod') || userAgent.includes('ipad')) {
return new Device(DeviceType.MOBILE, DevicePlatform.IOS);
}
// From keywords
if (isMobile) {
return new Device(DeviceType.MOBILE, DevicePlatform.UNKNOWN);
}
/**
* => Normal device
*/
return new Device(DeviceType.NORMAL, DevicePlatform.UNKNOWN);
}
}
DeviceService.KNOWN_MOBILE_USER_AGENT_PREFIXES = [
'w3c ',
'w3c-',
'acs-',
'alav',
'alca',
'amoi',
'avan',
'benq',
'bird',
'blac',
'blaz',
'brew',
'cell',
'cldc',
'cmd-',
'dang',
'doco',
'eric',
'hipt',
'htc_',
'inno',
'ipaq',
'ipod',
'jigs',
'kddi',
'keji',
'leno',
'lg-c',
'lg-d',
'lg-g',
'lge-',
'lg/u',
'maui',
'maxo',
'midp',
'mits',
'mmef',
'mobi',
'mot-',
'moto',
'mwbp',
'nec-',
'newt',
'noki',
'palm',
'pana',
'pant',
'phil',
'play',
'port',
'prox',
'qwap',
'sage',
'sams',
'sany',
'sch-',
'sec-',
'send',
'seri',
'sgh-',
'shar',
'sie-',
'siem',
'smal',
'smar',
'sony',
'sph-',
'symb',
't-mo',
'teli',
'tim-',
'tosh',
'tsm-',
'upg1',
'upsi',
'vk-v',
'voda',
'wap-',
'wapa',
'wapi',
'wapp',
'wapr',
'webc',
'winw',
'winw',
'xda ',
'xda-'
];
DeviceService.KNOWN_MOBILE_USER_AGENT_KEYWORDS = [
'blackberry',
'webos',
'ipod',
'lge vx',
'midp',
'maemo',
'mmp',
'mobile',
'netfront',
'hiptop',
'nintendo DS',
'novarra',
'openweb',
'opera mobi',
'opera mini',
'palm',
'psp',
'phone',
'smartphone',
'symbian',
'up.browser',
'up.link',
'wap',
'windows ce'
];
DeviceService.KNOWN_TABLET_USER_AGENT_KEYWORDS = ['ipad', 'playbook', 'hp-tablet', 'kindle'];
function deviceResolverFactory(userAgent, document) {
var _a, _b;
if (!userAgent && document) {
userAgent = (_b = (_a = document.defaultView) === null || _a === void 0 ? void 0 : _a.navigator) === null || _b === void 0 ? void 0 : _b.userAgent;
}
return DeviceService.resolveDevice(userAgent);
}
class DeviceModule {
/**
* In root module to provide the DEVICE
*/
static forRoot() {
return {
ngModule: DeviceModule,
providers: [
{
provide: DEVICE,
useFactory: deviceResolverFactory,
deps: [[new Optional(), USER_AGENT], [new Optional(), DOCUMENT]]
}
]
};
}
}
DeviceModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.2.0", ngImport: i0, type: DeviceModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
DeviceModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "13.2.0", ngImport: i0, type: DeviceModule });
DeviceModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "13.2.0", ngImport: i0, type: DeviceModule });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.2.0", ngImport: i0, type: DeviceModule, decorators: [{
type: NgModule
}] });
/*
* Public API Surface of device
*/
/**
* Generated bundle index. Do not edit.
*/
export { DEVICE, Device, DeviceModule, DevicePlatform, DeviceType, USER_AGENT };
//# sourceMappingURL=ngx-toolkit-device.mjs.map