2 var autosavePeriodical;
3 var autosaveOldMessage = '';
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() { $.cancel(autosavePeriodical); });
13 function autosave_parse_response(response) {
14 var res = wpAjax.parseAjaxResponse(response, 'autosave'); // parse the ajax response
17 if ( res && res.responses && res.responses.length ) {
18 message = res.responses[0].data; // The saved message or error.
19 // someone else is editing: disable autosave, set errors
20 if ( res.responses[0].supplemental ) {
21 if ( 'disable' == res.responses[0].supplemental['disable_autosave'] ) {
22 autosave = function() {};
23 res = { errors: true };
25 jQuery.each(res.responses[0].supplemental, function(selector, value) {
26 if ( selector.match(/^replace-/) ) {
27 jQuery('#'+selector.replace('replace-', '')).val(value);
32 // if no errors: add preview link and slug UI
34 var postID = parseInt( res.responses[0].id );
35 if ( !isNaN(postID) && postID > 0 ) {
36 autosave_update_preview_link(postID);
37 autosave_update_slug(postID);
41 if ( message ) { jQuery('#autosave').html(message); } // update autosave message
42 else if ( autosaveOldMessage && res ) { jQuery('#autosave').html( autosaveOldMessage ); }
46 // called when autosaving pre-existing post
47 function autosave_saved(response) {
48 autosave_parse_response(response); // parse the ajax response
49 autosave_enable_buttons(); // re-enable disabled form buttons
52 // called when autosaving new post
53 function autosave_saved_new(response) {
54 var res = autosave_parse_response(response); // parse the ajax response
55 // if no errors: update post_ID from the temporary value, grab new save-nonce for that new ID
56 if ( res && res.responses.length && !res.errors ) {
57 var tempID = jQuery('#post_ID').val();
58 var postID = parseInt( res.responses[0].id );
59 autosave_update_post_ID( postID ); // disabled form buttons are re-enabled here
60 if ( tempID < 0 && postID > 0) // update media buttons
61 jQuery('#media-buttons a').each(function(){
62 this.href = this.href.replace(tempID, postID);
65 autosave_enable_buttons(); // re-enable disabled form buttons
69 function autosave_update_post_ID( postID ) {
70 if ( !isNaN(postID) && postID > 0 ) {
71 if ( postID == parseInt(jQuery('#post_ID').val()) ) { return; } // no need to do this more than once
72 jQuery('#post_ID').attr({name: "post_ID"});
73 jQuery('#post_ID').val(postID);
75 jQuery.post(autosaveL10n.requestFile, {
76 action: "autosave-generate-nonces",
78 autosavenonce: jQuery('#autosavenonce').val(),
79 post_type: jQuery('#post_type').val()
81 jQuery('#_wpnonce').val(html);
82 autosave_enable_buttons(); // re-enable disabled form buttons
84 jQuery('#hiddenaction').val('editpost');
88 function autosave_update_preview_link(post_id) {
89 // Add preview button if not already there
90 if ( !jQuery('#previewview > *').size() ) {
91 var post_type = jQuery('#post_type').val();
92 var previewText = 'page' == post_type ? autosaveL10n.previewPageText : autosaveL10n.previewPostText;
93 jQuery.post(autosaveL10n.requestFile, {
94 action: "get-permalink",
96 getpermalinknonce: jQuery('#getpermalinknonce').val()
97 }, function(permalink) {
98 jQuery('#previewview').html('<a target="_blank" href="'+permalink+'" tabindex="4">'+previewText+'</a>');
103 function autosave_update_slug(post_id) {
104 // create slug area only if not already there
105 if ( jQuery.isFunction(make_slugedit_clickable) && !jQuery('#edit-slug-box > *').size() ) {
107 slugL10n.requestFile,
109 action: 'sample-permalink',
111 new_title: jQuery('#title').val(),
112 samplepermalinknonce: jQuery('#samplepermalinknonce').val()
115 jQuery('#edit-slug-box').html(data);
116 make_slugedit_clickable();
122 function autosave_loading() {
123 jQuery('#autosave').html(autosaveL10n.savingText);
126 function autosave_enable_buttons() {
127 jQuery("#submitpost :button:disabled, #submitpost :submit:disabled").attr('disabled', '');
130 function autosave_disable_buttons() {
131 jQuery("#submitpost :button:enabled, #submitpost :submit:enabled").attr('disabled', 'disabled');
132 setTimeout(autosave_enable_buttons, 5000); // Re-enable 5 sec later. Just gives autosave a head start to avoid collisions.
135 var autosave = function() {
136 // (bool) is rich editor enabled and active
137 var rich = (typeof tinyMCE != "undefined") && tinyMCE.activeEditor && !tinyMCE.activeEditor.isHidden();
140 post_ID: jQuery("#post_ID").val() || 0,
141 post_title: jQuery("#title").val() || "",
142 autosavenonce: jQuery('#autosavenonce').val(),
143 tags_input: jQuery("#tags-input").val() || "",
144 post_type: jQuery('#post_type').val() || "",
148 // We always send the ajax request in order to keep the post lock fresh.
149 // This (bool) tells whether or not to write the post to the DB during the ajax request.
150 var doAutoSave = true;
152 // No autosave while thickbox is open (media buttons)
153 if ( jQuery("#TB_window").css('display') == 'block' )
156 /* Gotta do this up here so we can check the length when tinyMCE is in use */
157 if ( rich ) { tinyMCE.triggerSave(); }
159 post_data["content"] = jQuery("#content").val();
160 if ( jQuery('#post_name').val() )
161 post_data["post_name"] = jQuery('#post_name').val();
163 // Nothing to save or no change.
164 if( (post_data["post_title"].length==0 && post_data["content"].length==0) || post_data["post_title"] + post_data["content"] == autosaveLast) {
168 autosave_disable_buttons();
170 var origStatus = jQuery('#original_post_status').val();
171 if ( 'draft' != origStatus ) // autosave currently only turned on for drafts
174 autosaveLast = jQuery("#title").val()+jQuery("#content").val();
176 jQuery("[@name='post_category[]']:checked").each( function(i) {
177 goodcats.push(this.value);
179 post_data["catslist"] = goodcats.join(",");
181 if ( jQuery("#comment_status").attr("checked") )
182 post_data["comment_status"] = 'open';
183 if ( jQuery("#ping_status").attr("checked") )
184 post_data["ping_status"] = 'open';
185 if ( jQuery("#excerpt") )
186 post_data["excerpt"] = jQuery("#excerpt").val();
187 if ( jQuery("#post_author") )
188 post_data["post_author"] = jQuery("#post_author").val();
190 // Don't run while the TinyMCE spellcheck is on. Why? Who knows.
191 if ( rich && tinyMCE.activeEditor.plugins.spellchecker && tinyMCE.activeEditor.plugins.spellchecker.active ) {
195 if(parseInt(post_data["post_ID"]) < 1) {
196 post_data["temp_ID"] = post_data["post_ID"];
197 var successCallback = autosave_saved_new;; // new post
199 var successCallback = autosave_saved; // pre-existing post
203 post_data['autosave'] = 0;
206 autosaveOldMessage = jQuery('#autosave').html();
210 beforeSend: doAutoSave ? autosave_loading : null,
212 url: autosaveL10n.requestFile,
213 success: successCallback