"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"), _typeof = require("@babel/runtime/helpers/typeof");

Object.defineProperty(exports, "__esModule", {
  value: !0
}), exports.default = void 0;

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties")), _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")), _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")), _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")), _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")), _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized")), _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits")), _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn")), _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf")), _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")), _react = _interopRequireWildcard(require("react")), _propTypes = _interopRequireDefault(require("prop-types")), _amosTool = require("amos-tool"), _amosFramework = require("amos-framework"), _amosJsplumb = require("amos-jsplumb"), _rayEventpool = require("ray-eventpool"), _amosReactRouter = require("amos-react-router"), _ruleDesignService = require("./../../../services/ruleDesignService"), _StageHeader = _interopRequireDefault(require("./header/StageHeader")), _RuleScenePlumb = _interopRequireDefault(require("./RuleScenePlumb")), _EleCatalogPanel = _interopRequireDefault(require("./extra/EleCatalogPanel")), _stageConf = require("./modelConf/stageConf"), _addNode = require("./addNode"), _excluded = [ "objId" ];

function _getRequireWildcardCache(e) {
  if ("function" != typeof WeakMap) return null;
  var t = new WeakMap, o = new WeakMap;
  return (_getRequireWildcardCache = function(e) {
    return e ? o : t;
  })(e);
}

function _interopRequireWildcard(e, t) {
  if (!t && e && e.__esModule) return e;
  if (null === e || "object" !== _typeof(e) && "function" != typeof e) return {
    default: e
  };
  var o = _getRequireWildcardCache(t);
  if (o && o.has(e)) return o.get(e);
  var n = {}, r = Object.defineProperty && Object.getOwnPropertyDescriptor;
  for (var a in e) if ("default" !== a && Object.prototype.hasOwnProperty.call(e, a)) {
    var i = r ? Object.getOwnPropertyDescriptor(e, a) : null;
    i && (i.get || i.set) ? Object.defineProperty(n, a, i) : n[a] = e[a];
  }
  return n.default = e, o && o.set(e, n), n;
}

function ownKeys(e, t) {
  var o = Object.keys(e);
  if (Object.getOwnPropertySymbols) {
    var n = Object.getOwnPropertySymbols(e);
    t && (n = n.filter((function(t) {
      return Object.getOwnPropertyDescriptor(e, t).enumerable;
    }))), o.push.apply(o, n);
  }
  return o;
}

function _objectSpread(e) {
  for (var t = 1; t < arguments.length; t++) {
    var o = null != arguments[t] ? arguments[t] : {};
    t % 2 ? ownKeys(Object(o), !0).forEach((function(t) {
      (0, _defineProperty2.default)(e, t, o[t]);
    })) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(o)) : ownKeys(Object(o)).forEach((function(t) {
      Object.defineProperty(e, t, Object.getOwnPropertyDescriptor(o, t));
    }));
  }
  return e;
}

function _createSuper(e) {
  var t = _isNativeReflectConstruct();
  return function() {
    var o, n = (0, _getPrototypeOf2.default)(e);
    if (t) {
      var r = (0, _getPrototypeOf2.default)(this).constructor;
      o = Reflect.construct(n, arguments, r);
    } else o = n.apply(this, arguments);
    return (0, _possibleConstructorReturn2.default)(this, o);
  };
}

function _isNativeReflectConstruct() {
  if ("undefined" == typeof Reflect || !Reflect.construct) return !1;
  if (Reflect.construct.sham) return !1;
  if ("function" == typeof Proxy) return !0;
  try {
    return Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], (function() {}))), 
    !0;
  } catch (e) {
    return !1;
  }
}

