library-ts/browser/dom/UserAgentDeviceType.ts

122 lines
2.5 KiB
TypeScript

import { ElementAttribute } from "./ElementAttribute";
import { UserAgentInfo } from "./UserAgentInfo";
export type Android = "android";
export type Tablet = "tablet";
export type iPhone = "iphone";
export type iPad = "ipad";
export type Mac = "mac";
export type PC = "pc";
export type TV = "tv";
export type Other = "Other";
// export type (\w+)\s*=\s*".+;
// $1 |
export type UserAgentDeviceType =
Android |
Tablet |
iPhone |
iPad |
Mac |
PC |
TV |
Other
;
export class UserAgentDeviceTypes
{
// export type (\w+)\s*=\s*"(.+)".*;
// static readonly $1:$1 = "$2";
static readonly iPhone:iPhone = "iphone";
static readonly iPad:iPad = "ipad";
static readonly Android:Android = "android";
static readonly Tablet:Tablet = "tablet";
static readonly Mac:Mac = "mac";
static readonly PC:PC = "pc";
static readonly TV:TV = "tv";
static readonly Other:Other = "Other";
private static _deviceType:UserAgentDeviceType = null;
static get current()
{
if ( UserAgentDeviceTypes._deviceType !== null )
{
return UserAgentDeviceTypes._deviceType;
}
UserAgentDeviceTypes._deviceType = UserAgentDeviceTypes._guess();
return UserAgentDeviceTypes._deviceType;
}
static readonly attribute = new ElementAttribute( "device-type" );
static setOnBody()
{
UserAgentDeviceTypes.attribute.to( document.body, UserAgentDeviceTypes.current );
}
private static _guess():UserAgentDeviceType
{
if ( UserAgentInfo.isIPad )
{
return UserAgentDeviceTypes.iPad;
}
if ( UserAgentInfo.isIPhone )
{
return UserAgentDeviceTypes.iPhone;
}
if ( UserAgentInfo.isMac )
{
if ( UserAgentInfo.isTV )
{
return UserAgentDeviceTypes.TV;
}
return UserAgentDeviceTypes.Mac;
}
if ( UserAgentInfo.isAndroid )
{
if ( UserAgentInfo.isTV )
{
return UserAgentDeviceTypes.TV;
}
if ( UserAgentInfo.isTablet )
{
return UserAgentDeviceTypes.Tablet;
}
return UserAgentDeviceTypes.Android;
}
if ( UserAgentInfo.isTablet )
{
return UserAgentDeviceTypes.Tablet;
}
if ( UserAgentInfo.isTV )
{
return UserAgentDeviceTypes.TV;
}
if ( UserAgentInfo.isOther )
{
return UserAgentDeviceTypes.Other;
}
return UserAgentDeviceTypes.PC;
}
}