import { GlobalSettings } from './GlobalSettings' ; import { EventGroup } from './EventGroup' ; export type Settings = { [ key ] : any ; } ; export type SettingsFunction = ( settings : Settings ) => Settings ; export interface ICustomizations { settings : Settings ; scopedSettings : { [ key ] : Settings ; } ; inCustomizerContext ? : boolean ; } const CustomizationsGlobalKey = 'customizations' ; const NO_CUSTOMIZATIONS = { settings : { } , scopedSettings : { } , inCustomizerContext : false } ; let _allSettings = GlobalSettings . getValue < ICustomizations > ( CustomizationsGlobalKey , { settings : { } , scopedSettings : { } , inCustomizerContext : false } ) ; const _events = new EventGroup ( _allSettings ) ; export class Customizations { public static reset ( ) { _allSettings . settings = { } ; _allSettings . scopedSettings = { } ; } public static applySettings ( settings ) { _allSettings . settings = { ... _allSettings . settings , ... settings } ; Customizations . _raiseChange ( ) ; } public static applyScopedSettings ( scopeName , settings ) { _allSettings . scopedSettings [ scopeName ] = { ... _allSettings . scopedSettings [ scopeName ] , ... settings } ; Customizations . _raiseChange ( ) ; } public static getSettings ( properties , scopeName ? , localSettings = NO_CUSTOMIZATIONS ) { const settings = { } ; const localScopedSettings = ( scopeName && localSettings . scopedSettings [ scopeName ] ) || { } ; const globalScopedSettings = ( scopeName && _allSettings . scopedSettings [ scopeName ] ) || { } ; for ( let property of properties ) { settings [ property ] = localScopedSettings [ property ] || localSettings . settings [ property ] || globalScopedSettings [ property ] || _allSettings . settings [ property ] ; } return settings ; } public static observe ( onChange ) { _events . on ( _allSettings , 'change' , onChange ) ; } public static unobserve ( onChange ) { _events . off ( _allSettings , 'change' , onChange ) ; } private static _raiseChange ( ) { _events . raise ( 'change' ) ; } }