UNPKG

@txdfe/at

Version:

一个设计体系组件库

274 lines (273 loc) 12.4 kB
"use strict"; function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _react = _interopRequireDefault(require("react")); var _guid = _interopRequireDefault(require("../../util/guid")); var _reactDom = require("react-dom"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); } function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); } function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); } function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); } function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } var groupBy = function groupBy(array, field) { var id; var result = Object.create(null); if (typeof field === 'string') { var _iterator = _createForOfIteratorHelper(array), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var item = _step.value; id = String(item[field]); if (!result[id]) { result[id] = [item]; } else { result[id].push(item); } } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } } else if (typeof field === 'function') { var _iterator2 = _createForOfIteratorHelper(array), _step2; try { for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) { var _item = _step2.value; id = String(field(_item)); if (!result[id]) { result[id] = [_item]; } else { result[id].push(_item); } } } catch (err) { _iterator2.e(err); } finally { _iterator2.f(); } } return result; }; var getBestWidth = function getBestWidth(_ref) { var headerWidth = _ref.headerWidth, bodyWidths = _ref.bodyWidths; var sizeGroup = groupBy(bodyWidths, function (size) { var groupIndex = (size / 10).toFixed(); return groupIndex; }); var values = Object.values(sizeGroup).sort(function (sizesA, sizesB) { return sizesB.length - sizesA.length; })[0]; // const value = values.reduce( ( pv, cv, i ) => ( ( pv * i + cv ) / ( i + 1 ) ), 0 ); var value = Math.max.apply(null, values); return Math.max(headerWidth, value); }; var AutoWidth = exports["default"] = /*#__PURE__*/function () { function AutoWidth() { var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, _ref2$padding = _ref2.padding, padding = _ref2$padding === void 0 ? null : _ref2$padding, table = _ref2.table; _classCallCheck(this, AutoWidth); _defineProperty(this, "padding", 15); _defineProperty(this, "enable", true); _defineProperty(this, "columns", Object.create(null)); _defineProperty(this, "Calculators", Object.create(null)); _defineProperty(this, "colGroups", []); if (typeof padding === 'number') { this.padding = padding; } this.table = table; } return _createClass(AutoWidth, [{ key: "init", value: function init() { this.columns = Object.create(null); this.Calculators = Object.create(null); this.colGroups.length = 0; } }, { key: "analyzeColDefine", value: function analyzeColDefine(column) { if (column && column.width === 'auto') { this.columns[column.columnId] = { calced: false, headerCalculator: null, headerWidth: null, bodyCalculators: [], bodyWidths: [], bestWidth: null, isMax: false }; } } }, { key: "bindColGroup", value: function bindColGroup(colGroup) { if (!this.enable) { return; } var colGroups = this.colGroups; if (!colGroups.includes(colGroup)) { colGroups.push(colGroup); } } }, { key: "bindCalculator", value: function bindCalculator(id, target) { this.Calculators[id] = target; } }, { key: "renderCalculator", value: function renderCalculator(columnId, isInHeader, content, children) { if (!this.enable) { return null; } var column = this.columns[columnId]; if (column) { var calculatorId = (0, _guid["default"])('aw-'); if (isInHeader) { column.headerCalculator = calculatorId; } else { column.bodyCalculators.push(calculatorId); } return /*#__PURE__*/_react["default"].createElement(Calculator, { id: calculatorId, AutoWidthInstance: this }, content, children); } else { return null; } } }, { key: "calcAndUpdate", value: function calcAndUpdate() { var _this = this; var colGroups = this.colGroups, columns = this.columns; var columnIds = Object.keys(columns); var maxValue = 0; var maxValueId = null; for (var _i = 0, _columnIds = columnIds; _i < _columnIds.length; _i++) { var id = _columnIds[_i]; var column = columns[id]; var headerCalculator = column.headerCalculator, bodyCalculators = column.bodyCalculators; if (headerCalculator) { column.headerWidth = this.Calculators[headerCalculator].getWidth(); } if (bodyCalculators.length) { column.bodyWidths = bodyCalculators.map(function (i) { return _this.Calculators[i].getWidth(); }); } if (column.bodyWidths.length) { column.bestWidth = getBestWidth(column); if (column.bestWidth > maxValue) { maxValue = column.bestWidth; maxValueId = id; } } } if (maxValueId) { var _column = columns[maxValueId]; _column.bestWidth = null; } var _iterator3 = _createForOfIteratorHelper(colGroups), _step3; try { for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) { var colGroup = _step3.value; colGroup.forceUpdate(); } } catch (err) { _iterator3.e(err); } finally { _iterator3.f(); } } }, { key: "getBestWidth", value: function getBestWidth(columnId) { var column = this.columns[columnId]; if (column && column.bestWidth) { return column.bestWidth + this.padding * 2; } else { return null; } } }]); }(); var Calculator = /*#__PURE__*/function (_React$Component) { function Calculator(props) { var _this2; _classCallCheck(this, Calculator); _this2 = _callSuper(this, Calculator, [props]); var _this2$props = _this2.props, AutoWidthInstance = _this2$props.AutoWidthInstance, id = _this2$props.id; AutoWidthInstance.bindCalculator(id, _this2); return _this2; } _inherits(Calculator, _React$Component); return _createClass(Calculator, [{ key: "componentDidUpdate", value: function componentDidUpdate() { var _this$props = this.props, AutoWidthInstance = _this$props.AutoWidthInstance, id = _this$props.id; AutoWidthInstance.bindCalculator(id, this); var container = (0, _reactDom.findDOMNode)(this); container.style.display = 'block'; } }, { key: "getWidth", value: function getWidth() { var container = (0, _reactDom.findDOMNode)(this); var content = container.querySelector('.auto-width-calculator-content'); var _content$getBoundingC = content.getBoundingClientRect(), width = _content$getBoundingC.width; container.style.display = 'none'; return width; } }, { key: "render", value: function render() { var _this$props2 = this.props, content = _this$props2.content, children = _this$props2.children; return /*#__PURE__*/_react["default"].createElement("div", { style: { cssText: 'position: relative; opacity: 0; pointer-events: none' } }, /*#__PURE__*/_react["default"].createElement("div", { style: { cssText: 'position: absolute; width: 2000px' } }, /*#__PURE__*/_react["default"].createElement("div", { className: "auto-width-calculator-content", style: { cssText: 'position: absolute' } }, content, children))); } }]); }(_react["default"].Component);