User:Awesome Aasim/xfdvote.js/tfd.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.
// TfD code follows <nowiki>
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) {
        $('<div id="TfD-dialog"></div>').append('<p>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 <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="TfD-vote"><div><textarea id="TfD-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="TfD-vote-speedy"><label for="TfD-vote-speedy">Speedy</label>' + "</div></form>" + '<div id="TfD-vote-preview"></div>').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('<div class="mw-spinner mw-spinner-small mw-spinner-inline" title="..." id="TfD-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");
                            $("#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($('<span class="error">' + result.error.info + '</span>'));
                                    $(".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($('<span class="error">' + result.error.info + '</span>'));
                                            $(".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("<!--VCB " + mw.config.get("wgUserName") + " " + TfD.$target.attr("href").split("#")[1].split("_").join(" ") + "-->")) && vote.toLowerCase() != "comment") {
                                                $(".ui-button").prop("disabled", false).removeClass("ui-state-disabled");
                                                $("#TfD-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 TfD 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 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") ? " <!--VCB " + mw.config.get("wgUserName") + " " + TfD.$target.attr("href").split("#")[1].split("_").join(" ") + "-->" : ""),
                                                        summary: "/* " + TfD.$target.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]]"),
                                                        title: TfD.target,
                                                        nocreate: 1,
                                                        token: token
                                                    }).done(function (result) {
                                                        if (result.error) {
                                                            mw.notify($('<span class="error">' + result.error.info + '</span>'));
                                                            $("#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($("<b>Try that again.</b><br>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($("<b>Try that again.</b><br>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($("<b>Try that again.</b><br>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($("<b>Try that again.</b><br>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("<i>Could not load preview.</i>");
                        mw.notify($("<span class=\"error\">" + result.error.info + "</span>"));
                    } else {
                        $("#TfD-vote-preview").html("Preview: <br>" + result.parse.text["*"]);
                    }
                }).fail(function () {
                    $("#TfD-vote-preview").html("<i>Could not load preview.</i>");
                }).always(function () {
                    window.setTimeout(preview, 100);
                });
            }
        }
        $("#TfD-vote-reason").keypress(function (e) {
            if (e.which == 13) {
                e.preventDefault();
            }
        });
    };
    TfD.$keep = $('<button class="mw-ui-button mw-ui-progressive" id="TfD-vote-keep">Vote to keep</button>');
    TfD.$keep.click(function (e) {
        e.preventDefault();
        TfD.vote("Keep");
    }).css({ "margin-right": "5px", "margin-left": "1px" });
    TfD.$delete = $('<button class="mw-ui-button mw-ui-destructive" id="TfD-vote-delete">Vote to delete</button>');
    TfD.$delete.click(function (e) {
        e.preventDefault();
        TfD.vote("Delete");
    }).css({ "margin-right": "5px", "margin-left": "1px" });
    TfD.$merge = $('<button class="mw-ui-button" id="TfD-vote-merge">Vote to merge</button>');
    TfD.$merge.click(function (e) {
        e.preventDefault();
        var target;
        $('<div id="TfD-search"></div>').append('<p>Please enter a proposed target to merge this page with:</p><form><input list="TfD-search-results" id="TfD-search-box"><datalist id="TfD-search-results"></datalist></form>').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('<option value="' + result.query.search[i].title + '">');
                    }
                }
            }).fail(function (result) {
            })
        });
    }).css({ "margin-right": "5px", "margin-left": "1px" });
    TfD.$comment = $('<button class="mw-ui-button" id="TfD-vote-comment">Comment</button>');
    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</button>');
    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('<hr>Deletion discussion actions:<div id="TfD-vote-buttons"></div>');
        } 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('<hr>Deletion discussion actions:<div id="TfD-vote-buttons"></div>');
                } else {
                    var $text = $(result.parse.text["*"]);
                    TfD.$obj.find("tr .mbox-text").append("<hr>Reason: " + $text.find("p").eq(0).html() + '<hr>Deletion discussion actions:<div id="TfD-vote-buttons"></div>');
                }
            }).fail(function () {
                TfD.$obj.find("tr .mbox-text").append('<hr>Deletion discussion actions:<div id="TfD-vote-buttons"></div>');
            }).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('<hr>Deletion discussion actions:<div id="TfD-vote-buttons"></div>');
    }).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 </nowiki>