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

Side by Side Diff: static/js/main.js

Issue 29559620: Issue 5692 - Create Browser Selector with Browser Detection Component for help.eyeo.com (Closed) Base URL: https://hg.adblockplus.org/help.eyeo.com
Patch Set: Remove test class names from body Created Oct. 23, 2017, 4:14 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 (function(){ 1 (function(){
2 document.addEventListener("DOMContentLoaded", function() 2 document.addEventListener("DOMContentLoaded", function()
3 { 3 {
4 4
5 /************************************************************************** 5 /**************************************************************************
6 * General 6 * General
7 **************************************************************************/ 7 **************************************************************************/
8 8
9 // Change html class name from "no-js" to "js" 9 // Change html class name from "no-js" to "js"
10 document.documentElement.className = "js"; 10 document.documentElement.className = "js";
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
192 192
193 this.toggleSection(event.target); 193 this.toggleSection(event.target);
194 } 194 }
195 195
196 var productTopicsAccordion = document.getElementById('product-topics-accordi on'); 196 var productTopicsAccordion = document.getElementById('product-topics-accordi on');
197 if (productTopicsAccordion) 197 if (productTopicsAccordion)
198 { 198 {
199 new Accordion(productTopicsAccordion); 199 new Accordion(productTopicsAccordion);
200 } 200 }
201 201
202 /**************************************************************************
203 * BrowserSelect
204 **************************************************************************/
205
206 function BrowserSelect(select)
207 {
208 this.browserSelect = select;
juliandoucette 2017/10/24 12:12:28 NIT: You are repeating yourself by prefacing these
ire 2017/10/27 10:22:57 You're right. Done.
209 this.browserCustomSelect = new CustomSelect(this.browserSelect);
juliandoucette 2017/10/24 12:12:28 NIT/Suggest: Looking at this again, I think that t
ire 2017/10/27 10:22:56 Done.
210
211 this.DEFAULT_BROWSER = "chrome";
212 this.BROWSER_STORAGE_KEY = "BROWSER";
213 this.BROWSER_AUTODETECTED_STORAGE_KEY = "BROWSER_AUTODETECTED";
214
215 this.browserSelect
216 .addEventListener("click", this._onClickOrKeyDown.bind(this), false);
217
218 this.browserSelect
219 .addEventListener("keydown", this._onClickOrKeyDown.bind(this), false);
220
221 var storedBrowser = localStorage.getItem(this.BROWSER_STORAGE_KEY);
juliandoucette 2017/10/24 12:12:28 NIT/Suggest: Place this above event listeners with
ire 2017/10/27 10:22:56 I think I prefer this being close to where it's us
222 if (storedBrowser)
juliandoucette 2017/10/24 12:12:29 NIT/Suggest: Our coding style permits braceless on
ire 2017/10/27 10:22:58 Done.
223 {
224 this.selectOption(storedBrowser);
225 }
226 else
227 {
228 this.detectBrowser();
229 }
230 }
juliandoucette 2017/10/24 12:12:28 NIT: Missing ";" (the same applies elsewhere)
ire 2017/10/27 10:22:57 The ";" isn't used for the closing brace of functi
juliandoucette 2017/10/31 13:19:57 *facepalm* (because I commented on the only place
ire 2017/11/01 12:36:47 Haha :D
231
232 BrowserSelect.prototype.detectBrowser = function()
233 {
234 localStorage.setItem(this.BROWSER_AUTODETECTED_STORAGE_KEY, "true");
235
236 var browser;
237 if (bowser.chrome) browser = "chrome";
juliandoucette 2017/10/24 12:12:29 NIT/Suggest: this.supportedBrowsers = ["chorme",
ire 2017/10/27 10:22:57 I need to check for the specific browser name to s
juliandoucette 2017/10/31 13:19:58 I did so by setting the variable `supportedBrowser
ire 2017/11/01 12:36:47 Ooo I see! I don't think I read your example prope
juliandoucette 2017/11/01 12:49:21 Why don't we use their names instead?
ire 2017/11/01 17:20:11 We could. Done.
238 else if (bowser.opera) browser = "opera";
239 else if (bowser.coast) browser = "opera";
240 else if (bowser.samsungBrowser) browser = "samsung";
241 else if (bowser.yandexbrowser) browser = "yandex";
242 else if (bowser.maxthon) browser = "maxthon";
243 else if (bowser.msie) browser = "ie";
244 else if (bowser.msedge) browser = "edge";
245 else if (bowser.firefox) browser = "firefox";
246 else if (bowser.ios) browser = "ios";
247 else if (bowser.safari) browser = "safari";
248 else
249 {
250 localStorage.removeItem(this.BROWSER_AUTODETECTED_STORAGE_KEY);
251 browser = this.DEFAULT_BROWSER;
252 }
253
254 this.selectOption(browser);
255 }
256
257 BrowserSelect.prototype.selectOption = function(browser)
258 {
259 // Save value to Local Storage
juliandoucette 2017/10/24 12:12:29 NIT: I don't think that this comment is necessary?
ire 2017/10/27 10:22:56 Done.
260 localStorage.setItem(this.BROWSER_STORAGE_KEY, browser);
261
262 // Change body class
263 var bodyClassList = Array.prototype.slice.call(document.body.classList);
juliandoucette 2017/10/24 12:12:27 document.body.classList has length. You don't need
ire 2017/10/27 10:22:57 I can't do it that way because when a class is rem
juliandoucette 2017/10/31 13:19:57 Acknowledged. Doh.
264 for (var i = 0; i < bodyClassList.length; i++)
265 {
266 if (bodyClassList[i].indexOf('ua-') > -1)
267 {
268 document.body.classList.remove(bodyClassList[i]);
269 }
270 }
271 document.body.classList.add("ua-" + browser);
272
273 // Check selected option
274 var selectedItem = this.browserSelect
juliandoucette 2017/10/24 12:12:29 I think that we usually open the brace on the same
ire 2017/10/27 10:22:58 I moved the .querySelector line because it would b
juliandoucette 2017/10/31 13:19:58 I know. My comment still applies? We usually open
juliandoucette 2017/10/31 13:24:02 Can you advise us on this kzar?
kzar 2017/10/31 14:22:31 IIRC we don't have a ESLint rule for this case, so
ire 2017/11/01 12:36:48 Thanks kzar!
275 .querySelector("[data-value='" + browser + "']");
276 selectedItem.setAttribute("aria-checked", "true");
277
278 // Set selected option
279 var selectedOption = document.createElement("li");
juliandoucette 2017/10/24 12:12:28 You don't seem to be using this element (just mani
ire 2017/10/27 10:22:57 You're right, don't notice that :p Done.
280 selectedOption.innerHTML = selectedItem.innerHTML;
281
282 if (localStorage.getItem(this.BROWSER_AUTODETECTED_STORAGE_KEY))
283 {
284 var autodetected = document
285 .getElementById('browser-select-autodetected')
286 .innerHTML;
287 selectedOption.innerHTML += "<span class='muted'>(" + autodetected + ")< /span>";
288 }
289
290 this.browserSelect
291 .querySelector(".custom-select-selected")
292 .innerHTML = selectedOption.innerHTML;
293 }
294
295 BrowserSelect.prototype._onClickOrKeyDown = function(event)
296 {
297 if (!event.target.classList.contains("custom-select-option")) return;
298
299 var IS_ENTER_KEY = event.key == "Enter" || event.keyCode == 13;
300 if (event.keyCode && !IS_ENTER_KEY) return;
301
302 localStorage.removeItem(this.BROWSER_AUTODETECTED_STORAGE_KEY);
303
304 // Uncheck previously checked option
305 this.browserSelect
306 .querySelector("[aria-checked='true']")
307 .setAttribute("aria-checked", "false");
308
309 this.selectOption(event.target.getAttribute("data-value"));
310
311 // Close Select
312 this.browserCustomSelect.close();
313 }
314
315 var browserSelect = document.getElementById("browser-select");
316 if (browserSelect)
317 {
318 new BrowserSelect(browserSelect);
319 }
320
202 }, false); 321 }, false);
203 }()); 322 }());
OLDNEW

Powered by Google App Engine
This is Rietveld