mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-18 19:21:34 +08:00
146 lines
4.0 KiB
TypeScript
146 lines
4.0 KiB
TypeScript
|
|
import {isObject, isDefined, isFunction, extend} from '../util/util';
|
|
|
|
/**
|
|
* This is the Ionic Config
|
|
* @usage this is what you do to use it
|
|
*/
|
|
export class IonicConfig {
|
|
|
|
constructor(settings) {
|
|
|
|
// defaults
|
|
this._settings = {};
|
|
|
|
// override defaults w/ user config
|
|
if (settings) {
|
|
extend(this._settings, settings);
|
|
}
|
|
}
|
|
/**
|
|
* @description The settings description
|
|
*/
|
|
setting() {
|
|
const args = arguments;
|
|
const arg0 = args[0];
|
|
const arg1 = args[1];
|
|
|
|
let settings = this._settings;
|
|
|
|
switch (args.length) {
|
|
|
|
case 0:
|
|
// setting() = get settings object
|
|
return settings;
|
|
|
|
|
|
case 1:
|
|
// setting({...}) = set settings object
|
|
// setting('key') = get value
|
|
|
|
if (isObject(arg0)) {
|
|
// setting({...}) = set settings object
|
|
// arg0 = setting object
|
|
this._settings = arg0;
|
|
return this;
|
|
}
|
|
|
|
// time for the big show, get the value
|
|
// setting('key') = get value
|
|
// arg0 = key
|
|
|
|
if (!isDefined(settings[arg0])) {
|
|
// 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
|
|
settings[arg0] = null;
|
|
|
|
// check the platform settings object for this value
|
|
// loop though each of the active platforms
|
|
let activePlatformKeys = this._platforms;
|
|
let platformSettings = settings.platforms;
|
|
let platformObj = null;
|
|
if (platformSettings) {
|
|
let platformValue = undefined;
|
|
for (let i = 0; i < activePlatformKeys.length; i++) {
|
|
platformObj = platformSettings[ activePlatformKeys[i] ];
|
|
if (platformObj && isDefined(platformObj[arg0])) {
|
|
platformValue = platformObj[arg0];
|
|
}
|
|
}
|
|
if (isDefined(platformValue)) {
|
|
settings[arg0] = platformValue;
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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
|
|
if (isFunction(settings[arg0])) {
|
|
settings[arg0] = settings[arg0]();
|
|
}
|
|
return settings[arg0];
|
|
|
|
|
|
case 2:
|
|
// setting('ios', {...}) = set platform config object
|
|
// setting('key', 'value') = set key/value pair
|
|
if (isObject(arg1)) {
|
|
// setting('ios', {...}) = set platform config object
|
|
// arg0 = platform
|
|
// arg1 = platform config object
|
|
settings.platforms = settings.platforms || {};
|
|
settings.platforms[arg0] = arg1;
|
|
|
|
} else {
|
|
// setting('key', 'value') = set key/value pair
|
|
// arg0 = key
|
|
// arg1 = value
|
|
settings[arg0] = arg1;
|
|
}
|
|
return this;
|
|
|
|
|
|
case 3:
|
|
// setting('ios', 'key', 'value') = set key/value pair for platform
|
|
// arg0 = platform
|
|
// arg1 = key
|
|
// arg2 = value
|
|
settings.platforms = settings.platforms || {};
|
|
settings.platforms[arg0] = settings.platforms[arg0] || {};
|
|
settings.platforms[arg0][arg1] = args[2];
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* The setPlatform description
|
|
*/
|
|
setPlatform(platform) {
|
|
// get the array of active platforms, which also knows the hierarchy,
|
|
// with the last one the most important
|
|
this._platforms = platform.platforms();
|
|
|
|
// copy default platform settings into the user config platform settings
|
|
// user config platform settings should override default platform settings
|
|
this._settings.platforms = extend(platform.settings(), this._settings.platforms || {});
|
|
}
|
|
|
|
static set global(config) {
|
|
globalConfig = config;
|
|
}
|
|
|
|
static get global() {
|
|
return globalConfig;
|
|
}
|
|
|
|
}
|
|
|
|
let globalConfig = null;
|