MediaWiki:Gadget-allmessageFilter.js

From Moegirlpedia
Jump to: navigation, search

Note: After saving, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
/* <pre> */
/**
 * -------------------------------------------------------------------------
 * !!! DON'T MODIFY THIS PAGE MANUALLY, YOUR CHANGES WILL BE OVERWRITTEN !!!
 * -------------------------------------------------------------------------
 */
var _addText = '{{GHIACode|page=GHIA:MoegirlPediaInterfaceCodes/blob/master/src/gadgets/allmessageFilter/MediaWiki:Gadget-allmessageFilter.js|user=[[U:AnnAngela]]|longId=4f769b86c99bf278bee8d120937a50b49682e4e2|shortId=4f769b8|message=refactor: Stop using jQuery deprecated event shorthand methods}}';

"use strict";
(function (isCompleted) {
    function run() {
        if (mw.config.get("wgNamespaceNumber") !== -1 || mw.config.get("wgCanonicalSpecialPageName") !== "Allmessages") {
            return;
        }
        mw.util.addCSS([
            ".TablePager_nav {",
            "    user-select: none;",
            "}",
            ".TablePager_nav-enabled {",
            "    cursor: pointer;",
            "}",
            "#mw-allmessages-filter-status {",
            "    color: red;",
            "}",
        ].join("\n"));
        var containter = $("<fieldset/>"), api = new mw.Api(), length = +$("#mw-table_pager_limit_label").val();
        var index = 0, filter, rfilter, am;
        var enablePrevLink = function enablePrevLink(b) {
            if (!b) {
                $(".TablePager_nav-prev div").removeClass("TablePager_nav-enabled").addClass("TablePager_nav-disabled");
            }
            else {
                $(".TablePager_nav-prev div").removeClass("TablePager_nav-disabled").addClass("TablePager_nav-enabled");
            }
        };
        var enableNextLink = function enableNextLink(b) {
            if (!b) {
                $(".TablePager_nav-next div").removeClass("TablePager_nav-enabled").addClass("TablePager_nav-disabled");
            }
            else {
                $(".TablePager_nav-next div").removeClass("TablePager_nav-disabled").addClass("TablePager_nav-enabled");
            }
        };
        var check = function check(am) {
            if (am.length <= length) {
                enableNextLink(false);
                enablePrevLink(false);
            }
            else if (index === 0) {
                enablePrevLink(false);
                enableNextLink(true);
            }
            else if (index + length < am.length) {
                enablePrevLink(true);
                enableNextLink(true);
            }
            else {
                enablePrevLink(true);
                enableNextLink(false);
            }
        };
        var init = function init() {
            if (!$("#mw-allmessages-form")[0]) {
                return;
            }
            $("#mw-allmessages-form").remove();
            $(".TablePager_nav td").remove();
            $(".TablePager_nav tr").append('<td style="width: 50%;" class="TablePager_nav-prev"><div class="TablePager_nav-disabled">上一页</div></td><td style="width: 50%;" class="TablePager_nav-next"><div class="TablePager_nav-disabled">下一页</div></td>');
            $(".TablePager_nav-prev div").on("click", function () {
                if ($(this).hasClass("TablePager_nav-disabled")) {
                    return false;
                }
                index -= length;
                if (index < 0) {
                    index = 0;
                }
                load(am.slice(index, index + length));
            });
            $(".TablePager_nav-next div").on("click", function () {
                if ($(this).hasClass("TablePager_nav-disabled")) {
                    return false;
                }
                index += length;
                load(am.slice(index, index + length));
            });
        };
        var load = function (_am) {
            check(am);
            var head = $("#mw-allmessagestable thead");
            if (head.find("tr").length !== 1) {
                head.find("td:first").removeAttr("rowspan");
                head.find("tr:first td:last").text("当前信息文字");
                head.find("tr:not(:first)").remove();
            }
            var list = $("#mw-allmessagestable tbody");
            list.empty();
            _am.forEach(function (n) {
                var name = n.name, text = n["*"];
                var row = $("<tr/>");
                row.attr("id", name).append('<td class="am_title"></td><td class="am_default"></td>');
                $("<a/>").addClass("external").attr({
                    href: "".concat(mw.config.get("wgServer")).concat(mw.config.get("wgScriptPath"), "/MediaWiki:").concat(name),
                    rel: "nofollow"
                }).html(name.replace(rfilter, "<span style=\"font-weight: bold;\">".concat(filter, "</span>"))).appendTo(row.find(".am_title"));
                row.find(".am_default").text(text);
                list.append(row);
            });
        };
        $("#mw-allmessages-form").after(containter);
        containter.append("<legend>搜索</legend>");
        var table = $("<table/>");
        containter.append(table);
        table.append("<tr></tr><tr></tr>");
        $("<td/>").addClass("mw-label").html('<label for="mw-allmessages-filter">以含有此字符串过滤:</label>').appendTo(table.find("tr:first"));
        $("<td/>").addClass("mw-input").html('<input size="20" value="" id="mw-allmessages-filter">').appendTo(table.find("tr:first"));
        $("<td/>").appendTo(table.find("tr:last"));
        var submit = $("<button/>");
        submit.text("搜索");
        $("<td/>").addClass("mw-input").append(submit).appendTo(table.find("tr:last"));
        var input = $("#mw-allmessages-filter");
        submit.on("click", function () {
            if (!input.val()) {
                return oouiDialog.alert("请输入内容以搜索系统消息");
            }
            $("#mw-allmessages-filter-status").remove();
            init();
            filter = input.val();
            rfilter = RegExp(filter, "i");
            containter.append('<div id="mw-allmessages-filter-status">正在加载中……</div>');
            api.post({
                action: "query",
                format: "json",
                meta: "allmessages",
                amfilter: filter
            }).then(function (data) {
                am = data.query.allmessages;
                index = 0;
                $("#mw-allmessages-filter-status").remove();
                load(am.slice(index, index + length));
            }, function () {
                $("#mw-allmessages-filter-status").remove();
                containter.append('<div id="mw-allmessages-filter-status">发生错误,请重试!</div>');
            });
        });
        input.on("keypress", function (e) {
            if (e.key === "Enter") {
                submit.trigger("click");
            }
        });
    }
    if (isCompleted) {
        run();
    }
    else {
        $(window).on("load", run);
    }
})(document.readyState === "complete");

/* </pre> */