Skip to content

Commit

Permalink
fix(angular): Config provider
Browse files Browse the repository at this point in the history
  • Loading branch information
manucorporat committed Apr 20, 2018
1 parent 2b3c14b commit c87f0c5
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
16 changes: 13 additions & 3 deletions angular/src/module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ModuleWithProviders, NgModule } from '@angular/core';
import { InjectionToken, ModuleWithProviders, NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import * as c from './directives';
Expand Down Expand Up @@ -119,7 +119,7 @@ const PROVIDERS = [
p.NavController,
p.Platform,
p.Events,
p.DomController
p.DomController,
];

@NgModule({
Expand All @@ -135,12 +135,22 @@ const PROVIDERS = [
]
})
export class IonicModule {
static forRoot(): ModuleWithProviders {
static forRoot(config?: {[key: string]: any}): ModuleWithProviders {
return {
ngModule: IonicModule,
providers: [
// Load config with defualt values
{ provide: ConfigToken, useValue: config },
{ provide: p.Config, useFactory: setupConfig, deps: [ ConfigToken ] },

...PROVIDERS,
]
};
}
}

export const ConfigToken = new InjectionToken<any>('USERCONFIG');
export function setupConfig(userConfig: any): p.Config {
const config = new p.Config(userConfig);
return config;
}
40 changes: 40 additions & 0 deletions angular/src/providers/config.ts
Original file line number Diff line number Diff line change
@@ -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;
}
1 change: 1 addition & 0 deletions angular/src/providers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export { PopoverController } from './popover-controller';
export { ToastController } from './toast-controller';
export { NavController } from './nav-controller';
export { DomController } from './dom-controller';
export { Config } from './config';
3 changes: 2 additions & 1 deletion angular/src/providers/platform.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

import { PlatformConfig } from '@ionic/core';
import { HostListener } from '@angular/core';
import { Injectable } from '@angular/core';

@Injectable()
export class Platform {

private _platforms: PlatformConfig[] = [];
Expand Down

0 comments on commit c87f0c5

Please sign in to comment.