From 2a0fa285334f0a8b60fc1eafd8779c590c026fa4 Mon Sep 17 00:00:00 2001 From: Marko Zunic Date: Fri, 27 Mar 2020 14:32:56 +0100 Subject: [PATCH] New prop for g-gantt-chart: snap-back-on-overlap If true (and if push-on-overlap is false), if a bar ends up overlapping with another bar after a drag, the bar (or its whole bundle, if it belongs to one) snaps back to its original position Added .npmignore Bumped npm package version Updated README --- .npmignore | 23 ++++++ README.md | 12 +++ package-lock.json | 4 +- package.json | 6 +- src/GGanttBar.vue | 11 ++- src/GGanttChart.vue | 30 +++++++- src/Playground.vue | 176 ++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 254 insertions(+), 8 deletions(-) create mode 100644 .npmignore create mode 100644 src/Playground.vue diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..b7980dd --- /dev/null +++ b/.npmignore @@ -0,0 +1,23 @@ +.DS_Store +node_modules +/dist +src/Playground.vue + + +# local env files +.env.local +.env.*.local + +# Log files +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# Editor directories and files +.idea +.vscode +*.suo +*.ntvs* +*.njsproj +*.sln +*.sw? \ No newline at end of file diff --git a/README.md b/README.md index ec78207..e109381 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,18 @@ export default { ## Contributing Pull requests are warmly welcomed, while every major change or proposal ought to be discussed in an issue first. As the project is still new, I will gladly accept suggestions, proposals, contributions etc. +### Contributing - How to run the project + 1. Clone the project + 2. Install the Vue CLI service, if you don't already have it installed: + ``` + npm install -g @vue/cli + npm install -g @vue/cli-service-global + ``` + 3. Playground.vue is a dedicated Vue SFC where all Vue-Ganttastic components can be + played around with and tested out. Get it running using: + ``` + vue serve src/Playground.vue + ``` ## Dependencies [Moment.js](https://momentjs.com/) diff --git a/package-lock.json b/package-lock.json index f7de915..f13e818 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { - "name": "vue-ganttastic-beta", - "version": "0.9.1", + "name": "vue-ganttastic", + "version": "0.9.4", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 875b3a2..6b031a0 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,11 @@ { "name": "vue-ganttastic", - "version": "0.9.4", + "version": "0.9.5", "description": "A simple and customizable Gantt chart component for Vue.js", + "repository": { + "type": "git", + "url": "https://github.com/InfectoOne/vue-ganttastic" + }, "main": "./dist/index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", diff --git a/src/GGanttBar.vue b/src/GGanttBar.vue index 448fccc..f5b9b78 100644 --- a/src/GGanttBar.vue +++ b/src/GGanttBar.vue @@ -78,7 +78,9 @@ export default { // or is it dragged along some other bar from the same bundle cursorOffsetX: 0, mousemoveCallback: null, // gets initialized when starting to drag - // possible values: drag, dragByHandleLeft, dragByHandleRight + // possible values: drag, dragByHandleLeft, dragByHandleRight, + barStartBeforeDrag: null, + barEndBeforeDrag: null, } }, @@ -178,6 +180,8 @@ export default { /* --------------------------------------------------------- */ initDrag(e){ // "e" must be the mousedown event this.isDragging = true + this.barStartBeforeDrag = this.bar[this.barStart] + this.barEndBeforeDrag = this.bar[this.barEnd] let barX = this.$refs["g-gantt-bar"].getBoundingClientRect().left this.cursorOffsetX = e.clientX - barX let mousedownType = e.target.className @@ -252,6 +256,11 @@ export default { } }, + snapBack(){ + this.barStartMoment = this.barStartBeforeDrag + this.barEndMoment = this.barEndBeforeDrag + }, + manageOverlapping(){ if(!this.ganttChartProps.pushOnOverlap){ return diff --git a/src/GGanttChart.vue b/src/GGanttChart.vue index 6e81212..f9688b2 100644 --- a/src/GGanttChart.vue +++ b/src/GGanttChart.vue @@ -56,7 +56,8 @@ export default { grid: Boolean, highlightedHours: {type: Array, default: () => []}, width: {type: String, default: "100%"}, // the total width of the entire ganttastic component in % - pushOnOverlap: {type: Boolean} + pushOnOverlap: {type: Boolean}, + snapBackOnOverlap: {type: Boolean}, }, data(){ @@ -126,14 +127,33 @@ export default { }) }, + shouldSnapBackBar(ganttBar){ + if(this.snapBackOnOverlap){ + let {overlapBar} = ganttBar.getOverlapBarAndType(ganttBar.bar) + return !!overlapBar + } + return false + }, + + snapBackBundleIfNeeded(ganttBar){ + let barsFromBundle = this.getBarsFromBundle(ganttBar.barConfig.bundle) + if(this.shouldSnapBackBar(ganttBar) || barsFromBundle.some(gBar => this.shouldSnapBackBar(gBar))){ + ganttBar.snapBack() + barsFromBundle.forEach(gBar => gBar.snapBack()) + return true + } + return false + }, + onBarEvent(e, ganttBar){ this.$emit(`${e.type}-bar`, {event: e, bar: ganttBar.bar}) }, onDragendBar(e, ganttBar){ - let movedBarsInDrag = this.movedBarsInDrag + let didSnapBack = this.snapBackBundleIfNeeded(ganttBar) + let movedBars = didSnapBack ? new Set() : this.movedBarsInDrag this.movedBarsInDrag = new Set() - this.$emit("dragend-bar", {event: e, bar: ganttBar.bar, movedBars: movedBarsInDrag}) + this.$emit("dragend-bar", {event: e, bar: ganttBar.bar, movedBars}) }, // ------------------------------------------------------------------------ @@ -260,7 +280,9 @@ export default { moveBarsFromBundleOfPushedBar: (bar, minuteDiff, overlapType) => this.moveBarsFromBundleOfPushedBar(bar, minuteDiff, overlapType), setDragLimitsOfGanttBar : (ganttBar) => this.setDragLimitsOfGanttBar(ganttBar), onBarEvent: (e, ganttBar) => this.onBarEvent(e, ganttBar), - onDragendBar: (e, ganttBar) => this.onDragendBar(e, ganttBar) + onDragendBar: (e, ganttBar) => this.onDragendBar(e, ganttBar), + shouldSnapBackOnOverlap: () => this.snapBackOnOverlap, + snapBackBundle: (ganttBar) => this.snapBackBundle(ganttBar) } } } diff --git a/src/Playground.vue b/src/Playground.vue new file mode 100644 index 0000000..bbb9bf8 --- /dev/null +++ b/src/Playground.vue @@ -0,0 +1,176 @@ + + + + + + \ No newline at end of file