UNPKG

12.7 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _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; };
8
9var _react = require('react');
10
11var _react2 = _interopRequireDefault(_react);
12
13var _Column = require('./Column');
14
15var _Column2 = _interopRequireDefault(_Column);
16
17var _ColumnGroup = require('./ColumnGroup');
18
19var _ColumnGroup2 = _interopRequireDefault(_ColumnGroup);
20
21var _beeIcon = require('bee-icon');
22
23var _beeIcon2 = _interopRequireDefault(_beeIcon);
24
25function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
26
27function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
28
29function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
30
31//行控制管理
32var ColumnManager = function () {
33 function ColumnManager(columns, elements, originWidth, rowDraggAble, showRowNum) {
34 _classCallCheck(this, ColumnManager);
35
36 _initialiseProps.call(this);
37
38 columns = this.addDragHandleColumn(columns, rowDraggAble);
39 columns = this.addOrderColumn(columns, showRowNum);
40 columns = this.deleteColumnNotShow(columns);
41 this.columns = columns || this.normalize(elements);
42
43 this.originWidth = originWidth;
44 }
45
46 // 向数据列中添加一列:行拖拽标识
47
48
49 // delete the column which does not show
50
51
52 // 向数据列中添加一列:序号
53
54
55 ColumnManager.prototype.isAnyColumnsFixed = function isAnyColumnsFixed() {
56 var _this = this;
57
58 return this._cache('isAnyColumnsFixed', function () {
59 return _this.columns.some(function (column) {
60 return !!column.fixed;
61 });
62 });
63 };
64
65 ColumnManager.prototype.isAnyColumnsLeftFixed = function isAnyColumnsLeftFixed() {
66 var _this2 = this;
67
68 return this._cache('isAnyColumnsLeftFixed', function () {
69 return _this2.columns.some(function (column) {
70 return column.fixed === 'left' || column.fixed === true;
71 });
72 });
73 };
74
75 ColumnManager.prototype.isAnyColumnsRightFixed = function isAnyColumnsRightFixed() {
76 var _this3 = this;
77
78 return this._cache('isAnyColumnsRightFixed', function () {
79 return _this3.columns.some(function (column) {
80 return column.fixed === 'right';
81 });
82 });
83 };
84
85 ColumnManager.prototype.leftColumns = function leftColumns() {
86 var _this4 = this;
87
88 return this._cache('leftColumns', function () {
89 return _this4.groupedColumns().filter(function (column) {
90 return column.fixed === 'left' || column.fixed === true;
91 });
92 });
93 };
94
95 ColumnManager.prototype.rightColumns = function rightColumns() {
96 var _this5 = this;
97
98 return this._cache('rightColumns', function () {
99 return _this5.groupedColumns().filter(function (column) {
100 return column.fixed === 'right';
101 });
102 });
103 };
104
105 ColumnManager.prototype.centerColumns = function centerColumns() {
106 var _this6 = this;
107
108 return this._cache('centerColumns', function () {
109 return _this6.groupedColumns().filter(function (column) {
110 return !column.fixed;
111 });
112 });
113 };
114
115 ColumnManager.prototype.leafColumns = function leafColumns() {
116 var _this7 = this;
117
118 return this._cache('leafColumns', function () {
119 return _this7._leafColumns(_this7.columns);
120 });
121 };
122
123 ColumnManager.prototype.leftLeafColumns = function leftLeafColumns() {
124 var _this8 = this;
125
126 return this._cache('leftLeafColumns', function () {
127 return _this8._leafColumns(_this8.leftColumns());
128 });
129 };
130
131 ColumnManager.prototype.rightLeafColumns = function rightLeafColumns() {
132 var _this9 = this;
133
134 return this._cache('rightLeafColumns', function () {
135 return _this9._leafColumns(_this9.rightColumns());
136 });
137 };
138
139 ColumnManager.prototype.centerLeafColumns = function centerLeafColumns() {
140 var _this10 = this;
141
142 return this._cache('centerLeafColumns', function () {
143 return _this10._leafColumns(_this10.centerColumns());
144 });
145 };
146
147 // add appropriate rowspan and colspan to column
148
149
150 ColumnManager.prototype.groupedColumns = function groupedColumns(type) {
151 var _this11 = this;
152
153 return this._cache('groupedColumns', function () {
154 var _groupColumns = function _groupColumns(columns) {
155 var currentRow = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
156 var parentColumn = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
157 var rows = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
158
159 // track how many rows we got
160 rows[currentRow] = rows[currentRow] || [];
161 var grouped = [];
162 var setRowSpan = function setRowSpan(column) {
163 var rowSpan = rows.length - currentRow;
164 if (column && !column.children && // parent columns are supposed to be one row
165 rowSpan > 1 && (!column.rowSpan || column.rowSpan < rowSpan)) {
166 column.rowSpan = rowSpan;
167 }
168 };
169 columns.forEach(function (column, index) {
170 var defaultOpt = {
171 ifshow: true
172 };
173 if (!_this11.originWidth) {
174 defaultOpt.width = 200;
175 }
176 //获取非固定列
177 if (type == 'nofixed' && column.fixed) {
178 return false;
179 }
180 var newColumn = _extends({}, defaultOpt, column);
181 rows[currentRow].push(newColumn);
182 parentColumn.colSpan = parentColumn.colSpan || 0;
183 if (newColumn.children && newColumn.children.length > 0) {
184 newColumn.children = _groupColumns(newColumn.children, currentRow + 1, newColumn, rows);
185 parentColumn.colSpan = parentColumn.colSpan + newColumn.colSpan;
186 } else {
187 parentColumn.colSpan++;
188 }
189 // update rowspan to all same row columns
190 for (var i = 0; i < rows[currentRow].length - 1; ++i) {
191 setRowSpan(rows[currentRow][i]);
192 }
193 // last column, update rowspan immediately
194 if (index + 1 === columns.length) {
195 setRowSpan(newColumn);
196 }
197 grouped.push(newColumn);
198 });
199 return grouped;
200 };
201 return _groupColumns(_this11.columns);
202 });
203 };
204
205 ColumnManager.prototype.normalize = function normalize(elements) {
206 var _this12 = this;
207
208 var columns = [];
209 _react2["default"].Children.forEach(elements, function (element) {
210 if (!_this12.isColumnElement(element)) return;
211 var column = _extends({}, element.props);
212 if (element.key) {
213 column.key = element.key;
214 }
215 if (element.type === _ColumnGroup2["default"]) {
216 column.children = _this12.normalize(column.children);
217 }
218 columns.push(column);
219 });
220 return columns;
221 };
222
223 ColumnManager.prototype.isColumnElement = function isColumnElement(element) {
224 return element && (element.type === _Column2["default"] || element.type === _ColumnGroup2["default"]);
225 };
226
227 ColumnManager.prototype.reset = function reset(columns, elements, showRowNum, rowDraggAble) {
228 columns = this.addDragHandleColumn(columns, rowDraggAble);
229 columns = this.addOrderColumn(columns, showRowNum);
230 columns = this.deleteColumnNotShow(columns);
231 this.columns = columns || this.normalize(elements);
232 this._cached = {};
233 };
234
235 ColumnManager.prototype.getColumnWidth = function getColumnWidth(contentWidth) {
236 var columns = this.leafColumns();
237 var res = { computeWidth: 0, lastShowIndex: -1 };
238 columns.forEach(function (col, index) {
239 //如果列显示
240 if (col.ifshow) {
241 var width = col.width;
242 if (typeof width == 'string' && width.includes('%')) {
243 width = contentWidth * parseInt(col.width) / 100;
244 }
245 res.computeWidth += parseInt(width);
246 if (!col.fixed) {
247 res.lastShowIndex = index;
248 }
249 }
250 });
251 return res;
252 };
253
254 ColumnManager.prototype.getLeftColumnsWidth = function getLeftColumnsWidth() {
255 var _this13 = this;
256
257 var contentWidth = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
258
259 return this._cache('leftColumnsWidth', function () {
260 var leftColumnsWidth = 0;
261 _this13.groupedColumns().forEach(function (column) {
262 if (column.fixed === 'left' || column.fixed === true) {
263 var width = column.width;
264 if (typeof width == 'string' && width.includes('%')) {
265 width = contentWidth * parseInt(column.width) / 100;
266 }
267 leftColumnsWidth += parseInt(width);
268 }
269 });
270 return leftColumnsWidth;
271 });
272 };
273
274 ColumnManager.prototype.getRightColumnsWidth = function getRightColumnsWidth() {
275 var _this14 = this;
276
277 var contentWidth = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
278
279 return this._cache('rightColumnsWidth', function () {
280 var rightColumnsWidth = 0;
281 _this14.groupedColumns().forEach(function (column) {
282 if (column.fixed === 'right') {
283 var width = column.width;
284 if (typeof width == 'string' && width.includes('%')) {
285 width = contentWidth * parseInt(column.width) / 100;
286 }
287 rightColumnsWidth += parseInt(width);
288 }
289 });
290 return rightColumnsWidth;
291 });
292 };
293
294 ColumnManager.prototype._cache = function _cache(name, fn) {
295 if (name in this._cached) {
296 return this._cached[name];
297 }
298 this._cached[name] = fn();
299 return this._cached[name];
300 };
301
302 //todo 含有children的宽度计算
303
304
305 ColumnManager.prototype._leafColumns = function _leafColumns(columns) {
306 var _this15 = this;
307
308 var leafColumns = [];
309
310 columns.forEach(function (column) {
311 if (!column.children) {
312
313 var defaultOpt = {
314 ifshow: true
315 };
316 if (!_this15.originWidth) {
317 defaultOpt.width = 200;
318 }
319 var newColumn = _extends({}, defaultOpt, column);
320 leafColumns.push(newColumn);
321 } else {
322 leafColumns.push.apply(leafColumns, _toConsumableArray(_this15._leafColumns(column.children)));
323 }
324 });
325 return leafColumns;
326 };
327
328 return ColumnManager;
329}();
330
331var _initialiseProps = function _initialiseProps() {
332 this._cached = {};
333
334 this.addDragHandleColumn = function (columns, rowDraggAble) {
335 if (!rowDraggAble) {
336 return columns;
337 }
338 var dragHandleColumn = [{
339 className: "drag-handle-column",
340 title: "",
341 key: "dragHandle",
342 dataIndex: "dragHandle",
343 width: 49,
344 draggable: true,
345 render: function render() {
346 return _react2["default"].createElement(_beeIcon2["default"], { type: 'uf-navmenu' });
347 }
348 }];
349 columns = dragHandleColumn.concat(columns);
350 return columns;
351 };
352
353 this.deleteColumnNotShow = function (columns) {
354 var len = columns.length;
355 for (var i = 0; i < len; i++) {
356 if (columns && columns[i] && columns[i].isShow === false) {
357 columns.splice(i, 1);
358 i--;
359 }
360 }
361 return columns;
362 };
363
364 this.addOrderColumn = function (columns, showRowNum) {
365 if (!showRowNum) {
366 return columns;
367 }
368 var key = showRowNum.key,
369 fixed = showRowNum.fixed,
370 width = showRowNum.width,
371 name = showRowNum.name,
372 type = showRowNum.type,
373 base = showRowNum.base;
374
375 var order = {
376 dataIndex: key || '_index',
377 key: '_index',
378 fixed: fixed || 'left',
379 width: width || 50,
380 title: name || '序号',
381 render: function render(text, record, index) {
382 switch (type) {
383 case 'ascii':
384 {
385 return String.fromCharCode((base || 'a').charCodeAt() + index);
386 }
387 case 'number':
388 default:
389 {
390 return (base || 0) + index;
391 }
392 }
393 }
394 };
395 if (columns.length > 0 && columns[0].dataIndex !== 'checkbox' && columns[0].dataIndex !== 'radio') {
396 // 多选表格/单选表格时放在第二列,其他情况放到第一列
397 columns = [order].concat(columns);
398 } else {
399 columns.splice(1, 0, order); // splice方法改变原数组,返回切割出的数组,此处为[]
400 }
401 return columns;
402 };
403};
404
405exports["default"] = ColumnManager;
406module.exports = exports['default'];
\No newline at end of file