2006-08-11 14:19:32 -04:00
|
|
|
var autosaveLast = '';
|
2006-10-30 15:18:59 -05:00
|
|
|
var autosavePeriodical;
|
2008-03-03 19:05:30 -05:00
|
|
|
var autosaveOldMessage = '';
|
2008-06-23 12:05:10 -04:00
|
|
|
var autosaveDelayURL = null;
|
|
|
|
var previewwin;
|
2006-11-20 19:55:06 -05:00
|
|
|
|
2008-02-29 04:51:36 -05: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 03:46:33 -05:00
|
|
|
|
2006-11-20 19:55:06 -05:00
|
|
|
//Disable autosave after the form has been submitted
|
2008-02-29 04:51:36 -05:00
|
|
|
$("#post").submit(function() { $.cancel(autosavePeriodical); });
|
2008-08-09 01:36:14 -04:00
|
|
|
|
|
|
|
// Autosave when the preview button is clicked.
|
2008-06-23 12:05:10 -04:00
|
|
|
$('#previewview a').click(function(e) {
|
|
|
|
autosave();
|
|
|
|
autosaveDelayURL = this.href;
|
|
|
|
previewwin = window.open('','_blank');
|
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
|
|
|
});
|
2008-02-29 04:51:36 -05:00
|
|
|
});
|
2006-08-10 23:54:45 -04:00
|
|
|
|
2008-04-22 22:22:55 -04:00
|
|
|
function autosave_parse_response(response) {
|
2008-02-29 04:51:36 -05:00
|
|
|
var res = wpAjax.parseAjaxResponse(response, 'autosave'); // parse the ajax response
|
|
|
|
var message = '';
|
|
|
|
|
2008-03-03 16:12:17 -05:00
|
|
|
if ( res && res.responses && res.responses.length ) {
|
2008-02-29 04:51:36 -05:00
|
|
|
message = res.responses[0].data; // The saved message or error.
|
|
|
|
// someone else is editing: disable autosave, set errors
|
2008-03-17 22:43:20 -04: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 04:51:36 -05: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-03 19:05:30 -05:00
|
|
|
else if ( autosaveOldMessage && res ) { jQuery('#autosave').html( autosaveOldMessage ); }
|
2008-02-29 04:51:36 -05:00
|
|
|
return res;
|
2006-08-10 23:54:45 -04:00
|
|
|
}
|
2007-02-27 10:24:54 -05:00
|
|
|
|
2008-04-22 22:22:55 -04: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 04:51:36 -05:00
|
|
|
// called when autosaving new post
|
2008-03-22 04:15:48 -04:00
|
|
|
function autosave_saved_new(response) {
|
2008-04-22 22:22:55 -04:00
|
|
|
var res = autosave_parse_response(response); // parse the ajax response
|
2008-02-29 04:51:36 -05: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 16:28:40 -04:00
|
|
|
var tempID = jQuery('#post_ID').val();
|
2008-02-29 04:51:36 -05:00
|
|
|
var postID = parseInt( res.responses[0].id );
|
2008-04-24 20:32:29 -04:00
|
|
|
autosave_update_post_ID( postID ); // disabled form buttons are re-enabled here
|
2008-03-26 17:00:21 -04:00
|
|
|
if ( tempID < 0 && postID > 0) // update media buttons
|
2008-03-26 16:28:40 -04:00
|
|
|
jQuery('#media-buttons a').each(function(){
|
|
|
|
this.href = this.href.replace(tempID, postID);
|
|
|
|
});
|
2008-04-24 20:32:29 -04:00
|
|
|
} else {
|
|
|
|
autosave_enable_buttons(); // re-enable disabled form buttons
|
2008-03-22 04:15:48 -04: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-24 20:32:29 -04:00
|
|
|
autosave_enable_buttons(); // re-enable disabled form buttons
|
2008-03-22 04:15:48 -04:00
|
|
|
});
|
|
|
|
jQuery('#hiddenaction').val('editpost');
|
2006-08-10 23:54:45 -04:00
|
|
|
}
|
|
|
|
}
|
2006-08-11 14:50:28 -04:00
|
|
|
|
2008-02-21 01:19:46 -05:00
|
|
|
function autosave_update_preview_link(post_id) {
|
|
|
|
// Add preview button if not already there
|
2008-02-29 04:51:36 -05:00
|
|
|
if ( !jQuery('#previewview > *').size() ) {
|
2008-02-21 01:19:46 -05: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 17:42:31 -04:00
|
|
|
jQuery('#previewview').html('<a class="button" target="_blank" href="'+permalink+'" tabindex="4">'+previewText+'</a>');
|
2008-06-23 12:05:10 -04:00
|
|
|
|
2008-08-09 01:36:14 -04:00
|
|
|
// Autosave when the preview button is clicked.
|
2008-06-23 12:05:10 -04:00
|
|
|
jQuery('#previewview a').click(function(e) {
|
|
|
|
autosave();
|
|
|
|
autosaveDelayURL = this.href;
|
|
|
|
previewwin = window.open('','_blank');
|
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
|
|
|
});
|
2008-02-21 01:19:46 -05:00
|
|
|
});
|
2008-02-21 12:08:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function autosave_update_slug(post_id) {
|
|
|
|
// create slug area only if not already there
|
2008-02-29 04:51:36 -05: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 01:36:14 -04:00
|
|
|
new_title: jQuery('#title').val(),
|
2008-02-29 04:51:36 -05:00
|
|
|
samplepermalinknonce: jQuery('#samplepermalinknonce').val()
|
|
|
|
},
|
|
|
|
function(data) {
|
2008-02-21 12:08:06 -05:00
|
|
|
jQuery('#edit-slug-box').html(data);
|
|
|
|
make_slugedit_clickable();
|
2008-02-29 04:51:36 -05:00
|
|
|
}
|
|
|
|
);
|
2008-02-21 01:19:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-10 23:54:45 -04:00
|
|
|
function autosave_loading() {
|
2008-03-03 16:15:34 -05:00
|
|
|
jQuery('#autosave').html(autosaveL10n.savingText);
|
2006-08-10 23:54:45 -04:00
|
|
|
}
|
|
|
|
|
2008-02-29 04:51:36 -05:00
|
|
|
function autosave_enable_buttons() {
|
|
|
|
jQuery("#submitpost :button:disabled, #submitpost :submit:disabled").attr('disabled', '');
|
2008-06-23 12:05:10 -04:00
|
|
|
if ( autosaveDelayURL ) {
|
|
|
|
previewwin.location = autosaveDelayURL;
|
|
|
|
autosaveDelayURL = null;
|
|
|
|
}
|
2006-08-10 23:54:45 -04:00
|
|
|
}
|
2006-12-06 00:33:52 -05:00
|
|
|
|
|
|
|
function autosave_disable_buttons() {
|
2008-02-29 04:51:36 -05:00
|
|
|
jQuery("#submitpost :button:enabled, #submitpost :submit:enabled").attr('disabled', 'disabled');
|
2008-04-24 20:32:29 -04:00
|
|
|
setTimeout(autosave_enable_buttons, 5000); // Re-enable 5 sec later. Just gives autosave a head start to avoid collisions.
|
2006-12-06 00:33:52 -05:00
|
|
|
}
|
|
|
|
|
2008-02-29 04:51:36 -05:00
|
|
|
var autosave = function() {
|
|
|
|
// (bool) is rich editor enabled and active
|
|
|
|
var rich = (typeof tinyMCE != "undefined") && tinyMCE.activeEditor && !tinyMCE.activeEditor.isHidden();
|
2008-01-04 03:46:33 -05:00
|
|
|
var post_data = {
|
2008-02-29 04:51:36 -05: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-10 23:54:45 -04:00
|
|
|
|
2008-03-26 16:28:40 -04:00
|
|
|
// No autosave while thickbox is open (media buttons)
|
|
|
|
if ( jQuery("#TB_window").css('display') == 'block' )
|
|
|
|
doAutoSave = false;
|
|
|
|
|
2006-08-10 23:54:45 -04:00
|
|
|
/* Gotta do this up here so we can check the length when tinyMCE is in use */
|
2008-08-09 01:36:14 -04:00
|
|
|
if ( rich ) {
|
2008-06-03 04:15:39 -04: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 01:36:14 -04:00
|
|
|
|
2008-02-06 16:19:47 -05:00
|
|
|
post_data["content"] = jQuery("#content").val();
|
2008-02-21 15:19:34 -05:00
|
|
|
if ( jQuery('#post_name').val() )
|
|
|
|
post_data["post_name"] = jQuery('#post_name').val();
|
2006-08-10 23:54:45 -04:00
|
|
|
|
2008-02-29 04:51:36 -05:00
|
|
|
// Nothing to save or no change.
|
2008-03-19 01:38:58 -04:00
|
|
|
if( (post_data["post_title"].length==0 && post_data["content"].length==0) || post_data["post_title"] + post_data["content"] == autosaveLast) {
|
2008-02-29 04:51:36 -05:00
|
|
|
doAutoSave = false
|
2008-01-04 03:46:33 -05:00
|
|
|
}
|
2006-08-11 14:19:32 -04:00
|
|
|
|
2006-12-06 00:33:52 -05:00
|
|
|
autosave_disable_buttons();
|
|
|
|
|
2008-02-29 04:51:36 -05:00
|
|
|
var origStatus = jQuery('#original_post_status').val();
|
|
|
|
|
2008-01-04 03:46:33 -05:00
|
|
|
autosaveLast = jQuery("#title").val()+jQuery("#content").val();
|
2006-08-10 23:54:45 -04:00
|
|
|
goodcats = ([]);
|
2008-01-04 03:46:33 -05: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 11:59:17 -04:00
|
|
|
if ( jQuery("#excerpt").size() )
|
2008-01-04 03:46:33 -05:00
|
|
|
post_data["excerpt"] = jQuery("#excerpt").val();
|
2008-05-09 11:59:17 -04:00
|
|
|
if ( jQuery("#post_author").size() )
|
2008-04-16 18:56:37 -04:00
|
|
|
post_data["post_author"] = jQuery("#post_author").val();
|
2007-02-27 10:24:54 -05:00
|
|
|
|
2008-02-29 04:51:36 -05: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 10:24:54 -05:00
|
|
|
|
2008-01-04 03:46:33 -05:00
|
|
|
if(parseInt(post_data["post_ID"]) < 1) {
|
|
|
|
post_data["temp_ID"] = post_data["post_ID"];
|
2008-03-22 04:15:48 -04:00
|
|
|
var successCallback = autosave_saved_new;; // new post
|
2008-01-04 03:46:33 -05:00
|
|
|
} else {
|
2008-02-29 04:51:36 -05:00
|
|
|
var successCallback = autosave_saved; // pre-existing post
|
2008-01-04 03:46:33 -05:00
|
|
|
}
|
2008-02-29 04:51:36 -05:00
|
|
|
|
|
|
|
if ( !doAutoSave ) {
|
|
|
|
post_data['autosave'] = 0;
|
|
|
|
}
|
|
|
|
|
2008-03-03 19:05:30 -05:00
|
|
|
autosaveOldMessage = jQuery('#autosave').html();
|
|
|
|
|
2008-01-04 03:46:33 -05:00
|
|
|
jQuery.ajax({
|
|
|
|
data: post_data,
|
2008-02-29 04:51:36 -05:00
|
|
|
beforeSend: doAutoSave ? autosave_loading : null,
|
2008-01-04 03:46:33 -05:00
|
|
|
type: "POST",
|
2008-02-29 04:51:36 -05:00
|
|
|
url: autosaveL10n.requestFile,
|
|
|
|
success: successCallback
|
2008-01-04 03:46:33 -05:00
|
|
|
});
|
2006-08-16 03:28:24 -04:00
|
|
|
}
|