var extraSize = {
  top: 90,
  left: 230
}, RuleStage = function(e) {
  (0, _inherits2.default)(o, e);
  var t = _createSuper(o);
  function o(e) {
    var n;
    return (0, _classCallCheck2.default)(this, o), n = t.call(this, e), (0, _defineProperty2.default)((0, 
    _assertThisInitialized2.default)(n), "onAddConnection", (function(e, t, o) {
      var r = {
        id: e,
        sourceId: t,
        targetId: o
      };
      n.setState({
        connections: [].concat((0, _toConsumableArray2.default)(n.state.connections), [ r ])
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(n), "onRemoveConnection", (function(e, t) {
      n.setState({
        connections: n.state.connections.filter((function(t) {
          return t.id !== e;
        }))
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(n), "onZoom", (function(e) {
      n.setState({
        zoom: e
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(n), "changeZoom", (function(e) {
      var t = n.props.zoom;
      e ? t += .2 : t -= .2, n.props.onZoom(t);
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(n), "onRemoveNode", (function(e) {
      var t = n.state.nodes.filter((function(t) {
        return t.id !== e;
      }));
      n.setState({
        nodes: t
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(n), "onChange", (function(e) {
      var t = e.nodes, o = e.connections;
      n.setState({
        nodes: t,
        connections: o
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(n), "onSceneCreated", (function(e) {
      n.plumbInstance = e;
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(n), "onSceneClick", (function(e) {
      n.setState({
        catalogVisible: !1,
        catalogOptions: {
          eleType: "",
          sourceId: "",
          position: {
            left: 0,
            top: 0
          },
          catalogs: []
        }
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(n), "onSubGraphClosed", (function() {
      n.setState({
        subGraphVisible: !1,
        subGraphOptions: {}
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(n), "onConnectionDbClick", (function(e, t) {
      console.log(e, t, e.getData());
      var o = e.source, r = e.target;
      (0, _stageConf.isStatusDragLink)(r) && n.props.onStatusConnLink(o, r, e.getData());
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(n), "initData", (function() {
      var e = n.props.objId;
      e && (0, _ruleDesignService.initStageView)(e).then((function(e) {
        n.setState({
          connections: e.linkVoList,
          nodes: e.nodeVoList,
          ruleSubObj: e.rule
        });
      }));
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(n), "reloadPlumb", (function() {
      var e = n.props.objId;
      e && (0, _ruleDesignService.initStageView)(e).then((function(e) {
        n.setState({
          needDestroy: !1,
          connections: e.linkVoList,
          nodes: e.nodeVoList
        });
      }));
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(n), "filterNodes", (function(e) {
      var t = n.state.nodes, o = e.filter((function(e) {
        return !t.find((function(t) {
          return t.id === e.id;
        }));
      }));
      return [].concat((0, _toConsumableArray2.default)(t), (0, _toConsumableArray2.default)(o));
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(n), "filterConns", (function(e) {
      var t = n.state.connections, o = e.filter((function(e) {
        return !t.find((function(t) {
          return t.sourceId === e.sourceId && t.targetId === e.targetId;
        }));
      }));
      return [].concat((0, _toConsumableArray2.default)(t), (0, _toConsumableArray2.default)(o));
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(n), "saveStage", (function(e) {
      var t = n.state, o = t.nodes, r = t.connections;
      _amosJsplumb.graph.completePosition(n.plumbInstance, o), (0, _ruleDesignService.saveStageView)({
        id: e,
        nodeVoList: o,
        linkVoList: r
      }).then((function(e) {
        _amosFramework.message.success("保存成功！");
      }), (function(e) {
        _amosFramework.message.danger(e || "保存失败!");
      }));
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(n), "exitStage", (function() {
      _amosReactRouter.browserHistory.goBack();
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(n), "addNode", (function(e) {
      var t = e.labelValue, o = n.state, r = o.nodes, a = o.connections, i = o.position, s = o.statusId, u = (0, 
      _addNode.addTrigerAndAction)({
        labelValue: t,
        position: i,
        statusId: s
      });
      (0, _ruleDesignService.addViewTrigger)(u), n.setState({
        nodes: [].concat((0, _toConsumableArray2.default)(r), (0, _toConsumableArray2.default)(u.node)),
        connections: [].concat((0, _toConsumableArray2.default)(a), (0, _toConsumableArray2.default)(u.conn)),
        visible: !1,
        eleType: "",
        statusId: ""
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(n), "cancel", (function(e) {
      n.setState({
        visible: !1
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(n), "handleDrop", (function(e) {
      (0, _amosJsplumb.stopEvent)(e);
      var t = _amosJsplumb.domTools.getNodeOffset(e), o = t.left, r = t.top, a = e.dataTransfer.getData("FlowChartNode"), i = a.split("-")[1], s = a.split("-")[2], u = (0, 
      _stageConf.transferDropData)(a.split("-")[0]), l = u.nodeType, d = u.sourceId;
      if ((0, _stageConf.isDragEle)(l)) d ? n.doDropAddNodeAndConn({
        nodeType: l,
        x: o,
        y: r,
        sourceId: d
      }) : n.doDropAddNode({
        nodeType: l,
        x: o,
        y: r,
        ruleNodeId: i,
        label: s
      }); else {
        var c = e.target;
        if ("plumb-charts" !== c.className) {
          var p = (0, _addNode.getEleNode)(c);
          p && n.doDropAddConn(d, p.id);
        }
      }
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(n), "doDropAddNode", (function(e) {
      var t = e.nodeType, o = e.x, r = e.y, a = e.ruleNodeId, i = e.label, s = n.state.nodes, u = n.props.objId, l = i || "节点", d = {
        left: o,
        top: r
      }, c = (0, _addNode.commonAddNode)({
        eleType: t,
        labelValue: l,
        position: d,
        ruleNodeId: a
      });
      (0, _extends2.default)(c, {
        ruleId: u
      }), (0, _ruleDesignService.addViewNode)(c), n.setState({
        catalogVisible: !1,
        nodes: [].concat((0, _toConsumableArray2.default)(s), [ c ])
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(n), "doDropAddNodeAndConn", (function(e) {
      var t = e.nodeType, o = e.x, r = e.y, a = e.sourceId, i = n.state, s = i.nodes, u = i.connections, l = n.props.objId, d = _stageConf.locale[t] || "节点", c = {
        left: o,
        top: r
      }, p = (0, _addNode.commonAddNodeAndConn)({
        eleType: t,
        labelValue: d,
        position: c,
        sourceId: a
      });
      (0, _ruleDesignService.addViewNode)(_objectSpread(_objectSpread({}, p.node), {}, {
        subObjId: l,
        sourceId: a
      })), n.setState({
        catalogVisible: !1,
        nodes: [].concat((0, _toConsumableArray2.default)(s), [ p.node ]),
        connections: [].concat((0, _toConsumableArray2.default)(u), [ p.conn ])
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(n), "doDropAddConn", (function(e, t) {
      var o = n.props.objId;
      (0, _ruleDesignService.addViewLink)({
        oid: o,
        sourceId: e,
        targetId: t
      }).then((function(e) {
        n.setState({
          needDestroy: !0,
          catalogVisible: !1
        }, (function() {
          return n.reloadPlumb();
        }));
      }), (function(e) {
        _amosFramework.message.danger(e || "添加链路失败！");
      }));
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(n), "doPreAddEle", (function(e) {
      var t = e.type, o = e.x, r = e.y;
      _amosTool.utils.isEmpty(t) || n.setState({
        visible: !0,
        eleType: t,
        position: {
          left: o,
          top: r
        }
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(n), "beforeDrop", (function(e) {
      (e.targetId = e.dropEndpoint.elementId, e.targetId !== e.sourceId) && n.jspInstance.getConnections({
        source: e.sourceId,
        target: e.targetId
      }).length;
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(n), "updateGraph", (function() {
      var e = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {};
      n.setState(_objectSpread({}, e));
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(n), "updateGraphWithDestroy", (function() {
      var e = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {};
      n.setState({
        destroy: !0
      }, (function() {
        n.setState(_objectSpread({}, e));
      }));
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(n), "onDropHandler", (function(e, t, o) {
      var r = n.state.nodes;
      r.map((function(n) {
        if (n.id === e) {
          var r = {
            left: t,
            top: o
          };
          n.config.style = r;
        }
      })), n.setState({
        nodes: r
      });
    })), n.state = {
      connections: [],
      nodes: [],
      zoom: 1,
      visible: !1,
      catalogVisible: !1,
      eleType: "",
      statusId: "",
      position: {
        left: 0,
        top: 0
      },
      ruleSubObj: {},
      needDestroy: !1,
      catalogOptions: {},
      subGraphVisible: !1,
      subGraphOptions: {}
    }, n;
  }
  return (0, _createClass2.default)(o, [ {
    key: "componentWillMount",
    value: function() {}
  }, {
    key: "componentDidMount",
    value: function() {
      var e = this;
      this.initData(), this.pubsub_eleadd = _rayEventpool.PubSub.subscribe(_stageConf.ELE_ADD_TOPIC.eleAdd, (function(t, o) {
        var n = o.eleType, r = o.statusId, a = o.position;
        _amosTool.utils.isEmpty(n) || e.setState({
          visible: !0,
          eleType: n,
          statusId: r,
          position: a
        });
      })), this.pubsub_eledel = _rayEventpool.PubSub.subscribe(_stageConf.ELE_DEL_TOPIC.eleDel, (function(t, o) {
        var n = o.eleId;
        _amosTool.utils.isEmpty(n) || (0, _ruleDesignService.delViewNode)(n).then((function(t) {
          _amosFramework.message.success("删除成功！"), e.setState({
            needDestroy: !0
          }, (function() {
            return e.reloadPlumb();
          }));
        }), (function(e) {
          _amosFramework.message.danger(e || "删除失败");
        }));
      })), this.pubsub_catalog = _rayEventpool.PubSub.subscribe(_stageConf.ELE_CATALOG_TOPIC.NORMAL, (function(t, o) {
        var n = o.catalogOptions, r = o.catalogVisible;
        _amosTool.utils.isEmpty(n) || e.setState({
          catalogVisible: r,
          catalogOptions: n
        });
      })), this.pubsub_subgraph = _rayEventpool.PubSub.subscribe(_stageConf.ELE_ENTRY_TOPIC.eleEntry, (function(t, o) {
        var n = o.subGraphOptions, r = o.subGraphVisible;
        _amosTool.utils.isEmpty(n) || e.setState({
          subGraphOptions: n,
          subGraphVisible: r
        });
      }));
    }
  }, {
    key: "componentWillReceiveProps",
    value: function(e) {
      var t = this;
      e.needReloadStage && (this.props.toggleReloadStage(!1), this.setState({
        needDestroy: !0
      }, (function() {
        return t.reloadPlumb();
      })));
    }
  }, {
    key: "shouldComponentUpdate",
    value: function(e, t) {
      return _amosFramework.PureRenderMixin.shouldComponentUpdate.apply(this, e, t);
    }
  }, {
    key: "componentWillUnmount",
    value: function() {
      _rayEventpool.PubSub.unsubscribe(this.pubsub_eleadd), _rayEventpool.PubSub.unsubscribe(this.pubsub_eledel), 
      _rayEventpool.PubSub.unsubscribe(this.pubsub_catalog), _rayEventpool.PubSub.unsubscribe(this.pubsub_subgraph);
    }
  }, {
    key: "render",
    value: function() {
      var e = this.state, t = e.zoom, o = e.nodes, n = e.connections, r = e.catalogOptions, a = e.catalogVisible, i = e.ruleSubObj, s = e.needDestroy, u = {
        nodes: o,
        connections: n
      }, l = {
        onDrop: this.onDropHandler
      }, d = this.props, c = d.objId, p = (0, _objectWithoutProperties2.default)(d, _excluded), f = _amosJsplumb.tools.omit(p, [ "updateGraph", "updateGraphWithDestroy" ]), _ = {
        dblclick: this.onConnectionDbClick,
        connection: _amosJsplumb.handlers.onConnectionEventHandler,
        beforeDrop: this.beforeDrop
      };
      return _react.default.createElement("div", {
        className: "rule-stage"
      }, _react.default.createElement(_StageHeader.default, {
        saveStage: this.saveStage,
        exitStage: this.exitStage,
        ruleSubObj: i,
        objId: c
      }), _react.default.createElement(_RuleScenePlumb.default, {
        handleDrop: this.handleDrop,
        onAddConnection: this.onAddConnection,
        onRemoveConnection: this.onRemoveConnection,
        onRemoveNode: this.onRemoveNode,
        dataSource: u,
        newRestProps: f,
        onPlumbChange: this.onChange,
        nodeProps: l,
        onSceneCreated: this.onSceneCreated,
        onPlumbClick: this.onSceneClick,
        eventListeners: _,
        zoom: t,
        onZoom: this.onZoom,
        destroy: s
      }), _react.default.createElement(_EleCatalogPanel.default, (0, _extends2.default)({
        visible: a
      }, r)));
    }
  } ]), o;
}(_react.Component);

(0, _defineProperty2.default)(RuleStage, "propTypes", {
  objId: _propTypes.default.oneOfType([ _propTypes.default.string, _propTypes.default.number ]),
  needReloadStage: _propTypes.default.bool,
  toggleReloadStage: _propTypes.default.func,
  onStatusConnLink: _propTypes.default.func
}), RuleStage.displayName = "RuleStage";

var _default = RuleStage;

exports.default = _default;