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

Delta Between Two Patch Sets: 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
Left Patch Set: Rebased Created Oct. 11, 2017, 5:20 p.m.
Right 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
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 19 matching lines...) Expand all
31 } 32 }
32 33
33 /************************************************************************** 34 /**************************************************************************
34 * CustomSelect 35 * CustomSelect
35 **************************************************************************/ 36 **************************************************************************/
36 37
37 function CustomSelect(select) 38 function CustomSelect(select)
38 { 39 {
39 this.select = select; 40 this.select = select;
40 this.close(); 41 this.close();
41 select.addEventListener("click", this._onClick.bind(this), false); 42 this.select
43 .addEventListener("click", this._onClick.bind(this), false);
44 this.select
45 .addEventListener("focusout", this._onFocusOut.bind(this), false);
46 }
47
48 CustomSelect.prototype._onFocusOut = function()
49 {
50 // setTimeout to allow document.activeElement
51 // to move to newly focused element
52 setTimeout(function()
53 {
54 var newFocus = document.activeElement;
55
56 if (newFocus
57 .classList.contains("custom-select-selected") ||
58 newFocus
59 .classList.contains("custom-select-option") ||
60 newFocus
61 .parentElement
62 .classList.contains("custom-select-option"))
63 {
64 return;
65 }
66
67 this.close();
68
69 }.bind(this), 1);
42 } 70 }
43 71
44 CustomSelect.prototype._onClick = function(event) 72 CustomSelect.prototype._onClick = function(event)
45 { 73 {
46 if (event.target.classList.contains("custom-select-selected")) 74 if (event.target.classList.contains("custom-select-selected"))
47 { 75 {
48 var options = this.select.querySelector(".custom-select-options"); 76 var options = this.select.querySelector(".custom-select-options");
49 if (options.getAttribute("aria-hidden") == "true") 77 if (options.getAttribute("aria-hidden") == "true")
50 { 78 {
51 this.open(); 79 this.open();
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 var productTopicsAccordion = document.getElementById('product-topics-accordi on'); 224 var productTopicsAccordion = document.getElementById('product-topics-accordi on');
197 if (productTopicsAccordion) 225 if (productTopicsAccordion)
198 { 226 {
199 new Accordion(productTopicsAccordion); 227 new Accordion(productTopicsAccordion);
200 } 228 }
201 229
202 /************************************************************************** 230 /**************************************************************************
203 * BrowserSelect 231 * BrowserSelect
204 **************************************************************************/ 232 **************************************************************************/
205 233
206 function BrowserSelect(select) 234 function BrowserSelect(select)
juliandoucette 2017/10/13 13:39:56 Note: You could extend CustomSelect's prototype (e
ire 2017/10/17 15:03:33 Cool. I'll keep what i did here then.
207 { 235 {
208 this.browserSelect = select; 236 this.select = select;
209 this.browserCustomSelect = new CustomSelect(this.browserSelect); 237 CustomSelect.apply(this, [this.select]);
210 238
211 this.DEFAULT_BROWSER = "chrome";
212 this.BROWSER_STORAGE_KEY = "BROWSER"; 239 this.BROWSER_STORAGE_KEY = "BROWSER";
213 this.BROWSER_AUTODETECTED_STORAGE_KEY = "BROWSER_AUTODETECTED"; 240 this.BROWSER_AUTODETECTED_STORAGE_KEY = "BROWSER_AUTODETECTED";
214 241 this.SUPPORTED_BROWSERS = ["chrome", "opera", "samsungBrowser",
215 this.browserSelect 242 "yandexbrowser", "maxthon", "msie",
243 "msedge", "firefox", "ios", "safari"];
244 this.DEFAULT_BROWSER = "chrome";
245
246 this.select
216 .addEventListener("click", this._onClickOrKeyDown.bind(this), false); 247 .addEventListener("click", this._onClickOrKeyDown.bind(this), false);
217 248
juliandoucette 2017/10/13 13:39:56 NIT: You should be able to click-away too (click e
ire 2017/10/17 15:03:34 Yes, I will fix this in a separate issue https://
juliandoucette 2017/10/18 15:04:10 Acknowledged.
218 this.browserSelect 249 this.select
219 .addEventListener("keydown", this._onClickOrKeyDown.bind(this), false); 250 .addEventListener("keydown", this._onClickOrKeyDown.bind(this), false);
220 251
221 var storedBrowser = localStorage.getItem(this.BROWSER_STORAGE_KEY); 252 var storedBrowser = localStorage.getItem(this.BROWSER_STORAGE_KEY);
222 if (storedBrowser) 253 if (storedBrowser) this.selectOption(storedBrowser);
223 { 254 else this.detectBrowser();
224 this.selectOption(storedBrowser); 255 }
225 } 256
226 else 257 BrowserSelect.prototype = Object.create(CustomSelect.prototype);
227 { 258 BrowserSelect.prototype.constructor = BrowserSelect;
228 this.detectBrowser();
229 }
230 }
231 259
232 BrowserSelect.prototype.detectBrowser = function() 260 BrowserSelect.prototype.detectBrowser = function()
233 { 261 {
234 localStorage.setItem(this.BROWSER_AUTODETECTED_STORAGE_KEY, "true"); 262 for (var i = 0; i < this.SUPPORTED_BROWSERS.length; i++)
235 263 {
236 var browser; 264 var supportedBrowser = this.SUPPORTED_BROWSERS[i];
237 if (bowser.chrome) browser = "chrome"; 265 if (bowser[supportedBrowser])
juliandoucette 2017/10/13 13:39:55 Wait, what? How can browser have properties if it
ire 2017/10/17 15:03:34 You can override previously defined (or undefined)
juliandoucette 2017/10/18 15:04:10 Can you elaborate / explain when/how this variable
ire 2017/10/20 13:40:46 Sure! I create a pen to illustrate this - https:/
238 else if (bowser.opera) browser = "opera"; 266 {
239 else if (bowser.coast) browser = "opera"; 267 localStorage.setItem(this.BROWSER_AUTODETECTED_STORAGE_KEY, "true");
240 else if (bowser.samsungBrowser) browser = "samsung"; 268 return this.selectOption(supportedBrowser);
241 else if (bowser.yandexbrowser) browser = "yandex"; 269 }
242 else if (bowser.maxthon) browser = "maxthon"; 270 }
243 else if (bowser.msie) browser = "ie"; 271
244 else if (bowser.msedge) browser = "edge"; 272 this.selectOption(this.DEFAULT_BROWSER);
245 else if (bowser.firefox) browser = "firefox"; 273 };
246 else if (bowser.ios) browser = "ios";
247 else if (bowser.safari) browser = "safari";
juliandoucette 2017/10/18 15:04:10 Why check each browser property and not *just* bro
ire 2017/10/20 13:40:46 I felt it wouldn't be as reliable/steady as this.
248 else
249 {
250 localStorage.removeItem(this.BROWSER_AUTODETECTED_STORAGE_KEY);
251 browser = this.DEFAULT_BROWSER;
252 }
253
254 this.selectOption(browser);
255 }
256 274
257 BrowserSelect.prototype.selectOption = function(browser) 275 BrowserSelect.prototype.selectOption = function(browser)
258 { 276 {
259 // Save value to Local Storage
260 localStorage.setItem(this.BROWSER_STORAGE_KEY, browser); 277 localStorage.setItem(this.BROWSER_STORAGE_KEY, browser);
261 278
262 // Change body class 279 // Change body class
263 document.body.className = "ua-"+browser; 280 var bodyClassList = Array.prototype.slice.call(document.body.classList);
juliandoucette 2017/10/13 13:39:55 What if there are not related classes attached to
juliandoucette 2017/10/13 13:41:36 unrelated*
ire 2017/10/17 15:03:34 Done.
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");
264 294
265 // Set selected option 295 // Set selected option
266 var selectedOption = document.createElement("li"); 296 var selectedOption = selectedItem.innerHTML;
267 selectedOption.innerHTML = this
268 .browserSelect
juliandoucette 2017/10/13 13:39:55 NIT: spaces around +?
ire 2017/10/17 15:03:34 I thought we don't put spaces within the ()?
juliandoucette 2017/10/18 15:04:11 We don't put spaces after the opening brace and be
ire 2017/10/20 13:40:48 Alright, makes sense. Done.
269 .querySelector("[data-value='"+browser+"']")
270 .innerHTML;
271 297
272 if (localStorage.getItem(this.BROWSER_AUTODETECTED_STORAGE_KEY)) 298 if (localStorage.getItem(this.BROWSER_AUTODETECTED_STORAGE_KEY))
273 { 299 {
274 selectedOption.innerHTML += "<span class='muted'>(autodetected)</span>"; 300 var autodetected = document
juliandoucette 2017/10/13 13:39:55 This isn't translated. I suggest translating it in
ire 2017/10/17 15:03:34 I place it in a template so I wouldn't have to hav
juliandoucette 2017/10/18 15:04:10 You mean you will place it in a template? Or somet
ire 2017/10/20 13:40:48 Sorry I meant to say "I will place it in a templat
275 } 301 .getElementById('browser-select-autodetected')
276 302 .innerHTML;
277 this.browserSelect 303 selectedOption += "<span class='muted'>(" + autodetected + ")</span>";
278 .querySelector(".custom-select-selected") 304 }
279 .innerHTML = selectedOption.innerHTML; 305
280 } 306 this.select
307 .querySelector(".custom-select-selected")
308 .innerHTML = selectedOption;
309 };
281 310
282 BrowserSelect.prototype._onClickOrKeyDown = function(event) 311 BrowserSelect.prototype._onClickOrKeyDown = function(event)
283 { 312 {
284 if (!event.target.classList.contains("custom-select-option")) return; 313 if (!event.target.classList.contains("custom-select-option")) return;
285 314
286 var IS_ENTER_KEY = event.key == "Enter" || event.keyCode == 13; 315 var IS_ENTER_KEY = event.key == "Enter" || event.keyCode == 13;
287 if (event.keyCode && !IS_ENTER_KEY) return; 316 if (event.keyCode && !IS_ENTER_KEY) return;
juliandoucette 2017/10/13 13:39:56 Why check event.keyCode here?
ire 2017/10/17 15:03:33 Because this function is also relevant for clicks.
juliandoucette 2017/10/18 15:04:11 Acknowledged. I was confused.
288 317
289 localStorage.removeItem(this.BROWSER_AUTODETECTED_STORAGE_KEY); 318 localStorage.removeItem(this.BROWSER_AUTODETECTED_STORAGE_KEY);
290 319
320 // Uncheck previously checked option
321 this.select
322 .querySelector("[aria-checked='true']")
323 .setAttribute("aria-checked", "false");
324
291 this.selectOption(event.target.getAttribute("data-value")); 325 this.selectOption(event.target.getAttribute("data-value"));
292 326
293 // Close Select 327 this.close();
294 this.browserCustomSelect.close(); 328 };
295 }
296 329
297 var browserSelect = document.getElementById("browser-select"); 330 var browserSelect = document.getElementById("browser-select");
298 if (browserSelect) 331 if (browserSelect)
299 { 332 {
300 new BrowserSelect(browserSelect); 333 new BrowserSelect(browserSelect);
301 } 334 }
302 335
303 }, false); 336 }, false);
304 }()); 337 }());
LEFTRIGHT

Powered by Google App Engine
This is Rietveld