// <syntaxhighlight lang="JavaScript">
/* <nowiki>
 * This is a script that helps automate AfD closing by adding closing tabs to AfD debates in edit mode.
 * 
 * Written by ] based on ] version of
 *  ] by ]
 */

function autoafd_disable_wikEd(){
    if (typeof(window.wikEd) != 'undefined') {
        if (window.wikEd.useWikEd == true) {
            if (wikEd.turnedOn === false) {
                wikEd.useWikEd = false;
                window.wikEdUseWikEd = wikEd.useWikEd;
                wikEd.disabled = true;
                wikEd.SetLogo('incompatible', 'Wikipedia:WikiProject User scripts/Scripts/CloseAFD');
            }
            else {
 
                // interrupt fullscreen mode
                if (wikEd.fullscreen === true) {
                    wikEd.FullScreen(false);
                }
 
                // turn classic textarea on
                if (wikEd.useWikEd === true) {
                    wikEd.UpdateTextarea();
                }
                wikEd.SetEditArea(false);
 
                // reset textarea dimensions
                wikEd.textarea.style.height = (wikEd.textareaOffsetHeightInitial - wikEd.frameBorderHeight) + 'px';
                wikEd.textarea.style.width = '100%';
 
                wikEd.frameHeight = (wikEd.textareaOffsetHeightInitial - wikEd.frameBorderHeight) + 'px';
                wikEd.frameWidth = (wikEd.editorWrapper.clientWidth - wikEd.frameBorderWidth) + 'px';
                wikEd.frame.style.height = wikEd.frameHeight;
                wikEd.frame.style.width = wikEd.frameWidth;
 
                wikEd.buttonBarWrapper.style.display = 'none';
                wikEd.buttonBarPreview.style.display = 'none';
 
                wikEd.previewArticle.style.display = 'none';
                wikEd.previewDiff.style.display = 'none';
                wikEd.localPrevWrapper.style.display = 'none';
                wikEd.localPrevWrapper.style.height = 'auto';
 
                if (wikEd.buttonBarJump !== null) {
                    wikEd.buttonBarJump.style.display = 'none';
                }
 
                wikEd.useWikEd = false;
                window.wikEdUseWikEd = wikEd.useWikEd;
                wikEd.disabled = true;
                wikEd.SetLogo('incompatible', 'CloseAFD');
 
                // run scheduled custom functions
                wikEd.ExecuteHook(wikEd.config.offHook);
            }
        }
    }
}

function autoafd_result() {
    autoafd_disable_wikEd();
    var closeR = prompt("Result of debate?");
    if (closeR != null && closeR != "") {
        var f = document.editform, t = f.wpTextbox1;
        t.value = t.value.split('{{REMOVE THIS TEMPLATE WHEN CLOSING THIS AfD').join('{{ns:0');
        var temp = "{{SUBST:At}} '''" + closeR + "'''.";
        var closeC = prompt("Optional comment:");
        if (closeC != null && closeC != ""){ temp += " " + closeC; }
        if ($.inArray("sysop", mw.config.get( 'wgUserGroups' )) === -1){ temp += " {{SUBST:Nac}}"; }
        temp += ' ~~~~\n\n';
        if($.inArray("Relisted AfD debates", mw.config.get( 'wgCategories' )) >= 0){
            if($.inArray("AfD debates relisted 3 or more times", mw.config.get( 'wgCategories' )) >= 0){ temp += t.value.replace(/\\]/gi, "").replace(/\\]/gi, ""); }
            else { temp += t.value.replace(/\\]/gi, ""); }
        } else {  temp += t.value; }
        if (t.value.length > 0) { temp += '\n'; }
        t.value = temp + "{{SUBST:Ab}}";
        f.wpSummary.value = "Closing debate; result was " + closeR;
        if ($.inArray("sysop", mw.config.get( 'wgUserGroups' )) === -1){ f.wpSummary.value += " (])"; }
        $('#wpPreview').click();
    } else {
        if(confirm("You forgot to type in a result!\n\n\tClick 'OK' to try again or 'Cancel' to quit.\n")){
            autoafd_result();
        }
    }
}
 
