@opensig/opendesign
Version:
357 lines (356 loc) • 16.5 kB
JavaScript
import { defineComponent, toRefs, useSlots, computed, inject, ref, watch, provide, resolveComponent, createElementBlock, openBlock, Fragment, createElementVNode, createCommentVNode, unref, normalizeClass, renderList, mergeProps, createBlock, renderSlot, isRef, createVNode, resolveDynamicComponent, createSlots, withCtx } from "vue";
import "../table/index.mjs";
import { getRenderableComponent } from "../_utils/vue-utils.mjs";
import { OCheckbox } from "../checkbox/index.mjs";
import { IconLoadingSmall, IconChevronRightSmall } from "../_utils/icons.mjs";
import { isNil, isArray, isNumber } from "../_utils/is.mjs";
import { promiseWithResolvers } from "../_utils/helper.mjs";
import _sfc_main$1 from "./TableCellRenderer.vue.mjs";
import { dataTableInjectKey, dataTableRowInjectKey } from "./provide.mjs";
import { getIsLevelExpandable, getCellValue, getColumnPosition } from "./utils.mjs";
import { DEFAULT_ROW_LAST_MARKER, DEFAULT_CELL_LAST_COL_MARKER, DEFAULT_CELL_FIRST_COL_MARKER } from "../table/useTableMeta.mjs";
const _hoisted_1 = ["data-level"];
const _hoisted_2 = ["data-cell-index"];
const _hoisted_3 = { class: "o-table-cell__inner" };
const _hoisted_4 = {
key: 0,
class: "o-table-row-icon-placeholder"
};
const _hoisted_5 = {
key: 0,
class: "o-data-table-header-divider-v"
};
const _hoisted_6 = ["colspan"];
const _hoisted_7 = { class: "o-table-cell__inner o-table-expand-cell__inner" };
const _sfc_main = /* @__PURE__ */ defineComponent({
__name: "TableRow",
props: {
row: {},
rowIndex: {},
level: {}
},
setup(__props) {
const props = __props;
const { row, rowIndex, level } = toRefs(props);
const slots = useSlots();
const tdSlotNames = computed(() => Object.keys(slots).filter((name) => name.startsWith("td_")));
const {
getRowKey,
data,
border,
headerStyle,
spanMethod,
selection,
checkStrictly,
dataColumns,
groupColumns,
hasExpandSlot,
expandMethod,
expandedRowKeys,
isBodyCellRemoved,
isLastLeftFixedCell,
isFirstRightFixedCell,
rowKeyMap,
toFilteredSelectable,
selectedKeys,
handleTableSelection,
handleLoadChildren
} = inject(dataTableInjectKey);
const { isLevelExpandable } = inject(dataTableRowInjectKey);
const rowKey = computed(() => getRowKey(row.value, rowIndex.value));
const expandBy = computed(() => {
if (hasExpandSlot.value || !isNil(expandMethod == null ? void 0 : expandMethod.value)) {
return "expand";
}
return "children";
});
const indeterminate = computed(() => {
if (checkStrictly.value || selectedKeys.value.includes(rowKey.value)) {
return void 0;
}
const { descendantRowKeys } = rowKeyMap.value.get(rowKey.value);
const selectableDescendantRowKeys = toFilteredSelectable(descendantRowKeys);
const someChecked = selectableDescendantRowKeys.some((v) => selectedKeys.value.includes(v));
const someUncheck = selectableDescendantRowKeys.some((v) => !selectedKeys.value.includes(v));
return someChecked && someUncheck;
});
const handleRowSelection = (newVal) => {
if (checkStrictly.value) {
return;
}
const { ancestorRowKeys, descendantRowKeys } = rowKeyMap.value.get(rowKey.value);
let _selectedKeys = selectedKeys.value.filter((v) => !descendantRowKeys.includes(v));
if (newVal.includes(rowKey.value)) {
_selectedKeys.push(...toFilteredSelectable(descendantRowKeys));
} else {
_selectedKeys = _selectedKeys.filter((v) => !descendantRowKeys.includes(v));
}
ancestorRowKeys.forEach((v) => {
const { descendantRowKeys: ancestorDescendantRowKeys } = rowKeyMap.value.get(v);
if (toFilteredSelectable(ancestorDescendantRowKeys).every((x) => _selectedKeys.includes(x))) {
_selectedKeys.push(v);
} else {
_selectedKeys = _selectedKeys.filter((x) => x !== v);
}
});
selectedKeys.value = Array.from(new Set(_selectedKeys));
handleTableSelection(rowKey.value, newVal);
};
const expandLoading = ref(false);
const isRowExpandable = computed(() => {
if (!isLevelExpandable.value.expandable) {
return false;
}
return isLevelExpandable.value.expandableRowIndexes.includes(rowIndex.value);
});
const isRowExpanded = computed(() => expandedRowKeys.value.includes(rowKey.value));
const toggleRowExpand = () => {
const index = expandedRowKeys.value.findIndex((v) => v === rowKey.value);
if (index !== -1) {
expandedRowKeys.value.splice(index, 1);
return;
}
expandedRowKeys.value.push(rowKey.value);
};
watch(
expandedRowKeys,
() => {
var _a;
const index = expandedRowKeys.value.findIndex((v) => v === rowKey.value);
if (index === -1) {
return;
}
if (!isArray(row.value.children) && row.value.children) {
return;
}
if (!row.value.hasChildren || ((_a = row.value.children) == null ? void 0 : _a.length)) {
return;
}
const { reject, resolve, promise } = promiseWithResolvers();
expandLoading.value = true;
handleLoadChildren({ row: row.value, rowIndex: rowIndex.value, rowKey: rowKey.value, reject, resolve });
promise.catch(() => expandedRowKeys.value.splice(index, 1)).finally(() => expandLoading.value = false);
},
{ deep: true }
);
provide(dataTableRowInjectKey, {
isLevelExpandable: computed(() => getIsLevelExpandable({ list: row.value.children, hasExpandSlot, expandMethod: expandMethod == null ? void 0 : expandMethod.value }))
});
return (_ctx, _cache) => {
var _a;
const _component_TableRow = resolveComponent("TableRow", true);
return openBlock(), createElementBlock(
Fragment,
null,
[
createElementVNode("tr", {
class: normalizeClass([
"o-table-row",
"o-table-body-row",
{
[unref(DEFAULT_ROW_LAST_MARKER)]: unref(rowIndex) === unref(data).length - 1 && !isRowExpandable.value,
"o-table-row-disabled": (_a = unref(rowKeyMap).get(rowKey.value)) == null ? void 0 : _a.disabled
}
]),
"data-level": unref(level)
}, [
(openBlock(true), createElementBlock(
Fragment,
null,
renderList(unref(dataColumns), (column, colIndex) => {
var _a2, _b, _c, _d;
return openBlock(), createElementBlock(
Fragment,
{
key: column.key
},
[
!unref(isBodyCellRemoved)(unref(rowIndex), colIndex) ? (openBlock(), createElementBlock("td", mergeProps({
key: 0,
ref_for: true
}, (_a2 = unref(spanMethod)) == null ? void 0 : _a2({ row: unref(row), column, cellValue: unref(getCellValue)({ row: unref(row), column }), rowIndex: unref(rowIndex), colIndex }), {
class: {
"o-table-cell": true,
"o-table-body-cell": true,
"o-table-column-as-header": column.asHeader,
"o-cell-last-row": unref(rowIndex) === unref(data).length - 1 && !isRowExpandable.value,
"o-table-cell-fixed": column.fixed,
"o-table-cell-fixed-left": column.fixed === "left",
"o-table-cell-fixed-right": column.fixed === "right",
"o-table-cell-last-left-fixed": unref(isLastLeftFixedCell)(unref(rowIndex), colIndex),
"o-table-cell-first-right-fixed": unref(isFirstRightFixedCell)(unref(rowIndex), colIndex),
[unref(DEFAULT_CELL_FIRST_COL_MARKER)]: column.isFirstCol,
[unref(DEFAULT_CELL_LAST_COL_MARKER)]: column.isLastCol,
"o-table-cell-tooltip": column.showOverflowToolTip,
"o-table-cell-wrappable": unref(isNumber)(column.showOverflowToolTip) && column.showOverflowToolTip > 1
},
style: {
...unref(getColumnPosition)({
column,
dataColumns: unref(dataColumns),
groupColumns: unref(groupColumns),
border: unref(border),
colSpan: (_c = (_b = unref(spanMethod)) == null ? void 0 : _b({ row: unref(row), column, cellValue: unref(getCellValue)({ row: unref(row), column }), rowIndex: unref(rowIndex), colIndex })) == null ? void 0 : _c.colSpan
}),
"--cell-max-row": unref(isNumber)(column.showOverflowToolTip) ? column.showOverflowToolTip : 1
},
"data-cell-index": `td_${unref(rowIndex)}_${colIndex}`
}), [
createElementVNode("span", _hoisted_3, [
column.isFirstCol && unref(selection) ? (openBlock(), createBlock(unref(OCheckbox), {
key: 0,
modelValue: unref(selectedKeys),
"onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => isRef(selectedKeys) ? selectedKeys.value = $event : null),
indeterminate: indeterminate.value,
value: unref(getRowKey)(unref(row), unref(rowIndex)),
disabled: (_d = unref(rowKeyMap).get(rowKey.value)) == null ? void 0 : _d.disabled,
class: "o-table-row-checkbox",
onChange: handleRowSelection
}, null, 8, ["modelValue", "indeterminate", "value", "disabled"])) : createCommentVNode("v-if", true),
column.isFirstCol ? (openBlock(), createElementBlock(
Fragment,
{ key: 1 },
[
unref(level) > 0 && !unref(isLevelExpandable).expandable ? (openBlock(), createElementBlock("span", _hoisted_4)) : createCommentVNode("v-if", true),
(openBlock(true), createElementBlock(
Fragment,
null,
renderList(unref(level), (i) => {
return openBlock(), createElementBlock("span", {
key: i,
class: "o-table-row-icon-placeholder"
});
}),
128
/* KEYED_FRAGMENT */
))
],
64
/* STABLE_FRAGMENT */
)) : createCommentVNode("v-if", true),
column.isFirstCol && unref(isLevelExpandable).expandable ? (openBlock(), createElementBlock(
Fragment,
{ key: 2 },
[
expandLoading.value ? (openBlock(), createBlock(unref(IconLoadingSmall), {
key: 0,
class: "o-rotating o-table-row-expand-trigger loading"
})) : (openBlock(), createBlock(unref(IconChevronRightSmall), {
key: 1,
class: normalizeClass({
"o-table-row-expand-trigger": true,
expandable: isRowExpandable.value,
expanded: isRowExpanded.value
}),
onClick: toggleRowExpand
}, null, 8, ["class"]))
],
64
/* STABLE_FRAGMENT */
)) : createCommentVNode("v-if", true),
renderSlot(_ctx.$slots, `td_${column.key}`, {
row: unref(row),
column,
cellValue: unref(getCellValue)({ row: unref(row), column }),
index: unref(rowIndex)
}, () => [
createVNode(_sfc_main$1, {
row: unref(row),
column,
"cell-value": unref(getCellValue)({ row: unref(row), column }),
"row-index": unref(rowIndex),
"col-index": colIndex
}, null, 8, ["row", "column", "cell-value", "row-index", "col-index"])
])
]),
unref(headerStyle) === "split-line" && column.asHeader ? (openBlock(), createElementBlock("div", _hoisted_5)) : createCommentVNode("v-if", true)
], 16, _hoisted_2)) : createCommentVNode("v-if", true)
],
64
/* STABLE_FRAGMENT */
);
}),
128
/* KEYED_FRAGMENT */
))
], 10, _hoisted_1),
isRowExpandable.value && expandBy.value === "expand" && isRowExpanded.value ? (openBlock(), createElementBlock(
"tr",
{
key: 0,
class: normalizeClass([
"o-table-row",
"o-table-body-row",
"o-table-row-expand",
{
[unref(DEFAULT_ROW_LAST_MARKER)]: unref(rowIndex) === unref(data).length - 1
}
])
},
[
createElementVNode("td", {
colspan: unref(dataColumns).length,
class: normalizeClass({
"o-table-cell": true,
"o-table-body-cell": true,
"o-table-expand-cell": true,
"o-cell-last-row": unref(rowIndex) === unref(data).length - 1
})
}, [
createElementVNode("span", _hoisted_7, [
renderSlot(_ctx.$slots, "expand", {
row: unref(row),
rowIndex: unref(rowIndex)
}, () => {
var _a2;
return [
(openBlock(), createBlock(resolveDynamicComponent(unref(getRenderableComponent)((_a2 = unref(expandMethod)) == null ? void 0 : _a2(unref(row), unref(rowIndex))))))
];
})
])
], 10, _hoisted_6)
],
2
/* CLASS */
)) : unref(isArray)(unref(row).children) && unref(row).children.length && isRowExpanded.value ? (openBlock(true), createElementBlock(
Fragment,
{ key: 1 },
renderList(unref(row).children, (child, childIndex) => {
return openBlock(), createBlock(_component_TableRow, {
key: unref(getRowKey)(child, childIndex),
row: child,
"row-index": childIndex,
level: unref(level) + 1
}, createSlots({
expand: withCtx(() => [
renderSlot(_ctx.$slots, "expand", {
row: child,
rowIndex: childIndex
})
]),
_: 2
/* DYNAMIC */
}, [
renderList(tdSlotNames.value, (name) => {
return {
name,
fn: withCtx((slotProps) => [
renderSlot(_ctx.$slots, name, mergeProps({ ref_for: true }, slotProps))
])
};
})
]), 1032, ["row", "row-index", "level"]);
}),
128
/* KEYED_FRAGMENT */
)) : createCommentVNode("v-if", true)
],
64
/* STABLE_FRAGMENT */
);
};
}
});
export {
_sfc_main as default
};