From db0049ff7396bbe54b7b7f53bd3f115d55377677 Mon Sep 17 00:00:00 2001 From: "Manu Mtz.-Almeida" Date: Sat, 11 Aug 2018 18:59:31 +0200 Subject: [PATCH] fix(config): add persistance mode fixes #15102 --- core/src/global/config.ts | 1 + core/src/global/ionic-global.ts | 11 ++++++++--- core/src/utils/config.ts | 10 ++++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/core/src/global/config.ts b/core/src/global/config.ts index a558b44a60..ff9009d0fb 100644 --- a/core/src/global/config.ts +++ b/core/src/global/config.ts @@ -6,6 +6,7 @@ export interface IonicConfig { * Possible values are: `"ios"` or `"md"`. */ mode?: Mode; + _persist?: boolean; isDevice?: boolean; statusbarPadding?: boolean; diff --git a/core/src/global/ionic-global.ts b/core/src/global/ionic-global.ts index d83b1aebdc..3a2da20fb5 100644 --- a/core/src/global/ionic-global.ts +++ b/core/src/global/ionic-global.ts @@ -1,6 +1,6 @@ import 'ionicons'; -import { configFromURL } from '../utils/config'; +import { configFromSession, configFromURL, saveConfig } from '../utils/config'; import { isIOS } from '../utils/platform'; import { Config } from './config'; @@ -16,10 +16,15 @@ Object.defineProperty(Ionic, 'queue', { // create the Ionic.config from raw config object (if it exists) // and convert Ionic.config into a ConfigApi that has a get() fn -const config = Ionic['config'] = Context['config'] = new Config({ +const configObj = { ...Ionic['config'], + ...configFromSession(), ...configFromURL() -}); +}; +const config = Ionic['config'] = Context['config'] = new Config(configObj); +if (config.getBoolean('_persist', false)) { + saveConfig(configObj); +} // first see if the mode was set as an attribute on // which could have been set by the user, or by prerendering diff --git a/core/src/utils/config.ts b/core/src/utils/config.ts index e064f62f54..1dd4da75ea 100644 --- a/core/src/utils/config.ts +++ b/core/src/utils/config.ts @@ -16,6 +16,16 @@ export function setupConfig(config: IonicConfig) { } const IONIC_PREFIX = 'ionic:'; +const IONIC_SESSION_KEY = 'ionic-persist-config'; + +export function configFromSession(): any { + const configStr = window.sessionStorage.getItem(IONIC_SESSION_KEY); + return configStr ? JSON.parse(configStr) : {}; +} + +export function saveConfig(config: any) { + window.sessionStorage.setItem(IONIC_SESSION_KEY, JSON.stringify(config)); +} export function configFromURL() { const config: any = {};