1 var autosave, autosaveLast = '', autosavePeriodical, autosaveOldMessage = '', autosaveDelayPreview = false, notSaved = true;
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() { $.cancel(autosavePeriodical); });
12 $('.submitbox input[type="submit"], .submitbox a.submitdelete').click(function(){window.onbeforeunload = null;});
14 window.onbeforeunload = function(){
15 var mce = typeof(tinyMCE) != 'undefined' ? tinyMCE.activeEditor : false, title, content;
17 if ( mce && !mce.isHidden() ) {
19 return autosaveL10n.saveAlert;
21 title = $('#post #title').val(), content = $('#post #content').val();
22 if ( ( title || content ) && title + content != autosaveLast )
23 return autosaveL10n.saveAlert;
28 $('#post-preview').click(function(){
29 if ( 1 > $('#post_ID').val() && notSaved ) {
30 autosaveDelayPreview = true;
38 doPreview = function() {
39 $('input#wp-preview').val('dopreview');
40 $('form#post').attr('target', 'wp-preview').submit().attr('target', '');
41 $('input#wp-preview').val('');
44 // This code is meant to allow tabbing from Title to Post if tinyMCE is defined.
45 if ( typeof tinyMCE != 'undefined' ) {
46 $('#title')[$.browser.opera ? 'keypress' : 'keydown'](function (e) {
47 if ( e.which == 9 && !e.shiftKey && !e.controlKey && !e.altKey ) {
48 if ( ($("#post_ID").val() < 1) && ($("#title").val().length > 0) ) { autosave(); }
49 if ( tinyMCE.activeEditor && ! tinyMCE.activeEditor.isHidden() && dotabkey ) {
52 tinyMCE.activeEditor.focus();
60 function autosave_parse_response(response) {
61 var res = wpAjax.parseAjaxResponse(response, 'autosave'), message = '', postID;
63 if ( res && res.responses && res.responses.length ) {
64 message = res.responses[0].data; // The saved message or error.
65 // someone else is editing: disable autosave, set errors
66 if ( res.responses[0].supplemental ) {
67 if ( 'disable' == res.responses[0].supplemental['disable_autosave'] ) {
68 autosave = function() {};
69 res = { errors: true };
71 jQuery.each(res.responses[0].supplemental, function(selector, value) {
72 if ( selector.match(/^replace-/) ) {
73 jQuery('#'+selector.replace('replace-', '')).val(value);
78 // if no errors: add slug UI
80 postID = parseInt( res.responses[0].id, 10 );
81 if ( !isNaN(postID) && postID > 0 ) {
82 autosave_update_slug(postID);
86 if ( message ) { jQuery('#autosave').html(message); } // update autosave message
87 else if ( autosaveOldMessage && res ) { jQuery('#autosave').html( autosaveOldMessage ); }
91 // called when autosaving pre-existing post
92 function autosave_saved(response) {
93 autosave_parse_response(response); // parse the ajax response
94 autosave_enable_buttons(); // re-enable disabled form buttons
97 // called when autosaving new post
98 function autosave_saved_new(response) {
99 var res = autosave_parse_response(response), tempID, postID;
100 // if no errors: update post_ID from the temporary value, grab new save-nonce for that new ID
101 if ( res && res.responses.length && !res.errors ) {
102 tempID = jQuery('#post_ID').val();
103 postID = parseInt( res.responses[0].id, 10 );
104 autosave_update_post_ID( postID ); // disabled form buttons are re-enabled here
105 if ( tempID < 0 && postID > 0 ) { // update media buttons
107 jQuery('#media-buttons a').each(function(){
108 this.href = this.href.replace(tempID, postID);
111 if ( autosaveDelayPreview ) {
112 autosaveDelayPreview = false;
116 autosave_enable_buttons(); // re-enable disabled form buttons
120 function autosave_update_post_ID( postID ) {
121 if ( !isNaN(postID) && postID > 0 ) {
122 if ( postID == parseInt(jQuery('#post_ID').val(), 10) ) { return; } // no need to do this more than once
123 jQuery('#post_ID').attr({name: "post_ID"});
124 jQuery('#post_ID').val(postID);
125 // We need new nonces
126 jQuery.post(autosaveL10n.requestFile, {
127 action: "autosave-generate-nonces",
129 autosavenonce: jQuery('#autosavenonce').val(),
130 post_type: jQuery('#post_type').val()
132 jQuery('#_wpnonce').val(html);
133 autosave_enable_buttons(); // re-enable disabled form buttons
135 jQuery('#hiddenaction').val('editpost');
139 function autosave_update_slug(post_id) {
140 // create slug area only if not already there
141 if ( jQuery.isFunction(make_slugedit_clickable) && !jQuery('#edit-slug-box > *').size() ) {
143 slugL10n.requestFile,
145 action: 'sample-permalink',
147 new_title: jQuery('#title').val(),
148 samplepermalinknonce: jQuery('#samplepermalinknonce').val()
151 jQuery('#edit-slug-box').html(data);
152 make_slugedit_clickable();
158 function autosave_loading() {
159 jQuery('#autosave').html(autosaveL10n.savingText);
162 function autosave_enable_buttons() {
163 jQuery(".submitbox :button:disabled, .submitbox :submit:disabled").attr('disabled', '');
166 function autosave_disable_buttons() {
167 jQuery(".submitbox :button:enabled, .submitbox :submit:enabled").attr('disabled', 'disabled');
168 // Re-enable 5 sec later. Just gives autosave a head start to avoid collisions.
169 setTimeout(autosave_enable_buttons, 5000);
172 autosave = function() {
173 // (bool) is rich editor enabled and active
174 var rich = (typeof tinyMCE != "undefined") && tinyMCE.activeEditor && !tinyMCE.activeEditor.isHidden(), post_data, doAutoSave, ed, origStatus, successCallback;
178 post_ID: jQuery("#post_ID").val() || 0,
179 post_title: jQuery("#title").val() || "",
180 autosavenonce: jQuery('#autosavenonce').val(),
181 //tags_input: jQuery("#tags-input").val() || "",
182 post_type: jQuery('#post_type').val() || "",
186 jQuery('.tags-input').each( function() {
187 post_data[this.name] = this.value;
190 // We always send the ajax request in order to keep the post lock fresh.
191 // This (bool) tells whether or not to write the post to the DB during the ajax request.
194 // No autosave while thickbox is open (media buttons)
195 if ( jQuery("#TB_window").css('display') == 'block' )
198 /* Gotta do this up here so we can check the length when tinyMCE is in use */
200 ed = tinyMCE.activeEditor;
201 if ( 'mce_fullscreen' == ed.id )
202 tinyMCE.get('content').setContent(ed.getContent({format : 'raw'}), {format : 'raw'});
203 tinyMCE.get('content').save();
206 post_data["content"] = jQuery("#content").val();
207 if ( jQuery('#post_name').val() )
208 post_data["post_name"] = jQuery('#post_name').val();
210 // Nothing to save or no change.
211 if( ( post_data["post_title"].length == 0 && post_data["content"].length == 0 ) || post_data["post_title"] + post_data["content"] == autosaveLast) {
215 autosave_disable_buttons();
217 origStatus = jQuery('#original_post_status').val();
219 autosaveLast = jQuery("#title").val()+jQuery("#content").val();
221 jQuery("[name='post_category[]']:checked").each( function(i) {
222 goodcats.push(this.value);
224 post_data["catslist"] = goodcats.join(",");
226 if ( jQuery("#comment_status").attr("checked") )
227 post_data["comment_status"] = 'open';
228 if ( jQuery("#ping_status").attr("checked") )
229 post_data["ping_status"] = 'open';
230 if ( jQuery("#excerpt").size() )
231 post_data["excerpt"] = jQuery("#excerpt").val();
232 if ( jQuery("#post_author").size() )
233 post_data["post_author"] = jQuery("#post_author").val();
234 post_data["user_ID"] = jQuery("#user-id").val();
236 // Don't run while the TinyMCE spellcheck is on. It resets all found words.
237 if ( rich && tinyMCE.activeEditor.plugins.spellchecker && tinyMCE.activeEditor.plugins.spellchecker.active ) {
241 if ( parseInt(post_data["post_ID"], 10) < 1 ) {
242 post_data["temp_ID"] = post_data["post_ID"];
243 successCallback = autosave_saved_new; // new post
245 successCallback = autosave_saved; // pre-existing post
249 post_data['autosave'] = 0;
252 autosaveOldMessage = jQuery('#autosave').html();
256 beforeSend: doAutoSave ? autosave_loading : null,
258 url: autosaveL10n.requestFile,
259 success: successCallback