"use strict";

var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.TableDOMHelper = void 0;
var _slice = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/slice"));
var _from = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/array/from"));
var _concat = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/concat"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/createClass"));
var _styles = require("../styles");
// 表格 DOM 结构
// div.art-table-wrapper
// └── div.art-loading-wrapper
//     ├── div.art-loading-indicator-wrapper
//     │   └── div.art-loading-indicator
//     │
//     └── div.art-loading-content-wrapper
//         ├── div.art-table
//         │   │
//         │   ├── div.art-table-header
//         │   │  └── table
//         │   │      ├── colgroup
//         │   │      └── thead  注意这里会出现自定义内容，可能存在嵌套表格
//         │   │
//         │   ├── div.art-table-body
//         │   │   ├── div.art-virtual-blank.top
//         │   │   ├── table
//         │   │   │   ├── colgroup
//         │   │   │   └── tbody 注意这里会出现自定义内容，可能存在嵌套表格
//         │   │   └── div.art-virtual-blank.bottom
//         │   │
//         │   ├── div.art-table-footer
//         │   │  └── table
//         │   │      ├── colgroup
//         │   │      └── tfoot  注意这里会出现自定义内容，可能存在嵌套表格
//         │   │
//         │   ├── div.art-lock-shadow-mask
//         │   │   └── div.art-left-lock-shadow
//         │   └── div.art-lock-shadow-mask
//         │       └── div.art-right-lock-shadow
//         │
//         └── div.art-sticky-scroll
//             └── div.art-sticky-scroll-item
//
// 在「可能存在嵌套表格」的情况下，我们可以采用以下的方式来避免「querySelector 不小心获取到了的嵌套表格上的元素」：
//  artTable.querySelector('.art-lock-shadow-mask .art-left-lock-shadow')
// 表格 DOM 结构辅助工具
var TableDOMHelper = exports.TableDOMHelper = /*#__PURE__*/function () {
  function TableDOMHelper(artTableWrapper) {
    var _this = this,
      _context2,
      _context3;
    (0, _classCallCheck2.default)(this, TableDOMHelper);
    this.getRowNodeListByEvent = function (e) {
      var nodeList = null;
      var rowIndex = e.currentTarget.dataset.rowindex;
      if (rowIndex !== undefined) {
        var targetParent = _this.tableBody.contains(e.currentTarget) ? _this.tableBody : _this.tableFooter;
        nodeList = targetParent.querySelectorAll("tr[data-rowindex=\"".concat(rowIndex, "\"]"));
      }
      return nodeList;
    };
    this.getInRangeRowByCellEvent = function (e) {
      var _context;
      var _a;
      var _getParentNode = function getParentNode(ele, target) {
        if (ele.parentNode.nodeName === target) {
          return ele.parentNode;
        }
        return _getParentNode(ele.parentNode, target);
      };
      e = e instanceof Array ? e[0] : e;
      var curCell = e === null || e === void 0 ? void 0 : e.currentTarget;
      var curRow = _getParentNode(curCell, 'TR');
      var curRowSpan = parseInt(e.currentTarget.getAttribute('rowspan')) || 1;
      var rows = (_a = _getParentNode(curCell, 'TABLE')) === null || _a === void 0 ? void 0 : _a.rows;
      return (0, _slice.default)(_context = (0, _from.default)(rows)).call(_context, curRow.rowIndex, curRow.rowIndex + curRowSpan);
    };
    this.artTableWrapper = artTableWrapper;
    this.artTable = artTableWrapper.querySelector(".".concat(_styles.Classes.artTable));
    this.tableHeader = this.artTable.querySelector(".".concat(_styles.Classes.tableHeader));
    this.tableHeaderMain = this.artTable.querySelector(".".concat(_styles.Classes.tableHeaderMain));
    this.tableBody = this.artTable.querySelector(".".concat(_styles.Classes.tableBody));
    this.virtual = this.artTable.querySelector(".".concat(_styles.Classes.virtual));
    this.tableElement = this.artTable.querySelector(".".concat(_styles.Classes.tableBody, " table"));
    this.tableFooter = this.artTable.querySelector(".".concat(_styles.Classes.tableFooter));
    this.tableFooterMain = this.artTable.querySelector(".".concat(_styles.Classes.tableFooterMain));
    var stickyScrollSelector = (0, _concat.default)(_context2 = (0, _concat.default)(_context3 = ".".concat(_styles.Classes.artTable, " + .")).call(_context3, _styles.Classes.horizontalStickyScrollContainer, " .")).call(_context2, _styles.Classes.stickyScroll);
    var stickyScrolls = artTableWrapper.querySelectorAll(stickyScrollSelector);
    this.stickyScroll = stickyScrolls[stickyScrolls.length - 1]; // 当嵌套多层表格时，需要查找最后一个，否则会查找到父表格内嵌套的子表格的
    this.stickyScrollItem = this.stickyScroll.querySelector(".".concat(_styles.Classes.stickyScrollItem));
  }
  return (0, _createClass2.default)(TableDOMHelper, [{
    key: "getVirtualTop",
    value: function getVirtualTop() {
      return this.tableBody.querySelector(".".concat(_styles.Classes.virtualBlank, ".top"));
    }
  }, {
    key: "getTableRows",
    value: function getTableRows() {
      var _context4;
      var tbody = this.artTable.querySelector((0, _concat.default)(_context4 = ".".concat(_styles.Classes.tableBody, " .")).call(_context4, _styles.Classes.virtual, " table tbody"));
      return tbody.childNodes;
    }
  }, {
    key: "getTableBodyHtmlTable",
    value: function getTableBodyHtmlTable() {
      var _context5;
      return this.artTable.querySelector((0, _concat.default)(_context5 = ".".concat(_styles.Classes.tableBody, " .")).call(_context5, _styles.Classes.virtual, " table"));
    }
  }, {
    key: "getLeftLockShadow",
    value: function getLeftLockShadow() {
      var _context6;
      var selector = (0, _concat.default)(_context6 = ".".concat(_styles.Classes.lockShadowMask, " .")).call(_context6, _styles.Classes.leftLockShadow);
      var allLeftLockShadow = this.artTable.querySelectorAll(selector);
      return allLeftLockShadow[allLeftLockShadow.length - 1]; // 当table-body、table-footer嵌套多层表格时，需要查找最后一个，否则会查找到嵌套表格的
    }
  }, {
    key: "getRightLockShadow",
    value: function getRightLockShadow() {
      var _context7;
      var selector = (0, _concat.default)(_context7 = ".".concat(_styles.Classes.lockShadowMask, " .")).call(_context7, _styles.Classes.rightLockShadow);
      var allRightLockShadow = this.artTable.querySelectorAll(selector);
      return allRightLockShadow[allRightLockShadow.length - 1]; // 当table-body、table-footer嵌套多层表格时，需要查找最后一个，否则会查找到嵌套表格的
    }
  }, {
    key: "getLoadingIndicator",
    value: function getLoadingIndicator() {
      return this.artTableWrapper.querySelector(".".concat(_styles.Classes.loadingIndicator));
    }
  }, {
    key: "getRowTop",
    value: function getRowTop(rowIndex) {
      var _context8, _context9;
      if (rowIndex === 0) return 0;
      var selector = (0, _concat.default)(_context8 = (0, _concat.default)(_context9 = ".".concat(_styles.Classes.tableBody, " .")).call(_context9, _styles.Classes.tableRow, "[data-rowindex=\"")).call(_context8, rowIndex, "\"]");
      var row = this.artTable.querySelector(selector);
      var rowOffsetTop = row && row.offsetTop || 0;
      var tableOffsetTop = this.tableElement.offsetTop || 0;
      return rowOffsetTop + tableOffsetTop;
    }
  }]);
}();