UNPKG

react-sigma

Version:

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

110 lines (82 loc) 3.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireDefault(require("react")); var Utils = _interopRequireWildcard(require("./Utils")); var _propTypes = _interopRequireDefault(require("prop-types")); function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } 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; } /** ReactSigmaLayoutPlugin is a base class for sigma plugins. Usage ``` const NOverlap = (props: Props) => { const s = props.sigma if(s) return <ReactSigmaLayoutPlugin start={()=>s.startNoverlap()} config={options=>s.configNoverlap(options)} stop={s.stopNoverlap()} {...props} /> return null } ... <NOverlap easing="cubicInOut"/> ``` **/ class ReactSigmaLayoutPlugin extends _react.default.Component { constructor(props) { super(props); _defineProperty(this, "_mounted", false); _defineProperty(this, "render", () => null); this.state = { running: false }; } componentDidMount() { this._start(); this._mounted = true; } // Change sigma status only after react rendering complete componentDidUpdate(prevProps, prevState) { if (prevState.running && !this.state.running && this._mounted) { if (this.props.sigma) this.props.sigma.refresh(); } else if (Utils.propsChanged(prevProps, this.props)) { this.props.stop(); this._start(); } } componentWillUnmount() { this._mounted = false; this.props.stop(); } //TODO: Render composition of child components after animation _start() { this.props.config(ReactSigmaLayoutPlugin._stripOptions(this.props)); let listener = this.props.start(); listener.bind('stop', () => { this._mounted && this.setState({ running: false }); }); this.setState({ running: true }); } static _stripOptions(props) { let config = {}; for (let key in props) if (key !== "start" && key !== "stop" && key !== "config" && key !== "sigma" && key !== "children") config[key] = props[key]; return config; } } _defineProperty(ReactSigmaLayoutPlugin, "propTypes", { config: _propTypes.default.func.isRequired, start: _propTypes.default.func.isRequired, stop: _propTypes.default.func.isRequired, sigma: function () { return (typeof sigma === "function" ? _propTypes.default.instanceOf(sigma) : _propTypes.default.any).apply(this, arguments); } }); var _default = ReactSigmaLayoutPlugin; exports.default = _default;