Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify domain lists, remove a lot of duplicated code #1154

Merged
merged 5 commits into from Feb 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 22 additions & 7 deletions groups-domains.php
Expand Up @@ -6,11 +6,19 @@
* This file is copyright under the latest version of the EUPL.
* Please see LICENSE file for your rights under this license. */
require "scripts/pi-hole/php/header.php";
$type = "all";
$pagetitle = "Domain";
$adjective = "";
if (isset($_GET['type']) && ($_GET['type'] === "white" || $_GET['type'] === "black")) {
$type = $_GET['type'];
$pagetitle = ucfirst($type)."list";
$adjective = $type."listed";
}
?>

<!-- Title -->
<div class="page-header">
<h1>Domain group management</h1>
<h1><?php echo $pagetitle; ?> management</h1>
</div>

<!-- Domain Input -->
Expand All @@ -20,7 +28,7 @@
<!-- /.box-header -->
<div class="box-header with-border">
<h3 class="box-title">
Add a new domain
Add a new <?php echo $adjective; ?> domain
</h3>
</div>
<!-- /.box-header -->
Expand All @@ -33,10 +41,17 @@
<div class="col-md-2">
<label for="ex2">Type:</label>
<select id="new_type" class="form-control">
<option value="0">Exact whitelist</option>
<option value="1">Exact blacklist</option>
<option value="2">Regex whitelist</option>
<option value="3">Regex blacklist</option>
<?php if($type === "all" || $type === "white") { ?>
<option value="0">Exact whitelist</option>
<option value="2">Regex whitelist</option>
<?php } if($type === "white") { ?>
<option value="2W">Wildcard whitelist</option>
<?php } if($type === "all" || $type === "black") { ?>
<option value="1">Exact blacklist</option>
<option value="3">Regex blacklist</option>
<?php } if($type === "black") { ?>
<option value="3W">Wildcard blacklist</option>
<?php } ?>
</select>
</div>
<div class="col-md-4">
Expand All @@ -56,7 +71,7 @@
<div class="box" id="domains-list">
<div class="box-header with-border">
<h3 class="box-title">
List of configured domains
List of <?php echo $adjective; ?> domains
</h3>
</div>
<!-- /.box-header -->
Expand Down
84 changes: 0 additions & 84 deletions list.php

This file was deleted.

84 changes: 55 additions & 29 deletions scripts/pi-hole/js/groups-domains.js
Expand Up @@ -11,6 +11,7 @@ var table;
var groups = [];
var token = $("#token").html();
var GETDict = {};
var showtype = "all";

function get_groups() {
$.post(
Expand All @@ -32,6 +33,10 @@ $(document).ready(function() {
GETDict[item.split("=")[0]] = item.split("=")[1];
});

if ("type" in GETDict && (GETDict.type === "white" || GETDict.type === "black")) {
showtype = GETDict.type;
}

$("#btnAdd").on("click", addDomain);

get_groups();
Expand All @@ -46,7 +51,7 @@ function initTable() {
table = $("#domainsTable").DataTable({
ajax: {
url: "scripts/pi-hole/php/groups.php",
data: { action: "get_domains", token: token },
data: { action: "get_domains", showtype: showtype, token: token },
type: "POST"
},
order: [[0, "asc"]],
Expand All @@ -71,23 +76,35 @@ function initTable() {
"\nDatabase ID: " +
data.id;
$("td:eq(0)", row).html(
'<code id="domain" title="' + tooltip + '">' + data.domain + "</code>"
'<code id="domain" title="' + tooltip + '" class="breakall">' + data.domain + "</code>"
);

$("td:eq(1)", row).html(
'<select id="type" class="form-control">' +
var whitelist_options = "";
if (showtype === "all" || showtype === "white") {
whitelist_options =
'<option value="0"' +
(data.type === 0 ? " selected" : "") +
">Exact whitelist</option>" +
'<option value="1"' +
(data.type === 1 ? " selected" : "") +
">Exact blacklist</option>" +
'<option value="2"' +
(data.type === 2 ? " selected" : "") +
">Regex whitelist</option>" +
">Regex whitelist</option>";
}

var blacklist_options = "";
if (showtype === "all" || showtype === "black") {
blacklist_options =
'<option value="1"' +
(data.type === 1 ? " selected " : " ") +
">Exact blacklist</option>" +
'<option value="3"' +
(data.type === 3 ? " selected" : "") +
">Regex blacklist</option>" +
">Regex blacklist</option>";
}

$("td:eq(1)", row).html(
'<select id="type" class="form-control">' +
whitelist_options +
blacklist_options +
"</select>"
);
$("#type", row).on("change", editDomain);
Expand All @@ -113,21 +130,30 @@ function initTable() {
$("#comment", row).val(data.comment);
$("#comment", row).on("change", editDomain);

$("td:eq(4)", row).empty();
$("td:eq(4)", row).append('<select id="multiselect" multiple="multiple"></select>');
var sel = $("#multiselect", row);
// Add all known groups
for (var i = 0; i < groups.length; i++) {
var extra = "";
if (!groups[i].enabled) {
extra = " (disabled)";
// Show group assignment field only if in full domain management mode
if (table.column(5).visible()) {
$("td:eq(4)", row).empty();
$("td:eq(4)", row).append('<select id="multiselect" multiple="multiple"></select>');
var sel = $("#multiselect", row);
// Add all known groups
for (var i = 0; i < groups.length; i++) {
var extra = "";
if (!groups[i].enabled) {
extra = " (disabled)";
}

sel.append(
$("<option />")
.val(groups[i].id)
.text(groups[i].name + extra)
);
}

sel.append(
$("<option />")
.val(groups[i].id)
.text(groups[i].name + extra)
);
// Select assigned groups
sel.val(data.groups);
// Initialize multiselect
sel.multiselect({ includeSelectAllOption: true });
sel.on("change", editDomain);
}

// Highlight row
Expand All @@ -137,19 +163,17 @@ function initTable() {
.addClass("highlight");
}

// Select assigned groups
sel.val(data.groups);
// Initialize multiselect
sel.multiselect({ includeSelectAllOption: true });
sel.on("change", editDomain);

var button =
'<button class="btn btn-danger btn-xs deleteDomain" type="button" data-id="' +
data.id +
'">' +
'<span class="glyphicon glyphicon-trash"></span>' +
"</button>";
$("td:eq(5)", row).html(button);
if (table.column(5).visible()) {
$("td:eq(5)", row).html(button);
} else {
$("td:eq(4)", row).html(button);
}
},
dom:
"<'row'<'col-sm-4'l><'col-sm-8'f>>" +
Expand Down Expand Up @@ -179,6 +203,8 @@ function initTable() {
data.search.search = "";
// Reset visibility of ID column
data.columns[0].visible = false;
// Show group assignment column only on full page
data.columns[5].visible = showtype === "all";
// Apply loaded state to table
return data;
},
Expand Down