From 5969519a01b685607532619f9130ef89ac28106d Mon Sep 17 00:00:00 2001 From: moisseev Date: Wed, 22 Nov 2023 10:28:30 +0300 Subject: [PATCH] [Minor] Convert callbacks to arrow functions --- .eslintrc.json | 1 - interface/js/app/config.js | 30 +++++------- interface/js/app/graph.js | 62 +++++++++++------------- interface/js/app/history.js | 40 ++++++---------- interface/js/app/rspamd.js | 89 +++++++++++++++++------------------ interface/js/app/selectors.js | 18 +++---- interface/js/app/stats.js | 28 +++++------ interface/js/app/symbols.js | 20 ++++---- interface/js/app/upload.js | 20 ++++---- interface/js/main.js | 2 +- 10 files changed, 139 insertions(+), 171 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 5ae3238ea..f76a38e21 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -33,7 +33,6 @@ "no-ternary": "off", "object-shorthand": "off", "one-var": ["error", { "initialized": "never" }], - "prefer-arrow-callback": "off", "prefer-named-capture-group": "off", "prefer-object-has-own": "off", "prefer-spread": "off", diff --git a/interface/js/app/config.js b/interface/js/app/config.js index 9f62c97db..1aaf71289 100644 --- a/interface/js/app/config.js +++ b/interface/js/app/config.js @@ -25,7 +25,7 @@ /* global require */ define(["jquery", "app/rspamd"], - function ($, rspamd) { + ($, rspamd) => { "use strict"; const ui = {}; @@ -34,7 +34,7 @@ define(["jquery", "app/rspamd"], success: function (data) { $("#actionsFormField").empty(); const items = []; - $.each(data[0].data, function (i, item) { + $.each(data[0].data, (i, item) => { const actionsOrder = ["greylist", "add header", "rewrite subject", "reject"]; const idx = actionsOrder.indexOf(item.action); if (idx >= 0) { @@ -52,14 +52,10 @@ define(["jquery", "app/rspamd"], } }); - items.sort(function (a, b) { - return a.idx - b.idx; - }); + items.sort((a, b) => a.idx - b.idx); $("#actionsFormField").html( - items.map(function (e) { - return e.html; - }).join("")); + items.map((e) => e.html).join("")); }, server: (checked_server === "All SERVERS") ? "local" : checked_server }); @@ -68,9 +64,7 @@ define(["jquery", "app/rspamd"], ui.saveActions = function (server) { function descending(arr) { let desc = true; - const filtered = arr.filter(function (el) { - return el !== null; - }); + const filtered = arr.filter((el) => el !== null); for (let i = 0; i < filtered.length - 1; i++) { if (filtered[i + 1] >= filtered[i]) { desc = false; @@ -125,7 +119,7 @@ define(["jquery", "app/rspamd"], $("#modalBody").empty(); const $tbody = $(""); - $.each(data, function (i, item) { + $.each(data, (i, item) => { let $td = 'Read'; if (!(item.editable === false || rspamd.read_only)) { $td = $($td).append(' Write'); @@ -178,7 +172,7 @@ define(["jquery", "app/rspamd"], '">").appendTo("#modalBody"); if (editor[mode].codejar) { - require(["codejar", "linenumbers", "prism"], function (CodeJar, withLineNumbers, Prism) { + require(["codejar", "linenumbers", "prism"], (CodeJar, withLineNumbers, Prism) => { jar = new CodeJar( document.querySelector("#editor"), withLineNumbers((el) => Prism.highlightElement(el)) @@ -207,7 +201,7 @@ define(["jquery", "app/rspamd"], }); return false; }); - $("#modalDialog").on("hidden.bs.modal", function () { + $("#modalDialog").on("hidden.bs.modal", () => { if (editor[mode].codejar) { jar.destroy(); $(".codejar-wrap").remove(); @@ -216,10 +210,10 @@ define(["jquery", "app/rspamd"], } }); - $("#saveActionsBtn").on("click", function () { + $("#saveActionsBtn").on("click", () => { ui.saveActions(); }); - $("#saveActionsClusterBtn").on("click", function () { + $("#saveActionsClusterBtn").on("click", () => { ui.saveActions("All SERVERS"); }); @@ -241,10 +235,10 @@ define(["jquery", "app/rspamd"], server: server }); } - $("#modalSave").on("click", function () { + $("#modalSave").on("click", () => { saveMap(); }); - $("#modalSaveAll").on("click", function () { + $("#modalSaveAll").on("click", () => { saveMap("All SERVERS"); }); diff --git a/interface/js/app/graph.js b/interface/js/app/graph.js index a661d4a19..2fc00a457 100644 --- a/interface/js/app/graph.js +++ b/interface/js/app/graph.js @@ -26,7 +26,7 @@ /* global FooTable */ define(["jquery", "app/rspamd", "d3evolution", "d3pie", "d3", "footable"], - function ($, rspamd, D3Evolution, D3Pie, d3) { + ($, rspamd, D3Evolution, D3Pie, d3) => { "use strict"; const rrd_pie_config = { @@ -96,17 +96,17 @@ define(["jquery", "app/rspamd", "d3evolution", "d3pie", "d3", "footable"], } function getRrdSummary(json, scaleFactor) { - const xExtents = d3.extent(d3.merge(json), function (d) { return d.x; }); + const xExtents = d3.extent(d3.merge(json), (d) => d.x); const timeInterval = xExtents[1] - xExtents[0]; let total = 0; - const rows = json.map(function (curr, i) { + const rows = json.map((curr, i) => { // Time intervals that don't have data are excluded from average calculation as d3.mean()ignores nulls - const avg = d3.mean(curr, function (d) { return d.y; }); + const avg = d3.mean(curr, (d) => d.y); // To find an integral on the whole time interval we need to convert nulls to zeroes // eslint-disable-next-line no-bitwise - const value = d3.mean(curr, function (d) { return Number(d.y); }) * timeInterval / scaleFactor ^ 0; - const yExtents = d3.extent(curr, function (d) { return d.y; }); + const value = d3.mean(curr, (d) => Number(d.y)) * timeInterval / scaleFactor ^ 0; + const yExtents = d3.extent(curr, (d) => d.y); total += value; return { @@ -139,22 +139,20 @@ define(["jquery", "app/rspamd", "d3evolution", "d3pie", "d3", "footable"], {name: "max", title: "Maximum, " + unit + "", defaultContent: ""}, {name: "last", title: "Last, " + unit}, ], - rows: rows.map(function (curr, i) { - return { - options: { - style: { - color: graph_options.legend.entries[i].color - } - }, - value: curr - }; - }, []) + rows: rows.map((curr, i) => ({ + options: { + style: { + color: graph_options.legend.entries[i].color + } + }, + value: curr + }), []) }); } function drawRrdTable(rows, unit) { if (Object.prototype.hasOwnProperty.call(rspamd.tables, "rrd_summary")) { - $.each(rspamd.tables.rrd_summary.rows.all, function (i, row) { + $.each(rspamd.tables.rrd_summary.rows.all, (i, row) => { row.val(rows[i], false, true); }); } else { @@ -169,12 +167,12 @@ define(["jquery", "app/rspamd", "d3evolution", "d3pie", "d3", "footable"], if (data) { // Autoranging let scaleFactor = 1; - const yMax = d3.max(d3.merge(data), function (d) { return d.y; }); + const yMax = d3.max(d3.merge(data), (d) => d.y); if (yMax < 1) { scaleFactor = 60; unit = "msg/min"; - data.forEach(function (s) { - s.forEach(function (d) { + data.forEach((s) => { + s.forEach((d) => { if (d.y !== null) { d.y *= scaleFactor; } }); }); @@ -205,14 +203,14 @@ define(["jquery", "app/rspamd", "d3evolution", "d3pie", "d3", "footable"], success: function (req_data) { let data = null; const neighbours_data = req_data - .filter(function (d) { return d.status; }) // filter out unavailable neighbours - .map(function (d) { return d.data; }); + .filter((d) => d.status) // filter out unavailable neighbours + .map((d) => d.data); if (neighbours_data.length === 1) { [data] = neighbours_data; } else { let time_match = true; - neighbours_data.reduce(function (res, curr, _, arr) { + neighbours_data.reduce((res, curr, _, arr) => { if ((curr[0][0].x !== res[0][0].x) || (curr[0][curr[0].length - 1].x !== res[0][res[0].length - 1].x)) { time_match = false; @@ -224,16 +222,10 @@ define(["jquery", "app/rspamd", "d3evolution", "d3pie", "d3", "footable"], }); if (time_match) { - data = neighbours_data.reduce(function (res, curr) { - return curr.map(function (action, j) { - return action.map(function (d, i) { - return { - x: d.x, - y: (res[j][i].y === null) ? d.y : res[j][i].y + d.y - }; - }); - }); - }); + data = neighbours_data.reduce((res, curr) => curr.map((action, j) => action.map((d, i) => ({ + x: d.x, + y: (res[j][i].y === null) ? d.y : res[j][i].y + d.y + })))); } } updateWidgets(data); @@ -247,11 +239,11 @@ define(["jquery", "app/rspamd", "d3evolution", "d3pie", "d3", "footable"], // Handling mouse events on overlapping elements - $("#rrd-pie").mouseover(function () { + $("#rrd-pie").mouseover(() => { $("#rrd-pie,#rrd-pie-tooltip").css("z-index", "200"); $("#rrd-table_toggle").css("z-index", "300"); }); - $("#rrd-table_toggle").mouseover(function () { + $("#rrd-table_toggle").mouseover(() => { $("#rrd-pie,#rrd-pie-tooltip").css("z-index", "0"); $("#rrd-table_toggle").css("z-index", "0"); }); diff --git a/interface/js/app/history.js b/interface/js/app/history.js index 568c69763..26b2e96a6 100644 --- a/interface/js/app/history.js +++ b/interface/js/app/history.js @@ -25,7 +25,7 @@ /* global FooTable */ define(["jquery", "app/rspamd", "d3", "footable"], - function ($, rspamd, d3) { + ($, rspamd, d3) => { "use strict"; const ui = {}; let prevVersion = null; @@ -37,15 +37,13 @@ define(["jquery", "app/rspamd", "d3", "footable"], $("#selSymOrder_history, label[for='selSymOrder_history']").hide(); - $.each(data, function (i, item) { + $.each(data, (i, item) => { item.time = rspamd.unix_time_format(item.unix_time); rspamd.preprocess_item(item); item.symbols = Object.keys(item.symbols) - .map(function (key) { - return item.symbols[key]; - }) + .map((key) => item.symbols[key]) .sort(compare) - .map(function (e) { return e.name; }) + .map((e) => e.name) .join(", "); item.time = { value: rspamd.unix_time_format(item.unix_time), @@ -324,9 +322,7 @@ define(["jquery", "app/rspamd", "d3", "footable"], rspamd.query("history", { success: function (req_data) { function differentVersions(neighbours_data) { - const dv = neighbours_data.some(function (e) { - return e.version !== neighbours_data[0].version; - }); + const dv = neighbours_data.some((e) => e.version !== neighbours_data[0].version); if (dv) { rspamd.alertMessage("alert-error", "Neighbours history backend versions do not match. Cannot display history."); @@ -336,16 +332,14 @@ define(["jquery", "app/rspamd", "d3", "footable"], } const neighbours_data = req_data - .filter(function (d) { return d.status; }) // filter out unavailable neighbours - .map(function (d) { return d.data; }); + .filter((d) => d.status) // filter out unavailable neighbours + .map((d) => d.data); if (neighbours_data.length && !differentVersions(neighbours_data)) { let data = {}; const [{version}] = neighbours_data; if (version) { data.rows = [].concat.apply([], neighbours_data - .map(function (e) { - return e.rows; - })); + .map((e) => e.rows)); data.version = version; $("#legacy-history-badge").hide(); } else { @@ -363,7 +357,7 @@ define(["jquery", "app/rspamd", "d3", "footable"], } else { rspamd.destroyTable("history"); // Is there a way to get an event when the table is destroyed? - setTimeout(function () { + setTimeout(() => { rspamd.initHistoryTable(data, items, "history", get_history_columns(data), false); }, 200); } @@ -421,14 +415,10 @@ define(["jquery", "app/rspamd", "d3", "footable"], rspamd.query("errors", { success: function (data) { const neighbours_data = data - .filter(function (d) { - return d.status; - }) // filter out unavailable neighbours - .map(function (d) { - return d.data; - }); + .filter((d) => d.status) // filter out unavailable neighbours + .map((d) => d.data); const rows = [].concat.apply([], neighbours_data); - $.each(rows, function (i, item) { + $.each(rows, (i, item) => { item.ts = { value: rspamd.unix_time_format(item.ts), options: { @@ -445,7 +435,7 @@ define(["jquery", "app/rspamd", "d3", "footable"], }); $("#updateErrors").off("click"); - $("#updateErrors").on("click", function (e) { + $("#updateErrors").on("click", (e) => { e.preventDefault(); ui.getErrors(); }); @@ -456,14 +446,14 @@ define(["jquery", "app/rspamd", "d3", "footable"], rspamd.bindHistoryTableEventHandlers("history", 8); $("#updateHistory").off("click"); - $("#updateHistory").on("click", function (e) { + $("#updateHistory").on("click", (e) => { e.preventDefault(); ui.getHistory(); }); // @reset history log $("#resetHistory").off("click"); - $("#resetHistory").on("click", function (e) { + $("#resetHistory").on("click", (e) => { e.preventDefault(); if (!confirm("Are you sure you want to reset history log?")) { // eslint-disable-line no-alert return; diff --git a/interface/js/app/rspamd.js b/interface/js/app/rspamd.js index 64eab27a0..1fac8b939 100644 --- a/interface/js/app/rspamd.js +++ b/interface/js/app/rspamd.js @@ -27,7 +27,7 @@ define(["jquery", "nprogress", "stickytabs", "visibility", "bootstrap", "fontawesome"], -function ($, NProgress) { +($, NProgress) => { "use strict"; const ui = { chartLegend: [ @@ -92,8 +92,8 @@ function ($, NProgress) { } function disconnect() { - [graphs, tables].forEach(function (o) { - Object.keys(o).forEach(function (key) { + [graphs, tables].forEach((o) => { + Object.keys(o).forEach((key) => { o[key].destroy(); delete o[key]; }); @@ -139,7 +139,7 @@ function ($, NProgress) { let timeLeft = interval; $("#countdown").text("00:00"); - timer_id.countdown = Visibility.every(1000, 1000, function () { + timer_id.countdown = Visibility.every(1000, 1000, () => { timeLeft -= 1000; $("#countdown").text(new Date(timeLeft).toISOString().substr(14, 5)); if (timeLeft <= 0) Visibility.stop(timer_id.countdown); @@ -151,7 +151,7 @@ function ($, NProgress) { countdown(refreshInterval); if (!refreshInterval) return; - timer_id[timer] = Visibility.every(refreshInterval, function () { + timer_id[timer] = Visibility.every(refreshInterval, () => { countdown(refreshInterval); if ($("#refresh").attr("disabled")) return; $("#refresh").attr("disabled", true).addClass("disabled", true); @@ -170,7 +170,7 @@ function ($, NProgress) { require(["app/stats"], (module) => { const refreshInterval = $(".dropdown-menu a.active.preset").data("value"); setAutoRefresh(refreshInterval, "status", - function () { return module.statWidgets(graphs, checked_server); }); + () => module.statWidgets(graphs, checked_server)); if (id !== "#autoRefresh") module.statWidgets(graphs, checked_server); $(".preset").show(); @@ -192,7 +192,7 @@ function ($, NProgress) { refreshInterval = null; } setAutoRefresh(refreshInterval, "throughput", - function () { return module.draw(graphs, neighbours, checked_server, selData); }); + () => module.draw(graphs, neighbours, checked_server, selData)); if (id !== "#autoRefresh") module.draw(graphs, neighbours, checked_server, selData); $(".preset").hide(); @@ -223,7 +223,7 @@ function ($, NProgress) { } const refreshInterval = $(".dropdown-menu a.active.history").data("value"); setAutoRefresh(refreshInterval, "history", - function () { return getHistoryAndErrors(); }); + () => getHistoryAndErrors()); if (id !== "#autoRefresh") getHistoryAndErrors(); $(".preset").hide(); @@ -237,7 +237,7 @@ function ($, NProgress) { default: } - setTimeout(function () { + setTimeout(() => { // Do not enable Refresh button until AJAX requests to all neighbours are finished if (tab_id === "#history_nav") navBarControls = $(navBarControls).not("#refresh"); @@ -292,11 +292,9 @@ function ($, NProgress) { function sort_symbols(o, compare_function) { return Object.keys(o) - .map(function (key) { - return o[key]; - }) + .map((key) => o[key]) .sort(compare_function) - .map(function (e) { return e.str; }) + .map((e) => e.str) .join("
\n"); } @@ -316,7 +314,7 @@ function ($, NProgress) { success: function (neighbours_status) { $("#selSrv").empty(); $("#selSrv").append($('')); - neighbours_status.forEach(function (e) { + neighbours_status.forEach((e) => { $("#selSrv").append($('")); if (checked_server === e.name) { $('#selSrv [value="' + e.name + '"]').prop("selected", true); @@ -351,7 +349,7 @@ function ($, NProgress) { "" + alertText + ""); $(".notification-area").append(a); - setTimeout(function () { + setTimeout(() => { $(a).fadeTo(500, 0).slideUp(500, function () { $(this).alert("close"); }); @@ -371,12 +369,11 @@ function ($, NProgress) { const xhr = $.ajaxSettings.xhr(); // Download progress if (req_url !== "neighbours") { - xhr.addEventListener("progress", function (e) { + xhr.addEventListener("progress", (e) => { if (e.lengthComputable) { neighbours_status[ind].percentComplete = e.loaded / e.total; - const percentComplete = neighbours_status.reduce(function (prev, curr) { - return curr.percentComplete ? curr.percentComplete + prev : prev; - }, 0); + const percentComplete = neighbours_status + .reduce((prev, curr) => (curr.percentComplete ? curr.percentComplete + prev : prev), 0); NProgress.set(percentComplete / neighbours_status.length); } }, false); @@ -409,8 +406,8 @@ function ($, NProgress) { } }, complete: function (jqXHR) { - if (neighbours_status.every(function (elt) { return elt.checked; })) { - if (neighbours_status.some(function (elt) { return elt.status; })) { + if (neighbours_status.every((elt) => elt.checked)) { + if (neighbours_status.some((elt) => elt.status)) { if (o.success) { o.success(neighbours_status, jqXHR); } else { @@ -429,7 +426,7 @@ function ($, NProgress) { req_params.method = o.method; } if (o.params) { - $.each(o.params, function (k, v) { + $.each(o.params, (k, v) => { req_params[k] = v; }); } @@ -470,7 +467,7 @@ function ($, NProgress) { }) .modal("show"); - $("#connectForm").off("submit").on("submit", function (e) { + $("#connectForm").off("submit").on("submit", (e) => { e.preventDefault(); const password = $("#connectPassword").val(); @@ -546,7 +543,7 @@ function ($, NProgress) { ui.query = function (url, options) { // Force options to be an object const o = options || {}; - Object.keys(o).forEach(function (option) { + Object.keys(o).forEach((option) => { if (["complete", "data", "error", "errorMessage", "errorOnceId", "headers", "method", "params", "server", "statusCode", "success"] .indexOf(option) < 0) { @@ -575,14 +572,14 @@ function ($, NProgress) { neighbours = data; } neighbours_status = []; - $.each(neighbours, function (ind) { + $.each(neighbours, (ind) => { neighbours_status.push({ name: ind, host: neighbours[ind].host, url: neighbours[ind].url, }); }); - $.each(neighbours_status, function (ind) { + $.each(neighbours_status, (ind) => { queryServer(neighbours_status, ind, url, o); }); }, @@ -610,7 +607,7 @@ function ($, NProgress) { function change_symbols_order(order) { $(".btn-sym-" + table + "-" + order).addClass("active").siblings().removeClass("active"); const compare_function = get_compare_function(table); - $.each(tables[table].rows.all, function (i, row) { + $.each(tables[table].rows.all, (i, row) => { const cell_val = sort_symbols(ui.symbols[table][i], compare_function); row.cells[symbolsCol].val(cell_val, false, true); }); @@ -690,7 +687,7 @@ function ($, NProgress) { text: self.def })).appendTo($form_grp); - $.each(self.actions, function (i, action) { + $.each(self.actions, (i, action) => { self.$action.append($("