-
Notifications
You must be signed in to change notification settings - Fork 213
Expand file tree
/
Copy pathTabSetNode.ts
More file actions
executable file
·599 lines (516 loc) · 21.3 KB
/
Copy pathTabSetNode.ts
File metadata and controls
executable file
·599 lines (516 loc) · 21.3 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
import { Attribute } from "../Attribute";
import { AttributeDefinitions } from "../AttributeDefinitions";
import { DockLocation } from "../DockLocation";
import { DropInfo } from "../DropInfo";
import { Orientation } from "../Orientation";
import { Rect } from "../Rect";
import { CLASSES } from "../Types";
import { canDockToWindow } from "../view/Utils";
import { BorderNode } from "./BorderNode";
import { IDraggable } from "./IDraggable";
import { IDropTarget } from "./IDropTarget";
import { IJsonTabSetNode } from "./IJsonModel";
import { LayoutWindow } from "./LayoutWindow";
import { Model } from "./Model";
import { Node } from "./Node";
import { RowNode } from "./RowNode";
import { TabNode } from "./TabNode";
import { adjustSelectedIndex } from "./Utils";
export class TabSetNode extends Node implements IDraggable, IDropTarget {
static readonly TYPE = "tabset";
/** @internal */
static fromJson(json: any, model: Model, layoutWindow: LayoutWindow) {
const newLayoutNode = new TabSetNode(model, json);
if (json.children != null) {
for (const jsonChild of json.children) {
const child = TabNode.fromJson(jsonChild, model);
newLayoutNode.addChild(child);
}
}
if (newLayoutNode.children.length === 0) {
newLayoutNode.setSelected(-1);
}
if (json.maximized && json.maximized === true) {
layoutWindow.maximizedTabSet = newLayoutNode;
}
if (json.active && json.active === true) {
layoutWindow.activeTabSet = newLayoutNode;
}
return newLayoutNode;
}
/** @internal */
private static attributeDefinitions: AttributeDefinitions = TabSetNode.createAttributeDefinitions();
/** @internal */
private tabStripRect: Rect = Rect.empty();
/** @internal */
private contentRect: Rect = Rect.empty();
/** @internal */
private calculatedMinHeight: number;
/** @internal */
private calculatedMinWidth: number;
/** @internal */
private calculatedMaxHeight: number;
/** @internal */
private calculatedMaxWidth: number;
/** @internal */
constructor(model: Model, json: any) {
super(model);
this.calculatedMinHeight = 0;
this.calculatedMinWidth = 0;
this.calculatedMaxHeight = 0;
this.calculatedMaxWidth = 0;
TabSetNode.attributeDefinitions.fromJson(json, this.attributes);
model.addNode(this);
}
getName() {
return this.getAttr("name") as string | undefined;
}
isEnableActiveIcon() {
return this.getAttr("enableActiveIcon") as boolean;
}
getSelected() {
const selected = this.attributes.selected;
if (selected !== undefined) {
return selected as number;
}
return -1;
}
getSelectedNode() {
const selected = this.getSelected();
if (selected !== -1) {
return this.children[selected];
}
return undefined;
}
getWeight(): number {
return this.getAttr("weight") as number;
}
getAttrMinWidth() {
return (this.getAttr("minWidth") as number);
}
getAttrMinHeight() {
return this.getAttr("minHeight") as number;
}
getMinWidth() {
return this.calculatedMinWidth;
}
getMinHeight() {
return this.calculatedMinHeight;
}
/** @internal */
getMinSize(orientation: Orientation) {
if (orientation === Orientation.HORZ) {
return this.getMinWidth();
} else {
return this.getMinHeight();
}
}
getAttrMaxWidth() {
return (this.getAttr("maxWidth") as number);
}
getAttrMaxHeight() {
return this.getAttr("maxHeight") as number;
}
getMaxWidth() {
return this.calculatedMaxWidth;
}
getMaxHeight() {
return this.calculatedMaxHeight;
}
/** @internal */
getMaxSize(orientation: Orientation) {
if (orientation === Orientation.HORZ) {
return this.getMaxWidth();
} else {
return this.getMaxHeight();
}
}
/**
* Returns the config attribute that can be used to store node specific data that
* WILL be saved to the json. The config attribute should be changed via the action Actions.updateNodeAttributes rather
* than directly, for example:
* this.state.model.doAction(
* FlexLayout.Actions.updateNodeAttributes(node.getId(), {config:myConfigObject}));
*/
getConfig() {
return this.attributes.config;
}
isMaximized() {
return this.model.getMaximizedTabset(this.getWindowId()) === this;
}
isActive() {
return this.model.getActiveTabset(this.getWindowId()) === this;
}
isEnableDeleteWhenEmpty() {
return this.getAttr("enableDeleteWhenEmpty") as boolean;
}
isEnableHideWhenEmpty() {
return this.getAttr("enableHideWhenEmpty") as boolean;
}
isEnableDrop() {
return this.getAttr("enableDrop") as boolean;
}
isEnableTabWrap() {
return this.getAttr("enableTabWrap") as boolean;
}
isEnableDrag() {
return this.getAttr("enableDrag") as boolean;
}
isEnableDivide() {
return this.getAttr("enableDivide") as boolean;
}
isEnableMaximize() {
return this.getAttr("enableMaximize") as boolean;
}
isEnableClose() {
return this.getAttr("enableClose") as boolean;
}
isEnableSingleTabStretch() {
return this.getAttr("enableSingleTabStretch") as boolean;
}
isEnableTabStrip() {
return this.getAttr("enableTabStrip") as boolean;
}
isAutoSelectTab() {
return this.getAttr("autoSelectTab") as boolean;
}
isEnableTabScrollbar() {
return this.getAttr("enableTabScrollbar") as boolean;
}
getClassNameTabStrip() {
return this.getAttr("classNameTabStrip") as string | undefined;
}
getTabLocation() {
return this.getAttr("tabLocation") as string;
}
toJson(): IJsonTabSetNode {
const json: any = {};
TabSetNode.attributeDefinitions.toJson(json, this.attributes);
json.children = this.children.map((child) => child.toJson());
if (this.isActive()) {
json.active = true;
}
if (this.isMaximized()) {
json.maximized = true;
}
return json;
}
/** @internal */
calcMinMaxSize() {
this.calculatedMinHeight = this.getAttrMinHeight();
this.calculatedMinWidth = this.getAttrMinWidth();
this.calculatedMaxHeight = this.getAttrMaxHeight();
this.calculatedMaxWidth = this.getAttrMaxWidth();
for (const child of this.children) {
const c = child as TabNode;
this.calculatedMinWidth = Math.max(this.calculatedMinWidth, c.getMinWidth());
this.calculatedMinHeight = Math.max(this.calculatedMinHeight, c.getMinHeight());
this.calculatedMaxWidth = Math.min(this.calculatedMaxWidth, c.getMaxWidth());
this.calculatedMaxHeight = Math.min(this.calculatedMaxHeight, c.getMaxHeight());
}
this.calculatedMinHeight += this.tabStripRect.height;
this.calculatedMaxHeight += this.tabStripRect.height;
}
/** @internal */
canMaximize() {
if (this.isEnableMaximize()) {
// always allow maximize toggle if already maximized
if (this.getModel().getMaximizedTabset(this.getWindowId()) === this) {
return true;
}
// only one tabset, so disable
if (this.getParent() === this.getModel().getRoot(this.getWindowId()) && this.getModel().getRoot(this.getWindowId()).getChildren().length === 1) {
return false;
}
return true;
}
return false;
}
/** @internal */
setContentRect(rect: Rect) {
this.contentRect = rect;
}
/** @internal */
getContentRect() {
return this.contentRect;
}
/** @internal */
setTabStripRect(rect: Rect) {
this.tabStripRect = rect;
}
/** @internal */
setWeight(weight: number) {
this.attributes.weight = weight;
}
/** @internal */
setSelected(index: number) {
this.attributes.selected = index;
}
getWindowId() {
return (this.parent as RowNode).getWindowId();
}
/** @internal */
canDrop(dragNode: Node & IDraggable, x: number, y: number): DropInfo | undefined {
let dropInfo;
if (dragNode === this) {
const dockLocation = DockLocation.CENTER;
const outlineRect = this.tabStripRect;
dropInfo = new DropInfo(this, outlineRect!, dockLocation, -1, CLASSES.FLEXLAYOUT__OUTLINE_RECT);
} else if (this.getWindowId() !== Model.MAIN_WINDOW_ID && !canDockToWindow(dragNode)) {
return undefined;
} else if (this.contentRect!.contains(x, y)) {
let dockLocation = DockLocation.CENTER;
if (this.model.getMaximizedTabset((this.parent as RowNode).getWindowId()) === undefined) {
dockLocation = DockLocation.getLocation(this.contentRect!, x, y);
}
const outlineRect = dockLocation.getDockRect(this.rect);
dropInfo = new DropInfo(this, outlineRect, dockLocation, -1, CLASSES.FLEXLAYOUT__OUTLINE_RECT);
} else if (this.tabStripRect != null && this.tabStripRect.contains(x, y)) {
let r: Rect;
let yy: number;
let h: number;
if (this.children.length === 0) {
r = this.tabStripRect.clone();
yy = r.y + 3;
h = r.height - 4;
r.width = 2;
} else {
let child = this.children[0] as TabNode;
r = child.getTabRect()!;
yy = r.y;
h = r.height;
let p = this.tabStripRect.x;
let childCenter = 0;
for (let i = 0; i < this.children.length; i++) {
child = this.children[i] as TabNode;
r = child.getTabRect()!;
if (r.y !== yy) {
yy = r.y
p = this.tabStripRect.x;
}
childCenter = r.x + r.width / 2;
if (p <= x && x < childCenter && r.y < y && y < r.getBottom()) {
const dockLocation = DockLocation.CENTER;
const outlineRect = new Rect(r.x - 2, r.y, 3, r.height);
if (this.rect.x < r.x && r.x < this.rect.getRight()) {
dropInfo = new DropInfo(this, outlineRect, dockLocation, i, CLASSES.FLEXLAYOUT__OUTLINE_RECT);
break;
} else {
return undefined;
}
}
p = childCenter;
}
}
if (dropInfo == null && r.getRight() < this.rect!.getRight()) {
const dockLocation = DockLocation.CENTER;
const outlineRect = new Rect(r.getRight() - 2, yy, 3, h);
dropInfo = new DropInfo(this, outlineRect, dockLocation, this.children.length, CLASSES.FLEXLAYOUT__OUTLINE_RECT);
}
}
if (!dragNode.canDockInto(dragNode, dropInfo)) {
return undefined;
}
return dropInfo;
}
/** @internal */
delete() {
(this.parent as RowNode).removeChild(this);
}
/** @internal */
remove(node: TabNode) {
const removedIndex = this.removeChild(node);
this.model.tidy();
adjustSelectedIndex(this, removedIndex);
}
/** @internal */
drop(dragNode: Node, location: DockLocation, index: number, select?: boolean) {
const dockLocation = location;
if (this === dragNode) {
// tabset drop into itself
return; // dock back to itself
}
let dragParent = dragNode.getParent() as BorderNode | TabSetNode | RowNode;
let fromIndex = 0;
if (dragParent !== undefined) {
fromIndex = dragParent.removeChild(dragNode);
// if selected node in border is being docked into tabset then deselect border tabs
if (dragParent instanceof BorderNode && dragParent.getSelected() === fromIndex) {
dragParent.setSelected(-1);
} else {
adjustSelectedIndex(dragParent, fromIndex);
}
}
// if dropping a tab back to same tabset and moving to forward position then reduce insertion index
if (dragNode instanceof TabNode && dragParent === this && fromIndex < index && index > 0) {
index--;
}
// simple_bundled dock to existing tabset
if (dockLocation === DockLocation.CENTER) {
let insertPos = index;
if (insertPos === -1) {
insertPos = this.children.length;
}
if (dragNode instanceof TabNode) {
this.addChild(dragNode, insertPos);
if (select || (select !== false && this.isAutoSelectTab())) {
this.setSelected(insertPos);
}
// console.log("added child at : " + insertPos);
} else if (dragNode instanceof RowNode) {
(dragNode as RowNode).forEachNode((child, level) => {
if (child instanceof TabNode) {
this.addChild(child, insertPos);
// console.log("added child at : " + insertPos);
insertPos++;
}
}, 0);
} else {
for (let i = 0; i < dragNode.getChildren().length; i++) {
const child = dragNode.getChildren()[i];
this.addChild(child, insertPos);
// console.log("added child at : " + insertPos);
insertPos++;
}
if (this.getSelected() === -1 && this.children.length > 0) {
this.setSelected(0);
}
}
this.model.setActiveTabset(this, (this.parent as RowNode).getWindowId());
} else {
let moveNode = dragNode as TabSetNode | RowNode | TabNode;
if (dragNode instanceof TabNode) {
// create new tabset parent
// console.log("create a new tabset");
const callback = this.model.getOnCreateTabSet();
moveNode = new TabSetNode(this.model, callback ? callback(dragNode as TabNode) : {});
moveNode.addChild(dragNode);
// console.log("added child at end");
dragParent = moveNode;
} else if (dragNode instanceof RowNode) {
const parent = (this.getParent()! as RowNode);
// need to turn round if same orientation unless docking oposite direction
if (dragNode.getOrientation() === parent.getOrientation() &&
(location.getOrientation() === parent.getOrientation() || location === DockLocation.CENTER)) {
const node = new RowNode(this.model, this.getWindowId(), {});
node.addChild(dragNode);
moveNode = node;
}
} else {
moveNode = dragNode as TabSetNode;
}
const parentRow = this.parent as Node;
const pos = parentRow.getChildren().indexOf(this);
if (parentRow.getOrientation() === dockLocation.orientation) {
moveNode.setWeight(this.getWeight() / 2);
this.setWeight(this.getWeight() / 2);
// console.log("added child 50% size at: " + pos + dockLocation.indexPlus);
parentRow.addChild(moveNode, pos + dockLocation.indexPlus);
} else {
// create a new row to host the new tabset (it will go in the opposite direction)
// console.log("create a new row");
const newRow = new RowNode(this.model, this.getWindowId(), {});
newRow.setWeight(this.getWeight());
newRow.addChild(this);
this.setWeight(50);
moveNode.setWeight(50);
// console.log("added child 50% size at: " + dockLocation.indexPlus);
newRow.addChild(moveNode, dockLocation.indexPlus);
parentRow.removeChild(this);
parentRow.addChild(newRow, pos);
}
if (moveNode instanceof TabSetNode) {
this.model.setActiveTabset(moveNode, this.getWindowId());
}
}
this.model.tidy();
}
/** @internal */
updateAttrs(json: any) {
TabSetNode.attributeDefinitions.update(json, this.attributes);
}
/** @internal */
getAttributeDefinitions() {
return TabSetNode.attributeDefinitions;
}
/** @internal */
static getAttributeDefinitions() {
return TabSetNode.attributeDefinitions;
}
/** @internal */
private static createAttributeDefinitions(): AttributeDefinitions {
const attributeDefinitions = new AttributeDefinitions();
attributeDefinitions.add("type", TabSetNode.TYPE, true).setType(Attribute.STRING).setFixed();
attributeDefinitions.add("id", undefined).setType(Attribute.STRING).setDescription(
`the unique id of the tab set, if left undefined a uuid will be assigned`
);
attributeDefinitions.add("weight", 100).setType(Attribute.NUMBER).setDescription(
`relative weight for sizing of this tabset in parent row`
);
attributeDefinitions.add("selected", 0).setType(Attribute.NUMBER).setDescription(
`index of selected/visible tab in tabset`
);
attributeDefinitions.add("name", undefined).setType(Attribute.STRING);
attributeDefinitions.add("config", undefined).setType("any").setDescription(
`a place to hold json config used in your own code`
);
attributeDefinitions.addInherited("enableDeleteWhenEmpty", "tabSetEnableDeleteWhenEmpty").setDescription(
`whether to delete this tabset when is has no tabs`
);
attributeDefinitions.addInherited("enableHideWhenEmpty", "tabSetEnableHideWhenEmpty").setDescription(
"whether to hide this tabset when it has no tabs"
);
attributeDefinitions.addInherited("enableDrop", "tabSetEnableDrop").setDescription(
`allow user to drag tabs into this tabset`
);
attributeDefinitions.addInherited("enableDrag", "tabSetEnableDrag").setDescription(
`allow user to drag tabs out this tabset`
);
attributeDefinitions.addInherited("enableDivide", "tabSetEnableDivide").setDescription(
`allow user to drag tabs to region of this tabset, splitting into new tabset`
);
attributeDefinitions.addInherited("enableMaximize", "tabSetEnableMaximize").setDescription(
`allow user to maximize tabset to fill view via maximize button`
);
attributeDefinitions.addInherited("enableClose", "tabSetEnableClose").setDescription(
`allow user to close tabset via a close button`
);
attributeDefinitions.addInherited("enableSingleTabStretch", "tabSetEnableSingleTabStretch").setDescription(
`if the tabset has only a single tab then stretch the single tab to fill area and display in a header style`
);
attributeDefinitions.addInherited("classNameTabStrip", "tabSetClassNameTabStrip").setDescription(
`a class name to apply to the tab strip`
);
attributeDefinitions.addInherited("enableTabStrip", "tabSetEnableTabStrip").setDescription(
`enable tab strip and allow multiple tabs in this tabset`
);
attributeDefinitions.addInherited("minWidth", "tabSetMinWidth").setDescription(
`minimum width (in px) for this tabset`
);
attributeDefinitions.addInherited("minHeight", "tabSetMinHeight").setDescription(
`minimum height (in px) for this tabset`
);
attributeDefinitions.addInherited("maxWidth", "tabSetMaxWidth").setDescription(
`maximum width (in px) for this tabset`
);
attributeDefinitions.addInherited("maxHeight", "tabSetMaxHeight").setDescription(
`maximum height (in px) for this tabset`
);
attributeDefinitions.addInherited("enableTabWrap", "tabSetEnableTabWrap").setDescription(
`wrap tabs onto multiple lines`
);
attributeDefinitions.addInherited("tabLocation", "tabSetTabLocation").setDescription(
`the location of the tabs either top or bottom`
);
attributeDefinitions.addInherited("autoSelectTab", "tabSetAutoSelectTab").setType(Attribute.BOOLEAN).setDescription(
`whether to select new/moved tabs in tabset`
);
attributeDefinitions.addInherited("enableActiveIcon", "tabSetEnableActiveIcon").setType(Attribute.BOOLEAN).setDescription(
`whether the active icon (*) should be displayed when the tabset is active`
);
attributeDefinitions.addInherited("enableTabScrollbar", "tabSetEnableTabScrollbar").setType(Attribute.BOOLEAN).setDescription(
`whether to show a mini scrollbar for the tabs`
);
return attributeDefinitions;
}
}