function autoafd_relist() {
    autoafd_disable_wikEd();
    var relistCount = 0;
    if($.inArray("Relisted AfD debates", mw.config.get( 'wgCategories' )) >= 0){
        relistCount = $('.xfd_relist').length;
        relistCountL = $('#wpTextbox1').html().match(/(&lt;!-- from Template:Relist --&gt;)/gi).length;//backwards compatibility
        if(relistCount < relistCountL){relistCount = relistCountL;} 
    }
    var f = document.editform, t = f.wpTextbox1;
    if (t.value.length > 0) { t.value += '\n'; }
    t.value += "{{SUBST:Relist"
    if(relistCount >= 2){
        t.value += "|1=";
        var relistReason = prompt("This discussion has already been relisted " + relistCount + " times.\n\nPlease give a reason for relisting again per:\n\thttps://en.wikipedia.orghttps://wiki95.com/en/Wikipedia:Deletion_process#Relisting_discussions\n");
        if(relistReason == null || relistReason === ""){ relistReason = "No ] given for relist #" + (relistCount+1) + "."; }
        t.value += relistReason;
    }
    t.value += "|3=" + (relistCount+1) + "}}";
    t.scrollTop = t.scrollHeight;location.href = "#editform";
    f.wpSummary.value = "Relist #" + (relistCount+1) + " for this debate.";
    if(relistCount >= 2){ f.wpSummary.value += " (" + relistReason + ")"; }
}
 
function autoafd_keep() {
    autoafd_disable_wikEd();
    var date = prompt("Nomination was made when?");
    if (date != null && date != "") {
        var votepage = prompt("Vote page is? (Enter 'd' for default.)", "d");
        if (votepage != null && votepage != "") {
            var f = document.editform, t = f.wpTextbox1;
            // If default votepage...
            if (votepage=="d") {
                var temp = document.editform.action.split("/w/index.php?title=");
                temp = temp.split("&action=submit");
                temp = temp.substring(5);
                votepage = temp;
            }
            t.value = "{{Oldafdfull|date=" + date + "|votepage=" + votepage + '}}\n\n' + t.value;
            f.wpSummary.value = "Article survived AfD";
            if ($.inArray("sysop", mw.config.get( 'wgUserGroups' )) === -1){ f.wpSummary.value += " (])"; }
            $('#wpPreview').click();
        }
    }
}
 
function autoafd_no_consensus() {
    autoafd_disable_wikEd();
    var date = prompt("Nomination was made when?");
    if (date != null && date != "") {
        var votepage = prompt("Vote page is? (Enter 'd' for default.)", "d")
        if (votepage != null && votepage != "") {
            var f = document.editform, t = f.wpTextbox1;
            // If default votepage...
            if (votepage=="d") {
                var temp = document.editform.action.split("/w/index.php?title=");
                temp = temp.split("&action=submit");
                temp = temp.substring(5);
                votepage = temp;
            }
            t.value = "{{Oldafdfull|date=" + date + "|result='''no consensus'''|votepage=" + votepage + '}}\n\n' + t.value;
            f.wpSummary.value = "Article survived AfD with no consensus";
            if ($.inArray("sysop", mw.config.get( 'wgUserGroups' )) === -1){ f.wpSummary.value += " (])"; }
            $('#wpPreview').click();
        }
    }
}
 
