Another converted file

This commit is contained in:
Alex Janousek
2025-10-22 14:38:01 -04:00
parent 51c7587637
commit 2fdb0e30f9

View File

@@ -1,10 +1,11 @@
(function (window) { (function (window) {
'use strict'; 'use strict';
var $ = window.jQuery; // this javascript could be removed in the future since it is only being used
// on the page "send-files-by-email". which is not currently in use.
function ShowHideContent () { function ShowHideContent () {
var self = this; var self = this;
var eventHandlers = new Map();
var selectors = { var selectors = {
namespace: 'ShowHideContent', namespace: 'ShowHideContent',
@@ -12,126 +13,159 @@
checkbox: '[data-target] > input[type="checkbox"]' checkbox: '[data-target] > input[type="checkbox"]'
}; };
function initToggledContent () { function initToggledContent (control) {
var $control = $(this); var content = getToggledContent(control);
var $content = getToggledContent($control);
if ($content.length) { if (content) {
$control.attr('aria-controls', $content.attr('id')); control.setAttribute('aria-controls', content.getAttribute('id'));
$control.attr('aria-expanded', 'false'); control.setAttribute('aria-expanded', 'false');
$content.attr('aria-hidden', 'true'); content.setAttribute('aria-hidden', 'true');
} }
} }
function getToggledContent ($control) { function getToggledContent (control) {
try { try {
var id = $control.attr('aria-controls'); var id = control.getAttribute('aria-controls');
if (!id) { if (!id) {
id = $control.closest('[data-target]').data('target'); var parent = control.closest('[data-target]');
id = parent ? parent.dataset.target : null;
} }
if (!id || !/^[\w-]+$/.test(id)) { if (!id || !/^[\w-]+$/.test(id)) {
console.warn('Invalid element ID:', id); console.warn('Invalid element ID:', id);
return $(); return null;
} }
return $('#' + id); return document.getElementById(id);
} catch (error) { } catch (error) {
console.error('Error getting toggled content:', error); console.error('Error getting toggled content:', error);
return $(); return null;
} }
} }
function showToggledContent ($control, $content) { function showToggledContent (control, content) {
if ($content.hasClass('display-none')) { if (content.classList.contains('display-none')) {
$content.removeClass('display-none'); content.classList.remove('display-none');
$content.attr('aria-hidden', 'false'); content.setAttribute('aria-hidden', 'false');
if ($control.attr('aria-controls')) { if (control.getAttribute('aria-controls')) {
$control.attr('aria-expanded', 'true'); control.setAttribute('aria-expanded', 'true');
} }
} }
} }
function hideToggledContent ($control, $content) { function hideToggledContent (control, content) {
$content.addClass('display-none'); content.classList.add('display-none');
$content.attr('aria-hidden', 'true'); content.setAttribute('aria-hidden', 'true');
if ($control.attr('aria-controls')) { if (control.getAttribute('aria-controls')) {
$control.attr('aria-expanded', 'false'); control.setAttribute('aria-expanded', 'false');
} }
} }
function handleRadioContent ($control, $content) { function handleRadioContent (control, content) {
var selector = selectors.radio + '[name=' + escapeElementName($control.attr('name')) + ']'; var selector = selectors.radio + '[name=' + escapeElementName(control.getAttribute('name')) + ']';
var $radios = $(selector); var radios = document.querySelectorAll(selector);
$radios.each(function () { radios.forEach(function (radio) {
hideToggledContent($(this), getToggledContent($(this))); var radioContent = getToggledContent(radio);
if (radioContent) {
hideToggledContent(radio, radioContent);
}
}); });
showToggledContent($control, $content); if (content) {
showToggledContent(control, content);
}
} }
function handleCheckboxContent ($control, $content) { function handleCheckboxContent (control, content) {
if ($control.is(':checked')) { if (!content) {
showToggledContent($control, $content); return;
}
if (control.checked) {
showToggledContent(control, content);
} else { } else {
hideToggledContent($control, $content); hideToggledContent(control, content);
} }
} }
function escapeElementName (str) { function escapeElementName (str) {
// First escape backslashes, then escape other special characters // First escape backslashes, then escape other special characters
// This prevents double-escaping issues identified by CodeQL // This prevents double-escaping issues identified by CodeQL
return str return str ? str.replace(/\\/g, '\\\\').replace(/([!"#$%&'()*+,./:;<=>?@[\]^`{|}~])/g, '\\$1') : str;
? str.replace(/\\/g, '\\\\').replace(/([!"#$%&'()*+,./:;<=>?@[\]^`{|}~])/g, '\\$1')
: str;
} }
function setupHandlers () { function setupHandlers () {
var $controls = $(selectors.radio + ', ' + selectors.checkbox); var radios = document.querySelectorAll(selectors.radio);
var checkboxes = document.querySelectorAll(selectors.checkbox);
$(selectors.radio).on('click.' + selectors.namespace, function () { radios.forEach(function (radio) {
handleRadioContent($(this), getToggledContent($(this))); var handler = function () {
handleRadioContent(radio, getToggledContent(radio));
};
radio.addEventListener('click', handler);
eventHandlers.set(radio, handler);
}); });
$(selectors.checkbox).on('click.' + selectors.namespace, function () { checkboxes.forEach(function (checkbox) {
handleCheckboxContent($(this), getToggledContent($(this))); var handler = function () {
handleCheckboxContent(checkbox, getToggledContent(checkbox));
};
checkbox.addEventListener('click', handler);
eventHandlers.set(checkbox, handler);
}); });
if ($controls.filter(':checked').length) {
$controls.filter(':checked').each(function () {
var $control = $(this);
var $content = getToggledContent($control);
if ($control.is('[type=radio]')) { var allControls = Array.from(radios).concat(Array.from(checkboxes));
handleRadioContent($control, $content); var checkedControls = allControls.filter(function (control) {
return control.checked;
});
if (checkedControls.length) {
checkedControls.forEach(function (control) {
var content = getToggledContent(control);
if (content) {
if (control.type === 'radio') {
handleRadioContent(control, content);
} else { } else {
handleCheckboxContent($control, $content); handleCheckboxContent(control, content);
}
} }
}); });
} }
} }
self.destroy = function () { self.destroy = function () {
var $controls = $(selectors.radio + ', ' + selectors.checkbox); var radios = document.querySelectorAll(selectors.radio);
var checkboxes = document.querySelectorAll(selectors.checkbox);
var allControls = Array.from(radios).concat(Array.from(checkboxes));
$controls.each(function () { allControls.forEach(function (control) {
var $control = $(this); var content = getToggledContent(control);
var $content = getToggledContent($control);
$control.removeAttr('aria-controls aria-expanded'); control.removeAttribute('aria-controls');
$content.removeAttr('aria-hidden'); control.removeAttribute('aria-expanded');
if (content) {
content.removeAttribute('aria-hidden');
}
var handler = eventHandlers.get(control);
if (handler) {
control.removeEventListener('click', handler);
eventHandlers.delete(control);
}
}); });
$(selectors.radio).off('.' + selectors.namespace);
$(selectors.checkbox).off('.' + selectors.namespace);
}; };
self.init = function () { self.init = function () {
try { try {
$(selectors.radio + ', ' + selectors.checkbox).each(initToggledContent); var radios = document.querySelectorAll(selectors.radio);
var checkboxes = document.querySelectorAll(selectors.checkbox);
var allControls = Array.from(radios).concat(Array.from(checkboxes));
allControls.forEach(initToggledContent);
setupHandlers(); setupHandlers();
} catch (error) { } catch (error) {