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 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 title = $('#post #title').val(), content = $('#post #content').val();
38 if ( ( title || content ) && title + content != autosaveLast )
39 return autosaveL10n.saveAlert;
44 $('#post-preview').click(function(){
45 if ( $('#auto_draft').val() == '1' && notSaved ) {
46 autosaveDelayPreview = true;
54 doPreview = function() {
55 $('input#wp-preview').val('dopreview');
56 $('form#post').attr('target', 'wp-preview').submit().attr('target', '');
57 $('input#wp-preview').val('');
60 // This code is meant to allow tabbing from Title to Post if tinyMCE is defined.
61 if ( typeof tinyMCE != 'undefined' ) {
62 $('#title')[$.browser.opera ? 'keypress' : 'keydown'](function (e) {
63 if ( e.which == 9 && !e.shiftKey && !e.controlKey && !e.altKey ) {
64 if ( ($('#auto_draft').val() == '1') && ($("#title").val().length > 0) ) { autosave(); }
65 if ( tinyMCE.activeEditor && ! tinyMCE.activeEditor.isHidden() && dotabkey ) {
68 tinyMCE.activeEditor.focus();
75 // autosave new posts after a title is typed but not if Publish or Save Draft is clicked
76 if ( '1' == $('#auto_draft').val() ) {
77 $('#title').blur( function() {
78 if ( !this.value || $('#auto_draft').val() != '1' )
85 function autosave_parse_response(response) {
86 var res = wpAjax.parseAjaxResponse(response, 'autosave'), message = '', postID, sup, url;
88 if ( res && res.responses && res.responses.length ) {
89 message = res.responses[0].data; // The saved message or error.
90 // someone else is editing: disable autosave, set errors
91 if ( res.responses[0].supplemental ) {
92 sup = res.responses[0].supplemental;
93 if ( 'disable' == sup['disable_autosave'] ) {
94 autosave = function() {};
95 res = { errors: true };
97 if ( sup['session_expired'] && (url = sup['session_expired']) ) {
98 if ( !interimLogin || interimLogin.closed ) {
99 interimLogin = window.open(url, 'login', 'width=600,height=450,resizable=yes,scrollbars=yes,status=yes');
100 interimLogin.focus();
102 delete sup['session_expired'];
104 jQuery.each(sup, function(selector, value) {
105 if ( selector.match(/^replace-/) ) {
106 jQuery('#'+selector.replace('replace-', '')).val(value);
111 // if no errors: add slug UI
113 postID = parseInt( res.responses[0].id, 10 );
114 if ( !isNaN(postID) && postID > 0 ) {
115 autosave_update_slug(postID);
119 if ( message ) { jQuery('#autosave').html(message); } // update autosave message
120 else if ( autosaveOldMessage && res ) { jQuery('#autosave').html( autosaveOldMessage ); }
124 // called when autosaving pre-existing post
125 function autosave_saved(response) {
127 autosave_parse_response(response); // parse the ajax response
128 autosave_enable_buttons(); // re-enable disabled form buttons
131 // called when autosaving new post
132 function autosave_saved_new(response) {
134 var res = autosave_parse_response(response), tempID, postID;
135 if ( res && res.responses.length && !res.errors ) {
136 // An ID is sent only for real auto-saves, not for autosave=0 "keepalive" saves
137 postID = parseInt( res.responses[0].id, 10 );
138 if ( !isNaN(postID) && postID > 0 ) {
140 jQuery('#auto_draft').val('0'); // No longer an auto-draft
142 autosave_enable_buttons();
143 if ( autosaveDelayPreview ) {
144 autosaveDelayPreview = false;
148 autosave_enable_buttons(); // re-enable disabled form buttons
152 function autosave_update_slug(post_id) {
153 // create slug area only if not already there
154 if ( 'undefined' != makeSlugeditClickable && jQuery.isFunction(makeSlugeditClickable) && !jQuery('#edit-slug-box > *').size() ) {
158 action: 'sample-permalink',
160 new_title: jQuery('#title').val(),
161 samplepermalinknonce: jQuery('#samplepermalinknonce').val()
164 jQuery('#edit-slug-box').html(data);
165 makeSlugeditClickable();
171 function autosave_loading() {
172 jQuery('#autosave').html(autosaveL10n.savingText);
175 function autosave_enable_buttons() {
176 // delay that a bit to avoid some rare collisions while the DOM is being updated.
177 setTimeout(function(){
178 jQuery(':button, :submit', '#submitpost').removeAttr('disabled');
179 jQuery('.ajax-loading').css('visibility', 'hidden');
183 function autosave_disable_buttons() {
184 jQuery(':button, :submit', '#submitpost').attr('disabled', 'disabled');
185 // Re-enable 5 sec later. Just gives autosave a head start to avoid collisions.
186 setTimeout(autosave_enable_buttons, 5000);
189 function delayed_autosave() {
190 setTimeout(function(){
197 autosave = function() {
198 // (bool) is rich editor enabled and active
200 var rich = (typeof tinyMCE != "undefined") && tinyMCE.activeEditor && !tinyMCE.activeEditor.isHidden(), post_data, doAutoSave, ed, origStatus, successCallback;
202 autosave_disable_buttons();
206 post_ID: jQuery("#post_ID").val() || 0,
207 post_title: jQuery("#title").val() || "",
208 autosavenonce: jQuery('#autosavenonce').val(),
209 post_type: jQuery('#post_type').val() || "",
213 jQuery('.tags-input').each( function() {
214 post_data[this.name] = this.value;
217 // We always send the ajax request in order to keep the post lock fresh.
218 // This (bool) tells whether or not to write the post to the DB during the ajax request.
221 // No autosave while thickbox is open (media buttons)
222 if ( jQuery("#TB_window").css('display') == 'block' )
225 /* Gotta do this up here so we can check the length when tinyMCE is in use */
226 if ( rich && doAutoSave ) {
227 ed = tinyMCE.activeEditor;
228 // Don't run while the TinyMCE spellcheck is on. It resets all found words.
229 if ( ed.plugins.spellchecker && ed.plugins.spellchecker.active ) {
232 if ( 'mce_fullscreen' == ed.id )
233 tinyMCE.get('content').setContent(ed.getContent({format : 'raw'}), {format : 'raw'});
234 tinyMCE.get('content').save();
238 post_data["content"] = jQuery("#content").val();
239 if ( jQuery('#post_name').val() )
240 post_data["post_name"] = jQuery('#post_name').val();
242 // Nothing to save or no change.
243 if ( ( post_data["post_title"].length == 0 && post_data["content"].length == 0 ) || post_data["post_title"] + post_data["content"] == autosaveLast ) {
247 origStatus = jQuery('#original_post_status').val();
250 jQuery("[name='post_category[]']:checked").each( function(i) {
251 goodcats.push(this.value);
253 post_data["catslist"] = goodcats.join(",");
255 if ( jQuery("#comment_status").attr("checked") )
256 post_data["comment_status"] = 'open';
257 if ( jQuery("#ping_status").attr("checked") )
258 post_data["ping_status"] = 'open';
259 if ( jQuery("#excerpt").size() )
260 post_data["excerpt"] = jQuery("#excerpt").val();
261 if ( jQuery("#post_author").size() )
262 post_data["post_author"] = jQuery("#post_author").val();
263 if ( jQuery("#parent_id").val() )
264 post_data["parent_id"] = jQuery("#parent_id").val();
265 post_data["user_ID"] = jQuery("#user-id").val();
266 if ( jQuery('#auto_draft').val() == '1' )
267 post_data["auto_draft"] = '1';
270 autosaveLast = jQuery("#title").val() + jQuery("#content").val();
272 post_data['autosave'] = 0;
275 if ( post_data["auto_draft"] == '1' ) {
276 successCallback = autosave_saved_new; // new post
278 successCallback = autosave_saved; // pre-existing post
281 autosaveOldMessage = jQuery('#autosave').html();
284 beforeSend: doAutoSave ? autosave_loading : null,
286 url: autosaveL10n.requestFile,
287 success: successCallback