mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-21 01:25:55 +02:00
Make URLs created by filter form a lot shorter
This commit is contained in:
parent
8f94a58c71
commit
87913ba3b5
5 changed files with 80 additions and 6 deletions
43
assets/controllers/helpers/form_cleanup_controller.js
Normal file
43
assets/controllers/helpers/form_cleanup_controller.js
Normal file
|
@ -0,0 +1,43 @@
|
|||
import {Controller} from "@hotwired/stimulus";
|
||||
|
||||
/**
|
||||
* Purpose of this controller is to clean up the form before it is finally submitted. This means empty fields get disabled, so they are not submitted.
|
||||
* This is especially useful for GET forms, to prevent very long URLs
|
||||
*/
|
||||
export default class extends Controller {
|
||||
|
||||
/**
|
||||
* Call during the submit event of the form. This will disable all empty fields, so they are not submitted.
|
||||
* @param event
|
||||
*/
|
||||
submit(event) {
|
||||
/** Find the form this event belongs to */
|
||||
/** @type {HTMLFormElement} */
|
||||
const form = event.target.closest('form');
|
||||
|
||||
for(const element of form.elements) {
|
||||
if(! element.value) {
|
||||
element.disabled = true;
|
||||
}
|
||||
|
||||
//Workaround for tristate checkboxes which use a hidden field to store the value
|
||||
if ((element.type === 'hidden' || element.type === 'checkbox') && element.value === 'indeterminate') {
|
||||
element.disabled = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Submits the form with all form elements disabled, so they are not submitted. This is useful for GET forms, to reset the form to not filled state.
|
||||
* @param event
|
||||
*/
|
||||
clearAll(event)
|
||||
{
|
||||
const form = event.target.closest('form');
|
||||
for(const element of form.elements) {
|
||||
element.disabled = true;
|
||||
}
|
||||
|
||||
form.submit();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue