import _ from 'underscore';
import React from 'react';
import './svgExporter'; // create handlers for SVG and PNG download buttons
var Graphers = {};
export default function Grapher(Graph) {
return class extends React.Component {
constructor(props) {
super(props);
this.state = { collapsed: this.props.collapsed };
}
collapseId () {
return Graph.collapseId(this.props);
}
render () {
var cssClasses = Graph.className() + ' grapher';
return (
{ this.state.collapsed ?
this.plusIcon() : this.minusIcon() }
{Graph.name()}
{ !this.state.collapsed && this.graphLinksJSX() }
{ this.svgContainerJSX() }
);
}
minusIcon () {
return (
);
}
plusIcon () {
return (
);
}
graphLinksJSX () {
return (
);
}
svgContainerJSX () {
var cssClasses = Graph.className() + ' svg-container collapse';
if (!this.state.collapsed) cssClasses += ' in';
return (
);
}
componentDidMount () {
Graphers[this.collapseId()] = this;
// Draw visualisation for the first time. Visualisations are
// redrawn when browser window is resized.
this.draw();
}
componentDidUpdate () {
// Re-draw visualisation when the component change state.
this.draw();
}
svgContainer () {
return $(React.findDOMNode(this.refs.svgContainer));
}
draw () {
// Clean slate.
this.svgContainer().empty();
this.graph = null;
// Draw if uncollapsed.
if (this.state.collapsed) { return }
this.graph = new Graph(this.svgContainer(), this.props);
this.svgContainer().find('svg').attr('data-name', Graph.dataName(this.props));
}
};
}
// Redraw if window resized.
$(window).resize(_.debounce(function () {
_.each(Graphers, (grapher) => {
grapher.draw();
});
}, 125));
// Swap-icon and toggle .graph-links on collapse.
$('body').on('hidden.bs.collapse', ".collapse", function () {
var component = Graphers[$(this).attr('id')];
if (component) {
component.setState({ collapsed: true });
}
});
$('body').on('shown.bs.collapse', ".collapse", function () {
var component = Graphers[$(this).attr('id')];
if (component) {
component.setState({ collapsed: false });
}
});