2018-10-12 03:11:57 +00:00
|
|
|
/*global gettext*/
|
2019-05-02 16:43:51 +00:00
|
|
|
(function() {
|
2018-10-12 03:11:57 +00:00
|
|
|
'use strict';
|
2019-05-02 16:43:51 +00:00
|
|
|
var closestElem = function(elem, tagName) {
|
|
|
|
if (elem.nodeName === tagName.toUpperCase()) {
|
|
|
|
return elem;
|
|
|
|
}
|
|
|
|
if (elem.parentNode.nodeName === 'BODY') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return elem.parentNode && closestElem(elem.parentNode, tagName);
|
|
|
|
};
|
|
|
|
|
|
|
|
window.addEventListener('load', function() {
|
2018-10-12 03:11:57 +00:00
|
|
|
// Add anchor tag for Show/Hide link
|
2019-05-02 16:43:51 +00:00
|
|
|
var fieldsets = document.querySelectorAll('fieldset.collapse');
|
|
|
|
for (var i = 0; i < fieldsets.length; i++) {
|
|
|
|
var elem = fieldsets[i];
|
2018-10-12 03:11:57 +00:00
|
|
|
// Don't hide if fields in this fieldset have errors
|
2019-05-02 16:43:51 +00:00
|
|
|
if (elem.querySelectorAll('div.errors').length === 0) {
|
|
|
|
elem.classList.add('collapsed');
|
|
|
|
var h2 = elem.querySelector('h2');
|
|
|
|
var link = document.createElement('a');
|
|
|
|
link.setAttribute('id', 'fieldsetcollapser' + i);
|
|
|
|
link.setAttribute('class', 'collapse-toggle');
|
|
|
|
link.setAttribute('href', '#');
|
|
|
|
link.textContent = gettext('Show');
|
|
|
|
h2.appendChild(document.createTextNode(' ('));
|
|
|
|
h2.appendChild(link);
|
|
|
|
h2.appendChild(document.createTextNode(')'));
|
2018-10-12 03:11:57 +00:00
|
|
|
}
|
2019-05-02 16:43:51 +00:00
|
|
|
}
|
2018-10-12 03:11:57 +00:00
|
|
|
// Add toggle to anchor tag
|
2019-05-02 16:43:51 +00:00
|
|
|
var toggles = document.querySelectorAll('fieldset.collapse a.collapse-toggle');
|
|
|
|
var toggleFunc = function(ev) {
|
|
|
|
ev.preventDefault();
|
|
|
|
var fieldset = closestElem(this, 'fieldset');
|
|
|
|
if (fieldset.classList.contains('collapsed')) {
|
2018-10-12 03:11:57 +00:00
|
|
|
// Show
|
2019-05-02 16:43:51 +00:00
|
|
|
this.textContent = gettext('Hide');
|
|
|
|
fieldset.classList.remove('collapsed');
|
2018-10-12 03:11:57 +00:00
|
|
|
} else {
|
|
|
|
// Hide
|
2019-05-02 16:43:51 +00:00
|
|
|
this.textContent = gettext('Show');
|
|
|
|
fieldset.classList.add('collapsed');
|
2018-10-12 03:11:57 +00:00
|
|
|
}
|
2019-05-02 16:43:51 +00:00
|
|
|
};
|
|
|
|
for (i = 0; i < toggles.length; i++) {
|
|
|
|
toggles[i].addEventListener('click', toggleFunc);
|
|
|
|
}
|
2018-10-12 03:11:57 +00:00
|
|
|
});
|
2019-05-02 16:43:51 +00:00
|
|
|
})();
|