1 var autosave, autosaveLast = '', autosavePeriodical, autosaveOldMessage = '', autosaveDelayPreview = false, notSaved = true, blockSave = false, interimLogin = false;
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 $('#ajax-loading').css('visibility', 'visible');
27 window.onbeforeunload = function(){
28 var mce = typeof(tinyMCE) != 'undefined' ? tinyMCE.activeEditor : false, title, content;
30 if ( mce && !mce.isHidden() ) {
32 return autosaveL10n.saveAlert;
34 title = $('#post #title').val(), content = $('#post #content').val();
35 if ( ( title || content ) && title + content != autosaveLast )
36 return autosaveL10n.saveAlert;
41 $('#post-preview').click(function(){
42 if ( 1 > $('#post_ID').val() && notSaved ) {
43 autosaveDelayPreview = true;
51 doPreview = function() {
52 $('input#wp-preview').val('dopreview');
53 $('form#post').attr('target', 'wp-preview').submit().attr('target', '');
54 $('input#wp-preview').val('');
57 // This code is meant to allow tabbing from Title to Post if tinyMCE is defined.
58 if ( typeof tinyMCE != 'undefined' ) {
59 $('#title')[$.browser.opera ? 'keypress' : 'keydown'](function (e) {
60 if ( e.which == 9 && !e.shiftKey && !e.controlKey && !e.altKey ) {
61 if ( ($("#post_ID").val() < 1) && ($("#title").val().length > 0) ) { autosave(); }
62 if ( tinyMCE.activeEditor && ! tinyMCE.activeEditor.isHidden() && dotabkey ) {
65 tinyMCE.activeEditor.focus();
72 // autosave new posts after a title is typed but not if Publish or Save Draft is clicked
73 if ( 0 > $('#post_ID').val() ) {
74 $('#title').blur( function() {
75 if ( !this.value || 0 < $('#post_ID').val() )
83 function autosave_parse_response(response) {
84 var res = wpAjax.parseAjaxResponse(response, 'autosave'), message = '', postID, sup, url;
86 if ( res && res.responses && res.responses.length ) {
87 message = res.responses[0].data; // The saved message or error.
88 // someone else is editing: disable autosave, set errors
89 if ( res.responses[0].supplemental ) {
90 sup = res.responses[0].supplemental;
91 if ( 'disable' == sup['disable_autosave'] ) {
92 autosave = function() {};
93 res = { errors: true };
95 if ( sup['session_expired'] && (url = sup['session_expired']) ) {
96 if ( !interimLogin || interimLogin.closed ) {
97 interimLogin = window.open(url, 'login', 'width=600,height=450,resizable=yes,scrollbars=yes,status=yes');
100 delete sup['session_expired'];
102 jQuery.each(sup, function(selector, value) {
103 if ( selector.match(/^replace-/) ) {
104 jQuery('#'+selector.replace('replace-', '')).val(value);
109 // if no errors: add slug UI
111 postID = parseInt( res.responses[0].id, 10 );
112 if ( !isNaN(postID) && postID > 0 ) {
113 autosave_update_slug(postID);
117 if ( message ) { jQuery('#autosave').html(message); } // update autosave message
118 else if ( autosaveOldMessage && res ) { jQuery('#autosave').html( autosaveOldMessage ); }
122 // called when autosaving pre-existing post
123 function autosave_saved(response) {
124 autosave_parse_response(response); // parse the ajax response
125 autosave_enable_buttons(); // re-enable disabled form buttons
128 // called when autosaving new post
129 function autosave_saved_new(response) {
130 var res = autosave_parse_response(response), tempID, postID;
131 // if no errors: update post_ID from the temporary value, grab new save-nonce for that new ID
132 if ( res && res.responses.length && !res.errors ) {
133 tempID = jQuery('#post_ID').val();
134 postID = parseInt( res.responses[0].id, 10 );
135 autosave_update_post_ID( postID ); // disabled form buttons are re-enabled here
136 if ( tempID < 0 && postID > 0 ) { // update media buttons
138 jQuery('#media-buttons a').each(function(){
139 this.href = this.href.replace(tempID, postID);
142 if ( autosaveDelayPreview ) {
143 autosaveDelayPreview = false;
147 autosave_enable_buttons(); // re-enable disabled form buttons
151 function autosave_update_post_ID( postID ) {
152 if ( !isNaN(postID) && postID > 0 ) {
153 if ( postID == parseInt(jQuery('#post_ID').val(), 10) ) { return; } // no need to do this more than once
154 jQuery('#post_ID').attr({name: "post_ID"});
155 jQuery('#post_ID').val(postID);
156 // We need new nonces
157 jQuery.post(autosaveL10n.requestFile, {
158 action: "autosave-generate-nonces",
160 autosavenonce: jQuery('#autosavenonce').val(),
161 post_type: jQuery('#post_type').val()
163 jQuery('#_wpnonce').val(html.updateNonce);
164 jQuery('#delete-action a.submitdelete').attr('href', html.deleteURL);
165 autosave_enable_buttons(); // re-enable disabled form buttons
166 jQuery('#delete-action a.submitdelete').fadeIn();
169 jQuery('#hiddenaction').val('editpost');
173 function autosave_update_slug(post_id) {
174 // create slug area only if not already there
175 if ( 'undefined' != makeSlugeditClickable && jQuery.isFunction(makeSlugeditClickable) && !jQuery('#edit-slug-box > *').size() ) {
179 action: 'sample-permalink',
181 new_title: jQuery('#title').val(),
182 samplepermalinknonce: jQuery('#samplepermalinknonce').val()
185 jQuery('#edit-slug-box').html(data);
186 makeSlugeditClickable();
192 function autosave_loading() {
193 jQuery('#autosave').html(autosaveL10n.savingText);
196 function autosave_enable_buttons() {
197 // delay that a bit to avoid some rare collisions while the DOM is being updated.
198 setTimeout(function(){
199 jQuery(':button, :submit', '#submitpost').removeAttr('disabled');
200 jQuery('#ajax-loading').css('visibility', 'hidden');
204 function autosave_disable_buttons() {
205 jQuery(':button, :submit', '#submitpost').attr('disabled', 'disabled');
206 // Re-enable 5 sec later. Just gives autosave a head start to avoid collisions.
207 setTimeout(autosave_enable_buttons, 5000);
210 function delayed_autosave() {
211 setTimeout(function(){
218 autosave = function() {
219 // (bool) is rich editor enabled and active
220 var rich = (typeof tinyMCE != "undefined") && tinyMCE.activeEditor && !tinyMCE.activeEditor.isHidden(), post_data, doAutoSave, ed, origStatus, successCallback;
222 autosave_disable_buttons();
226 post_ID: jQuery("#post_ID").val() || 0,
227 post_title: jQuery("#title").val() || "",
228 autosavenonce: jQuery('#autosavenonce').val(),
229 post_type: jQuery('#post_type').val() || "",
233 jQuery('.tags-input').each( function() {
234 post_data[this.name] = this.value;
237 // We always send the ajax request in order to keep the post lock fresh.
238 // This (bool) tells whether or not to write the post to the DB during the ajax request.
241 // No autosave while thickbox is open (media buttons)
242 if ( jQuery("#TB_window").css('display') == 'block' )
245 /* Gotta do this up here so we can check the length when tinyMCE is in use */
246 if ( rich && doAutoSave ) {
247 ed = tinyMCE.activeEditor;
248 // Don't run while the TinyMCE spellcheck is on. It resets all found words.
249 if ( ed.plugins.spellchecker && ed.plugins.spellchecker.active ) {
252 if ( 'mce_fullscreen' == ed.id )
253 tinyMCE.get('content').setContent(ed.getContent({format : 'raw'}), {format : 'raw'});
254 tinyMCE.get('content').save();
258 post_data["content"] = jQuery("#content").val();
259 if ( jQuery('#post_name').val() )
260 post_data["post_name"] = jQuery('#post_name').val();
262 // Nothing to save or no change.
263 if ( ( post_data["post_title"].length == 0 && post_data["content"].length == 0 ) || post_data["post_title"] + post_data["content"] == autosaveLast ) {
267 origStatus = jQuery('#original_post_status').val();
270 jQuery("[name='post_category[]']:checked").each( function(i) {
271 goodcats.push(this.value);
273 post_data["catslist"] = goodcats.join(",");
275 if ( jQuery("#comment_status").attr("checked") )
276 post_data["comment_status"] = 'open';
277 if ( jQuery("#ping_status").attr("checked") )
278 post_data["ping_status"] = 'open';
279 if ( jQuery("#excerpt").size() )
280 post_data["excerpt"] = jQuery("#excerpt").val();
281 if ( jQuery("#post_author").size() )
282 post_data["post_author"] = jQuery("#post_author").val();
283 post_data["user_ID"] = jQuery("#user-id").val();
286 autosaveLast = jQuery("#title").val()+jQuery("#content").val();
288 post_data['autosave'] = 0;
291 if ( parseInt(post_data["post_ID"], 10) < 1 ) {
292 post_data["temp_ID"] = post_data["post_ID"];
293 successCallback = autosave_saved_new; // new post
295 successCallback = autosave_saved; // pre-existing post
298 autosaveOldMessage = jQuery('#autosave').html();
302 beforeSend: doAutoSave ? autosave_loading : null,
304 url: autosaveL10n.requestFile,
305 success: successCallback