forked from algorithm-visualizer/algorithm-visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
67 lines (60 loc) · 2.25 KB
/
Copy pathindex.js
File metadata and controls
67 lines (60 loc) · 2.25 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
import React from 'react';
import { classes } from 'common/util';
import { Divider } from 'components';
import styles from './ResizableContainer.module.scss';
class ResizableContainer extends React.Component {
handleResize(prevIndex, index, targetElement, clientX, clientY) {
const { horizontal, visibles, onChangeWeights } = this.props;
const weights = [...this.props.weights];
const { left, top } = targetElement.getBoundingClientRect();
const { offsetWidth, offsetHeight } = targetElement.parentElement;
const position = horizontal ? clientX - left : clientY - top;
const containerSize = horizontal ? offsetWidth : offsetHeight;
let totalWeight = 0;
let subtotalWeight = 0;
weights.forEach((weight, i) => {
if (visibles && !visibles[i]) return;
totalWeight += weight;
if (i < index) subtotalWeight += weight;
});
const newWeight = position / containerSize * totalWeight;
let deltaWeight = newWeight - subtotalWeight;
deltaWeight = Math.max(deltaWeight, -weights[prevIndex]);
deltaWeight = Math.min(deltaWeight, weights[index]);
weights[prevIndex] += deltaWeight;
weights[index] -= deltaWeight;
onChangeWeights(weights);
}
render() {
const { className, children, horizontal, weights, visibles } = this.props;
const elements = [];
let lastIndex = -1;
const totalWeight = weights.filter((weight, i) => !visibles || visibles[i])
.reduce((sumWeight, weight) => sumWeight + weight, 0);
children.forEach((child, i) => {
if (!visibles || visibles[i]) {
if (~lastIndex) {
const prevIndex = lastIndex;
elements.push(
<Divider key={`divider-${i}`} horizontal={horizontal}
onResize={((target, dx, dy) => this.handleResize(prevIndex, i, target, dx, dy))} />,
);
}
elements.push(
<div key={i} className={classes(styles.wrapper)} style={{
flexGrow: weights[i] / totalWeight,
}}>
{child}
</div>,
);
lastIndex = i;
}
});
return (
<div className={classes(styles.resizable_container, horizontal && styles.horizontal, className)}>
{elements}
</div>
);
}
}
export default ResizableContainer;