Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: static/js/main.js

Issue 29548567: Issue 4925 - Create accordion component for Help Center (Closed) Base URL: https://hg.adblockplus.org/help.eyeo.com
Patch Set: Rebase, Accordion object, hover state Created Sept. 25, 2017, 11:24 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « static/img/svg/arrow-icon-secondary.svg ('k') | static/scss/components/_accordion.scss » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: static/js/main.js
===================================================================
--- a/static/js/main.js
+++ b/static/js/main.js
@@ -44,10 +44,118 @@
{
customSelectEls[i]
.addEventListener("click", onClickCustomSelect, false);
customSelectEls[i]
.nextElementSibling
.setAttribute("aria-hidden", "true");
}
+ // Accordion menu
+
+ function Accordion(accordion)
+ {
+ this.accordion = accordion;
+ this.init();
juliandoucette 2017/09/26 18:57:06 NIT: It's unnecessary to separate this constructor
ire 2017/09/28 14:48:52 Done.
+ }
+
+ Accordion.prototype.init = function()
+ {
+ var accordionButtons = this.accordion.getElementsByClassName('accordion-toggle-button');
+
+ for (var i = 0; i < accordionButtons.length; i++)
+ {
+ accordionButtons[i]
+ .addEventListener("click", this.toggleAccordionSection, false);
+ accordionButtons[i]
+ .addEventListener("keydown", this.navigateAccordionHeadings, false);
+
+ // Close all sections except the first
+ if (i !== 0)
+ {
+ accordionButtons[i].setAttribute("aria-expanded", "false");
+ document
+ .getElementById( accordionButtons[i].getAttribute("aria-controls") )
+ .setAttribute("hidden", "true");
+ }
+ }
+ };
+
+ Accordion.prototype.toggleAccordionSection = function()
+ {
+ // Hide currently expanded section
+ var accordion = this.parentElement.parentElement;
+ var expandedButton = accordion.querySelector("button[aria-expanded='true']");
+ if (expandedButton)
+ {
+ expandedButton.setAttribute("aria-expanded", "false");
+ document
+ .getElementById( expandedButton.getAttribute("aria-controls") )
+ .setAttribute("hidden", "true");
+ }
+
+ // If currently expanded section is clicked
+ if (expandedButton === this) return;
+
+ // Expand new section
+ this.setAttribute("aria-expanded", "true");
+ document
+ .getElementById( this.getAttribute("aria-controls") )
+ .removeAttribute("hidden");
+ }
+
+ Accordion.prototype.navigateAccordionHeadings = function(event)
juliandoucette 2017/09/26 18:57:06 Suggest: Accordion.prototype._onKeyDown(event) {
ire 2017/09/28 14:48:53 Made some changes.
+ {
+ var IS_KEY_UP = event.key == 38 || event.keyCode == 38;
juliandoucette 2017/09/26 18:57:06 I think that the value of event.key for up is "Arr
ire 2017/09/28 14:48:53 Done. (Sorry I didn't check :/)
+ var IS_KEY_DOWN = event.key == 40 || event.keyCode == 40;
+
+ if (!IS_KEY_UP && !IS_KEY_DOWN) return;
+
+ var accordion = this.parentElement.parentElement;
+
+ if (IS_KEY_UP)
+ {
+ var prevAccordionBody = this.parentElement.previousElementSibling;
+ if (prevAccordionBody)
+ {
+ prevAccordionBody // .accordion-body
+ .previousElementSibling // .accordion-heading
+ .firstElementChild // .accordion-toggle-button
+ .focus();
+ }
+ else
+ {
+ accordion
+ .lastElementChild // .accordion-body
+ .previousElementSibling // .accordion-heading
+ .firstElementChild // .accordion-toggle-button
+ .focus();
+ }
+ }
+
+ else if (IS_KEY_DOWN)
+ {
+ var nextheading = this.parentElement.nextElementSibling.nextElementSibling;
+ if (nextheading)
+ {
+ nextheading // .accordion-heading
+ .firstElementChild // .accordion-toggle-button
+ .focus();
+ }
+ else
+ {
+ accordion
+ .firstElementChild // .accordion-heading
+ .firstElementChild // .accordion-toggle-button
+ .focus();
+ }
+ }
+
+ }
+
+ var accordions = document.getElementsByClassName('accordion');
+ for (var i = 0; i < accordions.length; i++)
+ {
+ new Accordion(accordions[i]);
+ }
+
}, false);
}());
« no previous file with comments | « static/img/svg/arrow-icon-secondary.svg ('k') | static/scss/components/_accordion.scss » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld