@txdfe/at
Version:
一个设计体系组件库
340 lines (274 loc) • 13.2 kB
JavaScript
;
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
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(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
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 } }); Object.defineProperty(subClass, "prototype", { writable: false }); 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 _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
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); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
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 _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, 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 normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
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 = /*#__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;
}
_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;
}
}
}]);
return AutoWidth;
}();
exports["default"] = AutoWidth;
var Calculator = /*#__PURE__*/function (_React$Component) {
_inherits(Calculator, _React$Component);
var _super = _createSuper(Calculator);
function Calculator(props) {
var _this2;
_classCallCheck(this, Calculator);
_this2 = _super.call(this, props);
var _this2$props = _this2.props,
AutoWidthInstance = _this2$props.AutoWidthInstance,
id = _this2$props.id;
AutoWidthInstance.bindCalculator(id, _assertThisInitialized(_this2));
return _this2;
}
_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)));
}
}]);
return Calculator;
}(_react["default"].Component);