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: Rebase, Accordion object, hover state Created Sept. 25, 2017, 11:24 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 {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 var customSelectEls = document.getElementsByClassName('custom-select-selecte d'); 42 var customSelectEls = document.getElementsByClassName('custom-select-selecte d');
43 for (var i = 0; i < customSelectEls.length; i++) 43 for (var i = 0; i < customSelectEls.length; i++)
44 { 44 {
45 customSelectEls[i] 45 customSelectEls[i]
46 .addEventListener("click", onClickCustomSelect, false); 46 .addEventListener("click", onClickCustomSelect, false);
47 customSelectEls[i] 47 customSelectEls[i]
48 .nextElementSibling 48 .nextElementSibling
49 .setAttribute("aria-hidden", "true"); 49 .setAttribute("aria-hidden", "true");
50 } 50 }
51 51
52 // Accordion menu
53
54 function Accordion(accordion)
55 {
56 this.accordion = accordion;
57 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.
58 }
59
60 Accordion.prototype.init = function()
61 {
62 var accordionButtons = this.accordion.getElementsByClassName('accordion-to ggle-button');
63
64 for (var i = 0; i < accordionButtons.length; i++)
65 {
66 accordionButtons[i]
67 .addEventListener("click", this.toggleAccordionSection, false);
68 accordionButtons[i]
69 .addEventListener("keydown", this.navigateAccordionHeadings, false);
70
71 // Close all sections except the first
72 if (i !== 0)
73 {
74 accordionButtons[i].setAttribute("aria-expanded", "false");
75 document
76 .getElementById( accordionButtons[i].getAttribute("aria-controls") )
77 .setAttribute("hidden", "true");
78 }
79 }
80 };
81
82 Accordion.prototype.toggleAccordionSection = function()
83 {
84 // Hide currently expanded section
85 var accordion = this.parentElement.parentElement;
86 var expandedButton = accordion.querySelector("button[aria-expanded='true'] ");
87 if (expandedButton)
88 {
89 expandedButton.setAttribute("aria-expanded", "false");
90 document
91 .getElementById( expandedButton.getAttribute("aria-controls") )
92 .setAttribute("hidden", "true");
93 }
94
95 // If currently expanded section is clicked
96 if (expandedButton === this) return;
97
98 // Expand new section
99 this.setAttribute("aria-expanded", "true");
100 document
101 .getElementById( this.getAttribute("aria-controls") )
102 .removeAttribute("hidden");
103 }
104
105 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.
106 {
107 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 :/)
108 var IS_KEY_DOWN = event.key == 40 || event.keyCode == 40;
109
110 if (!IS_KEY_UP && !IS_KEY_DOWN) return;
111
112 var accordion = this.parentElement.parentElement;
113
114 if (IS_KEY_UP)
115 {
116 var prevAccordionBody = this.parentElement.previousElementSibling;
117 if (prevAccordionBody)
118 {
119 prevAccordionBody // .accordion-body
120 .previousElementSibling // .accordion-heading
121 .firstElementChild // .accordion-toggle-button
122 .focus();
123 }
124 else
125 {
126 accordion
127 .lastElementChild // .accordion-body
128 .previousElementSibling // .accordion-heading
129 .firstElementChild // .accordion-toggle-button
130 .focus();
131 }
132 }
133
134 else if (IS_KEY_DOWN)
135 {
136 var nextheading = this.parentElement.nextElementSibling.nextElementSibli ng;
137 if (nextheading)
138 {
139 nextheading // .accordion-heading
140 .firstElementChild // .accordion-toggle-button
141 .focus();
142 }
143 else
144 {
145 accordion
146 .firstElementChild // .accordion-heading
147 .firstElementChild // .accordion-toggle-button
148 .focus();
149 }
150 }
151
152 }
153
154 var accordions = document.getElementsByClassName('accordion');
155 for (var i = 0; i < accordions.length; i++)
156 {
157 new Accordion(accordions[i]);
158 }
159
52 }, false); 160 }, false);
53 }()); 161 }());
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