Files
im-uniapp/components/Gantt/core/DataManager.js
2025-07-16 14:23:42 +08:00

43 lines
1.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// DataManager任务数据管理器负责任务的增删改查、分组、校验和变更通知
export default class DataManager {
constructor(tasks = [], dimensions = []) {
this.tasks = tasks;
this.dimensions = dimensions;
this.listeners = [];
}
// 新增任务
addTask(task) {
this.tasks.push(task);
this.notify();
}
// 更新任务
updateTask(id, data) {
const idx = this.tasks.findIndex(t => t.id === id);
if (idx !== -1) {
this.tasks[idx] = { ...this.tasks[idx], ...data };
this.notify();
}
}
// 删除任务
removeTask(id) {
this.tasks = this.tasks.filter(t => t.id !== id);
this.notify();
}
// 按维度分组
groupByDimension(dim) {
const groups = {};
this.tasks.forEach(task => {
const key = task.dimensions && task.dimensions[dim] ? task.dimensions[dim].id : '未分组';
if (!groups[key]) groups[key] = { id: key, name: task.dimensions && task.dimensions[dim] ? task.dimensions[dim].name : '未分组', tasks: [] };
groups[key].tasks.push(task);
});
return Object.values(groups);
}
// 变更监听
onChange(cb) {
this.listeners.push(cb);
}
notify() {
this.listeners.forEach(cb => cb(this.tasks));
}
}