Added full config logic

Added state display
This commit is contained in:
Max W. 2024-05-18 13:08:34 +02:00
parent 99f146aa1d
commit c2826b8a08
3 changed files with 103 additions and 36 deletions

View File

@ -48,17 +48,17 @@
.toggle-switch input[type="checkbox"]:checked + .toggle-switch-handle {
transform: translateX(45px);
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2), 0 0 0 3px #05c46b;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2), 0 0 0 3px #198754;
}
.toggle-switch input[type="checkbox"]:checked + .toggle-switch-background {
background-color: #05c46b;
box-shadow: inset 0 0 0 2px #04b360;
background-color: #198754;
box-shadow: inset 0 0 0 2px #198754;
}
.toggle-switch input[type="checkbox"]:checked + .toggle-switch:before {
content: "On";
color: #05c46b;
color: #198754;
right: -15px;
}

View File

@ -1,5 +1,9 @@
<div class="position-relative">
<button type="button" class="btn btn-light back-button top-0 start-0 m-3" onclick="loadView('home')">Back</button>
<button type="button" class="btn btn-light back-button top-0 start-0 m-3" onclick="loadView('home')">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-left" viewBox="0 0 16 16">
<path fill-rule="evenodd" d="M15 8a.5.5 0 0 0-.5-.5H2.707l3.147-3.146a.5.5 0 1 0-.708-.708l-4 4a.5.5 0 0 0 0 .708l4 4a.5.5 0 0 0 .708-.708L2.707 8.5H14.5A.5.5 0 0 0 15 8"/>
</svg>
Back</button>
</div>
<section class="py-5">
<div class="container text-center">
@ -7,8 +11,8 @@
<p class="lead mb-5">Create and manage filtering rules</p>
<!-- CONTENT -->
<div class="d-flex justify-content-center" id="add-rule-btn">
<button class="btn btn-success mx-2">
<div class="d-flex justify-content-center">
<button class="btn btn-success mx-2" id="add-rule-btn">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-plus-lg" viewBox="0 0 16 16">
<path fill-rule="evenodd" d="M8 2a.5.5 0 0 1 .5.5v5h5a.5.5 0 0 1 0 1h-5v5a.5.5 0 0 1-1 0v-5h-5a.5.5 0 0 1 0-1h5v-5A.5.5 0 0 1 8 2"/>
</svg>
@ -19,6 +23,12 @@
<path d="M14.5 3a1 1 0 0 1-1 1H13v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V4h-.5a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1H6a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1h3.5a1 1 0 0 1 1 1zM4.118 4 4 4.059V13a1 1 0 0 0 1 1h6a1 1 0 0 0 1-1V4.059L11.882 4zM2.5 3h11V2h-11z"/>
</svg>
Clear rules</button>
<button type="button" class="btn btn-dark">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-box-arrow-right" viewBox="0 0 16 16">
<path fill-rule="evenodd" d="M10 12.5a.5.5 0 0 1-.5.5h-8a.5.5 0 0 1-.5-.5v-9a.5.5 0 0 1 .5-.5h8a.5.5 0 0 1 .5.5v2a.5.5 0 0 0 1 0v-2A1.5 1.5 0 0 0 9.5 2h-8A1.5 1.5 0 0 0 0 3.5v9A1.5 1.5 0 0 0 1.5 14h8a1.5 1.5 0 0 0 1.5-1.5v-2a.5.5 0 0 0-1 0z"/>
<path fill-rule="evenodd" d="M15.854 8.354a.5.5 0 0 0 0-.708l-3-3a.5.5 0 0 0-.708.708L14.293 7.5H5.5a.5.5 0 0 0 0 1h8.793l-2.147 2.146a.5.5 0 0 0 .708.708z"/>
</svg>
Export config</button>
</div>
<table class="table mt-5 table-borderless">
<thead>
@ -31,30 +41,26 @@
</tr>
</thead>
<tbody>
<tr class="border-bottom" id="rule-1">
<td scope="row" class="align-middle"><p class="mb-0">1</p></td>
<td class="align-middle"><input type="text" class="form-control rule-input-str-input" placeholder="String to replace"></td>
<td class="align-middle"><input type="text" class="form-control rule-output-str-input" placeholder="Replace with"></td>
<td class="align-middle">
<label class="toggle-switch">
<input class="rule-enabled-by-default-toggle" type="checkbox" checked>
<div class="toggle-switch-background">
<div class="toggle-switch-handle"></div>
</div>
</label>
</td>
<td class="align-middle">
<button class="btn btn-danger rule-trash-btn">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-trash" viewBox="0 0 16 16">
<path d="M5.5 5.5A.5.5 0 0 1 6 6v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5m2.5 0a.5.5 0 0 1 .5.5v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5m3 .5a.5.5 0 0 0-1 0v6a.5.5 0 0 0 1 0z"/>
<path d="M14.5 3a1 1 0 0 1-1 1H13v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V4h-.5a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1H6a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1h3.5a1 1 0 0 1 1 1zM4.118 4 4 4.059V13a1 1 0 0 0 1 1h6a1 1 0 0 0 1-1V4.059L11.882 4zM2.5 3h11V2h-11z"/>
</svg>
</button>
</td>
</tr>
<!-- Additional rows can be added here -->
<!-- ROWS -->
</tbody>
</table>
<div class="text-center mt-3" id="config-save-status">
<div id="saving-status">
<div class="spinner-border spinner-border-sm" role="status">
<span class="visually-hidden">Saving config...</span>
</div>
<p>Saving...</p>
</div>
<div id="saved-status" style="display: none;">
<p class="mb-0">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-check-lg" viewBox="0 0 16 16">
<path d="M12.736 3.97a.733.733 0 0 1 1.047 0c.286.289.29.756.01 1.05L7.88 12.01a.733.733 0 0 1-1.065.02L3.217 8.384a.757.757 0 0 1 0-1.06.733.733 0 0 1 1.047 0l3.052 3.093 5.4-6.425z"/>
</svg>
Configuration state up to date.
</p>
</div>
</div>
</div>
</section>

View File

@ -19,24 +19,32 @@ var ruleRowTemplate = '<tr class="border-bottom" id="rule-[INDEX]">' +
' </button>' +
' </td>' +
' </tr>';
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) {
@ -44,3 +52,56 @@ function generateRowTemplate(str, outputStr, enabled) {
}
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();
});
}