This commit is contained in:
Adam Bradley
2015-06-25 10:52:13 -05:00
parent 3be98eb378
commit 1e5035c42d
4 changed files with 78 additions and 51 deletions

View File

@ -146,7 +146,11 @@ export function ionicBootstrap(ComponentType, config) {
let platform = Platform.create(app); let platform = Platform.create(app);
config = config || new IonicConfig(); config = config || new IonicConfig();
config.platform(platform);
// copy default platform settings into the user config platform settings
// user config platform settings should override default platform settings
config.setPlatform(platform);
GlobalIonicConfig = config; GlobalIonicConfig = config;

View File

@ -152,8 +152,13 @@ export function main(ionicBootstrap) {
let myConfig = new IonicConfig(); let myConfig = new IonicConfig();
//myConfig.setting('someKey', 'userConfig');
// myConfig.setting('ios', 'someKey', 'iosConfig');
// myConfig.setting('ipad', 'someKey', 'ipadConfig');
ionicBootstrap(MyApp, myConfig).then(root => { ionicBootstrap(MyApp, myConfig).then(root => {
console.log('someKey', myConfig.setting('someKey'));
console.log(myConfig.setting('mode')); console.log(myConfig.setting('mode'));
console.log('mobile', root.platform.is('mobile')) console.log('mobile', root.platform.is('mobile'))

View File

@ -1,14 +1,10 @@
import {isObject, isDefined} from '../util/util'; import {isString, isObject, isDefined, extend} from '../util/util';
export class IonicConfig { export class IonicConfig {
constructor(settings={}) { constructor(settings) {
this.setting(settings); this.setting(settings || {});
}
platform(platform) {
this._platform = platform;
} }
setting() { setting() {
@ -33,37 +29,43 @@ export class IonicConfig {
// setting({...}) = set settings object // setting({...}) = set settings object
// arg0 = setting object // arg0 = setting object
this._settings = arg0; this._settings = arg0;
break; return this;
} }
// time for the big show, get the value // arg0 must be a string to get a property
// setting('key') = get value if (isString(arg0)) {
// arg0 = key // time for the big show, get the value
if (isDefined(s[arg0])) { // setting('key') = get value
// value found in users settings object // arg0 = key
return s[arg0];
}
// check the users platform settings object if (!isDefined(s[arg0])) {
// loop though each of the active platforms // if the value was already set this will all be skipped
let activePlatformKeys = this._platform.platforms(); // if there was no user config then it'll check each of
let platformSettings = s.platforms; // the user config's platforms, which already contains
if (platformSettings) { // settings from default platform configs
let platformValue = undefined; s[arg0] = null;
for (let i = 0; i < activePlatformKeys.length; i++) {
if ( platformSettings[ activePlatformKeys[i] ] ) { // check the platform settings object for this value
platformValue = platformSettings[ activePlatformKeys[i] ][arg0]; // loop though each of the active platforms
let activePlatformKeys = this._platforms;
let platformSettings = s.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)) {
s[arg0] = platformValue;
}
} }
} }
if (isDefined(platformValue)) {
return platformValue;
}
}
// check the value from the default platform settings // return value.
platformSettings = this._platform.settings(); return s[arg0];
if (isDefined(platformSettings[arg0])) {
return platformSettings[arg0];
} }
// idk // idk
@ -73,21 +75,20 @@ export class IonicConfig {
case 2: case 2:
// setting('ios', {...}) = set platform config object // setting('ios', {...}) = set platform config object
// setting('key', 'value') = set key/value pair // setting('key', 'value') = set key/value pair
if (isObject(arg1)) { if (isObject(arg1)) {
// setting('ios', {...}) = set platform config object // setting('ios', {...}) = set platform config object
// arg0 = platform // arg0 = platform
// arg1 = platform config object // arg1 = platform config object
s.platforms = s.platforms || {}; s.platforms = s.platforms || {};
s.platforms[arg0] = arg1; s.platforms[arg0] = arg1;
break;
}
// setting('key', 'value') = set key/value pair } else {
// arg0 = key // setting('key', 'value') = set key/value pair
// arg1 = value // arg0 = key
s[arg0] = arg1; // arg1 = value
break; s[arg0] = arg1;
}
return this;
case 3: case 3:
@ -98,9 +99,21 @@ export class IonicConfig {
s.platforms = s.platforms || {}; s.platforms = s.platforms || {};
s.platforms[arg0] = s.platforms[arg0] || {}; s.platforms[arg0] = s.platforms[arg0] || {};
s.platforms[arg0][arg1] = args[2]; s.platforms[arg0][arg1] = args[2];
return this;
} }
} }
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 = this._settings.platforms || {};
this._settings.platforms = extend(platform.settings(), this._settings.platforms);
}
} }

View File

@ -12,13 +12,6 @@ export class Platform {
return (this._platforms.indexOf(platformName) > -1); return (this._platforms.indexOf(platformName) > -1);
} }
platforms(val) {
if (arguments.length) {
this._platforms = val;
}
return this._platforms;
}
settings(val) { settings(val) {
if (arguments.length) { if (arguments.length) {
this._settings = val; this._settings = val;
@ -39,6 +32,12 @@ export class Platform {
this._platforms.push(platformName); this._platforms.push(platformName);
} }
platforms() {
// get the array of active platforms, which also knows the hierarchy,
// with the last one the most important
return this._platforms;
}
/* Static Methods */ /* Static Methods */
static create(app) { static create(app) {
@ -81,17 +80,23 @@ export class Platform {
let platform = new Platform(); let platform = new Platform();
if (rootNode) { if (rootNode) {
let platformNode = rootNode.child(); let platformNode = rootNode;
while (platformNode) { while (platformNode) {
insertSuperset(platformNode); insertSuperset(platformNode);
platformNode = platformNode.child(); platformNode = platformNode.child();
} }
platformNode = rootNode.child(); platformNode = rootNode;
let settings = {}; let settings = {};
while (platformNode) { while (platformNode) {
// set the array of active platforms with
// the last one in the array the most important
platform.add(platformNode.name()); platform.add(platformNode.name());
util.extend(settings, platformNode.settings());
// copy default platform settings into this platform settings obj
settings[platformNode.name()] = util.extend({}, platformNode.settings());
// go to the next child
platformNode = platformNode.child(); platformNode = platformNode.child();
} }