commit b48b703: [Minor] Remove temporary IIFEs

moisseev moiseev at mezonplus.ru
Thu Nov 16 16:00:06 UTC 2023


Author: moisseev
Date: 2023-11-16 17:42:18 +0300
URL: https://github.com/rspamd/rspamd/commit/b48b703a363a31cc2058fc444082d4b2966b14f5 (refs/pull/4705/head)

[Minor] Remove temporary IIFEs

---
 .eslintrc.json                |   2 +-
 interface/js/app/config.js    | 193 ++++++++++++++--------------
 interface/js/app/graph.js     |  22 ++--
 interface/js/app/history.js   |  59 +++++----
 interface/js/app/rspamd.js    | 288 +++++++++++++++++++++---------------------
 interface/js/app/selectors.js |  93 +++++++-------
 interface/js/app/symbols.js   |  23 ++--
 interface/js/app/upload.js    | 113 ++++++++---------
 8 files changed, 394 insertions(+), 399 deletions(-)

diff --git a/.eslintrc.json b/.eslintrc.json
index a2d754d5d..c224b5d00 100644
--- a/.eslintrc.json
+++ b/.eslintrc.json
@@ -28,7 +28,7 @@
         "line-comment-position": "off",
         "logical-assignment-operators": ["error", "never"],
         "max-params": ["warn", 6],
-        "max-statements": ["warn", 50],
+        "max-statements": ["warn", 55],
         "max-statements-per-line": ["error", { "max": 2 }],
         "multiline-comment-style": "off",
         "multiline-ternary": ["error", "always-multiline"],
diff --git a/interface/js/app/config.js b/interface/js/app/config.js
index df912eeea..dd47578ae 100644
--- a/interface/js/app/config.js
+++ b/interface/js/app/config.js
@@ -144,109 +144,108 @@ define(["jquery", "app/rspamd"],
             });
         };
 
-        (() => {
-            var jar = {};
-            const editor = {
-                advanced: {
-                    codejar: true,
-                    elt: "div",
-                    class: "editor language-clike",
-                    readonly_attr: {contenteditable: false},
-                },
-                basic: {
-                    elt: "textarea",
-                    class: "form-control map-textarea",
-                    readonly_attr: {readonly: true},
-                }
-            };
-            let mode = "advanced";
-
-            // Modal form for maps
-            $(document).on("click", "[data-bs-toggle=\"modal\"]", function () {
-                var checked_server = rspamd.getSelector("selSrv");
-                var item = $(this).data("item");
-                rspamd.query("getmap", {
-                    headers: {
-                        Map: item.map
-                    },
-                    success: function (data) {
-                        // Highlighting a large amount of text is unresponsive
-                        mode = (new Blob([data[0].data]).size > 5120) ? "basic" : $("input[name=editorMode]:checked").val();
-
-                        $("<" + editor[mode].elt + ' id="editor" class="' + editor[mode].class + '" data-id="' + item.map + '">' +
-                            "</" + editor[mode].elt + ">").appendTo("#modalBody");
-
-                        if (editor[mode].codejar) {
-                            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);
-                        }
 
-                        var icon = "fa-edit";
-                        if (item.editable === false || rspamd.read_only) {
-                            $("#editor").attr(editor[mode].readonly_attr);
-                            icon = "fa-eye";
-                            $("#modalSaveGroup").hide();
-                        } else {
-                            $("#modalSaveGroup").show();
-                        }
-                        $("#modalDialog .modal-header").find("[data-fa-i2svg]").addClass(icon);
-                        $("#modalTitle").html(item.uri);
+        var jar = {};
+        const editor = {
+            advanced: {
+                codejar: true,
+                elt: "div",
+                class: "editor language-clike",
+                readonly_attr: {contenteditable: false},
+            },
+            basic: {
+                elt: "textarea",
+                class: "form-control map-textarea",
+                readonly_attr: {readonly: true},
+            }
+        };
+        let mode = "advanced";
+
+        // Modal form for maps
+        $(document).on("click", "[data-bs-toggle=\"modal\"]", function () {
+            var checked_server = rspamd.getSelector("selSrv");
+            var item = $(this).data("item");
+            rspamd.query("getmap", {
+                headers: {
+                    Map: item.map
+                },
+                success: function (data) {
+                    // Highlighting a large amount of text is unresponsive
+                    mode = (new Blob([data[0].data]).size > 5120) ? "basic" : $("input[name=editorMode]:checked").val();
+
+                    $("<" + editor[mode].elt + ' id="editor" class="' + editor[mode].class + '" data-id="' + item.map + '">' +
+                        "</" + editor[mode].elt + ">").appendTo("#modalBody");
+
+                    if (editor[mode].codejar) {
+                        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);
+                    }
 
-                        $("#modalDialog").modal("show");
-                    },
-                    errorMessage: "Cannot receive maps data",
-                    server: (checked_server === "All SERVERS") ? "local" : checked_server
-                });
-                return false;
-            });
-            $("#modalDialog").on("hidden.bs.modal", function () {
-                if (editor[mode].codejar) {
-                    jar.destroy();
-                    $(".codejar-wrap").remove();
-                } else {
-                    $("#editor").remove();
-                }
-            });
+                    var icon = "fa-edit";
+                    if (item.editable === false || rspamd.read_only) {
+                        $("#editor").attr(editor[mode].readonly_attr);
+                        icon = "fa-eye";
+                        $("#modalSaveGroup").hide();
+                    } else {
+                        $("#modalSaveGroup").show();
+                    }
+                    $("#modalDialog .modal-header").find("[data-fa-i2svg]").addClass(icon);
+                    $("#modalTitle").html(item.uri);
 
-            $("#saveActionsBtn").on("click", function () {
-                ui.saveActions();
-            });
-            $("#saveActionsClusterBtn").on("click", function () {
-                ui.saveActions("All SERVERS");
+                    $("#modalDialog").modal("show");
+                },
+                errorMessage: "Cannot receive maps data",
+                server: (checked_server === "All SERVERS") ? "local" : checked_server
             });
-
-            function saveMap(server) {
-                rspamd.query("savemap", {
-                    success: function () {
-                        rspamd.alertMessage("alert-success", "Map data successfully saved");
-                        $("#modalDialog").modal("hide");
-                    },
-                    errorMessage: "Save map error",
-                    method: "POST",
-                    headers: {
-                        Map: $("#editor").data("id"),
-                    },
-                    params: {
-                        data: editor[mode].codejar ? jar.toString() : $("#editor").val(),
-                        dataType: "text",
-                    },
-                    server: server
-                });
+            return false;
+        });
+        $("#modalDialog").on("hidden.bs.modal", function () {
+            if (editor[mode].codejar) {
+                jar.destroy();
+                $(".codejar-wrap").remove();
+            } else {
+                $("#editor").remove();
             }
-            $("#modalSave").on("click", function () {
-                saveMap();
-            });
-            $("#modalSaveAll").on("click", function () {
-                saveMap("All SERVERS");
+        });
+
+        $("#saveActionsBtn").on("click", function () {
+            ui.saveActions();
+        });
+        $("#saveActionsClusterBtn").on("click", function () {
+            ui.saveActions("All SERVERS");
+        });
+
+        function saveMap(server) {
+            rspamd.query("savemap", {
+                success: function () {
+                    rspamd.alertMessage("alert-success", "Map data successfully saved");
+                    $("#modalDialog").modal("hide");
+                },
+                errorMessage: "Save map error",
+                method: "POST",
+                headers: {
+                    Map: $("#editor").data("id"),
+                },
+                params: {
+                    data: editor[mode].codejar ? jar.toString() : $("#editor").val(),
+                    dataType: "text",
+                },
+                server: server
             });
-        })();
+        }
+        $("#modalSave").on("click", function () {
+            saveMap();
+        });
+        $("#modalSaveAll").on("click", function () {
+            saveMap("All SERVERS");
+        });
 
         return ui;
     });
diff --git a/interface/js/app/graph.js b/interface/js/app/graph.js
index e55214c95..3dca7da9c 100644
--- a/interface/js/app/graph.js
+++ b/interface/js/app/graph.js
@@ -199,6 +199,7 @@ define(["jquery", "app/rspamd", "d3evolution", "d3pie", "d3", "footable"],
                 graphs.graph = initGraph();
             }
 
+
             rspamd.query("graph", {
                 success: function (req_data) {
                     var data = null;
@@ -243,17 +244,16 @@ define(["jquery", "app/rspamd", "d3evolution", "d3pie", "d3", "footable"],
             });
         };
 
-        (() => {
-            // Handling mouse events on overlapping elements
-            $("#rrd-pie").mouseover(function () {
-                $("#rrd-pie,#rrd-pie-tooltip").css("z-index", "200");
-                $("#rrd-table_toggle").css("z-index", "300");
-            });
-            $("#rrd-table_toggle").mouseover(function () {
-                $("#rrd-pie,#rrd-pie-tooltip").css("z-index", "0");
-                $("#rrd-table_toggle").css("z-index", "0");
-            });
-        })();
+
+        // Handling mouse events on overlapping elements
+        $("#rrd-pie").mouseover(function () {
+            $("#rrd-pie,#rrd-pie-tooltip").css("z-index", "200");
+            $("#rrd-table_toggle").css("z-index", "300");
+        });
+        $("#rrd-table_toggle").mouseover(function () {
+            $("#rrd-pie,#rrd-pie-tooltip").css("z-index", "0");
+            $("#rrd-table_toggle").css("z-index", "0");
+        });
 
         return ui;
     });
