-
Notifications
You must be signed in to change notification settings - Fork 211
Expand file tree
/
Copy pathNode.ts
More file actions
executable file
·319 lines (274 loc) · 8.77 KB
/
Node.ts
File metadata and controls
executable file
·319 lines (274 loc) · 8.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import { AttributeDefinitions } from "../AttributeDefinitions";
import { DockLocation } from "../DockLocation";
import { DropInfo } from "../DropInfo";
import { Orientation } from "../Orientation";
import { Rect } from "../Rect";
import { IDraggable } from "./IDraggable";
import { IJsonBorderNode, IJsonRowNode, IJsonTabNode, IJsonTabSetNode } from "./IJsonModel";
import { Model, ILayoutMetrics } from "./Model";
export abstract class Node {
/** @internal */
protected _model: Model;
/** @internal */
protected _attributes: Record<string, any>;
/** @internal */
protected _parent?: Node;
/** @internal */
protected _children: Node[];
/** @internal */
protected _fixed: boolean;
/** @internal */
protected _rect: Rect;
/** @internal */
protected _visible: boolean;
/** @internal */
protected _listeners: Record<string, ((params: any) => void)[]>;
/** @internal */
protected _dirty: boolean = false;
/** @internal */
protected _tempSize: number = 0;
/** @internal */
protected constructor(model: Model) {
this._model = model;
this._attributes = {};
this._children = [];
this._fixed = false;
this._rect = Rect.empty();
this._visible = false;
this._listeners = {};
}
getId() {
let id = this._attributes.id;
if (id !== undefined) {
return id as string;
}
id = this._model._nextUniqueId();
this._setId(id);
return id as string;
}
getModel() {
return this._model;
}
getType() {
return this._attributes.type as string;
}
getParent() {
return this._parent;
}
getChildren() {
return this._children;
}
getRect() {
return this._rect;
}
isVisible() {
return this._visible;
}
getOrientation(): Orientation {
if (this._parent === undefined) {
return this._model.isRootOrientationVertical() ? Orientation.VERT : Orientation.HORZ;
} else {
return Orientation.flip(this._parent.getOrientation());
}
}
/**
* Adds the given event listener. Can optionally replace existing listeners.
* @param event valid events: resize, visibility, save, close
* @param callback
* @param replace if set to true, existing listeners will be cleared
*/
setEventListener(event: string, callback: (params: any) => void, replace:boolean=true) {
if (replace || this._listeners[event] === undefined) {
this._listeners[event] = [callback];
} else {
this._listeners[event].push(callback);
}
}
/**
* If a callback is provided, it will be removed from the event.
* If no callback is provided, all callbacks will be removed from the event.
* @param event valid events: resize, visibility, save, close
* @param callback the specific callback to remove
*/
removeEventListener(event: string, callback?: (params: any) => void) {
if (this._listeners[event] !== undefined) {
if (callback) {
const index = this._listeners[event].indexOf(callback);
if (index !== -1) {
this._listeners[event].splice(index, 1);
}
} else {
this._listeners[event] = [];
}
}
}
abstract toJson(): IJsonRowNode | IJsonBorderNode | IJsonTabSetNode | IJsonTabNode | undefined;
/** @internal */
_setId(id: string) {
this._attributes.id = id;
}
/** @internal */
_fireEvent(event: string, params: any) {
// console.log(this._type, " fireEvent " + event + " " + JSON.stringify(params));
if (this._listeners[event] !== undefined) {
for(const callback of this._listeners[event]) {
callback(params);
}
}
}
/** @internal */
_getAttr(name: string) {
let val = this._attributes[name];
if (val === undefined) {
const modelName = this._getAttributeDefinitions().getModelName(name);
if (modelName !== undefined) {
val = this._model._getAttribute(modelName);
}
}
// console.log(name + "=" + val);
return val;
}
/** @internal */
_forEachNode(fn: (node: Node, level: number) => void, level: number) {
fn(this, level);
level++;
for (const node of this._children) {
node._forEachNode(fn, level);
}
}
/** @internal */
_setVisible(visible: boolean) {
if (visible !== this._visible) {
this._fireEvent("visibility", { visible });
this._visible = visible;
}
}
/** @internal */
_getDrawChildren(): Node[] | undefined {
return this._children;
}
/** @internal */
_setParent(parent: Node) {
this._parent = parent;
}
/** @internal */
_setRect(rect: Rect) {
this._rect = rect;
}
/** @internal */
_setWeight(weight: number) {
this._attributes.weight = weight;
}
/** @internal */
_setSelected(index: number) {
this._attributes.selected = index;
}
/** @internal */
_isFixed() {
return this._fixed;
}
/** @internal */
_layout(rect: Rect, metrics: ILayoutMetrics) {
this._rect = rect;
}
/** @internal */
_findDropTargetNode(dragNode: Node & IDraggable, x: number, y: number): DropInfo | undefined {
let rtn: DropInfo | undefined;
if (this._rect.contains(x, y)) {
if (this._model.getMaximizedTabset() !== undefined) {
rtn = this._model.getMaximizedTabset()!.canDrop(dragNode, x, y);
} else {
rtn = this.canDrop(dragNode, x, y);
if (rtn === undefined) {
if (this._children.length !== 0) {
for (const child of this._children) {
rtn = child._findDropTargetNode(dragNode, x, y);
if (rtn !== undefined) {
break;
}
}
}
}
}
}
return rtn;
}
/** @internal */
canDrop(dragNode: Node & IDraggable, x: number, y: number): DropInfo | undefined {
return undefined;
}
/** @internal */
_canDockInto(dragNode: Node & IDraggable, dropInfo: DropInfo | undefined): boolean {
if (dropInfo != null) {
if (dropInfo.location === DockLocation.CENTER && dropInfo.node.isEnableDrop() === false) {
return false;
}
// prevent named tabset docking into another tabset, since this would lose the header
if (dropInfo.location === DockLocation.CENTER && dragNode.getType() === "tabset" && dragNode.getName() !== undefined) {
return false;
}
if (dropInfo.location !== DockLocation.CENTER && dropInfo.node.isEnableDivide() === false) {
return false;
}
// finally check model callback to check if drop allowed
if (this._model._getOnAllowDrop()) {
return (this._model._getOnAllowDrop() as (dragNode: Node, dropInfo: DropInfo) => boolean)(dragNode, dropInfo);
}
}
return true;
}
/** @internal */
_removeChild(childNode: Node) {
const pos = this._children.indexOf(childNode);
if (pos !== -1) {
this._children.splice(pos, 1);
}
this._dirty = true;
return pos;
}
/** @internal */
_addChild(childNode: Node, pos?: number) {
if (pos != null) {
this._children.splice(pos, 0, childNode);
} else {
this._children.push(childNode);
pos = this._children.length - 1;
}
childNode._parent = this;
this._dirty = true;
return pos;
}
/** @internal */
_removeAll() {
this._children = [];
this._dirty = true;
}
/** @internal */
_styleWithPosition(style?: Record<string, any>) {
if (style == null) {
style = {};
}
return this._rect.styleWithPosition(style);
}
/** @internal */
_getTempSize() {
return this._tempSize;
}
/** @internal */
_setTempSize(value: number) {
this._tempSize = value;
}
/** @internal */
isEnableDivide() {
return true;
}
/** @internal */
_toAttributeString() {
return JSON.stringify(this._attributes, undefined, "\t");
}
// implemented by subclasses
/** @internal */
abstract _updateAttrs(json: any): void;
/** @internal */
abstract _getAttributeDefinitions(): AttributeDefinitions;
}