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

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

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

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

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

var _reactDom = require("react-dom");

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

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

var _checkbox = _interopRequireDefault(require("../checkbox"));

var _search = _interopRequireDefault(require("./search"));

var _item = _interopRequireDefault(require("./item"));

var _triggerEvent = _interopRequireDefault(require("../_util/triggerEvent"));

var _animate = _interopRequireDefault(require("../animate"));

var _PureRenderMixin = _interopRequireDefault(require("../rc-components/util/PureRenderMixin"));

function isIEorEDGE() {
  return document.documentMode || /Edge/.test(navigator.userAgent);
}

function isRenderResultPlainObject(result) {
  return result && ! /*#__PURE__*/(0, _react.isValidElement)(result) && Object.prototype.toString.call(result) === '[object Object]';
}

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

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

  function TransferList() {
    var _this;

    (0, _classCallCheck2["default"])(this, TransferList);
    _this = _super.apply(this, arguments);
    _this.state = {
      mounted: false
    };

    _this.handleSelect = function (selectedItem) {
      var _this$props = _this.props,
          checkedKeys = _this$props.checkedKeys,
          handleSelect = _this$props.handleSelect;
      var result = checkedKeys.some(function (key) {
        return key === selectedItem.key;
      });
      handleSelect(selectedItem, !result);
    };

    _this.handleFilter = function (e) {
      var _this$props2 = _this.props,
          handleFilter = _this$props2.handleFilter,
          prefixCls = _this$props2.prefixCls;
      handleFilter(e);

      if (!e.target.value) {
        return;
      } // Manually trigger scroll event for lazy search bug


      _this.triggerScrollTimer = window.setTimeout(function () {
        var transferNode = (0, _reactDom.findDOMNode)((0, _assertThisInitialized2["default"])(_this));
        var listNode = transferNode.querySelectorAll(".".concat(prefixCls, "-content"))[0];

        if (listNode) {
          (0, _triggerEvent["default"])(listNode, 'scroll');
        }
      }, 0);

      _this.fixIERepaint();
    };

    _this.handleClear = function () {
      var handleClear = _this.props.handleClear;
      handleClear();

      _this.fixIERepaint();
    };

    _this.matchFilter = function (text, item) {
      var _this$props3 = _this.props,
          filter = _this$props3.filter,
          filterOption = _this$props3.filterOption;

      if (filterOption) {
        return filterOption(filter, item);
      }

      return text.indexOf(filter) >= 0;
    };

    _this.renderItem = function (item) {
      var _this$props$render = _this.props.render,
          render = _this$props$render === void 0 ? _noop["default"] : _this$props$render;
      var renderResult = render(item);
      var isRenderResultPlain = isRenderResultPlainObject(renderResult);
      return {
        renderedText: isRenderResultPlain ? renderResult.value : renderResult,
        renderedEl: isRenderResultPlain ? renderResult.label : renderResult
      };
    };

    _this.saveNotFoundRef = function (node) {
      _this.notFoundNode = node;
    };

    return _this;
  }

  (0, _createClass2["default"])(TransferList, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var _this2 = this;

      this.timer = window.setTimeout(function () {
        _this2.setState({
          mounted: true
        });
      }, 0);
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      clearTimeout(this.timer);
      clearTimeout(this.triggerScrollTimer);
      clearTimeout(this.fixIERepaintTimer);
    }
  }, {
    key: "shouldComponentUpdate",
    value: function shouldComponentUpdate() {
      for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
        args[_key] = arguments[_key];
      }

      return _PureRenderMixin["default"].shouldComponentUpdate.apply(this, args);
    }
  }, {
    key: "getCheckStatus",
    value: function getCheckStatus(filteredDataSource) {
      var checkedKeys = this.props.checkedKeys;

      if (checkedKeys.length === 0) {
        return 'none';
      }

      if (filteredDataSource.every(function (item) {
        return checkedKeys.indexOf(item.key) >= 0;
      })) {
        return 'all';
      }

      return 'part';
    } // Fix IE/Edge repaint

  }, {
    key: "fixIERepaint",
    value: function fixIERepaint() {
      var _this3 = this;

      if (!isIEorEDGE()) {
        return;
      }

      this.fixIERepaintTimer = window.setTimeout(function () {
        if (_this3.notFoundNode) {
          var className = _this3.notFoundNode.className;
          _this3.notFoundNode.className = className;
        }
      }, 0);
    }
  }, {
    key: "render",
    value: function render() {
      var _this4 = this;

      var _this$props4 = this.props,
          prefixCls = _this$props4.prefixCls,
          checkboxPrefixCls = _this$props4.checkboxPrefixCls,
          dataSource = _this$props4.dataSource,
          titleText = _this$props4.titleText,
          checkedKeys = _this$props4.checkedKeys,
          highlightKey = _this$props4.highlightKey,
          lazy = _this$props4.lazy,
          _this$props4$body = _this$props4.body,
          body = _this$props4$body === void 0 ? _noop["default"] : _this$props4$body,
          _this$props4$footer = _this$props4.footer,
          footer = _this$props4$footer === void 0 ? _noop["default"] : _this$props4$footer,
          showSearch = _this$props4.showSearch,
          style = _this$props4.style,
          filter = _this$props4.filter,
          searchPlaceholder = _this$props4.searchPlaceholder,
          notFoundContent = _this$props4.notFoundContent,
          itemUnit = _this$props4.itemUnit,
          itemsUnit = _this$props4.itemsUnit,
          onScroll = _this$props4.onScroll,
          handleSelectAll = _this$props4.handleSelectAll,
          inputProps = _this$props4.inputProps;
      var mounted = this.state.mounted; // Custom Layout

      var footerDom = footer((0, _objectSpread2["default"])({}, this.props));
      var bodyDom = body((0, _objectSpread2["default"])({}, this.props));
      var listCls = (0, _classnames["default"])(prefixCls, (0, _defineProperty2["default"])({}, "".concat(prefixCls, "-with-footer"), !!footerDom));
      var filteredDataSource = [];
      var totalDataSource = [];
      var showItems = dataSource.map(function (item) {
        var _this4$renderItem = _this4.renderItem(item),
            renderedText = _this4$renderItem.renderedText,
            renderedEl = _this4$renderItem.renderedEl;

        if (filter && filter.trim() && !_this4.matchFilter(renderedText, item)) {
          return null;
        } // all show items


        totalDataSource.push(item);

        if (!item.disabled) {
          // response to checkAll items
          filteredDataSource.push(item);
        }

        var checked = checkedKeys.indexOf(item.key) >= 0;
        var isHighlight = highlightKey === item.key;
        return /*#__PURE__*/_react["default"].createElement(_item["default"], {
          key: item.key,
          item: item,
          lazy: lazy,
          renderedText: renderedText,
          renderedEl: renderedEl,
          checked: checked,
          isHighlight: isHighlight,
          prefixCls: prefixCls,
          onClick: _this4.handleSelect,
          checkboxPrefixCls: checkboxPrefixCls
        });
      });
      var unit = dataSource.length > 1 ? itemsUnit : itemUnit;
      var search = showSearch ? /*#__PURE__*/_react["default"].createElement("div", {
        className: "".concat(prefixCls, "-body-search-wrapper")
      }, /*#__PURE__*/_react["default"].createElement(_search["default"], {
        prefixCls: "".concat(prefixCls, "-search"),
        onChange: this.handleFilter,
        handleClear: this.handleClear,
        placeholder: searchPlaceholder,
        value: filter,
        inputProps: inputProps
      })) : null;

      var listBody = bodyDom || /*#__PURE__*/_react["default"].createElement("div", {
        className: showSearch ? "".concat(prefixCls, "-body ").concat(prefixCls, "-body-with-search") : "".concat(prefixCls, "-body")
      }, search, /*#__PURE__*/_react["default"].createElement(_animate["default"], {
        component: "ul",
        componentProps: {
          onScroll: onScroll
        },
        className: "".concat(prefixCls, "-content"),
        transitionName: mounted ? "".concat(prefixCls, "-content-item-highlight") : '',
        transitionLeave: false
      }, showItems), /*#__PURE__*/_react["default"].createElement("div", {
        className: "".concat(prefixCls, "-body-not-found"),
        ref: this.saveNotFoundRef
      }, notFoundContent));

      var listFooter = footerDom ? /*#__PURE__*/_react["default"].createElement("div", {
        className: "".concat(prefixCls, "-footer")
      }, footerDom) : null;
      var checkStatus = this.getCheckStatus(filteredDataSource);
      var checkedAll = checkStatus === 'all';

      var checkAllCheckbox = /*#__PURE__*/_react["default"].createElement(_checkbox["default"], {
        prefixCls: checkboxPrefixCls,
        checked: checkedAll,
        indeterminate: checkStatus === 'part',
        onChange: function onChange() {
          return handleSelectAll(filteredDataSource, checkedAll);
        }
      });

      return /*#__PURE__*/_react["default"].createElement("div", {
        className: listCls,
        style: style
      }, /*#__PURE__*/_react["default"].createElement("div", {
        className: "".concat(prefixCls, "-header")
      }, checkAllCheckbox, /*#__PURE__*/_react["default"].createElement("span", {
        className: "".concat(prefixCls, "-header-selected")
      }, /*#__PURE__*/_react["default"].createElement("span", null, (checkedKeys.length > 0 ? "".concat(checkedKeys.length, "/") : '') + totalDataSource.length, ' ', unit), /*#__PURE__*/_react["default"].createElement("span", {
        className: "".concat(prefixCls, "-header-title")
      }, titleText))), listBody, listFooter);
    }
  }]);
  return TransferList;
}(_react.Component);

exports["default"] = TransferList;
TransferList.displayName = 'TransferList';
TransferList.defaultProps = {
  dataSource: [],
  titleText: '',
  showSearch: false,
  render: _noop["default"],
  lazy: {}
};
//# sourceMappingURL=list.js.map
