forked from algorithm-visualizer/algorithm-visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLayout.js
55 lines (46 loc) · 1.4 KB
/
Layout.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
import React from 'react';
import { ResizableContainer } from 'components';
import { HorizontalLayout } from 'core/layouts';
class Layout {
constructor(key, getObject, children) {
this.key = key;
this.getObject = getObject;
this.children = children.map(key => this.getObject(key));
this.weights = children.map(() => 1);
this.ref = React.createRef();
this.handleChangeWeights = this.handleChangeWeights.bind(this);
}
add(key, index = this.children.length) {
const child = this.getObject(key);
this.children.splice(index, 0, child);
this.weights.splice(index, 0, 1);
}
remove(key) {
const child = this.getObject(key);
const index = this.children.indexOf(child);
if (~index) {
this.children.splice(index, 1);
this.weights.splice(index, 1);
}
}
removeAll() {
this.children = [];
this.weights = [];
}
handleChangeWeights(weights) {
this.weights.splice(0, this.weights.length, ...weights);
this.ref.current.forceUpdate();
}
render() {
const horizontal = this instanceof HorizontalLayout;
return (
<ResizableContainer key={this.key} ref={this.ref} weights={this.weights} horizontal={horizontal}
onChangeWeights={this.handleChangeWeights}>
{
this.children.map(tracer => tracer && tracer.render())
}
</ResizableContainer>
);
}
}
export default Layout;