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

Delta Between Two Patch Sets: static/js/main.js

Issue 29548567: Issue 4925 - Create accordion component for Help Center (Closed) Base URL: https://hg.adblockplus.org/help.eyeo.com
Left Patch Set: Rebase, Accordion object, hover state Created Sept. 25, 2017, 11:24 a.m.
Right Patch Set: Refactoring Created Sept. 28, 2017, 2:48 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « static/img/svg/arrow-icon-secondary.svg ('k') | static/scss/components/_accordion.scss » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 52 // Accordion menu
53 53
54 function Accordion(accordion) 54 function Accordion(accordion)
55 { 55 {
56 this.accordion = accordion; 56 this.accordion = accordion;
57 this.init(); 57
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'); 58 var accordionButtons = this.accordion.getElementsByClassName('accordion-to ggle-button');
63
64 for (var i = 0; i < accordionButtons.length; i++) 59 for (var i = 0; i < accordionButtons.length; i++)
65 { 60 {
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 61 // Close all sections except the first
72 if (i !== 0) 62 if (i !== 0)
73 { 63 {
74 accordionButtons[i].setAttribute("aria-expanded", "false"); 64 accordionButtons[i].setAttribute("aria-expanded", "false");
75 document 65 document
76 .getElementById( accordionButtons[i].getAttribute("aria-controls") ) 66 .getElementById( accordionButtons[i].getAttribute("aria-controls") )
juliandoucette 2017/10/09 21:18:16 NIT: Extra whitespace inside brackets (The same a
77 .setAttribute("hidden", "true"); 67 .setAttribute("hidden", "true");
78 } 68 }
79 } 69 }
80 };
81 70
82 Accordion.prototype.toggleAccordionSection = function() 71 this.accordion
72 .addEventListener("click", this._onClick.bind(this), false);
73 this.accordion
74 .addEventListener("keydown", this._onKeyDown.bind(this), false);
75 }
76
77 Accordion.prototype.toggleSection = function(clickedButton)
83 { 78 {
84 // Hide currently expanded section 79 // Hide currently expanded section
85 var accordion = this.parentElement.parentElement; 80 var expandedButton = this.accordion.querySelector("button[aria-expanded='t rue']");
86 var expandedButton = accordion.querySelector("button[aria-expanded='true'] ");
87 if (expandedButton) 81 if (expandedButton)
88 { 82 {
89 expandedButton.setAttribute("aria-expanded", "false"); 83 expandedButton.setAttribute("aria-expanded", "false");
90 document 84 document
91 .getElementById( expandedButton.getAttribute("aria-controls") ) 85 .getElementById( expandedButton.getAttribute("aria-controls") )
92 .setAttribute("hidden", "true"); 86 .setAttribute("hidden", "true");
93 } 87 }
94 88
95 // If currently expanded section is clicked 89 // If currently expanded section is clicked
96 if (expandedButton === this) return; 90 if (expandedButton === clickedButton) return;
97 91
98 // Expand new section 92 // Expand new section
99 this.setAttribute("aria-expanded", "true"); 93 clickedButton.setAttribute("aria-expanded", "true");
100 document 94 document
101 .getElementById( this.getAttribute("aria-controls") ) 95 .getElementById( clickedButton.getAttribute("aria-controls") )
102 .removeAttribute("hidden"); 96 .removeAttribute("hidden");
103 } 97 }
104 98
105 Accordion.prototype.navigateAccordionHeadings = function(event) 99 Accordion.prototype.focusNextSection = function()
juliandoucette 2017/09/26 18:57:06 Suggest: Accordion.prototype._onKeyDown(event) {
ire 2017/09/28 14:48:53 Made some changes.
106 { 100 {
107 var IS_KEY_UP = event.key == 38 || event.keyCode == 38; 101 var currentFocus = document.activeElement;
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; 102 var nextheading = currentFocus.parentElement.nextElementSibling.nextElemen tSibling;
109 103
110 if (!IS_KEY_UP && !IS_KEY_DOWN) return; 104 if (nextheading)
105 {
106 nextheading // .accordion-heading
107 .firstElementChild // .accordion-toggle-button
108 .focus();
109 }
110 else
111 {
112 this.accordion
113 .firstElementChild // .accordion-heading
114 .firstElementChild // .accordion-toggle-button
115 .focus();
116 }
117 }
111 118
112 var accordion = this.parentElement.parentElement; 119 Accordion.prototype.focusPrevSection = function()
120 {
121 var currentFocus = document.activeElement;
122 var prevAccordionBody = currentFocus.parentElement.previousElementSibling;
113 123
114 if (IS_KEY_UP) 124 if (prevAccordionBody)
115 { 125 {
116 var prevAccordionBody = this.parentElement.previousElementSibling; 126 prevAccordionBody // .accordion-body
117 if (prevAccordionBody) 127 .previousElementSibling // .accordion-heading
118 { 128 .firstElementChild // .accordion-toggle-button
119 prevAccordionBody // .accordion-body 129 .focus();
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 } 130 }
131 else
132 {
133 this.accordion
134 .lastElementChild // .accordion-body
135 .previousElementSibling // .accordion-heading
136 .firstElementChild // .accordion-toggle-button
137 .focus();
138 }
139 }
133 140
134 else if (IS_KEY_DOWN) 141 Accordion.prototype._onKeyDown = function(event)
142 {
143 if (!event.target.classList.contains("accordion-toggle-button")) return;
144
145 if (event.key == "ArrowUp" || event.keyCode == 38)
135 { 146 {
136 var nextheading = this.parentElement.nextElementSibling.nextElementSibli ng; 147 this.focusPrevSection();
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 } 148 }
149 else if (event.key == "ArrowDown" || event.keyCode == 40)
150 {
151 this.focusNextSection();
152 }
153 }
151 154
155 Accordion.prototype._onClick = function(event)
156 {
157 if (!event.target.classList.contains("accordion-toggle-button")) return;
158
159 this.toggleSection(event.target);
152 } 160 }
153 161
154 var accordions = document.getElementsByClassName('accordion'); 162 var accordions = document.getElementsByClassName('accordion');
155 for (var i = 0; i < accordions.length; i++) 163 for (var i = 0; i < accordions.length; i++)
156 { 164 {
157 new Accordion(accordions[i]); 165 new Accordion(accordions[i]);
158 } 166 }
159 167
160 }, false); 168 }, false);
161 }()); 169 }());
LEFTRIGHT

Powered by Google App Engine
This is Rietveld