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

// TfD code follows var TfD = {}; $(document).ready(function {    TfD.$obj = $(".mbox-tfd").eq(0); //only add to first TfD tag in case of duplicate tags    TfD.$target = $(TfD.$obj).find("a:contains(\"its entry\")").eq(0).attr("title") ? $(TfD.$obj).find("a:contains(\"its entry\")").eq(0) : $(TfD.$obj).find("a:contains(\"this template's entry\")").eq(0);    TfD.target = TfD.$target.attr("title"); // detect target of TfD    TfD.vote = function (vote) {        $(' ').append(' Enter your argument below. TfD 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 = $("#TfD-vote-reason").val; var speedy = $("#TfD-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"); $("#TfD-vote-reason").prop("disabled", true); $("#TfD-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");                                    $("#TfD-spinner").remove;                                    $("#TfD-vote-reason").prop("disabled", false);                                    $("#TfD-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: TfD.target                                    }).done(function (result) {                                        if (result.error) {                                            mw.notify($(' ' + result.error.info + ' '));                                            $(".ui-button").prop("disabled", false).removeClass("ui-state-disabled");                                            $("#TfD-spinner").remove;                                            $("#TfD-vote-reason").prop("disabled", false);                                            $("#TfD-vote-speedy").prop("disabled", false);                                        } else { var wikitext = result.parse.wikitext["*"]; if ((wikitext.includes("")) && vote.toLowerCase != "comment") { $(".ui-button").prop("disabled", false).removeClass("ui-state-disabled"); $("#TfD-dialog").dialog("destroy").remove; $(' ').dialog({                                                   buttons: [{                                                        text: "Visit TfD page",                                                        click: function  {                                                            window.location.href = "/wiki/" + TfD.target + "#" + TfD.$target.attr("href").split("#")[1].split("_").join(" ");                                                        }                                                    },                                                    {                                                        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 {                                                $.get(mw.config.get("wgScriptPath") + "/api.php", { action: "parse", prop: "text", format: "json", page: TfD.target }).done(function (result) { var section; $(result.parse.text["*"]).find(".mw-headline").each(function (i) {                                                       if ($(this).text == TfD.$target.attr("href").split("#")[1].split("_").join(" ")) {                                                            section = i + 1;                                                        }                                                    }); $.post(mw.config.get("wgScriptPath") + "/api.php", {                                                       action: "edit",                                                        format: "json",                                                        section: section,                                                        appendtext: "\n* " + vote + "" + (comment ? ": " + comment : "") + " ~" + ((vote.toLowerCase != "comment") ? " " : ""),                                                       summary: "/* " + TfD.$target.attr("href").split("#")[1].split("_").join(" ") + "*/ " + ((vote.toLowerCase == "comment") ? "Comment via XFD Participation tool" : "!Vote via XFD Participation tool"),                                                       title: TfD.target,                                                        nocreate: 1,                                                        token: token                                                    }).done(function (result) {                                                        if (result.error) {                                                            mw.notify($(' ' + result.error.info + ' '));                                                            $("#TfD-vote-speedy").prop("disabled", false);                                                            $("#TfD-vote-reason").prop("disabled", false);                                                            $(".ui-button").prop("disabled", false).removeClass("ui-state-disabled"); $("#TfD-spinner").remove; } else { window.location.href = "/wiki/" + TfD.target + "#" + TfD.$target.attr("href").split("#")[1].split("_").join(" "); }                                                   }).fail(function  { $("#TfD-vote-reason").prop("disabled", false); $("#TfD-vote-speedy").prop("disabled", false); $(".ui-button").prop("disabled", false).removeClass("ui-state-disabled"); $("#TfD-spinner").remove; mw.notify($("Try that again. Could not cast your vote.")); });                                               }).fail(function (result) {                                                    $("#TfD-vote-reason").prop("disabled", false);                                                    $("#TfD-vote-speedy").prop("disabled", false);                                                    $(".ui-button").prop("disabled", false).removeClass("ui-state-disabled");                                                    $("#TfD-spinner").remove;                                                    mw.notify($("Try that again. Could not cast your vote."));                                                }); }                                       }                                    }).fail(function  { $("#TfD-vote-reason").prop("disabled", false); $("#TfD-vote-speedy").prop("disabled", false); $(".ui-button").prop("disabled", false).removeClass("ui-state-disabled"); $("#TfD-spinner").remove; mw.notify($("Try that again. Could not cast your vote.")); });                               }                            }).fail(function  {                                $("#TfD-vote-reason").prop("disabled", false);                                $("#TfD-vote-speedy").prop("disabled", false);                                $(".ui-button").prop("disabled", false).removeClass("ui-state-disabled");                                $("#TfD-spinner").remove;                                mw.notify($("Try that again. Could not cast your vote."));                            }); }                   }                },                {                    text: "Cancel", click: function { $(this).dialog("destroy").remove; $("#TfD-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 ($("#TfD-dialog").length > 0) {                var comment = $("#TfD-vote-reason").val;                var speedy = $("#TfD-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: TfD.target, prop: "text", pst: "1", preview: "1", text: "\n* " + temp + "" + (comment ? ": " + comment : "") + " ~" }).done(function (result) { if (result.error) { $("#TfD-vote-preview").html("Could not load preview."); mw.notify($("" + result.error.info + " ")); } else { $("#TfD-vote-preview").html("Preview: " + result.parse.text["*"]); }               }).fail(function  { $("#TfD-vote-preview").html("Could not load preview."); }).always(function { window.setTimeout(preview, 100); });           }        }        $("#TfD-vote-reason").keypress(function (e) { if (e.which == 13) { e.preventDefault; }       });    };    TfD.$keep = $('Vote to keep ');    TfD.$keep.click(function (e) { e.preventDefault; TfD.vote("Keep"); }).css({ "margin-right": "5px", "margin-left": "1px" });   TfD.$delete = $('Vote to delete ');    TfD.$delete.click(function (e) { e.preventDefault; TfD.vote("Delete"); }).css({ "margin-right": "5px", "margin-left": "1px" });   TfD.$merge = $('Vote to merge ');    TfD.$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 = $("#TfD-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 { TfD.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        }); $("#TfD-search-box").keyup(function {            $.get(mw.config.get("wgScriptPath") + "/api.php", { action: "query", list: "search", srsearch: $("#TfD-search-box").val, format: "json" }).done(function (result) { if (result.error) { } else { $("#TfD-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; }                       $("#TfD-search-results").append(''); }               }            }).fail(function (result) { })       });    }).css({ "margin-right": "5px", "margin-left": "1px" });    TfD.$comment = $('Comment ');    TfD.$comment.click(function (e) { e.preventDefault; TfD.vote("Comment"); }).css({ "margin-right": "5px", "margin-left": "1px" });   TfD.$custom = $('<button class="mw-ui-button" id="TfD-vote-custom">Custom vote ');    TfD.$custom.click(function (e) { e.preventDefault; TfD.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: TfD.target, prop: "text" }).done(function (result) { if (result.error) { TfD.$obj.find("tr .mbox-text").append(' Deletion discussion actions: '); } else { var section; $(result.parse.text["*"]).find(".mw-headline").each(function (i) {               if ($(this).text == TfD.$target.attr("href").split("#")[1].split("_").join(" ")) {                    section = i + 1;                }            }); $.get(mw.config.get("wgScriptPath") + "/api.php", {               action: "parse",                format: "json",                page: TfD.target,                prop: "text",                section: section            }).done(function (result) {                if (result.error) {                    TfD.$obj.find("tr .mbox-text").append(' Deletion discussion actions: ');                } else {                    var $text = $(result.parse.text["*"]);                    TfD.$obj.find("tr .mbox-text").append(" Reason: " + $text.find("p").eq(0).html + ' Deletion discussion actions: ');                }            }).fail(function  {                TfD.$obj.find("tr .mbox-text").append(' Deletion discussion actions: ');            }).always(function  {                $("#TfD-vote-buttons").append(TfD.$keep).append(TfD.$delete).append(TfD.$merge).append(TfD.$comment).append(TfD.$custom); if ((mw.config.get("wgNamespaceNumber") != 10 && mw.config.get("wgNamespaceNumber") != 828) || mw.config.get("wgPageName") == "Template:Template_for_discussion" || mw.config.get("wgPageName") == "Template:Tfm") { $("#TfD-vote-buttons").html("If this was a real TfD nomination, the buttons to vote on the nomination would appear here."); }           });        }    }).fail(function  {        TfD.$obj.find("tr .mbox-text").append(' Deletion discussion actions: ');    }).always(function  {        $("#TfD-vote-buttons").append(TfD.$keep).append(TfD.$delete).append(TfD.$merge).append(TfD.$comment).append(TfD.$custom);        if ((mw.config.get("wgNamespaceNumber") != 10 && mw.config.get("wgNamespaceNumber") != 828) || mw.config.get("wgPageName") == "Template:Template_for_discussion" || mw.config.get("wgPageName") == "Template:Tfm") {            $("#TfD-vote-buttons").html("If this was a real TfD nomination, the buttons to vote on the nomination would appear here.");        }    }); }); // End of TfD code