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

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