~canonical-sysadmins/wordpress/4.8.3

« back to all changes in this revision

Viewing changes to wp-admin/js/post.js

  • Committer: Ryan Finnie
  • Date: 2015-08-31 16:09:47 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: ryan.finnie@canonical.com-20150831160947-1h6rfxby9z1ec62u
Merge WP4.3 from upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* global postL10n, ajaxurl, wpAjax, setPostThumbnailL10n, postboxes, pagenow, tinymce, alert, deleteUserSetting */
2
 
/* global theList:true, theExtraList:true, getUserSetting, setUserSetting */
 
2
/* global theList:true, theExtraList:true, getUserSetting, setUserSetting, commentReply */
3
3
 
4
4
var commentsBox, WPSetThumbnailHTML, WPSetThumbnailID, WPRemoveThumbnail, wptitlehint, makeSlugeditClickable, editPermalink;
5
5
// Back-compat: prevent fatal errors
170
170
        }
171
171
 
172
172
        $(document).on( 'heartbeat-send.wp-refresh-nonces', function( e, data ) {
173
 
                var nonce, post_id;
 
173
                var post_id,
 
174
                        $authCheck = $('#wp-auth-check-wrap');
174
175
 
175
 
                if ( check ) {
176
 
                        if ( ( post_id = $('#post_ID').val() ) && ( nonce = $('#_wpnonce').val() ) ) {
 
176
                if ( check || ( $authCheck.length && ! $authCheck.hasClass( 'hidden' ) ) ) {
 
177
                        if ( ( post_id = $('#post_ID').val() ) && $('#_wpnonce').val() ) {
177
178
                                data['wp-refresh-post-nonces'] = {
178
 
                                        post_id: post_id,
179
 
                                        post_nonce: nonce
 
179
                                        post_id: post_id
180
180
                                };
181
181
                        }
182
182
                }
203
203
jQuery(document).ready( function($) {
204
204
        var stamp, visibility, $submitButtons, updateVisibility, updateText,
205
205
                sticky = '',
206
 
                last = 0,
207
 
                co = $('#content'),
 
206
                $textarea = $('#content'),
208
207
                $document = $(document),
209
208
                $editSlugWrap = $('#edit-slug-box'),
210
209
                postId = $('#post_ID').val() || 0,
212
211
                releaseLock = true,
213
212
                $postVisibilitySelect = $('#post-visibility-select'),
214
213
                $timestampdiv = $('#timestampdiv'),
215
 
                $postStatusSelect = $('#post-status-select');
 
214
                $postStatusSelect = $('#post-status-select'),
 
215
                isMac = window.navigator.platform ? window.navigator.platform.indexOf( 'Mac' ) !== -1 : false;
216
216
 
217
217
        postboxes.add_postbox_toggles(pagenow);
218
218
 
266
266
                                wp.autosave.server.suspend();
267
267
                        }
268
268
 
 
269
                        if ( typeof commentReply !== 'undefined' ) {
 
270
                                /*
 
271
                                 * Close the comment edit/reply form if open to stop the form
 
272
                                 * action from interfering with the post's form action.
 
273
                                 */
 
274
                                commentReply.close();
 
275
                        }
 
276
 
269
277
                        releaseLock = false;
270
278
                        $(window).off( 'beforeunload.edit-post' );
271
279
 
313
321
 
314
322
        // This code is meant to allow tabbing from Title to Post content.
315
323
        $('#title').on( 'keydown.editor-focus', function( event ) {
316
 
                var editor, $textarea;
 
324
                var editor;
317
325
 
318
326
                if ( event.keyCode === 9 && ! event.ctrlKey && ! event.altKey && ! event.shiftKey ) {
319
327
                        editor = typeof tinymce != 'undefined' && tinymce.get('content');
320
 
                        $textarea = $('#content');
321
328
 
322
329
                        if ( editor && ! editor.isHidden() ) {
323
330
                                editor.focus();
551
558
                                $('#timestamp').html(stamp);
552
559
                        } else {
553
560
                                $('#timestamp').html(
554
 
                                        publishOn + ' <b>' +
555
 
                                        postL10n.dateFormat.replace( '%1$s', $('option[value="' + $('#mm').val() + '"]', '#mm').text() )
556
 
                                                .replace( '%2$s', jj )
 
561
                                        '\n' + publishOn + ' <b>' +
 
562
                                        postL10n.dateFormat
 
563
                                                .replace( '%1$s', $( 'option[value="' + mm + '"]', '#mm' ).attr( 'data-text' ) )
 
564
                                                .replace( '%2$s', parseInt( jj, 10 ) )
557
565
                                                .replace( '%3$s', aa )
558
 
                                                .replace( '%4$s', hh )
559
 
                                                .replace( '%5$s', mn ) +
 
566
                                                .replace( '%4$s', ( '00' + hh ).slice( -2 ) )
 
567
                                                .replace( '%5$s', ( '00' + mn ).slice( -2 ) ) +
560
568
                                                '</b> '
561
569
                                );
562
570
                        }
599
607
                $( '#visibility .edit-visibility').click( function () {
600
608
                        if ( $postVisibilitySelect.is(':hidden') ) {
601
609
                                updateVisibility();
602
 
                                $postVisibilitySelect.slideDown('fast').find('input[type="radio"]').first().focus();
 
610
                                $postVisibilitySelect.slideDown( 'fast', function() {
 
611
                                        $postVisibilitySelect.find( 'input[type="radio"]' ).first().focus();
 
612
                                } );
603
613
                                $(this).hide();
604
614
                        }
605
615
                        return false;
618
628
 
619
629
                $postVisibilitySelect.find('.save-post-visibility').click( function( event ) { // crazyhorse - multiple ok cancels
620
630
                        $postVisibilitySelect.slideUp('fast');
621
 
                        $('#visibility .edit-visibility').show();
 
631
                        $('#visibility .edit-visibility').show().focus();
622
632
                        updateText();
623
633
 
624
634
                        if ( $postVisibilitySelect.find('input:radio:checked').val() != 'public' ) {
641
651
 
642
652
                $timestampdiv.siblings('a.edit-timestamp').click( function( event ) {
643
653
                        if ( $timestampdiv.is( ':hidden' ) ) {
644
 
                                $timestampdiv.slideDown('fast');
645
 
                                $('#mm').focus();
 
654
                                $timestampdiv.slideDown( 'fast', function() {
 
655
                                        $( 'input, select', $timestampdiv.find( '.timestamp-wrap' ) ).first().focus();
 
656
                                } );
646
657
                                $(this).hide();
647
658
                        }
648
659
                        event.preventDefault();
662
673
                $timestampdiv.find('.save-timestamp').click( function( event ) { // crazyhorse - multiple ok cancels
663
674
                        if ( updateText() ) {
664
675
                                $timestampdiv.slideUp('fast');
665
 
                                $timestampdiv.siblings('a.edit-timestamp').show();
 
676
                                $timestampdiv.siblings('a.edit-timestamp').show().focus();
666
677
                        }
667
678
                        event.preventDefault();
668
679
                });
682
693
 
683
694
                $postStatusSelect.siblings('a.edit-post-status').click( function( event ) {
684
695
                        if ( $postStatusSelect.is( ':hidden' ) ) {
685
 
                                $postStatusSelect.slideDown('fast').find('select').focus();
 
696
                                $postStatusSelect.slideDown( 'fast', function() {
 
697
                                        $postStatusSelect.find('select').focus();
 
698
                                } );
686
699
                                $(this).hide();
687
700
                        }
688
701
                        event.preventDefault();
689
702
                });
690
703
 
691
704
                $postStatusSelect.find('.save-post-status').click( function( event ) {
692
 
                        $postStatusSelect.slideUp('fast').siblings('a.edit-post-status').show();
 
705
                        $postStatusSelect.slideUp( 'fast' ).siblings( 'a.edit-post-status' ).show().focus();
693
706
                        updateText();
694
707
                        event.preventDefault();
695
708
                });
696
709
 
697
710
                $postStatusSelect.find('.cancel-post-status').click( function( event ) {
698
 
                        $('#post-status-select').slideUp('fast').siblings( 'a.edit-post-status' ).show().focus();
 
711
                        $postStatusSelect.slideUp( 'fast' ).siblings( 'a.edit-post-status' ).show().focus();
699
712
                        $('#post_status').val( $('#hidden_post_status').val() );
700
713
                        updateText();
701
714
                        event.preventDefault();
788
801
                });
789
802
        }
790
803
 
791
 
        // word count
792
 
        if ( typeof(wpWordCount) != 'undefined' ) {
793
 
                $document.triggerHandler('wpcountwords', [ co.val() ]);
794
 
 
795
 
                co.keyup( function(e) {
796
 
                        var k = e.keyCode || e.charCode;
797
 
 
798
 
                        if ( k == last )
799
 
                                return true;
800
 
 
801
 
                        if ( 13 == k || 8 == last || 46 == last )
802
 
                                $document.triggerHandler('wpcountwords', [ co.val() ]);
803
 
 
804
 
                        last = k;
805
 
                        return true;
806
 
                });
807
 
        }
808
 
 
809
804
        wptitlehint = function(id) {
810
805
                id = id || 'title';
811
806
 
835
830
        // Resize the visual and text editors
836
831
        ( function() {
837
832
                var editor, offset, mce,
838
 
                        $textarea = $('textarea#content'),
839
833
                        $handle = $('#post-status-info'),
840
834
                        $postdivrich = $('#postdivrich');
841
835
 
923
917
                        }
924
918
                });
925
919
        }
 
920
 
 
921
        // Save on pressing Ctrl/Command + S in the Text editor
 
922
        $textarea.on( 'keydown.wp-autosave', function( event ) {
 
923
                if ( event.which === 83 ) {
 
924
                        if ( event.shiftKey || event.altKey || ( isMac && ( ! event.metaKey || event.ctrlKey ) ) || ( ! isMac && ! event.ctrlKey ) ) {
 
925
                                return;
 
926
                        }
 
927
 
 
928
                        wp.autosave && wp.autosave.server.triggerSave();
 
929
                        event.preventDefault();
 
930
                }
 
931
        });
926
932
});
 
933
 
 
934
( function( $, counter ) {
 
935
        $( function() {
 
936
                var $content = $( '#content' ),
 
937
                        $count = $( '#wp-word-count' ).find( '.word-count' ),
 
938
                        prevCount = 0,
 
939
                        contentEditor;
 
940
 
 
941
                function update() {
 
942
                        var text, count;
 
943
 
 
944
                        if ( ! contentEditor || contentEditor.isHidden() ) {
 
945
                                text = $content.val();
 
946
                        } else {
 
947
                                text = contentEditor.getContent( { format: 'raw' } );
 
948
                        }
 
949
 
 
950
                        count = counter.count( text );
 
951
 
 
952
                        if ( count !== prevCount ) {
 
953
                                $count.text( count );
 
954
                        }
 
955
 
 
956
                        prevCount = count;
 
957
                }
 
958
 
 
959
                $( document ).on( 'tinymce-editor-init', function( event, editor ) {
 
960
                        if ( editor.id !== 'content' ) {
 
961
                                return;
 
962
                        }
 
963
 
 
964
                        contentEditor = editor;
 
965
 
 
966
                        editor.on( 'nodechange keyup', _.debounce( update, 1000 ) );
 
967
                } );
 
968
 
 
969
                $content.on( 'input keyup', _.debounce( update, 1000 ) );
 
970
 
 
971
                update();
 
972
        } );
 
973
} )( jQuery, new wp.utils.WordCounter() );