User:PlayOboe/xfdvote.js/afd.js

// AfD code follows 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) {        $(' ').append(' 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 this page and this page for more information.   ' + (vote.toLowerCase == "delete" || vote.toLowerCase == "keep" ? ' ' : ' ') + 'Speedy ' + " " + ' ').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('                           '); $(".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($(' ' + result.error.info + ' '));                                    $(".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($(' ' + result.error.info + ' '));                                            $(".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; $(' ').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("") || wikitext.includes("")) && vote.toLowerCase != "comment") {                                                $(".ui-button").prop("disabled", false).removeClass("ui-state-disabled");                                                $("#AfD-dialog").dialog("destroy").remove;                                                $(' ').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") ? " " : ""), summary: ((vote.toLowerCase == "comment") ? "Comment via XFD voting tool" : "Vote via XFD voting tool"), title: AfD.target, nocreate: 1, token: token }).done(function (result) { if (result.error) { mw.notify($(' ' + result.error.info + ' ')); $("#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($("Try that again. 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($("Try that again. 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($("Try that again. 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") ? " " : "")               }).done(function (result) {                    if (result.error) {                        $("#AfD-vote-preview").html("Could not load preview.");                        mw.notify($("" + result.error.info + " "));                    } else {                        $("#AfD-vote-preview").html("Preview: " + result.parse.text["*"]);                    }                }).fail(function  {                    $("#AfD-vote-preview").html("Could not load preview.");                }).always(function  {                    window.setTimeout(preview, 100);                }); }       }        $("#AfD-vote-reason").keypress(function (e) {            if (e.which == 13) {                e.preventDefault;            }        }); };   AfD.$keep = $('Vote to keep '); AfD.$keep.click(function (e) {       e.preventDefault;        AfD.vote("Keep");    }).css({ "margin-right": "5px", "margin-left": "1px" }); AfD.$delete = $('Vote to delete '); AfD.$delete.click(function (e) {       e.preventDefault;        AfD.vote("Delete");    }).css({ "margin-right": "5px", "margin-left": "1px" }); AfD.$merge = $('Vote to merge '); AfD.$merge.click(function (e) {       e.preventDefault;        var target;        $(' ').append(' Please enter a proposed target to merge this article with:   ').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('');                    }                }            }).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 = $('Vote to redirect '); AfD.$redirect.click(function (e) {       e.preventDefault;        var target;        $(' ').append(' Please enter a proposed target to redirect this article to: <input list="AfD-search-results" id="AfD-search-box"><datalist id="AfD-search-results">  ').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 '); 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 '); 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(' Deletion discussion actions: ');        } else {            var $text = $(result.parse.text["*"]);            $text.find('.afd-help').remove;            AfD.$obj.find("tr .mbox-text").append(' Reason: ' + $text.find("p").eq(0).html + ' Deletion discussion actions: ');        }    }).fail(function  {        AfD.$obj.find("tr .mbox-text").append(' Deletion discussion actions: ');    }).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