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 ( $('#auto_draft').val() == '1' && 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 ( ($('#auto_draft').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 ( '1' == $('#auto_draft').val() ) {
74 $('#title').blur( function() {
75 if ( !this.value || $('#auto_draft').val() != '1' )
82 function autosave_parse_response(response) {
83 var res = wpAjax.parseAjaxResponse(response, 'autosave'), message = '', postID, sup, url;
85 if ( res && res.responses && res.responses.length ) {
86 message = res.responses[0].data; // The saved message or error.
87 // someone else is editing: disable autosave, set errors
88 if ( res.responses[0].supplemental ) {
89 sup = res.responses[0].supplemental;
90 if ( 'disable' == sup['disable_autosave'] ) {
91 autosave = function() {};
92 res = { errors: true };
94 if ( sup['session_expired'] && (url = sup['session_expired']) ) {
95 if ( !interimLogin || interimLogin.closed ) {
96 interimLogin = window.open(url, 'login', 'width=600,height=450,resizable=yes,scrollbars=yes,status=yes');
99 delete sup['session_expired'];
101 jQuery.each(sup, function(selector, value) {
102 if ( selector.match(/^replace-/) ) {
103 jQuery('#'+selector.replace('replace-', '')).val(value);
108 // if no errors: add slug UI
110 postID = parseInt( res.responses[0].id, 10 );
111 if ( !isNaN(postID) && postID > 0 ) {
112 autosave_update_slug(postID);
116 if ( message ) { jQuery('#autosave').html(message); } // update autosave message
117 else if ( autosaveOldMessage && res ) { jQuery('#autosave').html( autosaveOldMessage ); }
121 // called when autosaving pre-existing post
122 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) {
131 var res = autosave_parse_response(response), tempID, postID;
132 if ( res && res.responses.length && !res.errors ) {
133 // An ID is sent only for real auto-saves, not for autosave=0 "keepalive" saves
134 postID = parseInt( res.responses[0].id, 10 );
135 if ( !isNaN(postID) && postID > 0 ) {
137 jQuery('#auto_draft').val('0'); // No longer an auto-draft
139 autosave_enable_buttons();
140 if ( autosaveDelayPreview ) {
141 autosaveDelayPreview = false;
145 autosave_enable_buttons(); // re-enable disabled form buttons
149 function autosave_update_slug(post_id) {
150 // create slug area only if not already there
151 if ( 'undefined' != makeSlugeditClickable && jQuery.isFunction(makeSlugeditClickable) && !jQuery('#edit-slug-box > *').size() ) {
155 action: 'sample-permalink',
157 new_title: jQuery('#title').val(),
158 samplepermalinknonce: jQuery('#samplepermalinknonce').val()
161 jQuery('#edit-slug-box').html(data);
162 makeSlugeditClickable();
168 function autosave_loading() {
169 jQuery('#autosave').html(autosaveL10n.savingText);
172 function autosave_enable_buttons() {
173 // delay that a bit to avoid some rare collisions while the DOM is being updated.
174 setTimeout(function(){
175 jQuery(':button, :submit', '#submitpost').removeAttr('disabled');
176 jQuery('#ajax-loading').css('visibility', 'hidden');
180 function autosave_disable_buttons() {
181 jQuery(':button, :submit', '#submitpost').attr('disabled', 'disabled');
182 // Re-enable 5 sec later. Just gives autosave a head start to avoid collisions.
183 setTimeout(autosave_enable_buttons, 5000);
186 function delayed_autosave() {
187 setTimeout(function(){
194 autosave = function() {
195 // (bool) is rich editor enabled and active
197 var rich = (typeof tinyMCE != "undefined") && tinyMCE.activeEditor && !tinyMCE.activeEditor.isHidden(), post_data, doAutoSave, ed, origStatus, successCallback;
199 autosave_disable_buttons();
203 post_ID: jQuery("#post_ID").val() || 0,
204 post_title: jQuery("#title").val() || "",
205 autosavenonce: jQuery('#autosavenonce').val(),
206 post_type: jQuery('#post_type').val() || "",
210 jQuery('.tags-input').each( function() {
211 post_data[this.name] = this.value;
214 // We always send the ajax request in order to keep the post lock fresh.
215 // This (bool) tells whether or not to write the post to the DB during the ajax request.
218 // No autosave while thickbox is open (media buttons)
219 if ( jQuery("#TB_window").css('display') == 'block' )
222 /* Gotta do this up here so we can check the length when tinyMCE is in use */
223 if ( rich && doAutoSave ) {
224 ed = tinyMCE.activeEditor;
225 // Don't run while the TinyMCE spellcheck is on. It resets all found words.
226 if ( ed.plugins.spellchecker && ed.plugins.spellchecker.active ) {
229 if ( 'mce_fullscreen' == ed.id )
230 tinyMCE.get('content').setContent(ed.getContent({format : 'raw'}), {format : 'raw'});
231 tinyMCE.get('content').save();
235 post_data["content"] = jQuery("#content").val();
236 if ( jQuery('#post_name').val() )
237 post_data["post_name"] = jQuery('#post_name').val();
239 // Nothing to save or no change.
240 if ( ( post_data["post_title"].length == 0 && post_data["content"].length == 0 ) || post_data["post_title"] + post_data["content"] == autosaveLast ) {
244 origStatus = jQuery('#original_post_status').val();
247 jQuery("[name='post_category[]']:checked").each( function(i) {
248 goodcats.push(this.value);
250 post_data["catslist"] = goodcats.join(",");
252 if ( jQuery("#comment_status").attr("checked") )
253 post_data["comment_status"] = 'open';
254 if ( jQuery("#ping_status").attr("checked") )
255 post_data["ping_status"] = 'open';
256 if ( jQuery("#excerpt").size() )
257 post_data["excerpt"] = jQuery("#excerpt").val();
258 if ( jQuery("#post_author").size() )
259 post_data["post_author"] = jQuery("#post_author").val();
260 if ( jQuery("#parent_id").val() )
261 post_data["parent_id"] = jQuery("#parent_id").val();
262 post_data["user_ID"] = jQuery("#user-id").val();
263 if ( jQuery('#auto_draft').val() == '1' )
264 post_data["auto_draft"] = '1';
267 autosaveLast = jQuery("#title").val() + jQuery("#content").val();
269 post_data['autosave'] = 0;
272 if ( post_data["auto_draft"] == '1' ) {
273 successCallback = autosave_saved_new; // new post
275 successCallback = autosave_saved; // pre-existing post
278 autosaveOldMessage = jQuery('#autosave').html();
281 beforeSend: doAutoSave ? autosave_loading : null,
283 url: autosaveL10n.requestFile,
284 success: successCallback