User:Quarl/autoafd.js

From Wikipedia, the free encyclopedia

Note: After saving, you have to bypass your browser's cache to see the changes. In Internet Explorer and Firefox, hold down the Ctrl key and click the Refresh or Reload button. Opera users have to clear their caches through Tools→Preferences, see the instructions for Opera. Konqueror and Safari users can just click the Reload button.

// User:Quarl/autoafd.js
 
// requires: wikipage.js, wikipageAfd.js, wikiedit.js, addilink.js, util.js
// enhanced by: advanced_sig.js
 
// quarl 2006-01-23 new version that opens the nomination page on invocation; adds 
//                  checkboxes for tag & log (default on); do the tagging and logging
//                  asynchronously on 'submit'.
 
// TODO: don't lose the extra stuff on preview/diff
 
// TODO: add buttons for manually tagging/listing
 
// <pre><nowiki>
 
if(typeof window.makeSignature=='undefined')makeSignature=function(){return "~~~~"};
 
function autoafd_makeBeginUrl() {
    var wp = wikiPage.afdPageX();
    return wp.qurl + '&action=edit&fakeaction=autoafd';
}
 
function autoafd_load()
{
    if (queryVars['fakeaction'] == 'autoafd') {
        autoafd_begin();
        return;
    }
 
    if (!wikiPage.nsMainP) {
        // don't bother with non-main-namespace articles
        return;
    }
 
    if (document.getElementById('afd')) {
        // this article already has an AFD tag.
        return;
    }
 
    if (wikiPage.xfdType() == 'afd') 
        addTab(autoafd_makeBeginUrl(), 'Afd', 'ca-autoafd', 'Nominate for deletion');
}
 
function autoafd_begin() {
    if (window.afdEditor) return; // already initialized
 
    // TODO: some of this is redundant with wikipageAfd.js
    window.wpAfd = wikiPage;
    window.wpAfdTarget = wpAfd.afdTargetPage();
    if (!wpAfdTarget) {
        alert("Doesn't look like an AFD page?!");
        return;
    }
    window.wpAfdLog = afdLogPage();
 
    var editor = window.afdEditor = wpAfd.getEditor();
    if (editor.wpTextbox1) {
        // TODO: automatically go to #2 etc.
        alert("There's an old AFD at the default location already.\n\n" +
              'Please manually edit [[' + wikiPage.page + ' (2)]]).');
        return;
    }
 
    editor.wpTextbox1 = '===[[' + wpAfdTarget.page + ']]===\n' +
              "Nomination.  '''Delete''' " + makeSignature() + "\n*\n*\n*\n";
    editor.wpSummary = "Initial nomination for deletion of [[" + wpAfdTarget.page + "]]";
    editor.updateForm();
 
    autoafd_annotate();
}
 
function autoafd_annotate() {
    autoafd_add_new_checkboxes();
    hookEventObj(document.editform.wpSave, 'click', autoafd_submitHook_Event);
    window.autoafd_async_wait = null;
    // Don't submit! Let the user type in peace.
}
 
function autoafd_add_new_checkboxes() {
    if (document.getElementById('autoafd-options')) return;
 
    var tagTitle = 'Add {{subst:afd}} to ' + string_quote_escape(wpAfdTarget.page)
    var tagText = 'Tag <a href="' + url_getpath(wpAfdTarget.url) + '">' +
                  wpAfdTarget.page + '</a> with {{afd}}';
 
    var listTitle = 'List this AFD on today\'s AFD log page';
    var listText = 'List in <a href="'+url_getpath(wpAfdLog.url) + '">' +
                   //wpAfdLog.page + 
                   'log page' +
                   '</a>';
 
    var newstuff = document.createElement('div');
    window.afd_newstuff = newstuff;
    newstuff.id = 'autoafd-options';
    newstuff.innerHTML = '<input type="checkbox" value="1" name="wpTagAFD" id="wpTagAFD" checked="1" />'+
                         '<label for="wpTagAFD" title="' + tagTitle + '">' + 
                         tagText + '</label>' + 
                         '&nbsp;&nbsp;&nbsp;' +
                         '<input type="checkbox" value="1" name="wpListAFD" id="wpListAFD"  checked="1" />'+
                         '<label for="wpListAFD" title="' + listTitle + '">' + 
                         listText + '</label>';
    add_before(document.editform.wpMinoredit, newstuff);
}
 
 
function autoafd_submitHook_Event(event)
{
    if (!(autoafd_submitHook())) {
        event.preventDefault();
        event.stopPropagation();
    }
}
 
