feat: allow duration instead of end_time

This commit is contained in:
Safwan Samsudeen 2024-04-05 14:19:19 +05:30
parent 7670d13634
commit 0f15ab1875
3 changed files with 40 additions and 9 deletions

View File

@ -42,7 +42,7 @@
}, },
{ {
start: '2018-10-03', start: '2018-10-03',
end: '2018-10-06', duration: '1m 4d',
name: 'Write new content', name: 'Write new content',
id: "Task 1", id: "Task 1",
progress: 5, progress: 5,

View File

@ -7,6 +7,28 @@ const SECOND = 'second';
const MILLISECOND = 'millisecond'; const MILLISECOND = 'millisecond';
export default { export default {
parse_duration(duration) {
const regex = /([0-9])+(y|m|d|h|min|s|ms)/gm;
const matches = regex.exec(duration);
if (matches !== null) {
if (matches[2] === "y") {
return { duration: parseInt(matches[1]), scale: `year` };
} else if (matches[2] === "m") {
return { duration: parseInt(matches[1]), scale: `month` };
} else if (matches[2] === "d") {
return { duration: parseInt(matches[1]), scale: `day` };
} else if (matches[2] === "h") {
return { duration: parseInt(matches[1]), scale: `hour` };
} else if (matches[2] === "min") {
return { duration: parseInt(matches[1]), scale: `minute` };
} else if (matches[2] === "s") {
return { duration: parseInt(matches[1]), scale: `second` };
} else if (matches[2] === "ms") {
return { duration: parseInt(matches[1]), scale: `millisecond` };
}
}
},
parse(date, date_separator = '-', time_separator = /[.:]/) { parse(date, date_separator = '-', time_separator = /[.:]/) {
if (date instanceof Date) { if (date instanceof Date) {
return date; return date;

View File

@ -96,6 +96,15 @@ export default class Gantt {
this.tasks = tasks.map((task, i) => { this.tasks = tasks.map((task, i) => {
// convert to Date objects // convert to Date objects
task._start = date_utils.parse(task.start); task._start = date_utils.parse(task.start);
if (task.end === undefined && task.duration !== undefined) {
task.end = task._start;
let durations = task.duration.split(" ");
durations.forEach(tmpDuration => {
let { duration, scale } = date_utils.parse_duration(tmpDuration);
task.end = date_utils.add(task.end, duration, scale);
})
}
task._end = date_utils.parse(task.end); task._end = date_utils.parse(task.end);
// make task invalid if duration too large // make task invalid if duration too large