"use strict";

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

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

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

var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

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

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

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 _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

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

var _tslib = require("tslib");

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

var _mobxReact = require("mobx-react");

var _mobx = require("mobx");

var _isObject = _interopRequireDefault(require("lodash/isObject"));

var _isString = _interopRequireDefault(require("lodash/isString"));

var _isNumber = _interopRequireDefault(require("lodash/isNumber"));

var _UnitConvertor = require("../../../../lib/_util/UnitConvertor");

var _icon = _interopRequireDefault(require("../../../../lib/icon"));

var _enum = require("../../../../lib/progress/enum");

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

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

var _enum2 = require("../enum");

var _TableButtons = _interopRequireDefault(require("./TableButtons"));

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

var _Button = _interopRequireDefault(require("../../button/Button"));

var _radio = _interopRequireDefault(require("../../radio"));

var _enum3 = require("../../button/enum");

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

var _localeContext = require("../../locale-context");

var _TableContext = _interopRequireDefault(require("../TableContext"));

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

var _dataSet = _interopRequireDefault(require("../../data-set"));

var _modal = _interopRequireDefault(require("../../modal"));

var _progress = _interopRequireDefault(require("../../progress"));

var _Column = _interopRequireDefault(require("../Column"));

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

var _CombineSort = _interopRequireDefault(require("./CombineSort"));

var _TableToolBar = _interopRequireDefault(require("./TableToolBar"));

var _TableFilterBar = _interopRequireDefault(require("./TableFilterBar"));

var _TableAdvancedQueryBar = _interopRequireDefault(require("./TableAdvancedQueryBar"));

var _TableProfessionalBar = _interopRequireDefault(require("./TableProfessionalBar"));

var _TableComboBar = _interopRequireDefault(require("./TableComboBar"));

var _TableDynamicFilterBar = _interopRequireDefault(require("./TableDynamicFilterBar"));

var _utils2 = require("../../data-set/utils");

var _Dropdown = _interopRequireDefault(require("../../dropdown/Dropdown"));

var _menu = _interopRequireDefault(require("../../menu"));

var _textField = _interopRequireDefault(require("../../text-field"));

var _excluded = ["afterClick"],
    _excluded2 = ["afterClick"];
