Skip to content

Commit

Permalink
[WebUI] Load modules dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
moisseev committed Nov 16, 2023
1 parent 4bc8131 commit 57dac6a
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 158 deletions.
32 changes: 18 additions & 14 deletions interface/js/app/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
THE SOFTWARE.
*/

define(["jquery", "codejar", "linenumbers", "prism"],
function ($, CodeJar, withLineNumbers, Prism) {
/* global require */

define(["jquery", "app/rspamd"],
function ($, rspamd) {
"use strict";
var ui = {};

ui.getActions = function getActions(rspamd, checked_server) {
ui.getActions = function getActions(checked_server) {
rspamd.query("actions", {
success: function (data) {
$("#actionsFormField").empty();
Expand Down Expand Up @@ -63,7 +65,7 @@ define(["jquery", "codejar", "linenumbers", "prism"],
});
};

ui.saveActions = function (rspamd, server) {
ui.saveActions = function (server) {
function descending(arr) {
var desc = true;
var filtered = arr.filter(function (el) {
Expand Down Expand Up @@ -113,7 +115,7 @@ define(["jquery", "codejar", "linenumbers", "prism"],
}
};

ui.getMaps = function (rspamd, checked_server) {
ui.getMaps = function (checked_server) {
var $listmaps = $("#listMaps");
$listmaps.closest(".card").hide();
rspamd.query("maps", {
Expand Down Expand Up @@ -142,7 +144,7 @@ define(["jquery", "codejar", "linenumbers", "prism"],
});
};

ui.setup = function (rspamd) {
(() => {
var jar = {};
const editor = {
advanced: {
Expand Down Expand Up @@ -175,11 +177,13 @@ define(["jquery", "codejar", "linenumbers", "prism"],
"</" + editor[mode].elt + ">").appendTo("#modalBody");

if (editor[mode].codejar) {
jar = new CodeJar(
document.querySelector("#editor"),
withLineNumbers((el) => Prism.highlightElement(el))
);
jar.updateCode(data[0].data);
require(["codejar", "linenumbers", "prism"], function (CodeJar, withLineNumbers, Prism) {
jar = new CodeJar(
document.querySelector("#editor"),
withLineNumbers((el) => Prism.highlightElement(el))
);
jar.updateCode(data[0].data);
});
} else {
document.querySelector("#editor").innerHTML = rspamd.escapeHTML(data[0].data);
}
Expand Down Expand Up @@ -212,10 +216,10 @@ define(["jquery", "codejar", "linenumbers", "prism"],
});

$("#saveActionsBtn").on("click", function () {
ui.saveActions(rspamd);
ui.saveActions();
});
$("#saveActionsClusterBtn").on("click", function () {
ui.saveActions(rspamd, "All SERVERS");
ui.saveActions("All SERVERS");
});

function saveMap(server) {
Expand All @@ -242,7 +246,7 @@ define(["jquery", "codejar", "linenumbers", "prism"],
$("#modalSaveAll").on("click", function () {
saveMap("All SERVERS");
});
};
})();

return ui;
});
20 changes: 9 additions & 11 deletions interface/js/app/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
THE SOFTWARE.
*/

/* global d3, FooTable */
/* global FooTable */

define(["jquery", "d3evolution", "d3pie", "footable"],
function ($, D3Evolution, D3Pie) {
define(["jquery", "app/rspamd", "d3evolution", "d3pie", "d3", "footable"],
function ($, rspamd, D3Evolution, D3Pie, d3) {
"use strict";

var rrd_pie_config = {
Expand Down Expand Up @@ -59,7 +59,7 @@ define(["jquery", "d3evolution", "d3pie", "footable"],
var ui = {};
var prevUnit = "msg/s";

ui.draw = function (rspamd, graphs, tables, neighbours, checked_server, type) {
ui.draw = function (graphs, neighbours, checked_server, type) {
var graph_options = {
title: "Rspamd throughput",
width: 1060,
Expand Down Expand Up @@ -126,7 +126,7 @@ define(["jquery", "d3evolution", "d3pie", "footable"],
}

function initSummaryTable(rows, unit) {
tables.rrd_summary = FooTable.init("#rrd-table", {
rspamd.tables.rrd_summary = FooTable.init("#rrd-table", {
sorting: {
enabled: true
},
Expand All @@ -152,8 +152,8 @@ define(["jquery", "d3evolution", "d3pie", "footable"],
}

function drawRrdTable(rows, unit) {
if (Object.prototype.hasOwnProperty.call(tables, "rrd_summary")) {
$.each(tables.rrd_summary.rows.all, function (i, row) {
if (Object.prototype.hasOwnProperty.call(rspamd.tables, "rrd_summary")) {
$.each(rspamd.tables.rrd_summary.rows.all, function (i, row) {
row.val(rows[i], false, true);
});
} else {
Expand Down Expand Up @@ -243,7 +243,7 @@ define(["jquery", "d3evolution", "d3pie", "footable"],
});
};

ui.setup = function (rspamd) {
(() => {
// Handling mouse events on overlapping elements
$("#rrd-pie").mouseover(function () {
$("#rrd-pie,#rrd-pie-tooltip").css("z-index", "200");
Expand All @@ -253,9 +253,7 @@ define(["jquery", "d3evolution", "d3pie", "footable"],
$("#rrd-pie,#rrd-pie-tooltip").css("z-index", "0");
$("#rrd-table_toggle").css("z-index", "0");
});

return rspamd.getSelector("selData");
};
})();

return ui;
});
48 changes: 24 additions & 24 deletions interface/js/app/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
THE SOFTWARE.
*/

/* global d3, FooTable */
/* global FooTable */

define(["jquery", "footable"],
function ($) {
define(["jquery", "app/rspamd", "d3", "footable"],
function ($, rspamd, d3) {
"use strict";
var ui = {};
var prevVersion = null;

function process_history_legacy(rspamd, data) {
function process_history_legacy(data) {
var items = [];

var compare = function (e1, e2) {
Expand All @@ -41,7 +41,7 @@ define(["jquery", "footable"],

$.each(data, function (i, item) {
item.time = rspamd.unix_time_format(item.unix_time);
rspamd.preprocess_item(rspamd, item);
rspamd.preprocess_item(item);
item.symbols = Object.keys(item.symbols)
.map(function (key) {
return item.symbols[key];
Expand Down Expand Up @@ -292,7 +292,7 @@ define(["jquery", "footable"],
legacy: columns_legacy
};

function process_history_data(rspamd, data) {
function process_history_data(data) {
var process_functions = {
2: rspamd.process_history_v2,
legacy: process_history_legacy
Expand All @@ -306,7 +306,7 @@ define(["jquery", "footable"],
}
}

return pf(rspamd, data, "history");
return pf(data, "history");
}

function get_history_columns(data) {
Expand All @@ -322,7 +322,7 @@ define(["jquery", "footable"],
return func();
}

ui.getHistory = function (rspamd, tables) {
ui.getHistory = function () {
rspamd.query("history", {
success: function (req_data) {
function differentVersions(neighbours_data) {
Expand Down Expand Up @@ -355,18 +355,18 @@ define(["jquery", "footable"],
data = [].concat.apply([], neighbours_data);
$("#legacy-history-badge").show();
}
var o = process_history_data(rspamd, data);
var o = process_history_data(data);
var items = o.items;
rspamd.symbols.history = o.symbols;

if (Object.prototype.hasOwnProperty.call(tables, "history") &&
if (Object.prototype.hasOwnProperty.call(rspamd.tables, "history") &&
version === prevVersion) {
tables.history.rows.load(items);
rspamd.tables.history.rows.load(items);
} else {
rspamd.destroyTable("history");
// Is there a way to get an event when the table is destroyed?
setTimeout(function () {
rspamd.initHistoryTable(rspamd, data, items, "history", get_history_columns(data), false);
rspamd.initHistoryTable(data, items, "history", get_history_columns(data), false);
}, 200);
}
prevVersion = version;
Expand All @@ -379,14 +379,14 @@ define(["jquery", "footable"],
});
};

ui.setup = function (rspamd, tables) {
(() => {
rspamd.set_page_size("history", $("#history_page_size").val());
rspamd.bindHistoryTableEventHandlers("history", 8);

$("#updateHistory").off("click");
$("#updateHistory").on("click", function (e) {
e.preventDefault();
ui.getHistory(rspamd, tables);
ui.getHistory();
});

// @reset history log
Expand All @@ -401,16 +401,16 @@ define(["jquery", "footable"],

rspamd.query("historyreset", {
success: function () {
ui.getHistory(rspamd, tables);
ui.getErrors(rspamd, tables);
ui.getHistory();
ui.getErrors();
},
errorMessage: "Cannot reset history log"
});
});
};
})();

function initErrorsTable(rspamd, tables, rows) {
tables.errors = FooTable.init("#errorsLog", {
function initErrorsTable(rows) {
rspamd.tables.errors = FooTable.init("#errorsLog", {
columns: [
{
sorted: true,
Expand Down Expand Up @@ -443,7 +443,7 @@ define(["jquery", "footable"],
});
}

ui.getErrors = function (rspamd, tables) {
ui.getErrors = function () {
if (rspamd.read_only) return;

rspamd.query("errors", {
Expand All @@ -464,18 +464,18 @@ define(["jquery", "footable"],
}
};
});
if (Object.prototype.hasOwnProperty.call(tables, "errors")) {
tables.errors.rows.load(rows);
if (Object.prototype.hasOwnProperty.call(rspamd.tables, "errors")) {
rspamd.tables.errors.rows.load(rows);
} else {
initErrorsTable(rspamd, tables, rows);
initErrorsTable(rows);
}
}
});

$("#updateErrors").off("click");
$("#updateErrors").on("click", function (e) {
e.preventDefault();
ui.getErrors(rspamd, tables);
ui.getErrors();
});
};

Expand Down

0 comments on commit 57dac6a

Please sign in to comment.