| 1 |
/** |
| 2 |
* WPSubsAccordion — expand/collapse sections. |
| 3 |
* |
| 4 |
* Markup: |
| 5 |
* <div class="wpsubs-accordion" data-multi="1"> |
| 6 |
* <div class="wpsubs-accordion__item"> |
| 7 |
* <button class="wpsubs-accordion__header" aria-controls="a1" aria-expanded="false">Title</button> |
| 8 |
* <div class="wpsubs-accordion__panel" id="a1" hidden>…</div> |
| 9 |
* </div> |
| 10 |
* </div> |
| 11 |
* |
| 12 |
* data-multi (truthy) allows multiple panels open at once; default is |
| 13 |
* single-open (opening one collapses the others). A header with |
| 14 |
* aria-expanded="true" starts open. |
| 15 |
* |
| 16 |
* Events fired on the root element (bubbles): |
| 17 |
* wpsubs:accordion:toggle — { id, open } |
| 18 |
*/ |
| 19 |
(function () { |
| 20 |
"use strict"; |
| 21 |
|
| 22 |
/** |
| 23 |
* @param {HTMLElement} el Root .wpsubs-accordion element. |
| 24 |
*/ |
| 25 |
function WPSubsAccordion(el) { |
| 26 |
this.el = el; |
| 27 |
this.multi = !!el.dataset.multi; |
| 28 |
this.headers = Array.prototype.slice.call(el.querySelectorAll(".wpsubs-accordion__header")); |
| 29 |
this._bind(); |
| 30 |
|
| 31 |
// Sync initial panel visibility to each header's aria-expanded. |
| 32 |
var self = this; |
| 33 |
this.headers.forEach(function (header) { |
| 34 |
var panel = self._panel(header); |
| 35 |
if (panel) panel.hidden = header.getAttribute("aria-expanded") !== "true"; |
| 36 |
}); |
| 37 |
} |
| 38 |
|
| 39 |
WPSubsAccordion.prototype._panel = function (header) { |
| 40 |
if (!header) return null; |
| 41 |
var id = header.getAttribute("aria-controls"); |
| 42 |
return id ? this.el.querySelector("#" + id) : null; |
| 43 |
}; |
| 44 |
|
| 45 |
/** |
| 46 |
* Open or close one section. |
| 47 |
* |
| 48 |
* @param {HTMLElement} header Section header button. |
| 49 |
* @param {boolean} open Desired state. |
| 50 |
*/ |
| 51 |
WPSubsAccordion.prototype.set = function (header, open) { |
| 52 |
var panel = this._panel(header); |
| 53 |
header.setAttribute("aria-expanded", open ? "true" : "false"); |
| 54 |
if (panel) panel.hidden = !open; |
| 55 |
|
| 56 |
// Single-open mode: collapse every other section when opening this one. |
| 57 |
if (open && !this.multi) { |
| 58 |
var self = this; |
| 59 |
this.headers.forEach(function (other) { |
| 60 |
if (other !== header) self.set(other, false); |
| 61 |
}); |
| 62 |
} |
| 63 |
|
| 64 |
this.el.dispatchEvent( |
| 65 |
new CustomEvent("wpsubs:accordion:toggle", { |
| 66 |
bubbles: true, |
| 67 |
detail: { id: header.getAttribute("aria-controls") || "", open: open }, |
| 68 |
}), |
| 69 |
); |
| 70 |
}; |
| 71 |
|
| 72 |
WPSubsAccordion.prototype._bind = function () { |
| 73 |
var self = this; |
| 74 |
this.headers.forEach(function (header) { |
| 75 |
header.addEventListener("click", function (e) { |
| 76 |
e.preventDefault(); |
| 77 |
self.set(header, header.getAttribute("aria-expanded") !== "true"); |
| 78 |
}); |
| 79 |
}); |
| 80 |
}; |
| 81 |
|
| 82 |
/** |
| 83 |
* Initialise all un-initialised .wpsubs-accordion elements under root. |
| 84 |
* |
| 85 |
* @param {Document|HTMLElement} [root] |
| 86 |
*/ |
| 87 |
function init(root) { |
| 88 |
(root || document).querySelectorAll(".wpsubs-accordion:not([data-accordion-init])").forEach(function (el) { |
| 89 |
el.setAttribute("data-accordion-init", "1"); |
| 90 |
new WPSubsAccordion(el); |
| 91 |
}); |
| 92 |
} |
| 93 |
|
| 94 |
if (document.readyState === "loading") { |
| 95 |
document.addEventListener("DOMContentLoaded", function () { |
| 96 |
init(); |
| 97 |
}); |
| 98 |
} else { |
| 99 |
init(); |
| 100 |
} |
| 101 |
|
| 102 |
// Public API |
| 103 |
window.WPSubsAccordion = { init: init }; |
| 104 |
})(); |
| 105 |
|