UNPKG

react-bpmn

Version:

Embed BPMN 2.0 diagrams in your React app

230 lines (192 loc) 6.86 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('react'), require('bpmn-js/dist/bpmn-navigated-viewer.production.min.js')) : typeof define === 'function' && define.amd ? define(['react', 'bpmn-js/dist/bpmn-navigated-viewer.production.min.js'], factory) : (global = global || self, global.ReactBpmn = factory(global.React, global.BpmnJS)); }(this, (function (React, BpmnJS) { 'use strict'; React = React && React.hasOwnProperty('default') ? React['default'] : React; BpmnJS = BpmnJS && BpmnJS.hasOwnProperty('default') ? BpmnJS['default'] : BpmnJS; 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 _typeof2(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof2 = function _typeof2(obj) { return typeof obj; }; } else { _typeof2 = function _typeof2(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof2(obj); } function _typeof(obj) { if (typeof Symbol === "function" && _typeof2(Symbol.iterator) === "symbol") { _typeof = function _typeof(obj) { return _typeof2(obj); }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : _typeof2(obj); }; } return _typeof(obj); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } 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); } var ReactBpmn = /*#__PURE__*/ function (_React$Component) { _inherits(ReactBpmn, _React$Component); function ReactBpmn(props) { var _this; _classCallCheck(this, ReactBpmn); _this = _possibleConstructorReturn(this, _getPrototypeOf(ReactBpmn).call(this, props)); _this.state = {}; _this.containerRef = React.createRef(); return _this; } _createClass(ReactBpmn, [{ key: "componentDidMount", value: function componentDidMount() { var _this2 = this; var _this$props = this.props, url = _this$props.url, diagramXML = _this$props.diagramXML; var container = this.containerRef.current; this.bpmnViewer = new BpmnJS({ container: container }); this.bpmnViewer.on('import.done', function (event) { var error = event.error, warnings = event.warnings; if (error) { return _this2.handleError(error); } _this2.bpmnViewer.get('canvas').zoom('fit-viewport'); return _this2.handleShown(warnings); }); if (url) { return this.fetchDiagram(url); } if (diagramXML) { return this.displayDiagram(diagramXML); } } }, { key: "componentWillUnmount", value: function componentWillUnmount() { this.bpmnViewer.destroy(); } }, { key: "componentDidUpdate", value: function componentDidUpdate(prevProps, prevState) { var props = this.props, state = this.state; if (props.url !== prevProps.url) { return this.fetchDiagram(props.url); } var currentXML = props.diagramXML || state.diagramXML; var previousXML = prevProps.diagramXML || prevState.diagramXML; if (currentXML && currentXML !== previousXML) { return this.displayDiagram(currentXML); } } }, { key: "displayDiagram", value: function displayDiagram(diagramXML) { this.bpmnViewer.importXML(diagramXML); } }, { key: "fetchDiagram", value: function fetchDiagram(url) { var _this3 = this; this.handleLoading(); fetch(url).then(function (response) { return response.text(); }).then(function (text) { return _this3.setState({ diagramXML: text }); })["catch"](function (err) { return _this3.handleError(err); }); } }, { key: "handleLoading", value: function handleLoading() { var onLoading = this.props.onLoading; if (onLoading) { onLoading(); } } }, { key: "handleError", value: function handleError(err) { var onError = this.props.onError; if (onError) { onError(err); } } }, { key: "handleShown", value: function handleShown(warnings) { var onShown = this.props.onShown; if (onShown) { onShown(warnings); } } }, { key: "render", value: function render() { return React.createElement("div", { className: "react-bpmn-diagram-container", ref: this.containerRef }); } }]); return ReactBpmn; }(React.Component); return ReactBpmn; })));