-
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathDockGraphDeserializer.ts
More file actions
136 lines (121 loc) · 5.5 KB
/
Copy pathDockGraphDeserializer.ts
File metadata and controls
136 lines (121 loc) · 5.5 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
import { DockManager } from "./DockManager.js";
import { DockModel } from "./DockModel.js";
import { DockNode } from "./DockNode.js";
import { PanelContainer } from "./PanelContainer.js";
import { HorizontalDockContainer } from "./HorizontalDockContainer.js";
import { VerticalDockContainer } from "./VerticalDockContainer.js";
import { DocumentManagerContainer } from "./DocumentManagerContainer.js";
import { FillDockContainer } from "./FillDockContainer.js";
import { Dialog } from "./Dialog.js";
import { Utils } from "./Utils.js";
import { IPanelInfo } from "./interfaces/IPanelInfo.js";
import { INodeInfo } from "./interfaces/INodeInfo.js";
import { IDockContainer } from "./interfaces/IDockContainer.js";
/**
* Deserializes the dock layout hierarchy from JSON and creates a dock hierarhcy graph
*/
export class DockGraphDeserializer {
dockManager: DockManager;
documentManagerNode: DockNode;
constructor(dockManager: DockManager) {
this.dockManager = dockManager;
}
async deserialize(_json: string): Promise<DockModel> {
let info = JSON.parse(_json);
let model = new DockModel();
model.rootNode = await this._buildGraph(info.graphInfo);
model.dialogs = await this._buildDialogs(info.dialogsInfo);
model.documentManagerNode = this.documentManagerNode;
return model;
}
async _buildGraph(nodeInfo: INodeInfo) {
let childrenInfo = nodeInfo.children;
let children: DockNode[] = [];
for (let childInfo of childrenInfo) {
let childNode = await this._buildGraph(childInfo);
if (childNode !== null) {
children.push(childNode);
}
};
// Build the container owned by this node
let container = await this._createContainer(nodeInfo, children);
if (container === null) {
return null;
}
// Build the node for this container and attach it's children
let node = new DockNode(container);
if (container instanceof DocumentManagerContainer)
this.documentManagerNode = node;
node.children = children;
for (let childNode of node.children.reverse()) {
childNode.parent = node;
};
node.children.reverse();
// node.container.setActiveChild(node.container);
return node;
}
async _createContainer(nodeInfo: INodeInfo, children: DockNode[]) {
let containerType = nodeInfo.containerType;
let containerState = nodeInfo.state;
let container;
let childContainers: IDockContainer[] = [];
for (let childNode of children) {
childContainers.push(childNode.container);
}
if (containerType === 'panel') {
container = await PanelContainer.loadFromState(containerState, this.dockManager);
if (!container.prepareForDocking)
return null;
container.prepareForDocking();
Utils.removeNode(container.elementPanel);
}
else if (containerType === 'horizontal')
container = new HorizontalDockContainer(this.dockManager, childContainers);
else if (containerType === 'vertical')
container = new VerticalDockContainer(this.dockManager, childContainers);
else if (containerType === 'fill') {
// Check if this is a document manager
// TODO: Layout engine compares the string 'fill', so cannot create another subclass type
// called document_manager and have to resort to this hack. use RTTI in layout engine
let typeDocumentManager = containerState.documentManager;
if (typeDocumentManager)
container = new DocumentManagerContainer(this.dockManager);
else
container = new FillDockContainer(this.dockManager);
}
else
throw new Error('Cannot create dock container of unknown type: ' + containerType);
// Restore the state of the container
container.loadState(containerState);
// container.performLayout(childContainers);
return container;
}
async _buildDialogs(dialogsInfo: IPanelInfo[]) {
let dialogs: Dialog[] = [];
for (let dialogInfo of dialogsInfo) {
let containerType = dialogInfo.containerType;
let containerState = dialogInfo.state;
let container;
if (containerType === 'panel') {
container = await PanelContainer.loadFromState(containerState, this.dockManager);
if (container.prepareForDocking) {
Utils.removeNode(container.elementPanel);
container.isDialog = true;
let dialog = new Dialog(container, this.dockManager);
dialog.initialize();
if (dialogInfo.position.x > document.body.clientWidth ||
dialogInfo.position.y > document.body.clientHeight - 70) {
dialogInfo.position.x = 20;
dialogInfo.position.y = 70;
}
dialog.setPosition(dialogInfo.position.x, dialogInfo.position.y);
dialog.isHidden = dialogInfo.isHidden;
if (dialog.isHidden)
dialog.hide();
dialogs.push(dialog);
}
}
}
return dialogs;
}
}