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_ciimport Document from './Document';
21e484b35bSopenharmony_ciimport Watcher from '../main/reactivity/watcher';
22e484b35bSopenharmony_ci
23e484b35bSopenharmony_ci/**
24e484b35bSopenharmony_ci * This class generate an unique id.
25e484b35bSopenharmony_ci */
26e484b35bSopenharmony_ciclass IDGenerator {
27e484b35bSopenharmony_ci  private static _id: number = 0;
28e484b35bSopenharmony_ci
29e484b35bSopenharmony_ci  /**
30e484b35bSopenharmony_ci   * Return an unique id, which value is a string converted from number.
31e484b35bSopenharmony_ci   * @return {string} A string converted from number.
32e484b35bSopenharmony_ci   */
33e484b35bSopenharmony_ci  public static getUniqueId(): string {
34e484b35bSopenharmony_ci    this._id++;
35e484b35bSopenharmony_ci    return this._id.toString();
36e484b35bSopenharmony_ci  }
37e484b35bSopenharmony_ci}
38e484b35bSopenharmony_ci
39e484b35bSopenharmony_ci/**
40e484b35bSopenharmony_ci * Enum for NodeTypes
41e484b35bSopenharmony_ci * @enum {number}
42e484b35bSopenharmony_ci */
43e484b35bSopenharmony_ci/* eslint-disable no-unused-vars */
44e484b35bSopenharmony_cienum NodeType {
45e484b35bSopenharmony_ci  /**
46e484b35bSopenharmony_ci   * Element type
47e484b35bSopenharmony_ci   */
48e484b35bSopenharmony_ci  Element = 1,
49e484b35bSopenharmony_ci
50e484b35bSopenharmony_ci  /**
51e484b35bSopenharmony_ci   * Comment type
52e484b35bSopenharmony_ci   */
53e484b35bSopenharmony_ci  Comment = 8
54e484b35bSopenharmony_ci}
55e484b35bSopenharmony_ci/* eslint-enable no-unused-vars */
56e484b35bSopenharmony_ci
57e484b35bSopenharmony_ci/**
58e484b35bSopenharmony_ci * Node is base class for Element and Comment.<br>
59e484b35bSopenharmony_ci * It has basic method for setting nodeId, getting children, etc...
60e484b35bSopenharmony_ci */
61e484b35bSopenharmony_ciclass Node {
62e484b35bSopenharmony_ci  public static NodeType = NodeType;
63e484b35bSopenharmony_ci
64e484b35bSopenharmony_ci  protected _depth: number;
65e484b35bSopenharmony_ci  protected _nodeId: string;
66e484b35bSopenharmony_ci  protected _nodeType: number;
67e484b35bSopenharmony_ci  protected _ref: string;
68e484b35bSopenharmony_ci  protected _nextSibling: Node;
69e484b35bSopenharmony_ci  protected _previousSibling: Node;
70e484b35bSopenharmony_ci  protected _watchers: Watcher[];
71e484b35bSopenharmony_ci  protected _destroyHook: () => void;
72e484b35bSopenharmony_ci  protected _type: string;
73e484b35bSopenharmony_ci  protected _parentNode: Node;
74e484b35bSopenharmony_ci  protected _ownerDocument: Document;
75e484b35bSopenharmony_ci  protected _docId: string;
76e484b35bSopenharmony_ci
77e484b35bSopenharmony_ci  constructor() {
78e484b35bSopenharmony_ci    this._nodeId = IDGenerator.getUniqueId();
79e484b35bSopenharmony_ci    this._ref = this.nodeId;
80e484b35bSopenharmony_ci    this._nextSibling = null;
81e484b35bSopenharmony_ci    this._previousSibling = null;
82e484b35bSopenharmony_ci    this._parentNode = null;
83e484b35bSopenharmony_ci    this._watchers = [];
84e484b35bSopenharmony_ci    this._destroyHook = null;
85e484b35bSopenharmony_ci  }
86e484b35bSopenharmony_ci
87e484b35bSopenharmony_ci  /**
88e484b35bSopenharmony_ci   * Parent node.
89e484b35bSopenharmony_ci   * @type {Node}
90e484b35bSopenharmony_ci   */
91e484b35bSopenharmony_ci  public set parentNode(newParentNode: Node) {
92e484b35bSopenharmony_ci    this._parentNode = newParentNode;
93e484b35bSopenharmony_ci  }
94e484b35bSopenharmony_ci
95e484b35bSopenharmony_ci  public get parentNode() {
96e484b35bSopenharmony_ci    return this._parentNode;
97e484b35bSopenharmony_ci  }
98e484b35bSopenharmony_ci
99e484b35bSopenharmony_ci  /**
100e484b35bSopenharmony_ci   * Watchers for recativity in a Node.
101e484b35bSopenharmony_ci   * @type {Watcher[]}
102e484b35bSopenharmony_ci   */
103e484b35bSopenharmony_ci  public get watchers() {
104e484b35bSopenharmony_ci    return this._watchers;
105e484b35bSopenharmony_ci  }
106e484b35bSopenharmony_ci
107e484b35bSopenharmony_ci  public set watchers(newWatchers: Watcher[]) {
108e484b35bSopenharmony_ci    this._watchers = newWatchers;
109e484b35bSopenharmony_ci  }
110e484b35bSopenharmony_ci
111e484b35bSopenharmony_ci  /**
112e484b35bSopenharmony_ci   * Node ID.
113e484b35bSopenharmony_ci   * @type {string}
114e484b35bSopenharmony_ci   */
115e484b35bSopenharmony_ci  public get nodeId() {
116e484b35bSopenharmony_ci    return this._nodeId;
117e484b35bSopenharmony_ci  }
118e484b35bSopenharmony_ci
119e484b35bSopenharmony_ci  public set nodeId(newNodeId: string) {
120e484b35bSopenharmony_ci    this._nodeId = newNodeId;
121e484b35bSopenharmony_ci  }
122e484b35bSopenharmony_ci
123e484b35bSopenharmony_ci  /**
124e484b35bSopenharmony_ci   * Node type.
125e484b35bSopenharmony_ci   */
126e484b35bSopenharmony_ci  public get nodeType() {
127e484b35bSopenharmony_ci    return this._nodeType;
128e484b35bSopenharmony_ci  }
129e484b35bSopenharmony_ci
130e484b35bSopenharmony_ci  public set nodeType(newNodeType: NodeType) {
131e484b35bSopenharmony_ci    this._nodeType = newNodeType;
132e484b35bSopenharmony_ci  }
133e484b35bSopenharmony_ci
134e484b35bSopenharmony_ci  /**
135e484b35bSopenharmony_ci   * Destroy hook.
136e484b35bSopenharmony_ci   * @type {Function}
137e484b35bSopenharmony_ci   */
138e484b35bSopenharmony_ci  public set destroyHook(hook: () => void) {
139e484b35bSopenharmony_ci    this._destroyHook = hook;
140e484b35bSopenharmony_ci  }
141e484b35bSopenharmony_ci
142e484b35bSopenharmony_ci  public get destroyHook() {
143e484b35bSopenharmony_ci    return this._destroyHook;
144e484b35bSopenharmony_ci  }
145e484b35bSopenharmony_ci
146e484b35bSopenharmony_ci  /**
147e484b35bSopenharmony_ci   * The level from current node to root element.
148e484b35bSopenharmony_ci   * @type {number}
149e484b35bSopenharmony_ci   */
150e484b35bSopenharmony_ci  public set depth(newValue: number) {
151e484b35bSopenharmony_ci    this._depth = newValue;
152e484b35bSopenharmony_ci  }
153e484b35bSopenharmony_ci
154e484b35bSopenharmony_ci  public get depth() {
155e484b35bSopenharmony_ci    return this._depth;
156e484b35bSopenharmony_ci  }
157e484b35bSopenharmony_ci
158e484b35bSopenharmony_ci  /**
159e484b35bSopenharmony_ci   * <p>XML tag name, like div, button.</p>
160e484b35bSopenharmony_ci   * <p>If node type is NodeType.Comment, it's value is "comment".</p>
161e484b35bSopenharmony_ci   * @type {string}
162e484b35bSopenharmony_ci   */
163e484b35bSopenharmony_ci  public set type(newType: string) {
164e484b35bSopenharmony_ci    this._type = newType;
165e484b35bSopenharmony_ci  }
166e484b35bSopenharmony_ci
167e484b35bSopenharmony_ci  public get type() {
168e484b35bSopenharmony_ci    return this._type;
169e484b35bSopenharmony_ci  }
170e484b35bSopenharmony_ci
171e484b35bSopenharmony_ci  /**
172e484b35bSopenharmony_ci   * <p>Node Reference, it's value is same as nodeId, It will send to native.</p>
173e484b35bSopenharmony_ci   * <p>Document element's ref is "_documentElement", root element's ref is "root".</p>
174e484b35bSopenharmony_ci   * @type {string}
175e484b35bSopenharmony_ci   */
176e484b35bSopenharmony_ci  public set ref(newRef: string) {
177e484b35bSopenharmony_ci    this._ref = newRef;
178e484b35bSopenharmony_ci  }
179e484b35bSopenharmony_ci
180e484b35bSopenharmony_ci  public get ref() {
181e484b35bSopenharmony_ci    return this._ref;
182e484b35bSopenharmony_ci  }
183e484b35bSopenharmony_ci
184e484b35bSopenharmony_ci  /**
185e484b35bSopenharmony_ci   * Next sibling node.
186e484b35bSopenharmony_ci   * @type {Node}
187e484b35bSopenharmony_ci   */
188e484b35bSopenharmony_ci  public set nextSibling(nextSibling: Node) {
189e484b35bSopenharmony_ci    this._nextSibling = nextSibling;
190e484b35bSopenharmony_ci  }
191e484b35bSopenharmony_ci
192e484b35bSopenharmony_ci  public get nextSibling() {
193e484b35bSopenharmony_ci    return this._nextSibling;
194e484b35bSopenharmony_ci  }
195e484b35bSopenharmony_ci
196e484b35bSopenharmony_ci  /**
197e484b35bSopenharmony_ci   * Previous sibling node.
198e484b35bSopenharmony_ci   * @type {Node}
199e484b35bSopenharmony_ci   */
200e484b35bSopenharmony_ci  public set previousSibling(previousSibling: Node) {
201e484b35bSopenharmony_ci    this._previousSibling = previousSibling;
202e484b35bSopenharmony_ci  }
203e484b35bSopenharmony_ci
204e484b35bSopenharmony_ci  public get previousSibling() {
205e484b35bSopenharmony_ci    return this._previousSibling;
206e484b35bSopenharmony_ci  }
207e484b35bSopenharmony_ci
208e484b35bSopenharmony_ci  /**
209e484b35bSopenharmony_ci   * Document reference which this element belong to.
210e484b35bSopenharmony_ci   * @type {Document}
211e484b35bSopenharmony_ci   */
212e484b35bSopenharmony_ci  public set ownerDocument(doc: Document) {
213e484b35bSopenharmony_ci    this._ownerDocument = doc;
214e484b35bSopenharmony_ci  }
215e484b35bSopenharmony_ci
216e484b35bSopenharmony_ci  public get ownerDocument() {
217e484b35bSopenharmony_ci    return this._ownerDocument;
218e484b35bSopenharmony_ci  }
219e484b35bSopenharmony_ci
220e484b35bSopenharmony_ci  /**
221e484b35bSopenharmony_ci   * ID of the document which element belong to.
222e484b35bSopenharmony_ci   * @type {string}
223e484b35bSopenharmony_ci   */
224e484b35bSopenharmony_ci  public get docId() {
225e484b35bSopenharmony_ci    return this._docId;
226e484b35bSopenharmony_ci  }
227e484b35bSopenharmony_ci
228e484b35bSopenharmony_ci  public set docId(value: string) {
229e484b35bSopenharmony_ci    this._docId = value;
230e484b35bSopenharmony_ci  }
231e484b35bSopenharmony_ci
232e484b35bSopenharmony_ci  /**
233e484b35bSopenharmony_ci   * Destroy current node, and remove itself form nodeMap.
234e484b35bSopenharmony_ci   */
235e484b35bSopenharmony_ci  public destroy(): void {
236e484b35bSopenharmony_ci    this._nextSibling = null;
237e484b35bSopenharmony_ci    this._previousSibling = null;
238e484b35bSopenharmony_ci    this._parentNode = null;
239e484b35bSopenharmony_ci    this._watchers = null;
240e484b35bSopenharmony_ci    this._destroyHook = null;
241e484b35bSopenharmony_ci    this._ownerDocument = null;
242e484b35bSopenharmony_ci  }
243e484b35bSopenharmony_ci}
244e484b35bSopenharmony_ci
245e484b35bSopenharmony_ciexport default Node;
246