var ExportBody = (0, _mobxReact.observer)(function (props) {
  var dataSet = props.dataSet,
      prefixCls = props.prefixCls;
  var exportMessage = (0, _localeContext.$l)('Table', 'export_ing');
  var exportProgress = {
    percent: 1,
    status: _enum.ProgressStatus.active
  };

  switch (dataSet.exportStatus) {
    case _enum4.DataSetExportStatus.start:
      exportProgress = {
        percent: 1,
        status: _enum.ProgressStatus.active
      };
      break;

    case _enum4.DataSetExportStatus.exporting:
      exportProgress = {
        percent: dataSet.exportProgress,
        status: _enum.ProgressStatus.active
      };
      break;

    case _enum4.DataSetExportStatus.progressing:
      exportProgress = {
        percent: dataSet.exportProgress,
        status: _enum.ProgressStatus.active
      };
      break;

    case _enum4.DataSetExportStatus.failed:
      exportMessage = (0, _localeContext.$l)('Table', 'export_failed');
      exportProgress = {
        percent: 50,
        status: _enum.ProgressStatus.exception
      };
      break;

    case _enum4.DataSetExportStatus.success:
      exportMessage = (0, _localeContext.$l)('Table', 'export_success');
      exportProgress = {
        percent: 100,
        status: _enum.ProgressStatus.success
      };
      break;

    default:
      break;
  }

  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefixCls, "-export-progress-body")
  }, /*#__PURE__*/_react["default"].createElement("span", null, exportMessage), /*#__PURE__*/_react["default"].createElement(_progress["default"], (0, _extends2["default"])({}, exportProgress)));
});
var ExportFooter = (0, _mobxReact.observer)(function (props) {
  var dataSet = props.dataSet,
      prefixCls = props.prefixCls,
      exportButton = props.exportButton;

  var _useState = (0, _react.useState)(dataSet.name || (0, _localeContext.$l)('Table', 'default_export')),
      _useState2 = (0, _slicedToArray2["default"])(_useState, 2),
      username = _useState2[0],
      setUsername = _useState2[1];

  var handleClick = function handleClick() {
    exportButton(dataSet.exportStatus, username);
  };

  var _useState3 = (0, _react.useState)(undefined),
      _useState4 = (0, _slicedToArray2["default"])(_useState3, 2),
      messageTimeout = _useState4[0],
      setMessageTimeout = _useState4[1];

  _react["default"].useEffect(function () {
    var currentTimeout = null;
    currentTimeout = setTimeout(function () {
      if (dataSet && dataSet.exportStatus !== _enum4.DataSetExportStatus.success && dataSet.exportStatus !== _enum4.DataSetExportStatus.failed) {
        setMessageTimeout((0, _localeContext.$l)('Table', 'export_waiting'));
      }
    }, 5000);
    return function () {
      if (currentTimeout) {
        clearTimeout(currentTimeout);
      }
    };
  }, []);

  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefixCls, "-export-progress-footer")
  }, dataSet.exportStatus === _enum4.DataSetExportStatus.failed && /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement("span", null, (0, _localeContext.$l)('Table', 'export_break')), /*#__PURE__*/_react["default"].createElement(_Button["default"], {
    onClick: handleClick
  }, (0, _localeContext.$l)('Table', 'retry_button'))), dataSet.exportStatus === _enum4.DataSetExportStatus.success && /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement("span", null, "".concat((0, _localeContext.$l)('Table', 'file_name'), ":")), /*#__PURE__*/_react["default"].createElement(_textField["default"], {
    value: username,
    onChange: function onChange(value) {
      setUsername(value);
    }
  })), /*#__PURE__*/_react["default"].createElement(_Button["default"], {
    color: _enum3.ButtonColor.primary,
    onClick: handleClick
  }, (0, _localeContext.$l)('Table', 'download_button'))), dataSet.exportStatus !== _enum4.DataSetExportStatus.success && dataSet.exportStatus !== _enum4.DataSetExportStatus.failed && /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement("span", null, messageTimeout || (0, _localeContext.$l)('Table', 'export_operating')), /*#__PURE__*/_react["default"].createElement(_Button["default"], {
    color: _enum3.ButtonColor.gray,
    onClick: handleClick
  }, (0, _localeContext.$l)('Table', 'cancel_button'))));
});

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

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

  function TableQueryBar() {
    var _this;

    (0, _classCallCheck2["default"])(this, TableQueryBar);
    _this = _super.apply(this, arguments);
    _this.queryFieldsElement = [];
    /**
     * 点击汇总条展开收起
     * @param summary
     */

    _this.openMore = function (summary) {
      if (_this.moreSummary && _this.moreSummary.length) {
        _this.moreSummary = [];
      } else {
        _this.moreSummary = _this.renderSummary(summary);
      }

      return _this.moreSummary;
    };

    return _this;
  }

  (0, _createClass2["default"])(TableQueryBar, [{
    key: "showQueryBar",
    get: function get() {
      var showQueryBar = this.props.showQueryBar,
          queryBar = this.context.tableStore.queryBar;
      return showQueryBar !== false && queryBar !== _enum2.TableQueryBarType.none;
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      if (this.exportModal) {
        this.exportModal.close(true);
      }
    }
  }, {
    key: "handleButtonCreate",
    value: function handleButtonCreate() {
      var dataSet = this.context.dataSet;
      dataSet.create({}, 0);
    }
  }, {
    key: "handleButtonSubmit",
    value: function handleButtonSubmit() {
      var dataSet = this.context.dataSet;
      return dataSet.submit();
    }
  }, {
    key: "handleButtonDelete",
    value: function handleButtonDelete() {
      var dataSet = this.context.dataSet;
      return dataSet["delete"](dataSet.selected);
    }
  }, {
    key: "handleButtonRemove",
    value: function handleButtonRemove() {
      var dataSet = this.context.dataSet;
      dataSet.remove(dataSet.selected);
    }
  }, {
    key: "handleButtonReset",
    value: function handleButtonReset() {
      var dataSet = this.context.dataSet;
      dataSet.reset();
    }
  }, {
    key: "handleQueryReset",
    value: function handleQueryReset() {
      var queryDataSet = this.context.dataSet.queryDataSet;

      if (queryDataSet) {
        var current = queryDataSet.current;

        if (current) {
          current.reset();
        }

        this.handleQuery();
      }
    }
  }, {
    key: "handleExpandAll",
    value: function handleExpandAll() {
      var tableStore = this.context.tableStore;
      tableStore.expandAll();
    }
  }, {
    key: "handleCollapseAll",
    value: function handleCollapseAll() {
      var tableStore = this.context.tableStore;
      tableStore.collapseAll();
    }
  }, {
    key: "handleChangeExportStrategy",
    value: function handleChangeExportStrategy(value) {
      var dataSet = this.context.dataSet;
      dataSet.setState('__EXPORT-STRATEGY__', value);
    }
  }, {
    key: "handleButtonExport",
    value: function () {
      var _handleButtonExport = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() {
        var _this2 = this;

        var _this$context, tableStore, prefixCls, getConfig, columnHeaders;

        return _regenerator["default"].wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                _this$context = this.context, tableStore = _this$context.tableStore, prefixCls = _this$context.prefixCls, getConfig = _this$context.tableStore.getConfig;
                _context.next = 3;
                return tableStore.getColumnHeaders();

              case 3:
                columnHeaders = _context.sent;
                this.exportDataSet = new _dataSet["default"]({
                  data: columnHeaders,
                  paging: false
                }, {
                  getConfig: getConfig
                });
                this.exportDataSet.selectAll();
                this.handleChangeExportStrategy('ALL');
                this.exportModal = _modal["default"].open({
                  title: (0, _localeContext.$l)('Table', 'choose_export_columns'),
                  children: /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement(_Table["default"], {
                    dataSet: this.exportDataSet,
                    style: {
                      height: (0, _UnitConvertor.pxToRem)(300)
                    }
                  }, /*#__PURE__*/_react["default"].createElement(_Column["default"], {
                    header: (0, _localeContext.$l)('Table', 'column_name'),
                    name: "label",
                    resizable: false
                  }))),
                  closable: true,
                  okText: (0, _localeContext.$l)('Table', 'export_button'),
                  onOk: this.handleExport,
                  footer: function footer(okBtn, cancelBtn) {
                    return /*#__PURE__*/_react["default"].createElement("div", {
                      className: "".concat(prefixCls, "-export-modal-footer")
                    }, /*#__PURE__*/_react["default"].createElement("div", {
                      className: "".concat(prefixCls, "-export-modal-footer-radio")
                    }, /*#__PURE__*/_react["default"].createElement(_radio["default"], {
                      name: "exportStrategy",
                      value: "ALL",
                      defaultChecked: true,
                      onChange: _this2.handleChangeExportStrategy
                    }, (0, _localeContext.$l)('Table', 'export_all')), /*#__PURE__*/_react["default"].createElement(_radio["default"], {
                      name: "exportStrategy",
                      value: "SELECTED",
                      onChange: _this2.handleChangeExportStrategy
                    }, (0, _localeContext.$l)('Table', 'export_selected'))), /*#__PURE__*/_react["default"].createElement("div", null, okBtn, cancelBtn));
                  },
                  style: {
                    width: (0, _UnitConvertor.pxToRem)(500)
                  }
                });

              case 8:
              case "end":
                return _context.stop();
            }
          }
        }, _callee, this);
      }));

      function handleButtonExport() {
        return _handleButtonExport.apply(this, arguments);
      }

      return handleButtonExport;
    }()
  }, {
    key: "handleQuery",
    value: function () {
      var _handleQuery = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2() {
        var _this$context2, tableStore, dataSet, queryDataSet;

        return _regenerator["default"].wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                _this$context2 = this.context, tableStore = _this$context2.tableStore, dataSet = _this$context2.dataSet, queryDataSet = _this$context2.dataSet.queryDataSet;

                if (!(tableStore.queryBar === _enum2.TableQueryBarType.filterBar)) {
                  _context2.next = 19;
                  break;
                }

                _context2.next = 4;
                return dataSet.modifiedCheck(undefined, dataSet, 'query');

              case 4:
                _context2.t2 = _context2.sent;

                if (!_context2.t2) {
                  _context2.next = 7;
                  break;
                }

                _context2.t2 = queryDataSet;

              case 7:
                _context2.t1 = _context2.t2;

                if (!_context2.t1) {
                  _context2.next = 10;
                  break;
                }

                _context2.t1 = queryDataSet.current;

              case 10:
                _context2.t0 = _context2.t1;

                if (!_context2.t0) {
                  _context2.next = 15;
                  break;
                }

                _context2.next = 14;
                return queryDataSet.current.validate();

              case 14:
                _context2.t0 = _context2.sent;

              case 15:
                if (!_context2.t0) {
                  _context2.next = 17;
                  break;
                }

                return _context2.abrupt("return", dataSet.query());

              case 17:
                _context2.next = 20;
                break;

              case 19:
                return _context2.abrupt("return", dataSet.query());

              case 20:
              case "end":
                return _context2.stop();
            }
          }
        }, _callee2, this);
      }));

      function handleQuery() {
        return _handleQuery.apply(this, arguments);
      }

      return handleQuery;
    }()
  }, {
    key: "handleExport",
    value: function handleExport() {
      var _this3 = this;

      var selected = this.exportDataSet.selected;
      var clientExportQuantity = this.props.clientExportQuantity,
          _this$context3 = this.context,
          prefixCls = _this$context3.prefixCls,
          dataSet = _this$context3.dataSet;

      if (selected.length) {
        var exportModal = this.exportModal;
        dataSet["export"](selected.reduce(function (columns, record) {
          var myName = record.get('name');
          var myField = dataSet.getField(myName);

          if (myField && myField.type === _enum4.FieldType.object) {
            var bindField = (0, _utils2.findBindFieldBy)(myField, dataSet.fields, 'textField');

            if (bindField) {
              myName = bindField.name;
            }
          }

          columns[myName] = record.get('label');
          return columns;
        }, {}), clientExportQuantity).then(function (exportData) {
          _this3.exportData = exportData;
        });

        if (exportModal) {
          exportModal.update({
            title: (0, _localeContext.$l)('Table', 'export_button'),
            children: /*#__PURE__*/_react["default"].createElement(ExportBody, {
              prefixCls: prefixCls,
              dataSet: dataSet
            }),
            onCancel: this.handleExportButton,
            footer: /*#__PURE__*/_react["default"].createElement(ExportFooter, {
              prefixCls: prefixCls,
              exportButton: this.handleExportButton,
              exportModal: exportModal,
              dataSet: dataSet
            })
          });
        }
      }

      return false;
    }
  }, {
    key: "handleExportButton",
    value: function handleExportButton(data, filename) {
      var _this$context4 = this.context,
          dataSet = _this$context4.dataSet,
          tableStore = _this$context4.tableStore;

      if (data === _enum4.DataSetExportStatus.success) {
        if (this.exportData) {
          (0, _utils2.exportExcel)(this.exportData, filename, tableStore.getConfig('xlsx'));
          this.exportModal.close();
          this.exportData = null;
        }
      } else if (data === _enum4.DataSetExportStatus.failed) {
        this.exportData = null;
        this.handleExport();
      } else {
        this.exportModal.close();
        this.exportData = null;
      }

      dataSet.exportStatus = undefined;
    }
  }, {
    key: "getButtonProps",
    value: function getButtonProps(type) {
      var _this$context5 = this.context,
          isTree = _this$context5.isTree,
          dataSet = _this$context5.dataSet;
      var disabled = dataSet.status !== _enum4.DataSetStatus.ready;

      switch (type) {
        case _enum2.TableButtonType.add:
          return {
            icon: 'playlist_add',
            onClick: this.handleButtonCreate,
            children: (0, _localeContext.$l)('Table', 'create_button'),
            disabled: disabled || (dataSet.parent ? !dataSet.parent.current : false)
          };

        case _enum2.TableButtonType.save:
          return {
            icon: 'save',
            onClick: this.handleButtonSubmit,
            children: (0, _localeContext.$l)('Table', 'save_button'),
            type: _enum3.ButtonType.submit,
            disabled: disabled
          };

        case _enum2.TableButtonType["delete"]:
          return {
            icon: 'delete',
            onClick: this.handleButtonDelete,
            children: (0, _localeContext.$l)('Table', 'delete_button'),
            disabled: disabled || dataSet.selected.length === 0
          };

        case _enum2.TableButtonType.remove:
          return {
            icon: 'remove_circle',
            onClick: this.handleButtonRemove,
            children: (0, _localeContext.$l)('Table', 'remove_button'),
            disabled: disabled || dataSet.selected.length === 0
          };

        case _enum2.TableButtonType.reset:
          return {
            icon: 'undo',
            onClick: this.handleButtonReset,
            children: (0, _localeContext.$l)('Table', 'reset_button'),
            type: _enum3.ButtonType.reset
          };

        case _enum2.TableButtonType.query:
          return {
            icon: 'search',
            onClick: this.handleQuery,
            children: (0, _localeContext.$l)('Table', 'query_button')
          };

        case _enum2.TableButtonType["export"]:
          return {
            icon: 'export',
            onClick: this.handleButtonExport,
            children: (0, _localeContext.$l)('Table', 'export_button')
          };

        case _enum2.TableButtonType.expandAll:
          return isTree ? {
            icon: 'add_box',
            onClick: this.handleExpandAll,
            children: (0, _localeContext.$l)('Table', 'expand_button')
          } : undefined;

        case _enum2.TableButtonType.collapseAll:
          return isTree ? {
            icon: 'short_text',
            onClick: this.handleCollapseAll,
            children: (0, _localeContext.$l)('Table', 'collapse_button')
          } : undefined;

        default:
      }
    }
    /**
     * 渲染表头汇总列
     * @param summary
     */

  }, {
    key: "renderSummary",
    value: function renderSummary(summary) {
      if (summary) {
        var length = summary.length;

        if (length) {
          var summaryBar = this.props.summaryBar;

          if (summaryBar) {
            var summaryLength = summaryBar.length;
            var _this$props = this.props,
                summaryFieldsLimit = _this$props.summaryFieldsLimit,
                summaryBarFieldWidth = _this$props.summaryBarFieldWidth,
                _this$context6 = this.context,
                tableStore = _this$context6.tableStore,
                dataSet = _this$context6.dataSet,
                prefixCls = _this$context6.prefixCls;
            var queryBarProps = tableStore.props.queryBarProps;
            var tableQueryBarProps = (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, tableStore.getConfig('queryBarProps')), queryBarProps);
            var summaryFieldsLimits = summaryFieldsLimit || tableQueryBarProps && tableQueryBarProps.summaryFieldsLimit || 3;
            var fieldTypeArr = [_enum4.FieldType.currency, _enum4.FieldType.number];
            return summary.reduce(function (list, summaryCol, index) {
              var hasSeparate = length > summaryFieldsLimits || index !== summaryLength - 1;

              if ((0, _isString["default"])(summaryCol)) {
                var field = dataSet.getField(summaryCol);

                if (field && fieldTypeArr.includes(field.get('type'))) {
                  var summaryValue = dataSet.reduce(function (sum, record) {
                    var n = record.get(summaryCol);

                    if ((0, _isNumber["default"])(n) || _dataset.math.isBigNumber(n)) {
                      return _dataset.math.plus(sum, n);
                    }

                    return sum;
                  }, 0);
                  var name = field.get('name');
                  var label = field.get('label');
                  var sumNode = _dataset.math.isBigNumber(summaryValue) ? _dataset.math.toString(summaryValue) : summaryValue;
                  list.push( /*#__PURE__*/_react["default"].createElement("div", {
                    key: name
                  }, /*#__PURE__*/_react["default"].createElement("div", {
                    className: "".concat(prefixCls, "-summary-col"),
                    style: {
                      width: summaryBarFieldWidth
                    }
                  }, /*#__PURE__*/_react["default"].createElement("div", {
                    className: "".concat(prefixCls, "-summary-col-label"),
                    title: String(label)
                  }, label, ":"), /*#__PURE__*/_react["default"].createElement("div", {
                    className: "".concat(prefixCls, "-summary-col-value"),
                    title: String(sumNode)
                  }, sumNode)), hasSeparate && /*#__PURE__*/_react["default"].createElement("div", {
                    className: "".concat(prefixCls, "-summary-col-separate")
                  }, /*#__PURE__*/_react["default"].createElement("div", null))));
                }
              } else if (typeof summaryCol === 'function') {
                var summaryObj = summaryCol({
                  summaryFieldsLimit: summaryFieldsLimits,
                  summaryBarFieldWidth: summaryBarFieldWidth,
                  dataSet: dataSet
                });
                list.push( /*#__PURE__*/_react["default"].createElement("div", {
                  key: (0, _isString["default"])(summaryObj.label) ? summaryObj.label : ''
                }, /*#__PURE__*/_react["default"].createElement("div", {
                  className: "".concat(prefixCls, "-summary-col"),
                  style: {
                    width: summaryBarFieldWidth
                  }
                }, /*#__PURE__*/_react["default"].createElement("div", {
                  className: "".concat(prefixCls, "-summary-col-label"),
                  title: (0, _isString["default"])(summaryObj.label) ? summaryObj.label : ''
                }, summaryObj.label, ":"), /*#__PURE__*/_react["default"].createElement("div", {
                  className: "".concat(prefixCls, "-summary-col-value"),
                  title: (0, _isString["default"])(summaryObj.value) || (0, _isNumber["default"])(summaryObj.value) ? summaryObj.value.toString() : ''
                }, summaryObj.value)), hasSeparate && /*#__PURE__*/_react["default"].createElement("div", {
                  className: "".concat(prefixCls, "-summary-col-separate")
                }, /*#__PURE__*/_react["default"].createElement("div", null))));
              }

              return list;
            }, []);
          }
        }
      }
    }
    /**
     * 汇总条展开收起按钮
     * @param summary
     */

  }, {
    key: "getMoreSummaryButton",
    value: function getMoreSummaryButton(summary) {
      var _this4 = this;

      if (summary.length) {
        var prefixCls = this.context.prefixCls;
        return /*#__PURE__*/_react["default"].createElement("div", {
          className: "".concat(prefixCls, "-summary-button-more")
        }, /*#__PURE__*/_react["default"].createElement("a", {
          onClick: function onClick() {
            return _this4.openMore(summary);
          }
        }, (0, _localeContext.$l)('Table', 'more'), this.moreSummary && this.moreSummary.length ? /*#__PURE__*/_react["default"].createElement(_icon["default"], {
          type: 'expand_less'
        }) : /*#__PURE__*/_react["default"].createElement(_icon["default"], {
          type: 'expand_more'
        })));
      }
    }
    /**
     * 渲染汇总条
     */

  }, {
    key: "getSummaryBar",
    value: function getSummaryBar() {
      var _this$props2 = this.props,
          summaryBar = _this$props2.summaryBar,
          summaryFieldsLimit = _this$props2.summaryFieldsLimit,
          _this$context7 = this.context,
          prefixCls = _this$context7.prefixCls,
          tableStore = _this$context7.tableStore;
      var queryBarProps = tableStore.props.queryBarProps;
      var tableQueryBarProps = (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, tableStore.getConfig('queryBarProps')), queryBarProps);
      var summaryFieldsLimits = summaryFieldsLimit || tableQueryBarProps && tableQueryBarProps.summaryFieldsLimit || 3;

      if (summaryBar) {
        var currentSummaryBar = this.renderSummary(summaryBar.slice(0, summaryFieldsLimits));
        var moreSummary = summaryBar.slice(summaryFieldsLimits);
        var moreSummaryButton = this.getMoreSummaryButton(moreSummary);
        return /*#__PURE__*/_react["default"].createElement("div", {
          className: "".concat(prefixCls, "-summary-group-wrapper")
        }, /*#__PURE__*/_react["default"].createElement("div", {
          className: "".concat(prefixCls, "-summary-group")
        }, currentSummaryBar, this.moreSummary), moreSummaryButton);
      }
    }
    /**
     * buttons 大于 buttonsLimits 放入下拉
     * @param buttonsLimits
     * @param hasCombineSort
     */

  }, {
    key: "getMoreButton",
    value: function getMoreButton(buttonsLimits, hasCombineSort) {
      var _this5 = this;

      var buttons = this.props.buttons;
      var _this$context8 = this.context,
          prefixCls = _this$context8.prefixCls,
          tableStore = _this$context8.tableStore;
      var tableButtonProps = tableStore.getConfig('tableButtonProps');
      var children = [];

      if (buttons && buttons.length && buttonsLimits) {
        buttons.slice(hasCombineSort ? buttonsLimits - 1 : buttonsLimits).forEach(function (button) {
          var props = {};

          if ((0, _mobx.isArrayLike)(button)) {
            props = button[1] || {};
            button = button[0];
          }

          if ((0, _isString["default"])(button) && button in _enum2.TableButtonType) {
            var _props = props,
                afterClick = _props.afterClick,
                buttonProps = (0, _objectWithoutProperties2["default"])(_props, _excluded);

            var defaultButtonProps = _this5.getButtonProps(button);

            if (defaultButtonProps) {
              if (afterClick) {
                var onClick = defaultButtonProps.onClick;

                defaultButtonProps.onClick = /*#__PURE__*/function () {
                  var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3(e) {
                    return _regenerator["default"].wrap(function _callee3$(_context3) {
                      while (1) {
                        switch (_context3.prev = _context3.next) {
                          case 0:
                            e.persist();
                            _context3.prev = 1;
                            _context3.next = 4;
                            return onClick(e);

                          case 4:
                            _context3.prev = 4;
                            afterClick(e);
                            return _context3.finish(4);

                          case 7:
                          case "end":
                            return _context3.stop();
                        }
                      }
                    }, _callee3, null, [[1,, 4, 7]]);
                  }));

                  return function (_x) {
                    return _ref.apply(this, arguments);
                  };
                }();
              }

              children.push( /*#__PURE__*/_react["default"].createElement(_menu["default"].Item, {
                hidden: tableButtonProps.hidden,
                key: button
              }, /*#__PURE__*/_react["default"].createElement(_Button["default"], (0, _extends2["default"])({
                key: "".concat(button, "-btn")
              }, tableButtonProps, defaultButtonProps, buttonProps, {
                funcType: _enum3.FuncType.link
              }))));
            }
          } else if ( /*#__PURE__*/(0, _react.isValidElement)(button)) {
            children.push( /*#__PURE__*/_react["default"].createElement(_menu["default"].Item, {
              hidden: button.props.hidden,
              key: button.key
            }, /*#__PURE__*/(0, _react.cloneElement)(button, (0, _objectSpread2["default"])((0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, tableButtonProps), button.props), {}, {
              funcType: _enum3.FuncType.link
            }))));
          } else if ((0, _isObject["default"])(button)) {
            children.push( /*#__PURE__*/_react["default"].createElement(_menu["default"].Item, {
              hidden: props.hidden,
              key: button.key
            }, /*#__PURE__*/_react["default"].createElement(_Button["default"], (0, _extends2["default"])({}, tableButtonProps, button, {
              funcType: _enum3.FuncType.link
            }))));
          }
        });
      }

      var menu = /*#__PURE__*/_react["default"].createElement(_menu["default"], {
        prefixCls: "".concat(prefixCls, "-dropdown-menu")
      }, children);

      return /*#__PURE__*/_react["default"].createElement(_Dropdown["default"], {
        overlay: menu,
        key: "dropdown_button"
      }, /*#__PURE__*/_react["default"].createElement(_Button["default"], (0, _extends2["default"])({}, tableButtonProps, {
        key: "more_button"
      }), /*#__PURE__*/_react["default"].createElement("span", null, (0, _localeContext.$l)('Table', 'more')), /*#__PURE__*/_react["default"].createElement(_icon["default"], {
        type: 'expand_more'
      })));
    }
  }, {
    key: "getButtons",
    value: function getButtons() {
      var _this6 = this;

      var _this$context$tableSt = this.context.tableStore,
          queryBar = _this$context$tableSt.queryBar,
          prefixCls = _this$context$tableSt.prefixCls,
          dataSet = _this$context$tableSt.dataSet,
          customizedBtn = _this$context$tableSt.customizedBtn,
          customizable = _this$context$tableSt.customizable,
          customizedColumnHeader = _this$context$tableSt.customizedColumnHeader;
      var _this$props3 = this.props,
          originalButtons = _this$props3.buttons,
          summaryBar = _this$props3.summaryBar,
          buttonsLimit = _this$props3.buttonsLimit;
      var tableStore = this.context.tableStore;
      var children = [];
      var buttons = originalButtons;

      if (customizable && customizedBtn) {
        buttons = [customizedColumnHeader()].concat((0, _toConsumableArray2["default"])(buttons || []));
      }

      var hasCombineSort = queryBar !== _enum2.TableQueryBarType.filterBar && dataSet.props.combineSort;

      if (hasCombineSort) {
        var sortableFieldNames = this.getSortableFieldNames();

        if (sortableFieldNames.length > 0) {
          buttons = [/*#__PURE__*/_react["default"].createElement(_CombineSort["default"], {
            key: "CombineSort",
            dataSet: dataSet,
            prefixCls: prefixCls,
            sortableFieldNames: sortableFieldNames
          })].concat((0, _toConsumableArray2["default"])(buttons || []));
        }
      }

      if (buttons) {
        // 汇总条存在下 buttons 大于 3 个放入下拉
        var buttonsLimits = summaryBar ? buttonsLimit || 3 : buttonsLimit;
        var tableButtonProps = tableStore.getConfig('tableButtonProps');
        var buttonsArr = buttons.slice(0, buttonsLimits);
        buttonsArr.forEach(function (button) {
          var props = {};

          if ((0, _mobx.isArrayLike)(button)) {
            props = button[1] || {};
            button = button[0];
          }

          if ((0, _isString["default"])(button) && button in _enum2.TableButtonType) {
            var _props2 = props,
                afterClick = _props2.afterClick,
                buttonProps = (0, _objectWithoutProperties2["default"])(_props2, _excluded2);

            var defaultButtonProps = _this6.getButtonProps(button);

            if (defaultButtonProps) {
              if (afterClick) {
                var onClick = defaultButtonProps.onClick;

                defaultButtonProps.onClick = /*#__PURE__*/function () {
                  var _ref2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4(e) {
                    return _regenerator["default"].wrap(function _callee4$(_context4) {
                      while (1) {
                        switch (_context4.prev = _context4.next) {
                          case 0:
                            e.persist();
                            _context4.prev = 1;
                            _context4.next = 4;
                            return onClick(e);

                          case 4:
                            _context4.prev = 4;
                            afterClick(e);
                            return _context4.finish(4);

                          case 7:
                          case "end":
                            return _context4.stop();
                        }
                      }
                    }, _callee4, null, [[1,, 4, 7]]);
                  }));

                  return function (_x2) {
                    return _ref2.apply(this, arguments);
                  };
                }();
              }

              children.push( /*#__PURE__*/_react["default"].createElement(_Button["default"], (0, _extends2["default"])({
                key: button
              }, tableButtonProps, defaultButtonProps, buttonProps)));
            }
          } else if ( /*#__PURE__*/(0, _react.isValidElement)(button)) {
            children.push( /*#__PURE__*/(0, _react.cloneElement)(button, (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, tableButtonProps), button.props)));
          } else if ((0, _isObject["default"])(button)) {
            children.push( /*#__PURE__*/_react["default"].createElement(_Button["default"], (0, _extends2["default"])({}, tableButtonProps, button)));
          }
        });

        if (buttonsLimits && buttons.length > buttonsLimits) {
          var moreButton = this.getMoreButton(buttonsLimits, hasCombineSort);
          children.push(moreButton);
        }
      }

      return children;
    }
  }, {
    key: "getQueryFields",
    value: function getQueryFields() {
      var _this$context9 = this.context,
          dataSet = _this$context9.dataSet,
          _this$context9$tableS = _this$context9.tableStore,
          queryBar = _this$context9$tableS.queryBar,
          getConfig = _this$context9$tableS.getConfig,
          _this$props4 = this.props,
          queryFields = _this$props4.queryFields,
          buttons = _this$props4.buttons;
      var queryDataSet = dataSet.queryDataSet;
      var result = [];

      if (queryDataSet) {
        var fields = queryDataSet.fields,
            current = queryDataSet.current,
            _queryDataSet$props$f = queryDataSet.props.fields,
            propFields = _queryDataSet$props$f === void 0 ? [] : _queryDataSet$props$f; // 减少重复渲染
        // 使用了 buttons 判断，是因为 getButtons 中有用 DataSet.status 可观察对象判断，确保 buttons 用到 status 时，此处才用 status 判断，减少渲染

        if (this.queryFieldsElement.length && (!current || buttons && buttons.some(function (button) {
          return (0, _isString["default"])(button) && button in _enum2.TableButtonType;
        }) && dataSet.status !== _enum4.DataSetStatus.ready)) return this.queryFieldsElement;
        var cloneFields = fields.toJS();
        var tlsKey = getConfig('tlsKey');

        var processField = function processField(field, name) {
          if (!field.get('bind', current) && !name.includes(tlsKey)) {
            var element = queryFields[name];
            var filterBarProps = {};

            if (queryBar === _enum2.TableQueryBarType.filterBar) {
              var placeholder = /*#__PURE__*/(0, _react.isValidElement)(element) && element.props.placeholder ? element.props.placeholder : (0, _utils.getPlaceholderByField)(field, current);
              filterBarProps = {
                placeholder: placeholder,
                border: false,
                clearButton: true
              };
            }

            var props = (0, _objectSpread2["default"])({
              key: name,
              name: name,
              dataSet: queryDataSet,
              isFlat: queryBar === _enum2.TableQueryBarType.filterBar
            }, filterBarProps);
            result.push( /*#__PURE__*/(0, _react.isValidElement)(element) ? /*#__PURE__*/(0, _react.cloneElement)(element, props) : /*#__PURE__*/(0, _react.cloneElement)((0, _utils.getEditorByField)(field, current, queryBar !== _enum2.TableQueryBarType.professionalBar, queryBar === _enum2.TableQueryBarType.filterBar || queryBar === _enum2.TableQueryBarType.comboBar), (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, props), (0, _isObject["default"])(element) ? element : {})));
          }
        };

        propFields.forEach(function (_ref3) {
          var name = _ref3.name;

          if (name) {
            var field = cloneFields.get(name);

            if (field) {
              cloneFields["delete"](name);
              processField(field, name);
            }
          }
        });
        cloneFields.forEach(function (field, name) {
          processField(field, name);
        });
      }

      this.queryFieldsElement = result;
      return result;
    }
  }, {
    key: "getSortableFieldNames",
    value: function getSortableFieldNames() {
      var columnGroups = this.context.tableStore.columnGroups;
      var columns = columnGroups.columns;
      var headerRows = (0, _utils.getTableHeaderRows)(columns);
      var sortableFieldNames = new Set();
      headerRows.forEach(function (cols) {
        cols.forEach(function (col) {
          if (col.column && col.column.name && col.column.sortable) {
            sortableFieldNames.add(col.column.name);
          }
        });
      });
      return (0, _toConsumableArray2["default"])(sortableFieldNames);
    }
  }, {
    key: "renderToolBar",
    value: function renderToolBar(props) {
      var prefixCls = this.context.prefixCls;
      return /*#__PURE__*/_react["default"].createElement(_TableToolBar["default"], (0, _extends2["default"])({
        key: "toolbar",
        prefixCls: prefixCls
      }, props));
    }
  }, {
    key: "renderFilterBar",
    value: function renderFilterBar(props) {
      var prefixCls = this.context.prefixCls;
      var _this$props5 = this.props,
          filterBarFieldName = _this$props5.filterBarFieldName,
          filterBarPlaceholder = _this$props5.filterBarPlaceholder;
      return /*#__PURE__*/_react["default"].createElement(_TableFilterBar["default"], (0, _extends2["default"])({
        key: "toolbar",
        prefixCls: prefixCls,
        paramName: filterBarFieldName,
        placeholder: filterBarPlaceholder
      }, props));
    }
  }, {
    key: "renderAdvancedQueryBar",
    value: function renderAdvancedQueryBar(props) {
      var prefixCls = this.context.prefixCls;
      return /*#__PURE__*/_react["default"].createElement(_TableAdvancedQueryBar["default"], (0, _extends2["default"])({
        key: "toolbar",
        prefixCls: prefixCls
      }, props));
    }
  }, {
    key: "renderProfessionalBar",
    value: function renderProfessionalBar(props) {
      var prefixCls = this.context.prefixCls;
      return /*#__PURE__*/_react["default"].createElement(_TableProfessionalBar["default"], (0, _extends2["default"])({
        key: "toolbar",
        prefixCls: prefixCls
      }, props));
    }
  }, {
    key: "renderDynamicFilterBar",
    value: function renderDynamicFilterBar(props) {
      var _this$props6 = this.props,
          dynamicFilterBar = _this$props6.dynamicFilterBar,
          searchCode = _this$props6.searchCode;
      var _this$context10 = this.context,
          prefixCls = _this$context10.prefixCls,
          tableStore = _this$context10.tableStore;
      var sortableFieldNames = this.getSortableFieldNames();
      return /*#__PURE__*/_react["default"].createElement(_TableDynamicFilterBar["default"], (0, _extends2["default"])({
        key: "toolbar",
        searchCode: searchCode,
        dynamicFilterBar: dynamicFilterBar,
        prefixCls: prefixCls,
        sortableFieldNames: sortableFieldNames,
        tableStore: tableStore
      }, props));
    }
  }, {
    key: "renderComboBar",
    value: function renderComboBar(props) {
      var comboFilterBar = this.props.comboFilterBar;
      var prefixCls = this.context.prefixCls;
      return /*#__PURE__*/_react["default"].createElement(_TableComboBar["default"], (0, _extends2["default"])({
        key: "toolbar",
        comboFilterBar: comboFilterBar,
        prefixCls: prefixCls
      }, props));
    }
  }, {
    key: "expandTree",
    value: function expandTree() {
      var tableStore = this.context.tableStore;
      var queryBarProps = tableStore.props.queryBarProps;

      if (queryBarProps && typeof queryBarProps.onQuery === 'function') {
        queryBarProps.onQuery();
      }

      tableStore.expandAll();
    }
  }, {
    key: "collapseTree",
    value: function collapseTree() {
      var tableStore = this.context.tableStore;
      var queryBarProps = tableStore.props.queryBarProps;

      if (queryBarProps && typeof queryBarProps.onReset === 'function') {
        queryBarProps.onReset();
      }

      tableStore.collapseAll();
    }
  }, {
    key: "render",
    value: function render() {
      var buttons = this.getButtons();
      var summaryBar = this.getSummaryBar();
      var _this$context11 = this.context,
          prefixCls = _this$context11.prefixCls,
          dataSet = _this$context11.dataSet,
          isTree = _this$context11.isTree,
          tableStore = _this$context11.tableStore,
          _this$context11$table = _this$context11.tableStore,
          queryBar = _this$context11$table.queryBar,
          queryBarProps = _this$context11$table.props.queryBarProps,
          _this$props7 = this.props,
          queryFieldsLimit = _this$props7.queryFieldsLimit,
          summaryFieldsLimit = _this$props7.summaryFieldsLimit,
          pagination = _this$props7.pagination,
          treeQueryExpanded = _this$props7.treeQueryExpanded,
          showQueryBar = this.showQueryBar;

      if (showQueryBar) {
        var queryDataSet = dataSet.queryDataSet;
        var queryFields = this.getQueryFields();
        var tableQueryBarProps = (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, tableStore.getConfig('queryBarProps')), queryBarProps);
        var onReset = typeof tableQueryBarProps.onReset === 'function' ? tableQueryBarProps.onReset : _noop["default"];
        var onQuery = typeof tableQueryBarProps.onQuery === 'function' ? tableQueryBarProps.onQuery : _noop["default"];
        var queryFieldsLimits = queryFieldsLimit || tableQueryBarProps.queryFieldsLimit;
        var summaryFieldsLimits = summaryFieldsLimit || tableQueryBarProps.summaryFieldsLimit || 3;
        var props = (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, tableQueryBarProps), {}, {
          dataSet: dataSet,
          queryDataSet: queryDataSet,
          buttons: buttons,
          pagination: pagination,
          queryFields: queryFields,
          queryFieldsLimit: queryFieldsLimits,
          summaryFieldsLimit: summaryFieldsLimits,
          summaryBar: summaryBar,
          onQuery: treeQueryExpanded && isTree ? this.expandTree : onQuery,
          onReset: treeQueryExpanded && isTree ? this.collapseTree : onReset
        });

        if (typeof queryBar === 'function') {
          return queryBar(props);
        }

        switch (queryBar) {
          case _enum2.TableQueryBarType.normal:
            return this.renderToolBar(props);

          case _enum2.TableQueryBarType.bar:
            return this.renderFilterBar(props);

          case _enum2.TableQueryBarType.advancedBar:
            return this.renderAdvancedQueryBar(props);

          case _enum2.TableQueryBarType.professionalBar:
            return this.renderProfessionalBar(props);

          case _enum2.TableQueryBarType.filterBar:
            return this.renderDynamicFilterBar(props);

          case _enum2.TableQueryBarType.comboBar:
            return this.renderComboBar(props);

          default:
        }
      }

      return [/*#__PURE__*/_react["default"].createElement(_TableButtons["default"], {
        key: "toolbar",
        prefixCls: prefixCls,
        buttons: buttons
      }, summaryBar), pagination];
    }
  }], [{
    key: "contextType",
    get: function get() {
      return _TableContext["default"];
    }
  }]);
  return TableQueryBar;
}(_react.Component);

