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: Use bowser browser names Created Nov. 1, 2017, 5:19 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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
48 { 48 {
49 // setTimeout to allow document.activeElement 49 // setTimeout to allow document.activeElement
50 // to move to newly focused element 50 // to move to newly focused element
51 setTimeout(function() 51 setTimeout(function()
52 { 52 {
53 var newFocus = document.activeElement; 53 var newFocus = document.activeElement;
54 54
55 if (newFocus 55 if (newFocus
56 .classList.contains("custom-select-selected") || 56 .classList.contains("custom-select-selected") ||
57 newFocus 57 newFocus
58 .classList.contains("custom-select-option") ||
59 newFocus
58 .parentElement 60 .parentElement
59 .classList.contains("custom-select-option")) 61 .classList.contains("custom-select-option"))
60 { 62 {
61 return; 63 return;
62 } 64 }
63 65
64 this.close(); 66 this.close();
65 67
66 }.bind(this), 1); 68 }.bind(this), 1);
67 } 69 }
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 219
218 this.toggleSection(event.target); 220 this.toggleSection(event.target);
219 } 221 }
220 222
221 var productTopicsAccordion = document.getElementById('product-topics-accordi on'); 223 var productTopicsAccordion = document.getElementById('product-topics-accordi on');
222 if (productTopicsAccordion) 224 if (productTopicsAccordion)
223 { 225 {
224 new Accordion(productTopicsAccordion); 226 new Accordion(productTopicsAccordion);
225 } 227 }
226 228
229 /**************************************************************************
230 * BrowserSelect
231 **************************************************************************/
232
233 function BrowserSelect(select)
234 {
235 this.select = select;
236 CustomSelect.apply(this, [this.select]);
237
238 this.BROWSER_STORAGE_KEY = "BROWSER";
239 this.BROWSER_AUTODETECTED_STORAGE_KEY = "BROWSER_AUTODETECTED";
240 this.SUPPORTED_BROWSERS = ["chrome", "opera", "samsungBrowser",
241 "yandexbrowser", "maxthon", "msie",
242 "msedge", "firefox", "ios", "safari"];
243 this.DEFAULT_BROWSER = "chrome";
244
245 this.select
246 .addEventListener("click", this._onClickOrKeyDown.bind(this), false);
247
248 this.select
249 .addEventListener("keydown", this._onClickOrKeyDown.bind(this), false);
250
251 var storedBrowser = localStorage.getItem(this.BROWSER_STORAGE_KEY);
252 if (storedBrowser) this.selectOption(storedBrowser);
253 else this.detectBrowser();
254 }
255
256 BrowserSelect.prototype = Object.create(CustomSelect.prototype);
257 BrowserSelect.prototype.constructor = BrowserSelect;
258
259 BrowserSelect.prototype.detectBrowser = function()
260 {
261 for (var i = 0; i < this.SUPPORTED_BROWSERS.length; i++)
262 {
263 var supportedBrowser = this.SUPPORTED_BROWSERS[i];
264 if (bowser[supportedBrowser])
265 {
266 localStorage.setItem(this.BROWSER_AUTODETECTED_STORAGE_KEY, "true");
267 return this.selectOption(supportedBrowser);
268 }
269 }
270
271 this.selectOption(this.DEFAULT_BROWSER);
272 };
273
274 BrowserSelect.prototype.selectOption = function(browser)
275 {
276 localStorage.setItem(this.BROWSER_STORAGE_KEY, browser);
277
278 // Change body class
279 var bodyClassList = Array.prototype.slice.call(document.body.classList);
280 for (var i = 0; i < bodyClassList.length; i++)
281 {
282 if (bodyClassList[i].indexOf('ua-') > -1)
283 {
284 document.body.classList.remove(bodyClassList[i]);
285 }
286 }
287 document.body.classList.add("ua-" + browser);
288
289 // Check selected option
290 var selectedItem = this.select
291 .querySelector("[data-value='" + browser + "']");
292 selectedItem.setAttribute("aria-checked", "true");
293
294 // Set selected option
295 var selectedOption = selectedItem.innerHTML;
296
297 if (localStorage.getItem(this.BROWSER_AUTODETECTED_STORAGE_KEY))
298 {
299 var autodetected = document
300 .getElementById('browser-select-autodetected')
301 .innerHTML;
302 selectedOption += "<span class='muted'>(" + autodetected + ")</span>";
303 }
304
305 this.select
306 .querySelector(".custom-select-selected")
307 .innerHTML = selectedOption;
308 };
309
310 BrowserSelect.prototype._onClickOrKeyDown = function(event)
311 {
312 if (!event.target.classList.contains("custom-select-option")) return;
313
314 var IS_ENTER_KEY = event.key == "Enter" || event.keyCode == 13;
315 if (event.keyCode && !IS_ENTER_KEY) return;
316
317 localStorage.removeItem(this.BROWSER_AUTODETECTED_STORAGE_KEY);
318
319 // Uncheck previously checked option
320 this.select
321 .querySelector("[aria-checked='true']")
322 .setAttribute("aria-checked", "false");
323
324 this.selectOption(event.target.getAttribute("data-value"));
325
326 this.close();
327 };
328
329 var browserSelect = document.getElementById("browser-select");
330 if (browserSelect)
331 {
332 new BrowserSelect(browserSelect);
333 }
334
227 }, false); 335 }, false);
228 }()); 336 }());
OLDNEW

Powered by Google App Engine
This is Rietveld