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: Extend CustomSelect, address NITs Created Oct. 27, 2017, 10:22 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-down-secondary.svg ('k') | static/js/vendor/bowser.js » ('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 /************************************************************************** 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 18 matching lines...) Expand all
29 toggleNavbarCollapseEls[i] 29 toggleNavbarCollapseEls[i]
30 .addEventListener("click", toggleNavbarCollapse, false); 30 .addEventListener("click", toggleNavbarCollapse, false);
31 } 31 }
32 32
33 /************************************************************************** 33 /**************************************************************************
34 * CustomSelect 34 * CustomSelect
35 **************************************************************************/ 35 **************************************************************************/
36 36
37 function CustomSelect(select) 37 function CustomSelect(select)
38 { 38 {
39 console.log(select)
juliandoucette 2017/10/31 13:19:58 Hello, world
ire 2017/11/01 12:36:49 Oops! Thanks :)
40
39 this.select = select; 41 this.select = select;
40 this.close(); 42 this.close();
41 this.select 43 this.select
42 .addEventListener("click", this._onClick.bind(this), false); 44 .addEventListener("click", this._onClick.bind(this), false);
43 this.select 45 this.select
44 .addEventListener("focusout", this._onFocusOut.bind(this), false); 46 .addEventListener("focusout", this._onFocusOut.bind(this), false);
45 } 47 }
46 48
47 CustomSelect.prototype._onFocusOut = function() 49 CustomSelect.prototype._onFocusOut = function()
48 { 50 {
49 // setTimeout to allow document.activeElement 51 // setTimeout to allow document.activeElement
50 // to move to newly focused element 52 // to move to newly focused element
51 setTimeout(function() 53 setTimeout(function()
52 { 54 {
53 var newFocus = document.activeElement; 55 var newFocus = document.activeElement;
54 56
55 if (newFocus 57 if (newFocus
56 .classList.contains("custom-select-selected") || 58 .classList.contains("custom-select-selected") ||
57 newFocus 59 newFocus
60 .classList.contains("custom-select-option") ||
61 newFocus
58 .parentElement 62 .parentElement
59 .classList.contains("custom-select-option")) 63 .classList.contains("custom-select-option"))
60 { 64 {
61 return; 65 return;
62 } 66 }
63 67
64 this.close(); 68 this.close();
65 69
66 }.bind(this), 1); 70 }.bind(this), 1);
67 } 71 }
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 221
218 this.toggleSection(event.target); 222 this.toggleSection(event.target);
219 } 223 }
220 224
221 var productTopicsAccordion = document.getElementById('product-topics-accordi on'); 225 var productTopicsAccordion = document.getElementById('product-topics-accordi on');
222 if (productTopicsAccordion) 226 if (productTopicsAccordion)
223 { 227 {
224 new Accordion(productTopicsAccordion); 228 new Accordion(productTopicsAccordion);
225 } 229 }
226 230
231 /**************************************************************************
232 * BrowserSelect
233 **************************************************************************/
234
235 function BrowserSelect(select)
236 {
237 this.select = select;
238 CustomSelect.apply(this, [this.select]);
239
240 this.DEFAULT_BROWSER = "chrome";
241 this.BROWSER_STORAGE_KEY = "BROWSER";
juliandoucette 2017/10/31 13:19:59 NIT/Suggest: The word "STORAGE" is unnecessary (ve
ire 2017/11/01 12:36:49 I don't think it's unnecessary, because it's relat
242 this.BROWSER_AUTODETECTED_STORAGE_KEY = "BROWSER_AUTODETECTED";
243
244 this.select
245 .addEventListener("click", this._onClickOrKeyDown.bind(this), false);
246
247 this.select
248 .addEventListener("keydown", this._onClickOrKeyDown.bind(this), false);
249
250 var storedBrowser = localStorage.getItem(this.BROWSER_STORAGE_KEY);
251 if (storedBrowser) this.selectOption(storedBrowser);
252 else this.detectBrowser();
253 }
254
255 BrowserSelect.prototype = CustomSelect.prototype;
juliandoucette 2017/10/31 13:19:59 This assignment is by reference; not by copy. As a
ire 2017/11/01 12:36:48 Thanks! Done.
256
257 BrowserSelect.prototype.detectBrowser = function()
258 {
259 localStorage.setItem(this.BROWSER_AUTODETECTED_STORAGE_KEY, "true");
260
261 var browser;
262 if (bowser.chrome) browser = "chrome";
263 else if (bowser.opera) browser = "opera";
264 else if (bowser.coast) browser = "opera";
265 else if (bowser.samsungBrowser) browser = "samsung";
266 else if (bowser.yandexbrowser) browser = "yandex";
267 else if (bowser.maxthon) browser = "maxthon";
268 else if (bowser.msie) browser = "ie";
269 else if (bowser.msedge) browser = "edge";
270 else if (bowser.firefox) browser = "firefox";
271 else if (bowser.ios) browser = "ios";
272 else if (bowser.safari) browser = "safari";
273 else
274 {
275 localStorage.removeItem(this.BROWSER_AUTODETECTED_STORAGE_KEY);
276 browser = this.DEFAULT_BROWSER;
277 }
278
279 this.selectOption(browser);
280 };
281
282 BrowserSelect.prototype.selectOption = function(browser)
283 {
284 localStorage.setItem(this.BROWSER_STORAGE_KEY, browser);
285
286 // Change body class
287 var bodyClassList = Array.prototype.slice.call(document.body.classList);
288 for (var i = 0; i < bodyClassList.length; i++)
289 {
290 if (bodyClassList[i].indexOf('ua-') > -1)
291 {
292 document.body.classList.remove(bodyClassList[i]);
293 }
294 }
295 document.body.classList.add("ua-" + browser);
296
297 // Check selected option
298 var selectedItem = this.select
299 .querySelector("[data-value='" + browser + "']");
300 selectedItem.setAttribute("aria-checked", "true");
301
302 // Set selected option
303 var selectedOption = selectedItem.innerHTML;
304
305 if (localStorage.getItem(this.BROWSER_AUTODETECTED_STORAGE_KEY))
306 {
307 var autodetected = document
308 .getElementById('browser-select-autodetected')
309 .innerHTML;
310 selectedOption += "<span class='muted'>(" + autodetected + ")</span>";
311 }
312
313 this.select
314 .querySelector(".custom-select-selected")
315 .innerHTML = selectedOption;
316 };
317
318 BrowserSelect.prototype._onClickOrKeyDown = function(event)
319 {
320 if (!event.target.classList.contains("custom-select-option")) return;
321
322 var IS_ENTER_KEY = event.key == "Enter" || event.keyCode == 13;
323 if (event.keyCode && !IS_ENTER_KEY) return;
324
325 localStorage.removeItem(this.BROWSER_AUTODETECTED_STORAGE_KEY);
326
327 // Uncheck previously checked option
328 this.select
329 .querySelector("[aria-checked='true']")
330 .setAttribute("aria-checked", "false");
331
332 this.selectOption(event.target.getAttribute("data-value"));
333
334 this.close();
335 };
336
337 var browserSelect = document.getElementById("browser-select");
338 if (browserSelect)
339 {
340 new BrowserSelect(browserSelect);
341 }
342
227 }, false); 343 }, false);
228 }()); 344 }());
OLDNEW
« no previous file with comments | « static/img/svg/arrow-icon-down-secondary.svg ('k') | static/js/vendor/bowser.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld