Skip to content

Commit

Permalink
Add missing JS
Browse files Browse the repository at this point in the history
  • Loading branch information
rachidatecs committed May 2, 2024
1 parent 4211418 commit 3244f73
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/registrar/assets/js/dja-collapse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* We will run our own version of
* https://github.com/django/django/blob/195d885ca01b14e3ce9a1881c3b8f7074f953736/django/contrib/admin/static/admin/js/collapse.js
* Works with our fieldset override
*/

/*global gettext*/
'use strict';
{
window.addEventListener('load', function() {
// Add anchor tag for Show/Hide link
const fieldsets = document.querySelectorAll('fieldset.collapse--dotgov');
for (const [i, elem] of fieldsets.entries()) {
// Don't hide if fields in this fieldset have errors
if (elem.querySelectorAll('div.errors, ul.errorlist').length === 0) {
elem.classList.add('collapsed');
const button = elem.querySelector('button');
button.id = 'fieldsetcollapser' + i;
button.className = 'collapse-toggle--dotgov usa-button usa-button--unstyled';
}
}
// Add toggle to hide/show anchor tag
const toggleFuncDotgov = function(e) {
e.preventDefault();
e.stopPropagation();
const fieldset = this.closest('fieldset');
const spanElement = this.querySelector('span');
const useElement = this.querySelector('use');
if (fieldset.classList.contains('collapsed')) {
// Show
spanElement.textContent = 'Hide details';
useElement.setAttribute('xlink:href', '/public/img/sprite.svg#expand_less');
fieldset.classList.remove('collapsed');
} else {
// Hide
spanElement.textContent = 'Show details';
useElement.setAttribute('xlink:href', '/public/img/sprite.svg#expand_more');
fieldset.classList.add('collapsed');
}
};
document.querySelectorAll('.collapse-toggle--dotgov').forEach(function(el) {
el.addEventListener('click', toggleFuncDotgov);
});
});
}

0 comments on commit 3244f73

Please sign in to comment.