From 91095287bded8318b942054e82a0fe4643bb4973 Mon Sep 17 00:00:00 2001 From: Wahyu Ramadhan Date: Tue, 8 Aug 2023 09:48:54 +0700 Subject: [PATCH] View mode scroll fix --- view-mode/function/function.js | 30 ++++++++++++++++++++++++++ view-mode/function/ganttEvent.js | 30 ++++++++++++++++++++++++++ view-mode/function/restActivityLink.js | 6 ++++++ 3 files changed, 66 insertions(+) diff --git a/view-mode/function/function.js b/view-mode/function/function.js index 35f4d40..07ac9dc 100644 --- a/view-mode/function/function.js +++ b/view-mode/function/function.js @@ -522,3 +522,33 @@ function replaceAll(str, find, replace) { } return str } + +// Function to get the earliest planned start date among all tasks +function getEarliestTaskDate() { + var earliestDate = null; + gantt.eachTask(function (task) { + var taskStartDate = task.planned_start || task.start_date; + if (!earliestDate || task.planned_start < earliestDate) { + earliestDate = taskStartDate; + if (task.start_date < earliestDate) { + earliestDate = task.start_date; + } + } + }); + return earliestDate; +} + +// Function to get the latest planned end date among all tasks +function getLatestTaskDate() { + var latestDate = null; + gantt.eachTask(function (task) { + var taskEndDate = task.planned_end || task.end_date; + if (!latestDate || task.planned_end > latestDate) { + latestDate = taskEndDate; + if (task.end_date > latestDate) { + latestDate = task.end_date; + } + } + }); + return latestDate; +} \ No newline at end of file diff --git a/view-mode/function/ganttEvent.js b/view-mode/function/ganttEvent.js index 92c72fd..64aabb7 100644 --- a/view-mode/function/ganttEvent.js +++ b/view-mode/function/ganttEvent.js @@ -97,4 +97,34 @@ gantt.attachEvent("onGanttRender", function(){ }else{ gantt.config.readonly = true; } +}); + +gantt.attachEvent("onGanttScroll", function (left, top){ + var left_date = gantt.dateFromPos(left) + var right_date = gantt.dateFromPos(left + gantt.$task.offsetWidth) + + let taskCount = gantt.getTaskCount(); + if (taskCount > 0) { + gantt.config.start_date = gantt.config.start_date || gantt.getState().min_date; + gantt.config.end_date = gantt.config.end_date || gantt.getState().max_date; + + var min_allowed_date = gantt.date.add(gantt.config.start_date, 1, "day"); + var max_allowed_date = gantt.date.add(gantt.config.end_date, -1, "day"); + + var repaint = false; + if (earliest && +left_date <= +min_allowed_date && +left_date >= +earliest){ + gantt.config.start_date = gantt.date.add(gantt.config.start_date, -1, "day"); + repaint = true; + } + if (latest && +right_date >= +max_allowed_date && +right_date <= +latest){ + gantt.config.end_date = gantt.date.add(gantt.config.end_date, 1, "day"); + repaint = true; + } + + if (repaint) { + setTimeout(function(){ + gantt.render() + },20) + } +} }); \ No newline at end of file diff --git a/view-mode/function/restActivityLink.js b/view-mode/function/restActivityLink.js index bca311a..d16c69d 100644 --- a/view-mode/function/restActivityLink.js +++ b/view-mode/function/restActivityLink.js @@ -1,4 +1,6 @@ var parts = []; +let latest; +let earliest; window.parent.postMessage({ action: 'getUrl' }, '*'); window.addEventListener('message', function(event) { if (event.data && event.data.action === 'sendUrl') { @@ -40,6 +42,8 @@ gantt.ajax.get({ if(!parts[2]){ gantt.silent(function () { gantt.parse(data); + earliest = getEarliestTaskDate(); + latest = getLatestTaskDate(); }); } }); @@ -62,6 +66,8 @@ function refresData(id){ if(!parts[2]){ gantt.silent(function () { gantt.parse(data); + earliest = getEarliestTaskDate(); + latest = getLatestTaskDate(); }); } if(id){