Make WordPress Core

Changeset 27038


Ignore:
Timestamp:
01/27/2014 12:27:42 AM (13 years ago)
Author:
azaozz
Message:

Autosave:

  • Move the 'Saving post' and 'Draft saved at...' strings from autosaveL10n to postL10n as they are used only there.
  • Use the custom jQuery events 'before-autosave' and 'after-autosave' to show these messages.
  • Separate autosave.suspend() for local and server so local autosaves can continue while server autosaves are suspended.
  • Remove the recently added autosave.server.disable() and use autosave.server.suspend() instead.
  • Bring back .button.disabled, button-primary.disabled and use .disabled to prevent multiple form submissions.

See #25272.

Location:
trunk/src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/js/post.js

    r27035 r27038  
    1 /* global postL10n, ajaxurl, wpAjax, setPostThumbnailL10n, postboxes, pagenow, tinymce, alert, deleteUserSetting, getUserSetting, setUserSetting */
    2 /* global theList:true, theExtraList:true, autosave:true */
    3 
    4 var tagBox, commentsBox, WPSetThumbnailHTML, WPSetThumbnailID, WPRemoveThumbnail, wptitlehint;
     1/* global postL10n, ajaxurl, wpAjax, setPostThumbnailL10n, postboxes, pagenow, tinymce, alert, deleteUserSetting */
     2/* global theList:true, theExtraList:true, getUserSetting, setUserSetting */
     3
     4var tagBox, commentsBox, WPSetThumbnailHTML, WPSetThumbnailID, WPRemoveThumbnail, wptitlehint, makeSlugeditClickable, editPermalink;
    55// Back-compat: prevent fatal errors
    66makeSlugeditClickable = editPermalink = function(){};
     7
     8window.wp = window.wp || {};
    79
    810// return an array with any duplicate, whitespace or values removed
     
    284286
    285287                        if ( wrap.length && ! wrap.is(':visible') ) {
    286                                 if ( typeof wp != 'undefined' && wp.autosave ) {
     288                                if ( wp.autosave ) {
    287289                                        // Save the latest changes and disable
    288290                                        $(document).one( 'heartbeat-tick', function() {
    289                                                 wp.autosave.server.disable();
     291                                                wp.autosave.server.suspend();
    290292                                                wrap.removeClass('saving').addClass('saved');
    291293                                                $(window).off( 'beforeunload.edit-post' );
     
    314316                                action: 'sample-permalink',
    315317                                post_id: $('#post_ID').val(),
    316                                 new_title: typeof fullscreen != 'undefined' && fullscreen.settings.visible ? $('#wp-fullscreen-title').val() : $('#title').val(),
     318                                new_title: $('#title').val(),
    317319                                samplepermalinknonce: $('#samplepermalinknonce').val()
    318320                        },
     
    369371
    370372jQuery(document).ready( function($) {
    371         var stamp, visibility, $submitButtons,
     373        var stamp, visibility, $submitButtons, updateVisibility, updateText,
    372374                sticky = '',
    373375                last = 0,
     
    400402
    401403        // Set the heartbeat interval to 15 sec. if post lock dialogs are enabled
    402         if ( typeof wp !== 'undefined' && wp.heartbeat && $('#post-lock-dialog').length ) {
     404        if ( wp.heartbeat && $('#post-lock-dialog').length ) {
    403405                wp.heartbeat.interval( 15 );
    404406        }
     
    408410                var $button = $(this);
    409411
    410                 if ( $button.hasClass('button-disabled') ) {
     412                if ( $button.hasClass('disabled') ) {
    411413                        event.preventDefault();
    412414                        return;
     
    424426                        }
    425427
    426                         if ( typeof wp != 'undefined' && wp.autosave ) {
    427                                 wp.autosave.server.disable();
     428                        // Stop autosave
     429                        if ( wp.autosave ) {
     430                                wp.autosave.server.suspend();
    428431                        }
    429432
     
    431434                        $(window).off( 'beforeunload.edit-post' );
    432435
    433                         $submitButtons.addClass( 'button-disabled' );
     436                        $submitButtons.addClass( 'disabled' );
    434437
    435438                        if ( $button.attr('id') === 'publish' ) {
     
    448451                        target = $this.attr('target') || 'wp-preview',
    449452                        ua = navigator.userAgent.toLowerCase();
    450                
     453
    451454                event.preventDefault();
    452455
    453                 if ( $this.prop('disabled') ) {
     456                if ( $this.hasClass('disabled') ) {
    454457                        return;
    455458                }
    456459
    457                 if ( typeof wp != 'undefined' && wp.autosave ) {
     460                if ( wp.autosave ) {
    458461                        wp.autosave.server.tempBlockSave();
    459462                }
     
    497500                        }
    498501
    499                         if ( typeof wp != 'undefined' && wp.autosave ) {
     502                        if ( wp.autosave ) {
    500503                                wp.autosave.server.triggerSave();
    501504                        }
     
    504507
    505508        $(document).on( 'autosave-disable-buttons.edit-post', function() {
    506                 $submitButtons.addClass( 'button-disabled' );
     509                $submitButtons.addClass( 'disabled' );
    507510        }).on( 'autosave-enable-buttons.edit-post', function() {
    508                 if ( ! window.wp || ! window.wp.heartbeat || ! window.wp.heartbeat.hasConnectionError() ) {
    509                         $submitButtons.removeClass( 'button-disabled' );
    510                 }
     511                if ( ! wp.heartbeat || ! wp.heartbeat.hasConnectionError() ) {
     512                        $submitButtons.removeClass( 'disabled' );
     513                }
     514        }).on( 'before-autosave.edit-post', function() {
     515                $( '.autosave-message' ).text( postL10n.savingText );
     516        }).on( 'after-autosave.edit-post', function( event, data ) {
     517                $( '.autosave-message' ).text( data.message );
    511518        });
    512519
     
    514521                var editor = typeof tinymce !== 'undefined' && tinymce.get('content');
    515522
    516                 if ( ( editor && ! editor.isHidden() && editor.isDirty() ) || 
    517                         ( typeof wp !== 'undefined' && wp.autosave && wp.autosave.server.postChanged() ) ) {
    518 
    519                         return autosaveL10n.saveAlert;
     523                if ( ( editor && ! editor.isHidden() && editor.isDirty() ) ||
     524                        ( wp.autosave && wp.autosave.server.postChanged() ) ) {
     525
     526                        return postL10n.saveAlert;
    520527                }
    521528        }).on( 'unload.edit-post', function( event ) {
     
    818825                                $timestampdiv.show();
    819826
    820                                 if ( typeof wp !== 'undefined' && wp.autosave ) {
     827                                if ( wp.autosave ) {
    821828                                        wp.autosave.enableButtons();
    822829                                }
     
    849856
    850857        // permalink
     858        function editPermalink() {
     859                var i, slug_value,
     860                        c = 0,
     861                        e = $('#editable-post-name'),
     862                        revert_e = e.html(),
     863                        real_slug = $('#post_name'),
     864                        revert_slug = real_slug.val(),
     865                        b = $('#edit-slug-buttons'),
     866                        revert_b = b.html(),
     867                        full = $('#editable-post-name-full').html();
     868
     869                $('#view-post-btn').hide();
     870                b.html('<a href="#" class="save button button-small">'+postL10n.ok+'</a> <a class="cancel" href="#">'+postL10n.cancel+'</a>');
     871                b.children('.save').click(function() {
     872                        var new_slug = e.children('input').val();
     873                        if ( new_slug == $('#editable-post-name-full').text() ) {
     874                                return $('#edit-slug-buttons .cancel').click();
     875                        }
     876                        $.post(ajaxurl, {
     877                                action: 'sample-permalink',
     878                                post_id: postId,
     879                                new_slug: new_slug,
     880                                new_title: $('#title').val(),
     881                                samplepermalinknonce: $('#samplepermalinknonce').val()
     882                        }, function(data) {
     883                                var box = $('#edit-slug-box');
     884                                box.html(data);
     885                                if (box.hasClass('hidden')) {
     886                                        box.fadeIn('fast', function () {
     887                                                box.removeClass('hidden');
     888                                        });
     889                                }
     890                                b.html(revert_b);
     891                                real_slug.val(new_slug);
     892                                $('#view-post-btn').show();
     893                        });
     894                        return false;
     895                });
     896
     897                $('#edit-slug-buttons .cancel').click(function() {
     898                        $('#view-post-btn').show();
     899                        e.html(revert_e);
     900                        b.html(revert_b);
     901                        real_slug.val(revert_slug);
     902                        return false;
     903                });
     904
     905                for ( i = 0; i < full.length; ++i ) {
     906                        if ( '%' == full.charAt(i) )
     907                                c++;
     908                }
     909
     910                slug_value = ( c > full.length / 4 ) ? '' : full;
     911                e.html('<input type="text" id="new-post-slug" value="'+slug_value+'" />').children('input').keypress(function(e) {
     912                        var key = e.keyCode || 0;
     913                        // on enter, just save the new slug, don't save the post
     914                        if ( 13 == key ) {
     915                                b.children('.save').click();
     916                                return false;
     917                        }
     918                        if ( 27 == key ) {
     919                                b.children('.cancel').click();
     920                                return false;
     921                        }
     922                } ).keyup( function() {
     923                        real_slug.val(this.value);
     924                }).focus();
     925        }
     926
    851927        if ( $editSlugWrap.length ) {
    852                 function editPermalink() {
    853                         var i, c = 0, e = $('#editable-post-name'), revert_e = e.html(), real_slug = $('#post_name'),
    854                                 revert_slug = real_slug.val(), b = $('#edit-slug-buttons'), revert_b = b.html(),
    855                                 full = $('#editable-post-name-full').html();
    856 
    857                         $('#view-post-btn').hide();
    858                         b.html('<a href="#" class="save button button-small">'+postL10n.ok+'</a> <a class="cancel" href="#">'+postL10n.cancel+'</a>');
    859                         b.children('.save').click(function() {
    860                                 var new_slug = e.children('input').val();
    861                                 if ( new_slug == $('#editable-post-name-full').text() ) {
    862                                         return $('#edit-slug-buttons .cancel').click();
    863                                 }
    864                                 $.post(ajaxurl, {
    865                                         action: 'sample-permalink',
    866                                         post_id: postId,
    867                                         new_slug: new_slug,
    868                                         new_title: $('#title').val(),
    869                                         samplepermalinknonce: $('#samplepermalinknonce').val()
    870                                 }, function(data) {
    871                                         var box = $('#edit-slug-box');
    872                                         box.html(data);
    873                                         if (box.hasClass('hidden')) {
    874                                                 box.fadeIn('fast', function () {
    875                                                         box.removeClass('hidden');
    876                                                 });
    877                                         }
    878                                         b.html(revert_b);
    879                                         real_slug.val(new_slug);
    880                                         $('#view-post-btn').show();
    881                                 });
    882                                 return false;
    883                         });
    884 
    885                         $('#edit-slug-buttons .cancel').click(function() {
    886                                 $('#view-post-btn').show();
    887                                 e.html(revert_e);
    888                                 b.html(revert_b);
    889                                 real_slug.val(revert_slug);
    890                                 return false;
    891                         });
    892 
    893                         for ( i = 0; i < full.length; ++i ) {
    894                                 if ( '%' == full.charAt(i) )
    895                                         c++;
    896                         }
    897 
    898                         slug_value = ( c > full.length / 4 ) ? '' : full;
    899                         e.html('<input type="text" id="new-post-slug" value="'+slug_value+'" />').children('input').keypress(function(e) {
    900                                 var key = e.keyCode || 0;
    901                                 // on enter, just save the new slug, don't save the post
    902                                 if ( 13 == key ) {
    903                                         b.children('.save').click();
    904                                         return false;
    905                                 }
    906                                 if ( 27 == key ) {
    907                                         b.children('.cancel').click();
    908                                         return false;
    909                                 }
    910                         } ).keyup( function() {
    911                                 real_slug.val(this.value);
    912                         }).focus();
    913                 };
    914 
    915928                $editSlugWrap.on( 'click', function( event ) {
    916929                        var $target = $( event.target );
     
    972985                        $textarea = $('textarea#content'),
    973986                        $handle = $('#post-status-info');
    974                
     987
    975988                // No point for touch devices
    976989                if ( ! $textarea.length || 'ontouchstart' in window ) {
  • trunk/src/wp-includes/css/buttons.css

    r26993 r27038  
    166166.wp-core-ui .button[disabled],
    167167.wp-core-ui .button:disabled,
     168.wp-core-ui .button.disabled,
    168169.wp-core-ui .button-secondary[disabled],
    169170.wp-core-ui .button-secondary:disabled,
     171.wp-core-ui .button-secondary.disabled,
    170172.wp-core-ui .button-disabled {
    171173        color: #aaa !important;
     
    222224.wp-core-ui .button-primary[disabled],
    223225.wp-core-ui .button-primary:disabled,
    224 .wp-core-ui .button-primary-disabled {
     226.wp-core-ui .button-primary-disabled,
     227.wp-core-ui .button-primary.disabled {
    225228        color: #94cde7 !important;
    226229        background: #298cba !important;
  • trunk/src/wp-includes/js/autosave.js

    r26995 r27038  
    77                var initialCompareString,
    88                lastTriggerSave = 0,
    9                 isSuspended = false,
    109                $document = $(document);
    1110
     
    8887                }
    8988
    90                 function suspend() {
    91                         isSuspended = true;
    92                 }
    93 
    94                 function resume() {
    95                         isSuspended = false;
    96                 }
    97 
    9889                // Autosave in localStorage
    9990                function autosaveLocal() {
    10091                        var restorePostData, undoPostData, blog_id, post_id, hasStorage, intervalTimer,
    101                                 lastCompareString;
     92                                lastCompareString,
     93                                isSuspended = false;
    10294
    10395                        // Check if the browser supports sessionStorage and it's not disabled
     
    195187
    196188                                return setStorage( stored );
     189                        }
     190
     191                        function suspend() {
     192                                isSuspended = true;
     193                        }
     194
     195                        function resume() {
     196                                isSuspended = false;
    197197                        }
    198198
     
    416416                                hasStorage: hasStorage,
    417417                                getSavedPostData: getSavedPostData,
    418                                 save: save
     418                                save: save,
     419                                suspend: suspend,
     420                                resume: resume
    419421                        };
    420422                }
     
    422424                // Autosave on the server
    423425                function autosaveServer() {
    424                         var _disabled, _blockSave, _blockSaveTimer, previousCompareString, lastCompareString,
    425                                 nextRun = 0;
     426                        var _blockSave, _blockSaveTimer, previousCompareString, lastCompareString,
     427                                nextRun = 0,
     428                                isSuspended = false;
    426429
    427430                        // Block saving for the next 10 sec.
     
    433436                                        _blockSave = false;
    434437                                }, 10000 );
     438                        }
     439
     440                        function suspend() {
     441                                isSuspended = true;
     442                        }
     443
     444                        function resume() {
     445                                isSuspended = false;
    435446                        }
    436447
     
    443454
    444455                                $document.trigger( 'after-autosave', [data] );
    445                                 $( '.autosave-message' ).text( data.message );
    446456                                enableButtons();
    447457
     
    453463
    454464                        /**
    455                          * Disable autosave
    456                          *
    457                          * Intended to run on form.submit
    458                          */
    459                         function disable() {
    460                                 _disabled = true;
    461                         }
    462 
    463                         /**
    464465                         * Save immediately
    465466                         *
     
    489490                                var postData, compareString;
    490491
    491                                 if ( isSuspended || _disabled || _blockSave ) {
     492                                if ( isSuspended || _blockSave ) {
    492493                                        return false;
    493494                                }
     
    517518                                        .trigger( 'before-autosave', [ postData ] );
    518519
    519                                 $( '.autosave-message' ).text( autosaveL10n.savingText );
    520520                                postData._wpnonce = $( '#_wpnonce' ).val() || '';
    521521
     
    557557
    558558                        return {
    559                                 disable: disable,
    560559                                tempBlockSave: tempBlockSave,
    561560                                triggerSave: triggerSave,
    562                                 postChanged: postChanged
     561                                postChanged: postChanged,
     562                                suspend: suspend,
     563                                resume: resume
    563564                        };
    564565                }
     
    585586                        disableButtons: disableButtons,
    586587                        enableButtons: enableButtons,
    587                         suspend: suspend,
    588                         resume: resume,
    589588                        local: autosaveLocal(),
    590589                        server: autosaveServer()
  • trunk/src/wp-includes/script-loader.php

    r27028 r27038  
    431431                        'published' => __('Published'),
    432432                        'comma' => _x( ',', 'tag delimiter' ),
     433                        'saveAlert' => __('The changes you made will be lost if you navigate away from this page.'),
     434                        'savingText' => __('Saving Draft&#8230;'),
    433435                ) );
    434436
     
    654656        wp_localize_script( 'autosave', 'autosaveL10n', array(
    655657                'autosaveInterval' => AUTOSAVE_INTERVAL,
    656                 'savingText' => __('Saving Draft&#8230;'),
    657                 'saveAlert' => __('The changes you made will be lost if you navigate away from this page.'),
    658658                'blog_id' => get_current_blog_id(),
    659659        ) );
Note: See TracChangeset for help on using the changeset viewer.