fix(angular): Config provider

This commit is contained in:
Manu Mtz.-Almeida
2018-04-20 17:06:35 +02:00
parent 2b3c14b608
commit c87f0c561e
4 changed files with 56 additions and 4 deletions

View File

@ -1,4 +1,4 @@
import { ModuleWithProviders, NgModule } from '@angular/core'; import { InjectionToken, ModuleWithProviders, NgModule } from '@angular/core';
import { CommonModule } from '@angular/common'; import { CommonModule } from '@angular/common';
import * as c from './directives'; import * as c from './directives';
@ -119,7 +119,7 @@ const PROVIDERS = [
p.NavController, p.NavController,
p.Platform, p.Platform,
p.Events, p.Events,
p.DomController p.DomController,
]; ];
@NgModule({ @NgModule({
@ -135,12 +135,22 @@ const PROVIDERS = [
] ]
}) })
export class IonicModule { export class IonicModule {
static forRoot(): ModuleWithProviders { static forRoot(config?: {[key: string]: any}): ModuleWithProviders {
return { return {
ngModule: IonicModule, ngModule: IonicModule,
providers: [ providers: [
// Load config with defualt values
{ provide: ConfigToken, useValue: config },
{ provide: p.Config, useFactory: setupConfig, deps: [ ConfigToken ] },
...PROVIDERS, ...PROVIDERS,
] ]
}; };
} }
} }
export const ConfigToken = new InjectionToken<any>('USERCONFIG');
export function setupConfig(userConfig: any): p.Config {
const config = new p.Config(userConfig);
return config;
}

View File

@ -0,0 +1,40 @@
import { Config as CoreConfig } from '@ionic/core';
export class Config {
constructor(defaultConfig: {[key: string]: any}) {
const Ionic = (window as any).Ionic;
if (Ionic.config && Ionic.config.constructor.name !== 'Object') {
console.error('ionic config was already initialized');
return;
}
Ionic.config = {
...Ionic.config,
...defaultConfig
};
}
get(key: string, fallback?: any): any {
return getConfig().get(key, fallback);
}
getBoolean(key: string, fallback?: boolean): boolean {
return getConfig().getBoolean(key, fallback);
}
getNumber(key: string, fallback?: number): number {
return getConfig().getNumber(key, fallback);
}
set(key: string, value?: any) {
getConfig().set(key, value);
}
}
function getConfig(): CoreConfig {
const Ionic = (window as any).Ionic;
if (Ionic && Ionic.config) {
return Ionic.config;
}
return null;
}

View File

@ -12,3 +12,4 @@ export { PopoverController } from './popover-controller';
export { ToastController } from './toast-controller'; export { ToastController } from './toast-controller';
export { NavController } from './nav-controller'; export { NavController } from './nav-controller';
export { DomController } from './dom-controller'; export { DomController } from './dom-controller';
export { Config } from './config';

View File

@ -1,7 +1,8 @@
import { PlatformConfig } from '@ionic/core'; import { PlatformConfig } from '@ionic/core';
import { HostListener } from '@angular/core'; import { Injectable } from '@angular/core';
@Injectable()
export class Platform { export class Platform {
private _platforms: PlatformConfig[] = []; private _platforms: PlatformConfig[] = [];