UNPKG

vue-gantt-3

Version:

A gantt component for Vue 3

308 lines (307 loc) 10.6 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const vue = require("vue"); const dayjs = require("dayjs"); const common = require("../utils/common.js"); const useGanttRowNode = ({ ganttViewRef, tableViewRef, rows, getRowId, setExpand, setSelect, refreshCells, onViewPortChanged }) => { const rowNodeMap = vue.shallowRef(/* @__PURE__ */ new Map()); const rowNodeIds = vue.ref([]); const visibleRowIds = vue.ref([]); const rowDataList = vue.shallowRef([]); const firstLevelRowNode = vue.shallowRef([]); vue.onBeforeMount(() => { initRowNode(); }); vue.watch(rows, (newRows, oldRows) => { onRowsChange(newRows, oldRows); }, { deep: false }); const initRowNode = () => { const oldRowNodeMap = rowNodeMap.value; const newRowNodeMap = /* @__PURE__ */ new Map(); const newRowNodeIds = []; const newVisibleRowIds = []; const newRowDataList = []; const newFirstLevelRowNode = []; convertALLRows(rows.value, oldRowNodeMap, newRowNodeMap, newRowNodeIds, newRowDataList); rowNodeIds.value = newRowNodeIds; rowNodeMap.value = newRowNodeMap; rowDataList.value = newRowDataList; for (let rowId of newRowNodeIds) { const rowNode = newRowNodeMap.get(rowId); if (rowNode) { if (rowNode.expand) { newVisibleRowIds.push(rowId); } if (!rowNode.parentId) { newFirstLevelRowNode.push(rowNode); } } } visibleRowIds.value = newVisibleRowIds; firstLevelRowNode.value = newFirstLevelRowNode; }; const convertALLRows = (rows2, oldRowNodeMap, newRowNodeMap, newRowNodeIds, newRowDataList, level = 0) => { for (let row of rows2) { const id = getRowId(row); newRowNodeIds.push(id); newRowDataList.push(row); if (row.children) { convertALLRows(row.children, oldRowNodeMap, newRowNodeMap, newRowNodeIds, newRowDataList, level + 1); } const newRowNode = oldRowNodeMap.get(id) || createRowNode(row, newRowNodeMap); updateRowNodeInfo(newRowNode, row, newRowNodeMap, level); newRowNodeMap.set(id, newRowNode); } }; const createRowNode = (row, newRowNodeMap) => { const startDateArr = []; const endDateArr = []; const hasChildren = row.children && row.children.length > 0; if (hasChildren) { for (let child of row.children) { const id2 = getRowId(child); const childRowNode = newRowNodeMap.get(id2); if (childRowNode) { childRowNode.startDate && startDateArr.push(childRowNode.startDate); childRowNode.endDate && endDateArr.push(childRowNode.endDate); } } } else if (row.timeLines) { for (let timeLine of row.timeLines) { startDateArr.push(dayjs(timeLine.startDate)); endDateArr.push(dayjs(timeLine.endDate)); } } const id = getRowId(row); const baseNode = { id, data: row, startDate: dayjs.min(startDateArr), endDate: dayjs.max(endDateArr), hasChildren: !!hasChildren, setExpand, setSelect, expand: true, level: 0 }; return baseNode; }; const updateRowNodeInfo = (rowNode, row, newRowNodeMap, level) => { const hasChildren = row.children && row.children.length > 0; const children = []; if (hasChildren) { for (let child of row.children) { const id = getRowId(child); const childRowNode = newRowNodeMap.get(id); if (childRowNode) { children.push(childRowNode); } } } for (let child of children) { child.parentId = rowNode.id; } Object.assign(rowNode, { data: row, readOnly: hasChildren, hasChildren: !!hasChildren, children, level }); }; const onRowsChange = (newRows, oldRows) => { const oldRowNodeMap = rowNodeMap.value; const newAddRowIds = []; common.treeForEachSkipChildren(newRows, (currentRow) => { const id = getRowId(currentRow); if (!oldRowNodeMap.has(id)) { newAddRowIds.push(id); return "skipChildren"; } }); initRowNode(); const newRowNodeMap = rowNodeMap.value; const newDeleteRowIds = []; common.treeForEachSkipChildren(oldRows, (currentRow) => { const id = getRowId(currentRow); if (!newRowNodeMap.has(id)) { newDeleteRowIds.push(id); return "skipChildren"; } }); const topLevelRowNodeFromAdd = []; const topLevelRowNodeFromDelete = []; const topLevelRowNodeFromOldDelete = []; for (let rowId of newAddRowIds) { const topParentRowNode = getTopLevelRow(rowId, newRowNodeMap); topParentRowNode && topLevelRowNodeFromAdd.push(topParentRowNode); } for (let rowId of newDeleteRowIds) { const oldTopParentRowNode = getTopLevelRow(rowId, oldRowNodeMap); oldTopParentRowNode && topLevelRowNodeFromOldDelete.push(oldTopParentRowNode); const topParentRowNode = oldTopParentRowNode && newRowNodeMap.get(oldTopParentRowNode.id); if (topParentRowNode) { topLevelRowNodeFromDelete.push(topParentRowNode); } } const needFreshTopNodes = topLevelRowNodeFromAdd.concat(topLevelRowNodeFromDelete); refreshRowNodeDate(needFreshTopNodes); ganttViewRef.value && ganttViewRef.value.updateMinAndMaxDateByChangeRowNode( { addedRowNodes: topLevelRowNodeFromAdd, deletedRowNodes: topLevelRowNodeFromOldDelete }, firstLevelRowNode.value ); const allNeedFreshCellNodes = getAllChildren(needFreshTopNodes); ganttViewRef.value && ganttViewRef.value.freshTimeLines(needFreshTopNodes); refreshCells(allNeedFreshCellNodes.map((item) => item.id), true); const displayRows = getDisplayRows(); if (displayRows) { onViewPortChanged(displayRows); } }; const getTopLevelRow = (rowId, currentRowNodeMap) => { let currentRowNode = currentRowNodeMap.get(rowId); while (currentRowNode == null ? void 0 : currentRowNode.parentId) { currentRowNode = currentRowNodeMap.get(currentRowNode.parentId); } return currentRowNode; }; vue.provide( "getTopLevelRow", getTopLevelRow ); const freshRowNodes = (rows2) => { const needUpdateTopRowNodes = /* @__PURE__ */ new Set(); const rowIds = /* @__PURE__ */ new Set(); for (let row of rows2) { const startDateArr = []; const endDateArr = []; const id = getRowId(row); const currentRowNode = rowNodeMap.value.get(id); if (!currentRowNode) continue; rowIds.add(id); const hasChildren = currentRowNode.hasChildren; if (!hasChildren && row.timeLines) { for (let timeLine of row.timeLines) { startDateArr.push(dayjs(timeLine.startDate)); endDateArr.push(dayjs(timeLine.endDate)); } const oldStartDate = currentRowNode.startDate; const oldEndDate = currentRowNode.endDate; Object.assign(currentRowNode, { startDate: dayjs.min(startDateArr), endDate: dayjs.max(endDateArr), oldStartDate, oldEndDate }); const topLevelRowNode = getTopLevelRow(currentRowNode.id, rowNodeMap.value); topLevelRowNode && needUpdateTopRowNodes.add(topLevelRowNode); } } const needUpdateTopRowNodeList = [...needUpdateTopRowNodes]; refreshRowNodeDate(needUpdateTopRowNodeList); ganttViewRef.value && ganttViewRef.value.freshTimeLines(needUpdateTopRowNodeList); ganttViewRef.value && ganttViewRef.value.updateMinAndMaxDateByChangeRowNode({ updatedRowNodes: needUpdateTopRowNodeList }, firstLevelRowNode.value); refreshCells([...rowIds], true); }; const refreshRowNodeDate = (rowNodes) => { for (let rowNode of rowNodes) { const startDateArr = []; const endDateArr = []; const hasChildren = rowNode.hasChildren; if (hasChildren) { const children = rowNode.children || []; refreshRowNodeDate(children); for (let child of children) { child.startDate && startDateArr.push(child.startDate); child.endDate && endDateArr.push(child.endDate); } const oldStartDate = rowNode.startDate; const oldEndDate = rowNode.endDate; Object.assign(rowNode, { startDate: dayjs.min(startDateArr), endDate: dayjs.max(endDateArr), oldStartDate, oldEndDate }); } } }; const freshRowNodeDateByTimeLine = (rowId) => { const currentRowNode = rowNodeMap.value.get(rowId); const startDateArr = []; const endDateArr = []; if (currentRowNode == null ? void 0 : currentRowNode.timeLineNodes) { for (let timeLineNode of currentRowNode.timeLineNodes) { startDateArr.push(timeLineNode.startDate); endDateArr.push(timeLineNode.endDate); } const oldStartDate = currentRowNode.startDate; const oldEndDate = currentRowNode.endDate; Object.assign(currentRowNode, { startDate: dayjs.min(startDateArr), endDate: dayjs.max(endDateArr), oldStartDate, oldEndDate }); refreshRowNodeDate([getTopLevelRow(rowId, rowNodeMap.value)]); } }; vue.provide( "freshRowNodeDateByTimeLine", freshRowNodeDateByTimeLine ); const getAllChildren = (rowNodes) => { const result = []; common.treeForEachSkipChildren(rowNodes, (currentRowNode) => { result.push(currentRowNode); }); return result; }; const getDisplayRows = () => { var _a, _b; const firstRowIndex = (_a = tableViewRef.value) == null ? void 0 : _a.getFirstDisplayedRow(); const lastRowIndex = (_b = tableViewRef.value) == null ? void 0 : _b.getLastDisplayedRow(); if (firstRowIndex !== void 0 && lastRowIndex !== void 0) { return rowDataList.value.slice(firstRowIndex, lastRowIndex + 1); } else { return null; } }; const getRowNode = (id) => { return rowNodeMap.value.get(id); }; const getRowNodeChildren = (parentId) => { var _a; if (parentId) { return ((_a = rowNodeMap.value.get(parentId)) == null ? void 0 : _a.children) || []; } else { return firstLevelRowNode.value; } }; const getRowDataList = () => { return rowDataList.value; }; return { rowNodeMap, rowNodeIds, visibleRowIds, rowDataList, firstLevelRowNode, getRowNode, getRowNodeChildren, getRowDataList, freshRowNodes, getDisplayRows }; }; exports.useGanttRowNode = useGanttRowNode; //# sourceMappingURL=useGanttRowNode.js.map