react-sigma
Version:
Lightweight but powerful library for drawing network graphs built on top of SigmaJS
149 lines (114 loc) • 6.32 kB
JavaScript
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 * as Utils from './Utils';
/**
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"/>
```
**/
var ReactSigmaLayoutPlugin = /*#__PURE__*/function (_React$Component) {
_inherits(ReactSigmaLayoutPlugin, _React$Component);
var _super = _createSuper(ReactSigmaLayoutPlugin);
function ReactSigmaLayoutPlugin(props) {
var _this;
_classCallCheck(this, ReactSigmaLayoutPlugin);
_this = _super.call(this, props);
_defineProperty(_assertThisInitialized(_this), "_mounted", false);
_defineProperty(_assertThisInitialized(_this), "render", function () {
return null;
});
_this.state = {
running: false
};
return _this;
}
_createClass(ReactSigmaLayoutPlugin, [{
key: "componentDidMount",
value: function componentDidMount() {
this._start();
this._mounted = true;
} // Change sigma status only after react rendering complete
}, {
key: "componentDidUpdate",
value: function 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();
}
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
this._mounted = false;
this.props.stop();
} //TODO: Render composition of child components after animation
}, {
key: "_start",
value: function _start() {
var _this2 = this;
this.props.config(ReactSigmaLayoutPlugin._stripOptions(this.props));
var listener = this.props.start();
listener.bind('stop', function () {
_this2._mounted && _this2.setState({
running: false
});
});
this.setState({
running: true
});
}
}], [{
key: "_stripOptions",
value: function _stripOptions(props) {
var config = {};
for (var key in props) {
if (key !== "start" && key !== "stop" && key !== "config" && key !== "sigma" && key !== "children") config[key] = props[key];
}
return config;
}
}]);
return ReactSigmaLayoutPlugin;
}(React.Component);
_defineProperty(ReactSigmaLayoutPlugin, "propTypes", {
config: PropTypes.func.isRequired,
start: PropTypes.func.isRequired,
stop: PropTypes.func.isRequired,
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 ReactSigmaLayoutPlugin;
import PropTypes from "prop-types";