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

Unified Diff: options.js

Issue 29321198: Issue 2376 - Implement custom filters in new options page (Closed)
Patch Set: Addressed initial comments Created July 8, 2015, 6:13 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: options.js
===================================================================
--- a/options.js
+++ b/options.js
@@ -38,8 +38,8 @@
this.items.sort(function(a, b)
{
- var aValue = (a.title || a.url || a.text).toLowerCase();
- var bValue = (b.title || b.url || a.text).toLowerCase();
+ var aValue = (a.title || a.text || a.url).toLowerCase();
+ var bValue = (b.title || b.text || b.url).toLowerCase();
return aValue.localeCompare(bValue);
});
@@ -170,6 +170,12 @@
onClick: onRemoveFilterClick
}
]);
+ collections.customFilters = new Collection(
+ [
+ {
+ id: "custom-filters-table"
+ }
+ ]);
function updateSubscription(subscription)
{
@@ -266,12 +272,11 @@
{
filter.title = match[1];
collections.whitelist.addItems(filter);
- filtersMap[filter.text] = filter
}
else
- {
- // TODO: add `filters[i].text` to list of custom filters
- }
+ collections.customFilters.addItems(filter);
+
+ filtersMap[filter.text] = filter;
}
function loadRecommendations()
@@ -345,6 +350,13 @@
searchStyle.innerHTML = "#all-lang-table li:not([data-search*=\"" + this.value.toLowerCase() + "\"]) { display: none; }";
}
+ function isEnterPressed(e)
+ {
+ // e.keyCode has been deprecated so we attempt to use e.key
+ // keyCode "13" corresponds to "Enter"
+ return (e.key && e.key == "Enter") || (!e.key && e.keyCode == 13);
Sebastian Noack 2015/07/09 12:12:15 Please don't duplicate the check for presence of e
saroyanm 2015/07/09 16:31:42 Done.
+ }
+
// Initialize navigation sidebar
ext.backgroundPage.sendMessage(
{
@@ -395,9 +407,7 @@
E("whitelisting-add-button").addEventListener("click", addWhitelistedDomain, false);
E("whitelisting-textbox").addEventListener("keypress", function(e)
{
- // e.keyCode has been deprecated so we attempt to use e.key
- // keyCode "13" corresponds to "Enter"
- if ((e.key && e.key == "Enter") || (!e.key && e.keyCode == 13))
+ if (isEnterPressed(e))
addWhitelistedDomain();
}, false);
E("import-blockingList-button").addEventListener("click", function()
@@ -406,6 +416,66 @@
addEnableSubscription(url);
delete document.body.dataset.dialog;
Sebastian Noack 2015/07/09 12:12:15 I wasn't involved in the initial review, but just
saroyanm 2015/07/09 16:31:41 According to MDN dataset is supported since Safari
Sebastian Noack 2015/07/10 07:31:00 See https://codereview.adblockplus.org/29321417/di
saroyanm 2015/07/13 14:05:34 Yes Its should be fixed with the #2357
}, false);
+
+ // Advanced tab
+ var filterTextbox = document.querySelector("#custom-filter-add input");
+ placeholderValue = ext.i18n.getMessage("options_customFilters_textbox_placeholder");
+ filterTextbox.setAttribute("placeholder", placeholderValue);
+ function addCustomFilters()
+ {
+ ext.backgroundPage.sendMessage(
+ {
+ type: "filter.parse",
+ text: filterTextbox.value
+ },
+ function(result)
+ {
+ if (result.error)
+ {
+ alert(result.error);
+ return;
+ }
+ if (result.filter)
+ {
+ ext.backgroundPage.sendMessage(
+ {
+ type: "filters.add",
+ text: result.filter.text
+ });
+ }
+
+ filterTextbox.value = "";
+ });
+ }
+ E("custom-filter-add").addEventListener("submit", function(e)
+ {
+ e.preventDefault();
+ addCustomFilters();
+ }, false);
+ var customFilterEditButtons = document.querySelectorAll("#custom-filters-edit-wrapper button");
+ E("custom-filters-edit-wrapper").addEventListener("click", function(e)
+ {
+ var target = null;
+ if (e.target.tagName == "BUTTON")
Thomas Greiner 2015/07/09 11:07:56 Use `e.target.localName == "button"` instead becau
saroyanm 2015/07/09 16:31:42 Done.
+ target = e.target;
+ else if (e.target.parentElement.tagName == "BUTTON")
+ target = e.target.parentElement;
+ else
+ return;
Thomas Greiner 2015/07/09 11:07:56 The most flexible way to achieve this is to go up
saroyanm 2015/07/09 16:31:42 Should we keep it as it is now and add merge it in
Thomas Greiner 2015/07/10 12:38:53 Yes, either in #2357 or a later review.
+
+ var id = target.id;
+ E("custom-filters").classList.toggle("mode-edit");
Sebastian Noack 2015/07/09 12:12:15 classList.toggle() isn't supported in older Safari
saroyanm 2015/07/09 16:31:42 According to MDN it's supported from 5.1, but webk
Sebastian Noack 2015/07/10 07:31:00 It might be that I confused this with the second p
+ if (id == "custom-filters-edit-btn")
+ editCustomFilters();
+ else if (id == "custom-filters-save-btn")
+ {
+ ext.backgroundPage.sendMessage(
+ {
+ type: "filters.importRaw",
+ text: E("custom-filters-raw").value
+ });
+ }
+ }, false);
}
function openDialog(name)
@@ -485,7 +555,11 @@
function editCustomFilters()
{
- //TODO: NYI
+ var customFilterItems = collections.customFilters.items;
+ var filterTexts = [];
+ for (var i = 0; i < customFilterItems.length; i++)
+ filterTexts.push(customFilterItems[i].text);
+ E("custom-filters-raw").value = filterTexts.join("\n");
}
function getAcceptableAdsURL(callback)
@@ -558,6 +632,7 @@
case "removed":
var knownFilter = filtersMap[filter.text];
collections.whitelist.removeItem(knownFilter);
+ collections.customFilters.removeItem(knownFilter);
delete filtersMap[filter.text];
updateShareLink();
break;

Powered by Google App Engine
This is Rietveld