User:Awesome Aasim/xfdvote.js/mfd.js

// MfD code follows var MfD = {}; $(document).ready(function {    MfD.$obj = $(".mbox-mfd").eq(0); //only add to first MfD tag in case of duplicate tags    MfD.target = $(MfD.$obj).find("b a").eq(1).attr("title"); // detect target of MfD    MfD.vote = function (vote) {        $(' ').append(' Enter your argument below. MfD 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 = $("#MfD-vote-reason").val; var speedy = $("#MfD-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"); $("#MfD-vote-reason").prop("disabled", true); $("#MfD-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");                                    $("#MfD-spinner").remove;                                    $("#MfD-vote-reason").prop("disabled", false);                                    $("#MfD-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: MfD.target                                    }).done(function (result) {                                        if (result.error) {                                            mw.notify($(' ' + result.error.info + ' '));                                            $(".ui-button").prop("disabled", false).removeClass("ui-state-disabled");                                            $("#MfD-spinner").remove;                                            $("#MfD-vote-reason").prop("disabled", false);                                            $("#MfD-vote-speedy").prop("disabled", false);                                        } else { var wikitext = result.parse.wikitext["*"]; if (wikitext.includes('<div class="boilerplate mfd vfd xfd-closed""')) {                                               $(".ui-button").prop("disabled", false).removeClass("ui-state-disabled");                                                $("#MfD-dialog").dialog("destroy").remove;                                                $(' ').dialog({                                                    buttons: [{                                                        text: "Visit MfD page",                                                        click: function  {                                                            window.location.href = "/wiki/" + MfD.target;                                                        }                                                    },                                                    {                                                        text: "OK", click: function { $(this).dialog('destroy').remove; }                                                   }],                                                    closeOnEscape: false, modal: true, title: "MfD 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");                                                $("#MfD-dialog").dialog("destroy").remove;                                                $(' ').dialog({ buttons: [{ text: "Visit MfD page", click: function { window.location.href = "/wiki/" + MfD.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 Participation tool" : "!Vote via XFD Participation tool"), title: MfD.target, nocreate: 1, token: token }).done(function (result) { if (result.error) { mw.notify($(' ' + result.error.info + ' ')); $("#MfD-vote-speedy").prop("disabled", false); $("#MfD-vote-reason").prop("disabled", false); $(".ui-button").prop("disabled", false).removeClass("ui-state-disabled"); $("#MfD-spinner").remove; } else { window.location.href = "/wiki/" + MfD.target; }                                               }).fail(function  { $("#MfD-vote-reason").prop("disabled", false); $("#MfD-vote-speedy").prop("disabled", false); $(".ui-button").prop("disabled", false).removeClass("ui-state-disabled"); $("#MfD-spinner").remove; mw.notify($("Try that again. Could not cast your vote.")); });                                           }                                        }                                    }).fail(function  {                                        $("#MfD-vote-reason").prop("disabled", false);                                        $("#MfD-vote-speedy").prop("disabled", false);                                        $(".ui-button").prop("disabled", false).removeClass("ui-state-disabled");                                        $("#MfD-spinner").remove;                                		mw.notify($("Try that again. Could not cast your vote."));                                    }); }                           }).fail(function  { $("#MfD-vote-reason").prop("disabled", false); $("#MfD-vote-speedy").prop("disabled", false); $(".ui-button").prop("disabled", false).removeClass("ui-state-disabled"); $("#MfD-spinner").remove; mw.notify($("Try that again. Could not cast your vote.")); });                       }                    }                },                {                    text: "Cancel",                    click: function  {                        $(this).dialog("destroy").remove;                        $("#MfD-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 ($("#MfD-dialog").length > 0) { var comment = $("#MfD-vote-reason").val; var speedy = $("#MfD-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: MfD.target,                    prop: "text",                    pst: "1",                    preview: "1",                    text: "\n* " + temp + "" + (comment ? ": " + comment : "") + " ~" + ((vote.toLowerCase != "comment") ? " " : "")               }).done(function (result) {                    if (result.error) {                        $("#MfD-vote-preview").html("Could not load preview.");                        mw.notify($("" + result.error.info + " "));                    } else {                        $("#MfD-vote-preview").html("Preview: " + result.parse.text["*"]);                    }                }).fail(function  {                    $("#MfD-vote-preview").html("Could not load preview.");                }).always(function  {                    window.setTimeout(preview, 100);                }); }       }        $("#MfD-vote-reason").keypress(function (e) {            if (e.which == 13) {                e.preventDefault;            }        }); };   MfD.$keep = $('Vote to keep '); MfD.$keep.click(function (e) {       e.preventDefault;        MfD.vote("Keep");    }).css({ "margin-right": "5px", "margin-left": "1px" }); MfD.$delete = $('Vote to delete '); MfD.$delete.click(function (e) {       e.preventDefault;        MfD.vote("Delete");    }).css({ "margin-right": "5px", "margin-left": "1px" }); MfD.$merge = $('Vote to merge '); MfD.$merge.click(function (e) {       e.preventDefault;        var target;        $(' ').append(' Please enter a proposed target to merge this page with:   ').dialog({ buttons: [ {                   text: "Next", click: function { target = $("#MfD-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 {                                MfD.vote("Merge with " + (target ? "" + target + "" : "some other page"));                            }                        }); }               },                {                    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 });       $("#MfD-search-box").keyup(function  { $.get(mw.config.get("wgScriptPath") + "/api.php", {               action: "query",                list: "search",                srsearch: $("#MfD-search-box").val,                format: "json"            }).done(function (result) {                if (result.error) {                } else {                    $("#MfD-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;                        }                        $("#MfD-search-results").append('');                    }                }            }).fail(function (result) {            }) });   }).css({ "margin-right": "5px", "margin-left": "1px" }); MfD.$redirect = $('Vote to redirect '); MfD.$redirect.click(function (e) {       e.preventDefault;        var target;        $(' ').append(' Please enter a proposed target to redirect this page to: <input list="MfD-search-results" id="MfD-search-box"><datalist id="MfD-search-results">  ').dialog({ buttons: [ {                   text: "Next", click: function { target = $("#MfD-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 {                                MfD.vote("Redirect to " + (target ? "" + target + "" : "some other page"));                            }                        }); }               },                {                    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 });       $("#MfD-search-box").keyup(function  { $.get(mw.config.get("wgScriptPath") + "/api.php", {               action: "query",                list: "search",                srsearch: $("#MfD-search-box").val,                format: "json"            }).done(function (result) {                if (result.error) {                } else {                    $("#MfD-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;                        }                        $("#MfD-search-results").append('<option value="' + result.query.search[i].title + '">');                    }                }            }).fail(function (result) {            }) });   }).css({ "margin-right": "5px", "margin-left": "1px" }); MfD.$comment = $('<button class="mw-ui-button" id="MfD-vote-comment">Leave a comment '); MfD.$comment.click(function (e) {       e.preventDefault;        MfD.vote("Comment");    }).css({ "margin-right": "5px", "margin-left": "1px" }); MfD.$custom = $('<button class="mw-ui-button" id="MfD-vote-custom">Custom vote '); MfD.$custom.click(function (e) {       e.preventDefault;        MfD.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: MfD.target,        prop: "text"    }).done(function (result) {        if (result.error) {            MfD.$obj.find("tr .mbox-text").append(' Deletion discussion actions: ');        } else {            var $text = $(result.parse.text["*"]);            MfD.$obj.find("tr .mbox-text").append(' Reason: ' + $text.find("p").eq(0).html + ' Deletion discussion actions: ');        }    }).fail(function  {        MfD.$obj.find("tr .mbox-text").append(' Deletion discussion actions: ');    }).always(function  {        $("#MfD-vote-buttons").append(MfD.$keep).append(MfD.$delete).append(MfD.$merge).append(MfD.$redirect).append(MfD.$comment).append(MfD.$custom);        if (mw.config.get("wgPageName") == "Template:Mfd" || mw.config.get("wgPageName") == "Template:Mfdx") {            $("#MfD-vote-buttons").html("If this was a real MfD nomination, the buttons to vote on the nomination would appear here."); }   }); }); // End of MfD code