| [ Index ] |
PHP Cross Reference of WordPress |
[Summary view] [Print] [Text view]
1 var autosave, autosaveLast = '', autosavePeriodical, autosaveOldMessage = '', autosaveDelayPreview = false, notSaved = true, blockSave = false, fullscreen, autosaveLockRelease = true; 2 3 jQuery(document).ready( function($) { 4 var dotabkey = true; 5 6 autosaveLast = $('#post #title').val() + $('#post #content').val(); 7 autosavePeriodical = $.schedule({time: autosaveL10n.autosaveInterval * 1000, func: function() { autosave(); }, repeat: true, protect: true}); 8 9 //Disable autosave after the form has been submitted 10 $("#post").submit(function() { 11 $.cancel(autosavePeriodical); 12 autosaveLockRelease = false; 13 }); 14 15 $('input[type="submit"], a.submitdelete', '#submitpost').click(function(){ 16 blockSave = true; 17 window.onbeforeunload = null; 18 $(':button, :submit', '#submitpost').each(function(){ 19 var t = $(this); 20 if ( t.hasClass('button-primary') ) 21 t.addClass('button-primary-disabled'); 22 else 23 t.addClass('button-disabled'); 24 }); 25 if ( $(this).attr('id') == 'publish' ) 26 $('#ajax-loading').css('visibility', 'visible'); 27 else 28 $('#draft-ajax-loading').css('visibility', 'visible'); 29 }); 30 31 window.onbeforeunload = function(){ 32 var mce = typeof(tinyMCE) != 'undefined' ? tinyMCE.activeEditor : false, title, content; 33 34 if ( mce && !mce.isHidden() ) { 35 if ( mce.isDirty() ) 36 return autosaveL10n.saveAlert; 37 } else { 38 if ( fullscreen && fullscreen.settings.visible ) { 39 title = $('#wp-fullscreen-title').val(); 40 content = $("#wp_mce_fullscreen").val(); 41 } else { 42 title = $('#post #title').val(); 43 content = $('#post #content').val(); 44 } 45 46 if ( ( title || content ) && title + content != autosaveLast ) 47 return autosaveL10n.saveAlert; 48 } 49 }; 50 51 $(window).unload( function(e) { 52 if ( ! autosaveLockRelease ) 53 return; 54 55 // unload fires (twice) on removing the Thickbox iframe. Make sure we process only the main document unload. 56 if ( e.target && e.target.nodeName != '#document' ) 57 return; 58 59 $.ajax({ 60 type: 'POST', 61 url: ajaxurl, 62 async: false, 63 data: { 64 action: 'wp-remove-post-lock', 65 _wpnonce: $('#_wpnonce').val(), 66 post_ID: $('#post_ID').val(), 67 active_post_lock: $('#active_post_lock').val() 68 } 69 }); 70 } ); 71 72 // preview 73 $('#post-preview').click(function(){ 74 if ( $('#auto_draft').val() == '1' && notSaved ) { 75 autosaveDelayPreview = true; 76 autosave(); 77 return false; 78 } 79 doPreview(); 80 return false; 81 }); 82 83 doPreview = function() { 84 $('input#wp-preview').val('dopreview'); 85 $('form#post').attr('target', 'wp-preview').submit().attr('target', ''); 86 87 /* 88 * Workaround for WebKit bug preventing a form submitting twice to the same action. 89 * https://bugs.webkit.org/show_bug.cgi?id=28633 90 */ 91 if ( $.browser.safari ) { 92 $('form#post').attr('action', function(index, value) { 93 return value + '?t=' + new Date().getTime(); 94 }); 95 } 96 97 $('input#wp-preview').val(''); 98 } 99 100 // This code is meant to allow tabbing from Title to Post if tinyMCE is defined. 101 if ( typeof tinyMCE != 'undefined' ) { 102 $('#title')[$.browser.opera ? 'keypress' : 'keydown'](function (e) { 103 if ( e.which == 9 && !e.shiftKey && !e.controlKey && !e.altKey ) { 104 if ( ($('#auto_draft').val() == '1') && ($("#title").val().length > 0) ) { autosave(); } 105 if ( tinyMCE.activeEditor && ! tinyMCE.activeEditor.isHidden() && dotabkey ) { 106 e.preventDefault(); 107 dotabkey = false; 108 tinyMCE.activeEditor.focus(); 109 return false; 110 } 111 } 112 }); 113 } 114 115 // autosave new posts after a title is typed but not if Publish or Save Draft is clicked 116 if ( '1' == $('#auto_draft').val() ) { 117 $('#title').blur( function() { 118 if ( !this.value || $('#auto_draft').val() != '1' ) 119 return; 120 delayed_autosave(); 121 }); 122 } 123 }); 124 125 function autosave_parse_response(response) { 126 var res = wpAjax.parseAjaxResponse(response, 'autosave'), message = '', postID, sup; 127 128 if ( res && res.responses && res.responses.length ) { 129 message = res.responses[0].data; // The saved message or error. 130 // someone else is editing: disable autosave, set errors 131 if ( res.responses[0].supplemental ) { 132 sup = res.responses[0].supplemental; 133 if ( 'disable' == sup['disable_autosave'] ) { 134 autosave = function() {}; 135 autosaveLockRelease = false; 136 res = { errors: true }; 137 } 138 139 if ( sup['active-post-lock'] ) { 140 jQuery('#active_post_lock').val( sup['active-post-lock'] ); 141 } 142 143 if ( sup['alert'] ) { 144 jQuery('#autosave-alert').remove(); 145 jQuery('#titlediv').after('<div id="autosave-alert" class="error below-h2"><p>' + sup['alert'] + '</p></div>'); 146 } 147 148 jQuery.each(sup, function(selector, value) { 149 if ( selector.match(/^replace-/) ) { 150 jQuery('#'+selector.replace('replace-', '')).val(value); 151 } 152 }); 153 } 154 155 // if no errors: add slug UI 156 if ( !res.errors ) { 157 postID = parseInt( res.responses[0].id, 10 ); 158 if ( !isNaN(postID) && postID > 0 ) { 159 autosave_update_slug(postID); 160 } 161 } 162 } 163 if ( message ) { // update autosave message 164 jQuery('.autosave-message').html(message); 165 } else if ( autosaveOldMessage && res ) { 166 jQuery('.autosave-message').html( autosaveOldMessage ); 167 } 168 return res; 169 } 170 171 // called when autosaving pre-existing post 172 function autosave_saved(response) { 173 blockSave = false; 174 autosave_parse_response(response); // parse the ajax response 175 autosave_enable_buttons(); // re-enable disabled form buttons 176 } 177 178 // called when autosaving new post 179 function autosave_saved_new(response) { 180 blockSave = false; 181 var res = autosave_parse_response(response), postID; 182 if ( res && res.responses.length && !res.errors ) { 183 // An ID is sent only for real auto-saves, not for autosave=0 "keepalive" saves 184 postID = parseInt( res.responses[0].id, 10 ); 185 if ( !isNaN(postID) && postID > 0 ) { 186 notSaved = false; 187 jQuery('#auto_draft').val('0'); // No longer an auto-draft 188 } 189 autosave_enable_buttons(); 190 if ( autosaveDelayPreview ) { 191 autosaveDelayPreview = false; 192 doPreview(); 193 } 194 } else { 195 autosave_enable_buttons(); // re-enable disabled form buttons 196 } 197 } 198 199 function autosave_update_slug(post_id) { 200 // create slug area only if not already there 201 if ( 'undefined' != makeSlugeditClickable && jQuery.isFunction(makeSlugeditClickable) && !jQuery('#edit-slug-box > *').size() ) { 202 jQuery.post( ajaxurl, { 203 action: 'sample-permalink', 204 post_id: post_id, 205 new_title: fullscreen && fullscreen.settings.visible ? jQuery('#wp-fullscreen-title').val() : jQuery('#title').val(), 206 samplepermalinknonce: jQuery('#samplepermalinknonce').val() 207 }, 208 function(data) { 209 if ( data !== '-1' ) { 210 jQuery('#edit-slug-box').html(data); 211 makeSlugeditClickable(); 212 } 213 } 214 ); 215 } 216 } 217 218 function autosave_loading() { 219 jQuery('.autosave-message').html(autosaveL10n.savingText); 220 } 221 222 function autosave_enable_buttons() { 223 // delay that a bit to avoid some rare collisions while the DOM is being updated. 224 setTimeout(function(){ 225 jQuery(':button, :submit', '#submitpost').removeAttr('disabled'); 226 jQuery('.ajax-loading').css('visibility', 'hidden'); 227 }, 500); 228 } 229 230 function autosave_disable_buttons() { 231 jQuery(':button, :submit', '#submitpost').prop('disabled', true); 232 // Re-enable 5 sec later. Just gives autosave a head start to avoid collisions. 233 setTimeout(autosave_enable_buttons, 5000); 234 } 235 236 function delayed_autosave() { 237 setTimeout(function(){ 238 if ( blockSave ) 239 return; 240 autosave(); 241 }, 200); 242 } 243 244 autosave = function() { 245 // (bool) is rich editor enabled and active 246 blockSave = true; 247 var rich = (typeof tinyMCE != "undefined") && tinyMCE.activeEditor && !tinyMCE.activeEditor.isHidden(), 248 post_data, doAutoSave, ed, origStatus, successCallback; 249 250 autosave_disable_buttons(); 251 252 post_data = { 253 action: "autosave", 254 post_ID: jQuery("#post_ID").val() || 0, 255 autosavenonce: jQuery('#autosavenonce').val(), 256 post_type: jQuery('#post_type').val() || "", 257 autosave: 1 258 }; 259 260 jQuery('.tags-input').each( function() { 261 post_data[this.name] = this.value; 262 } ); 263 264 // We always send the ajax request in order to keep the post lock fresh. 265 // This (bool) tells whether or not to write the post to the DB during the ajax request. 266 doAutoSave = true; 267 268 // No autosave while thickbox is open (media buttons) 269 if ( jQuery("#TB_window").css('display') == 'block' ) 270 doAutoSave = false; 271 272 /* Gotta do this up here so we can check the length when tinyMCE is in use */ 273 if ( rich && doAutoSave ) { 274 ed = tinyMCE.activeEditor; 275 // Don't run while the TinyMCE spellcheck is on. It resets all found words. 276 if ( ed.plugins.spellchecker && ed.plugins.spellchecker.active ) { 277 doAutoSave = false; 278 } else { 279 if ( 'mce_fullscreen' == ed.id || 'wp_mce_fullscreen' == ed.id ) 280 tinyMCE.get('content').setContent(ed.getContent({format : 'raw'}), {format : 'raw'}); 281 tinyMCE.triggerSave(); 282 } 283 } 284 285 if ( fullscreen && fullscreen.settings.visible ) { 286 post_data["post_title"] = jQuery('#wp-fullscreen-title').val() || ''; 287 post_data["content"] = jQuery("#wp_mce_fullscreen").val() || ''; 288 } else { 289 post_data["post_title"] = jQuery("#title").val() || ''; 290 post_data["content"] = jQuery("#content").val() || ''; 291 } 292 293 if ( jQuery('#post_name').val() ) 294 post_data["post_name"] = jQuery('#post_name').val(); 295 296 // Nothing to save or no change. 297 if ( ( post_data["post_title"].length == 0 && post_data["content"].length == 0 ) || post_data["post_title"] + post_data["content"] == autosaveLast ) { 298 doAutoSave = false; 299 } 300 301 origStatus = jQuery('#original_post_status').val(); 302 303 goodcats = ([]); 304 jQuery("[name='post_category[]']:checked").each( function(i) { 305 goodcats.push(this.value); 306 } ); 307 post_data["catslist"] = goodcats.join(","); 308 309 if ( jQuery("#comment_status").prop("checked") ) 310 post_data["comment_status"] = 'open'; 311 if ( jQuery("#ping_status").prop("checked") ) 312 post_data["ping_status"] = 'open'; 313 if ( jQuery("#excerpt").size() ) 314 post_data["excerpt"] = jQuery("#excerpt").val(); 315 if ( jQuery("#post_author").size() ) 316 post_data["post_author"] = jQuery("#post_author").val(); 317 if ( jQuery("#parent_id").val() ) 318 post_data["parent_id"] = jQuery("#parent_id").val(); 319 post_data["user_ID"] = jQuery("#user-id").val(); 320 if ( jQuery('#auto_draft').val() == '1' ) 321 post_data["auto_draft"] = '1'; 322 323 if ( doAutoSave ) { 324 autosaveLast = post_data["post_title"] + post_data["content"]; 325 jQuery(document).triggerHandler('wpcountwords', [ post_data["content"] ]); 326 } else { 327 post_data['autosave'] = 0; 328 } 329 330 if ( post_data["auto_draft"] == '1' ) { 331 successCallback = autosave_saved_new; // new post 332 } else { 333 successCallback = autosave_saved; // pre-existing post 334 } 335 336 autosaveOldMessage = jQuery('#autosave').html(); 337 jQuery.ajax({ 338 data: post_data, 339 beforeSend: doAutoSave ? autosave_loading : null, 340 type: "POST", 341 url: ajaxurl, 342 success: successCallback 343 }); 344 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Fri May 25 03:56:23 2012 | Hosted by follow the white rabbit. |