"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

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

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));

var _tslib = require("tslib");

var _react = _interopRequireWildcard(require("react"));

var _classnames = _interopRequireDefault(require("classnames"));

var _mobx = require("mobx");

var _isPromise = _interopRequireDefault(require("is-promise"));

var _noop = _interopRequireDefault(require("lodash/noop"));

var _KeyCode = _interopRequireDefault(require("../../../lib/_util/KeyCode"));

var _dataset = require("choerodon-ui/dataset");

var _ConfigContext = _interopRequireDefault(require("../../../lib/config-provider/ConfigContext"));

var _Table = _interopRequireDefault(require("../table/Table"));

var _TableProfessionalBar = _interopRequireDefault(require("../table/query-bar/TableProfessionalBar"));

var _enum = require("../table/enum");

var _enum2 = require("../data-set/enum");

var _autobind = _interopRequireDefault(require("../_util/autobind"));

var _utils = require("../table/utils");

var _SelectionList = _interopRequireWildcard(require("./SelectionList"));

var _TriggerField = require("../trigger-field/TriggerField");

var _picture = _interopRequireDefault(require("../picture"));

var _numberField = _interopRequireDefault(require("../number-field"));

var LovView = /*#__PURE__*/function (_Component) {
  (0, _inherits2["default"])(LovView, _Component);

  var _super = (0, _createSuper2["default"])(LovView);

  function LovView() {
    var _this;

    (0, _classCallCheck2["default"])(this, LovView);
    _this = _super.apply(this, arguments);
    _this.resizedColumns = new Map();
    return _this;
  }

  (0, _createClass2["default"])(LovView, [{
    key: "componentWillMount",
    value: function componentWillMount() {
      var _this$props = this.props,
          dataSet = _this$props.dataSet,
          selection = _this$props.dataSet.selection,
          multiple = _this$props.multiple,
          viewMode = _this$props.viewMode;
      this.selection = selection;
      dataSet.selection = multiple ? _enum2.DataSetSelection.multiple : _enum2.DataSetSelection.single;

      if (viewMode === _TriggerField.TriggerViewMode.popup && multiple) {
        dataSet.addEventListener(_enum2.DataSetEvents.batchSelect, this.handleSelect);
        dataSet.addEventListener(_enum2.DataSetEvents.batchUnSelect, this.handleSelect);
      }
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      var _this$props2 = this.props,
          dataSet = _this$props2.dataSet,
          multiple = _this$props2.multiple,
          viewMode = _this$props2.viewMode;
      dataSet.selection = this.selection;

      if (viewMode === _TriggerField.TriggerViewMode.popup && multiple) {
        dataSet.removeEventListener(_enum2.DataSetEvents.batchSelect, this.handleSelect);
        dataSet.removeEventListener(_enum2.DataSetEvents.batchUnSelect, this.handleSelect);
      }
    }
  }, {
    key: "shouldComponentUpdate",
    value: function shouldComponentUpdate(nextProps) {
      var viewMode = this.props.viewMode;

      if (viewMode === _TriggerField.TriggerViewMode.popup && nextProps.popupHidden) {
        return false;
      }

      return true;
    }
    /* istanbul ignore next */

  }, {
    key: "getColumns",
    value: function getColumns() {
      var _this2 = this;

      var _this$props3 = this.props,
          lovItems = _this$props3.config.lovItems,
          tableProps = _this$props3.tableProps,
          viewMode = _this$props3.viewMode;
      return lovItems ? lovItems.filter(function (_ref) {
        var gridField = _ref.gridField;
        return gridField === 'Y';
      }).sort(function (_ref2, _ref3) {
        var seq1 = _ref2.gridFieldSequence;
        var seq2 = _ref3.gridFieldSequence;
        return seq1 - seq2;
      }).map(function (_ref4) {
        var display = _ref4.display,
            gridFieldName = _ref4.gridFieldName,
            gridFieldWidth = _ref4.gridFieldWidth,
            gridFieldAlign = _ref4.gridFieldAlign,
            gridFieldType = _ref4.gridFieldType;
        var column = {};

        if (tableProps && tableProps.columns) {
          column = tableProps.columns.find(function (c) {
            return c.name === gridFieldName;
          });
        } // 渲染 lov 中的 超链接 和 图片类型字段


        column = column || {};

        if (gridFieldType && gridFieldType.toLowerCase() === 'href') {
          column.renderer = function (_ref5) {
            var value = _ref5.value;
            return value ? /*#__PURE__*/_react["default"].createElement("a", {
              href: value,
              title: value,
              target: "_blank",
              rel: "noopener noreferrer"
            }, value) : undefined;
          };
        } else if (gridFieldType && gridFieldType.toLowerCase() === 'picture') {
          column.renderer = function (_ref6) {
            var value = _ref6.value;
            return value ? /*#__PURE__*/_react["default"].createElement(_picture["default"], {
              src: value,
              objectFit: "contain",
              height: "inherit",
              block: false
            }) : undefined;
          };
        } else if (gridFieldType && gridFieldType.toLowerCase() === 'percent') {
          column.renderer = function (_ref7) {
            var value = _ref7.value;
            return value ? "".concat(_dataset.math.multipliedBy(value, 100), "%") : value;
          };
        }

        return (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, column), {}, {
          key: gridFieldName,
          header: display,
          name: gridFieldName,
          width: viewMode === _TriggerField.TriggerViewMode.popup ? gridFieldName ? _this2.resizedColumns.get(gridFieldName) : undefined : gridFieldWidth,
          align: gridFieldAlign,
          editor: false
        });
      }) : undefined;
    }
  }, {
    key: "closeModal",
    value: function closeModal(record) {
      if (record) {
        var _this$props4 = this.props,
            onSelect = _this$props4.onSelect,
            modal = _this$props4.modal;

        if (modal) {
          modal.close();
        }

        onSelect(record);
      }
    }
  }, {
    key: "handleSelect",
    value: function handleSelect(event) {
      var _this3 = this;

      var selectionMode = this.selectionMode;
      var _this$props5 = this.props,
          _this$props5$onBefore = _this$props5.onBeforeSelect,
          onBeforeSelect = _this$props5$onBefore === void 0 ? _noop["default"] : _this$props5$onBefore,
          multiple = _this$props5.multiple,
          dataSet = _this$props5.dataSet,
          tableProps = _this$props5.tableProps;
      var records = selectionMode === _enum.SelectionMode.treebox ? dataSet.treeSelected : selectionMode === _enum.SelectionMode.rowbox || multiple ? dataSet.selected : dataSet.current ? [dataSet.current] : []; // 满足单选模式下，双击和勾选框选中均支持

      if (tableProps && tableProps.alwaysShowRowBox && !event) {
        records = dataSet.selected;
      }

      var record = multiple ? records : records[0];
      var beforeSelect = onBeforeSelect(record);

      if ((0, _isPromise["default"])(beforeSelect)) {
        return beforeSelect.then(function (result) {
          if (result !== false) {
            _this3.closeModal(record);
          }
        });
      }

      if (beforeSelect !== false) {
        this.closeModal(record);
      }

      return false;
    }
    /* istanbul ignore next */

  }, {
    key: "handleKeyDown",
    value: function handleKeyDown(e) {
      if (e.keyCode === _KeyCode["default"].ENTER) {
        this.handleSelect();
      }
    }
    /**
     * 单选 onRow 处理
     * @param props
     */

  }, {
    key: "handleRow",
    value: function handleRow(props) {
      var tableProps = this.props.tableProps;

      if (tableProps) {
        var onRow = tableProps.onRow;

        if (onRow) {
          return (0, _objectSpread2["default"])({
            onDoubleClick: this.handleSelect
          }, onRow(props));
        }
      }

      return {
        onDoubleClick: this.handleSelect
      };
    }
  }, {
    key: "handleSingleRow",
    value: function handleSingleRow(props) {
      var tableProps = this.props.tableProps;

      if (tableProps) {
        var onRow = tableProps.onRow;

        if (onRow) {
          return (0, _objectSpread2["default"])({
            onClick: this.handleSelect
          }, onRow(props));
        }
      }

      return {
        onClick: this.handleSelect
      };
    }
  }, {
    key: "handleColumnResize",
    value: function handleColumnResize(props) {
      var width = props.width,
          column = props.column;
      this.resizedColumns.set((0, _utils.getColumnKey)(column), width);
    }
  }, {
    key: "renderTable",
    value: function renderTable() {
      var _this$props6 = this.props,
          dataSet = _this$props6.dataSet,
          _this$props6$config = _this$props6.config,
          queryBar = _this$props6$config.queryBar,
          height = _this$props6$config.height,
          treeFlag = _this$props6$config.treeFlag,
          queryColumns = _this$props6$config.queryColumns,
          _this$props6$config$t = _this$props6$config.tableProps,
          configTableProps = _this$props6$config$t === void 0 ? {} : _this$props6$config$t,
          lovItems = _this$props6$config.lovItems,
          multiple = _this$props6.multiple,
          tableProps = _this$props6.tableProps,
          viewMode = _this$props6.viewMode,
          context = _this$props6.context,
          showSelectedInView = _this$props6.showSelectedInView;
      var getConfig = context.getConfig;
      var columns = this.getColumns();
      var popup = viewMode === _TriggerField.TriggerViewMode.popup;
      var modal = viewMode === _TriggerField.TriggerViewMode.modal;
      var drawer = viewMode === _TriggerField.TriggerViewMode.drawer;
      var percentItems = {};

      if (lovItems) {
        lovItems.filter(function (x) {
          return x.gridFieldType && x.gridFieldType.toLowerCase() === 'percent' && x.conditionField === 'Y';
        }).forEach(function (x) {
          percentItems[x.gridFieldName] = /*#__PURE__*/_react["default"].createElement(_numberField["default"], {
            suffix: /*#__PURE__*/_react["default"].createElement("span", null, "%")
          });
        });
      }

      var lovTableProps = (0, _objectSpread2["default"])((0, _objectSpread2["default"])((0, _objectSpread2["default"])({
        autoFocus: true,
        mode: treeFlag === 'Y' ? _enum.TableMode.tree : _enum.TableMode.list,
        onKeyDown: this.handleKeyDown,
        dataSet: dataSet,
        columns: columns,
        queryFieldsLimit: queryColumns,
        queryBar: queryBar || getConfig('lovQueryBar') || getConfig('queryBar'),
        selectionMode: _enum.SelectionMode.none
      }, configTableProps), tableProps), {}, {
        queryFields: (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, tableProps && tableProps.queryFields), percentItems),
        className: (0, _classnames["default"])(configTableProps && configTableProps.className, tableProps && tableProps.className),
        style: (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, configTableProps && configTableProps.style), {}, {
          height: height
        }, tableProps && tableProps.style),
        queryBarProps: (0, _objectSpread2["default"])((0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, tableProps && tableProps.queryBarProps), configTableProps && configTableProps.queryBarProps), getConfig('lovQueryBarProps'))
      });

      if (multiple) {
        if (popup || !tableProps || !tableProps.selectionMode) {
          if (lovTableProps.mode === _enum.TableMode.tree) {
            lovTableProps.selectionMode = _enum.SelectionMode.treebox;
          } else {
            lovTableProps.selectionMode = _enum.SelectionMode.rowbox;
          }
        }

        if (lovTableProps.selectionMode !== _enum.SelectionMode.rowbox && lovTableProps.selectionMode !== _enum.SelectionMode.treebox) {
          lovTableProps.highLightRow = false;
          lovTableProps.selectedHighLightRow = true;
        }
      } else if (popup) {
        lovTableProps.onRow = this.handleSingleRow;
      } else if (lovTableProps.selectionMode !== _enum.SelectionMode.rowbox) {
        lovTableProps.onRow = this.handleRow;
      } else {
        lovTableProps.highLightRow = false;
        lovTableProps.selectedHighLightRow = true;
      }

      if (popup) {
        if (lovTableProps.showSelectionCachedButton === undefined) {
          lovTableProps.showSelectionCachedButton = false;
          lovTableProps.showCachedSelection = true;
        }

        lovTableProps.autoFocus = false;
        lovTableProps.autoWidth = 'autoWidth' in lovTableProps ? lovTableProps.autoWidth : true;
        lovTableProps.onColumnResize = this.handleColumnResize;
      }

      var isProfessionalBar = lovTableProps.queryBar === _enum.TableQueryBarType.professionalBar;

      if (!popup && !lovTableProps.queryBar && isProfessionalBar) {
        lovTableProps.queryBar = function (props) {
          return /*#__PURE__*/_react["default"].createElement(_TableProfessionalBar["default"], (0, _extends2["default"])({}, props));
        };
      }

      if ((modal || drawer) && showSelectedInView) {
        lovTableProps.showSelectionTips = false;
      }

      this.selectionMode = lovTableProps.selectionMode;
      return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement(_Table["default"], (0, _extends2["default"])({}, lovTableProps)), modal && this.renderSelectionList());
    }
  }, {
    key: "renderSelectionList",
    value: function renderSelectionList() {
      var _this$props7 = this.props,
          dataSet = _this$props7.dataSet,
          _this$props7$valueFie = _this$props7.valueField,
          valueField = _this$props7$valueFie === void 0 ? '' : _this$props7$valueFie,
          _this$props7$textFiel = _this$props7.textField,
          textField = _this$props7$textFiel === void 0 ? '' : _this$props7$textFiel,
          _this$props7$config = _this$props7.config,
          treeFlag = _this$props7$config.treeFlag,
          configTableProps = _this$props7$config.tableProps,
          tableProps = _this$props7.tableProps,
          multiple = _this$props7.multiple,
          viewMode = _this$props7.viewMode,
          showSelectedInView = _this$props7.showSelectedInView,
          getSelectionProps = _this$props7.getSelectionProps,
          context = _this$props7.context;

      if (!showSelectedInView || !multiple) {
        return null;
      }

      if (!this.selectionMode) {
        var selectionMode = tableProps && tableProps.selectionMode || configTableProps && configTableProps.selectionMode;

        if (!selectionMode) {
          this.selectionMode = treeFlag === 'Y' ? _enum.SelectionMode.treebox : _enum.SelectionMode.rowbox;
        } else {
          this.selectionMode = selectionMode;
        }
      }

      var selectionsPosition = viewMode === _TriggerField.TriggerViewMode.drawer ? _SelectionList.SelectionsPosition.side : viewMode === _TriggerField.TriggerViewMode.modal ? _SelectionList.SelectionsPosition.below : undefined;
      return /*#__PURE__*/_react["default"].createElement(_SelectionList["default"], {
        dataSet: dataSet,
        treeFlag: treeFlag,
        valueField: valueField,
        textField: textField,
        selectionsPosition: selectionsPosition,
        selectionProps: getSelectionProps && getSelectionProps(),
        context: context
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props8 = this.props,
          modal = _this$props8.modal,
          viewRenderer = _this$props8.viewRenderer,
          dataSet = _this$props8.dataSet,
          viewMode = _this$props8.viewMode,
          lovConfig = _this$props8.config,
          textField = _this$props8.textField,
          valueField = _this$props8.valueField,
          multiple = _this$props8.multiple;

      if (modal) {
        modal.handleOk(this.handleSelect);
      }

      return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, viewMode === _TriggerField.TriggerViewMode.drawer && this.renderSelectionList(), /*#__PURE__*/_react["default"].createElement("div", null, viewRenderer ? (0, _mobx.toJS)(viewRenderer({
        dataSet: dataSet,
        lovConfig: lovConfig,
        textField: textField,
        valueField: valueField,
        multiple: multiple,
        modal: modal
      })) : this.renderTable()));
    }
  }], [{
    key: "contextType",
    get: function get() {
      return _ConfigContext["default"];
    }
  }]);
  return LovView;
}(_react.Component);

exports["default"] = LovView;
(0, _tslib.__decorate)([_mobx.action], LovView.prototype, "componentWillMount", null);
(0, _tslib.__decorate)([_mobx.action], LovView.prototype, "componentWillUnmount", null);
(0, _tslib.__decorate)([_autobind["default"]], LovView.prototype, "handleSelect", null);
(0, _tslib.__decorate)([_autobind["default"]], LovView.prototype, "handleKeyDown", null);
(0, _tslib.__decorate)([_autobind["default"]], LovView.prototype, "handleRow", null);
(0, _tslib.__decorate)([_autobind["default"]], LovView.prototype, "handleSingleRow", null);
(0, _tslib.__decorate)([_autobind["default"]], LovView.prototype, "handleColumnResize", null);
//# sourceMappingURL=LovView.js.map
