ANovokmet / svelte-gantt

:calendar: Interactive JavaScript Gantt chart/resource booking component

Home Page:https://anovokmet.github.io/svelte-gantt/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Drag task by unit (f.e. day)

martinfruehmorgen opened this issue · comments

Hello,
Perhaps I do not understand the magnetUnit fully.
What I try to achieve is:
I have a task, which has startDate:
2023-02-03 09:00
and I want to drag it one day earlier, so that the startDate will be
2023-02-02 09:00

Is there any way to do this? When I use the magnetUnit day, the startTime of the event is changed to 00:00.

Thanks!
Martin

magnetUnit works in a way it rounds the times down to the start of the nearest unit, so it will always be 00:00 for days, 1:00, 2:00... for hours, etc. To achieve what you need you could use task drag events to round its start date to 9:00, something like this:

gantt.api.tasks.on.change(([event])=> {
    const { task } = event;
    task.model.from = moment(task.model.from).set('hour', 9).valueOf();
    gantt.updateTask(task.model);
}),

I haven't tried this code so if it does not work I'll try to think of alternatives.

Thanks - that does not work unfortunately.
The model of the task is updated with the hours set by the function, but the task is not visually updated, this means it stays at the same hour where it was dragged.
Also it would be great to have in the return value of the change/changed event the old task.
What I need to achieve is to preserve the hours/mins from the old task and shift it only f.e. by a day.

I have tried the code, it did not work. Wrapping the update statement in a setTimeout call does:

        gantt.api.tasks.on.changed(([event])=> {
            console.log('gantt.api.tasks.on.change', event);
            const { task } = event;
            task.model.from = moment(task.model.from).set('hour', 9).set('minute', 0).valueOf();
            setTimeout(() => {
                gantt.updateTask(task.model);
            });
        });

Returning the old task state in the change event does make sense, will add it. Until then, keeping track of old states of tasks separately should not be too hard.

Thank you - that works!