1 var autosave, autosaveLast = '', autosavePeriodical, autosaveOldMessage = '', autosaveDelayPreview = false, notSaved = true, blockSave = false, fullscreen;
3 jQuery(document).ready( function($) {
6 autosaveLast = $('#post #title').val() + $('#post #content').val();
7 autosavePeriodical = $.schedule({time: autosaveL10n.autosaveInterval * 1000, func: function() { autosave(); }, repeat: true, protect: true});
9 //Disable autosave after the form has been submitted
10 $("#post").submit(function() {
11 $.cancel(autosavePeriodical);
14 $('input[type="submit"], a.submitdelete', '#submitpost').click(function(){
16 window.onbeforeunload = null;
17 $(':button, :submit', '#submitpost').each(function(){
19 if ( t.hasClass('button-primary') )
20 t.addClass('button-primary-disabled');
22 t.addClass('button-disabled');
24 if ( $(this).attr('id') == 'publish' )
25 $('#ajax-loading').css('visibility', 'visible');
27 $('#draft-ajax-loading').css('visibility', 'visible');
30 window.onbeforeunload = function(){
31 var mce = typeof(tinyMCE) != 'undefined' ? tinyMCE.activeEditor : false, title, content;
33 if ( mce && !mce.isHidden() ) {
35 return autosaveL10n.saveAlert;
37 if ( fullscreen && fullscreen.settings.visible ) {
38 title = $('#wp-fullscreen-title').val();
39 content = $("#wp_mce_fullscreen").val();
41 title = $('#post #title').val();
42 content = $('#post #content').val();
45 if ( ( title || content ) && title + content != autosaveLast )
46 return autosaveL10n.saveAlert;
51 $('#post-preview').click(function(){
52 if ( $('#auto_draft').val() == '1' && notSaved ) {
53 autosaveDelayPreview = true;
61 doPreview = function() {
62 $('input#wp-preview').val('dopreview');
63 $('form#post').attr('target', 'wp-preview').submit().attr('target', '');
64 $('input#wp-preview').val('');
67 // This code is meant to allow tabbing from Title to Post if tinyMCE is defined.
68 if ( typeof tinyMCE != 'undefined' ) {
69 $('#title')[$.browser.opera ? 'keypress' : 'keydown'](function (e) {
70 if ( e.which == 9 && !e.shiftKey && !e.controlKey && !e.altKey ) {
71 if ( ($('#auto_draft').val() == '1') && ($("#title").val().length > 0) ) { autosave(); }
72 if ( tinyMCE.activeEditor && ! tinyMCE.activeEditor.isHidden() && dotabkey ) {
75 tinyMCE.activeEditor.focus();
82 // autosave new posts after a title is typed but not if Publish or Save Draft is clicked
83 if ( '1' == $('#auto_draft').val() ) {
84 $('#title').blur( function() {
85 if ( !this.value || $('#auto_draft').val() != '1' )
92 function autosave_parse_response(response) {
93 var res = wpAjax.parseAjaxResponse(response, 'autosave'), message = '', postID, sup;
95 if ( res && res.responses && res.responses.length ) {
96 message = res.responses[0].data; // The saved message or error.
97 // someone else is editing: disable autosave, set errors
98 if ( res.responses[0].supplemental ) {
99 sup = res.responses[0].supplemental;
100 if ( 'disable' == sup['disable_autosave'] ) {
101 autosave = function() {};
102 res = { errors: true };
105 if ( sup['alert'] ) {
106 jQuery('#autosave-alert').remove();
107 jQuery('#titlediv').after('<div id="autosave-alert" class="error below-h2"><p>' + sup['alert'] + '</p></div>');
110 jQuery.each(sup, function(selector, value) {
111 if ( selector.match(/^replace-/) ) {
112 jQuery('#'+selector.replace('replace-', '')).val(value);
117 // if no errors: add slug UI
119 postID = parseInt( res.responses[0].id, 10 );
120 if ( !isNaN(postID) && postID > 0 ) {
121 autosave_update_slug(postID);
125 if ( message ) { // update autosave message
126 jQuery('.autosave-message').html(message);
127 } else if ( autosaveOldMessage && res ) {
128 jQuery('.autosave-message').html( autosaveOldMessage );
133 // called when autosaving pre-existing post
134 function autosave_saved(response) {
136 autosave_parse_response(response); // parse the ajax response
137 autosave_enable_buttons(); // re-enable disabled form buttons
140 // called when autosaving new post
141 function autosave_saved_new(response) {
143 var res = autosave_parse_response(response), postID;
144 if ( res && res.responses.length && !res.errors ) {
145 // An ID is sent only for real auto-saves, not for autosave=0 "keepalive" saves
146 postID = parseInt( res.responses[0].id, 10 );
147 if ( !isNaN(postID) && postID > 0 ) {
149 jQuery('#auto_draft').val('0'); // No longer an auto-draft
151 autosave_enable_buttons();
152 if ( autosaveDelayPreview ) {
153 autosaveDelayPreview = false;
157 autosave_enable_buttons(); // re-enable disabled form buttons
161 function autosave_update_slug(post_id) {
162 // create slug area only if not already there
163 if ( 'undefined' != makeSlugeditClickable && jQuery.isFunction(makeSlugeditClickable) && !jQuery('#edit-slug-box > *').size() ) {
164 jQuery.post( ajaxurl, {
165 action: 'sample-permalink',
167 new_title: fullscreen && fullscreen.settings.visible ? jQuery('#wp-fullscreen-title').val() : jQuery('#title').val(),
168 samplepermalinknonce: jQuery('#samplepermalinknonce').val()
171 if ( data !== '-1' ) {
172 jQuery('#edit-slug-box').html(data);
173 makeSlugeditClickable();
180 function autosave_loading() {
181 jQuery('.autosave-message').html(autosaveL10n.savingText);
184 function autosave_enable_buttons() {
185 // delay that a bit to avoid some rare collisions while the DOM is being updated.
186 setTimeout(function(){
187 jQuery(':button, :submit', '#submitpost').removeAttr('disabled');
188 jQuery('.ajax-loading').css('visibility', 'hidden');
192 function autosave_disable_buttons() {
193 jQuery(':button, :submit', '#submitpost').prop('disabled', true);
194 // Re-enable 5 sec later. Just gives autosave a head start to avoid collisions.
195 setTimeout(autosave_enable_buttons, 5000);
198 function delayed_autosave() {
199 setTimeout(function(){
206 autosave = function() {
207 // (bool) is rich editor enabled and active
209 var rich = (typeof tinyMCE != "undefined") && tinyMCE.activeEditor && !tinyMCE.activeEditor.isHidden(),
210 post_data, doAutoSave, ed, origStatus, successCallback;
212 autosave_disable_buttons();
216 post_ID: jQuery("#post_ID").val() || 0,
217 autosavenonce: jQuery('#autosavenonce').val(),
218 post_type: jQuery('#post_type').val() || "",
222 jQuery('.tags-input').each( function() {
223 post_data[this.name] = this.value;
226 // We always send the ajax request in order to keep the post lock fresh.
227 // This (bool) tells whether or not to write the post to the DB during the ajax request.
230 // No autosave while thickbox is open (media buttons)
231 if ( jQuery("#TB_window").css('display') == 'block' )
234 /* Gotta do this up here so we can check the length when tinyMCE is in use */
235 if ( rich && doAutoSave ) {
236 ed = tinyMCE.activeEditor;
237 // Don't run while the TinyMCE spellcheck is on. It resets all found words.
238 if ( ed.plugins.spellchecker && ed.plugins.spellchecker.active ) {
241 if ( 'mce_fullscreen' == ed.id || 'wp_mce_fullscreen' == ed.id )
242 tinyMCE.get('content').setContent(ed.getContent({format : 'raw'}), {format : 'raw'});
243 tinyMCE.triggerSave();
247 if ( fullscreen && fullscreen.settings.visible ) {
248 post_data["post_title"] = jQuery('#wp-fullscreen-title').val();
249 post_data["content"] = jQuery("#wp_mce_fullscreen").val();
251 post_data["post_title"] = jQuery("#title").val()
252 post_data["content"] = jQuery("#content").val();
255 if ( jQuery('#post_name').val() )
256 post_data["post_name"] = jQuery('#post_name').val();
258 // Nothing to save or no change.
259 if ( ( post_data["post_title"].length == 0 && post_data["content"].length == 0 ) || post_data["post_title"] + post_data["content"] == autosaveLast ) {
263 origStatus = jQuery('#original_post_status').val();
266 jQuery("[name='post_category[]']:checked").each( function(i) {
267 goodcats.push(this.value);
269 post_data["catslist"] = goodcats.join(",");
271 if ( jQuery("#comment_status").prop("checked") )
272 post_data["comment_status"] = 'open';
273 if ( jQuery("#ping_status").prop("checked") )
274 post_data["ping_status"] = 'open';
275 if ( jQuery("#excerpt").size() )
276 post_data["excerpt"] = jQuery("#excerpt").val();
277 if ( jQuery("#post_author").size() )
278 post_data["post_author"] = jQuery("#post_author").val();
279 if ( jQuery("#parent_id").val() )
280 post_data["parent_id"] = jQuery("#parent_id").val();
281 post_data["user_ID"] = jQuery("#user-id").val();
282 if ( jQuery('#auto_draft').val() == '1' )
283 post_data["auto_draft"] = '1';
286 autosaveLast = post_data["post_title"] + post_data["content"];
287 jQuery(document).triggerHandler('wpcountwords', [ post_data["content"] ]);
289 post_data['autosave'] = 0;
292 if ( post_data["auto_draft"] == '1' ) {
293 successCallback = autosave_saved_new; // new post
295 successCallback = autosave_saved; // pre-existing post
298 autosaveOldMessage = jQuery('#autosave').html();
301 beforeSend: doAutoSave ? autosave_loading : null,
304 success: successCallback