forked from EVOgeek/Vmeda.Online
		
	MOBILE-4080 core: Improve devtools settings
This commit is contained in:
		
							parent
							
								
									d121fa2a2d
								
							
						
					
					
						commit
						0c1b244055
					
				| @ -14,6 +14,7 @@ | ||||
| 
 | ||||
| import envJson from '@/assets/env.json'; | ||||
| import { EnvironmentConfig } from '@/types/config'; | ||||
| import { CoreBrowser } from '@singletons/browser'; | ||||
| 
 | ||||
| /** | ||||
|  * Context levels enumeration. | ||||
| @ -154,7 +155,8 @@ export class CoreConstants { | ||||
|         // @todo [4.0] This is not the proper way to check for development tools, we should rely only on the BUILD variable.
 | ||||
|         return this.BUILD.isDevelopment | ||||
|             || this.BUILD.isTesting | ||||
|             || this.CONFIG.versionname.includes('-dev'); | ||||
|             || this.CONFIG.versionname.includes('-dev') | ||||
|             || CoreBrowser.hasDevelopmentSetting('DevTools'); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
|  | ||||
| @ -16,9 +16,11 @@ import { CoreApp, CoreAppProvider } from '@services/app'; | ||||
| import { CoreConfig, CoreConfigProvider } from '@services/config'; | ||||
| import { CoreDB, CoreDbProvider } from '@services/db'; | ||||
| import { CoreCustomURLSchemes, CoreCustomURLSchemesProvider } from '@services/urlschemes'; | ||||
| import { CoreBrowser } from '@singletons/browser'; | ||||
| import { CoreConstants } from '../constants'; | ||||
| 
 | ||||
| type DevelopmentWindow = Window & { | ||||
|     browser?: typeof CoreBrowser; | ||||
|     appProvider?: CoreAppProvider; | ||||
|     configProvider?: CoreConfigProvider; | ||||
|     dbProvider?: CoreDbProvider; | ||||
| @ -26,6 +28,7 @@ type DevelopmentWindow = Window & { | ||||
| }; | ||||
| 
 | ||||
| function initializeDevelopmentWindow(window: DevelopmentWindow) { | ||||
|     window.browser = CoreBrowser; | ||||
|     window.appProvider = CoreApp.instance; | ||||
|     window.configProvider = CoreConfig.instance; | ||||
|     window.dbProvider = CoreDB.instance; | ||||
|  | ||||
| @ -201,11 +201,11 @@ export class CoreConfigProvider { | ||||
|      * Load development config overrides. | ||||
|      */ | ||||
|     protected loadDevelopmentConfig(): void { | ||||
|         if (!CoreConstants.enableDevTools() || !CoreBrowser.hasCookie('MoodleAppConfig')) { | ||||
|         if (!CoreConstants.enableDevTools() || !CoreBrowser.hasDevelopmentSetting('Config')) { | ||||
|             return; | ||||
|         } | ||||
| 
 | ||||
|         this.patchEnvironment(JSON.parse(CoreBrowser.getCookie('MoodleAppConfig') ?? '{}'), { patchDefault: true }); | ||||
|         this.patchEnvironment(JSON.parse(CoreBrowser.getDevelopmentSetting('Config') ?? '{}'), { patchDefault: true }); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|  | ||||
| @ -36,7 +36,7 @@ export class CoreDbProvider { | ||||
|      * @returns Whether queries should be logged. | ||||
|      */ | ||||
|     loggingEnabled(): boolean { | ||||
|         return CoreBrowser.hasCookie('MoodleAppDBLoggingEnabled') || CoreAppProvider.isAutomated(); | ||||
|         return CoreBrowser.hasDevelopmentSetting('DBLoggingEnabled') || CoreAppProvider.isAutomated(); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|  | ||||
| @ -27,6 +27,28 @@ export class CoreBrowser { | ||||
|         return new RegExp(`(\\s|;|^)${name}=`).test(document.cookie ?? ''); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Check whether a development setting is set. | ||||
|      * | ||||
|      * @param name Setting name. | ||||
|      * @returns Whether the development setting is set. | ||||
|      */ | ||||
|     static hasDevelopmentSetting(name: string): boolean { | ||||
|         const setting = this.getDevelopmentSettingKey(name); | ||||
| 
 | ||||
|         return this.hasCookie(setting) || this.hasLocalStorage(setting); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Check whether the given localStorage key is set. | ||||
|      * | ||||
|      * @param key localStorage key. | ||||
|      * @returns Whether the key is set. | ||||
|      */ | ||||
|     static hasLocalStorage(key: string): boolean { | ||||
|         return localStorage.getItem(key) !== null; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Read a cookie. | ||||
|      * | ||||
| @ -45,4 +67,60 @@ export class CoreBrowser { | ||||
|         return cookies[name] ?? null; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Read a localStorage key. | ||||
|      * | ||||
|      * @param key localStorage key. | ||||
|      * @return localStorage value. | ||||
|      */ | ||||
|     static getLocalStorage(key: string): string | null { | ||||
|         return localStorage.getItem(key); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Get development setting value. | ||||
|      * | ||||
|      * @param name Setting name. | ||||
|      * @returns Development setting value. | ||||
|      */ | ||||
|     static getDevelopmentSetting(name: string): string | null { | ||||
|         const setting = this.getDevelopmentSettingKey(name); | ||||
| 
 | ||||
|         return this.getCookie(setting) ?? this.getLocalStorage(setting); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Set development setting. | ||||
|      * | ||||
|      * @param name Setting name. | ||||
|      * @param value Setting value. | ||||
|      */ | ||||
|     static setDevelopmentSetting(name: string, value: string): void { | ||||
|         const setting = this.getDevelopmentSettingKey(name); | ||||
| 
 | ||||
|         document.cookie = `${setting}=${value};path=/`; | ||||
|         localStorage.setItem(setting, value); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Unset development setting. | ||||
|      * | ||||
|      * @param name Setting name. | ||||
|      */ | ||||
|     static clearDevelopmentSetting(name: string): void { | ||||
|         const setting = this.getDevelopmentSettingKey(name); | ||||
| 
 | ||||
|         document.cookie = `${setting}=;path=/;expires=Thu, 01 Jan 1970 00:00:01 GMT`; | ||||
|         localStorage.removeItem(setting); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Get development setting key. | ||||
|      * | ||||
|      * @param name Development setting name. | ||||
|      */ | ||||
|     protected static getDevelopmentSettingKey(name: string): string { | ||||
|         return `MoodleApp${name}`; | ||||
|     } | ||||
| 
 | ||||
| } | ||||
|  | ||||
| @ -69,7 +69,7 @@ export class CoreLogger { | ||||
|     static getInstance(className: string): CoreLogger { | ||||
|         // Disable log on production and testing.
 | ||||
|         if ( | ||||
|             !CoreBrowser.hasCookie('MoodleAppLoggingEnabled') && | ||||
|             !CoreBrowser.hasDevelopmentSetting('LoggingEnabled') && | ||||
|             (CoreConstants.BUILD.isProduction || CoreConstants.BUILD.isTesting) | ||||
|         ) { | ||||
|             if (CoreConstants.BUILD.isProduction) { | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user