function autoafd_redirect() {
    autoafd_disable_wikEd();
    var date = prompt("Nomination was made when?");
    if (date != null && date != "") {
        var redirect = prompt("Redirect to?");
        if (redirect != null && redirect != "") {
            var votepage = prompt("Vote page is? (Enter 'd' for default.)", "d");
            if (votepage != null && votepage != "") {
                var f = document.editform, t = f.wpTextbox1;
                // If default votepage...
                if (votepage=="d") {
                    var temp = document.editform.action.split("/w/index.php?title=");
                    temp = temp.split("&action=submit");
                    temp = temp.substring(5);
                    votepage = temp;
                }
                t.value = "{{Oldafdfull|date=" + date + "|result='''redirect to ]'''|votepage=" + votepage + '}}\n\n' + t.value;
                f.wpSummary.value = "Article redirected to ] as per AfD";
                if ($.inArray("sysop", mw.config.get( 'wgUserGroups' )) === -1){ f.wpSummary.value += " (])"; }
                $('#wpPreview').click();
            }
        }
    }
}
 
function autoafd_merge() {
    autoafd_disable_wikEd();
    var date = prompt("Nomination was made when?");
    if (date != null && date != "") {
        var redirect = prompt("Merge and redirect to?");
        if (redirect != null && redirect != "") {
            var votepage = prompt("Vote page is? (Enter 'd' for default.)", "d");
            if (redirect != null && redirect != "") {
                var f = document.editform, t = f.wpTextbox1;
                // If default votepage...
                if (votepage=="d") {
                    var temp = document.editform.action.split("/w/index.php?title=");
                    temp = temp.split("&action=submit");
                    temp = temp.substring(5);
                    votepage = temp;
                }
                t.value = "{{Oldafdfull|date=" + date + "|result='''merge and redirect to ]'''|votepage=" + votepage + '}}\n\n' + t.value;
                f.wpSummary.value = "Article merged and redirected to ] as per AfD";
                if ($.inArray("sysop", mw.config.get( 'wgUserGroups' )) === -1){ f.wpSummary.value += " (])"; }
                $('#wpPreview').click();
            }
        }
    }
}
 
function autoafd_other() {
    autoafd_disable_wikEd();
    var date = prompt("Nomination was made when?");
    if (date != null && date != "") {
        var result = prompt("Result was?");
        if (result != null && result != "") {
            var votepage = prompt("Vote page is? (Enter 'd' for default.)", "d");
            if (votepage != null && votepage != "") {
                var f = document.editform, t = f.wpTextbox1;
                // If default votepage...
                if (votepage=="d") {
                    var temp = document.editform.action.split("/w/index.php?title=");
                    temp = temp.split("&action=submit");
                    temp = temp.substring(5);
                    votepage = temp;
                }
                t.value = "{{Oldafdfull|date=" + date + "|result='''" + result + "'''|votepage=" + votepage + '}}\n\n' + t.value;
                f.wpSummary.value = "AfDed; result was " + result;
                if ($.inArray("sysop", mw.config.get( 'wgUserGroups' )) === -1){ f.wpSummary.value += " (])"; }
                $('#wpPreview').click();
            }
        }
    }
}
 
function autoafd_add_portlet(func, display) {
    var portletLink = mw.util.addPortletLink('p-cactions', '#', display);
    $(portletLink).click(function (e) {
        e.preventDefault();
        func();
    });
}

function autoafd_add_afd_tabs() {
    // Only add for pages with the right string somewhere in the title
    if (document.title.indexOf("Editing Wikipedia:Articles for deletion/") != -1) {
        autoafd_add_portlet(autoafd_result, "close");
        autoafd_add_portlet(autoafd_relist, "relist");
    }
    if (document.title.indexOf("Editing Talk:") != -1 || document.title.indexOf("Creating Talk:") != -1) {
        autoafd_add_portlet(autoafd_keep, "keep");
        autoafd_add_portlet(autoafd_no_consensus, "no consensus");
        autoafd_add_portlet(autoafd_redirect, "redirect");
        autoafd_add_portlet(autoafd_merge, "merge");
        autoafd_add_portlet(autoafd_other, "other");
    }
}
 
$(autoafd_add_afd_tabs);
 
//end AfD closing script
//</nowiki> </syntaxhighlight>