Skip to content

Commit 0c1476e

Browse files
committed
fix(config): add setupConfig util
1 parent d06409a commit 0c1476e

File tree

6 files changed

+78
-64
lines changed

6 files changed

+78
-64
lines changed

core/src/global/config.ts

Lines changed: 0 additions & 34 deletions
This file was deleted.

core/src/global/ionic-global.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import 'ionicons';
2-
import { Config } from './config';
3-
import { configFromURL, isIOS } from '../utils/platform';
2+
import { Config, configFromURL } from '../utils/config';
3+
import { isIOS } from '../utils/platform';
44

55
const Ionic = (window as any).Ionic = (window as any).Ionic || {};
66
declare const Context: any;

core/src/global/queue-controller.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

core/src/index.d.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,7 @@ export { Toolbar } from './components/toolbar/toolbar';
104104
// export all of the component declarations that are dynamically created
105105
export * from './components';
106106

107-
export { Config } from './global/config';
108-
export { QueueController, RafCallback } from './global/queue-controller';
107+
export { Config } from './utils/config';
109108
export { FrameworkDelegate } from './utils/framework-delegate';
110109
export { OverlayEventDetail } from './utils/overlays';
111110
export * from './utils/platform';
@@ -116,6 +115,20 @@ export type ComponentRef = Function | HTMLElement | string;
116115
export type ComponentProps = {[key: string]: any};
117116
export type CssClassMap = { [className: string]: boolean };
118117

118+
export interface QueueController {
119+
read: DomControllerCallback;
120+
write: DomControllerCallback;
121+
}
122+
123+
export interface RafCallback {
124+
(timeStamp: number): void;
125+
}
126+
127+
export interface DomControllerCallback {
128+
(cb: RafCallback): void;
129+
}
130+
131+
119132
declare global {
120133

121134
namespace JSXElements {

core/src/utils/config.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
export function setupConfig(config: {[key: string]: any}, win: any) {
3+
const Ionic = win.Ionic;
4+
if (Ionic && Ionic.config && Ionic.config.constructor.name !== 'Object') {
5+
console.error('ionic config was already initialized');
6+
return;
7+
}
8+
win.Ionic = win.Ionic || {};
9+
win.Ionic.Config = {
10+
...win.Ionic.Config,
11+
...config
12+
};
13+
return win.Ionic.Config;
14+
}
15+
16+
export function configFromURL(win: Window) {
17+
const config: any = {};
18+
win.location.search.slice(1)
19+
.split('&')
20+
.filter(entryText => entryText.startsWith('ionic:'))
21+
.map(entryText => entryText.split('='))
22+
.forEach(entry => {
23+
config[entry[0].slice(6)] = decodeURIComponent(entry[1]);
24+
});
25+
26+
return config;
27+
}
28+
29+
export class Config {
30+
31+
private m: Map<string, any>;
32+
33+
constructor(configObj: {[key: string]: any}) {
34+
this.m = new Map<string, any>(Object.entries(configObj));
35+
}
36+
37+
get(key: string, fallback?: any): any {
38+
const value = this.m.get(key);
39+
return (value !== undefined) ? value : fallback;
40+
}
41+
42+
getBoolean(key: string, fallback = false): boolean {
43+
const val = this.m.get(key);
44+
if (val === undefined) {
45+
return fallback;
46+
}
47+
if (typeof val === 'string') {
48+
return val === 'true';
49+
}
50+
return !!val;
51+
}
52+
53+
getNumber(key: string, fallback?: number): number {
54+
const val = parseFloat(this.m.get(key));
55+
return isNaN(val) ? (fallback !== undefined ? fallback : NaN) : val;
56+
}
57+
58+
set(key: string, value: any) {
59+
this.m.set(key, value);
60+
}
61+
}

core/src/utils/platform.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,3 @@ export function needInputShims(win: Window) {
6363
export function testUserAgent(win: Window, expr: RegExp) {
6464
return expr.test(win.navigator.userAgent);
6565
}
66-
67-
export function configFromURL(win: Window) {
68-
const config: any = {};
69-
win.location.search.slice(1)
70-
.split('&')
71-
.filter(entryText => entryText.startsWith('ionic:'))
72-
.map(entryText => entryText.split('='))
73-
.forEach(entry => {
74-
config[entry[0].slice(6)] = decodeURIComponent(entry[1]);
75-
});
76-
77-
return config;
78-
}

0 commit comments

Comments
 (0)