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

Source: Wikipedia, the free encyclopedia.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// FfD code follows <nowiki>
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) {
        $('<div id="FfD-dialog"></div>').append('<p>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 <a href="/wiki/Wikipedia:Polling_is_not_a_substitute_for_discussion">this page</a> and <a href="/wiki/Wikipedia:Arguments_to_avoid_in_deletion_discussions">this page</a> for more information.</p><form id="FfD-vote"><div><textarea id="FfD-vote-reason" placeholder="' + ((vote.toLowerCase() == "comment") ? 'Comment' : 'Reason to ' + vote.toLowerCase()) + '" height="5"></textarea></div>' + (vote.toLowerCase() == "delete" || vote.toLowerCase() == "keep" ? '<div>' : '<div style="display:none;">') + '<input type="checkbox" id="FfD-vote-speedy"><label for="FfD-vote-speedy">Speedy</label>' + "</div></form>" + '<div id="FfD-vote-preview"></div>').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('<div class="mw-spinner mw-spinner-small mw-spinner-inline" title="..." id="FfD-spinner"><div class="mw-spinner-container"><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div></div></div>');
                            $(".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($('<span class="error">' + result.error.info + '</span>'));
                                    $(".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($('<span class="error">' + result.error.info + '</span>'));
                                            $(".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("<!--VCB " + mw.config.get("wgUserName") + " " + $(FfD.$obj).find("b a").eq(1).attr("href").split("#")[1].split("_").join(" ") + "-->")) && vote.toLowerCase() != "comment") {
                                                $(".ui-button").prop("disabled", false).removeClass("ui-state-disabled");
                                                $("#FfD-dialog").dialog("destroy").remove();
                                                $('<table class="plainlinks plainlinks ombox ombox-content" role="presentation"><tbody><tr><td class="mbox-image"><img alt="" src="//upload.wikimedia.org/wikipedia/commons/thumb/3/33/OOjs_UI_icon_clear-destructive.svg/40px-OOjs_UI_icon_clear-destructive.svg.png" decoding="async" width="40" height="40" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/3/33/OOjs_UI_icon_clear-destructive.svg/60px-OOjs_UI_icon_clear-destructive.svg.png 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/3/33/OOjs_UI_icon_clear-destructive.svg/80px-OOjs_UI_icon_clear-destructive.svg.png 2x" data-file-width="20" data-file-height="20"></td><td class="mbox-text"><b>You\'ve already voted.</b>  Please modify your existing vote on the FfD page rather than casting a new vote.  If you have struck out your vote, please make sure to remove the "&lt;!--Vote cast using XFD voting tool by ' + mw.config.get("wgUserName") + ' -->" or "&lt;"!--VCB ' + mw.config.get("wgUserName") + '-->"code or similar before trying again.</p></td></tr></tbody></table>').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") ? " <!--VCB " + mw.config.get("wgUserName") + " " + $(FfD.$obj).find("b a").eq(1).attr("href").split("#")[1].split("_").join(" ") + "-->" : ""),
                                                        summary: "/* " + $(FfD.$obj).find("b a").eq(1).attr("href").split("#")[1].split("_").join(" ") + "*/ " + ((vote.toLowerCase() == "comment") ? "Comment via [[User:Awesome Aasim/xfdvote|XFD Participation tool]]" : "!Vote via [[User:Awesome Aasim/xfdvote|XFD Participation tool]]"),
                                                        nocreate: 1,
                                                        token: token
                                                    }).done(function (result) {
                                                        if (result.error) {
                                                            mw.notify($('<span class="error">' + result.error.info + '</span>'));
                                                            $("#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($("<b>Try that again.</b><br>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($("<b>Try that again.</b><br>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($("<b>Try that again.</b><br>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($("<b>Try that again.</b><br>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("<i>Could not load preview.</i>");
                        mw.notify($("<span class=\"error\">" + result.error.info + "</span>"));
                    } else {
                        $("#FfD-vote-preview").html("Preview: <br>" + result.parse.text["*"]);
                    }
                }).fail(function () {
                    $("#FfD-vote-preview").html("<i>Could not load preview.</i>");
                }).always(function () {
                    window.setTimeout(preview, 100);
                });
            }
        }
        $("#FfD-vote-reason").keypress(function (e) {
            if (e.which == 13) {
                e.preventDefault();
            }
        });
    };
    FfD.$keep = $('<button class="mw-ui-button mw-ui-progressive" id="FfD-vote-keep">Vote to keep</button>');
    FfD.$keep.click(function (e) {
        e.preventDefault();
        FfD.vote("Keep");
    }).css({ "margin-right": "5px", "margin-left": "1px" });
    FfD.$delete = $('<button class="mw-ui-button mw-ui-destructive" id="FfD-vote-delete">Vote to delete</button>');
    FfD.$delete.click(function (e) {
        e.preventDefault();
        FfD.vote("Delete");
    }).css({ "margin-right": "5px", "margin-left": "1px" });
    FfD.$comment = $('<button class="mw-ui-button" id="FfD-vote-comment">Leave a comment</button>');
    FfD.$comment.click(function (e) {
        e.preventDefault();
        FfD.vote("Comment");
    }).css({ "margin-right": "5px", "margin-left": "1px" });
    FfD.$custom = $('<button class="mw-ui-button" id="FfD-vote-custom">Custom vote</button>');
    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('<hr>Deletion discussion actions:<div id="FfD-vote-buttons"></div>');
        } 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('<hr>Deletion discussion actions:<div id="FfD-vote-buttons"></div>');
                } else {
                    var $text = $(result.parse.text["*"]);
                    FfD.$obj.find("tr .mbox-text").append('<hr>Reason: ' + $text.find("p").eq(0).html() + '<hr>Deletion discussion actions:<div id="FfD-vote-buttons"></div>');
                }
            }).fail(function () {
                FfD.$obj.find("tr .mbox-text").append('<hr>Deletion discussion actions:<div id="FfD-vote-buttons"></div>');
            }).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('<hr>Deletion discussion actions:<div id="FfD-vote-buttons"></div>');
    }).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 </nowiki>