16e80583aSopenharmony_ci/**
26e80583aSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
36e80583aSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
46e80583aSopenharmony_ci * you may not use this file except in compliance with the License.
56e80583aSopenharmony_ci * You may obtain a copy of the License at
66e80583aSopenharmony_ci *
76e80583aSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
86e80583aSopenharmony_ci *
96e80583aSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
106e80583aSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
116e80583aSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
126e80583aSopenharmony_ci * See the License for the specific language governing permissions and
136e80583aSopenharmony_ci * limitations under the License.
146e80583aSopenharmony_ci */
156e80583aSopenharmony_ci
166e80583aSopenharmony_ciimport { ILayoutConfig } from './ILayoutConfig';
176e80583aSopenharmony_ciimport { CommonConstants } from '../constants/CommonConstants';
186e80583aSopenharmony_ci
196e80583aSopenharmony_ci/**
206e80583aSopenharmony_ci * Layout configuration management
216e80583aSopenharmony_ci * There are currently three types of layout management:
226e80583aSopenharmony_ci * 1.Layout mode management, such as grid/list layout, such layout configuration can be easily converted into setting items.
236e80583aSopenharmony_ci * 2.Layout style management, such as layout parameters such as margins, sizes, colors, etc., used to configure adjustable layout styles.
246e80583aSopenharmony_ci * 3.Functional layout management, such as layout management of desktop layouts.
256e80583aSopenharmony_ci * Main features provided:
266e80583aSopenharmony_ci * 1.Save and manage all configuration objects.
276e80583aSopenharmony_ci * 2.Ability to query configuration values at three tiers (Product > Features > Public).
286e80583aSopenharmony_ci * 3.the ability to persist certain configuration values.
296e80583aSopenharmony_ci */
306e80583aSopenharmony_ciclass LayoutConfigManager {
316e80583aSopenharmony_ci  private readonly mCommonConfig: ILayoutConfig[][] = new Array<ILayoutConfig[]>();
326e80583aSopenharmony_ci
336e80583aSopenharmony_ci  private readonly mFeatureConfig: ILayoutConfig[][] = new Array<ILayoutConfig[]>();
346e80583aSopenharmony_ci
356e80583aSopenharmony_ci  private readonly mProductConfig: ILayoutConfig[][] = new Array<ILayoutConfig[]>();
366e80583aSopenharmony_ci
376e80583aSopenharmony_ci  private constructor() {
386e80583aSopenharmony_ci    this.resetConfigArray();
396e80583aSopenharmony_ci  }
406e80583aSopenharmony_ci
416e80583aSopenharmony_ci  private resetConfigArray(): void {
426e80583aSopenharmony_ci    this.initConfigArray(this.mCommonConfig);
436e80583aSopenharmony_ci    this.initConfigArray(this.mFeatureConfig);
446e80583aSopenharmony_ci    this.initConfigArray(this.mProductConfig);
456e80583aSopenharmony_ci  }
466e80583aSopenharmony_ci
476e80583aSopenharmony_ci  private initConfigArray(configArr: ILayoutConfig[][]): void {
486e80583aSopenharmony_ci    configArr[CommonConstants.LAYOUT_CONFIG_TYPE_MODE] = new Array<ILayoutConfig>();
496e80583aSopenharmony_ci    configArr[CommonConstants.LAYOUT_CONFIG_TYPE_STYLE] = new Array<ILayoutConfig>();
506e80583aSopenharmony_ci    configArr[CommonConstants.LAYOUT_CONFIG_TYPE_FUNCTION] = new Array<ILayoutConfig>();
516e80583aSopenharmony_ci  }
526e80583aSopenharmony_ci
536e80583aSopenharmony_ci  /**
546e80583aSopenharmony_ci   * Get the instance of the configuration management class
556e80583aSopenharmony_ci   */
566e80583aSopenharmony_ci  static getInstance(): LayoutConfigManager {
576e80583aSopenharmony_ci    if (globalThis.LayoutConfigManager == null) {
586e80583aSopenharmony_ci      globalThis.LayoutConfigManager = new LayoutConfigManager();
596e80583aSopenharmony_ci    }
606e80583aSopenharmony_ci    return globalThis.LayoutConfigManager;
616e80583aSopenharmony_ci  }
626e80583aSopenharmony_ci
636e80583aSopenharmony_ci  /**
646e80583aSopenharmony_ci   * Add configuration objects to the configuration management class
656e80583aSopenharmony_ci   */
666e80583aSopenharmony_ci  addConfigToManager(config: ILayoutConfig): void {
676e80583aSopenharmony_ci    const configLevel = config.getConfigLevel();
686e80583aSopenharmony_ci    let targetConfigType = null;
696e80583aSopenharmony_ci    switch (configLevel) {
706e80583aSopenharmony_ci    case CommonConstants.LAYOUT_CONFIG_LEVEL_COMMON:
716e80583aSopenharmony_ci      targetConfigType = this.mCommonConfig[config.getConfigType()];
726e80583aSopenharmony_ci      break;
736e80583aSopenharmony_ci    case CommonConstants.LAYOUT_CONFIG_LEVEL_FEATURE:
746e80583aSopenharmony_ci      targetConfigType = this.mFeatureConfig[config.getConfigType()];
756e80583aSopenharmony_ci      break;
766e80583aSopenharmony_ci    case CommonConstants.LAYOUT_CONFIG_LEVEL_PRODUCT:
776e80583aSopenharmony_ci      targetConfigType = this.mProductConfig[config.getConfigType()];
786e80583aSopenharmony_ci      break;
796e80583aSopenharmony_ci    default:
806e80583aSopenharmony_ci      break;
816e80583aSopenharmony_ci    }
826e80583aSopenharmony_ci    if (targetConfigType == null || targetConfigType.indexOf(config) != CommonConstants.INVALID_VALUE) {
836e80583aSopenharmony_ci      return;
846e80583aSopenharmony_ci    }
856e80583aSopenharmony_ci    targetConfigType.push(config);
866e80583aSopenharmony_ci  }
876e80583aSopenharmony_ci
886e80583aSopenharmony_ci  /**
896e80583aSopenharmony_ci   * Release the configuration object in the management class
906e80583aSopenharmony_ci   */
916e80583aSopenharmony_ci  removeConfigFromManager(): void {
926e80583aSopenharmony_ci    this.resetConfigArray();
936e80583aSopenharmony_ci  }
946e80583aSopenharmony_ci
956e80583aSopenharmony_ci  /**
966e80583aSopenharmony_ci   * Get the layout mode configuration corresponding to the configuration name
976e80583aSopenharmony_ci   *
986e80583aSopenharmony_ci   * @params configName
996e80583aSopenharmony_ci   * @params featureName
1006e80583aSopenharmony_ci   */
1016e80583aSopenharmony_ci  getModeConfig<T extends ILayoutConfig>(configName: string, featureName?: string): T {
1026e80583aSopenharmony_ci    const configArr = this.getTargetTypeConfigs(CommonConstants.LAYOUT_CONFIG_TYPE_MODE);
1036e80583aSopenharmony_ci    return this.getConfigByName(configArr, configName, featureName);
1046e80583aSopenharmony_ci  }
1056e80583aSopenharmony_ci
1066e80583aSopenharmony_ci  /**
1076e80583aSopenharmony_ci   * Get the layout style configuration corresponding to the configuration name
1086e80583aSopenharmony_ci   *
1096e80583aSopenharmony_ci   * @params configName
1106e80583aSopenharmony_ci   * @params featureName
1116e80583aSopenharmony_ci   */
1126e80583aSopenharmony_ci  getStyleConfig(configName: string, featureName?: string): any {
1136e80583aSopenharmony_ci    const configArr = this.getTargetTypeConfigs(CommonConstants.LAYOUT_CONFIG_TYPE_STYLE);
1146e80583aSopenharmony_ci    return this.getConfigByName(configArr, configName, featureName);
1156e80583aSopenharmony_ci  }
1166e80583aSopenharmony_ci
1176e80583aSopenharmony_ci  /**
1186e80583aSopenharmony_ci   * Get the function layout configuration corresponding to the configuration name
1196e80583aSopenharmony_ci   *
1206e80583aSopenharmony_ci   * @params configName
1216e80583aSopenharmony_ci   * @params featureName
1226e80583aSopenharmony_ci   */
1236e80583aSopenharmony_ci  getFunctionConfig<T extends ILayoutConfig>(configName: string, featureName?: string): T {
1246e80583aSopenharmony_ci    const configArr = this.getTargetTypeConfigs(CommonConstants.LAYOUT_CONFIG_TYPE_FUNCTION);
1256e80583aSopenharmony_ci    return this.getConfigByName(configArr, configName, featureName);
1266e80583aSopenharmony_ci  }
1276e80583aSopenharmony_ci
1286e80583aSopenharmony_ci  private getConfigByName<T extends ILayoutConfig>(configArr: ILayoutConfig[], configName: string, featureName?: string): T {
1296e80583aSopenharmony_ci    for (const config of configArr) {
1306e80583aSopenharmony_ci      if (config.getConfigName() == configName) {
1316e80583aSopenharmony_ci        if (!featureName || config.getFeatureName() == featureName) {
1326e80583aSopenharmony_ci          return <T>config;
1336e80583aSopenharmony_ci        }
1346e80583aSopenharmony_ci      }
1356e80583aSopenharmony_ci    }
1366e80583aSopenharmony_ci    return null;
1376e80583aSopenharmony_ci  }
1386e80583aSopenharmony_ci
1396e80583aSopenharmony_ci  private getTargetTypeConfigs(configType: number) {
1406e80583aSopenharmony_ci    let configArr = new Array<ILayoutConfig>();
1416e80583aSopenharmony_ci    if (this.mProductConfig[configType] && this.mFeatureConfig[configType] && this.mCommonConfig[configType]) {
1426e80583aSopenharmony_ci      configArr = configArr.concat(this.mProductConfig[configType]);
1436e80583aSopenharmony_ci      configArr = configArr.concat(this.mFeatureConfig[configType]);
1446e80583aSopenharmony_ci      configArr = configArr.concat(this.mCommonConfig[configType]);
1456e80583aSopenharmony_ci    }
1466e80583aSopenharmony_ci    return configArr;
1476e80583aSopenharmony_ci  }
1486e80583aSopenharmony_ci}
1496e80583aSopenharmony_ci
1506e80583aSopenharmony_ciexport const layoutConfigManager = LayoutConfigManager.getInstance();
151