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

Powered by Google App Engine
This is Rietveld