"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 _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")), _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")), _amosAntd = require("amos-antd"), _amosIcon = _interopRequireDefault(require("amos-icon")), _dt2react = require("dt2react"), _amosFramework = require("amos-framework"), _processor = require("../../../../utils/processor"), _urlConsts = _interopRequireDefault(require("../../../../consts/urlConsts")), _TreeHeader = _interopRequireDefault(require("./../header/TreeHeader")), _AddView = _interopRequireDefault(require("./AddView")), _ContextMenu = _interopRequireDefault(require("./ContextMenu")), _userConfig = require("./../../../../utils/userConfig");

function _getRequireWildcardCache(e) {
  if ("function" != typeof WeakMap) return null;
  var t = new WeakMap, r = new WeakMap;
  return (_getRequireWildcardCache = function(e) {
    return e ? r : 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 r = _getRequireWildcardCache(t);
  if (r && r.has(e)) return r.get(e);
  var a = {}, n = Object.defineProperty && Object.getOwnPropertyDescriptor;
  for (var i in e) if ("default" !== i && Object.prototype.hasOwnProperty.call(e, i)) {
    var o = n ? Object.getOwnPropertyDescriptor(e, i) : null;
    o && (o.get || o.set) ? Object.defineProperty(a, i, o) : a[i] = e[i];
  }
  return a.default = e, r && r.set(e, a), a;
}

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

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 TreeNode = _amosAntd.Tree.TreeNode, loop = function e(t, r, a) {
  t.forEach((function(t, n, i) {
    return t.key === r ? a(t, n, i) : t.children ? e(t.children, r, a) : void 0;
  }));
}, rootNode = {
  title: "根目录",
  key: "0",
  parentKey: "-1",
  children: [],
  sourceId: 0
}, TopoTree = function(e) {
  (0, _inherits2.default)(r, e);
  var t = _createSuper(r);
  function r(e) {
    var a;
    return (0, _classCallCheck2.default)(this, r), a = t.call(this, e), (0, _defineProperty2.default)((0, 
    _assertThisInitialized2.default)(a), "onExpand", (function(e) {
      a.setState({
        expandedKeys: e,
        autoExpandParent: !1
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "onCheck", (function(e) {
      console.log("onCheck", e), a.setState({
        checkedKeys: e
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "onSelect", (function(e, t) {
      var r = null === t.node.props.children ? t.node.props.parentKey : t.node.props.dataRef.parentKey, n = null === t.node.props.children ? t.node.props.title : t.node.props.dataRef.title, i = null === t.node.props.children ? t.node.props.sourceId : t.node.props.dataRef.sourceId, o = {
        parentKey: r,
        title: n,
        key: t.node.props.eventKey
      };
      void 0 !== i && (0, _extends2.default)(o, {
        id: i
      }), a.setState({
        selectedKeys: e,
        currentNode: o
      }), (0, a.props.setCurrentSubviewId)(i, n, o, e, a.editTreeData);
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "onArtificialSelect", (function(e, t) {
      var r = t.parentKey, n = t.title, i = t.sourceId, o = {
        parentKey: r,
        title: n,
        key: t.key
      };
      void 0 !== i && (0, _extends2.default)(o, {
        id: i
      }), a.setState({
        selectedKeys: e,
        currentNode: o
      }), (0, a.props.setCurrentSubviewId)(i, n, o, e, a.editTreeData);
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "getOffset", (function() {
      var e = (0, _dt2react.getOffset)(a.treeRoot) || {};
      a.setState({
        treeLocation: e
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "getTreeData", (function() {
      (0, _userConfig.currentUser)().userId;
      a.setState({
        currentTreeData: [ {
          sourceId: 0,
          key: "0",
          parentKey: "-1",
          title: "昆山宁华",
          children: [ {
            sourceId: 1,
            key: "0-0",
            parentKey: "0",
            title: "昆山A区",
            value: "昆山A区"
          }, {
            sourceId: 2,
            key: "0-1",
            parentKey: "0",
            title: "昆山B区",
            children: [ {
              sourceId: 3,
              key: "0-1-0",
              parentKey: "0-1",
              title: "一楼",
              value: "一楼"
            }, {
              sourceId: 4,
              key: "0-1-1",
              parentKey: "0-1",
              title: "二楼",
              value: "二楼"
            } ],
            value: "昆山B区"
          } ],
          value: "昆山宁华"
        }, {
          sourceId: 11,
          key: "1",
          parentKey: "-1",
          title: "信达可恩",
          children: [],
          value: "信达可恩"
        }, {
          sourceId: 11,
          key: "2",
          parentKey: "-1",
          title: "中安消防",
          children: [],
          value: "中安消防"
        }, {
          sourceId: 11,
          key: "3",
          parentKey: "-1",
          title: "鼎信消防",
          children: [],
          value: "鼎信消防"
        }, {
          sourceId: 11,
          key: "4",
          parentKey: "-1",
          title: "坚锐消防",
          children: [],
          value: "坚锐消防"
        } ]
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "nodeRightClick", (function(e) {
      a.contextMenuRef.showMenu(e.event, e.node.props.eventKey);
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "toggleView", (function() {
      var e = arguments.length > 0 && void 0 !== arguments[0] && arguments[0];
      a.setState({
        viewVisable: e
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "addView", (function() {
      1 === a.state.selectedKeys.length ? a.setState({
        viewVisable: !0
      }) : _amosFramework.message.danger("新选择父节点！");
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "delView", (function(e) {
      var t = a.state, r = t.currentNode, n = t.selectedKeys, i = r.parentKey;
      if ("-1" !== i) {
        var o = a.delTreeData;
        1 === n.length ? _amosAntd.Modal.confirm({
          title: "批量删除",
          content: "是否删除所选信息？",
          okText: "确定",
          cancelText: "取消",
          onOk: function() {
            o(i, r.key);
          },
          onCancel: function() {}
        }) : _amosFramework.message.danger("新选择要删除的节点节点！");
      } else _amosAntd.Modal.info({
        title: "系统提示",
        content: "不能删除根节点",
        okText: "确定"
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "addTreeData", (function(e) {
      var t = a.state, r = t.currentTreeData, n = t.expandedKeys, i = t.currentNode.key;
      loop(r, i, (function(t) {
        t.children = t.children || [];
        var r = a.generateKey(t.children, i), o = {
          createTime: (new Date).getTime(),
          name: e,
          currentKey: r,
          parentKey: i,
          userId: (0, _userConfig.currentUser)().userId
        };
        a.saveSubgraphData(o), -1 === n.indexOf(r) && (n.push(i), a.setState({
          expandedKeys: n
        }));
      }));
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "editTreeData", (function(e) {
      var t = a.state, r = t.currentTreeData, n = t.currentNode;
      if ("-1" !== n.parentKey) {
        var i = n.key;
        loop(r, i, (function(t) {
          t.title = e;
          var r = (new Date).getTime(), i = {
            id: n.id,
            createTime: r,
            name: e,
            currentKey: t.key,
            parentKey: t.parentKey,
            userId: (0, _userConfig.currentUser)().userId
          };
          a.saveSubgraphData(i);
        }));
      } else _amosAntd.Modal.info({
        title: "系统提示",
        content: "不能编辑根节点",
        okText: "确定"
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "saveSubgraphData", (function(e) {
      _processor.AmosFetch.post(_urlConsts.default.addSubgraph, {
        data: e
      }).then((function(e) {
        if (e.ok) return e.text();
        throw new Error("从服务端获取数据失败！");
      })).then((function(t) {
        var r = a.props.setCurrentName, n = JSON.parse(t);
        "SUCCESS" === n.result ? (a.getTreeData(), r(e.name), _amosFramework.message.success("保存成功")) : _amosFramework.message.success("保存失败：".concat(n.message));
      }));
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "delTreeData", (function(e, t) {
      "".concat(_urlConsts.default.delSubgraph, "/").concat(t);
      a.updateTreeData(e);
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "updateTreeData", (function(e) {
      (0, _userConfig.currentUser)().userId;
      a.setState({
        currentTreeData: [ {
          sourceId: 0,
          key: "0",
          parentKey: "-1",
          title: "昆山宁华",
          children: [ {
            sourceId: 1,
            key: "0-0",
            parentKey: "0",
            title: "昆山A区",
            value: "昆山A区"
          }, {
            sourceId: 2,
            key: "0-1",
            parentKey: "0",
            title: "昆山B区",
            children: [ {
              sourceId: 3,
              key: "0-1-0",
              parentKey: "0-1",
              title: "一楼",
              value: "一楼"
            }, {
              sourceId: 4,
              key: "0-1-1",
              parentKey: "0-1",
              title: "二楼",
              value: "二楼"
            } ],
            value: "昆山B区"
          } ],
          value: "昆山宁华"
        }, {
          sourceId: 11,
          key: "1",
          parentKey: "-1",
          title: "信达可恩",
          children: [],
          value: "信达可恩"
        }, {
          sourceId: 11,
          key: "2",
          parentKey: "-1",
          title: "中安消防",
          children: [],
          value: "中安消防"
        }, {
          sourceId: 11,
          key: "3",
          parentKey: "-1",
          title: "鼎信消防",
          children: [],
          value: "鼎信消防"
        }, {
          sourceId: 11,
          key: "4",
          parentKey: "-1",
          title: "坚锐消防",
          children: [],
          value: "坚锐消防"
        } ]
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "generateKey", (function(e, t) {
      if (void 0 === e || 0 === e.length) return "".concat(t, "-0");
      var r = [];
      e.map((function(e) {
        var t = e.key.split("-");
        r.push(parseInt(t[t.length - 1]));
      }));
      var a = Math.max.apply(null, r);
      return "".concat(t, "-").concat(a + 1);
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "renderTreeNodes", (function(e) {
      return e.map((function(e) {
        return e.children ? _react.default.createElement(TreeNode, {
          title: e.title,
          key: e.key,
          dataRef: e
        }, a.renderTreeNodes(e.children)) : _react.default.createElement(TreeNode, e);
      }));
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "renderTreeHeaderExtra", (function() {
      return [ _react.default.createElement("div", {
        key: "create-view",
        className: "tree-header-extra-item",
        onClick: a.addView,
        title: "创建视图"
      }, _react.default.createElement(_amosIcon.default, {
        icon: "luru"
      })), _react.default.createElement("div", {
        key: "del-view",
        className: "tree-header-extra-item",
        onClick: a.delView,
        title: "删除视图"
      }, _react.default.createElement(_amosIcon.default, {
        icon: "delete"
      })) ];
    })), a.state = {
      expandedKeys: [],
      autoExpandParent: !0,
      checkedKeys: [],
      selectedKeys: [],
      treeLocation: {},
      currentTreeData: [],
      currentNode: {},
      viewVisable: !1
    }, a;
  }
  return (0, _createClass2.default)(r, [ {
    key: "componentDidMount",
    value: function() {
      this.getOffset(), this.getTreeData();
    }
  }, {
    key: "render",
    value: function() {
      var e = this, t = this.state, r = t.treeLocation, a = t.viewVisable, n = t.selectedKeys, i = t.currentTreeData, o = t.expandedKeys, u = t.currentNode;
      return _react.default.createElement("div", {
        className: "topo-tree",
        ref: function(t) {
          return e.treeRoot = t;
        }
      }, _react.default.createElement(_TreeHeader.default, {
        title: "物理拓扑图",
        extra: this.renderTreeHeaderExtra()
      }), _react.default.createElement("div", {
        className: "topo-tree-content"
      }, _react.default.createElement(_amosAntd.Tree, {
        showLine: !0,
        onExpand: this.onExpand,
        expandedKeys: o,
        autoExpandParent: this.state.autoExpandParent,
        onCheck: this.onCheck,
        checkedKeys: this.state.checkedKeys,
        onSelect: this.onSelect,
        selectedKeys: this.state.selectedKeys,
        onRightClick: this.nodeRightClick
      }, this.renderTreeNodes(i)), _react.default.createElement(_ContextMenu.default, {
        treeLocation: r,
        selectedKeys: n,
        addTreeData: this.addTreeData,
        onDelClick: this.delView,
        currentNode: u,
        editTreeData: this.editTreeData,
        ref: function(t) {
          return e.contextMenuRef = t;
        }
      })), _react.default.createElement(_AddView.default, {
        visible: a,
        selectedKeys: n,
        addTreeData: this.addTreeData,
        toggleView: this.toggleView
      }));
    }
  } ]), r;
}(_react.Component);

TopoTree.displayName = "TopoTree", TopoTree.propTypes = {
  setCurrentSubviewId: _propTypes.default.func,
  setCurrentName: _propTypes.default.func
};

var _default = TopoTree;

exports.default = _default;