vue-ant-patterns
Version:
vue-ant-patterns
436 lines (370 loc) • 13.5 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray');
var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2);
var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');
var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);
var _defineProperty2 = require('babel-runtime/helpers/defineProperty');
var _defineProperty3 = _interopRequireDefault(_defineProperty2);
var _extends2 = require('babel-runtime/helpers/extends');
var _extends3 = _interopRequireDefault(_extends2);
var _resizeObserverPolyfill = require('resize-observer-polyfill');
var _resizeObserverPolyfill2 = _interopRequireDefault(_resizeObserverPolyfill);
var _table = require('ant-design-vue/es/table');
var _table2 = _interopRequireDefault(_table);
var _vueTypes = require('ant-design-vue/es/_util/vue-types');
var _vueTypes2 = _interopRequireDefault(_vueTypes);
var _raf = require('ant-design-vue/es/_util/raf');
var _raf2 = _interopRequireDefault(_raf);
var _classnames3 = require('classnames');
var _classnames4 = _interopRequireDefault(_classnames3);
var _dContainer = require('../d-container');
var _dContainer2 = _interopRequireDefault(_dContainer);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
/*
* @Author: w.pengfei
* @Date: 2020-10-13 18:20:14
* @LastEditTime: 2021-05-20 14:57:01
* @Description: ''
* @LastEditors: w.pengfei
*/
var Vertical = _dContainer2['default'].Vertical,
Horizontal = _dContainer2['default'].Horizontal;
var RESIZE_STATUS_NONE = 0;
var RESIZE_STATUS_RESIZING = 1;
var RESIZE_STATUS_RESIZED = 2;
var widgetSizeMaps = {
small: {
header: 36,
pagination: 60
},
middle: {
header: 48,
pagination: 60
},
'default': {
header: 54,
pagination: 68
},
large: {
header: 54,
pagination: 68
}
};
var depotCls = 'depot';
var AutoTable = {
name: 'DAutoTable',
props: (0, _extends3['default'])({}, _table2['default'].props, {
size: _vueTypes2['default'].oneOf(['default', 'middle', 'small', 'large']).def('small'),
isAutoHeight: _vueTypes2['default'].bool.def(true),
customComponent: _vueTypes2['default'].any.def('d-table'),
/**
* 是否是斑马线显示表格
*/
tableStriped: _vueTypes2['default'].bool.def(false),
/**
* 是否隐藏多选后表格上方出现选中项的详细信息,默认显示
*/
hideSelectedAlert: _vueTypes2['default'].bool.def(false),
/**
* 用于查看选中项显示的标签名字,如果不传,默认从 columns 前往后取非操作的第一列名显示
*/
rowTitle: _vueTypes2['default'].string.def('')
}),
beforeDestroy: function beforeDestroy() {
if (this.resizeObserver) {
this.resizeObserver.disconnect();
this.resizeObserver = null;
}
_raf2['default'].cancel(this.nextFrameActionId);
_raf2['default'].cancel(this.resizeFrameId);
},
created: function created() {
if (this.isAutoHeight) {
this.isAnyColumnsFixed();
this.isAnyColumnsLeftFixed();
this.isAnyColumnsRightFixed();
}
},
data: function data() {
var _$props = this.$props,
response = _$props.response,
scroll = _$props.scroll;
var y = scroll ? scroll.y : undefined;
var isNumY = false;
if (y !== undefined && y !== null && typeof y !== 'boolean' && (typeof y === 'number' || typeof Number(y) === 'number')) {
isNumY = true;
}
var isPagination = this.hasPagination();
return {
cached: {},
height: 0,
tableHeight: 0,
isWin: response === 'window',
resizeStatus: RESIZE_STATUS_NONE,
oldHeight: 0,
htmlOverflowY: '',
bodyOverflowY: '',
scrollTop: 0,
scrollLeft: 0,
isShowFooter: true,
isNumY: isNumY,
isPagination: isPagination
};
},
mounted: function mounted() {
var _this = this;
if (this.isAutoHeight && !this.isNumY) {
this.$nextTick(function () {
_this.setHeight();
_this.resizeObserver = new _resizeObserverPolyfill2['default'](_this.handleResize);
_this.resizeObserver.observe(_this.$el);
var DAutoTable = _this.$refs.DAutoTable;
if (DAutoTable) {
_this.resizeObserverTable = new _resizeObserverPolyfill2['default'](_this.handleResize);
_this.resizeObserverTable.observe(DAutoTable.$el);
}
});
}
},
watch: {
height: function height(val) {
this.$emit('resize', val);
}
},
methods: {
isAnyColumnsFixed: function isAnyColumnsFixed() {
var columns = this.columns;
if (!columns || !Array.isArray(columns)) return;
return this._cache('isAnyColumnsFixed', function () {
return columns.some(function (column) {
return !!column.fixed;
});
});
},
isAnyColumnsLeftFixed: function isAnyColumnsLeftFixed() {
var columns = this.columns;
if (!columns || !Array.isArray(columns)) return;
return this._cache('isAnyColumnsLeftFixed', function () {
return columns.some(function (column) {
return column.fixed === 'left' || column.fixed === true;
});
});
},
isAnyColumnsRightFixed: function isAnyColumnsRightFixed() {
var columns = this.columns;
if (!columns || !Array.isArray(columns)) return;
return this._cache('isAnyColumnsRightFixed', function () {
return columns.some(function (column) {
return column.fixed === 'right';
});
});
},
_cache: function _cache(name, fn) {
var cached = this.cached;
if (name in cached) {
return this.cached[name];
}
this.cached[name] = fn();
return this.cached[name];
},
hasPagination: function hasPagination(props) {
return (props || this.$props).pagination !== false;
},
getElSize: function getElSize() {
var $el = this.$el;
var sizeObj = $el.getBoundingClientRect();
return sizeObj;
},
getWrapElHeight: function getWrapElHeight() {
var sizeObj = this.getElSize();
var tableSizeObj = this.getTableElSize();
this.oldHeight = tableSizeObj ? parseInt(tableSizeObj.height) : parseInt(sizeObj.height);
// this.oldHeight = parseInt(sizeObj.height);
},
getTableElSize: function getTableElSize() {
var DAutoTable = this.$refs.DAutoTable;
var sizeObj = null;
if (DAutoTable) {
sizeObj = DAutoTable.$el.getBoundingClientRect();
}
return sizeObj;
},
setHeight: function setHeight() {
var isPagination = this.isPagination;
var _$props2 = this.$props,
_$props2$size = _$props2.size,
size = _$props2$size === undefined ? 'small' : _$props2$size,
pagination = _$props2.pagination,
showHeader = _$props2.showHeader;
var sizeMaps = widgetSizeMaps[size];
var headerHeight = sizeMaps.header,
paginationHeight = sizeMaps.pagination;
var sizeObj = this.getElSize();
var height = sizeObj.height;
var DAutoTable = this.$refs.DAutoTable;
var DTableSelectTagsBox = null;
var selectBoxSize = null;
if (DAutoTable) {
DTableSelectTagsBox = DAutoTable.$refs['DTable-SelectTagsBox'];
}
if (DTableSelectTagsBox) {
selectBoxSize = DTableSelectTagsBox.$el.getBoundingClientRect();
}
if (showHeader) {
height = height - headerHeight;
if (selectBoxSize) {
height -= selectBoxSize.height;
}
}
if (isPagination) {
height = height - paginationHeight;
if (pagination && pagination.position && pagination.position === 'both') {
height = height - paginationHeight;
}
}
height = parseInt(height);
this.height = height;
this.getWrapElHeight();
this.isShowFooter = true;
},
setState: function setState() {
var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var callback = arguments[1];
var newState = state;
(0, _extends3['default'])(this.$data, newState);
this.$forceUpdate();
this.$nextTick(function () {
callback && callback();
});
},
resizeTable: function resizeTable() {
var _this2 = this;
var isAutoHeight = this.$props.isAutoHeight;
if (isAutoHeight) {
this.setState({ resizeStatus: RESIZE_STATUS_RESIZING }, function () {
_raf2['default'].cancel(_this2.resizeFrameId);
_this2.resizeFrameId = (0, _raf2['default'])(function () {
_this2.setState({ resizeStatus: RESIZE_STATUS_NONE });
_this2.setHeight();
});
});
}
},
resizeOnNextFrame: function resizeOnNextFrame() {
_raf2['default'].cancel(this.nextFrameActionId);
this.nextFrameActionId = (0, _raf2['default'])(this.resizeTable);
},
handleResize: function handleResize(entries) {
var resizeStatus = this.resizeStatus;
var target = entries[0].target;
var _target$getBoundingCl = target.getBoundingClientRect(),
height = _target$getBoundingCl.height;
if (resizeStatus !== RESIZE_STATUS_NONE) {
return;
}
var fixedHeight = parseInt(height);
if (parseInt(this.oldHeight) !== fixedHeight) {
this.isShowFooter = false;
this.resizeOnNextFrame();
}
},
/**
* 根据 pagination position 位置渲染 footer
*/
renderFooter: function renderFooter(footer, paginationPosition) {
var _classnames;
var h = this.$createElement;
var isPagination = this.isPagination,
isShowFooter = this.isShowFooter;
var _$props3 = this.$props,
pagination = _$props3.pagination,
size = _$props3.size;
if (!isPagination) return null;
var position = pagination.position || 'bottom';
var isRender = position === paginationPosition || position === 'both';
var isTop = paginationPosition === 'top';
var footerProps = {
'class': (0, _classnames4['default'])((_classnames = {}, (0, _defineProperty3['default'])(_classnames, depotCls + '-auto-table-footer', 1), (0, _defineProperty3['default'])(_classnames, depotCls + '-auto-table-footer-' + size, 1), (0, _defineProperty3['default'])(_classnames, depotCls + '-auto-table-footer-top', position === 'both' && isTop), _classnames))
};
return isRender && isShowFooter ? h(
Horizontal,
footerProps,
[footer]
) : null;
}
},
render: function render(h) {
var _classnames2;
var _$props4 = this.$props,
isAutoHeight = _$props4.isAutoHeight,
_$props4$customCompon = _$props4.customComponent,
customComponent = _$props4$customCompon === undefined ? 'd-table' : _$props4$customCompon,
tableStriped = _$props4.tableStriped,
hideSelectedAlert = _$props4.hideSelectedAlert,
rowTitle = _$props4.rowTitle,
scroll = _$props4.scroll,
propsFooter = _$props4.footer,
tableProps = (0, _objectWithoutProperties3['default'])(_$props4, ['isAutoHeight', 'customComponent', 'tableStriped', 'hideSelectedAlert', 'rowTitle', 'scroll', 'footer']);
var $attrs = this.$attrs,
$scopedSlots = this.$scopedSlots,
$slots = this.$slots,
$listeners = this.$listeners,
height = this.height,
isNumY = this.isNumY;
var vScroll = (0, _extends3['default'])({}, scroll, { y: isAutoHeight ? height : true });
if (isNumY) {
vScroll.y = scroll.y;
}
var footer = $slots.footer,
slots = (0, _objectWithoutProperties3['default'])($slots, ['footer']);
/**
* !!! 注意, sFooter 变量即使未用也不可删除
*/
var sFooter = $scopedSlots.footer,
scopedSlots = (0, _objectWithoutProperties3['default'])($scopedSlots, ['footer']);
/**
* console.log(sFooter)
*/
var size = tableProps.size;
var componentProps = {
props: (0, _extends3['default'])({
hideSelectedAlert: hideSelectedAlert,
rowTitle: rowTitle
}, tableProps, {
scroll: (0, _extends3['default'])({}, scroll, vScroll)
}),
scopedSlots: (0, _extends3['default'])({}, scopedSlots),
attrs: (0, _extends3['default'])({}, $attrs),
'class': 'flex flex-col flex-grow',
on: (0, _extends3['default'])({}, $listeners),
ref: 'DAutoTable'
};
var contProps = {
props: {
grow: true,
flex: true
},
style: {
overflow: 'hidden',
position: 'relative'
},
'class': (0, _classnames4['default'])((_classnames2 = {}, (0, _defineProperty3['default'])(_classnames2, depotCls + '-auto-table', 1), (0, _defineProperty3['default'])(_classnames2, depotCls + '-auto-table-' + size, 1), _classnames2))
};
var T = h(customComponent, (0, _extends3['default'])({}, componentProps), [].concat((0, _toConsumableArray3['default'])(slots)));
if (isNumY) {
return T;
}
return h(
Vertical,
contProps,
[T, this.renderFooter(footer, 'top'), this.renderFooter(footer, 'bottom')]
);
}
};
AutoTable.install = function (Vue) {
Vue.component(AutoTable.name, AutoTable);
};
exports['default'] = AutoTable;