This repository has been archived by the owner on Aug 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
GoldenLayout.js
93 lines (83 loc) · 2.83 KB
/
GoldenLayout.js
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
// @flow
import Context from './Context';
import React from 'react';
import type { ChildrenArray } from 'react';
import GoldenLayout from 'golden-layout';
type GoldenLayoutComponentState = {|
goldenLayout : ?GoldenLayout;
goldenLayoutResizer : ?Function;
|};
/**
* React properties for the {@link GoldenLayoutComponent} component.
*/
export type GoldenLayoutComponentProps = {|
children: ChildrenArray<any>;
className?: string;
goldenLayoutRef?: (GoldenLayout) => mixed;
|};
/**
* Create a GoldenLayout instance
* @example
* import 'golden-layout/src/css/goldenlayout-base.css';
* import 'golden-layout/src/css/goldenlayout-dark-theme.css';
* import GoldenLayout, { Row, Stack, createGoldenLayoutComponent } from 'react-golden-layout';
* ...
* <GoldenLayout>
* <Row>
* <Stack>
* {
* // To apply properties to a React component, use createGoldenLayoutComponent
* }
* {createGoldenLayoutComponent(Foo, { isClosable: false, title: "Foo's Title" })}
* </Stack>
* <Stack>
* {
* // If you don't want any special settings you can just pass
* // React components directly.
* }
* {Bar}
* </Stack>
* </Row>
* </GoldenLayout>
*/
export default class GoldenLayoutComponent extends React.PureComponent<GoldenLayoutComponentProps, GoldenLayoutComponentState> {
constructor(props : GoldenLayoutComponentProps) {
super(props);
this.state = {
goldenLayout: null,
goldenLayoutResizer: null
}
}
componentWillUnmount() {
if (this.state.goldenLayout != null) {
this.state.goldenLayout.destroy();
window.removeEventListener('resize', this.state.goldenLayoutResizer);
}
}
render() {
return <div ref={this.__gotDiv.bind(this)} className={this.props.className}>
<Context.Provider value={this.state.goldenLayout ? this.state.goldenLayout.root : null}>
{this.props.children}
</Context.Provider>
</div>
}
__gotDiv(div : ?HTMLElement) {
if (div && !this.state.goldenLayout) {
const layout = new GoldenLayout({ content: [] }, div);
layout.init();
layout.on('initialised', () => {
if (this.state.goldenLayout === layout) return;
const goldenLayoutResizer = () => {
layout.updateSize();
};
window.addEventListener('resize', goldenLayoutResizer);
this.setState({
goldenLayout: layout,
goldenLayoutResizer
});
if (this.props.goldenLayoutRef)
this.props.goldenLayoutRef(layout);
});
}
}
};