2006-08-11 20:19:32 +02:00
|
|
|
var autosaveLast = '';
|
2006-10-30 21:18:59 +01:00
|
|
|
var autosavePeriodical;
|
2008-03-04 01:05:30 +01:00
|
|
|
var autosaveOldMessage = '';
|
2008-06-23 18:05:10 +02:00
|
|
|
var autosaveDelayURL = null;
|
|
|
|
var previewwin;
|
2006-11-21 01:55:06 +01:00
|
|
|
|
2008-02-29 10:51:36 +01:00
|
|
|
jQuery(function($) {
|
|
|
|
autosaveLast = $('#post #title').val()+$('#post #content').val();
|
|
|
|
autosavePeriodical = $.schedule({time: autosaveL10n.autosaveInterval * 1000, func: function() { autosave(); }, repeat: true, protect: true});
|
2008-01-04 09:46:33 +01:00
|
|
|
|
2006-11-21 01:55:06 +01:00
|
|
|
//Disable autosave after the form has been submitted
|
2008-02-29 10:51:36 +01:00
|
|
|
$("#post").submit(function() { $.cancel(autosavePeriodical); });
|
2008-08-09 07:36:14 +02:00
|
|
|
|
|
|
|
// Autosave when the preview button is clicked.
|
2008-06-23 18:05:10 +02:00
|
|
|
$('#previewview a').click(function(e) {
|
|
|
|
autosave();
|
|
|
|
autosaveDelayURL = this.href;
|
|
|
|
previewwin = window.open('','_blank');
|
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
|
|
|
});
|
2008-02-29 10:51:36 +01:00
|
|
|
});
|
2006-08-11 05:54:45 +02:00
|
|
|
|
2008-04-23 04:22:55 +02:00
|
|
|
function autosave_parse_response(response) {
|
2008-02-29 10:51:36 +01:00
|
|
|
var res = wpAjax.parseAjaxResponse(response, 'autosave'); // parse the ajax response
|
|
|
|
var message = '';
|
|
|
|
|
2008-03-03 22:12:17 +01:00
|
|
|
if ( res && res.responses && res.responses.length ) {
|
2008-02-29 10:51:36 +01:00
|
|
|
message = res.responses[0].data; // The saved message or error.
|
|
|
|
// someone else is editing: disable autosave, set errors
|
2008-03-18 03:43:20 +01:00
|
|
|
if ( res.responses[0].supplemental ) {
|
|
|
|
if ( 'disable' == res.responses[0].supplemental['disable_autosave'] ) {
|
|
|
|
autosave = function() {};
|
|
|
|
res = { errors: true };
|
|
|
|
}
|
|
|
|
jQuery.each(res.responses[0].supplemental, function(selector, value) {
|
|
|
|
if ( selector.match(/^replace-/) ) {
|
|
|
|
jQuery('#'+selector.replace('replace-', '')).val(value);
|
|
|
|
}
|
|
|
|
});
|
2008-02-29 10:51:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// if no errors: add preview link and slug UI
|
|
|
|
if ( !res.errors ) {
|
|
|
|
var postID = parseInt( res.responses[0].id );
|
|
|
|
if ( !isNaN(postID) && postID > 0 ) {
|
|
|
|
autosave_update_preview_link(postID);
|
|
|
|
autosave_update_slug(postID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( message ) { jQuery('#autosave').html(message); } // update autosave message
|
2008-03-04 01:05:30 +01:00
|
|
|
else if ( autosaveOldMessage && res ) { jQuery('#autosave').html( autosaveOldMessage ); }
|
2008-02-29 10:51:36 +01:00
|
|
|
return res;
|
2006-08-11 05:54:45 +02:00
|
|
|
}
|
2007-02-27 16:24:54 +01:00
|
|
|
|
2008-04-23 04:22:55 +02:00
|
|
|
// called when autosaving pre-existing post
|
|
|
|
function autosave_saved(response) {
|
|
|
|
autosave_parse_response(response); // parse the ajax response
|
|
|
|
autosave_enable_buttons(); // re-enable disabled form buttons
|
|
|
|
}
|
|
|
|
|
2008-02-29 10:51:36 +01:00
|
|
|
// called when autosaving new post
|
2008-03-22 09:15:48 +01:00
|
|
|
function autosave_saved_new(response) {
|
2008-04-23 04:22:55 +02:00
|
|
|
var res = autosave_parse_response(response); // parse the ajax response
|
2008-02-29 10:51:36 +01:00
|
|
|
// if no errors: update post_ID from the temporary value, grab new save-nonce for that new ID
|
|
|
|
if ( res && res.responses.length && !res.errors ) {
|
2008-03-26 21:28:40 +01:00
|
|
|
var tempID = jQuery('#post_ID').val();
|
2008-02-29 10:51:36 +01:00
|
|
|
var postID = parseInt( res.responses[0].id );
|
2008-04-25 02:32:29 +02:00
|
|
|
autosave_update_post_ID( postID ); // disabled form buttons are re-enabled here
|
2008-03-26 22:00:21 +01:00
|
|
|
if ( tempID < 0 && postID > 0) // update media buttons
|
2008-03-26 21:28:40 +01:00
|
|
|
jQuery('#media-buttons a').each(function(){
|
|
|
|
this.href = this.href.replace(tempID, postID);
|
|
|
|
});
|
2008-04-25 02:32:29 +02:00
|
|
|
} else {
|
|
|
|
autosave_enable_buttons(); // re-enable disabled form buttons
|
2008-03-22 09:15:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function autosave_update_post_ID( postID ) {
|
|
|
|
if ( !isNaN(postID) && postID > 0 ) {
|
|
|
|
if ( postID == parseInt(jQuery('#post_ID').val()) ) { return; } // no need to do this more than once
|
|
|
|
jQuery('#post_ID').attr({name: "post_ID"});
|
|
|
|
jQuery('#post_ID').val(postID);
|
|
|
|
// We need new nonces
|
|
|
|
jQuery.post(autosaveL10n.requestFile, {
|
|
|
|
action: "autosave-generate-nonces",
|
|
|
|
post_ID: postID,
|
|
|
|
autosavenonce: jQuery('#autosavenonce').val(),
|
|
|
|
post_type: jQuery('#post_type').val()
|
|
|
|
}, function(html) {
|
|
|
|
jQuery('#_wpnonce').val(html);
|
2008-04-25 02:32:29 +02:00
|
|
|
autosave_enable_buttons(); // re-enable disabled form buttons
|
2008-03-22 09:15:48 +01:00
|
|
|
});
|
|
|
|
jQuery('#hiddenaction').val('editpost');
|
2006-08-11 05:54:45 +02:00
|
|
|
}
|
|
|
|
}
|
2006-08-11 20:50:28 +02:00
|
|
|
|
2008-02-21 07:19:46 +01:00
|
|
|
function autosave_update_preview_link(post_id) {
|
|
|
|
// Add preview button if not already there
|
2008-02-29 10:51:36 +01:00
|
|
|
if ( !jQuery('#previewview > *').size() ) {
|
2008-02-21 07:19:46 +01:00
|
|
|
var post_type = jQuery('#post_type').val();
|
|
|
|
var previewText = 'page' == post_type ? autosaveL10n.previewPageText : autosaveL10n.previewPostText;
|
|
|
|
jQuery.post(autosaveL10n.requestFile, {
|
|
|
|
action: "get-permalink",
|
|
|
|
post_id: post_id,
|
|
|
|
getpermalinknonce: jQuery('#getpermalinknonce').val()
|
|
|
|
}, function(permalink) {
|
2008-08-20 23:42:31 +02:00
|
|
|
jQuery('#previewview').html('<a class="button" target="_blank" href="'+permalink+'" tabindex="4">'+previewText+'</a>');
|
2008-06-23 18:05:10 +02:00
|
|
|
|
2008-08-09 07:36:14 +02:00
|
|
|
// Autosave when the preview button is clicked.
|
2008-06-23 18:05:10 +02:00
|
|
|
jQuery('#previewview a').click(function(e) {
|
|
|
|
autosave();
|
|
|
|
autosaveDelayURL = this.href;
|
|
|
|
previewwin = window.open('','_blank');
|
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
|
|
|
});
|
2008-02-21 07:19:46 +01:00
|
|
|
});
|
2008-02-21 18:08:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function autosave_update_slug(post_id) {
|
|
|
|
// create slug area only if not already there
|
2008-02-29 10:51:36 +01:00
|
|
|
if ( jQuery.isFunction(make_slugedit_clickable) && !jQuery('#edit-slug-box > *').size() ) {
|
|
|
|
jQuery.post(
|
|
|
|
slugL10n.requestFile,
|
|
|
|
{
|
|
|
|
action: 'sample-permalink',
|
|
|
|
post_id: post_id,
|
2008-08-09 07:36:14 +02:00
|
|
|
new_title: jQuery('#title').val(),
|
2008-02-29 10:51:36 +01:00
|
|
|
samplepermalinknonce: jQuery('#samplepermalinknonce').val()
|
|
|
|
},
|
|
|
|
function(data) {
|
2008-02-21 18:08:06 +01:00
|
|
|
jQuery('#edit-slug-box').html(data);
|
|
|
|
make_slugedit_clickable();
|
2008-02-29 10:51:36 +01:00
|
|
|
}
|
|
|
|
);
|
2008-02-21 07:19:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-11 05:54:45 +02:00
|
|
|
function autosave_loading() {
|
2008-03-03 22:15:34 +01:00
|
|
|
jQuery('#autosave').html(autosaveL10n.savingText);
|
2006-08-11 05:54:45 +02:00
|
|
|
}
|
|
|
|
|
2008-02-29 10:51:36 +01:00
|
|
|
function autosave_enable_buttons() {
|
|
|
|
jQuery("#submitpost :button:disabled, #submitpost :submit:disabled").attr('disabled', '');
|
2008-06-23 18:05:10 +02:00
|
|
|
if ( autosaveDelayURL ) {
|
|
|
|
previewwin.location = autosaveDelayURL;
|
|
|
|
autosaveDelayURL = null;
|
|
|
|
}
|
2006-08-11 05:54:45 +02:00
|
|
|
}
|
2006-12-06 06:33:52 +01:00
|
|
|
|
|
|
|
function autosave_disable_buttons() {
|
2008-02-29 10:51:36 +01:00
|
|
|
jQuery("#submitpost :button:enabled, #submitpost :submit:enabled").attr('disabled', 'disabled');
|
2008-04-25 02:32:29 +02:00
|
|
|
setTimeout(autosave_enable_buttons, 5000); // Re-enable 5 sec later. Just gives autosave a head start to avoid collisions.
|
2006-12-06 06:33:52 +01:00
|
|
|
}
|
|
|
|
|
2008-02-29 10:51:36 +01:00
|
|
|
var autosave = function() {
|
|
|
|
// (bool) is rich editor enabled and active
|
|
|
|
var rich = (typeof tinyMCE != "undefined") && tinyMCE.activeEditor && !tinyMCE.activeEditor.isHidden();
|
2008-01-04 09:46:33 +01:00
|
|
|
var post_data = {
|
2008-02-29 10:51:36 +01:00
|
|
|
action: "autosave",
|
|
|
|
post_ID: jQuery("#post_ID").val() || 0,
|
|
|
|
post_title: jQuery("#title").val() || "",
|
|
|
|
autosavenonce: jQuery('#autosavenonce').val(),
|
|
|
|
tags_input: jQuery("#tags-input").val() || "",
|
|
|
|
post_type: jQuery('#post_type').val() || "",
|
|
|
|
autosave: 1
|
|
|
|
};
|
|
|
|
|
|
|
|
// We always send the ajax request in order to keep the post lock fresh.
|
|
|
|
// This (bool) tells whether or not to write the post to the DB during the ajax request.
|
|
|
|
var doAutoSave = true;
|
2006-08-11 05:54:45 +02:00
|
|
|
|
2008-03-26 21:28:40 +01:00
|
|
|
// No autosave while thickbox is open (media buttons)
|
|
|
|
if ( jQuery("#TB_window").css('display') == 'block' )
|
|
|
|
doAutoSave = false;
|
|
|
|
|
2006-08-11 05:54:45 +02:00
|
|
|
/* Gotta do this up here so we can check the length when tinyMCE is in use */
|
2008-08-09 07:36:14 +02:00
|
|
|
if ( rich ) {
|
2008-06-03 10:15:39 +02:00
|
|
|
var ed = tinyMCE.activeEditor;
|
|
|
|
if ( 'mce_fullscreen' == ed.id )
|
|
|
|
tinyMCE.get('content').setContent(ed.getContent({format : 'raw'}), {format : 'raw'});
|
|
|
|
tinyMCE.get('content').save();
|
|
|
|
}
|
2008-08-09 07:36:14 +02:00
|
|
|
|
2008-02-06 22:19:47 +01:00
|
|
|
post_data["content"] = jQuery("#content").val();
|
2008-02-21 21:19:34 +01:00
|
|
|
if ( jQuery('#post_name').val() )
|
|
|
|
post_data["post_name"] = jQuery('#post_name').val();
|
2006-08-11 05:54:45 +02:00
|
|
|
|
2008-02-29 10:51:36 +01:00
|
|
|
// Nothing to save or no change.
|
2008-03-19 06:38:58 +01:00
|
|
|
if( (post_data["post_title"].length==0 && post_data["content"].length==0) || post_data["post_title"] + post_data["content"] == autosaveLast) {
|
2008-02-29 10:51:36 +01:00
|
|
|
doAutoSave = false
|
2008-01-04 09:46:33 +01:00
|
|
|
}
|
2006-08-11 20:19:32 +02:00
|
|
|
|
2006-12-06 06:33:52 +01:00
|
|
|
autosave_disable_buttons();
|
|
|
|
|
2008-02-29 10:51:36 +01:00
|
|
|
var origStatus = jQuery('#original_post_status').val();
|
|
|
|
|
2008-01-04 09:46:33 +01:00
|
|
|
autosaveLast = jQuery("#title").val()+jQuery("#content").val();
|
2006-08-11 05:54:45 +02:00
|
|
|
goodcats = ([]);
|
2008-01-04 09:46:33 +01:00
|
|
|
jQuery("[@name='post_category[]']:checked").each( function(i) {
|
|
|
|
goodcats.push(this.value);
|
|
|
|
} );
|
|
|
|
post_data["catslist"] = goodcats.join(",");
|
|
|
|
|
|
|
|
if ( jQuery("#comment_status").attr("checked") )
|
|
|
|
post_data["comment_status"] = 'open';
|
|
|
|
if ( jQuery("#ping_status").attr("checked") )
|
|
|
|
post_data["ping_status"] = 'open';
|
2008-05-09 17:59:17 +02:00
|
|
|
if ( jQuery("#excerpt").size() )
|
2008-01-04 09:46:33 +01:00
|
|
|
post_data["excerpt"] = jQuery("#excerpt").val();
|
2008-05-09 17:59:17 +02:00
|
|
|
if ( jQuery("#post_author").size() )
|
2008-04-17 00:56:37 +02:00
|
|
|
post_data["post_author"] = jQuery("#post_author").val();
|
2007-02-27 16:24:54 +01:00
|
|
|
|
2008-02-29 10:51:36 +01:00
|
|
|
// Don't run while the TinyMCE spellcheck is on. Why? Who knows.
|
|
|
|
if ( rich && tinyMCE.activeEditor.plugins.spellchecker && tinyMCE.activeEditor.plugins.spellchecker.active ) {
|
|
|
|
doAutoSave = false;
|
|
|
|
}
|
2007-02-27 16:24:54 +01:00
|
|
|
|
2008-01-04 09:46:33 +01:00
|
|
|
if(parseInt(post_data["post_ID"]) < 1) {
|
|
|
|
post_data["temp_ID"] = post_data["post_ID"];
|
2008-03-22 09:15:48 +01:00
|
|
|
var successCallback = autosave_saved_new;; // new post
|
2008-01-04 09:46:33 +01:00
|
|
|
} else {
|
2008-02-29 10:51:36 +01:00
|
|
|
var successCallback = autosave_saved; // pre-existing post
|
2008-01-04 09:46:33 +01:00
|
|
|
}
|
2008-02-29 10:51:36 +01:00
|
|
|
|
|
|
|
if ( !doAutoSave ) {
|
|
|
|
post_data['autosave'] = 0;
|
|
|
|
}
|
|
|
|
|
2008-03-04 01:05:30 +01:00
|
|
|
autosaveOldMessage = jQuery('#autosave').html();
|
|
|
|
|
2008-01-04 09:46:33 +01:00
|
|
|
jQuery.ajax({
|
|
|
|
data: post_data,
|
2008-02-29 10:51:36 +01:00
|
|
|
beforeSend: doAutoSave ? autosave_loading : null,
|
2008-01-04 09:46:33 +01:00
|
|
|
type: "POST",
|
2008-02-29 10:51:36 +01:00
|
|
|
url: autosaveL10n.requestFile,
|
|
|
|
success: successCallback
|
2008-01-04 09:46:33 +01:00
|
|
|
});
|
2006-08-16 09:28:24 +02:00
|
|
|
}
|