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

// RfD code follows var RfD = {}; if (mw.config.get("wgPageName").includes("Wikipedia:Redirects_for_discussion/Log")) { $(document).ready(function {        RfD.vote = function (vote, target) {            $(' ').append(' Enter your argument below. RfD 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 = $("#RfD-vote-reason").val; var speedy = $("#RfD-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"); $("#RfD-vote-reason").prop("disabled", true); $("#RfD-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");                                        $("#RfD-spinner").remove;                                        $("#RfD-vote-reason").prop("disabled", false);                                        $("#RfD-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: mw.config.get("wgPageName")                                        }).done(function (result) {                                            if (result.error) {                                                mw.notify($(' ' + result.error.info + ' '));                                                $(".ui-button").prop("disabled", false).removeClass("ui-state-disabled");                                                $("#RfD-spinner").remove;                                                $("#RfD-vote-reason").prop("disabled", false);                                                $("#RfD-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"); $("#RfD-dialog").dialog("destroy").remove; $(' ').dialog({                                                       buttons: [                                                            {                                                                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", "format": "json", "prop": "text", page: mw.config.get("wgPageName") }).done(function (result) { var section; $(result.parse.text["*"]).find(".mw-headline").each(function (i) {                                                           if ($(this).text == target) {                                                                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: "/* " + target + " */ " + ((vote.toLowerCase == "comment") ? "Comment via XFD Participation tool" : "!Vote via XFD Participation tool"),                                                           title: mw.config.get("wgPageName"),                                                            nocreate: 1,                                                            token: token                                                        }).done(function (result) {                                                            if (result.error) {                                                                mw.notify($(' ' + result.error.info + ' '));                                                                $("#RfD-vote-speedy").prop("disabled", false);                                                                $("#RfD-vote-reason").prop("disabled", false);                                                                $(".ui-button").prop("disabled", false).removeClass("ui-state-disabled"); $("#RfD-spinner").remove; } else { window.location.reload; }                                                       }).fail(function  { $("#RfD-vote-reason").prop("disabled", false); $("#RfD-vote-speedy").prop("disabled", false); $(".ui-button").prop("disabled", false).removeClass("ui-state-disabled"); $("#RfD-spinner").remove; mw.notify($("Try that again. Could not cast your vote.")); });                                                   }).fail(function (result) {                                                        $("#RfD-vote-reason").prop("disabled", false);                                                        $("#RfD-vote-speedy").prop("disabled", false);                                                        $(".ui-button").prop("disabled", false).removeClass("ui-state-disabled");                                                        $("#RfD-spinner").remove;                                                        mw.notify($("Try that again. Could not cast your vote."));                                                    }); }                                           }                                        }).fail(function  { $("#RfD-vote-reason").prop("disabled", false); $("#RfD-vote-speedy").prop("disabled", false); $(".ui-button").prop("disabled", false).removeClass("ui-state-disabled"); $("#RfD-spinner").remove; mw.notify($("Try that again. Could not cast your vote.")); });                                   }                                }).fail(function  {                                    $("#RfD-vote-reason").prop("disabled", false);                                    $("#RfD-vote-speedy").prop("disabled", false);                                    $(".ui-button").prop("disabled", false).removeClass("ui-state-disabled");                                    $("#RfD-spinner").remove;                                    mw.notify($("Try that again. Could not cast your vote."));                                }); }                       }                    },                    {                        text: "Cancel", click: function { $(this).dialog("destroy").remove; $("#RfD-spinner").remove; }                   }                ],                closeOnEscape: false, modal: true, title: (vote.toLowerCase == "comment" ? "Comment on the nomination of " : "Vote to " + vote.toLowerCase) + " \"" + target + "\"", width: (0.50 * window.outerWidth > 300) ? 0.50 * window.outerWidth : 300 });           preview;            function preview {                if ($("#RfD-dialog").length > 0) {                    var comment = $("#RfD-vote-reason").val;                    var speedy = $("#RfD-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: target, prop: "text", pst: "1", preview: "1", text: "\n* " + temp + "" + (comment ? ": " + comment : "") + " ~" + ((vote.toLowerCase != "comment") ? " " : "") }).done(function (result) { if (result.error) { $("#RfD-vote-preview").html("Could not load preview."); mw.notify($("" + result.error.info + " ")); } else { $("#RfD-vote-preview").html("Preview: " + result.parse.text["*"]); }                   }).fail(function  { $("#RfD-vote-preview").html("Could not load preview."); }).always(function { window.setTimeout(preview, 100); });               }            }            $("#RfD-vote-reason").keypress(function (e) { if (e.which == 13) { e.preventDefault; }           });        };        $("h4").find(".mw-headline").each(function  { var target = $(this).text; console.log(target); RfD.$keep = $('Vote to keep'); RfD.$keep.click(function (e) {               e.preventDefault;                RfD.vote("Keep", target);            }); RfD.$delete = $('Vote to delete'); RfD.$delete.click(function (e) {               e.preventDefault;                RfD.vote("Delete", target);            }); RfD.$disambiguate = $('Vote to disambiguate'); RfD.$disambiguate.click(function (e) {               e.preventDefault;                RfD.vote("Disambiguate", target);            }); RfD.$retarget = $('Vote to retarget</a>'); RfD.$retarget.click(function (e) {               e.preventDefault;                var newtarget;                $(' ').append(' What should the title(s) be retargeted to? <input list="RfD-search-results" id="RfD-search-box"><datalist id="RfD-search-results"> ').dialog({ buttons: [ {                           text: "Next", click: function { newtarget = $("#RfD-search-box").val; $.get(mw.config.get("wgScriptPath") + "/api.php", {                                   action: "parse",                                    format: "json",                                    prop: "text",                                    page: newtarget                                }).done(function (result) {                                    if (result.error) {                                        if (result.error.code == "invalidtitle" && newtarget == "") {                                            RfD.vote("Retarget", target);                                        } else {                                            alert(result.error.info);                                        }                                    } else {                                        RfD.vote("Retarget to " + (newtarget ? "" + newtarget + "" : "some other page"), target); }                               });                            }                        },                        {                            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                }); $("#RfD-search-box").keyup(function {                    $.get(mw.config.get("wgScriptPath") + "/api.php", { action: "query", list: "search", srsearch: $("#RfD-search-box").val, format: "json" }).done(function (result) { if (result.error) { } else { $("#RfD-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; }                               $("#RfD-search-results").append('<option value="' + result.query.search[i].title + '">'); }                       }                    }).fail(function (result) { })               });            });            RfD.$comment = $('<a class="RfD-vote-comment" style="margin:3px;" href="javascript:void(0)">Leave a comment</a>');            RfD.$comment.click(function (e) { e.preventDefault; RfD.vote("Comment", target); });           RfD.$custom = $('<a class="RfD-vote-custom" style="margin:3px;" href="javascript:void(0)">Custom vote</a>');            RfD.$custom.click(function (e) { e.preventDefault; RfD.vote(prompt("Enter your custom vote here:"), target); });           $(this).parent.find(".mw-editsection").append(' [ ').append(RfD.$keep).append(' | ').append(RfD.$delete).append(' | ').append(RfD.$retarget).append(' | ').append(RfD.$disambiguate).append(' | ').append(RfD.$comment).append(' | ').append(RfD.$custom).append(' ] ');            if (mw.config.get("skin") != "timeless") {                $(".mw-editsection-divider").css("display", "inline");            }        }) }); } // End of RfD code