"use strict";

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

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

var _propTypes = _interopRequireDefault(require("prop-types"));

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

var _util = require("../util");

var _header = _interopRequireDefault(require("./fixed/header"));

var _body = _interopRequireDefault(require("./fixed/body"));

var _wrapper = _interopRequireDefault(require("./fixed/wrapper"));

var _util2 = require("./util");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function fixed(BaseComponent) {
  /** Table */
  var FixedTable =
  /*#__PURE__*/
  function (_React$Component) {
    _inherits(FixedTable, _React$Component);

    function FixedTable() {
      var _getPrototypeOf2;

      var _this;

      _classCallCheck(this, FixedTable);

      for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
        args[_key] = arguments[_key];
      }

      _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(FixedTable)).call.apply(_getPrototypeOf2, [this].concat(args)));

      _defineProperty(_assertThisInitialized(_this), "getNode", function (type, node, lockType) {
        lockType = lockType ? lockType.charAt(0).toUpperCase() + lockType.substr(1) : '';
        _this["".concat(type).concat(lockType, "Node")] = node;

        if (type === 'header' && !lockType) {
          _this.innerHeaderNode = _this.headerNode.querySelector('div');
        }
      });

      _defineProperty(_assertThisInitialized(_this), "onBodyScroll", function () {
        _this.scrollTo(_this.bodyNode.scrollLeft, _this.bodyNode.scrollTop);
      });

      return _this;
    }

    _createClass(FixedTable, [{
      key: "getChildContext",
      value: function getChildContext() {
        return {
          fixedHeader: this.props.fixedHeader,
          maxBodyHeight: this.props.maxBodyHeight,
          onBodyScroll: this.onBodyScroll,
          getNode: this.getNode
        };
      }
    }, {
      key: "componentDidMount",
      value: function componentDidMount() {
        this.adjustFixedHeaderSize();
      }
    }, {
      key: "componentDidUpdate",
      value: function componentDidUpdate() {
        this.adjustFixedHeaderSize();
      }
    }, {
      key: "adjustFixedHeaderSize",
      value: function adjustFixedHeaderSize() {
        var _this$props = this.props,
            hasHeader = _this$props.hasHeader,
            fixedHeader = _this$props.fixedHeader,
            rtl = _this$props.rtl;
        var paddingName = rtl ? 'paddingLeft' : 'paddingRight';

        if (hasHeader && fixedHeader && !this.props.lockType) {
          if (this.bodyNode.scrollHeight > this.bodyNode.clientHeight) {
            _util.dom.setStyle(this.headerNode, paddingName, _util.dom.scrollbar().width);
          } else {
            _util.dom.setStyle(this.headerNode, paddingName, 0);
          }
        }
      }
    }, {
      key: "scrollTo",
      value: function scrollTo(x) {
        if (this.innerHeaderNode) {
          this.innerHeaderNode.scrollLeft = x;
        }
      }
    }, {
      key: "render",
      value: function render() {
        /* eslint-disable no-unused-vars, prefer-const */
        var _this$props2 = this.props,
            components = _this$props2.components,
            className = _this$props2.className,
            prefix = _this$props2.prefix,
            fixedHeader = _this$props2.fixedHeader,
            maxBodyHeight = _this$props2.maxBodyHeight,
            others = _objectWithoutProperties(_this$props2, ["components", "className", "prefix", "fixedHeader", "maxBodyHeight"]);

        if (fixedHeader) {
          var _classnames;

          components = _objectSpread({}, components);

          if (!components.Header) {
            components.Header = _header["default"];
          }

          if (!components.Body) {
            components.Body = _body["default"];
          }

          if (!components.Wrapper) {
            components.Wrapper = _wrapper["default"];
          }

          className = (0, _classnames2["default"])((_classnames = {}, _defineProperty(_classnames, "".concat(prefix, "table-fixed"), true), _defineProperty(_classnames, className, className), _classnames));
        }

        return _react["default"].createElement(BaseComponent, _extends({}, others, {
          components: components,
          className: className,
          prefix: prefix
        }));
      }
    }]);

    return FixedTable;
  }(_react["default"].Component);

  _defineProperty(FixedTable, "FixedHeader", _header["default"]);

  _defineProperty(FixedTable, "FixedBody", _body["default"]);

  _defineProperty(FixedTable, "FixedWrapper", _wrapper["default"]);

  _defineProperty(FixedTable, "propTypes", _objectSpread({
    /**
     * 是否具有表头
     */
    hasHeader: _propTypes["default"].bool,

    /**
     * 表头是否固定，该属性配合maxBodyHeight使用，当内容区域的高度超过maxBodyHeight的时候，在内容区域会出现滚动条
     */
    fixedHeader: _propTypes["default"].bool,

    /**
     * 最大内容区域的高度,在`fixedHeader`为`true`的时候,超过这个高度会出现滚动条
     */
    maxBodyHeight: _propTypes["default"].oneOfType([_propTypes["default"].number, _propTypes["default"].string])
  }, BaseComponent.propTypes));

  _defineProperty(FixedTable, "defaultProps", _objectSpread({}, BaseComponent.defaultProps, {
    hasHeader: true,
    fixedHeader: false,
    maxBodyHeight: 200,
    components: {},
    refs: {},
    prefix: 'next-'
  }));

  _defineProperty(FixedTable, "childContextTypes", {
    fixedHeader: _propTypes["default"].bool,
    maxBodyHeight: _propTypes["default"].oneOfType([_propTypes["default"].number, _propTypes["default"].string]),
    onBodyScroll: _propTypes["default"].func,
    getNode: _propTypes["default"].func
  });

  (0, _util2.statics)(FixedTable, BaseComponent);
  return FixedTable;
}