ten-design-vue
Version:
ten-vue
165 lines (129 loc) • 5.17 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getColumnsTree = getColumnsTree;
exports.getColumnsTreeByProps = getColumnsTreeByProps;
exports.traverseTree = traverseTree;
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
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; }
var defaultProp = {
selector: {
width: '74'
},
expand: {
width: '78'
}
};
function getValidWidth(width) {
var validWidth = width;
if (typeof validWidth === 'string') {
validWidth = validWidth.replace(/[^\d]/g, '');
}
validWidth = parseFloat(validWidth);
if (isNaN(validWidth)) {
validWidth = undefined;
} else if (typeof width === 'string' && width.indexOf('%') > -1) {
validWidth = validWidth / 100;
}
return validWidth;
}
function createColumn(vnode, options) {
var column = _objectSpread(_objectSpread({
type: 'default',
sortable: false
}, defaultProp[vnode.componentOptions.propsData.type]), vnode.componentOptions.propsData);
column.validWidth = getValidWidth(column.width);
column.validMinWidth = getValidWidth(column.minWidth);
if (column.fixed === '' || column.fixed === true) {
column.fixed = 'left';
}
if (!column.fixed) delete column.fixed;
column.columnKey = column.columnKey || column.render || column.title;
if (!column.order && options.sortInfo.column.columnKey === column.columnKey) {
column.order = options.sortInfo.order || 'default';
} else if (!column.order) {
column.order = 'default';
}
if (vnode.componentOptions.propsData.sorter) {
column.sortable = true;
} // 兼容插槽的写法
var _scopedSlots = vnode.data.scopedSlots;
if (_scopedSlots) {
if (_scopedSlots.default) column.renderCell = _scopedSlots.default;
if (_scopedSlots.title) column.title = _scopedSlots.title;
}
return column;
}
function getColumnsTree(vnodes, options) {
return vnodes.map(function (vnode) {
if (vnode.tag && vnode.tag.indexOf('ten-table-column') > -1) {
var column = createColumn(vnode, options);
if (vnode.componentOptions.children) {
column.children = getColumnsTree(vnode.componentOptions.children, options);
}
return column;
}
return undefined;
}).filter(function (v) {
return v;
});
}
function traverseTree(tree) {
var depth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
var leaves = arguments.length > 2 ? arguments[2] : undefined;
return Math.max.apply(null, tree.map(function (_subTree) {
var subTree = _subTree;
if (subTree.children) {
if (subTree.fixed) {
subTree.children.forEach(function (c) {
c.fixed = subTree.fixed; // eslint-disable-line
});
}
var nowDepth = traverseTree(subTree.children, depth + 1, leaves);
subTree.leafCount = subTree.children.reduce(function (acc, cur) {
return acc + cur.leafCount;
}, 0);
subTree.depth = nowDepth;
return nowDepth;
}
subTree.leafCount = 1;
subTree.isLeaf = true;
subTree.depth = depth;
leaves.push(subTree);
return depth;
}));
}
function getColumnsTreeByProps(columns, options) {
return columns.map(function (_column) {
var column = _column;
column = _objectSpread(_objectSpread({
type: 'default',
sortable: false
}, defaultProp[column.type]), column);
column.validWidth = getValidWidth(column.width);
column.validMinWidth = getValidWidth(column.minWidth);
if (column.children) {
column.children = getColumnsTreeByProps(column.children, options);
}
if (column.key) {
column.render = column.key;
delete column.key;
}
if (column.fixed === '' || column.fixed === true) {
column.fixed = 'left';
}
if (!column.fixed) delete column.fixed;
column.columnKey = column.columnKey || column.render || column.title;
if (!column.order && options.sortInfo.column.columnKey === column.columnKey) {
column.order = options.sortInfo.order || 'default';
} else if (!column.order) {
column.order = 'default';
}
if (column.sorter) {
column.sortable = true;
}
return column;
});
}
;