Simplified collapse.js with Element.closest().

This commit is contained in:
Jon Dufresne 2020-04-21 21:13:58 -07:00 committed by GitHub
parent 290eb98764
commit 0f2885e3f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 3 additions and 14 deletions

View File

@ -1,16 +1,6 @@
/*global gettext*/ /*global gettext*/
(function() { (function() {
'use strict'; 'use strict';
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() { window.addEventListener('load', function() {
// Add anchor tag for Show/Hide link // Add anchor tag for Show/Hide link
var fieldsets = document.querySelectorAll('fieldset.collapse'); var fieldsets = document.querySelectorAll('fieldset.collapse');
@ -35,7 +25,7 @@
if (ev.target.matches('.collapse-toggle')) { if (ev.target.matches('.collapse-toggle')) {
ev.preventDefault(); ev.preventDefault();
ev.stopPropagation(); ev.stopPropagation();
var fieldset = closestElem(ev.target, 'fieldset'); var fieldset = ev.target.closest('fieldset');
if (fieldset.classList.contains('collapsed')) { if (fieldset.classList.contains('collapsed')) {
// Show // Show
ev.target.textContent = gettext('Hide'); ev.target.textContent = gettext('Hide');

View File

@ -1,3 +1,2 @@
(function(){var e=function(b,a){return b.nodeName===a.toUpperCase()?b:"BODY"===b.parentNode.nodeName?null:b.parentNode&&e(b.parentNode,a)};window.addEventListener("load",function(){for(var b=document.querySelectorAll("fieldset.collapse"),a=0;a<b.length;a++){var c=b[a];if(0===c.querySelectorAll("div.errors, ul.errorlist").length){c.classList.add("collapsed");c=c.querySelector("h2");var d=document.createElement("a");d.id="fieldsetcollapser"+a;d.className="collapse-toggle";d.href="#";d.textContent=gettext("Show"); (function(){window.addEventListener("load",function(){for(var d=document.querySelectorAll("fieldset.collapse"),b=0;b<d.length;b++){var a=d[b];if(0===a.querySelectorAll("div.errors, ul.errorlist").length){a.classList.add("collapsed");a=a.querySelector("h2");var c=document.createElement("a");c.id="fieldsetcollapser"+b;c.className="collapse-toggle";c.href="#";c.textContent=gettext("Show");a.appendChild(document.createTextNode(" ("));a.appendChild(c);a.appendChild(document.createTextNode(")"))}}d=function(a){if(a.target.matches(".collapse-toggle")){a.preventDefault();
c.appendChild(document.createTextNode(" ("));c.appendChild(d);c.appendChild(document.createTextNode(")"))}}b=function(a){if(a.target.matches(".collapse-toggle")){a.preventDefault();a.stopPropagation();var b=e(a.target,"fieldset");b.classList.contains("collapsed")?(a.target.textContent=gettext("Hide"),b.classList.remove("collapsed")):(a.target.textContent=gettext("Show"),b.classList.add("collapsed"))}};c=document.querySelectorAll("fieldset.module");for(a=0;a<c.length;a++)c[a].addEventListener("click", a.stopPropagation();var b=a.target.closest("fieldset");b.classList.contains("collapsed")?(a.target.textContent=gettext("Hide"),b.classList.remove("collapsed")):(a.target.textContent=gettext("Show"),b.classList.add("collapsed"))}};a=document.querySelectorAll("fieldset.module");for(b=0;b<a.length;b++)a[b].addEventListener("click",d)})})();
b)})})();