From 194f3e4aad2a6c5a10aa41260919b72e00ffff0a Mon Sep 17 00:00:00 2001 From: Adam Bradley Date: Mon, 5 Oct 2015 15:09:16 -0500 Subject: [PATCH] fix(config): set user platform modes --- ionic/config/config.ts | 67 ++++++++++++++++++++------------ ionic/config/modes.ts | 4 +- ionic/config/test/config.spec.ts | 64 ++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 27 deletions(-) diff --git a/ionic/config/config.ts b/ionic/config/config.ts index 96468c174e..e6cbd43680 100644 --- a/ionic/config/config.ts +++ b/ionic/config/config.ts @@ -80,6 +80,7 @@ export class IonicConfig { get(key) { if (!isDefined(this._c[key])) { + // 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 @@ -87,14 +88,16 @@ export class IonicConfig { this._c[key] = null; let userPlatformValue = undefined; - let platformValue = undefined; let userDefaultValue = this._s[key]; - let modeValue = undefined; + let userPlatformModeValue = undefined; + let userDefaultModeValue = undefined; + let platformValue = undefined; + let platformModeValue = undefined; + let configObj = null; if (this._platform) { // check the platform settings object for this value // loop though each of the active platforms - let platformObj = null; // array of active platforms, which also knows the hierarchy, // with the last one the most important @@ -105,34 +108,54 @@ export class IonicConfig { // get user defined platform values if (this._s.platforms) { - platformObj = this._s.platforms[ activePlatformKeys[i] ]; - if (platformObj && isDefined(platformObj[key])) { - userPlatformValue = platformObj[key]; + configObj = this._s.platforms[ activePlatformKeys[i] ]; + if (configObj) { + if (isDefined(configObj[key])) { + userPlatformValue = configObj[key]; + } + configObj = IonicConfig.getModeConfig(configObj.mode); + if (configObj && isDefined(configObj[key])) { + userPlatformModeValue = configObj[key]; + } } } // get default platform's setting - platformObj = IonicPlatform.get(activePlatformKeys[i]); - if (platformObj && platformObj.settings) { + configObj = IonicPlatform.get(activePlatformKeys[i]); + if (configObj && configObj.settings) { - if (isDefined(platformObj.settings[key])) { + if (isDefined(configObj.settings[key])) { // found a setting for this platform - platformValue = platformObj.settings[key]; + platformValue = configObj.settings[key]; } - platformObj = IonicConfig.modeConfig(platformObj.settings.mode); - if (platformObj && isDefined(platformObj[key])) { + configObj = IonicConfig.getModeConfig(configObj.settings.mode); + if (configObj && isDefined(configObj[key])) { // found setting for this platform's mode - modeValue = platformObj[key]; + platformModeValue = configObj[key]; } } } + + if (this._s.mode) { + configObj = IonicConfig.getModeConfig(this._s.mode); + if (configObj && isDefined(configObj[key])) { + userDefaultModeValue = configObj[key]; + } + } + } // cache the value - this._c[key] = isDefined(userPlatformValue) ? userPlatformValue : isDefined(platformValue) ? platformValue : isDefined(userDefaultValue) ? userDefaultValue : isDefined(modeValue) ? modeValue : null; + 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 @@ -155,18 +178,12 @@ export class IonicConfig { this._platform = platform; } - static modeConfig(mode, config) { - const args = arguments; - - if (args.length === 2) { - // modeConfig('ios', {...}) - modeConfigs[mode] = extend(modeConfigs[mode] || {}, config); - - } else { - // modeConfig('ios') - return modeConfigs[mode]; - } + static setModeConfig(mode, config) { + modeConfigs[mode] = config; + } + static getModeConfig(mode) { + return modeConfigs[mode] || null; } } diff --git a/ionic/config/modes.ts b/ionic/config/modes.ts index 292e71b721..ea8cf8917d 100644 --- a/ionic/config/modes.ts +++ b/ionic/config/modes.ts @@ -3,7 +3,7 @@ import {IonicConfig} from './config'; // iOS Mode Settings -IonicConfig.modeConfig('ios', { +IonicConfig.setModeConfig('ios', { actionSheetEnter: 'action-sheet-slide-in', actionSheetLeave: 'action-sheet-slide-out', @@ -27,7 +27,7 @@ IonicConfig.modeConfig('ios', { // Material Design Mode Settings -IonicConfig.modeConfig('md', { +IonicConfig.setModeConfig('md', { actionSheetEnter: 'action-sheet-md-slide-in', actionSheetLeave: 'action-sheet-md-slide-out', diff --git a/ionic/config/test/config.spec.ts b/ionic/config/test/config.spec.ts index 15b987b315..250d04936e 100644 --- a/ionic/config/test/config.spec.ts +++ b/ionic/config/test/config.spec.ts @@ -2,6 +2,70 @@ import {IonicConfig, IonicPlatform} from 'ionic/ionic'; export function run() { + it('should override mode settings', () => { + let config = new IonicConfig({ + mode: 'md' + }); + let platform = new IonicPlatform(['ios']); + config.setPlatform(platform); + + expect(config.get('mode')).toEqual('md'); + expect(config.get('tabBarPlacement')).toEqual('top'); + }); + + it('should override mode settings from platforms setting', () => { + let config = new IonicConfig({ + platforms: { + ios: { + mode: 'md' + } + } + }); + let platform = new IonicPlatform(['ios']); + config.setPlatform(platform); + + expect(config.get('mode')).toEqual('md'); + expect(config.get('tabBarPlacement')).toEqual('top'); + }); + + it('should override mode platform', () => { + let config = new IonicConfig({ + mode: 'modeA', + platforms: { + mobile: { + mode: 'modeB' + }, + ios: { + mode: 'modeC' + } + } + }); + let platform = new IonicPlatform(['mobile']); + config.setPlatform(platform); + + expect(config.get('mode')).toEqual('modeB'); + }); + + it('should override mode', () => { + let config = new IonicConfig({ + mode: 'modeA' + }); + let platform = new IonicPlatform(['core']); + config.setPlatform(platform); + + expect(config.get('mode')).toEqual('modeA'); + }); + + it('should get user settings after user platform settings', () => { + let config = new IonicConfig({ + hoverCSS: true + }); + let platform = new IonicPlatform(['ios']); + config.setPlatform(platform); + + expect(config.get('hoverCSS')).toEqual(true); + }); + it('should get ios mode for core platform', () => { let config = new IonicConfig(); let platform = new IonicPlatform(['core']);