diff --git a/interface/js/app/history.js b/interface/js/app/history.js
index add8e86bd..4118bc61d 100644
--- a/interface/js/app/history.js
+++ b/interface/js/app/history.js
@@ -379,36 +379,6 @@ define(["jquery", "app/rspamd", "d3", "footable"],
             });
         };
 
-        (() => {
-            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();
-            });
-
-            // @reset history log
-            $("#resetHistory").off("click");
-            $("#resetHistory").on("click", function (e) {
-                e.preventDefault();
-                if (!confirm("Are you sure you want to reset history log?")) { // eslint-disable-line no-alert
-                    return;
-                }
-                rspamd.destroyTable("history");
-                rspamd.destroyTable("errors");
-
-                rspamd.query("historyreset", {
-                    success: function () {
-                        ui.getHistory();
-                        ui.getErrors();
-                    },
-                    errorMessage: "Cannot reset history log"
-                });
-            });
-        })();
-
         function initErrorsTable(rows) {
             rspamd.tables.errors = FooTable.init("#errorsLog", {
                 columns: [
@@ -479,5 +449,34 @@ define(["jquery", "app/rspamd", "d3", "footable"],
             });
         };
 
+
+        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();
+        });
+
+        // @reset history log
+        $("#resetHistory").off("click");
+        $("#resetHistory").on("click", function (e) {
+            e.preventDefault();
+            if (!confirm("Are you sure you want to reset history log?")) { // eslint-disable-line no-alert
+                return;
+            }
+            rspamd.destroyTable("history");
+            rspamd.destroyTable("errors");
+
+            rspamd.query("historyreset", {
+                success: function () {
+                    ui.getHistory();
+                    ui.getErrors();
+                },
+                errorMessage: "Cannot reset history log"
+            });
+        });
+
         return ui;
     });
diff --git a/interface/js/app/rspamd.js b/interface/js/app/rspamd.js
index 2f1f66394..faec47f7f 100644
--- a/interface/js/app/rspamd.js
+++ b/interface/js/app/rspamd.js
@@ -439,150 +439,6 @@ function ($, NProgress) {
 
     // Public functions
     ui.alertMessage = alertMessage;
-    (() => {
-        (function initSettings() {
-            var selected_locale = null;
-            var custom_locale = null;
-            const localeTextbox = ".popover #settings-popover #locale";
-
-            function validateLocale(saveToLocalStorage) {
-                function toggle_form_group_class(remove, add) {
-                    $(localeTextbox).removeClass("is-" + remove).addClass("is-" + add);
-                }
-
-                var now = new Date();
-
-                if (custom_locale.length) {
-                    try {
-                        now.toLocaleString(custom_locale);
-
-                        if (saveToLocalStorage) localStorage.setItem("custom_locale", custom_locale);
-                        locale = (selected_locale === "custom") ? custom_locale : null;
-                        toggle_form_group_class("invalid", "valid");
-                    } catch (err) {
-                        locale = null;
-                        toggle_form_group_class("valid", "invalid");
-                    }
-                } else {
-                    if (saveToLocalStorage) localStorage.setItem("custom_locale", null);
-                    locale = null;
-                    $(localeTextbox).removeClass("is-valid is-invalid");
-                }
-
-                // Display date example
-                $(".popover #settings-popover #date-example").text(
-                    (locale)
-                        ? now.toLocaleString(locale)
-                        : now.toLocaleString()
-                );
-            }
-
-            $("#settings").popover({
-                container: "body",
-                placement: "bottom",
-                html: true,
-                sanitize: false,
-                content: function () {
-                    // Using .clone() has the side-effect of producing elements with duplicate id attributes.
-                    return $("#settings-popover").clone();
-                }
-            // Restore the tooltip of the element that the popover is attached to.
-            }).attr("title", function () {
-                return $(this).attr("data-original-title");
-            });
-            $("#settings").on("click", function (e) {
-                e.preventDefault();
-            });
-            $("#settings").on("inserted.bs.popover", function () {
-                selected_locale = localStorage.getItem("selected_locale") || "browser";
-                custom_locale = localStorage.getItem("custom_locale") || "";
-                validateLocale();
-
-                $('.popover #settings-popover input:radio[name="locale"]').val([selected_locale]);
-                $(localeTextbox).val(custom_locale);
-
-                ajaxSetup(localStorage.getItem("ajax_timeout"), true);
-            });
-            $(document).on("change", '.popover #settings-popover input:radio[name="locale"]', function () {
-                selected_locale = this.value;
-                localStorage.setItem("selected_locale", selected_locale);
-                validateLocale();
-            });
-            $(document).on("input", localeTextbox, function () {
-                custom_locale = $(localeTextbox).val();
-                validateLocale(true);
-            });
-            $(document).on("input", ajaxTimeoutBox, function () {
-                ajaxSetup($(ajaxTimeoutBox).val(), false, true);
-            });
-            $(document).on("click", ".popover #settings-popover #ajax-timeout-restore", function () {
-                ajaxSetup(null, true, true);
-            });
-
-            // Dismiss Bootstrap popover by clicking outside
-            $("body").on("click", function (e) {
-                $(".popover").each(function () {
-                    if (
-                        // Popover's descendant
-                        $(this).has(e.target).length ||
-                        // Button (or icon within a button) that triggers the popover.
-                        $(e.target).closest("button").attr("aria-describedby") === this.id
-                    ) return;
-                    $("#settings").popover("hide");
-                });
-            });
-        }());
-
-        $("#selData").change(function () {
-            tabClick("#throughput_nav");
-        });
-
-        $(document).ajaxStart(function () {
-            $("#refresh > svg").addClass("fa-spin");
-        });
-        $(document).ajaxComplete(function () {
-            setTimeout(function () {
-                $("#refresh > svg").removeClass("fa-spin");
-            }, 1000);
-        });
-
-        $('a[data-bs-toggle="tab"]').on("shown.bs.tab", function () {
-            tabClick("#" + $(this).attr("id"));
-        });
-        $("#refresh, #disconnect").on("click", function (e) {
-            e.preventDefault();
-            tabClick("#" + $(this).attr("id"));
-        });
-        $(".dropdown-menu a").click(function (e) {
-            e.preventDefault();
-            var classList = $(this).attr("class");
-            var menuClass = (/\b(?:dynamic|history|preset)\b/).exec(classList)[0];
-            $(".dropdown-menu a.active." + menuClass).removeClass("active");
-            $(this).addClass("active");
-            tabClick("#autoRefresh");
-        });
-
-        $("#selSrv").change(function () {
-            checked_server = this.value;
-            $("#selSrv [value=\"" + checked_server + "\"]").prop("checked", true);
-            if (checked_server === "All SERVERS") {
-                $("#learnServers").show();
-            } else {
-                $("#learnServers").hide();
-            }
-            tabClick("#" + $("#tablist > .nav-item > .nav-link.active").attr("id"));
-        });
-
-        // Radio buttons
-        $(document).on("click", "input:radio[name=\"clusterName\"]", function () {
-            if (!this.disabled) {
-                checked_server = this.value;
-                tabClick("#status_nav");
-            }
-        });
-
-        $("#loading").addClass("d-none");
-    })();
 
     ui.connect = function () {
         // Prevent locking out of the WebUI if timeout is too low.
@@ -1078,5 +934,149 @@ function ($, NProgress) {
         return null;
     };
 
+
+    (function initSettings() {
+        var selected_locale = null;
+        var custom_locale = null;
+        const localeTextbox = ".popover #settings-popover #locale";
+
+        function validateLocale(saveToLocalStorage) {
+            function toggle_form_group_class(remove, add) {
+                $(localeTextbox).removeClass("is-" + remove).addClass("is-" + add);
+            }
+
+            var now = new Date();
+
+            if (custom_locale.length) {
+                try {
+                    now.toLocaleString(custom_locale);
+
+                    if (saveToLocalStorage) localStorage.setItem("custom_locale", custom_locale);
+                    locale = (selected_locale === "custom") ? custom_locale : null;
+                    toggle_form_group_class("invalid", "valid");
+                } catch (err) {
+                    locale = null;
+                    toggle_form_group_class("valid", "invalid");
+                }
+            } else {
+                if (saveToLocalStorage) localStorage.setItem("custom_locale", null);
+                locale = null;
+                $(localeTextbox).removeClass("is-valid is-invalid");
+            }
+
+            // Display date example
+            $(".popover #settings-popover #date-example").text(
+                (locale)
+                    ? now.toLocaleString(locale)
+                    : now.toLocaleString()
+            );
+        }
+
+        $("#settings").popover({
+            container: "body",
+            placement: "bottom",
+            html: true,
+            sanitize: false,
+            content: function () {
+                // Using .clone() has the side-effect of producing elements with duplicate id attributes.
+                return $("#settings-popover").clone();
+            }
+        // Restore the tooltip of the element that the popover is attached to.
+        }).attr("title", function () {
+            return $(this).attr("data-original-title");
+        });
+        $("#settings").on("click", function (e) {
+            e.preventDefault();
+        });
+        $("#settings").on("inserted.bs.popover", function () {
+            selected_locale = localStorage.getItem("selected_locale") || "browser";
+            custom_locale = localStorage.getItem("custom_locale") || "";
+            validateLocale();
+
+            $('.popover #settings-popover input:radio[name="locale"]').val([selected_locale]);
+            $(localeTextbox).val(custom_locale);
+
+            ajaxSetup(localStorage.getItem("ajax_timeout"), true);
+        });
+        $(document).on("change", '.popover #settings-popover input:radio[name="locale"]', function () {
+            selected_locale = this.value;
+            localStorage.setItem("selected_locale", selected_locale);
+            validateLocale();
+        });
+        $(document).on("input", localeTextbox, function () {
+            custom_locale = $(localeTextbox).val();
+            validateLocale(true);
+        });
+        $(document).on("input", ajaxTimeoutBox, function () {
+            ajaxSetup($(ajaxTimeoutBox).val(), false, true);
+        });
+        $(document).on("click", ".popover #settings-popover #ajax-timeout-restore", function () {
+            ajaxSetup(null, true, true);
+        });
+
+        // Dismiss Bootstrap popover by clicking outside
+        $("body").on("click", function (e) {
+            $(".popover").each(function () {
+                if (
+                    // Popover's descendant
+                    $(this).has(e.target).length ||
+                    // Button (or icon within a button) that triggers the popover.
+                    $(e.target).closest("button").attr("aria-describedby") === this.id
+                ) return;
+                $("#settings").popover("hide");
+            });
+        });
+    }());
+
+    $("#selData").change(function () {
+        tabClick("#throughput_nav");
+    });
+
+    $(document).ajaxStart(function () {
+        $("#refresh > svg").addClass("fa-spin");
+    });
+    $(document).ajaxComplete(function () {
+        setTimeout(function () {
+            $("#refresh > svg").removeClass("fa-spin");
+        }, 1000);
+    });
+
+    $('a[data-bs-toggle="tab"]').on("shown.bs.tab", function () {
+        tabClick("#" + $(this).attr("id"));
+    });
+    $("#refresh, #disconnect").on("click", function (e) {
+        e.preventDefault();
+        tabClick("#" + $(this).attr("id"));
+    });
+    $(".dropdown-menu a").click(function (e) {
+        e.preventDefault();
+        var classList = $(this).attr("class");
+        var menuClass = (/\b(?:dynamic|history|preset)\b/).exec(classList)[0];
+        $(".dropdown-menu a.active." + menuClass).removeClass("active");
+        $(this).addClass("active");
+        tabClick("#autoRefresh");
+    });
+
+    $("#selSrv").change(function () {
+        checked_server = this.value;
+        $("#selSrv [value=\"" + checked_server + "\"]").prop("checked", true);
+        if (checked_server === "All SERVERS") {
+            $("#learnServers").show();
+        } else {
+            $("#learnServers").hide();
+        }
+        tabClick("#" + $("#tablist > .nav-item > .nav-link.active").attr("id"));
+    });
+
+    // Radio buttons
+    $(document).on("click", "input:radio[name=\"clusterName\"]", function () {
+        if (!this.disabled) {
+            checked_server = this.value;
+            tabClick("#status_nav");
+        }
+    });
+
+    $("#loading").addClass("d-none");
+
     return ui;
 });
