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 * Stop dispatch and broadcast. 57 */ 58 public stop() { 59 this.shouldStop = true; 60 } 61 62 /** 63 * Check if it can't be dispatched or broadcasted 64 */ 65 public hasStopped() { 66 return this.shouldStop; 67 } 68 69 /** 70 * ShouldStop of this Evt. 71 * @type {boolean} 72 */ 73 public get shouldStop() { 74 return this._shouldStop; 75 } 76 77 public set shouldStop(newStop: boolean) { 78 this._shouldStop = newStop; 79 } 80 81 /** 82 * Detail of this Evt. 83 * @type {*} 84 * @readonly 85 */ 86 public get detail() { 87 return this._detail; 88 } 89 90 /** 91 * Timestamp of this Evt. 92 * @type {number} 93 * @readonly 94 */ 95 public get timestamp() { 96 return this._timestamp; 97 } 98 99 /** 100 * Type of this Evt. 101 * @type {string} 102 * @readonly 103 */ 104 public get type() { 105 return this._type; 106 } 107} 108 109export const LIFE_CYCLE_TYPES: Array<PageLifecycleHooks | string> = [ 110 PageLifecycleHooks.ONINIT, 111 PageLifecycleHooks.ONREADY, 112 PageLifecycleHooks.ONSHOW, 113 PageLifecycleHooks.ONHIDE, 114 PageLifecycleHooks.ONBACKPRESS, 115 PageLifecycleHooks.ONMENUPRESS, 116 PageLifecycleHooks.ONMENUBUTTONPRESS, 117 PageLifecycleHooks.ONSTARTCONTINUATUIN, 118 PageLifecycleHooks.ONCOMPLETECONTINUATION, 119 PageLifecycleHooks.ONSAVEDATA, 120 PageLifecycleHooks.ONRESTOREDATA, 121 PageLifecycleHooks.ONNEWREQUEST, 122 PageLifecycleHooks.ONCONFIGURATIONUPDATED, 123 PageLifecycleHooks.ONACTIVE, 124 PageLifecycleHooks.ONINACTIVE, 125 PageLifecycleHooks.ONLAYOUTREADY, 126 'onAttached', 127 'onDetached', 128 'onPageShow', 129 'onPageHide', 130 'onDestroy' 131]; 132 133/** 134 * Init events. 135 * @param {Vm} vm - Vm object. 136 * @param {ExternalEvent} externalEvents - External events. 137 */ 138export function initEvents(vm: Vm, externalEvents: ExternalEvent): void { 139 const options = vm._vmOptions || {}; 140 for (const externalEvent in externalEvents) { 141 vm.$on(externalEvent, externalEvents[externalEvent]); 142 } 143 LIFE_CYCLE_TYPES.forEach((type) => { 144 vm.$on(`hook:${type}`, options[type]); 145 }); 146} 147