forked from webpack-contrib/webpack-bundle-analyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTreemap.jsx
172 lines (150 loc) · 4.41 KB
/
Treemap.jsx
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
import {Component} from 'preact';
import FoamTree from '@carrotsearch/foamtree';
export default class Treemap extends Component {
constructor(props) {
super(props);
this.treemap = null;
this.zoomOutDisabled = false;
}
componentDidMount() {
this.treemap = this.createTreemap();
window.addEventListener('resize', this.resize);
}
componentWillReceiveProps(nextProps) {
if (nextProps.data !== this.props.data) {
this.treemap.set({
dataObject: this.getTreemapDataObject(nextProps.data)
});
} else if (nextProps.highlightGroups !== this.props.highlightGroups) {
const groupsToRedraw = [
...nextProps.highlightGroups,
...this.props.highlightGroups
];
setTimeout(() => this.treemap.redraw(false, groupsToRedraw));
}
}
shouldComponentUpdate() {
return false;
}
componentWillUnmount() {
window.removeEventListener('resize', this.resize);
this.treemap.dispose();
}
render() {
return (
<div {...this.props} ref={this.saveNodeRef}/>
);
}
saveNodeRef = node => (this.node = node);
getTreemapDataObject(data = this.props.data) {
return {groups: data};
}
createTreemap() {
const component = this;
const {props} = this;
return new FoamTree({
element: this.node,
layout: 'squarified',
stacking: 'flattened',
pixelRatio: window.devicePixelRatio || 1,
maxGroups: Infinity,
maxGroupLevelsDrawn: Infinity,
maxGroupLabelLevelsDrawn: Infinity,
maxGroupLevelsAttached: Infinity,
wireframeLabelDrawing: 'always',
groupMinDiameter: 0,
groupLabelVerticalPadding: 0.2,
rolloutDuration: 0,
pullbackDuration: 0,
fadeDuration: 0,
groupExposureZoomMargin: 0.2,
zoomMouseWheelDuration: 300,
openCloseDuration: 200,
dataObject: this.getTreemapDataObject(),
titleBarDecorator(opts, props, vars) {
vars.titleBarShown = false;
},
groupColorDecorator(options, properties, variables) {
const {highlightGroups} = component.props;
const module = properties.group;
if (highlightGroups && highlightGroups.has(module)) {
variables.groupColor = {
model: 'rgba',
r: 255,
g: 0,
b: 0,
a: 0.8
};
}
},
/**
* Handle Foamtree's "group clicked" event
* @param {FoamtreeEvent} event - Foamtree event object
* (see https://get.carrotsearch.com/foamtree/demo/api/index.html#event-details)
* @returns {void}
*/
onGroupClick(event) {
preventDefault(event);
if ((event.ctrlKey || event.secondary) && props.onGroupSecondaryClick) {
props.onGroupSecondaryClick.call(component, event);
return;
}
component.zoomOutDisabled = false;
this.zoom(event.group);
},
onGroupDoubleClick: preventDefault,
onGroupHover(event) {
// Ignoring hovering on `FoamTree` branding group and the root group
if (event.group && (event.group.attribution || event.group === this.get('dataObject'))) {
event.preventDefault();
if (props.onMouseLeave) {
props.onMouseLeave.call(component, event);
}
return;
}
if (props.onGroupHover) {
props.onGroupHover.call(component, event);
}
},
onGroupMouseWheel(event) {
const {scale} = this.get('viewport');
const isZoomOut = (event.delta < 0);
if (isZoomOut) {
if (component.zoomOutDisabled) return preventDefault(event);
if (scale < 1) {
component.zoomOutDisabled = true;
preventDefault(event);
}
} else {
component.zoomOutDisabled = false;
}
}
});
}
zoomToGroup(group) {
this.zoomOutDisabled = false;
while (group && !this.treemap.get('state', group).revealed) {
group = this.treemap.get('hierarchy', group).parent;
}
if (group) {
this.treemap.zoom(group);
}
}
isGroupRendered(group) {
const groupState = this.treemap.get('state', group);
return !!groupState && groupState.revealed;
}
update() {
this.treemap.update();
}
resize = () => {
const {props} = this;
this.treemap.resize();
if (props.onResize) {
props.onResize();
}
}
}
function preventDefault(event) {
event.preventDefault();
}