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

Unified 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.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: static/js/main.js
===================================================================
--- a/static/js/main.js
+++ b/static/js/main.js
@@ -194,10 +194,129 @@
}
var productTopicsAccordion = document.getElementById('product-topics-accordion');
if (productTopicsAccordion)
{
new Accordion(productTopicsAccordion);
}
+ /**************************************************************************
+ * BrowserSelect
+ **************************************************************************/
+
+ function BrowserSelect(select)
+ {
+ 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.
+ 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.
+
+ this.DEFAULT_BROWSER = "chrome";
+ this.BROWSER_STORAGE_KEY = "BROWSER";
+ this.BROWSER_AUTODETECTED_STORAGE_KEY = "BROWSER_AUTODETECTED";
+
+ this.browserSelect
+ .addEventListener("click", this._onClickOrKeyDown.bind(this), false);
+
+ this.browserSelect
+ .addEventListener("keydown", this._onClickOrKeyDown.bind(this), false);
+
+ 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
+ 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.
+ {
+ this.selectOption(storedBrowser);
+ }
+ else
+ {
+ this.detectBrowser();
+ }
+ }
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
+
+ BrowserSelect.prototype.detectBrowser = function()
+ {
+ localStorage.setItem(this.BROWSER_AUTODETECTED_STORAGE_KEY, "true");
+
+ var browser;
+ 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.
+ else if (bowser.opera) browser = "opera";
+ else if (bowser.coast) browser = "opera";
+ else if (bowser.samsungBrowser) browser = "samsung";
+ else if (bowser.yandexbrowser) browser = "yandex";
+ else if (bowser.maxthon) browser = "maxthon";
+ else if (bowser.msie) browser = "ie";
+ else if (bowser.msedge) browser = "edge";
+ else if (bowser.firefox) browser = "firefox";
+ else if (bowser.ios) browser = "ios";
+ else if (bowser.safari) browser = "safari";
+ else
+ {
+ localStorage.removeItem(this.BROWSER_AUTODETECTED_STORAGE_KEY);
+ browser = this.DEFAULT_BROWSER;
+ }
+
+ this.selectOption(browser);
+ }
+
+ BrowserSelect.prototype.selectOption = function(browser)
+ {
+ // 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.
+ localStorage.setItem(this.BROWSER_STORAGE_KEY, browser);
+
+ // Change body class
+ 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.
+ for (var i = 0; i < bodyClassList.length; i++)
+ {
+ if (bodyClassList[i].indexOf('ua-') > -1)
+ {
+ document.body.classList.remove(bodyClassList[i]);
+ }
+ }
+ document.body.classList.add("ua-" + browser);
+
+ // Check selected option
+ 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!
+ .querySelector("[data-value='" + browser + "']");
+ selectedItem.setAttribute("aria-checked", "true");
+
+ // Set selected option
+ 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.
+ selectedOption.innerHTML = selectedItem.innerHTML;
+
+ if (localStorage.getItem(this.BROWSER_AUTODETECTED_STORAGE_KEY))
+ {
+ var autodetected = document
+ .getElementById('browser-select-autodetected')
+ .innerHTML;
+ selectedOption.innerHTML += "<span class='muted'>(" + autodetected + ")</span>";
+ }
+
+ this.browserSelect
+ .querySelector(".custom-select-selected")
+ .innerHTML = selectedOption.innerHTML;
+ }
+
+ BrowserSelect.prototype._onClickOrKeyDown = function(event)
+ {
+ if (!event.target.classList.contains("custom-select-option")) return;
+
+ var IS_ENTER_KEY = event.key == "Enter" || event.keyCode == 13;
+ if (event.keyCode && !IS_ENTER_KEY) return;
+
+ localStorage.removeItem(this.BROWSER_AUTODETECTED_STORAGE_KEY);
+
+ // Uncheck previously checked option
+ this.browserSelect
+ .querySelector("[aria-checked='true']")
+ .setAttribute("aria-checked", "false");
+
+ this.selectOption(event.target.getAttribute("data-value"));
+
+ // Close Select
+ this.browserCustomSelect.close();
+ }
+
+ var browserSelect = document.getElementById("browser-select");
+ if (browserSelect)
+ {
+ new BrowserSelect(browserSelect);
+ }
+
}, false);
}());

Powered by Google App Engine
This is Rietveld