fix(config): add setupConfig util

This commit is contained in:
Manu Mtz.-Almeida
2018-04-24 01:25:03 +02:00
parent d06409a2bd
commit 0c1476e0ff
6 changed files with 78 additions and 64 deletions

View File

@ -1,34 +0,0 @@
export class Config {
private m: Map<string, any>;
constructor(configObj: {[key: string]: any}|undefined) {
this.m = new Map<string, any>(configObj ? Object.entries(configObj) : undefined);
}
get(key: string, fallback?: any): any {
const value = this.m.get(key);
return (value !== undefined) ? value : fallback;
}
getBoolean(key: string, fallback = false): boolean {
const val = this.m.get(key);
if (val === undefined) {
return fallback;
}
if (typeof val === 'string') {
return val === 'true';
}
return !!val;
}
getNumber(key: string, fallback?: number): number {
const val = parseFloat(this.m.get(key));
return isNaN(val) ? (fallback !== undefined ? fallback : NaN) : val;
}
set(key: string, value: any) {
this.m.set(key, value);
}
}

View File

@ -1,6 +1,6 @@
import 'ionicons'; import 'ionicons';
import { Config } from './config'; import { Config, configFromURL } from '../utils/config';
import { configFromURL, isIOS } from '../utils/platform'; import { isIOS } from '../utils/platform';
const Ionic = (window as any).Ionic = (window as any).Ionic || {}; const Ionic = (window as any).Ionic = (window as any).Ionic || {};
declare const Context: any; declare const Context: any;

View File

@ -1,13 +0,0 @@
export interface QueueController {
read: DomControllerCallback;
write: DomControllerCallback;
}
export interface RafCallback {
(timeStamp: number): void;
}
export interface DomControllerCallback {
(cb: RafCallback): void;
}

17
core/src/index.d.ts vendored
View File

@ -104,8 +104,7 @@ export { Toolbar } from './components/toolbar/toolbar';
// export all of the component declarations that are dynamically created // export all of the component declarations that are dynamically created
export * from './components'; export * from './components';
export { Config } from './global/config'; export { Config } from './utils/config';
export { QueueController, RafCallback } from './global/queue-controller';
export { FrameworkDelegate } from './utils/framework-delegate'; export { FrameworkDelegate } from './utils/framework-delegate';
export { OverlayEventDetail } from './utils/overlays'; export { OverlayEventDetail } from './utils/overlays';
export * from './utils/platform'; export * from './utils/platform';
@ -116,6 +115,20 @@ export type ComponentRef = Function | HTMLElement | string;
export type ComponentProps = {[key: string]: any}; export type ComponentProps = {[key: string]: any};
export type CssClassMap = { [className: string]: boolean }; export type CssClassMap = { [className: string]: boolean };
export interface QueueController {
read: DomControllerCallback;
write: DomControllerCallback;
}
export interface RafCallback {
(timeStamp: number): void;
}
export interface DomControllerCallback {
(cb: RafCallback): void;
}
declare global { declare global {
namespace JSXElements { namespace JSXElements {

61
core/src/utils/config.ts Normal file
View File

@ -0,0 +1,61 @@
export function setupConfig(config: {[key: string]: any}, win: any) {
const Ionic = win.Ionic;
if (Ionic && Ionic.config && Ionic.config.constructor.name !== 'Object') {
console.error('ionic config was already initialized');
return;
}
win.Ionic = win.Ionic || {};
win.Ionic.Config = {
...win.Ionic.Config,
...config
};
return win.Ionic.Config;
}
export function configFromURL(win: Window) {
const config: any = {};
win.location.search.slice(1)
.split('&')
.filter(entryText => entryText.startsWith('ionic:'))
.map(entryText => entryText.split('='))
.forEach(entry => {
config[entry[0].slice(6)] = decodeURIComponent(entry[1]);
});
return config;
}
export class Config {
private m: Map<string, any>;
constructor(configObj: {[key: string]: any}) {
this.m = new Map<string, any>(Object.entries(configObj));
}
get(key: string, fallback?: any): any {
const value = this.m.get(key);
return (value !== undefined) ? value : fallback;
}
getBoolean(key: string, fallback = false): boolean {
const val = this.m.get(key);
if (val === undefined) {
return fallback;
}
if (typeof val === 'string') {
return val === 'true';
}
return !!val;
}
getNumber(key: string, fallback?: number): number {
const val = parseFloat(this.m.get(key));
return isNaN(val) ? (fallback !== undefined ? fallback : NaN) : val;
}
set(key: string, value: any) {
this.m.set(key, value);
}
}

View File

@ -63,16 +63,3 @@ export function needInputShims(win: Window) {
export function testUserAgent(win: Window, expr: RegExp) { export function testUserAgent(win: Window, expr: RegExp) {
return expr.test(win.navigator.userAgent); return expr.test(win.navigator.userAgent);
} }
export function configFromURL(win: Window) {
const config: any = {};
win.location.search.slice(1)
.split('&')
.filter(entryText => entryText.startsWith('ionic:'))
.map(entryText => entryText.split('='))
.forEach(entry => {
config[entry[0].slice(6)] = decodeURIComponent(entry[1]);
});
return config;
}