UNPKG

react-sigma-conglei

Version:

Lightweight but powerful library for drawing network graphs built on top of dunnock/react-sigma

83 lines (67 loc) 3.02 kB
import React from 'react'; import '../sigma/parsers.json.js'; import '../sigma/neo4j.cypher'; import NeoGraphItemsProducers from './NeoGraphItemsProducers'; import { embedProps } from './tools'; ; /** NeoCypher component, interface for neo4j.cypher sigma plugin. Can be used within Sigma component. Can be composed with other plugins: on load it mounts all child components (e.g. other sigma plugins). Child's componentWillMount should be used to enable plugins on loaded graph. @param {string} url Neo4j instance REST API URL @param {string} user Neo4j instance REST API user @param {string} password Neo4j instance REST API password @param {string} query Neo4j cypher query @param {NeoGraphItemsProducers} producers Optional transformer for creating Sigma nodes and edges, instance compatible with NeoGraphItemsProducers @param {Function} onGraphLoaded Optional callback for graph update [see sigma plugin page for more details](https://github.com/jacomyal/sigma.js/tree/master/plugins/sigma.neo4j.cypher) **/ class NeoCypher extends React.PureComponent { constructor(props) { super(props); this.state = { loaded: false }; this.onLoad = this._onLoad.bind(this); } componentDidMount() { this._runQuery(this.props.query); } componentWillUpdate(props) { // suppose url, user or password won't change for sigma instance, as well as sigma instance itself if (this.props.query !== props.query) { this.setState({ loaded: false }); this._runQuery(props.query); } } render() { if (!this.state.loaded) return null; return React.createElement( 'div', null, embedProps(this.props.children, { sigma: this.props.sigma }) ); } _runQuery(query) { // TODO: add exception handling capability to Sigma Neo4j plugin sigma.neo4j.cypher({ url: this.props.url, user: this.props.user, password: this.props.password }, query, this.props.sigma, this.onLoad, this.props.producers); } _onLoad() { this.setState({ loaded: true }); if (this.props.sigma) this.props.sigma.refresh(); if (this.props.onGraphLoaded) return this.props.onGraphLoaded(); } } NeoCypher.defaultProps = { producers: new NeoGraphItemsProducers() }; NeoCypher.propTypes = { url: require('prop-types').string.isRequired, user: require('prop-types').string.isRequired, password: require('prop-types').string.isRequired, query: require('prop-types').string.isRequired, producers: typeof ProducersInterface === 'function' ? require('prop-types').instanceOf(ProducersInterface).isRequired : require('prop-types').any.isRequired, onGraphLoaded: require('prop-types').func, children: require('prop-types').any, sigma: typeof sigma === 'function' ? require('prop-types').instanceOf(sigma) : require('prop-types').any }; export default NeoCypher;