diff --git a/interface/js/app/selectors.js b/interface/js/app/selectors.js
index b47488ad0..8e9da052b 100644
--- a/interface/js/app/selectors.js
+++ b/interface/js/app/selectors.js
@@ -91,56 +91,55 @@ define(["jquery", "app/rspamd"],
             if (!$("#selectorsSelArea").is(".is-valid, .is-invalid")) checkSelectors();
         };
 
-        (() => {
-            function toggleSidebar(side) {
-                $("#sidebar-" + side).toggleClass("collapsed");
-                var contentClass = "col-lg-6";
-                var openSidebarsCount = $("#sidebar-left").hasClass("collapsed") +
-                        $("#sidebar-right").hasClass("collapsed");
-                switch (openSidebarsCount) {
-                    case 1:
-                        contentClass = "col-lg-9";
-                        break;
-                    case 2:
-                        contentClass = "col-lg-12";
-                        break;
-                    default:
-                }
-                $("#content").removeClass("col-lg-12 col-lg-9 col-lg-6")
-                    .addClass(contentClass);
+
+        function toggleSidebar(side) {
+            $("#sidebar-" + side).toggleClass("collapsed");
+            var contentClass = "col-lg-6";
+            var openSidebarsCount = $("#sidebar-left").hasClass("collapsed") +
+                    $("#sidebar-right").hasClass("collapsed");
+            switch (openSidebarsCount) {
+                case 1:
+                    contentClass = "col-lg-9";
+                    break;
+                case 2:
+                    contentClass = "col-lg-12";
+                    break;
+                default:
*** OUTPUT TRUNCATED, 236 LINES SKIPPED ***


More information about the Commits mailing list