//@ts-nocheck
import { defineComponent, markRaw, provide } from "vue";
import { xU } from "../../ventoseUtils";
import $ from "jquery";
import { xVirTableTh } from "./xVirTableTh";
import { xVirTableBody, t_rowPayload } from "./xVirTableBody";
import { STATIC_WORD } from "../common";
const { MutatingProps } = xU;
type t_selectedConfigs = {
type: keyof typeof defXVirTableConfigs.type;
/* 数据ID */
prop: string;
isDisabled?: Function;
isSelect?: Function;
};
type t_defXVirTableConfigs = {
queryTableList?: Function;
selectedConfigs?: t_selectedConfigs;
selected?: [];
rowHeight: number;
dataSource: any[];
columns: object;
onClickRow?: (payload: t_rowPayload) => void;
getSelectedRow?: Function;
};
const mmWidth = _width =>
`width:${_width}; min-width:${_width}; max-width:${_width};`;
const widthNumber = widthPx => {
let widthNumber = 0;
try {
const [_, width] = String(widthPx).match(/(.*)px/i);
widthNumber = Number(width);
} catch (error) {}
return widthNumber;
};
export function defXVirTableConfigs(options: t_defXVirTableConfigs) {
const required = ["rowHeight", "columns"];
if (
xU.some(required, prop => {
if (!options[prop]) {
alert("defXVirTableConfigs miss required " + prop);
return true;
}
return false;
})
) {
throw new Error("defXVirTableConfigs miss required");
}
if (options.selectedConfigs) {
/* 如果有selectedConfigs one是单选*/
options.selected = options.selected || [];
options.getSelectedRow = markRaw(function () {
return xU.filter(options.dataSource, i => {
const idValue = i[options.selectedConfigs.prop];
return (
xU.isArrayFill(options?.selected) &&
options.selected.includes(idValue)
);
});
});
}
return options;
}
defXVirTableConfigs.type = {
many: "many",
one: "one"
};
/**
* 展示列的顺序
*/
export const xVirTable = defineComponent({
props: ["configs", "uniqBy"],
components: {
xVirTableTh,
xVirTableBody
},
setup(props) {
provide("uniqBy", props.uniqBy);
provide("configs", props.configs);
},
mounted() {
this.initStyle();
this.layoutDebounce();
},
provide() {
const vm = this;
return {
xVirTable: vm
};
},
data() {
this.resetOperationWidthDebounce = xU.debounce(
this.resetOperationWidth,
STATIC_WORD.NEXT_TICK_TIME
);
this.layoutDebounce = xU.debounce(this.layout, STATIC_WORD.NEXT_TICK_TIME);
this.resetColumnWidthDebounce = xU.debounce(
this.resetColumnWidth,
STATIC_WORD.NEXT_TICK_TIME
);
return {
styleWidthXVirTable: 0,
styleWidthOperation: "120px",
selectedAll: false
};
},
computed: {
selectedIndeterminate() {
const dataLength = this.configs?.dataSource?.length || 0;
const selectedLength = this.selected.length;
if (
dataLength == 0 ||
selectedLength == 0 ||
dataLength == selectedLength
) {
return false;
}
return true;
},
selected() {
return this.configs?.selected || [];
},
selectedType() {
/* 如果没有selectedConfigs则不显示多选列 */
if (!this.configs?.selectedConfigs) {
return false;
}
/* 如果有selectedConfigs,默认是many */
return (
this.configs?.selectedConfigs?.type || defXVirTableConfigs.type.many
);
},
selectedProp() {
if (!this.selectedType) {
return false;
}
if (!this.configs?.selectedConfigs?.prop) {
alert("vVirTable miss this.selected id prop");
}
return this.configs?.selectedConfigs?.prop;
},
selectedBy() {
if (!this.selectedType) {
return false;
}
if (xU.isFunction(this.configs?.selectedConfigs?.fn)) {
return this.configs?.selectedConfigs?.fn;
} else {
return false;
}
},
customClass() {
if (xU.isFunction(this.configs?.customClass)) {
return this.configs?.customClass(this.xVirTableId);
} else {
return "";
}
},
rowHeight() {
return this.configs?.rowHeight || 32;
},
/* 组件唯一标识 */
xVirTableId(): string {
return `xVirTableId_${this._.uid}`;
},
/* 展示列的顺序 */
columnOrder() {
if (this.configs?.columnOrder) {
return this.configs?.columnOrder;
}
return Object.keys(this.configs?.columns || {});
},
columnWidthStyleArray() {
const _columnWidthStyleArray = xU.reduce(
this.columnOrder,
(styleEachColumn, prop: any) => {
if (prop === STATIC_WORD.OPERATION) {
return styleEachColumn;
} else {
const configsColumn = this.configs.columns[prop] || {};
const { width, __calcWidth } = configsColumn;
const _width = __calcWidth || width;
/* 列各自的宽度 */
if (_width) {
styleEachColumn.push(
`#${
this.xVirTableId
} div[role=tr] >div[role=th][data-prop=${prop}],#${
this.xVirTableId
} div[role=tr] >div[role=td][data-prop=${prop}]{ ${mmWidth(
_width
)} }`
);
}
return styleEachColumn;
}
},
/* 操作列的宽度 */
[
`#${this.xVirTableId} div[role=tr] >div[role=th][data-prop=${
STATIC_WORD.OPERATION
}]{ ${mmWidth(this.styleWidthOperation)} }`,
`#${this.xVirTableId} div[role=tr] >div[role=td][data-prop=${
STATIC_WORD.OPERATION
}]{ ${mmWidth(this.styleWidthOperation)} }`
]
);
return _columnWidthStyleArray;
},
vDomTheadSelect() {
/* 没有this.selectedConfigs配置项连 空位都不要 */
if (!this.selectedType) {
return null;
}
let vDomTheadSelect = (