@@ -7,8 +11,8 @@
Create and manage filtering rules
-
-
+
+
@@ -19,6 +23,12 @@
Clear rules
+
+
+
+
+
+ Export config
+
+
+
+ Saving config...
+
+
Saving...
+
+
+
+
+
+
+ Configuration state up to date.
+
+
+
+
+
\ No newline at end of file
diff --git a/src/views/config/config.js b/src/views/config/config.js
index 50b0b62..8be2429 100644
--- a/src/views/config/config.js
+++ b/src/views/config/config.js
@@ -19,28 +19,89 @@ var ruleRowTemplate = '
' +
' ' +
' ' +
' ';
+var configJson = {};
$("document").ready(function() {
+ loadConfig();
+
$("#add-rule-btn").click(function() {
appendRow(generateRowTemplate("", "", true));
});
+
+ $(document).on("click", ".rule-trash-btn", function() {
+ $(this).closest("tr").remove();
+ updateRowIndexes();
+ buildAndSaveConfig();
+ });
+ bindChangeEvents();
});
function appendRow(rowTemplate) {
- $(".table").append(rowTemplate);
- $(".rule-trash-btn").click(function() {
- $(this).closest("tr").remove();
- });
+ $(".table tbody").append(rowTemplate);
+ bindChangeEvents();
+ updateRowIndexes();
}
-
function generateRowTemplate(str, outputStr, enabled) {
var row = ruleRowTemplate;
- row = row.replaceAll("[INDEX]", $(".table tr").length);
+ row = row.replaceAll("[INDEX]", $(".table tbody tr").length + 1);
row = row.replace("[STR-TO-REPLACE]", str);
row = row.replace("[REPLACE-STR]", outputStr);
if (!enabled) {
row = row.replace("checked", "");
}
return row;
+}
+
+function updateRowIndexes() {
+ $(".table tbody tr").each(function(index) {
+ $(this).attr("id", "rule-" + (index + 1));
+ $(this).find("td:first p").text(index + 1);
+ });
+}
+
+function buildAndSaveConfig() {
+ // Show saving status
+ $("#saving-status").show();
+ $("#saved-status").hide();
+
+ var rules = [];
+ $(".table tbody tr").each(function(index) {
+ var rule = {};
+ rule["strToReplace"] = $(this).find(".rule-input-str-input").val();
+ rule["replaceStr"] = $(this).find(".rule-output-str-input").val();
+ rule["enabledByDefault"] = $(this).find(".rule-enabled-by-default-toggle").is(":checked");
+ rules.push(rule);
+ });
+ configJson["rules"] = rules;
+ console.log(configJson);
+ window.localStorage.setItem("config", JSON.stringify(configJson));
+
+ // Simulate a delay for saving process
+ setTimeout(function() {
+ $("#saving-status").hide();
+ $("#saved-status").show();
+ }, 200);
+}
+
+function loadConfig() {
+ var config = window.localStorage.getItem("config");
+ if (config) {
+ configJson = JSON.parse(config);
+ configJson["rules"].forEach(function(rule) {
+ appendRow(generateRowTemplate(rule["strToReplace"], rule["replaceStr"], rule["enabledByDefault"]));
+ });
+ }
+ $("#saving-status").hide();
+ $("#saved-status").show();
+ bindChangeEvents();
+}
+
+function bindChangeEvents() {
+ $(".rule-input-str-input, .rule-output-str-input").off("input").on("input", function() {
+ buildAndSaveConfig();
+ });
+ $(".rule-enabled-by-default-toggle").off("change").on("change", function() {
+ buildAndSaveConfig();
+ });
}
\ No newline at end of file