Make checklist entries sortable by d&d
Some checks failed
/ Run tests (push) Failing after 15s
/ Run system tests (push) Failing after 14s
/ Build, push and deploy image (push) Has been skipped

This commit is contained in:
david 2024-10-27 22:37:11 +01:00
parent 1e1d80a2c3
commit 7acc0559ae
10 changed files with 98 additions and 20 deletions

View file

@ -0,0 +1,38 @@
import { Controller } from "@hotwired/stimulus"
import Sortable from "sortablejs"
import { put } from "@rails/request.js";
// Connects to data-controller="drag"
export default class extends Controller {
static values = {
group: String
}
connect() {
this.element.style.cursor = "grab"
new Sortable(this.element, {
group: this.groupValue,
onEnd: this.onEnd,
animation: 150,
swap: true,
swapThreshold: 0.65,
ghostClass: 'dragFrom',
swapClass: 'dragTo',
forceFallback: false,
})
}
onEnd(event) {
const position = event.newIndex + 1
const url = event.item.dataset["sortableUrl"]
// Expect backend to update list items via turbo if necessary
put(url, {
body: JSON.stringify({ checklist_entry: { position: position }}),
contentType: "application/json",
headers: {
"Accept": "text/vnd.turbo-stream.html, text/html, application/xhtml+xml"
}
})
}
}