1/** 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import { ILayoutConfig } from './ILayoutConfig'; 17import { CommonConstants } from '../constants/CommonConstants'; 18 19/** 20 * Layout configuration management 21 * There are currently three types of layout management: 22 * 1.Layout mode management, such as grid/list layout, such layout configuration can be easily converted into setting items. 23 * 2.Layout style management, such as layout parameters such as margins, sizes, colors, etc., used to configure adjustable layout styles. 24 * 3.Functional layout management, such as layout management of desktop layouts. 25 * Main features provided: 26 * 1.Save and manage all configuration objects. 27 * 2.Ability to query configuration values at three tiers (Product > Features > Public). 28 * 3.the ability to persist certain configuration values. 29 */ 30class LayoutConfigManager { 31 private readonly mCommonConfig: ILayoutConfig[][] = new Array<ILayoutConfig[]>(); 32 33 private readonly mFeatureConfig: ILayoutConfig[][] = new Array<ILayoutConfig[]>(); 34 35 private readonly mProductConfig: ILayoutConfig[][] = new Array<ILayoutConfig[]>(); 36 37 private constructor() { 38 this.resetConfigArray(); 39 } 40 41 private resetConfigArray(): void { 42 this.initConfigArray(this.mCommonConfig); 43 this.initConfigArray(this.mFeatureConfig); 44 this.initConfigArray(this.mProductConfig); 45 } 46 47 private initConfigArray(configArr: ILayoutConfig[][]): void { 48 configArr[CommonConstants.LAYOUT_CONFIG_TYPE_MODE] = new Array<ILayoutConfig>(); 49 configArr[CommonConstants.LAYOUT_CONFIG_TYPE_STYLE] = new Array<ILayoutConfig>(); 50 configArr[CommonConstants.LAYOUT_CONFIG_TYPE_FUNCTION] = new Array<ILayoutConfig>(); 51 } 52 53 /** 54 * Get the instance of the configuration management class 55 */ 56 static getInstance(): LayoutConfigManager { 57 if (globalThis.LayoutConfigManager == null) { 58 globalThis.LayoutConfigManager = new LayoutConfigManager(); 59 } 60 return globalThis.LayoutConfigManager; 61 } 62 63 /** 64 * Add configuration objects to the configuration management class 65 */ 66 addConfigToManager(config: ILayoutConfig): void { 67 const configLevel = config.getConfigLevel(); 68 let targetConfigType = null; 69 switch (configLevel) { 70 case CommonConstants.LAYOUT_CONFIG_LEVEL_COMMON: 71 targetConfigType = this.mCommonConfig[config.getConfigType()]; 72 break; 73 case CommonConstants.LAYOUT_CONFIG_LEVEL_FEATURE: 74 targetConfigType = this.mFeatureConfig[config.getConfigType()]; 75 break; 76 case CommonConstants.LAYOUT_CONFIG_LEVEL_PRODUCT: 77 targetConfigType = this.mProductConfig[config.getConfigType()]; 78 break; 79 default: 80 break; 81 } 82 if (targetConfigType == null || targetConfigType.indexOf(config) != CommonConstants.INVALID_VALUE) { 83 return; 84 } 85 targetConfigType.push(config); 86 } 87 88 /** 89 * Release the configuration object in the management class 90 */ 91 removeConfigFromManager(): void { 92 this.resetConfigArray(); 93 } 94 95 /** 96 * Get the layout mode configuration corresponding to the configuration name 97 * 98 * @params configName 99 * @params featureName 100 */ 101 getModeConfig<T extends ILayoutConfig>(configName: string, featureName?: string): T { 102 const configArr = this.getTargetTypeConfigs(CommonConstants.LAYOUT_CONFIG_TYPE_MODE); 103 return this.getConfigByName(configArr, configName, featureName); 104 } 105 106 /** 107 * Get the layout style configuration corresponding to the configuration name 108 * 109 * @params configName 110 * @params featureName 111 */ 112 getStyleConfig(configName: string, featureName?: string): any { 113 const configArr = this.getTargetTypeConfigs(CommonConstants.LAYOUT_CONFIG_TYPE_STYLE); 114 return this.getConfigByName(configArr, configName, featureName); 115 } 116 117 /** 118 * Get the function layout configuration corresponding to the configuration name 119 * 120 * @params configName 121 * @params featureName 122 */ 123 getFunctionConfig<T extends ILayoutConfig>(configName: string, featureName?: string): T { 124 const configArr = this.getTargetTypeConfigs(CommonConstants.LAYOUT_CONFIG_TYPE_FUNCTION); 125 return this.getConfigByName(configArr, configName, featureName); 126 } 127 128 private getConfigByName<T extends ILayoutConfig>(configArr: ILayoutConfig[], configName: string, featureName?: string): T { 129 for (const config of configArr) { 130 if (config.getConfigName() == configName) { 131 if (!featureName || config.getFeatureName() == featureName) { 132 return <T>config; 133 } 134 } 135 } 136 return null; 137 } 138 139 private getTargetTypeConfigs(configType: number) { 140 let configArr = new Array<ILayoutConfig>(); 141 if (this.mProductConfig[configType] && this.mFeatureConfig[configType] && this.mCommonConfig[configType]) { 142 configArr = configArr.concat(this.mProductConfig[configType]); 143 configArr = configArr.concat(this.mFeatureConfig[configType]); 144 configArr = configArr.concat(this.mCommonConfig[configType]); 145 } 146 return configArr; 147 } 148} 149 150export const layoutConfigManager = LayoutConfigManager.getInstance(); 151