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: Created June 29, 2015, 11:21 a.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)
Thomas Greiner 2015/06/30 09:23:29 You need to first filter out all empty lines befor
saroyanm 2015/07/08 18:25:42 I do wander why we even need to submit the empty s
Thomas Greiner 2015/07/09 11:07:54 Yep, the earlier we can filter those out the bette
saroyanm 2015/07/09 16:31:39 Done.
{
- 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 || a.url).toLowerCase();
Thomas Greiner 2015/06/30 09:23:27 Be careful: It's `b.url`, not `a.url`
saroyanm 2015/07/08 18:25:41 Done.
return aValue.localeCompare(bValue);
});
@@ -170,6 +170,12 @@
onClick: onRemoveFilterClick
}
]);
+ collections.customFilters = new Collection(
+ [
+ {
+ id: "custom-filters-table"
+ }
+ ]);
function updateSubscription(subscription)
{
@@ -266,11 +272,13 @@
{
filter.title = match[1];
collections.whitelist.addItems(filter);
- filtersMap[filter.text] = filter
+ filtersMap[filter.text] = filter;
}
else
{
// TODO: add `filters[i].text` to list of custom filters
Thomas Greiner 2015/06/30 09:23:28 Remove this comment
saroyanm 2015/07/08 18:25:42 Done.
+ collections.customFilters.addItems(filter);
+ filtersMap[filter.text] = filter;
Thomas Greiner 2015/06/30 09:23:28 This line is shared across both branches of the if
saroyanm 2015/07/08 18:25:41 Done.
}
}
@@ -346,6 +354,15 @@
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"
+ if ((e.key && e.key == "Enter") || (!e.key && e.keyCode == 13))
+ return true;
+ return false;
Thomas Greiner 2015/06/30 09:23:28 Detail: You could reduce that to one line: return
saroyanm 2015/07/08 18:25:41 Done. Please note that this function most probably
+ }
+
// Update version number in navigation sidebar
ext.backgroundPage.sendMessage(
{
@@ -384,9 +401,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()
@@ -395,6 +410,55 @@
addEnableSubscription(url);
delete document.body.dataset.dialog;
}, false);
+
+ // Advanced tab
+ E("custom-filters-add-textbox").setAttribute("placeholder", ext.i18n.getMessage("options_customFilters_textbox_placeholder"));
Thomas Greiner 2015/06/30 09:23:28 Split this up into two lines to avoid exceeding th
saroyanm 2015/07/08 18:25:41 Done.
+ function addCustomFilter()
Thomas Greiner 2015/06/30 09:23:28 "addCustomFilters" please
saroyanm 2015/07/08 18:25:41 Done.
+ {
+ var filterTextbox = E("custom-filters-add-textbox");
+ ext.backgroundPage.sendMessage(
+ {
+ type: "parse.filter",
+ text: filterTextbox.value
+ },
+ function(result)
+ {
+ if (result.error)
+ {
+ alert(result.error);
+ return;
+ }
+ if (result.filter)
+ addFilter(result.filter.text);
+
+ filterTextbox.value = "";
+ });
+ }
+ E("custom-filters-add-btn").addEventListener("click", addCustomFilter, false);
+ E("custom-filters-add-textbox").addEventListener("keypress", function(e)
+ {
+ if (isEnterPressed(e))
+ addCustomFilter();
+ }, false);
+ var customFilterEditButtons = document.querySelectorAll("#custom-filters-edit-wrapper button");
Thomas Greiner 2015/06/30 09:23:28 Please avoid setting event listeners individually
saroyanm 2015/07/08 18:25:40 Done.
+ for (var i = 0; i < customFilterEditButtons.length; i++)
+ {
+ customFilterEditButtons[i].addEventListener("click", function(e)
+ {
+ E("custom-filters").dataset.view = e.currentTarget.dataset.show;
Thomas Greiner 2015/06/30 09:23:28 This can be achieved more performantly by toggling
saroyanm 2015/07/08 18:25:41 Done.
+ var id = e.currentTarget.id;
+ if (id == "custom-filters-edit-btn")
+ editCustomFilters();
+ else if (id == "custom-filters-save-btn")
+ {
+ ext.backgroundPage.sendMessage(
+ {
+ type: "filters.importRaw",
+ text: E("custom-filters-textarea").value
+ });
+ }
+ }, false);
+ }
}
function openDialog(name)
@@ -474,7 +538,11 @@
function editCustomFilters()
{
- //TODO: NYI
+ var customFilterItems = collections.customFilters.items;
+ var text = "";
+ for (var i = 0; i < customFilterItems.length; i++)
+ text += customFilterItems[i].text + "\n";
+ E("custom-filters-textarea").value = text;
Thomas Greiner 2015/06/30 09:23:28 This loop is potentially creating a lot of strings
saroyanm 2015/07/08 18:25:41 Done.
}
function getAcceptableAdsURL(callback)
@@ -533,6 +601,15 @@
});
}
+ function addFilter(filter)
Thomas Greiner 2015/06/30 09:23:29 There's no need to create a function for that beca
saroyanm 2015/07/08 18:25:41 Done. Please note that we still have similar metho
Thomas Greiner 2015/07/09 11:07:54 Good point, feel free to include that in this revi
saroyanm 2015/07/09 16:31:39 Done.
+ {
+ ext.backgroundPage.sendMessage(
+ {
+ type: "filters.add",
+ text: filter
+ });
+ }
+
function onFilterMessage(action, filter)
{
switch (action)
@@ -547,6 +624,7 @@
case "removed":
var knownFilter = filtersMap[filter.text];
collections.whitelist.removeItem(knownFilter);
+ collections.customFilters.removeItem(knownFilter);
Thomas Greiner 2015/06/30 09:23:28 Note that when I click on "edit view" and right af
saroyanm 2015/07/08 18:25:41 The problem is that in "filters.importRaw" we were
delete filtersMap[filter.text];
updateShareLink();
break;

Powered by Google App Engine
This is Rietveld