forked from ionic-team/ionic-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.ts
More file actions
367 lines (322 loc) · 15.4 KB
/
config.ts
File metadata and controls
367 lines (322 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/**
* @ngdoc service
* @name Config
* @module ionic
* @description
* Config allows you to set the modes of your components
*/
import {Platform} from '../platform/platform';
import {isObject, isDefined, isFunction, isArray} from '../util/util';
/**
* @name Config
* @demo /docs/v2/demos/config/
* @description
* The Config lets you configure your entire app or specific platforms.
* You can set the tab placement, icon mode, animations, and more here.
*
* ```ts
* import {ionicBootstrap} from 'ionic-angular';
*
* ionicBootstrap(AppRoot, customProviders, {
* backButtonText: 'Go Back',
* iconMode: 'ios',
* modalEnter: 'modal-slide-in',
* modalLeave: 'modal-slide-out',
* tabsPlacement: 'bottom',
* pageTransition: 'ios',
* });
* ```
*
*
* Config can be overwritten at multiple levels allowing for more granular configuration.
* Below is an example where an app can override any setting we want based on a platform.
*
* ```ts
* import {ionicBootstrap} from 'ionic-angular';
*
* ionicBootstrap(AppRoot, customProviders, {
* tabsPlacement: 'bottom',
* platforms: {
* ios: {
* tabsPlacement: 'top',
* }
* });
* ```
*
* We could also configure these values at a component level. Take `tabsPlacement`,
* we can configure this as a property on our `ion-tabs`.
*
* ```html
* <ion-tabs tabsPlacement="top">
* <ion-tab tabTitle="Dash" tabIcon="pulse" [root]="tabRoot"></ion-tab>
* </ion-tabs>
* ```
*
* The last way we could configure is through URL query strings. This is useful for testing
* while in the browser. Simply add `?ionic<PROPERTYNAME>=<value>` to the url.
*
* ```bash
* http://localhost:8100/?ionicTabsPlacement=bottom
* ```
*
* Any value can be added to config, and looked up at a later in any component.
*
* ```js
* config.set('ios', 'favoriteColor', 'green');
*
* // from any page in your app:
* config.get('favoriteColor'); // 'green' when iOS
* ```
*
*
* A config value can come from anywhere and be anything, but there are default
* values for each mode. The [theming](../../../theming/platform-specific-styles/)
* documentation has a chart of the default mode configuration. The following
* chart displays each property with a description of what it controls.
*
*
* | Config Property | Type | Details |
* |--------------------------|---------------------|--------------------------------------------------------------------------------------------------------------------------------------------------|
* | `activator` | `string` | Used for buttons, changes the effect of pressing on a button. Available options: `"ripple"`, `"highlight"`. |
* | `actionSheetEnter` | `string` | The name of the transition to use while an action sheet is presented. |
* | `actionSheetLeave` | `string` | The name of the transition to use while an action sheet is dismissed. |
* | `alertEnter` | `string` | The name of the transition to use while an alert is presented. |
* | `alertLeave` | `string` | The name of the transition to use while an alert is dismissed. |
* | `backButtonText` | `string` | The text to display by the back button icon in the navbar. |
* | `backButtonIcon` | `string` | The icon to use as the back button icon. |
* | `iconMode` | `string` | The mode to use for all icons throughout the application. Available options: `"ios"`, `"md"` |
* | `loadingEnter` | `string` | The name of the transition to use while a loading indicator is presented. |
* | `loadingLeave` | `string` | The name of the transition to use while a loading indicator is dismissed. |
* | `menuType` | `string` | Type of menu to display. Available options: `"overlay"`, `"reveal"`, `"push"`. |
* | `modalEnter` | `string` | The name of the transition to use while a modal is presented. |
* | `modalLeave` | `string` | The name of the transition to use while a modal is dismiss. |
* | `pageTransition` | `string` | The name of the transition to use while changing pages. |
* | `pageTransitionDelay` | `number` | The delay in milliseconds before the transition starts while changing pages. |
* | `pickerEnter` | `string` | The name of the transition to use while a picker is presented. |
* | `pickerLeave` | `string` | The name of the transition to use while a picker is dismissed. |
* | `popoverEnter` | `string` | The name of the transition to use while a popover is presented. |
* | `popoverLeave` | `string` | The name of the transition to use while a popover is dismissed. |
* | `prodMode` | `boolean` | Disable development mode, which turns off assertions and other checks within the framework. One important assertion this disables verifies that a change detection pass does not result in additional changes to any bindings (also known as unidirectional data flow).
* | `spinner` | `string` | The default spinner to use when a name is not defined. |
* | `tabsHighlight` | `boolean` | Whether to show a highlight line under the tab when it is selected. |
* | `tabsLayout` | `string` | The layout to use for all tabs. Available options: `"icon-top"`, `"icon-left"`, `"icon-right"`, `"icon-bottom"`, `"icon-hide"`, `"title-hide"`. |
* | `tabsPlacement` | `string` | The position of the tabs relative to the content. Available options: `"top"`, `"bottom"` |
* | `toastEnter` | `string` | The name of the transition to use while a toast is presented. |
* | `toastLeave` | `string` | The name of the transition to use while a toast is dismissed. |
*
**/
export class Config {
private _c: any = {};
private _s: any = {};
/**
* @private
*/
platform: Platform;
constructor(config?: any) {
this._s = config && isObject(config) && !isArray(config) ? config : {};
}
/**
* @name get
* @description
* Returns a single config value, given a key.
*
* @param {string} [key] - the key for the config value
* @param {any} [fallbackValue] - a fallback value to use when the config
* value was not found, or is config value is `null`. Fallback value
* defaults to `null`.
*/
get(key: string, fallbackValue: any = null): any {
if (!isDefined(this._c[key])) {
if (!isDefined(key)) {
throw 'config key is not defined';
}
// if the value was already set this will all be skipped
// if there was no user config then it'll check each of
// the user config's platforms, which already contains
// settings from default platform configs
let userPlatformValue: any = undefined;
let userDefaultValue: any = this._s[key];
let userPlatformModeValue: any = undefined;
let userDefaultModeValue: any = undefined;
let platformValue: any = undefined;
let platformModeValue: any = undefined;
let configObj: any = null;
if (this.platform) {
let queryStringValue = this.platform.query('ionic' + key.toLowerCase());
if (isDefined(queryStringValue)) {
return this._c[key] = (queryStringValue === 'true' ? true : queryStringValue === 'false' ? false : queryStringValue);
}
// check the platform settings object for this value
// loop though each of the active platforms
// array of active platforms, which also knows the hierarchy,
// with the last one the most important
let activePlatformKeys = this.platform.platforms();
// loop through all of the active platforms we're on
for (let i = 0, l = activePlatformKeys.length; i < l; i++) {
// get user defined platform values
if (this._s.platforms) {
configObj = this._s.platforms[activePlatformKeys[i]];
if (configObj) {
if (isDefined(configObj[key])) {
userPlatformValue = configObj[key];
}
configObj = Config.getModeConfig(configObj.mode);
if (configObj && isDefined(configObj[key])) {
userPlatformModeValue = configObj[key];
}
}
}
// get default platform's setting
configObj = Platform.get(activePlatformKeys[i]);
if (configObj && configObj.settings) {
if (isDefined(configObj.settings[key])) {
// found a setting for this platform
platformValue = configObj.settings[key];
}
configObj = Config.getModeConfig(configObj.settings.mode);
if (configObj && isDefined(configObj[key])) {
// found setting for this platform's mode
platformModeValue = configObj[key];
}
}
}
}
configObj = Config.getModeConfig(this._s.mode);
if (configObj && isDefined(configObj[key])) {
userDefaultModeValue = configObj[key];
}
// cache the value
this._c[key] = isDefined(userPlatformValue) ? userPlatformValue :
isDefined(userDefaultValue) ? userDefaultValue :
isDefined(userPlatformModeValue) ? userPlatformModeValue :
isDefined(userDefaultModeValue) ? userDefaultModeValue :
isDefined(platformValue) ? platformValue :
isDefined(platformModeValue) ? platformModeValue :
null;
}
// return key's value
// either it came directly from the user config
// or it was from the users platform configs
// or it was from the default platform configs
// in that order
let rtnVal: any;
if (isFunction(this._c[key])) {
rtnVal = this._c[key](this.platform);
} else {
rtnVal = this._c[key];
}
return (rtnVal !== null ? rtnVal : fallbackValue);
}
/**
* @name getBoolean
* @description
* Same as `get()`, however always returns a boolean value. If the
* value from `get()` is `null`, then it'll return the `fallbackValue`
* which defaults to `false`. Otherwise, `getBoolean()` will return
* if the config value is truthy or not. It also returns `true` if
* the config value was the string value `"true"`.
* @param {string} [key] - the key for the config value
* @param {boolean} [fallbackValue] - a fallback value to use when the config
* value was `null`. Fallback value defaults to `false`.
*/
getBoolean(key: string, fallbackValue: boolean = false): boolean {
let val = this.get(key);
if (val === null) {
return fallbackValue;
}
if (typeof val === 'string') {
return val === 'true';
}
return !!val;
}
/**
* @name getNumber
* @description
* Same as `get()`, however always returns a number value. Uses `parseFloat()`
* on the value received from `get()`. If the result from the parse is `NaN`,
* then it will return the value passed to `fallbackValue`. If no fallback
* value was provided then it'll default to returning `NaN` when the result
* is not a valid number.
* @param {string} [key] - the key for the config value
* @param {number} [fallbackValue] - a fallback value to use when the config
* value turned out to be `NaN`. Fallback value defaults to `NaN`.
*/
getNumber(key: string, fallbackValue: number = NaN): number {
let val = parseFloat( this.get(key) );
return isNaN(val) ? fallbackValue : val;
}
/**
* @name set
* @description
* Sets a single config value.
*
* @param {string} [platform] - The platform (either 'ios' or 'android') that the config value should apply to. Leaving this blank will apply the config value to all platforms.
* @param {string} [key] - The key used to look up the value at a later point in time.
* @param {string} [value] - The config value being stored.
*/
set(...args: any[]) {
const arg0 = args[0];
const arg1 = args[1];
switch (args.length) {
case 2:
// set('key', 'value') = set key/value pair
// arg1 = value
this._s[arg0] = arg1;
delete this._c[arg0]; // clear cache
break;
case 3:
// setting('ios', 'key', 'value') = set key/value pair for platform
// arg0 = platform
// arg1 = key
// arg2 = value
this._s.platforms = this._s.platforms || {};
this._s.platforms[arg0] = this._s.platforms[arg0] || {};
this._s.platforms[arg0][arg1] = args[2];
delete this._c[arg1]; // clear cache
break;
}
return this;
}
/**
* @private
* @name settings()
* @description
*/
settings(arg0?: any, arg1?: any) {
switch (arguments.length) {
case 0:
return this._s;
case 1:
// settings({...})
this._s = arg0;
this._c = {}; // clear cache
break;
case 2:
// settings('ios', {...})
this._s.platforms = this._s.platforms || {};
this._s.platforms[arg0] = arg1;
this._c = {}; // clear cache
break;
}
return this;
}
/**
* @private
*/
setPlatform(platform: Platform) {
this.platform = platform;
}
/**
* @private
*/
static setModeConfig(mode: string, config: any) {
modeConfigs[mode] = config;
}
/**
* @private
*/
static getModeConfig(mode: string) {
return modeConfigs[mode] || null;
}
}
let modeConfigs: any = {};