'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray');

var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _TableRow = require('./TableRow');

var _TableRow2 = _interopRequireDefault(_TableRow);

var _TableHeader = require('./TableHeader');

var _TableHeader2 = _interopRequireDefault(_TableHeader);

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

var _shallowequal = require('shallowequal');

var _shallowequal2 = _interopRequireDefault(_shallowequal);

var _addEventListener = require('rc-util/lib/Dom/addEventListener');

var _addEventListener2 = _interopRequireDefault(_addEventListener);

var _ColumnManager = require('./ColumnManager');

var _ColumnManager2 = _interopRequireDefault(_ColumnManager);

var _createStore = require('./createStore');

var _createStore2 = _interopRequireDefault(_createStore);

var _componentClasses = require('component-classes');

var _componentClasses2 = _interopRequireDefault(_componentClasses);

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

var Table = function (_React$Component) {
  (0, _inherits3['default'])(Table, _React$Component);

  function Table(props) {
    (0, _classCallCheck3['default'])(this, Table);

    var _this = (0, _possibleConstructorReturn3['default'])(this, (Table.__proto__ || Object.getPrototypeOf(Table)).call(this, props));

    _this.onExpanded = function (expanded, record, e, index) {
      if (e) {
        e.preventDefault();
        e.stopPropagation();
      }
      var info = _this.findExpandedRow(record);
      if (typeof info !== 'undefined' && !expanded) {
        _this.onRowDestroy(record, index);
      } else if (!info && expanded) {
        var expandedRows = _this.getExpandedRows().concat();
        expandedRows.push(_this.getRowKey(record, index));
        _this.onExpandedRowsChange(expandedRows);
      }
      _this.props.onExpand(expanded, record);
    };

    _this.onRowDestroy = function (record, rowIndex) {
      var expandedRows = _this.getExpandedRows().concat();
      var rowKey = _this.getRowKey(record, rowIndex);
      var index = -1;
      expandedRows.forEach(function (r, i) {
        if (r === rowKey) {
          index = i;
        }
      });
      if (index !== -1) {
        expandedRows.splice(index, 1);
      }
      _this.onExpandedRowsChange(expandedRows);
    };

    _this.handleWindowResize = function () {
      _this.syncFixedTableRowHeight();
      _this.setScrollPositionClassName();
    };

    _this.syncFixedTableRowHeight = function () {
      var tableRect = _this.tableNode.getBoundingClientRect();
      // If tableNode's height less than 0, suppose it is hidden and don't recalculate rowHeight.
      // see: https://github.com/ant-design/ant-design/issues/4836
      if (tableRect.height !== undefined && tableRect.height <= 0) {
        return;
      }
      var prefixCls = _this.props.prefixCls;

      var headRows = _this.refs.headTable ? _this.refs.headTable.querySelectorAll('thead') : _this.refs.bodyTable.querySelectorAll('thead');
      var bodyRows = _this.refs.bodyTable.querySelectorAll('.' + prefixCls + '-row') || [];
      var fixedColumnsHeadRowsHeight = [].map.call(headRows, function (row) {
        return row.getBoundingClientRect().height || 'auto';
      });
      var fixedColumnsBodyRowsHeight = [].map.call(bodyRows, function (row) {
        return row.getBoundingClientRect().height || 'auto';
      });
      if ((0, _shallowequal2['default'])(_this.state.fixedColumnsHeadRowsHeight, fixedColumnsHeadRowsHeight) && (0, _shallowequal2['default'])(_this.state.fixedColumnsBodyRowsHeight, fixedColumnsBodyRowsHeight)) {
        return;
      }
      _this.setState({
        fixedColumnsHeadRowsHeight: fixedColumnsHeadRowsHeight,
        fixedColumnsBodyRowsHeight: fixedColumnsBodyRowsHeight
      });
    };

    _this.handleBodyScrollLeft = function (e) {
      // Fix https://github.com/ant-design/ant-design/issues/7635
      if (e.currentTarget !== e.target) {
        return;
      }
      var target = e.target;
      var _this$props$scroll = _this.props.scroll,
          scroll = _this$props$scroll === undefined ? {} : _this$props$scroll;
      var _this$refs = _this.refs,
          headTable = _this$refs.headTable,
          bodyTable = _this$refs.bodyTable;

      if (target.scrollLeft !== _this.lastScrollLeft && scroll.x) {
        if (target === bodyTable && headTable) {
          headTable.scrollLeft = target.scrollLeft;
        } else if (target === headTable && bodyTable) {
          bodyTable.scrollLeft = target.scrollLeft;
        }
        _this.setScrollPositionClassName();
      }
      // Remember last scrollLeft for scroll direction detecting.
      _this.lastScrollLeft = target.scrollLeft;
    };

    _this.handleBodyScrollTop = function (e) {
      var target = e.target;
      var _this$props$scroll2 = _this.props.scroll,
          scroll = _this$props$scroll2 === undefined ? {} : _this$props$scroll2;
      var _this$refs2 = _this.refs,
          headTable = _this$refs2.headTable,
          bodyTable = _this$refs2.bodyTable,
          fixedColumnsBodyLeft = _this$refs2.fixedColumnsBodyLeft,
          fixedColumnsBodyRight = _this$refs2.fixedColumnsBodyRight;

      if (target.scrollTop !== _this.lastScrollTop && scroll.y && target !== headTable) {
        var scrollTop = target.scrollTop;
        if (fixedColumnsBodyLeft && target !== fixedColumnsBodyLeft) {
          fixedColumnsBodyLeft.scrollTop = scrollTop;
        }
        if (fixedColumnsBodyRight && target !== fixedColumnsBodyRight) {
          fixedColumnsBodyRight.scrollTop = scrollTop;
        }
        if (bodyTable && target !== bodyTable) {
          bodyTable.scrollTop = scrollTop;
        }
      }
      // Remember last scrollTop for scroll direction detecting.
      _this.lastScrollTop = target.scrollTop;
    };

    _this.handleBodyScroll = function (e) {
      _this.handleBodyScrollLeft(e);
      _this.handleBodyScrollTop(e);
    };

    _this.handleRowHover = function (isHover, key) {
      _this.store.setState({
        currentHoverKey: isHover ? key : null
      });
    };

    var expandedRowKeys = [];
    var rows = [].concat((0, _toConsumableArray3['default'])(props.data));
    _this.columnManager = new _ColumnManager2['default'](props.columns, props.children);
    _this.store = (0, _createStore2['default'])({
      currentHoverKey: null,
      expandedRowsHeight: {}
    });
    _this.setScrollPosition('left');

    if (props.defaultExpandAllRows) {
      for (var i = 0; i < rows.length; i++) {
        var row = rows[i];
        expandedRowKeys.push(_this.getRowKey(row, i));
        rows = rows.concat(row[props.childrenColumnName] || []);
      }
    } else {
      expandedRowKeys = props.expandedRowKeys || props.defaultExpandedRowKeys;
    }
    _this.state = {
      expandedRowKeys: expandedRowKeys,
      currentHoverKey: null,
      fixedColumnsHeadRowsHeight: [],
      fixedColumnsBodyRowsHeight: []
    };
    return _this;
  }

  (0, _createClass3['default'])(Table, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      if (this.columnManager.isAnyColumnsFixed()) {
        this.handleWindowResize();
        this.debouncedWindowResize = (0, _utils.debounce)(this.handleWindowResize, 150);
        this.resizeEvent = (0, _addEventListener2['default'])(window, 'resize', this.debouncedWindowResize);
      }
    }
  }, {
    key: 'componentWillReceiveProps',
    value: function componentWillReceiveProps(nextProps) {
      if ('expandedRowKeys' in nextProps) {
        this.setState({
          expandedRowKeys: nextProps.expandedRowKeys
        });
      }
      if (nextProps.columns && nextProps.columns !== this.props.columns) {
        this.columnManager.reset(nextProps.columns);
      } else if (nextProps.children !== this.props.children) {
        this.columnManager.reset(null, nextProps.children);
      }
    }
  }, {
    key: 'componentDidUpdate',
    value: function componentDidUpdate(prevProps) {
      if (this.columnManager.isAnyColumnsFixed()) {
        this.handleWindowResize();
      }
      // when table changes to empty, reset scrollLeft
      if (prevProps.data.length > 0 && this.props.data.length === 0 && this.hasScrollX()) {
        this.resetScrollX();
      }
    }
  }, {
    key: 'componentWillUnmount',
    value: function componentWillUnmount() {
      if (this.resizeEvent) {
        this.resizeEvent.remove();
      }
      if (this.debouncedWindowResize) {
        this.debouncedWindowResize.cancel();
      }
    }
  }, {
    key: 'onExpandedRowsChange',
    value: function onExpandedRowsChange(expandedRowKeys) {
      if (!this.props.expandedRowKeys) {
        this.setState({ expandedRowKeys: expandedRowKeys });
      }
      this.props.onExpandedRowsChange(expandedRowKeys);
    }
  }, {
    key: 'getRowKey',
    value: function getRowKey(record, index) {
      var rowKey = this.props.rowKey;
      var key = typeof rowKey === 'function' ? rowKey(record, index) : record[rowKey];
      (0, _utils.warningOnce)(key !== undefined, 'Each record in table should have a unique `key` prop,' + 'or set `rowKey` to an unique primary key.');
      return key === undefined ? index : key;
    }
  }, {
    key: 'getExpandedRows',
    value: function getExpandedRows() {
      return this.props.expandedRowKeys || this.state.expandedRowKeys;
    }
  }, {
    key: 'getHeader',
    value: function getHeader(columns, fixed) {
      var _props = this.props,
          showHeader = _props.showHeader,
          expandIconAsCell = _props.expandIconAsCell,
          prefixCls = _props.prefixCls;

      var rows = this.getHeaderRows(columns);

      if (expandIconAsCell && fixed !== 'right') {
        rows[0].unshift({
          key: 'rc-table-expandIconAsCell',
          className: prefixCls + '-expand-icon-th',
          title: '',
          rowSpan: rows.length
        });
      }

      var trStyle = fixed ? this.getHeaderRowStyle(columns, rows) : null;

      return showHeader ? _react2['default'].createElement(_TableHeader2['default'], {
        prefixCls: prefixCls,
        rows: rows,
        rowStyle: trStyle
      }) : null;
    }
  }, {
    key: 'getHeaderRows',
    value: function getHeaderRows(columns) {
      var _this2 = this;

      var currentRow = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
      var rows = arguments[2];

      rows = rows || [];
      rows[currentRow] = rows[currentRow] || [];

      columns.forEach(function (column) {
        if (column.rowSpan && rows.length < column.rowSpan) {
          while (rows.length < column.rowSpan) {
            rows.push([]);
          }
        }
        var cell = {
          key: column.key,
          className: column.className || '',
          children: column.title
        };
        if (column.children) {
          _this2.getHeaderRows(column.children, currentRow + 1, rows);
        }
        if ('colSpan' in column) {
          cell.colSpan = column.colSpan;
        }
        if ('rowSpan' in column) {
          cell.rowSpan = column.rowSpan;
        }
        if (cell.colSpan !== 0) {
          rows[currentRow].push(cell);
        }
      });
      return rows.filter(function (row) {
        return row.length > 0;
      });
    }
  }, {
    key: 'getExpandedRow',
    value: function getExpandedRow(key, content, visible, className, fixed) {
      var _props2 = this.props,
          prefixCls = _props2.prefixCls,
          expandIconAsCell = _props2.expandIconAsCell;

      var colCount = void 0;
      if (fixed === 'left') {
        colCount = this.columnManager.leftLeafColumns().length;
      } else if (fixed === 'right') {
        colCount = this.columnManager.rightLeafColumns().length;
      } else {
        colCount = this.columnManager.leafColumns().length;
      }
      var columns = [{
        key: 'extra-row',
        render: function render() {
          return {
            props: {
              colSpan: colCount
            },
            children: fixed !== 'right' ? content : '&nbsp;'
          };
        }
      }];
      if (expandIconAsCell && fixed !== 'right') {
        columns.unshift({
          key: 'expand-icon-placeholder',
          render: function render() {
            return null;
          }
        });
      }
      return _react2['default'].createElement(_TableRow2['default'], {
        columns: columns,
        visible: visible,
        className: className,
        key: key + '-extra-row',
        rowKey: key + '-extra-row',
        prefixCls: prefixCls + '-expanded-row',
        indent: 1,
        expandable: false,
        store: this.store,
        expandedRow: true,
        fixed: !!fixed
      });
    }
  }, {
    key: 'getRowsByData',
    value: function getRowsByData(originalData, visible, indent, columns, fixed) {
      var props = this.props;
      var childrenColumnName = props.childrenColumnName,
          expandedRowRender = props.expandedRowRender,
          expandRowByClick = props.expandRowByClick,
          rowClassName = props.rowClassName,
          rowRef = props.rowRef,
          expandedRowClassName = props.expandedRowClassName,
          onRowClick = props.onRowClick,
          onRowDoubleClick = props.onRowDoubleClick,
          onRowContextMenu = props.onRowContextMenu,
          onRowMouseEnter = props.onRowMouseEnter,
          onRowMouseLeave = props.onRowMouseLeave;
      var fixedColumnsBodyRowsHeight = this.state.fixedColumnsBodyRowsHeight;

      var rst = [];
      var needIndentSpaced = props.data.some(function (record) {
        return record[childrenColumnName];
      });

      var expandIconAsCell = fixed !== 'right' ? props.expandIconAsCell : false;
      var expandIconColumnIndex = fixed !== 'right' ? props.expandIconColumnIndex : -1;
      var data = originalData;
      for (var i = 0; i < data.length; i++) {
        var record = data[i];
        var key = this.getRowKey(record, i);
        var childrenColumn = record[childrenColumnName];
        var isRowExpanded = this.isRowExpanded(record, i);
        var expandedRowContent = void 0;
        if (expandedRowRender && isRowExpanded) {
          expandedRowContent = expandedRowRender(record, i, indent);
        }
        var className = rowClassName(record, i, indent);

        var onHoverProps = {};
        if (this.columnManager.isAnyColumnsFixed()) {
          onHoverProps.onHover = this.handleRowHover;
        }

        var height = fixed && fixedColumnsBodyRowsHeight[i] ? fixedColumnsBodyRowsHeight[i] : null;

        var leafColumns = void 0;
        if (fixed === 'left') {
          leafColumns = this.columnManager.leftLeafColumns();
        } else if (fixed === 'right') {
          leafColumns = this.columnManager.rightLeafColumns();
        } else {
          leafColumns = this.columnManager.leafColumns();
        }

        rst.push(_react2['default'].createElement(_TableRow2['default'], (0, _extends3['default'])({
          indent: indent,
          indentSize: props.indentSize,
          needIndentSpaced: needIndentSpaced,
          className: className,
          record: record,
          expandIconAsCell: expandIconAsCell,
          onDestroy: this.onRowDestroy,
          index: i,
          visible: visible,
          expandRowByClick: expandRowByClick,
          onExpand: this.onExpanded,
          expandable: childrenColumn || expandedRowRender,
          expanded: isRowExpanded,
          prefixCls: props.prefixCls + '-row',
          childrenColumnName: childrenColumnName,
          columns: leafColumns,
          expandIconColumnIndex: expandIconColumnIndex,
          onRowClick: onRowClick,
          onRowDoubleClick: onRowDoubleClick,
          onRowContextMenu: onRowContextMenu,
          onRowMouseEnter: onRowMouseEnter,
          onRowMouseLeave: onRowMouseLeave,
          height: height
        }, onHoverProps, {
          key: key,
          hoverKey: key,
          ref: rowRef(record, i, indent),
          store: this.store
        })));

        var subVisible = visible && isRowExpanded;

        if (expandedRowContent && isRowExpanded) {
          rst.push(this.getExpandedRow(key, expandedRowContent, subVisible, expandedRowClassName(record, i, indent), fixed));
        }
        if (childrenColumn) {
          rst = rst.concat(this.getRowsByData(childrenColumn, subVisible, indent + 1, columns, fixed));
        }
      }
      return rst;
    }
  }, {
    key: 'getRows',
    value: function getRows(columns, fixed) {
      return this.getRowsByData(this.props.data, true, 0, columns, fixed);
    }
  }, {
    key: 'getColGroup',
    value: function getColGroup(columns, fixed) {
      var cols = [];
      if (this.props.expandIconAsCell && fixed !== 'right') {
        cols.push(_react2['default'].createElement('col', {
          className: this.props.prefixCls + '-expand-icon-col',
          key: 'rc-table-expand-icon-col'
        }));
      }
      var leafColumns = void 0;
      if (fixed === 'left') {
        leafColumns = this.columnManager.leftLeafColumns();
      } else if (fixed === 'right') {
        leafColumns = this.columnManager.rightLeafColumns();
      } else {
        leafColumns = this.columnManager.leafColumns();
      }
      cols = cols.concat(leafColumns.map(function (c) {
        return _react2['default'].createElement('col', { key: c.key || c.dataIndex, style: { width: c.width, minWidth: c.width } });
      }));
      return _react2['default'].createElement(
        'colgroup',
        null,
        cols
      );
    }
  }, {
    key: 'getLeftFixedTable',
    value: function getLeftFixedTable() {
      return this.getTable({
        columns: this.columnManager.leftColumns(),
        fixed: 'left'
      });
    }
  }, {
    key: 'getRightFixedTable',
    value: function getRightFixedTable() {
      return this.getTable({
        columns: this.columnManager.rightColumns(),
        fixed: 'right'
      });
    }
  }, {
    key: 'getTable',
    value: function getTable() {
      var _this3 = this;

      var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var columns = options.columns,
          fixed = options.fixed;
      var _props3 = this.props,
          prefixCls = _props3.prefixCls,
          _props3$scroll = _props3.scroll,
          scroll = _props3$scroll === undefined ? {} : _props3$scroll,
          getBodyWrapper = _props3.getBodyWrapper,
          showHeader = _props3.showHeader;
      var useFixedHeader = this.props.useFixedHeader;

      var bodyStyle = (0, _extends3['default'])({}, this.props.bodyStyle);
      var headStyle = {};

      var tableClassName = '';
      if (scroll.x || fixed) {
        tableClassName = prefixCls + '-fixed';
        bodyStyle.overflowX = bodyStyle.overflowX || 'auto';
        // Fix weired webkit render bug
        // https://github.com/ant-design/ant-design/issues/7783
        bodyStyle.WebkitTransform = 'translate3d (0, 0, 0)';
      }

      var innerBodyStyle = {};
      if (scroll.y) {
        // maxHeight will make fixed-Table scrolling not working
        // so we only set maxHeight to body-Table here
        if (fixed) {
          innerBodyStyle.maxHeight = bodyStyle.maxHeight || scroll.y;
          innerBodyStyle.overflowY = bodyStyle.overflowY || 'scroll';
        } else {
          bodyStyle.maxHeight = bodyStyle.maxHeight || scroll.y;
        }
        bodyStyle.overflowY = bodyStyle.overflowY || 'scroll';
        useFixedHeader = true;

        // Add negative margin bottom for scroll bar overflow bug
        var scrollbarWidth = (0, _utils.measureScrollbar)();
        if (scrollbarWidth > 0) {
          (fixed ? bodyStyle : headStyle).marginBottom = '-' + scrollbarWidth + 'px';
          (fixed ? bodyStyle : headStyle).paddingBottom = '0px';
        }
      }

      var renderTable = function renderTable() {
        var hasHead = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
        var hasBody = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;

        var tableStyle = {};
        if (!fixed && scroll.x) {
          // not set width, then use content fixed width
          if (scroll.x === true) {
            tableStyle.tableLayout = 'fixed';
          } else {
            tableStyle.width = scroll.x;
          }
        }
        var tableBody = hasBody ? getBodyWrapper(_react2['default'].createElement(
          'tbody',
          { className: prefixCls + '-tbody' },
          _this3.getRows(columns, fixed)
        )) : null;
        return _react2['default'].createElement(
          'table',
          { className: tableClassName, style: tableStyle, key: 'table' },
          _this3.getColGroup(columns, fixed),
          hasHead ? _this3.getHeader(columns, fixed) : null,
          tableBody
        );
      };

      var headTable = void 0;

      if (useFixedHeader && showHeader) {
        headTable = _react2['default'].createElement(
          'div',
          {
            key: 'headTable',
            className: prefixCls + '-header',
            ref: fixed ? null : 'headTable',
            style: headStyle,
            onScroll: this.handleBodyScrollLeft
          },
          renderTable(true, false)
        );
      }

      var bodyTable = _react2['default'].createElement(
        'div',
        {
          key: 'bodyTable',
          className: prefixCls + '-body',
          style: bodyStyle,
          ref: 'bodyTable',
          onScroll: this.handleBodyScroll
        },
        renderTable(!useFixedHeader)
      );

      if (fixed && columns.length) {
        var refName = void 0;
        if (columns[0].fixed === 'left' || columns[0].fixed === true) {
          refName = 'fixedColumnsBodyLeft';
        } else if (columns[0].fixed === 'right') {
          refName = 'fixedColumnsBodyRight';
        }
        delete bodyStyle.overflowX;
        delete bodyStyle.overflowY;
        bodyTable = _react2['default'].createElement(
          'div',
          {
            key: 'bodyTable',
            className: prefixCls + '-body-outer',
            style: (0, _extends3['default'])({}, bodyStyle)
          },
          _react2['default'].createElement(
            'div',
            {
              className: prefixCls + '-body-inner',
              style: innerBodyStyle,
              ref: refName,
              onScroll: this.handleBodyScroll
            },
            renderTable(!useFixedHeader)
          )
        );
      }
      return [headTable, bodyTable];
    }
  }, {
    key: 'getTitle',
    value: function getTitle() {
      var _props4 = this.props,
          title = _props4.title,
          prefixCls = _props4.prefixCls;

      return title ? _react2['default'].createElement(
        'div',
        { className: prefixCls + '-title', key: 'title' },
        title(this.props.data)
      ) : null;
    }
  }, {
    key: 'getFooter',
    value: function getFooter() {
      var _props5 = this.props,
          footer = _props5.footer,
          prefixCls = _props5.prefixCls;

      return footer ? _react2['default'].createElement(
        'div',
        { className: prefixCls + '-footer', key: 'footer' },
        footer(this.props.data)
      ) : null;
    }
  }, {
    key: 'getEmptyText',
    value: function getEmptyText() {
      var _props6 = this.props,
          emptyText = _props6.emptyText,
          prefixCls = _props6.prefixCls,
          data = _props6.data;

      if (data.length) {
        return null;
      }
      var emptyClassName = prefixCls + '-placeholder';
      return _react2['default'].createElement(
        'div',
        { className: emptyClassName, key: 'emptyText' },
        typeof emptyText === 'function' ? emptyText() : emptyText
      );
    }
  }, {
    key: 'getHeaderRowStyle',
    value: function getHeaderRowStyle(columns, rows) {
      var fixedColumnsHeadRowsHeight = this.state.fixedColumnsHeadRowsHeight;

      var headerHeight = fixedColumnsHeadRowsHeight[0];
      if (headerHeight && columns) {
        if (headerHeight === 'auto') {
          return { height: 'auto' };
        }
        return { height: headerHeight / rows.length };
      }
      return null;
    }
  }, {
    key: 'setScrollPosition',
    value: function setScrollPosition(position) {
      this.scrollPosition = position;
      if (this.tableNode) {
        var prefixCls = this.props.prefixCls;

        if (position === 'both') {
          (0, _componentClasses2['default'])(this.tableNode).remove(new RegExp('^' + prefixCls + '-scroll-position-.+$')).add(prefixCls + '-scroll-position-left').add(prefixCls + '-scroll-position-right');
        } else {
          (0, _componentClasses2['default'])(this.tableNode).remove(new RegExp('^' + prefixCls + '-scroll-position-.+$')).add(prefixCls + '-scroll-position-' + position);
        }
      }
    }
  }, {
    key: 'setScrollPositionClassName',
    value: function setScrollPositionClassName() {
      var node = this.refs.bodyTable;
      var scrollToLeft = node.scrollLeft === 0;
      var scrollToRight = node.scrollLeft + 1 >= node.children[0].getBoundingClientRect().width - node.getBoundingClientRect().width;
      if (scrollToLeft && scrollToRight) {
        this.setScrollPosition('both');
      } else if (scrollToLeft) {
        this.setScrollPosition('left');
      } else if (scrollToRight) {
        this.setScrollPosition('right');
      } else if (this.scrollPosition !== 'middle') {
        this.setScrollPosition('middle');
      }
    }
  }, {
    key: 'resetScrollX',
    value: function resetScrollX() {
      if (this.refs.headTable) {
        this.refs.headTable.scrollLeft = 0;
      }
      if (this.refs.bodyTable) {
        this.refs.bodyTable.scrollLeft = 0;
      }
    }
  }, {
    key: 'findExpandedRow',
    value: function findExpandedRow(record, index) {
      var _this4 = this;

      var rows = this.getExpandedRows().filter(function (i) {
        return i === _this4.getRowKey(record, index);
      });
      return rows[0];
    }
  }, {
    key: 'isRowExpanded',
    value: function isRowExpanded(record, index) {
      return typeof this.findExpandedRow(record, index) !== 'undefined';
    }
  }, {
    key: 'hasScrollX',
    value: function hasScrollX() {
      var _props$scroll = this.props.scroll,
          scroll = _props$scroll === undefined ? {} : _props$scroll;

      return 'x' in scroll;
    }
  }, {
    key: 'render',
    value: function render() {
      var _this5 = this;

      var props = this.props;
      var prefixCls = props.prefixCls;

      var className = props.prefixCls;
      if (props.className) {
        className += ' ' + props.className;
      }
      if (props.useFixedHeader || props.scroll && props.scroll.y) {
        className += ' ' + prefixCls + '-fixed-header';
      }
      if (this.scrollPosition === 'both') {
        className += ' ' + prefixCls + '-scroll-position-left ' + prefixCls + '-scroll-position-right';
      } else {
        className += ' ' + prefixCls + '-scroll-position-' + this.scrollPosition;
      }

      var isTableScroll = this.columnManager.isAnyColumnsFixed() || props.scroll.x || props.scroll.y;

      var content = [this.getTable({ columns: this.columnManager.groupedColumns() }), this.getEmptyText(), this.getFooter()];

      var scrollTable = isTableScroll ? _react2['default'].createElement(
        'div',
        { className: prefixCls + '-scroll' },
        content
      ) : content;

      return _react2['default'].createElement(
        'div',
        { ref: function ref(node) {
            return _this5.tableNode = node;
          }, className: className, style: props.style },
        this.getTitle(),
        _react2['default'].createElement(
          'div',
          { className: prefixCls + '-content' },
          scrollTable,
          this.columnManager.isAnyColumnsLeftFixed() && _react2['default'].createElement(
            'div',
            { className: prefixCls + '-fixed-left' },
            this.getLeftFixedTable()
          ),
          this.columnManager.isAnyColumnsRightFixed() && _react2['default'].createElement(
            'div',
            { className: prefixCls + '-fixed-right' },
            this.getRightFixedTable()
          )
        )
      );
    }
  }]);
  return Table;
}(_react2['default'].Component);

