mirror of
https://github.com/netbox-community/netbox.git
synced 2025-12-12 03:19:36 -06:00
Remove extraneous TS comments
This commit is contained in:
parent
fd67acc3ab
commit
ad96fb3ab4
@ -13,31 +13,23 @@ const MODIFIER_EMPTY_FALSE = 'empty_false';
|
|||||||
*/
|
*/
|
||||||
export function initFilterModifiers(): void {
|
export function initFilterModifiers(): void {
|
||||||
for (const form of getElements<HTMLFormElement>('form')) {
|
for (const form of getElements<HTMLFormElement>('form')) {
|
||||||
// Only process forms with modifier selects
|
|
||||||
const modifierSelects = form.querySelectorAll<HTMLSelectElement>('.modifier-select');
|
const modifierSelects = form.querySelectorAll<HTMLSelectElement>('.modifier-select');
|
||||||
if (modifierSelects.length === 0) continue;
|
if (modifierSelects.length === 0) continue;
|
||||||
|
|
||||||
// Initialize form state from URL parameters
|
|
||||||
initializeFromURL(form);
|
initializeFromURL(form);
|
||||||
|
|
||||||
// Add change listeners to modifier dropdowns to handle isnull
|
|
||||||
modifierSelects.forEach(select => {
|
modifierSelects.forEach(select => {
|
||||||
select.addEventListener('change', () => handleModifierChange(select));
|
select.addEventListener('change', () => handleModifierChange(select));
|
||||||
// Trigger initial state
|
|
||||||
handleModifierChange(select);
|
handleModifierChange(select);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Handle form submission - must use submit event for GET forms
|
// Must use submit event for GET forms
|
||||||
form.addEventListener('submit', e => {
|
form.addEventListener('submit', e => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
// Build FormData to get all form values
|
|
||||||
const formData = new FormData(form);
|
const formData = new FormData(form);
|
||||||
|
|
||||||
// Transform field names
|
|
||||||
handleFormDataTransform(form, formData);
|
handleFormDataTransform(form, formData);
|
||||||
|
|
||||||
// Build URL with transformed parameters
|
|
||||||
const params = new URLSearchParams();
|
const params = new URLSearchParams();
|
||||||
for (const [key, value] of formData.entries()) {
|
for (const [key, value] of formData.entries()) {
|
||||||
if (value && String(value).trim()) {
|
if (value && String(value).trim()) {
|
||||||
@ -45,7 +37,6 @@ export function initFilterModifiers(): void {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Navigate to new URL
|
|
||||||
window.location.href = `${form.action}?${params.toString()}`;
|
window.location.href = `${form.action}?${params.toString()}`;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -69,11 +60,9 @@ function handleModifierChange(modifierSelect: HTMLSelectElement): void {
|
|||||||
|
|
||||||
const modifier = modifierSelect.value;
|
const modifier = modifierSelect.value;
|
||||||
|
|
||||||
// Disable and add placeholder for empty modifiers
|
|
||||||
if (modifier === MODIFIER_EMPTY_TRUE || modifier === MODIFIER_EMPTY_FALSE) {
|
if (modifier === MODIFIER_EMPTY_TRUE || modifier === MODIFIER_EMPTY_FALSE) {
|
||||||
valueInput.disabled = true;
|
valueInput.disabled = true;
|
||||||
valueInput.value = '';
|
valueInput.value = '';
|
||||||
// Get translatable placeholder from modifier dropdown's data attribute
|
|
||||||
const placeholder = modifierSelect.dataset.emptyPlaceholder || '(automatically set)';
|
const placeholder = modifierSelect.dataset.emptyPlaceholder || '(automatically set)';
|
||||||
valueInput.setAttribute('placeholder', placeholder);
|
valueInput.setAttribute('placeholder', placeholder);
|
||||||
} else {
|
} else {
|
||||||
@ -90,7 +79,6 @@ function handleFormDataTransform(form: HTMLFormElement, formData: FormData): voi
|
|||||||
|
|
||||||
for (const group of modifierGroups) {
|
for (const group of modifierGroups) {
|
||||||
const modifierSelect = group.querySelector<HTMLSelectElement>('.modifier-select');
|
const modifierSelect = group.querySelector<HTMLSelectElement>('.modifier-select');
|
||||||
// Find input in the wrapper div (more specific selector)
|
|
||||||
const wrapper = group.querySelector('.filter-value-container');
|
const wrapper = group.querySelector('.filter-value-container');
|
||||||
if (!wrapper) continue;
|
if (!wrapper) continue;
|
||||||
|
|
||||||
@ -103,20 +91,17 @@ function handleFormDataTransform(form: HTMLFormElement, formData: FormData): voi
|
|||||||
const currentName = valueInput.name;
|
const currentName = valueInput.name;
|
||||||
const modifier = modifierSelect.value;
|
const modifier = modifierSelect.value;
|
||||||
|
|
||||||
// Handle empty special case
|
|
||||||
if (modifier === MODIFIER_EMPTY_TRUE || modifier === MODIFIER_EMPTY_FALSE) {
|
if (modifier === MODIFIER_EMPTY_TRUE || modifier === MODIFIER_EMPTY_FALSE) {
|
||||||
formData.delete(currentName);
|
formData.delete(currentName);
|
||||||
const boolValue = modifier === MODIFIER_EMPTY_TRUE ? 'true' : 'false';
|
const boolValue = modifier === MODIFIER_EMPTY_TRUE ? 'true' : 'false';
|
||||||
formData.set(`${currentName}__empty`, boolValue);
|
formData.set(`${currentName}__empty`, boolValue);
|
||||||
} else {
|
} else {
|
||||||
// Get all values (handles multi-select)
|
|
||||||
const values = formData.getAll(currentName);
|
const values = formData.getAll(currentName);
|
||||||
|
|
||||||
if (values.length > 0 && values.some(v => String(v).trim())) {
|
if (values.length > 0 && values.some(v => String(v).trim())) {
|
||||||
formData.delete(currentName);
|
formData.delete(currentName);
|
||||||
const newName = modifier === 'exact' ? currentName : `${currentName}__${modifier}`;
|
const newName = modifier === 'exact' ? currentName : `${currentName}__${modifier}`;
|
||||||
|
|
||||||
// Set all values with the new name
|
|
||||||
for (const value of values) {
|
for (const value of values) {
|
||||||
if (String(value).trim()) {
|
if (String(value).trim()) {
|
||||||
formData.append(newName, value);
|
formData.append(newName, value);
|
||||||
@ -142,12 +127,10 @@ function handleFormDataTransform(form: HTMLFormElement, formData: FormData): voi
|
|||||||
function initializeFromURL(form: HTMLFormElement): void {
|
function initializeFromURL(form: HTMLFormElement): void {
|
||||||
const urlParams = new URLSearchParams(window.location.search);
|
const urlParams = new URLSearchParams(window.location.search);
|
||||||
|
|
||||||
// Find all modifier groups
|
|
||||||
const modifierGroups = form.querySelectorAll('.filter-modifier-group');
|
const modifierGroups = form.querySelectorAll('.filter-modifier-group');
|
||||||
|
|
||||||
for (const group of modifierGroups) {
|
for (const group of modifierGroups) {
|
||||||
const modifierSelect = group.querySelector<HTMLSelectElement>('.modifier-select');
|
const modifierSelect = group.querySelector<HTMLSelectElement>('.modifier-select');
|
||||||
// Find input in the wrapper div (more specific selector)
|
|
||||||
const wrapper = group.querySelector('.filter-value-container');
|
const wrapper = group.querySelector('.filter-value-container');
|
||||||
if (!wrapper) continue;
|
if (!wrapper) continue;
|
||||||
|
|
||||||
@ -157,7 +140,7 @@ function initializeFromURL(form: HTMLFormElement): void {
|
|||||||
|
|
||||||
if (!modifierSelect || !valueInput) continue;
|
if (!modifierSelect || !valueInput) continue;
|
||||||
|
|
||||||
const baseFieldName = valueInput.name; // e.g., "serial"
|
const baseFieldName = valueInput.name;
|
||||||
|
|
||||||
// Special handling for empty - check if field__empty exists in URL
|
// Special handling for empty - check if field__empty exists in URL
|
||||||
const emptyParam = `${baseFieldName}__empty`;
|
const emptyParam = `${baseFieldName}__empty`;
|
||||||
@ -168,7 +151,6 @@ function initializeFromURL(form: HTMLFormElement): void {
|
|||||||
continue; // Don't set value input for empty
|
continue; // Don't set value input for empty
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check each possible lookup in URL
|
|
||||||
for (const option of modifierSelect.options) {
|
for (const option of modifierSelect.options) {
|
||||||
const lookup = option.value;
|
const lookup = option.value;
|
||||||
|
|
||||||
@ -180,15 +162,12 @@ function initializeFromURL(form: HTMLFormElement): void {
|
|||||||
if (urlParams.has(paramName)) {
|
if (urlParams.has(paramName)) {
|
||||||
modifierSelect.value = lookup;
|
modifierSelect.value = lookup;
|
||||||
|
|
||||||
// Handle multi-select vs single-value inputs
|
|
||||||
if (valueInput instanceof HTMLSelectElement && valueInput.multiple) {
|
if (valueInput instanceof HTMLSelectElement && valueInput.multiple) {
|
||||||
// For multi-select, set selected on matching options
|
|
||||||
const values = urlParams.getAll(paramName);
|
const values = urlParams.getAll(paramName);
|
||||||
for (const option of valueInput.options) {
|
for (const option of valueInput.options) {
|
||||||
option.selected = values.includes(option.value);
|
option.selected = values.includes(option.value);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// For single-value inputs, set value directly
|
|
||||||
valueInput.value = urlParams.get(paramName) || '';
|
valueInput.value = urlParams.get(paramName) || '';
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user