add js cleanGetUrl

This commit is contained in:
Pieter Lambrecht 2022-09-27 02:10:08 +02:00
parent 23a686fe6c
commit 845e0c801d
7 changed files with 34 additions and 17 deletions

Binary file not shown.

Binary file not shown.

View File

@ -14,6 +14,9 @@ import { initRackElevation } from './racks';
import { initLinks } from './links';
import { initHtmx } from './htmx';
// @ts-ignore
import { cleanGetUrl } from './util';
function initDocument(): void {
for (const init of [
initBootstrap,

View File

@ -477,3 +477,31 @@ export function replaceAll(input: string, pattern: string | RegExp, replacement:
return input.replace(pattern, replacement);
}
/**
* Disable empty FormElemnts before submitting the form.
*
* @param targetform HTMLFormElement where the FormElements need to be disabled.
*/
export function cleanGetUrl(targetform: HTMLFormElement) {
var form_elements = targetform.elements;
for (const element of form_elements) {
// The SELECT statement requires a different approach. It depends on the selectedIndex, rather that the value.
switch (element.nodeName) {
case "SELECT":
const selectElement = element as HTMLSelectElement;
if (selectElement.selectedIndex == null || selectElement.selectedIndex == -1 || selectElement[selectElement.selectedIndex].getAttribute('value') == '') {
element.setAttribute('disabled','');
}
break;
default:
if (element.getAttribute('value') == null) {
element.setAttribute('disabled','');
}
}
}
}

View File

@ -1,21 +1,7 @@
{% load form_helpers %}
{% load helpers %}
<script>
function cleangeturl(form) {
var form_elements = form.elements;
for (var i=0; i<form_elements.length; i++ ) {
var element = form_elements[i]
if (element.name && element.value == '') {
element.disabled=true;
}
}
</script>
<form action="." method="get" onsubmit="return cleangeturl(this)">
<form action="." method="get" onsubmit="return cleanGetUrl(this)">
<div class="card">
<div class="card-body overflow-visible d-flex flex-wrap justify-content-between py-3">
{% for field in filter_form.hidden_fields %}

View File

@ -66,7 +66,7 @@
{% else %}
<div class="row">
<div class="col col-12 col-lg-6 offset-lg-3">
<form action="{% url 'search' %}" method="get" class="form form-horizontal">
<form action="{% url 'search' %}" method="get" class="form form-horizontal" onsubmit="return cleanGetUrl(this)">
<div class="card">
<h5 class="card-header">
Search

View File

@ -1,4 +1,4 @@
<form class="input-group" action="{% url 'search' %}" method="get">
<form class="input-group" action="{% url 'search' %}" method="get" onsubmit="return util.cleanGetUrl(this)">
<input
name="q"
type="text"