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

Side by Side 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: Add support for navigating between headings with arrow keys Created Sept. 19, 2017, 9:14 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | 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 »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 (function(){ 1 (function(){
2 document.addEventListener("DOMContentLoaded", function() 2 document.addEventListener("DOMContentLoaded", function()
3 { 3 {
4 4
5 // Change html class name from "no-js" to "js" 5 // Change html class name from "no-js" to "js"
6 document.documentElement.className = "js"; 6 document.documentElement.className = "js";
7 7
8 // Toggle Navbar Collapse 8 // Toggle Navbar Collapse
9 function toggleNavbarCollapse() 9 function toggleNavbarCollapse()
10 { 10 {
11 var navbarCollapseEls = this.parentElement.getElementsByClassName("navbar- collapse"); 11 var navbarCollapseEls = this.parentElement.getElementsByClassName("navbar- collapse");
12 for (var i = 0; i < navbarCollapseEls.length; i++) 12 for (var i = 0; i < navbarCollapseEls.length; i++)
13 { 13 {
14 navbarCollapseEls[i] 14 navbarCollapseEls[i]
15 .classList.toggle("open") 15 .classList.toggle("open")
16 } 16 }
17 } 17 }
18 18
19 var toggleNavbarCollapseEls = document.getElementsByClassName("toggle-navbar -collapse"); 19 var toggleNavbarCollapseEls = document.getElementsByClassName("toggle-navbar -collapse");
20 for (var i = 0; i < toggleNavbarCollapseEls.length; i++) 20 for (var i = 0; i < toggleNavbarCollapseEls.length; i++)
21 { 21 {
22 toggleNavbarCollapseEls[i] 22 toggleNavbarCollapseEls[i]
23 .addEventListener("click", toggleNavbarCollapse, false); 23 .addEventListener("click", toggleNavbarCollapse, false);
24 } 24 }
25 25
26 // Accordion menu
27
28 function setupAccordionMenu(accordionEl)
29 {
30 var accordionButtons = accordionEl.getElementsByClassName('accordion-toggl e-button');
31
32 for ( var i = 0; i < accordionButtons.length; i++ )
33 {
34 accordionButtons[i]
35 .addEventListener("click", toggleAccordionSection, false);
36 accordionButtons[i]
37 .addEventListener("keydown", navigateAccordionHeadings, false);
38
39 // Close all sections besides the first
40 if ( i !== 0 )
41 {
42 accordionButtons[i].setAttribute("aria-expanded", "false");
43 accordionButtons[i].setAttribute("aria-disabled", "false");
44 document
45 .getElementById( accordionButtons[i].getAttribute("aria-controls") )
46 .setAttribute("hidden", "true");
47 }
48 }
49 }
50
51 function toggleAccordionSection()
52 {
53 // Hide currently expanded section
54 var accordion = this.parentElement.parentElement;
55 var expandedButton = accordion.querySelector("button[aria-expanded='true'] ");
56 if ( expandedButton )
57 {
58 expandedButton.setAttribute("aria-expanded", "false");
59 expandedButton.setAttribute("aria-disabled", "false");
60 document
61 .getElementById( expandedButton.getAttribute("aria-controls") )
62 .setAttribute("hidden", "true");
63 }
64
65 if ( expandedButton === this ) return;
66
67 // Expand new section
68 this.setAttribute("aria-expanded", "true");
69 this.setAttribute("aria-disabled", "true");
70 document
71 .getElementById( this.getAttribute("aria-controls") )
72 .removeAttribute("hidden");
73 }
74
75 function navigateAccordionHeadings(e)
76 {
77 if ( e.keyCode !== 38 && e.keyCode !== 40 ) return;
78
79 var accordion = this.parentElement.parentElement;
80
81 // Direction == up
82 if ( e.keyCode == 38 )
83 {
84 var prevEl = this.parentElement.previousElementSibling;
85 if ( prevEl )
86 {
87 prevEl // .accordion-body
88 .previousElementSibling // .accordion-heading
89 .firstElementChild // .accordion-toggle-button
90 .focus();
91 }
92 else
93 {
94 accordion
95 .lastElementChild // .accordion-body
96 .previousElementSibling // .accordion-heading
97 .firstElementChild // .accordion-toggle-button
98 .focus();
99 }
100 }
101
102 // Direction == down
103 else if ( e.keyCode == 40 )
104 {
105 var nextheading = this.parentElement.nextElementSibling.nextElementSibli ng;
106 if ( nextheading )
107 {
108 nextheading // .accordion-heading
109 .firstElementChild // .accordion-toggle-button
110 .focus();
111 }
112 else
113 {
114 accordion
115 .firstElementChild // .accordion-heading
116 .firstElementChild // .accordion-toggle-button
117 .focus();
118 }
119 }
120 }
121
122 var accordionMenus = document.getElementsByClassName('accordion');
123 for (var i = 0; i < accordionMenus.length; i++)
124 {
125 setupAccordionMenu( accordionMenus[i] );
126 }
127
26 }, false); 128 }, false);
27 }()); 129 }());
OLDNEW
« 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