1/** 2 * Copyright (c) 2021 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 ISettingsController from './ISettingsController' 17 18export default abstract class BaseSettingsController implements ISettingsController { 19 protected getComponent: () => any; 20 21 /** 22 * Bind component. 23 */ 24 bindComponent(component: any): ISettingsController{ 25 this.getComponent = () => component; 26 27 // set default property values by component 28 for (var key in this) { 29 if (key in component) { 30 this[key] = component[key]; 31 } 32 } 33 34 return this; 35 } 36 37 /** 38 * Bind component's properties, note that only basic types can be transported. 39 * Type like Resource may meet unexpected error. 40 * If you want to transport resource string or color, ets. AppStorage is suggested @StorageLink. 41 */ 42 bindProperties(componentProperties: string[], controllerProperties?: string[]): ISettingsController { 43 for (let i = 0; i < componentProperties.length; i++) { 44 this.defineBoundProperty(componentProperties[i], controllerProperties ? controllerProperties[i] : componentProperties[i]); 45 } 46 47 return this; 48 } 49 50 /** 51 * Initialize data. 52 */ 53 initData(): ISettingsController { 54 return this; 55 }; 56 57 /** 58 * Subscribe listeners. 59 */ 60 subscribe(): ISettingsController { 61 return this; 62 }; 63 64 /** 65 * Unsubscribe listeners. 66 */ 67 unsubscribe(): ISettingsController { 68 return this; 69 }; 70 71 /** 72 * Define bound properties. 73 */ 74 private defineBoundProperty(componentProperty: string, controllerProperty: string): void { 75 let __v = this[controllerProperty]; 76 77 Object.defineProperty(this, controllerProperty, { 78 get: function () { 79 return __v; 80 }, 81 set: function (value) { 82 __v = value; 83 this.getComponent()[componentProperty] = value; 84 } 85 }); 86 } 87 88}