Skip to content

Commit c8d2ca7

Browse files
committed
refactor(gantt): improve card clicked detection logic
Signed-off-by: grnd-alt <git@belakkaf.net>
1 parent 3d3a95a commit c8d2ca7

1 file changed

Lines changed: 32 additions & 37 deletions

File tree

src/components/board/GanttView.vue

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,11 @@ const GANTT_VIEW_MODES = [
167167
export default {
168168
name: 'GanttView',
169169
components: {
170-
NcButton,
171-
NcEmptyContent,
172-
ChartGanttIcon,
173-
ChevronDown,
174-
ChevronRight,
170+
NcButton,
171+
NcEmptyContent,
172+
ChartGanttIcon,
173+
ChevronDown,
174+
ChevronRight,
175175
},
176176
props: {
177177
board: {
@@ -187,7 +187,7 @@ export default {
187187
return {
188188
ganttInstance: null,
189189
currentViewMode: 'Day',
190-
isDragging: false,
190+
pendingChange: null,
191191
showUndated: false,
192192
viewModes: [
193193
{ value: 'Hour', label: t('deck', 'Hours') },
@@ -197,8 +197,8 @@ export default {
197197
],
198198
}
199199
},
200-
computed: {
201-
...mapGetters(['cardsByStack']),
200+
computed: {
201+
...mapGetters(['cardsByStack', 'canEdit']),
202202
partitionedCards() {
203203
const undatedCards = []
204204
const ganttTasks = []
@@ -228,7 +228,9 @@ export default {
228228
this.$nextTick(() => this.renderGantt())
229229
return
230230
}
231-
if (!this.isDragging && this.ganttInstance) {
231+
232+
// checking pendingChange to only refresh on updates not coming from the chart
233+
if (!this.pendingChange && this.ganttInstance) {
232234
const cloned = tasks.map(t => ({ ...t, start: new Date(t.start), end: new Date(t.end) }))
233235
this.ganttInstance.refresh(cloned)
234236
}
@@ -242,11 +244,21 @@ export default {
242244
},
243245
},
244246
mounted() {
245-
this._onMouseUp = () => {
246-
if (this._pendingChange) {
247-
const { task, start, end } = this._pendingChange
248-
this._pendingChange = null
249-
this.onDateChange(task, start, end)
247+
this._onMouseUp = async (event) => {
248+
if (this.pendingChange) {
249+
const { task, start, end } = this.pendingChange
250+
await this.updateTaskDate(task, start, end)
251+
this.pendingChange = null
252+
return
253+
}
254+
255+
const barWrapper = event.target.closest('.bar-wrapper')
256+
if (barWrapper) {
257+
const taskId = barWrapper.getAttribute('data-id')
258+
const task = this.ganttTasks.find(t => t.id === taskId)
259+
if (task) {
260+
this.openCard(task._card)
261+
}
250262
}
251263
}
252264
document.addEventListener('mouseup', this._onMouseUp)
@@ -323,22 +335,21 @@ export default {
323335
today_button: true,
324336
infinite_padding: false,
325337
readonly_progress: true,
338+
readonly: !this.canEdit,
326339
popup: false,
327-
on_click: (task) => {
328-
if (!this.isDragging) {
329-
this.openCard(task._card)
330-
}
331-
},
332340
on_date_change: (task, start, end) => {
333-
this.isDragging = true
334-
this._pendingChange = { task, start, end }
341+
this.pendingChange = { task, start, end }
335342
},
336343
})
337344
338345
this._patchBarDuration()
339346
this.ganttInstance.change_view_mode(this.currentViewMode)
340347
this.fitColumnsToWidth()
341348
},
349+
async updateTaskDate(task, start, end) {
350+
await this.$store.dispatch('updateCardDue', { ...task._card, duedate: new Date(end).toISOString() })
351+
await this.$store.dispatch('updateCardStartDate', { ...task._card, startdate: new Date(start).toISOString() })
352+
},
342353
_patchBarDuration() {
343354
const bars = this.ganttInstance?.bars
344355
if (!bars?.length) return
@@ -378,22 +389,6 @@ export default {
378389
gantt.change_view_mode(gantt.config.view_mode.name, true)
379390
}
380391
},
381-
async onDateChange(task, start, end) {
382-
const card = task._card
383-
if (!card) return
384-
try {
385-
await this.$store.dispatch('updateCardStartDate', {
386-
...card,
387-
startdate: new Date(start).toISOString(),
388-
})
389-
await this.$store.dispatch('updateCardDue', {
390-
...card,
391-
duedate: new Date(end).toISOString(),
392-
})
393-
} finally {
394-
this.isDragging = false
395-
}
396-
},
397392
},
398393
}
399394
</script>

0 commit comments

Comments
 (0)