Jump to content

User:Quarl/wikiedit.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Quarl (talk | contribs) at 16:29, 23 January 2006 (asyncWikiEditor - fix when not cur page). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
// User:Quarl/wikiedit.js - functions for automatically editing pages

// requires: util.js, wikipage.js

// synposis:  
//     function beginEdit() {
//         asyncWikiEditor(wikiPage, myPage_edit, data1, data2);
//     }
//     function myPage_edit(editor, data1, data2) {
//         // this == editor
//         this.wpTextbox1 += data1;
//         editor.wpSummary += data2;
//         editor.submit();
//

// WikiEditor is a class that facilitates editing and submitting Wikipedia edit forms.
//
// use asyncWikiEditor() to use the current edit form if available, else download one.
//     - inside the callback, "this" is equivalent to "editor"
//
// available properties:
//     wpTextbox1
//     wpSummary
//     wpMinoredit
//     wpWatchthis
//
// available functions:
//     submitDirect: submit form directly via document.form.submit
//     submitHidden: create a new hidden form, attach to document, and submit
//     submit: submitDirect if possible, else submitHidden
//     submitAsync: asynchronously submit a form via XMLHTTPRequest, and callback result
//     updateForm: update what the user sees and prepare for submitting
//     refuseCreate: if wpTextbox1 is empty, alert and return 'True'
//

// quarl 2006-01-23 initial version

// <pre><nowiki>

function getEditForm(doc) {
    if (!doc) doc = document;
    // Note: can't use "doc.editform", because 'doc' might actually be an XMLDocument (not HTMLDocument), if this is the result of an XMLHTTPRequest.
    return doc.getElementById('editform');
}

wpFormFields = [ 'wpSection', 'wpStarttime', 'wpEdittime', 'wpScrolltop', 
                 'wpTextbox1', 'wpSummary', 'wpMinoredit', 'wpWatchthis',
                 'wpEditToken' ];
wpButtons = [ 'wpSave', 'wpPreview', 'wpDiff' ];

function updateFields(target, source, fields) {
    var targetFormP = Boolean(target.nodeName);
    var sourceFormP = Boolean(source.nodeName);
    for (var i in fields) {
        var f = fields[i];
        var v;
        if (sourceFormP && source[f]) {
            if (source[f].type == "checkbox") {
                v = source[f].checked;
            } else {
                v = source[f].value;
            }
        } else {
            v = source[f];
        }
        if (targetFormP) {
            if (target[f].type == "checkbox") {
                target[f].checked = v;
            } else {
                target[f].value = v;
            }
        } else { 
            target[f] = v;
        }
    }
}

function WikiEditor(form) {
    if (!(this instanceof WikiEditor)) return new WikiEditor(form);
    window.wikiEditor = this;

    this.form = form || getEditForm();
    if (!this.form) { alert("WikiEditor error: no form!"); return; }

    updateFields(this, this.form, wpFormFields);

    this.refuseCreate = function() {
        if (!this.wpTextbox1) {
            alert("Error!  Page is empty; refusing to create.");
            return true;
        } else {
            return false;
        }
    }

    this.getFormParams = function(button) {
        if (!button) button = 'wpSave';
        d = {};
        updateFields(d, this, wpFormFields);
        d[button] = this.form[button];
        return d;
    }

    this.updateForm = function() {
        updateFields(this.form, this, wpFormFields);
    }

    this.submitDirect = function(button) {
        this.updateForm();
        // TODO: how to select the right button?  focus?
        this.form.submit();
    }

    this.submitHidden = function(button) {
        var newform = document.createElement('form');
        addFormHiddenParams(newform, this.getFormParams(button));
        newform.name = this.form.name;
        newform.method = this.form.method;
        newform.id = this.form.id;
        newform.action = this.form.action;
        document.getElementById('bodyContent').appendChild(newform);
        newform.submit();
    }

    this.submitAsync = function(button, callback) {
        var cb;
        if (callback) {
            cb = function(req) { callback.call(this, req); };
        } else {
            cb = function(req) { /* dummy */ };
        }
        asyncPostXML(this.form.action, this.getFormParams(button), cb);
    }

    // If this form is the current document's form, we can submit directly.
    // Else we must use the hidden submit method.
    if (this.form == document.editform) {
        this.submit = this.submitDirect;
    } else {
        this.submit = this.submitHidden;
    }
}

// If already editing the target page, return a WikiEditor now.
// Else, download the edit form first.
// Call-back with new WikiEditor instance.
function asyncWikiEditor(wp, callback) {
    var origCallback = callback; // save before it gets clobbered below
    var args = arguments; // copy reference to arguments because we need it in 'cb' below
    if (wp == wikiPage && document.editform) {
        var editor = WikiEditor();
        args[0] = editor;
        origCallback.apply(editor, args);
        return;
    } else {
        var cb = function(req) {
window.d = req;
            if (req.status != 200) {
                alert("asyncWikiEditor: Error downloading edit page!");
                return;
            }
            var form = getEditForm(req.responseXML);
            if (!form) {
                alert("asyncWikiEditor: Error getting edit form!");
                return;
            }
            var editor = WikiEditor(form);
            args[0] = editor;
            origCallback.apply(editor, args);
        };
        asyncDownloadXML(wp.qurl + '&action=edit', cb);
    }
}

// TODO: merge automod.js stuff

// </nowiki></pre>