efview
Version:
A high quality Service UI components Library with Vue.js
226 lines (203 loc) • 7.05 kB
JavaScript
import { scrollTop } from '../../utils/assist';
let common = {};
function Table_getSelection () {
// 分别拿根数据和子数据的已选项
let selectionIndexes = [];
let selectionRowKeys = [];
for (let i in this.objData) {
const objData = this.objData[i];
if (objData._isChecked) selectionIndexes.push(parseInt(i));
if (objData.children && objData.children.length) {
selectionRowKeys = selectionRowKeys.concat(this.getSelectionChildrenRowKeys(objData, selectionRowKeys));
}
}
// 去重的 RowKeys
selectionRowKeys = [...new Set(selectionRowKeys)];
let selection = [];
this.data.forEach((item, index) => {
if (selectionIndexes.indexOf(index) > -1) {
// 自定义:加行号索引 便于批量删除
item._customIndex = index;
selection = selection.concat(item);
}
if (item.children && item.children.length && selectionRowKeys.length) {
selection = selection.concat(this.getSelectionChildren(item, selection, selectionRowKeys));
}
});
selection = [...new Set(selection)];
return JSON.parse(JSON.stringify(selection));
}
function Table_handleResize () {
//let tableWidth = parseInt(getStyle(this.$el, 'width')) - 1;
// 原代码当列宽不设置的时 会出现缩进的现象
//let tableWidth = this.$el.offsetWidth - 1;
//自定义
let tableWidth = this.$el.parentNode.offsetWidth;
let columnsWidth = {};
let sumMinWidth = 0;
let hasWidthColumns = [];
let noWidthColumns = [];
let maxWidthColumns = [];
let noMaxWidthColumns = [];
this.cloneColumns.forEach((col) => {
if (col.width) {
hasWidthColumns.push(col);
}
else{
noWidthColumns.push(col);
if (col.minWidth) {
sumMinWidth += col.minWidth;
}
if (col.maxWidth) {
maxWidthColumns.push(col);
}
else {
noMaxWidthColumns.push(col);
}
}
col._width = null;
});
let unUsableWidth = hasWidthColumns.map(cell => cell.width).reduce((a, b) => a + b, 0);
let usableWidth = tableWidth - unUsableWidth - sumMinWidth - (this.showVerticalScrollBar?this.scrollBarWidth:0) - 1;
let usableLength = noWidthColumns.length;
let columnWidth = 0;
if(usableWidth > 0 && usableLength > 0){
columnWidth = parseInt(usableWidth / usableLength);
}
for (let i = 0; i < this.cloneColumns.length; i++) {
const column = this.cloneColumns[i];
let width = columnWidth + (column.minWidth?column.minWidth:0);
if(column.width){
width = column.width;
}
else{
if (column._width) {
width = column._width;
}
else {
if (column.minWidth > width){
width = column.minWidth;
}
else if (column.maxWidth < width){
width = column.maxWidth;
}
if (usableWidth>0) {
usableWidth -= width - (column.minWidth?column.minWidth:0);
usableLength--;
if (usableLength > 0) {
columnWidth = parseInt(usableWidth / usableLength);
}
else {
columnWidth = 0;
}
}
else{
columnWidth = 0;
}
}
}
column._width = width;
columnsWidth[column._index] = {
width: width
};
}
if(usableWidth>0) {
usableLength = noMaxWidthColumns.length;
columnWidth = parseInt(usableWidth / usableLength);
for (let i = 0; i < noMaxWidthColumns.length; i++) {
const column = noMaxWidthColumns[i];
let width = column._width + columnWidth;
if (usableLength > 1) {
usableLength--;
usableWidth -= columnWidth;
columnWidth = parseInt(usableWidth / usableLength);
}
else {
columnWidth = 0;
}
column._width = width;
columnsWidth[column._index] = {
width: width
};
}
}
this.tableWidth = this.cloneColumns.map(cell => cell._width).reduce((a, b) => a + b, 0) + (this.showVerticalScrollBar?this.scrollBarWidth:0) + 1;
this.columnsWidth = columnsWidth;
this.fixedHeader();
}
function Anchor_handleScrollTo () {
const anchor = document.getElementById(this.currentId);
const currentLinkElementA = document.querySelector(`a[data-href="${this.currentLink}"]`);
let offset = this.scrollOffset;
if (currentLinkElementA) {
offset = parseFloat(currentLinkElementA.getAttribute('data-scroll-offset'));
}
if (!anchor) return;
const offsetTop = anchor.offsetTop - this.wrapperTop - offset;
this.animating = true;
scrollTop(this.scrollContainer, this.scrollElement.scrollTop, offsetTop, 100, () => {
this.animating = false;
});
this.handleSetInkTop();
}
function handleCheckClick (event, new_window = false) {
if (this.to) {
if (this.target === '_blank') {
return false;
} else {
event.preventDefault();
if (this.to === 'click') {
this.$emit('click', event);
} else {
this.handleClick(new_window);
}
}
}
}
function Upload_handleClick () {
if (this.itemDisabled) return;
// 自定义:弹框之前check
if (this.$parent && this.$parent.onClick) {
this.$parent.onClick()
} else {
if (this.$refs.input) {
this.$refs.input.click();
}
}
}
common.initTable = function (obj) {
obj.methods.getSelection = Table_getSelection;
obj.methods.handleResize = Table_handleResize;
}
common.initAnchor = function (obj) {
obj.methods.handleScrollTo = Anchor_handleScrollTo;
}
common.initBreadcrumbItem = function (obj) {
obj.mixins[0].methods.handleCheckClick = handleCheckClick;
}
common.initButton = function (obj) {
obj.mixins[0].methods.handleCheckClick = handleCheckClick;
}
common.initCard = function (obj) {
obj.mixins[0].methods.handleCheckClick = handleCheckClick;
}
common.initCell = function (obj) {
obj.mixins[0].methods.handleCheckClick = handleCheckClick;
}
common.initMenuItem = function (obj) {
obj.mixins[2].methods.handleCheckClick = handleCheckClick;
}
common.initUpload = function (obj) {
obj.methods.handleClick = Upload_handleClick;
}
common.initEview = function (ViewUI) {
common.initTable(ViewUI.Table);
common.initAnchor(ViewUI.Anchor);
common.initBreadcrumbItem(ViewUI.BreadcrumbItem);
common.initButton(ViewUI.Button);
common.initCard(ViewUI.Card);
common.initCell(ViewUI.Cell);
common.initMenuItem(ViewUI.MenuItem);
common.initUpload(ViewUI.Upload);
}
export default common;