TableQueryBar.displayName = 'TableQueryBar';
TableQueryBar.defaultProps = {
  summaryBarFieldWidth: 170
};
(0, _tslib.__decorate)([_mobx.observable], TableQueryBar.prototype, "moreSummary", void 0);
(0, _tslib.__decorate)([_autobind["default"]], TableQueryBar.prototype, "handleButtonCreate", null);
(0, _tslib.__decorate)([_autobind["default"]], TableQueryBar.prototype, "handleButtonSubmit", null);
(0, _tslib.__decorate)([_autobind["default"]], TableQueryBar.prototype, "handleButtonDelete", null);
(0, _tslib.__decorate)([_autobind["default"]], TableQueryBar.prototype, "handleButtonRemove", null);
(0, _tslib.__decorate)([_autobind["default"]], TableQueryBar.prototype, "handleButtonReset", null);
(0, _tslib.__decorate)([_autobind["default"]], TableQueryBar.prototype, "handleQueryReset", null);
(0, _tslib.__decorate)([_autobind["default"]], TableQueryBar.prototype, "handleExpandAll", null);
(0, _tslib.__decorate)([_autobind["default"]], TableQueryBar.prototype, "handleCollapseAll", null);
(0, _tslib.__decorate)([_autobind["default"]], TableQueryBar.prototype, "handleChangeExportStrategy", null);
(0, _tslib.__decorate)([_autobind["default"]], TableQueryBar.prototype, "handleButtonExport", null);
(0, _tslib.__decorate)([_autobind["default"]], TableQueryBar.prototype, "handleQuery", null);
(0, _tslib.__decorate)([_autobind["default"]], TableQueryBar.prototype, "handleExport", null);
(0, _tslib.__decorate)([_autobind["default"], _mobx.action], TableQueryBar.prototype, "handleExportButton", null);
(0, _tslib.__decorate)([_mobx.action], TableQueryBar.prototype, "openMore", void 0);
(0, _tslib.__decorate)([_autobind["default"]], TableQueryBar.prototype, "expandTree", null);
(0, _tslib.__decorate)([_autobind["default"]], TableQueryBar.prototype, "collapseTree", null);
TableQueryBar = (0, _tslib.__decorate)([_mobxReact.observer], TableQueryBar);
var _default = TableQueryBar;
exports["default"] = _default;
//# sourceMappingURL=index.js.map
