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