mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2026-03-13 10:22:08 +08:00
fix(di): update dependency injection and default configs
This commit is contained in:
234
src/platform/platform-registry.ts
Normal file
234
src/platform/platform-registry.ts
Normal file
@@ -0,0 +1,234 @@
|
||||
import { OpaqueToken } from '@angular/core';
|
||||
|
||||
import { Platform, PlatformConfig } from './platform';
|
||||
import { windowLoad } from '../util/dom';
|
||||
|
||||
|
||||
export const PLATFORM_CONFIGS: {[key: string]: PlatformConfig} = {
|
||||
|
||||
/**
|
||||
* core
|
||||
*/
|
||||
core: {
|
||||
settings: {
|
||||
mode: 'md',
|
||||
keyboardHeight: 290
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* mobile
|
||||
*/
|
||||
mobile: {},
|
||||
|
||||
/**
|
||||
* phablet
|
||||
*/
|
||||
phablet: {
|
||||
isMatch(p: Platform) {
|
||||
let smallest = Math.min(p.width(), p.height());
|
||||
let largest = Math.max(p.width(), p.height());
|
||||
return (smallest > 390 && smallest < 520) &&
|
||||
(largest > 620 && largest < 800);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* tablet
|
||||
*/
|
||||
tablet: {
|
||||
isMatch(p: Platform) {
|
||||
let smallest = Math.min(p.width(), p.height());
|
||||
let largest = Math.max(p.width(), p.height());
|
||||
return (smallest > 460 && smallest < 820) &&
|
||||
(largest > 780 && largest < 1400);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* android
|
||||
*/
|
||||
android: {
|
||||
superset: 'mobile',
|
||||
subsets: [
|
||||
'phablet',
|
||||
'tablet'
|
||||
],
|
||||
settings: {
|
||||
activator: function(p: Platform): string {
|
||||
// md mode defaults to use ripple activator
|
||||
// however, under-powered devices shouldn't use ripple
|
||||
// if this a linux device, and is using Android Chrome v36 (Android 5.0)
|
||||
// or above then use ripple, otherwise do not use a ripple effect
|
||||
if (p.testNavigatorPlatform('linux')) {
|
||||
let chromeVersion = p.matchUserAgentVersion(/Chrome\/(\d+).(\d+)?/);
|
||||
if (chromeVersion) {
|
||||
// linux android device using modern android chrome browser gets ripple
|
||||
return (parseInt(chromeVersion.major, 10) < 36 ? 'none' : 'ripple');
|
||||
}
|
||||
// linux android device not using chrome browser checks just android's version
|
||||
if (p.version().major < 5) {
|
||||
return 'none';
|
||||
}
|
||||
}
|
||||
|
||||
// fallback to always use ripple
|
||||
return 'ripple';
|
||||
},
|
||||
autoFocusAssist: 'immediate',
|
||||
hoverCSS: false,
|
||||
keyboardHeight: 300,
|
||||
mode: 'md',
|
||||
},
|
||||
isMatch(p: Platform) {
|
||||
return p.isPlatformMatch('android', ['android', 'silk'], ['windows phone']);
|
||||
},
|
||||
versionParser(p: Platform) {
|
||||
return p.matchUserAgentVersion(/Android (\d+).(\d+)?/);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* ios
|
||||
*/
|
||||
ios: {
|
||||
superset: 'mobile',
|
||||
subsets: [
|
||||
'ipad',
|
||||
'iphone'
|
||||
],
|
||||
settings: {
|
||||
autoFocusAssist: 'delay',
|
||||
hoverCSS: false,
|
||||
inputBlurring: isIOSDevice,
|
||||
inputCloning: isIOSDevice,
|
||||
keyboardHeight: 300,
|
||||
mode: 'ios',
|
||||
scrollAssist: isIOSDevice,
|
||||
statusbarPadding: !!((<any>window).cordova),
|
||||
swipeBackEnabled: isIOSDevice,
|
||||
swipeBackThreshold: 40,
|
||||
tapPolyfill: isIOSDevice,
|
||||
virtualScrollEventAssist: !(window.indexedDB),
|
||||
canDisableScroll: !!(window.indexedDB),
|
||||
},
|
||||
isMatch(p: Platform) {
|
||||
return p.isPlatformMatch('ios', ['iphone', 'ipad', 'ipod'], ['windows phone']);
|
||||
},
|
||||
versionParser(p: Platform) {
|
||||
return p.matchUserAgentVersion(/OS (\d+)_(\d+)?/);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* ipad
|
||||
*/
|
||||
ipad: {
|
||||
superset: 'tablet',
|
||||
settings: {
|
||||
keyboardHeight: 500,
|
||||
},
|
||||
isMatch(p: Platform) {
|
||||
return p.isPlatformMatch('ipad');
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* iphone
|
||||
*/
|
||||
iphone: {
|
||||
subsets: [
|
||||
'phablet'
|
||||
],
|
||||
isMatch(p: Platform) {
|
||||
return p.isPlatformMatch('iphone');
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Windows
|
||||
*/
|
||||
windows: {
|
||||
superset: 'mobile',
|
||||
subsets: [
|
||||
'phablet',
|
||||
'tablet'
|
||||
],
|
||||
settings: {
|
||||
mode: 'wp',
|
||||
autoFocusAssist: 'immediate',
|
||||
hoverCSS: false
|
||||
},
|
||||
isMatch(p: Platform): boolean {
|
||||
return p.isPlatformMatch('windows', ['windows phone']);
|
||||
},
|
||||
versionParser(p: Platform): any {
|
||||
return p.matchUserAgentVersion(/Windows Phone (\d+).(\d+)?/);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* cordova
|
||||
*/
|
||||
cordova: {
|
||||
isEngine: true,
|
||||
initialize: function(p: Platform) {
|
||||
|
||||
// prepare a custom "ready" for cordova "deviceready"
|
||||
p.prepareReady = function() {
|
||||
// 1) ionic bootstrapped
|
||||
windowLoad(function() {
|
||||
// 2) window onload triggered or completed
|
||||
document.addEventListener('deviceready', function() {
|
||||
// 3) cordova deviceready event triggered
|
||||
|
||||
// add cordova listeners to emit platform events
|
||||
document.addEventListener('backbutton', function(ev: Event) {
|
||||
p.zone.run(() => {
|
||||
p.backButton.emit(ev);
|
||||
});
|
||||
});
|
||||
document.addEventListener('pause', function(ev: Event) {
|
||||
p.zone.run(() => {
|
||||
p.pause.emit(ev);
|
||||
});
|
||||
});
|
||||
document.addEventListener('resume', function(ev: Event) {
|
||||
p.zone.run(() => {
|
||||
p.resume.emit(ev);
|
||||
});
|
||||
});
|
||||
|
||||
// cordova has its own exitApp method
|
||||
p.exitApp = function() {
|
||||
(<any>window.navigator).app.exitApp();
|
||||
};
|
||||
|
||||
// cordova has fully loaded and we've added listeners
|
||||
p.triggerReady('cordova');
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
},
|
||||
isMatch(): boolean {
|
||||
return !!((<any>window).cordova || (<any>window).PhoneGap || (<any>window).phonegap);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
function isIOSDevice(p: Platform) {
|
||||
// shortcut function to be reused internally
|
||||
// checks navigator.platform to see if it's an actual iOS device
|
||||
// this does not use the user-agent string because it is often spoofed
|
||||
// an actual iPad will return true, a chrome dev tools iPad will return false
|
||||
return p.testNavigatorPlatform('iphone|ipad|ipod');
|
||||
}
|
||||
|
||||
|
||||
export const PlatformConfigToken = new OpaqueToken('PLTCONFIG');
|
||||
|
||||
export function providePlatformConfigs() {
|
||||
return PLATFORM_CONFIGS;
|
||||
}
|
||||
@@ -2,7 +2,7 @@ import { EventEmitter, NgZone, OpaqueToken } from '@angular/core';
|
||||
|
||||
import { QueryParams } from './query-params';
|
||||
import { ready, windowDimensions, flushDimensionCache } from '../util/dom';
|
||||
import { setupPlatformRegistry } from './registry';
|
||||
|
||||
|
||||
/**
|
||||
* @name Platform
|
||||
@@ -40,6 +40,8 @@ export class Platform {
|
||||
private _readyResolve: any;
|
||||
private _resizeTm: any;
|
||||
private _bbActions: BackButtonAction[] = [];
|
||||
private _registry: {[name: string]: PlatformConfig};
|
||||
private _default: string;
|
||||
|
||||
/** @private */
|
||||
zone: NgZone;
|
||||
@@ -502,29 +504,29 @@ export class Platform {
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
static register(platformConfig: PlatformConfig) {
|
||||
platformRegistry[platformConfig.name] = platformConfig;
|
||||
setPlatformConfigs(platformConfigs: {[key: string]: PlatformConfig}) {
|
||||
this._registry = platformConfigs || {};
|
||||
}
|
||||
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
static registry() {
|
||||
return platformRegistry;
|
||||
getPlatformConfig(platformName: string): PlatformConfig {
|
||||
return this._registry[platformName] || {};
|
||||
}
|
||||
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
static get(platformName: string): PlatformConfig {
|
||||
return platformRegistry[platformName] || {};
|
||||
registry() {
|
||||
return this._registry;
|
||||
}
|
||||
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
static setDefault(platformName: string) {
|
||||
platformDefault = platformName;
|
||||
setDefault(platformName: string) {
|
||||
this._default = platformName;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -586,13 +588,13 @@ export class Platform {
|
||||
}
|
||||
|
||||
/** @private */
|
||||
load() {
|
||||
init() {
|
||||
let rootPlatformNode: PlatformNode;
|
||||
let enginePlatformNode: PlatformNode;
|
||||
|
||||
// figure out the most specific platform and active engine
|
||||
let tmpPlatform: PlatformNode;
|
||||
for (let platformName in platformRegistry) {
|
||||
for (let platformName in this._registry) {
|
||||
|
||||
tmpPlatform = this.matchPlatform(platformName);
|
||||
if (tmpPlatform) {
|
||||
@@ -614,7 +616,7 @@ export class Platform {
|
||||
}
|
||||
|
||||
if (!rootPlatformNode) {
|
||||
rootPlatformNode = new PlatformNode(platformDefault);
|
||||
rootPlatformNode = new PlatformNode(this._registry, this._default);
|
||||
}
|
||||
|
||||
// build a Platform instance filled with the
|
||||
@@ -634,7 +636,7 @@ export class Platform {
|
||||
|
||||
let platformNode = rootPlatformNode;
|
||||
while (platformNode) {
|
||||
insertSuperset(platformNode);
|
||||
insertSuperset(this._registry, platformNode);
|
||||
platformNode = platformNode.child;
|
||||
}
|
||||
|
||||
@@ -674,7 +676,7 @@ export class Platform {
|
||||
// build a PlatformNode and assign config data to it
|
||||
// use it's getRoot method to build up its hierarchy
|
||||
// depending on which platforms match
|
||||
let platformNode = new PlatformNode(platformName);
|
||||
let platformNode = new PlatformNode(this._registry, platformName);
|
||||
let rootNode = platformNode.getRoot(this);
|
||||
|
||||
if (rootNode) {
|
||||
@@ -690,12 +692,12 @@ export class Platform {
|
||||
|
||||
}
|
||||
|
||||
function insertSuperset(platformNode: PlatformNode) {
|
||||
function insertSuperset(registry: any, platformNode: PlatformNode) {
|
||||
let supersetPlaformName = platformNode.superset();
|
||||
if (supersetPlaformName) {
|
||||
// add a platform in between two exist platforms
|
||||
// so we can build the correct hierarchy of active platforms
|
||||
let supersetPlatform = new PlatformNode(supersetPlaformName);
|
||||
let supersetPlatform = new PlatformNode(registry, supersetPlaformName);
|
||||
supersetPlatform.parent = platformNode.parent;
|
||||
supersetPlatform.child = platformNode;
|
||||
if (supersetPlatform.parent) {
|
||||
@@ -717,8 +719,8 @@ class PlatformNode {
|
||||
isEngine: boolean;
|
||||
depth: number;
|
||||
|
||||
constructor(platformName: string) {
|
||||
this.c = Platform.get(platformName);
|
||||
constructor(public registry: {[name: string]: PlatformConfig}, platformName: string) {
|
||||
this.c = registry[platformName];
|
||||
this.name = platformName;
|
||||
this.isEngine = this.c.isEngine;
|
||||
}
|
||||
@@ -741,9 +743,9 @@ class PlatformNode {
|
||||
|
||||
version(p: Platform): PlatformVersion {
|
||||
if (this.c.versionParser) {
|
||||
let v = this.c.versionParser(p);
|
||||
const v = this.c.versionParser(p);
|
||||
if (v) {
|
||||
let str = v.major + '.' + v.minor;
|
||||
const str = v.major + '.' + v.minor;
|
||||
return {
|
||||
str: str,
|
||||
num: parseFloat(str),
|
||||
@@ -767,7 +769,7 @@ class PlatformNode {
|
||||
let rootPlatformNode: PlatformNode = null;
|
||||
|
||||
for (let i = 0; i < parents.length; i++) {
|
||||
platformNode = new PlatformNode(parents[i]);
|
||||
platformNode = new PlatformNode(this.registry, parents[i]);
|
||||
platformNode.child = this;
|
||||
|
||||
rootPlatformNode = platformNode.getRoot(p);
|
||||
@@ -782,13 +784,11 @@ class PlatformNode {
|
||||
}
|
||||
|
||||
getSubsetParents(subsetPlatformName: string): string[] {
|
||||
let platformRegistry = Platform.registry();
|
||||
|
||||
let parentPlatformNames: string[] = [];
|
||||
const parentPlatformNames: string[] = [];
|
||||
let platform: PlatformConfig = null;
|
||||
|
||||
for (let platformName in platformRegistry) {
|
||||
platform = platformRegistry[platformName];
|
||||
for (let platformName in this.registry) {
|
||||
platform = this.registry[platformName];
|
||||
|
||||
if (platform.subsets && platform.subsets.indexOf(subsetPlatformName) > -1) {
|
||||
parentPlatformNames.push(platformName);
|
||||
@@ -800,11 +800,8 @@ class PlatformNode {
|
||||
|
||||
}
|
||||
|
||||
let platformRegistry: {[name: string]: PlatformConfig} = {};
|
||||
let platformDefault: string = null;
|
||||
|
||||
export interface PlatformConfig {
|
||||
name?: string;
|
||||
isEngine?: boolean;
|
||||
initialize?: Function;
|
||||
isMatch?: Function;
|
||||
@@ -826,21 +823,23 @@ interface BackButtonAction {
|
||||
priority: number;
|
||||
}
|
||||
|
||||
export function setupPlatform(queryParams: QueryParams, userAgent: string, navigatorPlatform: string, dir: string, lang: string, zone: NgZone): Platform {
|
||||
setupPlatformRegistry();
|
||||
|
||||
export function setupPlatform(platformConfigs: {[key: string]: PlatformConfig}, queryParams: QueryParams, userAgent: string, navigatorPlatform: string, docDirection: string, docLanguage: string, zone: NgZone): Platform {
|
||||
const p = new Platform();
|
||||
p.setDefault('core');
|
||||
p.setPlatformConfigs(platformConfigs);
|
||||
p.setUserAgent(userAgent);
|
||||
p.setQueryParams(queryParams);
|
||||
p.setNavigatorPlatform(navigatorPlatform);
|
||||
p.setDir(dir, false);
|
||||
p.setLang(lang, false);
|
||||
p.setDir(docDirection, false);
|
||||
p.setLang(docLanguage, false);
|
||||
p.setZone(zone);
|
||||
p.load();
|
||||
p.init();
|
||||
return p;
|
||||
}
|
||||
|
||||
export const UserAgent = new OpaqueToken('USERAGENT');
|
||||
export const UserNavigatorPlatform = new OpaqueToken('USERNAVPLT');
|
||||
export const UserDir = new OpaqueToken('USERDIR');
|
||||
export const UserLang = new OpaqueToken('USERLANG');
|
||||
|
||||
export const UserAgentToken = new OpaqueToken('USERAGENT');
|
||||
export const NavigatorPlatformToken = new OpaqueToken('NAVPLT');
|
||||
export const DocumentDirToken = new OpaqueToken('DOCDIR');
|
||||
export const DocLangToken = new OpaqueToken('DOCLANG');
|
||||
|
||||
@@ -27,7 +27,7 @@ export class QueryParams {
|
||||
|
||||
}
|
||||
|
||||
export const UserUrl = new OpaqueToken('USERURL');
|
||||
export const UrlToken = new OpaqueToken('USERURL');
|
||||
|
||||
export function setupQueryParams(url: string): QueryParams {
|
||||
return new QueryParams(url);
|
||||
|
||||
@@ -1,229 +0,0 @@
|
||||
import { Platform } from './platform';
|
||||
import { windowLoad } from '../util/dom';
|
||||
|
||||
const win: any = window;
|
||||
const doc: any = document;
|
||||
|
||||
|
||||
const PLATFORM_CORE: any = {
|
||||
name: 'core',
|
||||
settings: {
|
||||
mode: 'md',
|
||||
keyboardHeight: 290
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
const PLATFORM_MOBILE: any = {
|
||||
name: 'mobile'
|
||||
};
|
||||
|
||||
|
||||
const PLATFORM_PHABLET: any = {
|
||||
name: 'phablet',
|
||||
isMatch(p: Platform) {
|
||||
let smallest = Math.min(p.width(), p.height());
|
||||
let largest = Math.max(p.width(), p.height());
|
||||
return (smallest > 390 && smallest < 520) &&
|
||||
(largest > 620 && largest < 800);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
const PLATFORM_TABLET: any = {
|
||||
name: 'tablet',
|
||||
isMatch(p: Platform) {
|
||||
let smallest = Math.min(p.width(), p.height());
|
||||
let largest = Math.max(p.width(), p.height());
|
||||
return (smallest > 460 && smallest < 820) &&
|
||||
(largest > 780 && largest < 1400);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
const PLATFORM_ANDROID: any = {
|
||||
name: 'android',
|
||||
superset: 'mobile',
|
||||
subsets: [
|
||||
'phablet',
|
||||
'tablet'
|
||||
],
|
||||
settings: {
|
||||
activator: function(p: Platform): string {
|
||||
// md mode defaults to use ripple activator
|
||||
// however, under-powered devices shouldn't use ripple
|
||||
// if this a linux device, and is using Android Chrome v36 (Android 5.0)
|
||||
// or above then use ripple, otherwise do not use a ripple effect
|
||||
if (p.testNavigatorPlatform('linux')) {
|
||||
let chromeVersion = p.matchUserAgentVersion(/Chrome\/(\d+).(\d+)?/);
|
||||
if (chromeVersion) {
|
||||
// linux android device using modern android chrome browser gets ripple
|
||||
return (parseInt(chromeVersion.major, 10) < 36 ? 'none' : 'ripple');
|
||||
}
|
||||
// linux android device not using chrome browser checks just android's version
|
||||
if (p.version().major < 5) {
|
||||
return 'none';
|
||||
}
|
||||
}
|
||||
|
||||
// fallback to always use ripple
|
||||
return 'ripple';
|
||||
},
|
||||
autoFocusAssist: 'immediate',
|
||||
hoverCSS: false,
|
||||
keyboardHeight: 300,
|
||||
mode: 'md',
|
||||
},
|
||||
isMatch(p: Platform): boolean {
|
||||
return p.isPlatformMatch('android', ['android', 'silk'], ['windows phone']);
|
||||
},
|
||||
versionParser(p: Platform): any {
|
||||
return p.matchUserAgentVersion(/Android (\d+).(\d+)?/);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
const PLATFORM_IOS: any = {
|
||||
name: 'ios',
|
||||
superset: 'mobile',
|
||||
subsets: [
|
||||
'ipad',
|
||||
'iphone'
|
||||
],
|
||||
settings: {
|
||||
autoFocusAssist: 'delay',
|
||||
hoverCSS: false,
|
||||
inputBlurring: isIOSDevice,
|
||||
inputCloning: isIOSDevice,
|
||||
keyboardHeight: 300,
|
||||
mode: 'ios',
|
||||
scrollAssist: isIOSDevice,
|
||||
statusbarPadding: !!(win.cordova),
|
||||
swipeBackEnabled: isIOSDevice,
|
||||
swipeBackThreshold: 40,
|
||||
tapPolyfill: isIOSDevice,
|
||||
virtualScrollEventAssist: !(win.indexedDB),
|
||||
canDisableScroll: !!(win.indexedDB),
|
||||
},
|
||||
isMatch(p: Platform): boolean {
|
||||
return p.isPlatformMatch('ios', ['iphone', 'ipad', 'ipod'], ['windows phone']);
|
||||
},
|
||||
versionParser(p: Platform): any {
|
||||
return p.matchUserAgentVersion(/OS (\d+)_(\d+)?/);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
const PLATFORM_IPAD: any = {
|
||||
name: 'ipad',
|
||||
superset: 'tablet',
|
||||
settings: {
|
||||
keyboardHeight: 500,
|
||||
},
|
||||
isMatch(p: Platform): boolean {
|
||||
return p.isPlatformMatch('ipad');
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
const PLATFORM_IPHONE: any = {
|
||||
name: 'iphone',
|
||||
subsets: [
|
||||
'phablet'
|
||||
],
|
||||
isMatch(p: Platform): boolean {
|
||||
return p.isPlatformMatch('iphone');
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
const PLATFORM_WINDOWS: any = {
|
||||
name: 'windows',
|
||||
superset: 'mobile',
|
||||
subsets: [
|
||||
'phablet',
|
||||
'tablet'
|
||||
],
|
||||
settings: {
|
||||
mode: 'wp',
|
||||
autoFocusAssist: 'immediate',
|
||||
hoverCSS: false
|
||||
},
|
||||
isMatch(p: Platform): boolean {
|
||||
return p.isPlatformMatch('windows', ['windows phone']);
|
||||
},
|
||||
versionParser(p: Platform): any {
|
||||
return p.matchUserAgentVersion(/Windows Phone (\d+).(\d+)?/);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
const PLATFORM_CORDOVA: any = {
|
||||
name: 'cordova',
|
||||
isEngine: true,
|
||||
initialize: function(p: Platform) {
|
||||
|
||||
// prepare a custom "ready" for cordova "deviceready"
|
||||
p.prepareReady = function() {
|
||||
// 1) ionic bootstrapped
|
||||
windowLoad(function() {
|
||||
// 2) window onload triggered or completed
|
||||
doc.addEventListener('deviceready', function() {
|
||||
// 3) cordova deviceready event triggered
|
||||
|
||||
// add cordova listeners to emit platform events
|
||||
doc.addEventListener('backbutton', function(ev: Event) {
|
||||
p.zone.run(() => {
|
||||
p.backButton.emit(ev);
|
||||
});
|
||||
});
|
||||
doc.addEventListener('pause', function(ev: Event) {
|
||||
p.zone.run(() => {
|
||||
p.pause.emit(ev);
|
||||
});
|
||||
});
|
||||
doc.addEventListener('resume', function(ev: Event) {
|
||||
p.zone.run(() => {
|
||||
p.resume.emit(ev);
|
||||
});
|
||||
});
|
||||
|
||||
// cordova has its own exitApp method
|
||||
p.exitApp = function() {
|
||||
win.navigator.app.exitApp();
|
||||
};
|
||||
|
||||
// cordova has fully loaded and we've added listeners
|
||||
p.triggerReady('cordova');
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
},
|
||||
isMatch(): boolean {
|
||||
return !!(win.cordova || win.PhoneGap || win.phonegap);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
function isIOSDevice(p: Platform) {
|
||||
// shortcut function to be reused internally
|
||||
// checks navigator.platform to see if it's an actual iOS device
|
||||
// this does not use the user-agent string because it is often spoofed
|
||||
// an actual iPad will return true, a chrome dev tools iPad will return false
|
||||
return p.testNavigatorPlatform('iphone|ipad|ipod');
|
||||
}
|
||||
|
||||
export function setupPlatformRegistry() {
|
||||
Platform.register(PLATFORM_CORE);
|
||||
Platform.register(PLATFORM_MOBILE);
|
||||
Platform.register(PLATFORM_PHABLET);
|
||||
Platform.register(PLATFORM_TABLET);
|
||||
Platform.register(PLATFORM_ANDROID);
|
||||
Platform.register(PLATFORM_IOS);
|
||||
Platform.register(PLATFORM_IPAD);
|
||||
Platform.register(PLATFORM_IPHONE);
|
||||
Platform.register(PLATFORM_WINDOWS);
|
||||
Platform.register(PLATFORM_CORDOVA);
|
||||
Platform.setDefault('core');
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
import { Config } from '../../config/config';
|
||||
import { Platform } from '../platform';
|
||||
import { QueryParams } from '../query-params';
|
||||
import { setupPlatformRegistry } from '../registry';
|
||||
import { setupModeConfig } from '../../config/modes';
|
||||
import { PLATFORM_CONFIGS } from '../platform-registry';
|
||||
import { registerModeConfigs } from '../../config/mode-registry';
|
||||
|
||||
|
||||
describe('Platform', () => {
|
||||
@@ -9,8 +10,6 @@ describe('Platform', () => {
|
||||
describe('registerBackButtonAction', () => {
|
||||
|
||||
it('should register two actions with different priorities, call the highest one', () => {
|
||||
let platform = new Platform();
|
||||
|
||||
let ranAction1 = false;
|
||||
let action1 = () => {
|
||||
ranAction1 = true;
|
||||
@@ -30,8 +29,6 @@ describe('Platform', () => {
|
||||
});
|
||||
|
||||
it('should register two actions with the same priority, call the second one', () => {
|
||||
let platform = new Platform();
|
||||
|
||||
let ranAction1 = false;
|
||||
let action1 = () => {
|
||||
ranAction1 = true;
|
||||
@@ -51,8 +48,6 @@ describe('Platform', () => {
|
||||
});
|
||||
|
||||
it('should register a default action', () => {
|
||||
let platform = new Platform();
|
||||
|
||||
let ranAction1 = false;
|
||||
let action1 = () => {
|
||||
ranAction1 = true;
|
||||
@@ -65,18 +60,17 @@ describe('Platform', () => {
|
||||
});
|
||||
|
||||
it('should not run any actions when none registered', () => {
|
||||
let platform = new Platform();
|
||||
platform.runBackButtonAction();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
it('should set core as the fallback', () => {
|
||||
let platform = new Platform();
|
||||
let qp = new QueryParams('');
|
||||
platform.setDefault('core');
|
||||
platform.setQueryParams(qp);
|
||||
platform.setUserAgent('idk');
|
||||
platform.load();
|
||||
platform.init();
|
||||
|
||||
expect(platform.is('android')).toEqual(false);
|
||||
expect(platform.is('ios')).toEqual(false);
|
||||
@@ -84,10 +78,9 @@ describe('Platform', () => {
|
||||
});
|
||||
|
||||
it('should set windows via querystring', () => {
|
||||
let platform = new Platform();
|
||||
let qp = new QueryParams('/?ionicplatform=windows');
|
||||
platform.setQueryParams(qp);
|
||||
platform.load();
|
||||
platform.init();
|
||||
|
||||
expect(platform.is('core')).toEqual(false);
|
||||
expect(platform.is('mobile')).toEqual(true);
|
||||
@@ -97,10 +90,9 @@ describe('Platform', () => {
|
||||
});
|
||||
|
||||
it('should set ios via querystring', () => {
|
||||
let platform = new Platform();
|
||||
let qp = new QueryParams('/?ionicplatform=ios');
|
||||
platform.setQueryParams(qp);
|
||||
platform.load();
|
||||
platform.init();
|
||||
|
||||
expect(platform.is('core')).toEqual(false);
|
||||
expect(platform.is('mobile')).toEqual(true);
|
||||
@@ -110,11 +102,10 @@ describe('Platform', () => {
|
||||
});
|
||||
|
||||
it('should set windows via querystring, even with android user agent', () => {
|
||||
let platform = new Platform();
|
||||
let qp = new QueryParams('/?ionicplatform=windows');
|
||||
platform.setQueryParams(qp);
|
||||
platform.setUserAgent(ANDROID_UA);
|
||||
platform.load();
|
||||
platform.init();
|
||||
|
||||
expect(platform.is('core')).toEqual(false);
|
||||
expect(platform.is('android')).toEqual(false);
|
||||
@@ -123,11 +114,10 @@ describe('Platform', () => {
|
||||
});
|
||||
|
||||
it('should set ios via querystring, even with android user agent', () => {
|
||||
let platform = new Platform();
|
||||
let qp = new QueryParams('/?ionicplatform=ios');
|
||||
platform.setQueryParams(qp);
|
||||
platform.setUserAgent(ANDROID_UA);
|
||||
platform.load();
|
||||
platform.init();
|
||||
|
||||
expect(platform.is('core')).toEqual(false);
|
||||
expect(platform.is('android')).toEqual(false);
|
||||
@@ -136,10 +126,9 @@ describe('Platform', () => {
|
||||
});
|
||||
|
||||
it('should set android via querystring', () => {
|
||||
let platform = new Platform();
|
||||
let qp = new QueryParams('/?ionicplatform=android');
|
||||
platform.setQueryParams(qp);
|
||||
platform.load();
|
||||
platform.init();
|
||||
|
||||
expect(platform.is('core')).toEqual(false);
|
||||
expect(platform.is('android')).toEqual(true);
|
||||
@@ -148,11 +137,10 @@ describe('Platform', () => {
|
||||
});
|
||||
|
||||
it('should set android via querystring, even with ios user agent', () => {
|
||||
let platform = new Platform();
|
||||
let qp = new QueryParams('/?ionicplatform=android');
|
||||
platform.setQueryParams(qp);
|
||||
platform.setUserAgent(IPHONE_UA);
|
||||
platform.load();
|
||||
platform.init();
|
||||
|
||||
expect(platform.is('core')).toEqual(false);
|
||||
expect(platform.is('android')).toEqual(true);
|
||||
@@ -161,11 +149,10 @@ describe('Platform', () => {
|
||||
});
|
||||
|
||||
it('should set windows platform via user agent', () => {
|
||||
let platform = new Platform();
|
||||
let qp = new QueryParams('');
|
||||
platform.setQueryParams(qp);
|
||||
platform.setUserAgent(WINDOWS_PHONE_UA);
|
||||
platform.load();
|
||||
platform.init();
|
||||
|
||||
expect(platform.is('core')).toEqual(false);
|
||||
expect(platform.is('mobile')).toEqual(true);
|
||||
@@ -175,11 +162,10 @@ describe('Platform', () => {
|
||||
});
|
||||
|
||||
it('should set windows platform via windows8 mobile user agent', () => {
|
||||
let platform = new Platform();
|
||||
let qp = new QueryParams('');
|
||||
platform.setQueryParams(qp);
|
||||
platform.setUserAgent(WINDOWS8_PHONE_UA);
|
||||
platform.load();
|
||||
platform.init();
|
||||
|
||||
expect(platform.is('core')).toEqual(false);
|
||||
expect(platform.is('mobile')).toEqual(true);
|
||||
@@ -189,11 +175,10 @@ describe('Platform', () => {
|
||||
});
|
||||
|
||||
it('should set windows platform via windows7 mobile user agent', () => {
|
||||
let platform = new Platform();
|
||||
let qp = new QueryParams('');
|
||||
platform.setQueryParams(qp);
|
||||
platform.setUserAgent(WINDOWS7_PHONE_UA);
|
||||
platform.load();
|
||||
platform.init();
|
||||
|
||||
expect(platform.is('core')).toEqual(false);
|
||||
expect(platform.is('mobile')).toEqual(true);
|
||||
@@ -203,11 +188,10 @@ describe('Platform', () => {
|
||||
});
|
||||
|
||||
it('should set android via user agent', () => {
|
||||
let platform = new Platform();
|
||||
let qp = new QueryParams('');
|
||||
platform.setQueryParams(qp);
|
||||
platform.setUserAgent(ANDROID_UA);
|
||||
platform.load();
|
||||
platform.init();
|
||||
|
||||
expect(platform.is('core')).toEqual(false);
|
||||
expect(platform.is('mobile')).toEqual(true);
|
||||
@@ -217,11 +201,10 @@ describe('Platform', () => {
|
||||
});
|
||||
|
||||
it('should set iphone via user agent', () => {
|
||||
let platform = new Platform();
|
||||
let qp = new QueryParams('');
|
||||
platform.setQueryParams(qp);
|
||||
platform.setUserAgent(IPHONE_UA);
|
||||
platform.load();
|
||||
platform.init();
|
||||
|
||||
expect(platform.is('core')).toEqual(false);
|
||||
expect(platform.is('mobile')).toEqual(true);
|
||||
@@ -233,11 +216,10 @@ describe('Platform', () => {
|
||||
});
|
||||
|
||||
it('should set ipad via user agent', () => {
|
||||
let platform = new Platform();
|
||||
let qp = new QueryParams('');
|
||||
platform.setQueryParams(qp);
|
||||
platform.setUserAgent(IPAD_UA);
|
||||
platform.load();
|
||||
platform.init();
|
||||
|
||||
expect(platform.is('core')).toEqual(false);
|
||||
expect(platform.is('mobile')).toEqual(true);
|
||||
@@ -249,11 +231,11 @@ describe('Platform', () => {
|
||||
});
|
||||
|
||||
it('should set core platform for osx desktop firefox', () => {
|
||||
let platform = new Platform();
|
||||
let qp = new QueryParams('');
|
||||
platform.setDefault('core');
|
||||
platform.setQueryParams(qp);
|
||||
platform.setUserAgent(OSX_10_FIREFOX_43_UA);
|
||||
platform.load();
|
||||
platform.init();
|
||||
|
||||
expect(platform.is('core')).toEqual(true);
|
||||
expect(platform.is('mobile')).toEqual(false);
|
||||
@@ -265,11 +247,11 @@ describe('Platform', () => {
|
||||
});
|
||||
|
||||
it('should set core platform for osx desktop safari', () => {
|
||||
let platform = new Platform();
|
||||
let qp = new QueryParams('');
|
||||
platform.setDefault('core');
|
||||
platform.setQueryParams(qp);
|
||||
platform.setUserAgent(OSX_10_SAFARI_9_UA);
|
||||
platform.load();
|
||||
platform.init();
|
||||
|
||||
expect(platform.is('core')).toEqual(true);
|
||||
expect(platform.is('mobile')).toEqual(false);
|
||||
@@ -281,11 +263,11 @@ describe('Platform', () => {
|
||||
});
|
||||
|
||||
it('should set core platform for osx desktop chrome', () => {
|
||||
let platform = new Platform();
|
||||
let qp = new QueryParams('');
|
||||
platform.setDefault('core');
|
||||
platform.setQueryParams(qp);
|
||||
platform.setUserAgent(OSX_10_CHROME_49_UA);
|
||||
platform.load();
|
||||
platform.init();
|
||||
|
||||
expect(platform.is('core')).toEqual(true);
|
||||
expect(platform.is('mobile')).toEqual(false);
|
||||
@@ -297,11 +279,11 @@ describe('Platform', () => {
|
||||
});
|
||||
|
||||
it('should set core platform for windows desktop chrome', () => {
|
||||
let platform = new Platform();
|
||||
let qp = new QueryParams('');
|
||||
platform.setDefault('core');
|
||||
platform.setQueryParams(qp);
|
||||
platform.setUserAgent(WINDOWS_10_CHROME_40_UA);
|
||||
platform.load();
|
||||
platform.init();
|
||||
|
||||
expect(platform.is('core')).toEqual(true);
|
||||
expect(platform.is('mobile')).toEqual(false);
|
||||
@@ -313,11 +295,11 @@ describe('Platform', () => {
|
||||
});
|
||||
|
||||
it('should set core platform for windows desktop edge', () => {
|
||||
let platform = new Platform();
|
||||
let qp = new QueryParams('');
|
||||
platform.setDefault('core');
|
||||
platform.setQueryParams(qp);
|
||||
platform.setUserAgent(WINDOWS_10_EDGE_12_UA);
|
||||
platform.load();
|
||||
platform.init();
|
||||
|
||||
expect(platform.is('core')).toEqual(true);
|
||||
expect(platform.is('mobile')).toEqual(false);
|
||||
@@ -329,11 +311,11 @@ describe('Platform', () => {
|
||||
});
|
||||
|
||||
it('should set core platform for windows desktop IE', () => {
|
||||
let platform = new Platform();
|
||||
let qp = new QueryParams('');
|
||||
platform.setDefault('core');
|
||||
platform.setQueryParams(qp);
|
||||
platform.setUserAgent(WINDOWS_8_IE_11_UA);
|
||||
platform.load();
|
||||
platform.init();
|
||||
|
||||
expect(platform.is('core')).toEqual(true);
|
||||
expect(platform.is('mobile')).toEqual(false);
|
||||
@@ -344,9 +326,12 @@ describe('Platform', () => {
|
||||
expect(platform.is('tablet')).toEqual(false);
|
||||
});
|
||||
|
||||
let platform: Platform;
|
||||
|
||||
beforeEach(() => {
|
||||
setupModeConfig();
|
||||
setupPlatformRegistry();
|
||||
platform = new Platform();
|
||||
platform.setPlatformConfigs(PLATFORM_CONFIGS);
|
||||
registerModeConfigs(new Config());
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
import { QueryParams } from '../query-params';
|
||||
import '../registry';
|
||||
import '../../config/modes';
|
||||
|
||||
|
||||
describe('QueryParams', () => {
|
||||
@@ -96,4 +94,5 @@ describe('QueryParams', () => {
|
||||
key: ''
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user