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