UNPKG

react-sigma

Version:

Lightweight but powerful library for drawing network graphs built on top of SigmaJS

147 lines (115 loc) 6.79 kB
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } 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; } 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) **/ var NeoCypher = /*#__PURE__*/function (_React$Component) { _inherits(NeoCypher, _React$Component); var _super = _createSuper(NeoCypher); function NeoCypher(props) { var _this; _classCallCheck(this, NeoCypher); if (!props.producers) { props.producers = new NeoGraphItemsProducers(); } _this = _super.call(this, props); _defineProperty(_assertThisInitialized(_this), "onLoad", function () { _this.setState({ loaded: true }); if (_this.props.sigma) _this.props.sigma.refresh(); if (_this.props.onGraphLoaded) return _this.props.onGraphLoaded(); }); _this.state = { loaded: false }; return _this; } _createClass(NeoCypher, [{ key: "componentDidMount", value: function componentDidMount() { this._runQuery(this.props.query); } }, { key: "componentWillUpdate", value: function 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); } } }, { key: "render", value: function render() { if (!this.state.loaded) return null; return /*#__PURE__*/React.createElement("div", null, embedProps(this.props.children, { sigma: this.props.sigma })); } }, { key: "_runQuery", value: function _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); } }]); return NeoCypher; }(React.Component); _defineProperty(NeoCypher, "propTypes", { url: PropTypes.string.isRequired, user: PropTypes.string.isRequired, password: PropTypes.string.isRequired, query: PropTypes.string.isRequired, producers: PropTypes.shape({ node: PropTypes.func.isRequired, edge: PropTypes.func.isRequired }), onGraphLoaded: PropTypes.func, children: PropTypes.any, sigma: function (_sigma) { function sigma() { return _sigma.apply(this, arguments); } sigma.toString = function () { return _sigma.toString(); }; return sigma; }(function () { return (typeof sigma === "function" ? PropTypes.instanceOf(sigma) : PropTypes.any).apply(this, arguments); }) }); export default NeoCypher; import PropTypes from "prop-types";