Jump to content

User:Quarl/smartsubmit.js

From 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.
// [[User:Quarl/smartsubmit.js]] - smarter diff/preview buttons

// depends: wikiedit.js, util.js

// enhances: wikiedit.js, autoreplace.js, auto_summary.js

// <pre><nowiki>

//  Instead of navigating to a new page, the "Show preview" and "Show
//  changes" buttons download the target asynchronously and update the current
//  page.
//
// Advantages:
//  - Can continue editing without losing changes
//  - Can use up to 250 characters in edit summary (normally, preview/diff
//    would cut to 200 characters)
//  - enhances auto_summary.js: the auto diff is based on the "previous"
//    version of the page, but normally pressing preview/diff would cause the
//    preview version to be "previous", as there is no way to save the
//    original text
//  - enhances autoreplace.js (including advanced_sig.js): replacements not
//    substituted in the edit box on preview/diff
//  - don't lose cursor position in text box

// Side effects:
//  - Since the browser doesn't navigate the window to a new location, it
//    doesn't register as a history event (i.e. "back" goes to the page
//    visited before any initial editing)

// quarl 2006-02-02 initial version

smartsubmit = new Object();

smartsubmit.focus_after_download = true;

smartsubmit.load = function() {
    if (!wikiDoc.editingP) return;

    if (!smartsubmit.annotatePageInit()) return;

    document.editform.wpDiff.preventPreSumitHook = true;
    hookEventObj(document.editform.wpDiff, 'click',
                 function(event){smartsubmit.click(event,'wpDiff');});
    document.editform.wpPreview.preventPreSumitHook = true;
    hookEventObj(document.editform.wpPreview, 'click',
                 function(event){smartsubmit.click(event,'wpPreview');});
}

// returns true value on success
smartsubmit.annotatePageInit = function() {
    var divLoc = document.getElementById('jump-to-nav');
    if (!divLoc) {
        alert("Smart Submit: couldn't get 'jump-to-nav' (error 1639a197-bdd2-4e9d-9777-9c8a5987fe2c)");
        return null;
    }
    smartsubmit.div = document.createElement('div');
    smartsubmit.div.id = 'smartsubmit-preview';
    add_after(divLoc, smartsubmit.div);

    // create an empty <a> so that we can focus on it
    smartsubmit.focusloc = document.createElement('a');
    smartsubmit.focusloc.id = 'smartsubmit-focusloc';
    add_after(divLoc, smartsubmit.focusloc);

    return true;
}

smartsubmit.focusPreview = function() {
    smartsubmit.focusloc.focus();
}

// The user clicked wpDiff or wpPreview.  Instead of submitting, we're going
// to make a new asynchronous request, download the data, insert into the
// current page.
smartsubmit.click = function(event, button) {
    // don't allow multiple concurrent clicks
    document.editform[button].disabled = true;
    smartsubmit.div.innerHTML = 'Submitting...';

    wikiPage.getEditor().submitAsync(button, smartsubmit.updatePage, button);

    event.preventDefault();
    event.stopPropagation();
}

smartsubmit.updatePage = function(req, button) {
    var editor = this;
    // re-enable button
    document.editform[button].disabled = false;
    if (req.status != 200) {
        alert ("Smart Submit: downloading page failed!");
        return;
    }

    var newDoc = req.responseXML;

    if (button == 'wpDiff') {
        var divId = 'wikiDiff';
    } else if (button == 'wpPreview') {
        var divId = 'wikiPreview';
    } else {
        alert("## internal error dcd4ab5a-4e10-4576-9cd5-874d589455da");
        return;
    }

    var newDiv = newDoc.getElementById(divId);

    if (!newDiv) {
        alert("Smart Submit: Couldn't get "+divId+" from downloaded document!");
        return;
    }

    // clear and add newDiv
    smartsubmit.div.innerHTML = '';
    smartsubmit.div.appendChild(newDiv);

    if (smartsubmit.focus_after_download) {
        smartsubmit.focusPreview();
    }
}

$(smartsubmit.load);

// </nowiki></pre>