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 */ 15import { Log } from '../utils/Log'; 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 */ 30const TAG = 'LayoutConfigManager'; 31class LayoutConfigManager { 32 private readonly mCommonConfig: ILayoutConfig[][] = new Array<ILayoutConfig[]>(); 33 34 private readonly mFeatureConfig: ILayoutConfig[][] = new Array<ILayoutConfig[]>(); 35 36 private readonly mProductConfig: ILayoutConfig[][] = new Array<ILayoutConfig[]>(); 37 38 private constructor() { 39 this.resetConfigArray(); 40 } 41 42 private resetConfigArray(): void { 43 this.initConfigArray(this.mCommonConfig); 44 this.initConfigArray(this.mFeatureConfig); 45 this.initConfigArray(this.mProductConfig); 46 } 47 48 private initConfigArray(configArr: ILayoutConfig[][]): void { 49 configArr[CommonConstants.LAYOUT_CONFIG_TYPE_MODE] = new Array<ILayoutConfig>(); 50 configArr[CommonConstants.LAYOUT_CONFIG_TYPE_STYLE] = new Array<ILayoutConfig>(); 51 configArr[CommonConstants.LAYOUT_CONFIG_TYPE_FUNCTION] = new Array<ILayoutConfig>(); 52 } 53 54 /** 55 * Get the instance of the configuration management class 56 */ 57 static getInstance(): LayoutConfigManager { 58 if (globalThis.LayoutConfigManager == null) { 59 globalThis.LayoutConfigManager = new LayoutConfigManager(); 60 } 61 return globalThis.LayoutConfigManager; 62 } 63 64 /** 65 * Add configuration objects to the configuration management class 66 */ 67 addConfigToManager(config: ILayoutConfig): void { 68 const configLevel = config.getConfigLevel(); 69 let targetConfigType = null; 70 switch (configLevel) { 71 case CommonConstants.LAYOUT_CONFIG_LEVEL_COMMON: 72 targetConfigType = this.mCommonConfig[config.getConfigType()]; 73 break; 74 case CommonConstants.LAYOUT_CONFIG_LEVEL_FEATURE: 75 targetConfigType = this.mFeatureConfig[config.getConfigType()]; 76 break; 77 case CommonConstants.LAYOUT_CONFIG_LEVEL_PRODUCT: 78 targetConfigType = this.mProductConfig[config.getConfigType()]; 79 break; 80 default: 81 break; 82 } 83 if (targetConfigType == null || targetConfigType.indexOf(config) != CommonConstants.INVALID_VALUE) { 84 return; 85 } 86 targetConfigType.push(config); 87 } 88 89 /** 90 * Release the configuration object in the management class 91 */ 92 removeConfigFromManager(): void { 93 this.resetConfigArray(); 94 } 95 96 /** 97 * Get the layout mode configuration corresponding to the configuration name 98 * 99 * @params configName 100 * @params featureName 101 */ 102 getModeConfig<T extends ILayoutConfig>(configName: string, featureName?: string): T { 103 const configArr = this.getTargetTypeConfigs(CommonConstants.LAYOUT_CONFIG_TYPE_MODE); 104 return this.getConfigByName(configArr, configName, featureName); 105 } 106 107 /** 108 * Get the layout style configuration corresponding to the configuration name 109 * 110 * @params configName 111 * @params featureName 112 */ 113 getStyleConfig(configName: string, featureName?: string): any { 114 const configArr = this.getTargetTypeConfigs(CommonConstants.LAYOUT_CONFIG_TYPE_STYLE); 115 return this.getConfigByName(configArr, configName, featureName); 116 } 117 118 /** 119 * Get the function layout configuration corresponding to the configuration name 120 * 121 * @params configName 122 * @params featureName 123 */ 124 getFunctionConfig<T extends ILayoutConfig>(configName: string, featureName?: string): T { 125 const configArr = this.getTargetTypeConfigs(CommonConstants.LAYOUT_CONFIG_TYPE_FUNCTION); 126 return this.getConfigByName(configArr, configName, featureName); 127 } 128 129 private getConfigByName<T extends ILayoutConfig>(configArr: ILayoutConfig[], configName: string, featureName?: string): T { 130 for (const config of configArr) { 131 if (config.getConfigName() == configName) { 132 if (!featureName || config.getFeatureName() == featureName) { 133 return <T>config; 134 } 135 } 136 } 137 Log.showError(TAG, 'getConfigByName is null'); 138 return null; 139 } 140 141 private getTargetTypeConfigs(configType: number) { 142 let configArr = new Array<ILayoutConfig>(); 143 if (this.mProductConfig[configType] && this.mFeatureConfig[configType] && this.mCommonConfig[configType]) { 144 configArr = configArr.concat(this.mProductConfig[configType]); 145 configArr = configArr.concat(this.mFeatureConfig[configType]); 146 configArr = configArr.concat(this.mCommonConfig[configType]); 147 } 148 return configArr; 149 } 150} 151 152export const layoutConfigManager = LayoutConfigManager.getInstance(); 153