mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-23 22:17:40 +08:00
fix(angula): platform logic belongs to core
This commit is contained in:
@ -1,53 +1,12 @@
|
|||||||
|
|
||||||
import { EventEmitter, Injectable } from '@angular/core';
|
import { EventEmitter, Injectable } from '@angular/core';
|
||||||
import { proxyEvent } from '../util/util';
|
import { proxyEvent } from '../util/util';
|
||||||
import { isAndroid, isCordova, isElectron, isIOS, isIpad, isIphone, isPhablet, isTablet } from '@ionic/core';
|
import { PlatformConfig, detectPlatforms } from '@ionic/core';
|
||||||
|
|
||||||
export interface PlatformConfig {
|
|
||||||
name: string;
|
|
||||||
isMatch: (win: Window) => boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const PLATFORM_CONFIGS: PlatformConfig[] = [
|
|
||||||
{
|
|
||||||
name: 'ipad',
|
|
||||||
isMatch: isIpad
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: 'iphone',
|
|
||||||
isMatch: isIphone
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: 'ios',
|
|
||||||
isMatch: isIOS
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: 'android',
|
|
||||||
isMatch: isAndroid
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: 'phablet',
|
|
||||||
isMatch: isPhablet
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: 'tablet',
|
|
||||||
isMatch: isTablet
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: 'cordova',
|
|
||||||
isMatch: isCordova
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: 'electron',
|
|
||||||
isMatch: isElectron
|
|
||||||
}
|
|
||||||
|
|
||||||
];
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class Platform {
|
export class Platform {
|
||||||
|
|
||||||
private _platforms: PlatformConfig[] = PLATFORM_CONFIGS;
|
private _platforms = detectPlatforms(window);
|
||||||
private _readyPromise: Promise<string>;
|
private _readyPromise: Promise<string>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -88,11 +47,9 @@ export class Platform {
|
|||||||
this._readyPromise = new Promise(res => { readyResolve = res; } );
|
this._readyPromise = new Promise(res => { readyResolve = res; } );
|
||||||
if ((window as any)['cordova']) {
|
if ((window as any)['cordova']) {
|
||||||
window.addEventListener('deviceready', () => {
|
window.addEventListener('deviceready', () => {
|
||||||
this._platforms = this.detectPlatforms(window, this._platforms);
|
|
||||||
readyResolve('cordova');
|
readyResolve('cordova');
|
||||||
}, {once: true});
|
}, {once: true});
|
||||||
} else {
|
} else {
|
||||||
this._platforms = this.detectPlatforms(window, this._platforms);
|
|
||||||
readyResolve('dom');
|
readyResolve('dom');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -151,9 +108,8 @@ export class Platform {
|
|||||||
* Detects the platforms using window and the platforms config provided.
|
* Detects the platforms using window and the platforms config provided.
|
||||||
* Populates the platforms array so they can be used later on for platform detection.
|
* Populates the platforms array so they can be used later on for platform detection.
|
||||||
*/
|
*/
|
||||||
detectPlatforms(win: Window, platforms: PlatformConfig[]) {
|
detectPlatforms(platforms: PlatformConfig[]) {
|
||||||
// bracket notation to ensure they're not property renamed
|
return detectPlatforms(window, platforms);
|
||||||
return platforms.filter(p => p.isMatch(win));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -70,3 +70,48 @@ export function matchMedia(win: Window, query: string, fallback = false): boolea
|
|||||||
? win.matchMedia(query).matches
|
? win.matchMedia(query).matches
|
||||||
: fallback;
|
: fallback;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface PlatformConfig {
|
||||||
|
name: string;
|
||||||
|
isMatch: (win: Window) => boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const PLATFORM_CONFIGS: PlatformConfig[] = [
|
||||||
|
{
|
||||||
|
name: 'ipad',
|
||||||
|
isMatch: isIpad
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'iphone',
|
||||||
|
isMatch: isIphone
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'ios',
|
||||||
|
isMatch: isIOS
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'android',
|
||||||
|
isMatch: isAndroid
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'phablet',
|
||||||
|
isMatch: isPhablet
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'tablet',
|
||||||
|
isMatch: isTablet
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'cordova',
|
||||||
|
isMatch: isCordova
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'electron',
|
||||||
|
isMatch: isElectron
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
export function detectPlatforms(win: Window, platforms: PlatformConfig[] = PLATFORM_CONFIGS) {
|
||||||
|
// bracket notation to ensure they're not property renamed
|
||||||
|
return platforms.filter(p => p.isMatch(win));
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user