/* global FooTable */ define(["jquery", "app/common", "footable"], ($, common) => { "use strict"; const ui = {}; const columnsCustom = JSON.parse(localStorage.getItem("columns")) || {}; let pageSizeTimerId = null; let pageSizeInvocationCounter = 0; function get_compare_function(table) { const compare_functions = { magnitude: function (e1, e2) { return Math.abs(e2.score) - Math.abs(e1.score); }, name: function (e1, e2) { return e1.name.localeCompare(e2.name); }, score: function (e1, e2) { return e2.score - e1.score; } }; return compare_functions[common.getSelector("selSymOrder_" + table)]; } function sort_symbols(o, compare_function) { return Object.keys(o) .map((key) => o[key]) .sort(compare_function) .map((e) => e.str) .join("
\n"); } // Public functions ui.formatBytesIEC = function (bytes) { // FooTable represents data as text even column type is "number". if (!Number.isInteger(Number(bytes)) || bytes < 0) return "NaN"; const base = 1024; const exponent = Math.floor(Math.log(bytes) / Math.log(base)); if (exponent > 8) return "∞"; const value = parseFloat((bytes / (base ** exponent)).toPrecision(3)); let unit = "BKMGTPEZY"[exponent]; if (exponent) unit += "iB"; return value + " " + unit; }; ui.columns_v2 = function (table) { return [{ name: "id", title: "ID", style: { minWidth: 130, overflow: "hidden", textOverflow: "ellipsis", wordBreak: "break-all", whiteSpace: "normal" } }, { name: "file", title: "File name", breakpoints: "sm", sortValue: (val) => ((typeof val === "undefined") ? "" : val) }, { name: "ip", title: "IP address", breakpoints: "lg", style: { "minWidth": "calc(14ch + 8px)", "word-break": "break-all" }, // Normalize IPv4 sortValue: (ip) => ((typeof ip === "string") ? ip.split(".").map((x) => x.padStart(3, "0")).join("") : "0") }, { name: "sender_mime", title: "[Envelope From] From", breakpoints: "lg", style: { "minWidth": 100, "maxWidth": 200, "word-wrap": "break-word" } }, { name: "rcpt_mime_short", title: "[Envelope To] To/Cc/Bcc", breakpoints: "lg", filterable: false, classes: "d-none d-xl-table-cell", style: { "minWidth": 100, "maxWidth": 200, "word-wrap": "break-word" } }, { name: "rcpt_mime", title: "[Envelope To] To/Cc/Bcc", breakpoints: "all", style: {"word-wrap": "break-word"} }, { name: "subject", title: "Subject", breakpoints: "lg", style: { "word-break": "break-all", "minWidth": 150 } }, { name: "action", title: "Action", style: {minwidth: 82} }, { name: "passthrough_module", title: '
Pass-through module
', breakpoints: "sm", style: {minWidth: 98, maxWidth: 98}, sortValue: (val) => ((typeof val === "undefined") ? "" : val) }, { name: "score", title: "Score", style: { "maxWidth": 110, "text-align": "right", "white-space": "nowrap" }, sortValue: function (val) { return Number(val.options.sortValue); } }, { name: "symbols", title: "Symbols" + '
' + '
Sort by:
' + '
' + '' + '' + '' + "
" + "
", breakpoints: "all", style: {width: 550, maxWidth: 550} }, { name: "size", title: "Msg size", breakpoints: "lg", style: {minwidth: 50}, formatter: ui.formatBytesIEC }, { name: "time_real", title: "Scan time", breakpoints: "lg", style: {maxWidth: 72}, sortValue: function (val) { return Number(val); } }, { classes: "history-col-time", sorted: true, direction: "DESC", name: "time", title: "Time", sortValue: function (val) { return Number(val.options.sortValue); } }, { name: "user", title: "Authenticated user", breakpoints: "lg", style: { "minWidth": 100, "maxWidth": 130, "word-wrap": "break-word" } }].filter((col) => { switch (table) { case "history": return (col.name !== "file"); case "scan": return ["ip", "sender_mime", "rcpt_mime_short", "rcpt_mime", "subject", "size", "user"] .every((name) => col.name !== name); default: return null; } }); }; ui.set_page_size = function (table, page_size, changeTablePageSize) { const n = parseInt(page_size, 10); // HTML Input elements return string representing a number if (n > 0) { common.page_size[table] = n; if (changeTablePageSize && $("#historyTable_" + table + " tbody").is(":parent")) { // Table is not empty clearTimeout(pageSizeTimerId); const t = FooTable.get("#historyTable_" + table); if (t) { pageSizeInvocationCounter = 0; // Wait for input finish pageSizeTimerId = setTimeout(() => t.pageSize(n), 1000); } else if (++pageSizeInvocationCounter < 10) { // Wait for FooTable instance ready pageSizeTimerId = setTimeout(() => ui.set_page_size(table, n, true), 1000); } } } }; ui.bindHistoryTableEventHandlers = function (table, symbolsCol) { function change_symbols_order(order) { $(".btn-sym-" + table + "-" + order).addClass("active").siblings().removeClass("active"); const compare_function = get_compare_function(table); $.each(common.tables[table].rows.all, (i, row) => { const cell_val = sort_symbols(common.symbols[table][i], compare_function); row.cells[symbolsCol].val(cell_val, false, true); }); } $("#selSymOrder_" + table).unbind().change(function () { const order = this.value; change_symbols_order(order); }); $("#" + table + "_page_size").change((e) => ui.set_page_size(table, e.target.value, true)); $(document).on("click", ".btn-sym-order-" + table + " input", function () { const order = this.value; $("#selSymOrder_" + table).val(order); change_symbols_order(order); }); }; ui.destroyTable = function (table) { $("#" + table + " .ft-columns-btn.show").trigger("click.bs.dropdown"); // Hide dropdown $("#" + table + " .ft-columns-btn").attr("disabled", true); if (common.tables[table]) { common.tables[table].destroy(); delete common.tables[table]; } }; ui.initHistoryTable = function (data, items, table, columnsDefault, expandFirst, postdrawCallback) { /* eslint-disable no-underscore-dangle */ FooTable.Cell.extend("collapse", function () { // call the original method this._super(); // Copy cell classes to detail row tr element this._setClasses(this.$detail); }); /* eslint-enable no-underscore-dangle */ /* eslint-disable consistent-this, no-underscore-dangle, one-var-declaration-per-line */ FooTable.actionFilter = FooTable.Filtering.extend({ construct: function (instance) { this._super(instance); this.actions = ["reject", "add header", "greylist", "no action", "soft reject", "rewrite subject"]; this.def = "Any action"; this.$action = null; }, $create: function () { this._super(); const self = this; const $form_grp = $("
", { class: "form-group d-inline-flex align-items-center" }).append($("