1/* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19/* 20 * 2021.01.08 - Reconstruct the class 'Evt' and make it more adaptable to framework. 21 * Copyright (c) 2021 Huawei Device Co., Ltd. 22 */ 23 24/** 25 * @fileOverview 26 * Everything about component event which includes event object, event listener, 27 * event emitter and lifecycle hooks. 28 */ 29 30import Vm from './index'; 31import { PageLifecycleHooks } from './pageLife'; 32 33export type ExternalEvent = {'hook:_innerInit': () => void} | null; 34 35/** 36 * <p>Event object definition. An event object has `type`, `timestamp` and `detail`</p> 37 * <p>from which a component emit. The event object could be dispatched to</p> 38 * <p>parents or broadcasted to children except `this.stop()` is called.</p> 39 */ 40export class Evt { 41 private _timestamp: number; 42 private _detail: any; 43 private _type: string; 44 private _shouldStop: boolean; 45 46 constructor(type: string, detail: any) { 47 this._timestamp = Date.now(); 48 this._detail = detail; 49 this._type = type; 50 if (detail instanceof Evt) { 51 return detail; 52 } 53 } 54 55 /** 56 * Override toJSON function to fix version compatibility issues. 57 */ 58 toJSON() { 59 const jsonObj: Record<string, any> = {}; 60 for (const p in this) { 61 if (!p.startsWith('_')) { 62 jsonObj[p as string] = this[p]; 63 } 64 } 65 const proto = Object.getPrototypeOf(this); 66 const protoNames = Object.getOwnPropertyNames(proto); 67 for (const key of protoNames) { 68 const desc = Object.getOwnPropertyDescriptor(proto, key); 69 const hasGetter = desc && typeof desc.get === 'function'; 70 if (hasGetter) { 71 jsonObj[key] = this[key]; 72 } 73 } 74 return jsonObj; 75 } 76 77 /** 78 * Stop dispatch and broadcast. 79 */ 80 public stop() { 81 this.shouldStop = true; 82 } 83 84 /** 85 * Check if it can't be dispatched or broadcasted 86 */ 87 public hasStopped() { 88 return this.shouldStop; 89 } 90 91 /** 92 * ShouldStop of this Evt. 93 * @type {boolean} 94 */ 95 public get shouldStop() { 96 return this._shouldStop; 97 } 98 99 public set shouldStop(newStop: boolean) { 100 this._shouldStop = newStop; 101 } 102 103 /** 104 * Detail of this Evt. 105 * @type {*} 106 * @readonly 107 */ 108 public get detail() { 109 return this._detail; 110 } 111 112 /** 113 * Timestamp of this Evt. 114 * @type {number} 115 * @readonly 116 */ 117 public get timestamp() { 118 return this._timestamp; 119 } 120 121 /** 122 * Type of this Evt. 123 * @type {string} 124 * @readonly 125 */ 126 public get type() { 127 return this._type; 128 } 129} 130 131export const LIFE_CYCLE_TYPES: Array<PageLifecycleHooks | string> = [ 132 PageLifecycleHooks.ONINIT, 133 PageLifecycleHooks.ONREADY, 134 PageLifecycleHooks.ONSHOW, 135 PageLifecycleHooks.ONHIDE, 136 PageLifecycleHooks.ONBACKPRESS, 137 PageLifecycleHooks.ONMENUPRESS, 138 PageLifecycleHooks.ONMENUBUTTONPRESS, 139 PageLifecycleHooks.ONSTARTCONTINUATUIN, 140 PageLifecycleHooks.ONCOMPLETECONTINUATION, 141 PageLifecycleHooks.ONSAVEDATA, 142 PageLifecycleHooks.ONRESTOREDATA, 143 PageLifecycleHooks.ONNEWREQUEST, 144 PageLifecycleHooks.ONCONFIGURATIONUPDATED, 145 PageLifecycleHooks.ONACTIVE, 146 PageLifecycleHooks.ONINACTIVE, 147 PageLifecycleHooks.ONLAYOUTREADY, 148 PageLifecycleHooks.ONDIALOGUPDATED, 149 'onAttached', 150 'onDetached', 151 'onPageShow', 152 'onPageHide', 153 'onDestroy' 154]; 155 156/** 157 * Init events. 158 * @param {Vm} vm - Vm object. 159 * @param {ExternalEvent} externalEvents - External events. 160 */ 161export function initEvents(vm: Vm, externalEvents: ExternalEvent): void { 162 const options = vm._vmOptions || {}; 163 for (const externalEvent in externalEvents) { 164 vm.$on(externalEvent, externalEvents[externalEvent]); 165 } 166 LIFE_CYCLE_TYPES.forEach((type) => { 167 vm.$on(`hook:${type}`, options[type]); 168 }); 169} 170