Table.propTypes = {
  data: _propTypes2['default'].array,
  expandIconAsCell: _propTypes2['default'].bool,
  defaultExpandAllRows: _propTypes2['default'].bool,
  expandedRowKeys: _propTypes2['default'].array,
  defaultExpandedRowKeys: _propTypes2['default'].array,
  useFixedHeader: _propTypes2['default'].bool,
  columns: _propTypes2['default'].array,
  prefixCls: _propTypes2['default'].string,
  bodyStyle: _propTypes2['default'].object,
  style: _propTypes2['default'].object,
  rowKey: _propTypes2['default'].oneOfType([_propTypes2['default'].string, _propTypes2['default'].func]),
  rowClassName: _propTypes2['default'].func,
  expandedRowClassName: _propTypes2['default'].func,
  childrenColumnName: _propTypes2['default'].string,
  onExpand: _propTypes2['default'].func,
  onExpandedRowsChange: _propTypes2['default'].func,
  indentSize: _propTypes2['default'].number,
  onRowClick: _propTypes2['default'].func,
  onRowDoubleClick: _propTypes2['default'].func,
  onRowContextMenu: _propTypes2['default'].func,
  onRowMouseEnter: _propTypes2['default'].func,
  onRowMouseLeave: _propTypes2['default'].func,
  expandIconColumnIndex: _propTypes2['default'].number,
  showHeader: _propTypes2['default'].bool,
  title: _propTypes2['default'].func,
  footer: _propTypes2['default'].func,
  emptyText: _propTypes2['default'].oneOfType([_propTypes2['default'].node, _propTypes2['default'].func]),
  scroll: _propTypes2['default'].object,
  rowRef: _propTypes2['default'].func,
  getBodyWrapper: _propTypes2['default'].func,
  children: _propTypes2['default'].node
};
Table.defaultProps = {
  data: [],
  useFixedHeader: false,
  expandIconAsCell: false,
  defaultExpandAllRows: false,
  defaultExpandedRowKeys: [],
  rowKey: 'key',
  rowClassName: function rowClassName() {
    return '';
  },
  expandedRowClassName: function expandedRowClassName() {
    return '';
  },
  onExpand: function onExpand() {},
  onExpandedRowsChange: function onExpandedRowsChange() {},
  onRowClick: function onRowClick() {},
  onRowDoubleClick: function onRowDoubleClick() {},
  onRowContextMenu: function onRowContextMenu() {},
  onRowMouseEnter: function onRowMouseEnter() {},
  onRowMouseLeave: function onRowMouseLeave() {},

  prefixCls: 'rc-table',
  bodyStyle: {},
  style: {},
  childrenColumnName: 'children',
  indentSize: 15,
  expandIconColumnIndex: 0,
  showHeader: true,
  scroll: {},
  rowRef: function rowRef() {
    return null;
  },
  getBodyWrapper: function getBodyWrapper(body) {
    return body;
  },
  emptyText: function emptyText() {
    return 'No Data';
  }
};
exports['default'] = Table;
module.exports = exports['default'];