Fix drag-and-drop reordering

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl
2018-12-23 21:58:03 +01:00
parent acd85eb097
commit 950c6b9046

View File

@@ -25,16 +25,22 @@
<Controls :board="board" /> <Controls :board="board" />
<div v-if="board"> <div v-if="board">
<!-- example for external drop zone --> <!-- example for external drop zone -->
<container :should-accept-drop="() => true" style="border:1px solid #aaa;" /> <!-- <container :should-accept-drop="() => true" style="border:1px solid #aaa;" /> -->
<button @click="toggleSidebar">toggle sidebar</button>
<container lock-axix="y" orientation="horizontal" @drop="onDropStack"> <container lock-axix="y" orientation="horizontal" @drop="onDropStack">
<draggable v-for="stack in stacks" :key="stack.id" class="stack"> <draggable v-for="stack in stacks" :key="stack.id" class="stack">
<h3>{{ stack.title }}</h3> <h3>{{ stack.title }}</h3>
<Container group-name="stack"> <container :get-child-payload="payload(stack.id)" group-name="stack" @drop="($event) => onDropCard(stack.id, $event)">
<Draggable v-for="card in stack.cards"><card :id="card" @drop="onDropCard" /></Draggable> <draggable v-for="card in stack.cards" :key="card.id">
</Container> <card-item :id="card.id" />
</draggable> </draggable>
</container> </container>
</draggable>
</container>
</div>
<div v-else class="emptycontent">
<div class="icon icon-deck"></div>
<h2>{{ t('deck', 'Board not found')}}</h2>
<p></p>
</div> </div>
</div> </div>
</template> </template>
@@ -44,7 +50,7 @@
import { Container, Draggable } from 'vue-smooth-dnd' import { Container, Draggable } from 'vue-smooth-dnd'
import { mapState } from 'vuex' import { mapState } from 'vuex'
import Controls from '../Controls' import Controls from '../Controls'
import Card from '../card/Card' import CardItem from '../cards/CardItem'
const applyDrag = (arr, dragResult) => { const applyDrag = (arr, dragResult) => {
const { removedIndex, addedIndex, payload } = dragResult const { removedIndex, addedIndex, payload } = dragResult
@@ -68,14 +74,15 @@ const dummyCard = function(i) {
return { return {
id: i, id: i,
order: 0, order: 0,
title: 'card ' + i title: 'card ' + i,
stackId: 1
} }
} }
export default { export default {
name: 'Board', name: 'Board',
components: { components: {
Card, CardItem,
Controls, Controls,
Container, Container,
Draggable Draggable
@@ -115,11 +122,26 @@ export default {
}) })
}, },
methods: { methods: {
toggleSidebar: function() {
this.$store.dispatch('toggleSidebar')
},
onDropStack(dropResult) { onDropStack(dropResult) {
// TODO: persist new order in order field
this.stacks = applyDrag(this.stacks, dropResult) this.stacks = applyDrag(this.stacks, dropResult)
},
onDropCard(stackId, dropResult) {
if (dropResult.removedIndex !== null || dropResult.addedIndex !== null) {
// TODO: persist new order in order field
const stacks = this.stacks
const stack = stacks.filter(p => p.id === stackId)[0]
const stackIndex = stacks.indexOf(stack)
const newStack = Object.assign({}, stack)
newStack.cards = applyDrag(newStack.cards, dropResult)
stacks.splice(stackIndex, 1, newStack)
this.stacks = stacks
}
},
payload(stackId) {
return index => {
return this.stacks.find(stack => stack.id === stackId).cards[index]
}
} }
} }
} }
@@ -127,4 +149,17 @@ export default {
<style scoped> <style scoped>
.smooth-dnd-container.vertical {
display: flex;
flex-direction: column;
}
.smooth-dnd-container.vertical > .smooth-dnd-draggable-wrapper {
overflow: initial;
}
.smooth-dnd-container.vertical .smooth-dnd-draggable-wrapper {
height: auto;
}
</style> </style>