react-sigma
Version:
Lightweight but powerful library for drawing network graphs built on top of SigmaJS
114 lines (88 loc) • 3.57 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireDefault(require("react"));
require("../sigma/parsers.json.js");
require("../sigma/neo4j.cypher");
var _NeoGraphItemsProducers = _interopRequireDefault(require("./NeoGraphItemsProducers"));
var _tools = require("./tools");
var _propTypes = _interopRequireDefault(require("prop-types"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
;
/**
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.default.Component {
constructor(props) {
if (!props.producers) {
props.producers = new _NeoGraphItemsProducers.default();
}
super(props);
_defineProperty(this, "onLoad", () => {
this.setState({
loaded: true
});
if (this.props.sigma) this.props.sigma.refresh();
if (this.props.onGraphLoaded) return this.props.onGraphLoaded();
});
this.state = {
loaded: false
};
}
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 /*#__PURE__*/_react.default.createElement("div", null, (0, _tools.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);
}
}
_defineProperty(NeoCypher, "propTypes", {
url: _propTypes.default.string.isRequired,
user: _propTypes.default.string.isRequired,
password: _propTypes.default.string.isRequired,
query: _propTypes.default.string.isRequired,
producers: _propTypes.default.shape({
node: _propTypes.default.func.isRequired,
edge: _propTypes.default.func.isRequired
}),
onGraphLoaded: _propTypes.default.func,
children: _propTypes.default.any,
sigma: function () {
return (typeof sigma === "function" ? _propTypes.default.instanceOf(sigma) : _propTypes.default.any).apply(this, arguments);
}
});
var _default = NeoCypher;
exports.default = _default;