1 /* global switchEditors, autosaveL10n, tinymce, ajaxurl, wpAjax, makeSlugeditClickable, wpCookies */
2 var autosave, autosavePeriodical, fullscreen, doPreview,
4 autosaveDelayPreview = false,
7 autosaveLockRelease = true;
9 jQuery(document).ready( function($) {
11 if ( $('#wp-content-wrap').hasClass('tmce-active') && typeof switchEditors != 'undefined' ) {
12 autosaveLast = wp.autosave.getCompareString({
13 post_title : $('#title').val() || '',
14 content : switchEditors.pre_wpautop( $('#content').val() ) || '',
15 excerpt : $('#excerpt').val() || ''
18 autosaveLast = wp.autosave.getCompareString();
21 autosavePeriodical = $.schedule({time: autosaveL10n.autosaveInterval * 1000, func: function() { autosave(); }, repeat: true, protect: true});
23 //Disable autosave after the form has been submitted
24 $('#post').submit(function() {
25 $.cancel(autosavePeriodical);
26 autosaveLockRelease = false;
29 $('input[type="submit"], a.submitdelete', '#submitpost').click(function(){
31 window.onbeforeunload = null;
32 $(':button, :submit', '#submitpost').each(function(){
34 if ( t.hasClass('button-primary') )
35 t.addClass('button-primary-disabled');
37 t.addClass('button-disabled');
39 if ( $(this).attr('id') == 'publish' )
40 $('#major-publishing-actions .spinner').show();
42 $('#minor-publishing .spinner').show();
45 window.onbeforeunload = function(){
46 var editor = typeof(tinymce) != 'undefined' ? tinymce.activeEditor : false, compareString;
48 if ( editor && ! editor.isHidden() ) {
49 if ( editor.isDirty() )
50 return autosaveL10n.saveAlert;
52 if ( fullscreen && fullscreen.settings.visible ) {
53 compareString = wp.autosave.getCompareString({
54 post_title: $('#wp-fullscreen-title').val() || '',
55 content: $('#wp_mce_fullscreen').val() || '',
56 excerpt: $('#excerpt').val() || ''
59 compareString = wp.autosave.getCompareString();
62 if ( compareString != autosaveLast )
63 return autosaveL10n.saveAlert;
67 $(window).unload( function(e) {
68 if ( ! autosaveLockRelease )
71 // unload fires (twice) on removing the Thickbox iframe. Make sure we process only the main document unload.
72 if ( e.target && e.target.nodeName != '#document' )
80 action: 'wp-remove-post-lock',
81 _wpnonce: $('#_wpnonce').val(),
82 post_ID: $('#post_ID').val(),
83 active_post_lock: $('#active_post_lock').val()
89 $('#post-preview').click(function(){
90 if ( $('#auto_draft').val() == '1' && notSaved ) {
91 autosaveDelayPreview = true;
99 doPreview = function() {
100 $('input#wp-preview').val('dopreview');
101 $('form#post').attr('target', 'wp-preview').submit().attr('target', '');
104 * Workaround for WebKit bug preventing a form submitting twice to the same action.
105 * https://bugs.webkit.org/show_bug.cgi?id=28633
107 var ua = navigator.userAgent.toLowerCase();
108 if ( ua.indexOf('safari') != -1 && ua.indexOf('chrome') == -1 ) {
109 $('form#post').attr('action', function(index, value) {
110 return value + '?t=' + new Date().getTime();
114 $('input#wp-preview').val('');
117 // This code is meant to allow tabbing from Title to Post content.
118 $('#title').on('keydown.editor-focus', function(e) {
124 if ( !e.ctrlKey && !e.altKey && !e.shiftKey ) {
125 if ( typeof(tinymce) != 'undefined' )
126 ed = tinymce.get('content');
128 if ( ed && !ed.isHidden() ) {
129 $(this).one('keyup', function(){
130 $('#content_tbl td.mceToolbar > a').focus();
133 $('#content').focus();
140 // autosave new posts after a title is typed but not if Publish or Save Draft is clicked
141 if ( '1' == $('#auto_draft').val() ) {
142 $('#title').blur( function() {
143 if ( !this.value || $('#auto_draft').val() != '1' )
149 // When connection is lost, keep user from submitting changes.
150 $(document).on('heartbeat-connection-lost.autosave', function( e, error, status ) {
151 if ( 'timeout' === error || 503 == status ) {
152 var notice = $('#lost-connection-notice');
153 if ( ! wp.autosave.local.hasStorage ) {
154 notice.find('.hide-if-no-sessionstorage').hide();
157 autosave_disable_buttons();
159 }).on('heartbeat-connection-restored.autosave', function() {
160 $('#lost-connection-notice').hide();
161 autosave_enable_buttons();
165 function autosave_parse_response( response ) {
166 var res = wpAjax.parseAjaxResponse(response, 'autosave'), post_id, sup;
168 if ( res && res.responses && res.responses.length ) {
169 if ( res.responses[0].supplemental ) {
170 sup = res.responses[0].supplemental;
172 jQuery.each( sup, function( selector, value ) {
173 if ( selector.match(/^replace-/) )
174 jQuery( '#' + selector.replace('replace-', '') ).val( value );
178 // if no errors: add slug UI and update autosave-message
180 if ( post_id = parseInt( res.responses[0].id, 10 ) )
181 autosave_update_slug( post_id );
183 if ( res.responses[0].data ) // update autosave message
184 jQuery('.autosave-message').text( res.responses[0].data );
191 // called when autosaving pre-existing post
192 function autosave_saved(response) {
194 autosave_parse_response(response); // parse the ajax response
195 autosave_enable_buttons(); // re-enable disabled form buttons
198 // called when autosaving new post
199 function autosave_saved_new(response) {
201 var res = autosave_parse_response(response), post_id;
203 if ( res && res.responses.length && !res.errors ) {
204 // An ID is sent only for real auto-saves, not for autosave=0 "keepalive" saves
205 post_id = parseInt( res.responses[0].id, 10 );
209 jQuery('#auto_draft').val('0'); // No longer an auto-draft
212 autosave_enable_buttons();
214 if ( autosaveDelayPreview ) {
215 autosaveDelayPreview = false;
219 autosave_enable_buttons(); // re-enable disabled form buttons
223 function autosave_update_slug(post_id) {
224 // create slug area only if not already there
225 if ( 'undefined' != makeSlugeditClickable && jQuery.isFunction(makeSlugeditClickable) && !jQuery('#edit-slug-box > *').size() ) {
226 jQuery.post( ajaxurl, {
227 action: 'sample-permalink',
229 new_title: fullscreen && fullscreen.settings.visible ? jQuery('#wp-fullscreen-title').val() : jQuery('#title').val(),
230 samplepermalinknonce: jQuery('#samplepermalinknonce').val()
233 if ( data !== '-1' ) {
234 var box = jQuery('#edit-slug-box');
236 if (box.hasClass('hidden')) {
237 box.fadeIn('fast', function () {
238 box.removeClass('hidden');
241 makeSlugeditClickable();
248 function autosave_loading() {
249 jQuery('.autosave-message').html(autosaveL10n.savingText);
252 function autosave_enable_buttons() {
253 jQuery(document).trigger('autosave-enable-buttons');
254 if ( ! wp.heartbeat || ! wp.heartbeat.hasConnectionError() ) {
255 // delay that a bit to avoid some rare collisions while the DOM is being updated.
256 setTimeout(function(){
257 var parent = jQuery('#submitpost');
258 parent.find(':button, :submit').removeAttr('disabled');
259 parent.find('.spinner').hide();
264 function autosave_disable_buttons() {
265 jQuery(document).trigger('autosave-disable-buttons');
266 jQuery('#submitpost').find(':button, :submit').prop('disabled', true);
267 // Re-enable 5 sec later. Just gives autosave a head start to avoid collisions.
268 setTimeout( autosave_enable_buttons, 5000 );
271 function delayed_autosave() {
272 setTimeout(function(){
279 autosave = function() {
280 var post_data = wp.autosave.getPostData(),
286 // post_data.content cannot be retrieved at the moment
287 if ( ! post_data.autosave )
290 // No autosave while thickbox is open (media buttons)
291 if ( jQuery('#TB_window').css('display') == 'block' )
294 compareString = wp.autosave.getCompareString( post_data );
296 // Nothing to save or no change.
297 if ( compareString == autosaveLast )
300 autosaveLast = compareString;
301 jQuery(document).triggerHandler('wpcountwords', [ post_data.content ]);
303 // Disable buttons until we know the save completed.
304 autosave_disable_buttons();
306 if ( post_data.auto_draft == '1' ) {
307 successCallback = autosave_saved_new; // new post
309 successCallback = autosave_saved; // pre-existing post
314 beforeSend: autosave_loading,
317 success: successCallback
323 // Autosave in localStorage
324 // set as simple object/mixin for now
325 window.wp = window.wp || {};
326 wp.autosave = wp.autosave || {};
329 // Returns the data for saving in both localStorage and autosaves to the server
330 wp.autosave.getPostData = function() {
331 var ed = typeof tinymce != 'undefined' ? tinymce.activeEditor : null, post_name, parent_id, cats = [],
335 post_id: $('#post_ID').val() || 0,
336 autosavenonce: $('#autosavenonce').val() || '',
337 post_type: $('#post_type').val() || '',
338 post_author: $('#post_author').val() || '',
339 excerpt: $('#excerpt').val() || ''
342 if ( ed && !ed.isHidden() ) {
343 // Don't run while the tinymce spellcheck is on. It resets all found words.
344 if ( ed.plugins.spellchecker && ed.plugins.spellchecker.active ) {
345 data.autosave = false;
348 if ( 'mce_fullscreen' == ed.id )
349 tinymce.get('content').setContent(ed.getContent({format : 'raw'}), {format : 'raw'});
351 tinymce.triggerSave();
355 if ( typeof fullscreen != 'undefined' && fullscreen.settings.visible ) {
356 data.post_title = $('#wp-fullscreen-title').val() || '';
357 data.content = $('#wp_mce_fullscreen').val() || '';
359 data.post_title = $('#title').val() || '';
360 data.content = $('#content').val() || '';
364 // We haven't been saving tags with autosave since 2.8... Start again?
365 $('.the-tags').each( function() {
366 data[this.name] = this.value;
370 $('input[id^="in-category-"]:checked').each( function() {
371 cats.push(this.value);
373 data.catslist = cats.join(',');
375 if ( post_name = $('#post_name').val() )
376 data.post_name = post_name;
378 if ( parent_id = $('#parent_id').val() )
379 data.parent_id = parent_id;
381 if ( $('#comment_status').prop('checked') )
382 data.comment_status = 'open';
384 if ( $('#ping_status').prop('checked') )
385 data.ping_status = 'open';
387 if ( $('#auto_draft').val() == '1' )
388 data.auto_draft = '1';
393 // Concatenate title, content and excerpt. Used to track changes when auto-saving.
394 wp.autosave.getCompareString = function( post_data ) {
395 if ( typeof post_data === 'object' ) {
396 return ( post_data.post_title || '' ) + '::' + ( post_data.content || '' ) + '::' + ( post_data.excerpt || '' );
399 return ( $('#title').val() || '' ) + '::' + ( $('#content').val() || '' ) + '::' + ( $('#excerpt').val() || '' );
402 wp.autosave.local = {
408 // Check if the browser supports sessionStorage and it's not disabled
409 checkStorage: function() {
410 var test = Math.random(), result = false;
413 sessionStorage.setItem('wp-test', test);
414 result = sessionStorage.getItem('wp-test') == test;
415 sessionStorage.removeItem('wp-test');
418 this.hasStorage = result;
423 * Initialize the local storage
425 * @return mixed False if no sessionStorage in the browser or an Object containing all post_data for this blog
427 getStorage: function() {
428 var stored_obj = false;
429 // Separate local storage containers for each blog_id
430 if ( this.hasStorage && this.blog_id ) {
431 stored_obj = sessionStorage.getItem( 'wp-autosave-' + this.blog_id );
434 stored_obj = JSON.parse( stored_obj );
443 * Set the storage for this blog
445 * Confirms that the data was saved successfully.
449 setStorage: function( stored_obj ) {
452 if ( this.hasStorage && this.blog_id ) {
453 key = 'wp-autosave-' + this.blog_id;
454 sessionStorage.setItem( key, JSON.stringify( stored_obj ) );
455 return sessionStorage.getItem( key ) !== null;
462 * Get the saved post data for the current post
464 * @return mixed False if no storage or no data or the post_data as an Object
466 getData: function() {
467 var stored = this.getStorage(), post_id = $('#post_ID').val();
469 if ( !stored || !post_id )
472 return stored[ 'post_' + post_id ] || false;
476 * Set (save or delete) post data in the storage.
478 * If stored_data evaluates to 'false' the storage key for the current post will be removed
480 * $param stored_data The post data to store or null/false/empty to delete the key
483 setData: function( stored_data ) {
484 var stored = this.getStorage(), post_id = $('#post_ID').val();
486 if ( !stored || !post_id )
490 stored[ 'post_' + post_id ] = stored_data;
491 else if ( stored.hasOwnProperty( 'post_' + post_id ) )
492 delete stored[ 'post_' + post_id ];
496 return this.setStorage(stored);
500 * Save post data for the current post
502 * Runs on a 15 sec. schedule, saves when there are differences in the post title or content.
503 * When the optional data is provided, updates the last saved post data.
505 * $param data optional Object The post data for saving, minimum 'post_title' and 'content'
508 save: function( data ) {
509 var result = false, post_data, compareString;
512 post_data = wp.autosave.getPostData();
514 post_data = this.getData() || {};
515 $.extend( post_data, data );
516 post_data.autosave = true;
519 // Cannot get the post data at the moment
520 if ( ! post_data.autosave )
523 compareString = wp.autosave.getCompareString( post_data );
525 // If the content, title and excerpt did not change since the last save, don't save again
526 if ( compareString == this.lastSavedData )
529 post_data.save_time = (new Date()).getTime();
530 post_data.status = $('#post_status').val() || '';
531 result = this.setData( post_data );
534 this.lastSavedData = compareString;
539 // Initialize and run checkPost() on loading the script (before TinyMCE init)
540 init: function( settings ) {
543 // Check if the browser supports sessionStorage and it's not disabled
544 if ( ! this.checkStorage() )
547 // Don't run if the post type supports neither 'editor' (textarea#content) nor 'excerpt'.
548 if ( ! $('#content').length && ! $('#excerpt').length )
552 $.extend( this, settings );
555 this.blog_id = typeof window.autosaveL10n != 'undefined' ? window.autosaveL10n.blog_id : 0;
557 $(document).ready( function(){ self.run(); } );
564 // Check if the local post data is different than the loaded post data.
568 this.schedule = $.schedule({
570 func: function() { wp.autosave.local.save(); },
575 $('form#post').on('submit.autosave-local', function() {
576 var editor = typeof tinymce != 'undefined' && tinymce.get('content'), post_id = $('#post_ID').val() || 0;
578 if ( editor && ! editor.isHidden() ) {
579 // Last onSubmit event in the editor, needs to run after the content has been moved to the textarea.
580 editor.onSubmit.add( function() {
581 wp.autosave.local.save({
582 post_title: $('#title').val() || '',
583 content: $('#content').val() || '',
584 excerpt: $('#excerpt').val() || ''
589 post_title: $('#title').val() || '',
590 content: $('#content').val() || '',
591 excerpt: $('#excerpt').val() || ''
595 wpCookies.set( 'wp-saving-post-' + post_id, 'check' );
599 // Strip whitespace and compare two strings
600 compare: function( str1, str2 ) {
601 function remove( string ) {
602 return string.toString().replace(/[\x20\t\r\n\f]+/g, '');
605 return ( remove( str1 || '' ) == remove( str2 || '' ) );
609 * Check if the saved data for the current post (if any) is different than the loaded post data on the screen
611 * Shows a standard message letting the user restore the post data if different.
615 checkPost: function() {
616 var self = this, post_data = this.getData(), content, post_title, excerpt, notice,
617 post_id = $('#post_ID').val() || 0, cookie = wpCookies.get( 'wp-saving-post-' + post_id );
623 wpCookies.remove( 'wp-saving-post-' + post_id );
625 if ( cookie == 'saved' ) {
626 // The post was saved properly, remove old data and bail
627 this.setData( false );
632 // There is a newer autosave. Don't show two "restore" notices at the same time.
633 if ( $('#has-newer-autosave').length )
636 content = $('#content').val() || '';
637 post_title = $('#title').val() || '';
638 excerpt = $('#excerpt').val() || '';
640 if ( $('#wp-content-wrap').hasClass('tmce-active') && typeof switchEditors != 'undefined' )
641 content = switchEditors.pre_wpautop( content );
643 // cookie == 'check' means the post was not saved properly, always show #local-storage-notice
644 if ( cookie != 'check' && this.compare( content, post_data.content ) && this.compare( post_title, post_data.post_title ) && this.compare( excerpt, post_data.excerpt ) ) {
648 this.restore_post_data = post_data;
649 this.undo_post_data = {
651 post_title: post_title,
655 notice = $('#local-storage-notice');
656 $('.wrap h2').first().after( notice.addClass('updated').show() );
658 notice.on( 'click', function(e) {
659 var target = $( e.target );
661 if ( target.hasClass('restore-backup') ) {
662 self.restorePost( self.restore_post_data );
663 target.parent().hide();
664 $(this).find('p.undo-restore').show();
665 } else if ( target.hasClass('undo-restore-backup') ) {
666 self.restorePost( self.undo_post_data );
667 target.parent().hide();
668 $(this).find('p.local-restore').show();
675 // Restore the current title, content and excerpt from post_data.
676 restorePost: function( post_data ) {
680 // Set the last saved data
681 this.lastSavedData = wp.autosave.getCompareString( post_data );
683 if ( $('#title').val() != post_data.post_title )
684 $('#title').focus().val( post_data.post_title || '' );
686 $('#excerpt').val( post_data.excerpt || '' );
687 editor = typeof tinymce != 'undefined' && tinymce.get('content');
689 if ( editor && ! editor.isHidden() && typeof switchEditors != 'undefined' ) {
690 // Make sure there's an undo level in the editor
691 editor.undoManager.add();
692 editor.setContent( post_data.content ? switchEditors.wpautop( post_data.content ) : '' );
694 // Make sure the Text editor is selected
695 $('#content-html').click();
696 $('#content').val( post_data.content );
706 wp.autosave.local.init();