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
|
import * as GoldenLayout from "golden-layout";
import 'golden-layout/src/css/goldenlayout-base.css';
import 'golden-layout/src/css/goldenlayout-dark-theme.css';
import { action, computed, observable, reaction, trace } from "mobx";
import { DragManager } from "../../util/DragManager";
import { DocumentView } from "../nodes/DocumentView";
import { Document } from "../../../fields/Document";
import "./CollectionDockingView.scss";
import { CollectionViewBase, COLLECTION_BORDER_WIDTH, CollectionViewProps } from "./CollectionViewBase";
import React = require("react");
import * as ReactDOM from 'react-dom';
import Measure from "react-measure";
import { Utils } from "../../../Utils";
import { FieldId, FieldWaiting, Field } from "../../../fields/Field";
import { Server } from "../../Server";
import { observer } from "mobx-react";
import { ListField } from "../../../fields/ListField";
import { KeyStore } from "../../../fields/KeyStore";
import { Opt } from "../../../fields/Field";
import { TextField } from "../../../fields/TextField";
@observer
export class CollectionDockingView extends CollectionViewBase {
public static Instance: CollectionDockingView;
public static LayoutString() { return CollectionViewBase.LayoutString("CollectionDockingView"); }
public static makeDocumentConfig(document: Document) {
return {
type: 'react-component',
component: 'DocumentFrameRenderer',
title: document.Title,
props: {
documentId: document.Id
}
}
}
private _goldenLayout: any = null;
private _dragDiv: any = null;
private _dragParent: HTMLElement | null = null;
private _dragElement: HTMLDivElement | undefined;
private _dragFakeElement: HTMLDivElement | undefined;
private _containerRef = React.createRef<HTMLDivElement>();
private _makeFullScreen: boolean = false;
private _maximizedStack: any = null;
private _forceRecreate: boolean = false;
constructor(props: CollectionViewProps) {
super(props);
CollectionDockingView.Instance = this;
(window as any).React = React;
(window as any).ReactDOM = ReactDOM;
}
public StartOtherDrag(dragElement: HTMLDivElement, dragDoc: Document) {
this._dragElement = dragElement;
this._dragParent = dragElement.parentElement;
// bcz: we want to copy this document into the header, not move it there.
// However, GoldenLayout is setup to move things, so we have to do some kludgy stuff:
// - create a temporary invisible div and register that as a DragSource with GoldenLayout
this._dragDiv = document.createElement("div");
this._dragDiv.style.opacity = 0;
DragManager.Root().appendChild(this._dragDiv);
this._goldenLayout.createDragSource(this._dragDiv, CollectionDockingView.makeDocumentConfig(dragDoc));
// - add our document to that div so that GoldenLayout will get the move events its listening for
this._dragDiv.appendChild(this._dragElement);
// - add a duplicate of our document to the original document's container
// (GoldenLayout will be removing our original one)
this._dragFakeElement = dragElement.cloneNode(true) as HTMLDivElement;
this._dragParent!.appendChild(this._dragFakeElement);
// all of this must be undone when the document has been dropped (see tabCreated)
}
public OpenFullScreen(document: Document) {
this._makeFullScreen = true;
this._goldenLayout.root.contentItems[0].addChild(CollectionDockingView.makeDocumentConfig(document));
}
public CloseFullScreen() {
if (this._maximizedStack) {
this._maximizedStack.header.controlsContainer.find('.lm_close').click();
this._maximizedStack = null;
}
}
//
// Creates a vertical split on the right side of the docking view, and then adds the Document to that split
//
public AddRightSplit(document: Document) {
let newItemStackConfig = {
type: 'stack',
content: [CollectionDockingView.makeDocumentConfig(document)]
};
var newContentItem = new this._goldenLayout._typeToItem[newItemStackConfig.type](this._goldenLayout, newItemStackConfig, null);
if (this._goldenLayout.root.contentItems[0].isRow) {
this._goldenLayout.root.contentItems[0].addChild(newContentItem);
}
else {
var collayout = this._goldenLayout.root.contentItems[0];
var newRow = collayout.layoutManager.createContentItem({ type: "row" }, this._goldenLayout);
collayout.parent.replaceChild(collayout, newRow);
newRow.addChild(newContentItem, undefined, true);
newRow.addChild(collayout, 0, true);
collayout.config["width"] = 50;
newContentItem.config["width"] = 50;
}
this.stateChanged(); // shouldn't need to do either of these ... but our react component doesn't render when we add it manually like this.
this.setupGoldenLayout(true); // this forces it to render by the brute force method of recreating the whole golden layout
}
setupGoldenLayout(force: boolean = false) {
var config = this.props.Document.GetText(KeyStore.Data, "");
if (config) {
if (!this._goldenLayout)
this._goldenLayout = new GoldenLayout(JSON.parse(config));
else {
if (!force && JSON.stringify(this._goldenLayout.toConfig()) == JSON.stringify(JSON.parse(config)))
return;
this._goldenLayout.destroy();
this._goldenLayout = new GoldenLayout(JSON.parse(config));
}
this._goldenLayout.on('tabCreated', this.tabCreated);
this._goldenLayout.on('stackCreated', this.stackCreated);
this._goldenLayout.registerComponent('DocumentFrameRenderer', DockedFrameRenderer);
this._goldenLayout.container = this._containerRef.current;
this._goldenLayout.init();
this._goldenLayout.on('stateChanged', this.stateChanged);
}
}
componentDidMount: () => void = () => {
if (this._containerRef.current) {
reaction(
() => this.props.Document.GetText(KeyStore.Data, ""),
() => this.setupGoldenLayout(), { fireImmediately: true });
window.addEventListener('resize', this.onResize); // bcz: would rather add this event to the parent node, but resize events only come from Window
}
}
componentWillUnmount: () => void = () => {
window.removeEventListener('resize', this.onResize);
}
@action
onResize = (event: any) => {
var cur = this._containerRef.current;
// bcz: since GoldenLayout isn't a React component itself, we need to notify it to resize when its document container's size has changed
this._goldenLayout.updateSize(cur!.getBoundingClientRect().width, cur!.getBoundingClientRect().height);
}
@action
onPointerDown = (e: React.PointerEvent): void => {
if (e.button === 2 && this.active) {
e.stopPropagation();
e.preventDefault();
} else {
if (e.buttons === 1 && this.active) {
e.stopPropagation();
}
}
}
stateChanged = () => {
// if (!this._pointerIsDown) {
var json = JSON.stringify(this._goldenLayout.toConfig());
this.props.Document.SetText(KeyStore.Data, json)
//}
}
tabCreated = (tab: any) => {
if (this._dragDiv) {
this._dragDiv.removeChild(this._dragElement);
this._dragParent!.removeChild(this._dragFakeElement!);
this._dragParent!.appendChild(this._dragElement!);
DragManager.Root().removeChild(this._dragDiv);
this._dragDiv = null;
this.stateChanged();
}
tab.closeElement.off('click') //unbind the current click handler
.click(function () {
tab.contentItem.remove();
});
}
stackCreated = (stack: any) => {
if (this._makeFullScreen) {
this._maximizedStack = stack;
setTimeout(function () { stack.header.controlsContainer.find('.lm_maximise').click() }, 10);
this._makeFullScreen = false;
}
//stack.header.controlsContainer.find('.lm_popout').hide();
stack.header.controlsContainer.find('.lm_close') //get the close icon
.off('click') //unbind the current click handler
.click(function () {
//if (confirm('really close this?')) {
stack.remove();
//}
});
}
render() {
this.props.Document.GetNumber(KeyStore.Width, 0); // bcz: needed to force render when window size changes
this.props.Document.GetNumber(KeyStore.Height, 0);
return (
<div className="collectiondockingview-container" id="menuContainer"
onPointerDown={this.onPointerDown} onContextMenu={(e) => e.preventDefault()} ref={this._containerRef}
style={{
width: "100%",
height: "100%",
borderStyle: "solid",
borderWidth: `${COLLECTION_BORDER_WIDTH}px`,
}} />
);
}
}
interface DockedFrameProps {
documentId: FieldId,
}
@observer
export class DockedFrameRenderer extends React.Component<DockedFrameProps> {
_mainCont: any = null;
constructor(props: any) {
super(props);
Server.GetField(this.props.documentId, (f) => { this.Document = f as Document; })
}
setMainCont: any = (element: any) => {
this._mainCont = element;
}
@observable
private _parentScaling = 1; // used to transfer the dimensions of the content pane in the DOM to the ParentScaling prop of the DocumentView
@observable
private Document: Opt<Document>;
@action
setScaling = (r: any) => {
let nativeWidth = this.Document!.GetNumber(KeyStore.NativeWidth, 0);
let nativeHeight = this.Document!.GetNumber(KeyStore.NativeWidth, 0);
this._parentScaling = nativeWidth > 0 ? r.entry.width / nativeWidth : 1;
this._nativeWidth = r.entry.width ? r.entry.width : nativeWidth;
this._nativeHeight = nativeWidth ? r.entry.width / nativeWidth * nativeHeight : nativeHeight;
}
@observable
private _nativeWidth = 0;
@observable
private _nativeHeight = 0;
render() {
if (!this.Document)
return <div></div>
let nativeWidth = this.Document.GetNumber(KeyStore.NativeWidth, 0);
var layout = this.Document.GetText(KeyStore.Layout, "");
var content =
<div ref={this.setMainCont}>
<DocumentView key={this.Document.Id} Document={this.Document}
AddDocument={undefined}
RemoveDocument={undefined}
Scaling={this._parentScaling}
PanelSize={[this._nativeWidth, this._nativeHeight]}
ScreenToLocalTransform={() => {
let { scale, translateX, translateY } = Utils.GetScreenTransform(this._mainCont);
var props = CollectionDockingView.Instance.props;
return props.ScreenToLocalTransform().translate(-translateX, -translateY).scale(scale / this._parentScaling)
}}
isTopMost={true}
ContainingCollectionView={undefined} />
</div>
if (nativeWidth > 0 &&
(layout.indexOf("CollectionFreeForm") == -1 || layout.indexOf("AnnotationsKey") != -1)) { // contents of documents should be scaled if document is not a freeform view, or if the freeformview is an annotation layer (presumably on a document that is not a freeformview)
return <Measure onResize={this.setScaling}>
{({ measureRef }) => <div ref={measureRef}> {content} </div>}
</Measure>
}
return content
}
}
|