MediaWiki:Gadget-patrolCount.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/patrolCount/MediaWiki:Gadget-patrolCount.js|user=[[U:AnnAngela]]|longId=ce13c202dcc17282c237f263ba777e2112fdbcb5|shortId=ce13c20|message=refactor(patrolCount): stop using deprecated `rctoken`}}';

"use strict";
$(function () {
    if (mw.config.get("wgAction") !== "view") {
        return;
    }
    var namespaceWatched = [
        "(main)",
        "template",
    ];
    var apiPrefix = "".concat(mw.config.get("wgServer")).concat(mw.config.get("wgScriptPath"), "/api.php");
    var newPageMax = 50;
    var writeCountNum = function (pages, plus) {
        var strCount = "";
        var page;
        if (pages.length !== 0) {
            var vNum = Math.round(Math.random() * (pages.length - 1));
            page = pages[vNum];
            var link = "".concat(encodeURIComponent(page.title), "&redirect=no&rcid=").concat(page.rcid);
            strCount = pages.length.toString();
            if (plus) {
                strCount += "+";
            }
            var title = page.title;
            if (!page.confidence) {
                title += '" class="patrollListNotConfident';
            }
            strCount = "(<a id=\"unpatrollArticle\" href=\"".concat(mw.config.get("wgServer")).concat(mw.config.get("wgScriptPath"), "/index.php?title=").concat(link, "\" title=\"").concat(title, "\">").concat(strCount, "</a>)");
            ptPatrollLink.attr("href", "".concat(mw.config.get("wgServer")).concat(mw.config.get("wgScriptPath"), "/index.php?title=Special:\u6700\u65B0\u9875\u9762&hidepatrolled=1"));
        }
        else {
            ptPatrollLink.attr("href", "".concat(mw.config.get("wgServer")).concat(mw.config.get("wgScriptPath"), "/index.php?title=Special:\u6700\u65B0\u9875\u9762&hidepatrolled=0"));
        }
        $("span#not-patrolled-count").html(strCount);
        generateList(pages);
        return page;
    };
    var showAllUnbind = [];
    var showAll = false;
    var prepareList = function (pages, countMax) {
        var $list = $("#patrollTooltipList").empty();
        var addItem = function (istart, iend) {
            for (var idx = istart; idx < iend; ++idx) {
                var page = pages[idx];
                var link = "".concat(encodeURIComponent(page.title), "&redirect=no&rcid=").concat(page.rcid);
                var shortTitle = page.title;
                if (shortTitle.length > 8) {
                    shortTitle = "".concat(shortTitle.slice(0, 7), "...");
                }
                var item = $("<li></li>").html("<a href=\"".concat(mw.config.get("wgServer")).concat(mw.config.get("wgScriptPath"), "/index.php?title=").concat(link, "\" title=\"").concat(page.title, "\">").concat(shortTitle, "</a>")).appendTo($list);
                if (!page.confidence) {
                    item.addClass("patrollListNotConfident");
                }
            }
        };
        var length = pages.length;
        if (length > countMax && !showAll) {
            addItem(0, countMax);
            var $showAll_1 = $("#patrollListShowAll");
            if ($showAll_1.length === 0) {
                $showAll_1 = $("<div></div>", {
                    id: "patrollListShowAll"
                }).css({
                    "text-align": "right",
                    "font-weight": "bold",
                    "margin-bottom": "10px"
                }).append($("<a></a>", {
                    text: "more...",
                    href: "#patrollListShowAll",
                    title: "Show all unpatrolled articles"
                }));
                $list.after($showAll_1);
            }
            else {
                $showAll_1.show();
            }
            $showAll_1.off("click");
            $showAll_1.on("click", function () {
                addItem(countMax, length);
                $showAll_1.hide();
                for (var idx = 0; idx < showAllUnbind.length; ++idx) {
                    showAllUnbind[idx].off("mouseover.autohide mouseout");
                }
                showAll = true;
            });
        }
        else {
            addItem(0, pages.length);
        }
    };
    var loadCvtooltip = function () {
        $("body").append($("<div></div>", {
            id: "patrollTooltip",
            style: "display: none;"
        }).css({
            "font-size": "0.75em",
            "margin-right": "30px"
        }).append($("<ul></ul>", {
            id: "patrollTooltipList"
        })));
        (function ($) {
            $.fn.cvtooltip = function (options) {
                var self = $(this);
                var defaults = {
                    panel: "body",
                    selector: "",
                    width: 0,
                    left: 0,
                    top: 0,
                    delay: -1,
                    speed: 600,
                    close: true,
                    callback: function () {
                        $.noop();
                    }
                };
                var param = $.extend({}, defaults, options || {});
                var controlID = self.attr("ID");
                var cvToolTipCssBtm = "position: absolute; border-color: transparent transparent #A7D7F9 transparent; border-style: dashed dashed solid dashed; border-width: 7px 7px 7px 7px; width: 0; overflow: hidden; right:40px; top:-17px;";
                var cvToolTipCssTop = "position: absolute; border-color: transparent transparent #A7D7F9 transparent; border-style: dashed dashed solid dashed; border-width: 7px 7px 7px 7px; width: 0; overflow: hidden; right:40px; top:-17px;";
                var cvToolTipCss = "z-index:713; display:none; position: absolute; border: 3px solid #A7D7F9; background-color: #F3F3F3; line-height:14px; border-radius: 10px; right:".concat(param.left, "px; top:").concat(param.top, "px;");
                if (param.width !== 0) {
                    cvToolTipCss += "width: ".concat(param.width, "px;");
                }
                if (!document.getElementById("".concat(controlID, "Body"))) {
                    var cvTipsElement = $("<div>");
                    cvTipsElement.attr({
                        id: "".concat(controlID, "Body"),
                        "class": "cvToolTip",
                        style: cvToolTipCss
                    });
                    var cvTipsElementBtm = $("<span>");
                    cvTipsElementBtm.attr("style", cvToolTipCssBtm);
                    cvTipsElement.append(cvTipsElementBtm);
                    var cvTipsElementTop = $("<span>");
                    cvTipsElementTop.attr("style", cvToolTipCssTop);
                    cvTipsElement.append(cvTipsElementTop);
                    var cvTipsElementContent = $("<span>");
                    cvTipsElementContent.attr("id", "".concat(controlID, "Content"));
                    cvTipsElementContent.css("float", "left");
                    cvTipsElement.append(cvTipsElementContent);
                    if (param.close) {
                        var cvTipsElementClose = $("<a>");
                        cvTipsElementClose.attr("id", "".concat(controlID, "Close"));
                        cvTipsElementClose.css("display", "none");
                        cvTipsElementClose.html('<span style="float:right; font-family:verdana; position: absolute; top:1px; right:5px; font-size:12px; cursor:pointer;">x</span>');
                        cvTipsElement.append(cvTipsElementClose);
                    }
                    $(param.panel).append(cvTipsElement);
                }
                var cttBody = $(document.getElementById("".concat(controlID, "Body")));
                var cttContent = $(document.getElementById("".concat(controlID, "Content")));
                var cttClose = $(document.getElementById("".concat(controlID, "Close")));
                cttBody.show();
                var ctt = {
                    body: cttBody,
                    content: function () {
                        self.show();
                        return self;
                    },
                    position: function () {
                        var p = $(param.selector).position();
                        cttBody.css({
                            top: p.top + param.top,
                            left: p.left + param.left
                        });
                    },
                    hide: function () {
                        cttClose.hide();
                        cttBody.off();
                        cttContent.slideUp(param.speed, function () {
                            ctt.content().hide().appendTo($(param.panel));
                            cttBody.remove();
                        });
                        param.callback();
                    },
                    timer: null,
                    show: function () {
                        if (cttContent.html() === "") {
                            cttContent.append(ctt.content()).css("height", "".concat(cttContent[0].scrollHeight, "px")).hide().slideDown(param.speed, function () {
                                cttContent.css("height", "");
                                cttBody.on({
                                    mouseover: function () {
                                        cttClose.show();
                                    },
                                    mouseout: function () {
                                        cttClose.hide();
                                    }
                                });
                            });
                        }
                        if (param.selector !== "") {
                            ctt.position();
                        }
                        if (param.delay >= 0) {
                            setTimeout(ctt.hide, param.delay);
                        }
                    }
                };
                ctt.show();
                cttClose.on("click", ctt.hide);
                return ctt;
            };
        })(jQuery);
    };
    var ttListShow = false;
    var generateList = function (pages) {
        if (ttListShow) {
            prepareList(pages, 10);
        }
        else {
            var timer_1 = null;
            var $ptPatroll_1 = $("#pt-patroll").off("mouseover.autohide mouseout");
            $ptPatroll_1.on("mouseover", function () {
                if (timer_1) {
                    return;
                }
                timer_1 = setTimeout(function () {
                    timer_1 = null;
                    if (pages.length !== 0 && !ttListShow) {
                        if (typeof $.fn.cvtooltip === "undefined") {
                            loadCvtooltip();
                        }
                        prepareList(pages, 10);
                        ttListShow = true;
                        var ctt_1 = $("#patrollTooltip").cvtooltip({
                            left: $(window).width() - ($("#pt-patroll").offset().left + $("#pt-patroll").outerWidth()),
                            top: $("#pt-patroll").offset().top + $("#pt-patroll").height() + 10,
                            callback: function () {
                                ttListShow = false;
                                showAll = false;
                                $ptPatroll_1.off("mouseover.autohide mouseout");
                            }
                        });
                        var tipCloseTimer_1;
                        var clearHideTimer = function () {
                            if (tipCloseTimer_1) {
                                clearTimeout(tipCloseTimer_1);
                                tipCloseTimer_1 = null;
                            }
                        };
                        ctt_1.body.on("mouseover.autohide", clearHideTimer);
                        $ptPatroll_1.on("mouseover.autohide", clearHideTimer);
                        var setHideTimer = function () {
                            if (!tipCloseTimer_1) {
                                tipCloseTimer_1 = setTimeout(ctt_1.hide, 1000);
                            }
                        };
                        ctt_1.body.on("mouseout", setHideTimer);
                        $ptPatroll_1.on("mouseout", setHideTimer);
                        showAllUnbind = [ctt_1.body, $ptPatroll_1];
                    }
                }, 500);
                $ptPatroll_1.on("mouseout", function () {
                    if (timer_1) {
                        clearTimeout(timer_1);
                        timer_1 = null;
                    }
                });
            });
        }
    };
    var missingPage = {};
    var checkMissing = function (pages, plus) {
        var missingQuery = [];
        for (var idx = 0; idx < pages.length; ++idx) {
            var title = pages[idx].title;
            if (typeof title === "undefined") {
                continue;
            }
            var isMissing = missingPage[title];
            if (typeof isMissing === "undefined") {
                missingQuery.push(title);
            }
            else if (isMissing) {
                pages.splice(idx, 1);
            }
        }
        if (missingQuery.length !== 0) {
            var pagesStr = missingQuery.join("|");
            var checkMissingURI = "".concat(apiPrefix, "?action=query&format=xml&prop=info");
            $.post(checkMissingURI, {
                titles: pagesStr
            }, function (result) {
                var regenerate = false;
                $(result).find("pages page").each(function () {
                    var $this = $(this);
                    var isMissing = typeof $this.attr("missing") !== "undefined";
                    var title = $this.attr("title");
                    missingPage[title] = isMissing;
                    if (isMissing) {
                        for (var idx = 0; idx < pages.length; ++idx) {
                            if (pages[idx].title === title) {
                                pages.splice(idx, 1);
                                break;
                            }
                        }
                        if (title === $("#unpatrollArticle").attr("title")) {
                            regenerate = true;
                        }
                    }
                });
                if (regenerate) {
                    writeCountNum(pages, plus);
                }
            });
        }
    };
    var addPatrollLink = (function () {
        var checked = false;
        var addlink = function (page) {
            var $patrollinks = $("<a></a>", {
                href: "index.php?title=".concat(encodeURIComponent(page.title), "&rcid=").concat(encodeURIComponent(page.rcid)),
                text: "标记此页面为已巡查"
            });
            var $divPatrolllink = $("<div></div>", {
                "class": "patrollink"
            }).append("[").append($patrollinks).append("]");
            $("div.printfooter").before($divPatrolllink);
            var markAsPatrol = function (e) {
                e.preventDefault();
                var data = {
                    rcid: page.rcid,
                    token: page.rctoken
                };
                var uri = "".concat(apiPrefix, "?format=xml&action=patrol");
                $patrollinks.text("Marking as patrolled...");
                $patrollinks = $patrollinks.parent();
                $.post(uri, data, function (data, status) {
                    if (status === "success") {
                        $patrollinks.html('<span style="color:green">Marked as patrolled</span>');
                        if (typeof kAjaxPatrolLinks_closeafter !== "undefined" && window.kAjaxPatrolLinks_closeafter === true) {
                            window.close();
                            window.open("http://toolserver.org/~krinkle/close.html", "_self");
                        }
                    }
                    else {
                        $patrollinks.html('<span style="color:red">Cannot mark as patrolled</span>');
                    }
                });
            };
            $patrollinks.on("click", markAsPatrol);
        };
        return function (pages) {
            if (!checked && $("div.patrollink").length === 0) {
                var pageName = mw.config.get("wgPageName");
                for (var idx = 0; idx < pages.length; ++idx) {
                    var page = pages[idx];
                    if (page.title === pageName) {
                        addlink(page);
                        break;
                    }
                }
                checked = true;
            }
        };
    })();
    var namespaceList = mw.config.get("wgNamespaceIds");
    var namespaceWatchList = namespaceWatched.map(function (i) {
        return namespaceList[i === "(main)" ? "" : i];
    });
    var lastRequest = 0;
    var updateUnpatrolled = function () {
        var d = new Date();
        if (d - lastRequest < 10000) {
            return;
        }
        lastRequest = d;
        var requestid = d.getTime();
        var newPages = "".concat(apiPrefix, "?action=query&format=xml&list=recentchanges&rctype=new&rcnamespace=").concat(namespaceWatchList.join("|"), "&rcshow=!redirect|!patrolled&meta=tokens&type=patrol&rcprop=title|ids|user|tags");
        $.get(newPages, {
            rclimit: newPageMax,
            requestid: requestid
        }, function (result) {
            var pages = [];
            var jqResult = $(result);
            jqResult.find("rc").each(function () {
                var $self = $(this);
                var confidence = typeof $self.attr("anon") === "undefined" && $self.find("tag").length === 0;
                var t = {
                    title: $self.attr("title"),
                    rcid: $self.attr("rcid"),
                    rctoken: jqResult.find("tokens"),
                    confidence: confidence
                };
                pages.push(t);
            });
            addPatrollLink(pages);
            var plus = jqResult.find("query-continue").length !== 0;
            if (pages.length !== 0) {
                checkMissing(pages, plus);
            }
            writeCountNum(pages, plus);
        });
    };
    updateUnpatrolled();
    var ptPatrollLink = $("<a></a>", {
        href: "Special:最新页面?hidepatrolled=1",
        title: wgULS("最新页面", "最新頁面"),
        text: wgULS("最新页面", "最新頁面")
    });
    $("body #p-personal ul li#pt-watchlist").after($("<li></li>", {
        id: "pt-patroll"
    }).append(ptPatrollLink).append($("<span></span>", {
        id: "not-patrolled-count"
    })));
    $("#pt-patroll").on("mouseover", updateUnpatrolled);
});

/* </pre> */