function autoafd_submitHook() {
    window.afdEditor.updateThis();
 
    // autoafd_async_wait is initially set to null.  The first time we arrive here 
    // we set it to 0.  We wait for up to two asynchronous submissions to occur first
    // before actually submitting the form.
 
    if (autoafd_async_wait == 0) {
        return true;
    }
    if (autoafd_async_wait == null) {
        autoafd_async_wait = 0;
    }
 
    if (document.editform.wpTagAFD.checked && !window.asyncTagAFDStarted) {
        window.asyncTagAFDStarted = true;
        autoafd_async_wait++;
        var status = window.autoafd_status_tag = document.createElement('div');
        status.innerHTML = 'Tagging page: Downloading...';
        status.id = 'status-tagafd';
        add_after(afd_newstuff, status);
        wpAfdTarget.getEditorAsync(autoafd_TagAFD);
    }
    if (document.editform.wpListAFD.checked && !window.asyncListAFDStarted) {
        window.asyncListAFDStarted = true;
        autoafd_async_wait++;
        var status = window.autoafd_status_list = document.createElement('div');
        status.innerHTML = 'Listing: Downloading...';
        status.id = 'status-listafd';
        add_after(afd_newstuff, status);
        wpAfdLog.getEditorAsync(autoafd_ListAFD);
    }
 
    if (autoafd_async_wait == 0) return true;
 
    // prevent repeat clicks
    document.editform.wpSave.disabled = true;
    return false;
}
 
function autoafd_TagAFD(editor) {
    if (editor.refuseCreate()) return;
 
    if (editor.wpTextbox1.match(/{{afd/) ||
        editor.wpTextbox1.match(/\[\[Category:Pages for deletion\]\]/))
    {
         autoafd_status_tag.innerHTML += ' Already tagged!';
    } else {
        autoafd_status_tag.innerHTML += ' submitting...';
        editor.wpTextbox1 = '{{subst:afd}}\n\n' + trim_lines(editor.wpTextbox1);
        editor.wpSummary = 'Nominating [['+wpAfdTarget.page+']] for [['+wpAfd.page+'|AFD]]';
        editor.submitAsync(null, autoafd_TagAFD_done);
    }
}
 
function autoafd_ListAFD(editor) {
    if (editor.refuseCreate()) return; // safety check
    var newText = '{{'+wpAfd.page+'}}';
    if (editor.wpTextbox1.indexOf(newText)!=-1) {
        autoafd_status_list.innerHTML += ' already listed!';
    } else {
        autoafd_status_list.innerHTML += ' submitting...';
        editor.wpTextbox1 = trim_lines(editor.wpTextbox1) + '\n' + newText + '\n';
        editor.wpSummary = 'Listing [['+wpAfdTarget.page+']] for [['+wpAfd.page+'|AFD]]';
        editor.submitAsync(null, autoafd_ListAFD_done);
    }
}
 
function autoafd_TagAFD_done(req) {
    if (req.status != 200) {
        alert("Error tagging article for AFD!"); return;
    }
    autoafd_status_tag.innerHTML += ' done!';
    autoafd_async_done_1();
}
 
function autoafd_ListAFD_done(req) {
    if (req.status != 200) {
        alert("Error listing article for AFD!"); return;
    }
    autoafd_status_list.innerHTML += ' done!';
    autoafd_async_done_1();
}
 
function autoafd_async_done_1() {
    if (--autoafd_async_wait == 0) {
        document.editform.wpSave.disabled = false;
        //document.editform.wpSave.click();
        document.editform.submit();
    }
}
 
addOnloadHook(autoafd_load);
 
// </nowiki></pre>