fix(angular): populated the platforms array (#14466)

The `_platforms` array wasn't being populated on provider instantiation. Added the appropriate code to populate the array so that when we use other functions from the provider, we get the correct data in return.
This commit is contained in:
Ahsan Ayaz
2018-05-14 16:06:23 +05:00
committed by Manu MA
parent d93b1d57d1
commit d177087225

View File

@ -1,16 +1,53 @@
import { EventEmitter, Injectable } from '@angular/core';
import { proxyEvent } from '../util/util';
import { isAndroid, isCordova, isElectron, isIOS, isIpad, isIphone, isPhablet, isTablet } 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()
export class Platform {
private _platforms: PlatformConfig[] = [];
private _platforms: PlatformConfig[] = PLATFORM_CONFIGS;
private _readyPromise: Promise<string>;
/**
@ -51,9 +88,11 @@ export class Platform {
this._readyPromise = new Promise(res => { readyResolve = res; } );
if ((window as any)['cordova']) {
window.addEventListener('deviceready', () => {
this._platforms = this.detectPlatforms(window, this._platforms);
readyResolve('cordova');
}, {once: true});
} else {
this._platforms = this.detectPlatforms(window, this._platforms);
readyResolve('dom');
}
}
@ -104,6 +143,19 @@ export class Platform {
return this._platforms.some(p => p.name === platformName);
}
/**
* @param {Window} win the window object
* @param {PlatformConfig[]} platforms an array of platforms (platform configs)
* to get the appropriate platforms according to the configs provided.
* @description
* Detects the platforms using window and the platforms config provided.
* Populates the platforms array so they can be used later on for platform detection.
*/
detectPlatforms(win: Window, platforms: PlatformConfig[]) {
// bracket notation to ensure they're not property renamed
return platforms.filter(p => p.isMatch(win));
}
/**
* @returns {array} the array of platforms
* @description