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

// CfD code follows var CfD = {}; $(document).ready(function {    CfD.$obj = $(".mbox-cfd").eq(0); //only add to first CfD tag in case of duplicate tags    CfD.target = $(CfD.$obj).find('b a:contains("this category\'s entry")').eq(0).attr("title"); // detect target of CfD    CfD.vote = function (vote) {        $(' ').append(' Enter your argument below. CfD 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 = $("#CfD-vote-reason").val; var speedy = $("#CfD-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"); $("#CfD-vote-reason").prop("disabled", true); $("#CfD-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");                                    $("#CfD-spinner").remove;                                    $("#CfD-vote-reason").prop("disabled", false);                                    $("#CfD-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: CfD.target                                    }).done(function (result) {                                        if (result.error) {                                            mw.notify($(' ' + result.error.info + ' '));                                            $(".ui-button").prop("disabled", false).removeClass("ui-state-disabled");                                            $("#CfD-spinner").remove;                                            $("#CfD-vote-reason").prop("disabled", false);                                            $("#CfD-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"); $("#CfD-dialog").dialog("destroy").remove; $(' ').dialog({                                                   buttons: [{                                                        text: "Visit CfD page",                                                        click: function  {                                                            window.location.href = "/wiki/" + CfD.target + "#" + $(CfD.$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: CfD.target }).done(function (result) { var section; $(result.parse.text["*"]).find(".mw-headline").each(function (i) {                                                       if ($(this).text == $(CfD.$obj).find('b a:contains("this category\'s entry")').eq(0).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: "/* " + $(CfD.$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"),                                                       title: CfD.target,                                                        nocreate: 1,                                                        token: token                                                    }).done(function (result) {                                                        if (result.error) {                                                            mw.notify($(' ' + result.error.info + ' '));                                                            $("#CfD-vote-speedy").prop("disabled", false);                                                            $("#CfD-vote-reason").prop("disabled", false);                                                            $(".ui-button").prop("disabled", false).removeClass("ui-state-disabled"); $("#CfD-spinner").remove; } else { window.location.href = "/wiki/" + CfD.target + "#" + $(CfD.$obj).find("b a").eq(1).attr("href").split("#")[1].split("_").join(" "); }                                                   }).fail(function  { $("#CfD-vote-reason").prop("disabled", false); $("#CfD-vote-speedy").prop("disabled", false); $(".ui-button").prop("disabled", false).removeClass("ui-state-disabled"); $("#CfD-spinner").remove; mw.notify($("Try that again. Could not cast your vote.")); });                                               }).fail(function (result) {                                                    $("#CfD-vote-reason").prop("disabled", false);                                                    $("#CfD-vote-speedy").prop("disabled", false);                                                    $(".ui-button").prop("disabled", false).removeClass("ui-state-disabled");                                                    $("#CfD-spinner").remove;                                                    mw.notify($("Try that again. Could not cast your vote."));                                                }); }                                       }                                    }).fail(function  { $("#CfD-vote-reason").prop("disabled", false); $("#CfD-vote-speedy").prop("disabled", false); $(".ui-button").prop("disabled", false).removeClass("ui-state-disabled"); $("#CfD-spinner").remove; mw.notify($("Try that again. Could not cast your vote.")); });                               }                            }).fail(function  {                                $("#CfD-vote-reason").prop("disabled", false);                                $("#CfD-vote-speedy").prop("disabled", false);                                $(".ui-button").prop("disabled", false).removeClass("ui-state-disabled");                                $("#CfD-spinner").remove;                                mw.notify($("Try that again. Could not cast your vote."));                            }); }                   }                },                {                    text: "Cancel", click: function { $(this).dialog("destroy").remove; $("#CfD-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 ($("#CfD-dialog").length > 0) {                var comment = $("#CfD-vote-reason").val;                var speedy = $("#CfD-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: CfD.target, prop: "text", pst: "1", preview: "1", text: "\n* " + temp + "" + (comment ? ": " + comment : "") + " ~" }).done(function (result) { if (result.error) { $("#CfD-vote-preview").html("Could not load preview."); mw.notify($("" + result.error.info + " ")); } else { $("#CfD-vote-preview").html("Preview: " + result.parse.text["*"]); }               }).fail(function  { $("#CfD-vote-preview").html("Could not load preview."); }).always(function { window.setTimeout(preview, 100); });           }        }        $("#CfD-vote-reason").keypress(function (e) { if (e.which == 13) { e.preventDefault; }       });    };    CfD.$keep = $('Vote to keep ');    CfD.$keep.click(function (e) { e.preventDefault; CfD.vote("Keep"); }).css({ "margin-right": "5px", "margin-left": "1px" });   CfD.$delete = $('Vote to delete ');    CfD.$delete.click(function (e) { e.preventDefault; CfD.vote("Delete"); }).css({ "margin-right": "5px", "margin-left": "1px" });   CfD.$article = $('Vote to convert to article ');    CfD.$article.click(function (e) { e.preventDefault; var target; $(' ').append(' Please enter a proposed article name:  ').dialog({           buttons: [                {                    text: "Next",                    click: function  {                        target = $("#CfD-search-box").val;                        $.get(mw.config.get("wgScriptPath") + "/api.php", { action: "parse", format: "json", prop: "text", page: target }).done(function (result) { if (result.error) { CfD.vote("Convert to article" + (target ? " " + target + "" : "")); } else { alert("That article already exists!"); }                       });                    }                },                {                    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        }); }).css({ "margin-right": "5px", "margin-left": "1px" });   CfD.$rename = $('Vote to rename ');    CfD.$rename.click(function (e) { e.preventDefault; var target; $(' ').append(' Please enter a proposed category name:  ').dialog({           buttons: [                {                    text: "Next",                    click: function  {                        target = $("#CfD-search-box").val;                        $.get(mw.config.get("wgScriptPath") + "/api.php", { action: "parse", format: "json", prop: "text", page: target }).done(function (result) { if (result.error) { CfD.vote("Rename" + (target ? " to category Category:" + target + "" : "")); } else { alert("That category already exists!"); }                       });                    }                },                {                    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        }); }).css({ "margin-right": "5px", "margin-left": "1px" });   CfD.$list = $('Vote to listify ');    CfD.$list.click(function (e) { e.preventDefault; CfD.vote("Listify"); }).css({ "margin-right": "5px", "margin-left": "1px" });   CfD.$merge = $('<button class="mw-ui-button" id="CfD-vote-merge">Vote to merge ');    CfD.$merge.click(function (e) { e.preventDefault; var target; $(' ').append(' Please enter a proposed target to merge this page with: <input list="CfD-search-results" id="CfD-search-box"><datalist id="CfD-search-results"> ').dialog({            buttons: [                {                    text: "Next",                    click: function  {                        target = $("#CfD-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 { CfD.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        }); $("#CfD-search-box").keyup(function {            $.get(mw.config.get("wgScriptPath") + "/api.php", { action: "query", list: "search", srsearch: $("#CfD-search-box").val, format: "json" }).done(function (result) { if (result.error) { } else { $("#CfD-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; }                       $("#CfD-search-results").append('<option value="' + result.query.search[i].title + '">'); }               }            }).fail(function (result) { })       });    }).css({ "margin-right": "5px", "margin-left": "1px" });    CfD.$split = $('<button class="mw-ui-button" id="CfD-vote-split">Vote to split ');    CfD.$split.click(function (e) { e.preventDefault; var i = 3; $(' ').append(' Please enter two proposed split titles: <input id="CfD-split1" placeholder="First title"> <input id="CfD-split2" placeholder="Second title"> ').dialog({           buttons: [                {                    text: "Next",                    click: function  {                        var targetstring = "";                        for (var j = 1; j < i; j++) {                            if ($("#CfD-split" + j).val != "") {                                if (j != 1) {                                    targetstring += ", "                                }                                targetstring += "Category:" + $("";                            }                        }                        CfD.vote("Split" + (targetstring ? " into " + targetstring : ""));                    }                },                {                    text: "Add another category", click: function { $("#CfD-additional").append('<span id="CfD-split-field' + i + '"><input placeholder="title..." id="CfD-split' + i + '"> '); i++; }               },                {                    text: "Remove last category", click: function { try { i--; $("#CfD-split-field" + i).remove; if (i < 3) { i = 3; }                       } catch (error) { console.error(error); i++; }                   }                },                {                    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 });   }).css({ "margin-right": "5px", "margin-left": "1px" }); CfD.$comment = $('<button class="mw-ui-button" id="CfD-vote-comment">Comment '); CfD.$comment.click(function (e) {       e.preventDefault;        CfD.vote("Comment");    }).css({ "margin-right": "5px", "margin-left": "1px" }); CfD.$custom = $('<button class="mw-ui-button" id="CfD-vote-custom">Custom vote '); CfD.$custom.click(function (e) {       e.preventDefault;        CfD.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: CfD.target,        prop: "text"    }).done(function (result) {        if (result.error) {            CfD.$obj.find("tr .mbox-text").append(' Deletion discussion actions: ');        } else {            var section;            $(result.parse.text["*"]).find(".mw-headline").each(function (i) { if ($(this).text == $(CfD.$obj).find('b a:contains("this category\'s entry")').eq(0).attr("href").split("#")[1].split("_").join(" ")) { section = i + 1; }           });            $.get(mw.config.get("wgScriptPath") + "/api.php", { action: "parse", format: "json", page: CfD.target, prop: "text", section: section }).done(function (result) { if (result.error) { CfD.$obj.find("tr .mbox-text").append(' Deletion discussion actions: '); } else { var $text = $(result.parse.text["*"]); CfD.$obj.find("tr .mbox-text").append(" " + $text.find("dd").eq(1).html + ' Deletion discussion actions: '); }           }).fail(function  { CfD.$obj.find("tr .mbox-text").append(' Deletion discussion actions: '); }).always(function { $("#CfD-vote-buttons").append(CfD.$keep).append(CfD.$delete).append(CfD.$article).append(CfD.$rename).append(CfD.$list).append(CfD.$merge).append(CfD.$split).append(CfD.$comment).append(CfD.$custom); if (mw.config.get("wgNamespaceNumber") != 14) { $("#CfD-vote-buttons").html("If this was a real CfD nomination, the buttons to vote on the nomination would appear here."); }           });        }    }).fail(function  {        CfD.$obj.find("tr .mbox-text").append(' Deletion discussion actions: ');    }).always(function  {        $("#CfD-vote-buttons").append(CfD.$keep).append(CfD.$delete).append(CfD.$article).append(CfD.$rename).append(CfD.$list).append(CfD.$merge).append(CfD.$split).append(CfD.$comment).append(CfD.$custom);        if (mw.config.get("wgNamespaceNumber") != 14) {            $("#CfD-vote-buttons").html("If this was a real CfD nomination, the buttons to vote on the nomination would appear here.");        }    }); }); // End of CfD code