Jump to content

User:PlayOboe/xfdvote.js/afd.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// AfD code follows <nowiki>
var AfD = {};
$(document).ready(function () {
    AfD.$obj = $(".mbox-afd").eq(0); //only add to first AfD tag in case of duplicate tags
    AfD.target = $(AfD.$obj).find("b a").eq(1).attr("title"); // detect target of AfD
    AfD.vote = function (vote) {
        $('<div id="AfD-dialog"></div>').append('<p>Enter your argument below.  AfD votes are weighted based on the strength of arguments, not based on number of votes.  Cite relevant policies and guidelines where needed.  See <a href="/wiki/Wikipedia:Polling_is_not_a_substitute_for_discussion">this page</a> and <a href="/wiki/Wikipedia:Arguments_to_avoid_in_deletion_discussions">this page</a> for more information.</p><form id="AfD-vote"><div><textarea id="AfD-vote-reason" placeholder="' + ((vote.toLowerCase() == "comment") ? 'Comment' : 'Reason to ' + vote.toLowerCase()) + '" height="5"></textarea></div>' + (vote.toLowerCase() == "delete" || vote.toLowerCase() == "keep" ? '<div>' : '<div style="display:none;">') + '<input type="checkbox" id="AfD-vote-speedy"><label for="AfD-vote-speedy">Speedy</label>' + "</div></form>" + '<div id="AfD-vote-preview"></div>').dialog({
            buttons: [
                {
                    text: "Proceed",
                    click: function () {
                        var comment = $("#AfD-vote-reason").val();
                        var speedy = $("#AfD-vote-speedy").is(":checked") ? true : false;
                        if (vote.toLowerCase() == "comment" && comment.length == 0) {
                            alert("Please add a comment.");
                        } else {
                            if (speedy) {
                                vote = "Speedy " + vote.toLowerCase();
                            }
                            $(".ui-dialog-buttonpane").eq($(".ui-dialog-buttonpane").length - 1).prepend('<div class="mw-spinner mw-spinner-small mw-spinner-inline" title="..." id="AfD-spinner"><div class="mw-spinner-container"><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div></div></div>');
                            $(".ui-button").prop("disabled", true).addClass("ui-state-disabled");
                            $("#AfD-vote-reason").prop("disabled", true);
                            $("#AfD-vote-speedy").prop("disabled", true);
                            $.get(mw.config.get("wgScriptPath") + "/api.php", {
                                action: "query",
                                format: "json",
                                meta: "tokens",
                                type: "csrf"
                            }).done(function (result) {
                                if (result.error) {
                                    mw.notify($('<span class="error">' + result.error.info + '</span>'));
                                    $(".ui-button").prop("disabled", false).removeClass("ui-state-disabled");
                                    $("#AfD-spinner").remove();
                                    $("#AfD-vote-reason").prop("disabled", false);
                                    $("#AfD-vote-speedy").prop("disabled", false);
                                } else {
                                    var token = result.query.tokens.csrftoken;
                                    $.get(mw.config.get("wgScriptPath") + "/api.php", {
                                        action: "parse",
                                        prop: "wikitext",
                                        format: "json",
                                        page: AfD.target
                                    }).done(function (result) {
                                        if (result.error) {
                                            mw.notify($('<span class="error">' + result.error.info + '</span>'));
                                            $(".ui-button").prop("disabled", false).removeClass("ui-state-disabled");
                                            $("#AfD-spinner").remove();
                                            $("#AfD-vote-reason").prop("disabled", false);
                                            $("#AfD-vote-speedy").prop("disabled", false);
                                        } else {
                                            var wikitext = result.parse.wikitext["*"];
                                            if (wikitext.includes('<div class="boilerplate afd vfd xfd-closed"')) {
                                                $(".ui-button").prop("disabled", false).removeClass("ui-state-disabled");
                                                $("#AfD-dialog").dialog("destroy").remove();
                                                $('<table class="plainlinks plainlinks ombox ombox-content" role="presentation"><tbody><tr><td class="mbox-image"><img alt="" src="//upload.wikimedia.org/wikipedia/commons/thumb/3/33/OOjs_UI_icon_clear-destructive.svg/40px-OOjs_UI_icon_clear-destructive.svg.png" decoding="async" width="40" height="40" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/3/33/OOjs_UI_icon_clear-destructive.svg/60px-OOjs_UI_icon_clear-destructive.svg.png 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/3/33/OOjs_UI_icon_clear-destructive.svg/80px-OOjs_UI_icon_clear-destructive.svg.png 2x" data-file-width="20" data-file-height="20"></td><td class="mbox-text"><b>The AfD discussion appears to have been closed.</b><br><p>Please visit the AfD page to double-check.</p></td></tr></tbody></table>').dialog({
                                                    buttons: [{
                                                        text: "Visit AfD page",
                                                        click: function () {
                                                            window.location.href = "/wiki/" + AfD.target;
                                                        }
                                                    },
                                                    {
                                                        text: "OK",
                                                        click: function () {
                                                            $(this).dialog('destroy').remove();
                                                        }
                                                    }],
                                                    closeOnEscape: false,
                                                    modal: true,
                                                    title: "AfD Discussion Closed",
                                                    width: (0.50 * window.outerWidth > 300) ? 0.50 * window.outerWidth : 300
                                                });
                                            } else if ((wikitext.includes("<!--Vote cast using XFD voting tool by " + mw.config.get("wgUserName") + " -->") || wikitext.includes("<!--VCB " + mw.config.get("wgUserName") + "-->")) && vote.toLowerCase() != "comment") {
                                                $(".ui-button").prop("disabled", false).removeClass("ui-state-disabled");
                                                $("#AfD-dialog").dialog("destroy").remove();
                                                $('<table class="plainlinks plainlinks ombox ombox-content" role="presentation"><tbody><tr><td class="mbox-image"><img alt="" src="//upload.wikimedia.org/wikipedia/commons/thumb/3/33/OOjs_UI_icon_clear-destructive.svg/40px-OOjs_UI_icon_clear-destructive.svg.png" decoding="async" width="40" height="40" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/3/33/OOjs_UI_icon_clear-destructive.svg/60px-OOjs_UI_icon_clear-destructive.svg.png 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/3/33/OOjs_UI_icon_clear-destructive.svg/80px-OOjs_UI_icon_clear-destructive.svg.png 2x" data-file-width="20" data-file-height="20"></td><td class="mbox-text"><b>You\'ve already voted.</b>  Please modify your existing vote on the AfD page rather than casting a new vote.  If you have struck out your vote, please make sure to remove the "&lt;!--Vote cast using XFD voting tool by ' + mw.config.get("wgUserName") + ' -->" or "&lt;"!--VCB ' + mw.config.get("wgUserName") + '-->"code before trying again.</p></td></tr></tbody></table>').dialog({
                                                    buttons: [{
                                                        text: "Visit AfD page",
                                                        click: function () {
                                                            window.location.href = "/wiki/" + AfD.target;
                                                        }
                                                    },
                                                    {
                                                        text: "OK",
                                                        click: function () {
                                                            $(this).dialog('destroy').remove();
                                                        }
                                                    }],
                                                    closeOnEscape: false,
                                                    modal: true,
                                                    title: "You've already voted",
                                                    width: (0.50 * window.outerWidth > 300) ? 0.50 * window.outerWidth : 300
                                                });
                                            } else {
                                                $.post(mw.config.get("wgScriptPath") + "/api.php", {
                                                    action: "edit",
                                                    format: "json",
                                                    appendtext: "\n* '''" + vote + "'''" + (comment ? ": " + comment : "") + " ~~~~" + ((vote.toLowerCase() != "comment") ? " <!--VCB " + mw.config.get("wgUserName") + "-->" : ""),
                                                    summary: ((vote.toLowerCase() == "comment") ? "Comment via [[User:Awesome Aasim/xfdvote|XFD voting tool]]" : "Vote via [[User:Awesome Aasim/xfdvote|XFD voting tool]]"),
                                                    title: AfD.target,
                                                    nocreate: 1,
                                                    token: token
                                                }).done(function (result) {
                                                    if (result.error) {
                                                        mw.notify($('<span class="error">' + result.error.info + '</span>'));
                                                        $("#AfD-vote-speedy").prop("disabled", false);
                                                        $("#AfD-vote-reason").prop("disabled", false);
                                                        $(".ui-button").prop("disabled", false).removeClass("ui-state-disabled");
                                                        $("#AfD-spinner").remove();
                                                    } else {
                                                        window.location.href = "/wiki/" + AfD.target;
                                                    }
                                                }).fail(function () {
                                                    $("#AfD-vote-reason").prop("disabled", false);
                                                    $("#AfD-vote-speedy").prop("disabled", false);
                                                    $(".ui-button").prop("disabled", false).removeClass("ui-state-disabled");
                                                    $("#AfD-spinner").remove();
                                					mw.notify($("<b>Try that again.</b><br>Could not cast your vote."));
                                                });
                                            }
                                        }
                                    }).fail(function () {
                                        $("#AfD-vote-reason").prop("disabled", false);
                                        $("#AfD-vote-speedy").prop("disabled", false);
                                        $(".ui-button").prop("disabled", false).removeClass("ui-state-disabled");
                                        $("#AfD-spinner").remove();
                                		mw.notify($("<b>Try that again.</b><br>Could not cast your vote."));
                                    });
                                }
                            }).fail(function () {
                                $("#AfD-vote-reason").prop("disabled", false);
                                $("#AfD-vote-speedy").prop("disabled", false);
                                $(".ui-button").prop("disabled", false).removeClass("ui-state-disabled");
                                $("#AfD-spinner").remove();
                                mw.notify($("<b>Try that again.</b><br>Could not cast your vote."));
                            });
                        }
                    }
                },
                {
                    text: "Cancel",
                    click: function () {
                        $(this).dialog("destroy").remove();
                        $("#AfD-spinner").remove();
                    }
                }
            ],
            closeOnEscape: false,
            modal: true,
            title: (vote.toLowerCase() == "comment" ? "Comment on the nomination of " : "Vote to " + vote.toLowerCase()) + " \"" + mw.config.get("wgPageName") + "\"",
            width: (0.50 * window.outerWidth > 300) ? 0.50 * window.outerWidth : 300
        });
        preview();
        function preview() {
            if ($("#AfD-dialog").length > 0) {
                var comment = $("#AfD-vote-reason").val();
                var speedy = $("#AfD-vote-speedy").is(":checked") ? true : false;
                var temp = vote;
                if (speedy) {
                    temp = "Speedy " + temp.toLowerCase();
                }
                $.get(mw.config.get("wgScriptPath") + "/api.php", {
                    action: "parse",
                    format: "json",
                    title: AfD.target,
                    prop: "text",
                    pst: "1",
                    preview: "1",
                    text: "\n* '''" + temp + "'''" + (comment ? ": " + comment : "") + " ~~~~" + ((vote.toLowerCase() != "comment") ? " <!--VCB " + mw.config.get("wgUserName") + "-->" : "")
                }).done(function (result) {
                    if (result.error) {
                        $("#AfD-vote-preview").html("<i>Could not load preview.</i>");
                        mw.notify($("<span class=\"error\">" + result.error.info + "</span>"));
                    } else {
                        $("#AfD-vote-preview").html("Preview: <br>" + result.parse.text["*"]);
                    }
                }).fail(function () {
                    $("#AfD-vote-preview").html("<i>Could not load preview.</i>");
                }).always(function () {
                    window.setTimeout(preview, 100);
                });
            }
        }
        $("#AfD-vote-reason").keypress(function (e) {
            if (e.which == 13) {
                e.preventDefault();
            }
        });
    };
    AfD.$keep = $('<button class="mw-ui-button mw-ui-progressive" id="AfD-vote-keep">Vote to keep</button>');
    AfD.$keep.click(function (e) {
        e.preventDefault();
        AfD.vote("Keep");
    }).css({ "margin-right": "5px", "margin-left": "1px" });
    AfD.$delete = $('<button class="mw-ui-button mw-ui-destructive" id="AfD-vote-delete">Vote to delete</button>');
    AfD.$delete.click(function (e) {
        e.preventDefault();
        AfD.vote("Delete");
    }).css({ "margin-right": "5px", "margin-left": "1px" });
    AfD.$merge = $('<button class="mw-ui-button" id="AfD-vote-merge">Vote to merge</button>');
    AfD.$merge.click(function (e) {
        e.preventDefault();
        var target;
        $('<div id="AfD-search"></div>').append('<p>Please enter a proposed target to merge this article with:</p><form><input list="AfD-search-results" id="AfD-search-box"><datalist id="AfD-search-results"></datalist></form>').dialog({
            buttons: [
                {
                    text: "Next",
                    click: function () {
                        target = $("#AfD-search-box").val();
                        $.get(mw.config.get("wgScriptPath") + "/api.php", {
                            action: "parse",
                            format: "json",
                            prop: "text",
                            page: target
                        }).done(function (result) {
                            if (result.error) {
                                alert(result.error.info);
                            } else {
                                AfD.vote("Merge with " + (target ? "[[" + target + "]]" : "some other article"));
                            }
                        });
                    }
                },
                {
                    text: "Cancel",
                    click: function () {
                        $(this).dialog("destroy").remove();
                    }
                }
            ],
            closeOnEscape: false,
            modal: true,
            title: "Enter a title",
            width: (0.50 * window.outerWidth > 300) ? 0.50 * window.outerWidth : 300
        });
        $("#AfD-search-box").keyup(function () {
            $.get(mw.config.get("wgScriptPath") + "/api.php", {
                action: "query",
                list: "search",
                srsearch: $("#AfD-search-box").val(),
                format: "json"
            }).done(function (result) {
                if (result.error) {
                } else {
                    $("#AfD-search-results").html('');
                    for (var i = 0; i < result.query.search.length; i++) {
                        if (result.query.search[i].title == mw.config.get("wgPageName").replace("_", " ")) {
                            continue;
                        }
                        $("#AfD-search-results").append('<option value="' + result.query.search[i].title + '">');
                    }
                }
            }).fail(function (result) {
            })
        });
        $("#AfD-search-box").keydown(function(e) {
            if (e.which == 13) {
                target = $("#AfD-search-box").val();
                $.get(mw.config.get("wgScriptPath") + "/api.php", {
                    action: "parse",
                    format: "json",
                    prop: "text",
                    page: target
                }).done(function (result) {
                    if (result.error) {
                        alert(result.error.info);
                    } else {
                        AfD.vote("Redirect to " + (target ? "[[" + target + "]]" : "some other article"));
                    }
                });
            }
        });
    }).css({ "margin-right": "5px", "margin-left": "1px" });
    AfD.$redirect = $('<button class="mw-ui-button" id="AfD-vote-redirect">Vote to redirect</button>');
    AfD.$redirect.click(function (e) {
        e.preventDefault();
        var target;
        $('<div id="AfD-search"></div>').append('<p>Please enter a proposed target to redirect this article to:</p><form><input list="AfD-search-results" id="AfD-search-box"><datalist id="AfD-search-results"></datalist></form>').dialog({
            buttons: [
                {
                    text: "Next",
                    click: function () {
                        target = $("#AfD-search-box").val();
                        $.get(mw.config.get("wgScriptPath") + "/api.php", {
                            action: "parse",
                            format: "json",
                            prop: "text",
                            page: target
                        }).done(function (result) {
                            if (result.error) {
                                alert(result.error.info);
                            } else {
                                AfD.vote("Redirect to " + (target ? "[[" + target + "]]" : "some other article"));
                            }
                        });
                    }
                },
                {
                    text: "Cancel",
                    click: function () {
                        $(this).dialog("destroy").remove();
                    }
                }
            ],
            closeOnEscape: false,
            modal: true,
            title: "Enter a title",
            width: (0.50 * window.outerWidth > 300) ? 0.50 * window.outerWidth : 300
        });
        $("#AfD-search-box").keyup(function () {
            $.get(mw.config.get("wgScriptPath") + "/api.php", {
                action: "query",
                list: "search",
                srsearch: $("#AfD-search-box").val(),
                format: "json"
            }).done(function (result) {
                if (result.error) {
                } else {
                    $("#AfD-search-results").html('');
                    for (var i = 0; i < result.query.search.length; i++) {
                        if (result.query.search[i].title == mw.config.get("wgPageName").replace("_", " ")) {
                            continue;
                        }
                        $("#AfD-search-results").append('<option value="' + result.query.search[i].title + '">');
                    }
                }
            }).fail(function (result) {
            })
        });
        $("#AfD-search-box").keydown(function(e) {
            if (e.which == 13) {
                target = $("#AfD-search-box").val();
                $.get(mw.config.get("wgScriptPath") + "/api.php", {
                    action: "parse",
                    format: "json",
                    prop: "text",
                    page: target
                }).done(function (result) {
                    if (result.error) {
                        alert(result.error.info);
                    } else {
                        AfD.vote("Redirect to " + (target ? "[[" + target + "]]" : "some other article"));
                    }
                });
            }
        });
    }).css({ "margin-right": "5px", "margin-left": "1px" });
    AfD.$comment = $('<button class="mw-ui-button" id="AfD-vote-comment">Leave a comment</button>');
    AfD.$comment.click(function (e) {
        e.preventDefault();
        AfD.vote("Comment");
    }).css({ "margin-right": "5px", "margin-left": "1px" });
    AfD.$custom = $('<button class="mw-ui-button" id="AfD-vote-custom">Custom vote</button>');
    AfD.$custom.click(function (e) {
        e.preventDefault();
        AfD.vote(prompt("Enter your custom vote here:"));
    }).css({ "margin-right": "5px", "margin-left": "1px" });
    $.get(mw.config.get("wgScriptPath") + "/api.php", {
        action: "parse",
        format: "json",
        page: AfD.target,
        prop: "text"
    }).done(function (result) {
        if (result.error) {
            AfD.$obj.find("tr .mbox-text").append('<hr>Deletion discussion actions:<div id="AfD-vote-buttons"></div>');
        } else {
            var $text = $(result.parse.text["*"]);
            $text.find('.afd-help').remove();
            AfD.$obj.find("tr .mbox-text").append('<hr>Reason: ' + $text.find("p").eq(0).html() + '<hr>Deletion discussion actions:<div id="AfD-vote-buttons"></div>');
        }
    }).fail(function () {
        AfD.$obj.find("tr .mbox-text").append('<hr>Deletion discussion actions:<div id="AfD-vote-buttons"></div>');
    }).always(function () {
        $("#AfD-vote-buttons").append(AfD.$keep).append(AfD.$delete).append(AfD.$merge).append(AfD.$redirect).append(AfD.$comment).append(AfD.$custom);
        if (mw.config.get("wgNamespaceNumber") != 0) {
            $("#AfD-vote-buttons").html("If this was a real AfD nomination, the buttons to vote on the nomination would appear here.");
        }
    });
});
// End of AfD code </nowiki>