2
0
mirror of https://github.com/tenrok/vue-ganttastic.git synced 2026-06-24 08:10:33 +03:00

fix: unexpected mutation of "bars" prop

This commit is contained in:
2021-10-27 16:17:06 +03:00
parent e1d411e7cd
commit ea7cc64157
5 changed files with 50 additions and 34 deletions
+34 -23
View File
@@ -12,7 +12,7 @@
@contextmenu="onContextmenu($event)" @contextmenu="onContextmenu($event)"
> >
<div class="g-gantt-bar-label"> <div class="g-gantt-bar-label">
<slot name="bar-label" :bar="bar"> <slot name="bar-label" :bar="localBar">
{{ barConfig.label || '' }} {{ barConfig.label || '' }}
</slot> </slot>
</div> </div>
@@ -36,7 +36,7 @@
}" }"
/> />
<div> <div>
<div>{{ bar.tooltip }}</div> <div>{{ localBar.tooltip }}</div>
<div>{{ barStartText }} - {{ barEndText }}</div> <div>{{ barStartText }} - {{ barEndText }}</div>
</div> </div>
</div> </div>
@@ -79,7 +79,8 @@ export default {
cursorOffsetX: 0, cursorOffsetX: 0,
mousemoveCallback: null, // gets initialized when starting to drag, possible values: drag, dragByHandleLeft, dragByHandleRight, mousemoveCallback: null, // gets initialized when starting to drag, possible values: drag, dragByHandleLeft, dragByHandleRight,
barStartBeforeDrag: null, barStartBeforeDrag: null,
barEndBeforeDrag: null barEndBeforeDrag: null,
localBar: this.bar
} }
}, },
@@ -128,19 +129,19 @@ export default {
barStartMoment: { barStartMoment: {
get() { get() {
return moment(this.bar[this.barStartKey], this.timeFormat) return moment(this.localBar[this.barStartKey], this.timeFormat)
}, },
set(value) { set(value) {
this.bar[this.barStartKey] = value.format(this.timeFormat) this.localBar[this.barStartKey] = value.format(this.timeFormat)
} }
}, },
barEndMoment: { barEndMoment: {
get() { get() {
return moment(this.bar[this.barEndKey]) return moment(this.localBar[this.barEndKey])
}, },
set(value) { set(value) {
this.bar[this.barEndKey] = value.format(this.timeFormat) this.localBar[this.barEndKey] = value.format(this.timeFormat)
} }
}, },
@@ -158,16 +159,16 @@ export default {
}, },
barConfig() { barConfig() {
if (this.bar[this.barConfigKey]) { if (this.localBar[this.barConfigKey]) {
return { return {
...this.bar[this.barConfigKey], ...this.localBar[this.barConfigKey],
background: this.bar[this.barConfigKey].isShadow background: this.localBar[this.barConfigKey].isShadow
? 'grey' ? 'grey'
: this.bar[this.barConfigKey].background || : this.localBar[this.barConfigKey].background ||
this.bar[this.barConfigKey].backgroundColor, this.localBar[this.barConfigKey].backgroundColor,
opacity: this.bar[this.barConfigKey].isShadow opacity: this.localBar[this.barConfigKey].isShadow
? '0.3' ? '0.3'
: this.bar[this.barConfigKey].opacity : this.localBar[this.barConfigKey].opacity
} }
} }
return {} return {}
@@ -203,6 +204,12 @@ export default {
} }
}, },
watch: {
bar(value) {
this.localBar = value
}
},
methods: { methods: {
onMouseenter(e) { onMouseenter(e) {
if (this.tooltipTimeout) { if (this.tooltipTimeout) {
@@ -277,8 +284,8 @@ export default {
initDrag(e) { initDrag(e) {
// "e" must be the mousedown event // "e" must be the mousedown event
this.isDragging = true this.isDragging = true
this.barStartBeforeDrag = this.bar[this.barStartKey] this.barStartBeforeDrag = this.localBar[this.barStartKey]
this.barEndBeforeDrag = this.bar[this.barEndKey] this.barEndBeforeDrag = this.localBar[this.barEndKey]
let barX = this.$refs['g-gantt-bar'].getBoundingClientRect().left let barX = this.$refs['g-gantt-bar'].getBoundingClientRect().left
this.cursorOffsetX = e.clientX - barX this.cursorOffsetX = e.clientX - barX
@@ -377,22 +384,26 @@ export default {
} }
// if ( // if (
// moment(this.bar[this.barStartKey]).isAfter(this.barStartBeforeDrag) && // moment(this.localBar[this.barStartKey]).isAfter(this.barStartBeforeDrag) &&
// moment(this.bar[this.barStartKey]) // moment(this.localBar[this.barStartKey])
// .add(1, this.timeUnit) // .add(1, this.timeUnit)
// .isAfter(this.bar[this.barEndBeforeDrag]) // .isAfter(this.localBar[this.barEndBeforeDrag])
// ) { // ) {
// return true // return true
// } // }
const isSqueezeToLeft = const isSqueezeToLeft =
newXStart && newXStart &&
moment(this.bar[this.barStartKey]).isBefore(this.barStartBeforeDrag) moment(this.localBar[this.barStartKey]).isBefore(
this.barStartBeforeDrag
)
const isSqueezeToRight = const isSqueezeToRight =
newXEnd && newXEnd &&
moment(this.bar[this.barEndKey]).isAfter(this.barEndBeforeDrag) moment(this.localBar[this.barEndKey]).isAfter(this.barEndBeforeDrag)
const currentIndex = this.allBarsInRow.findIndex(bar => bar == this.bar) const currentIndex = this.allBarsInRow.findIndex(
bar => bar == this.localBar
)
let otherBars = [] let otherBars = []
if (isSqueezeToRight) { if (isSqueezeToRight) {
@@ -460,7 +471,7 @@ export default {
) { ) {
return return
} }
let currentBar = this.bar let currentBar = this.localBar
let { overlapBar, overlapType } = this.getOverlapBarAndType(currentBar) let { overlapBar, overlapType } = this.getOverlapBarAndType(currentBar)
while (overlapBar) { while (overlapBar) {
let minuteDiff let minuteDiff
+11 -6
View File
@@ -21,12 +21,12 @@
@mouseleave="onMouseleave()" @mouseleave="onMouseleave()"
> >
<g-gantt-bar <g-gantt-bar
v-for="(bar, index) in bars" v-for="(bar, index) in localBars"
:key="`ganttastic_bar_${index}`" :key="`ganttastic_bar_${index}`"
ref="ganttBar" ref="ganttBar"
:bar="bar" :bar="bar"
:bar-container="barContainer" :bar-container="barContainer"
:all-bars-in-row="bars" :all-bars-in-row="localBars"
> >
<template #bar-label="{ bar }"> <template #bar-label="{ bar }">
<slot name="bar-label" :bar="bar" /> <slot name="bar-label" :bar="bar" />
@@ -63,7 +63,8 @@ export default {
data() { data() {
return { return {
barContainer: {} barContainer: {},
localBars: this.bars
} }
}, },
@@ -126,6 +127,10 @@ export default {
'chartProps.gridSize'() { 'chartProps.gridSize'() {
this.barContainer = this.$refs.barContainer.getBoundingClientRect() this.barContainer = this.$refs.barContainer.getBoundingClientRect()
},
bars(value) {
this.localBars = value
} }
}, },
@@ -156,7 +161,7 @@ export default {
let xPos = e.clientX - barContainer.left let xPos = e.clientX - barContainer.left
let timeDiffFromStart = (xPos / barContainer.width) * this.timeCount let timeDiffFromStart = (xPos / barContainer.width) * this.timeCount
let time = moment(this.chartStart).add(timeDiffFromStart, this.timeUnit) let time = moment(this.chartStart).add(timeDiffFromStart, this.timeUnit)
let bar = this.bars.find(bar => let bar = this.localBars.find(bar =>
time.isBetween( time.isBetween(
bar[this.chartProps.barStartKey], bar[this.chartProps.barStartKey],
bar[this.chartProps.barEndKey] bar[this.chartProps.barEndKey]
@@ -180,8 +185,8 @@ export default {
.format() .format()
bar[this.barConfigKey] = { handles: true } bar[this.barConfigKey] = { handles: true }
this.bars.push(bar) this.localBars.push(bar)
this.bars.sort((first, second) => this.localBars.sort((first, second) =>
moment(first[this.chartProps.barStartKey]).diff( moment(first[this.chartProps.barStartKey]).diff(
second[this.chartProps.barStartKey] second[this.chartProps.barStartKey]
) )
+2 -2
View File
@@ -1,12 +1,12 @@
{ {
"name": "@tenrok/vue-ganttastic", "name": "@tenrok/vue-ganttastic",
"version": "0.10.16", "version": "0.10.17",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@tenrok/vue-ganttastic", "name": "@tenrok/vue-ganttastic",
"version": "0.10.16", "version": "0.10.17",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"vue": "^2.6.12" "vue": "^2.6.12"
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@tenrok/vue-ganttastic", "name": "@tenrok/vue-ganttastic",
"version": "0.10.16", "version": "0.10.17",
"description": "A simple and customizable Gantt chart component for Vue.js", "description": "A simple and customizable Gantt chart component for Vue.js",
"keywords": [ "keywords": [
"gantt", "gantt",
+2 -2
View File
@@ -336,8 +336,8 @@ export default {
methods: { methods: {
onDragend(e) { onDragend(e) {
let { event, bar } = e let { event, bar, movedBars } = e
console.log('onDragend', { event: event.type, bar }) console.log('onDragend', { event: event.type, bar, movedBars })
} }
} }
} }