Fixed tristate inputs (used for permissions input)

This commit is contained in:
Jan Böhmer 2022-07-24 21:12:11 +02:00
parent f06ad01eb2
commit 9ed487c629
7 changed files with 67 additions and 35 deletions

View file

@ -0,0 +1,51 @@
'use strict';
import "./lib/jquery.tristate"
const TristateHelper = class {
constructor() {
this.registerTriStateCheckboxes();
this.registerSubmitHandler();
}
registerSubmitHandler() {
document.addEventListener("turbo:submit-start", (e) => {
var form = e.detail.formSubmission.formElement;
var formData = e.detail.formSubmission.formData;
var $tristate_checkboxes = $('.tristate:checkbox', form);
//Iterate over each tristate checkbox in the form and set formData to the correct value
$tristate_checkboxes.each(function() {
var $checkbox = $(this);
var state = $checkbox.tristate('state');
formData.set($checkbox.attr('name'), state);
});
});
}
registerTriStateCheckboxes() {
//Initialize tristate checkboxes and if needed the multicheckbox functionality
document.addEventListener("turbo:load", () => {
$(".tristate").tristate( {
checked: "true",
unchecked: "false",
indeterminate: "indeterminate",
});
$('.permission_multicheckbox:checkbox').change(function() {
//Find the other checkboxes in this row, and change their value
var $row = $(this).parents('tr');
//@ts-ignore
var new_state = $(this).tristate('state');
//@ts-ignore
$('.tristate:checkbox', $row).tristate('state', new_state);
});
})
}
}
export default new TristateHelper();