1 /* global postL10n, ajaxurl, wpAjax, setPostThumbnailL10n, postboxes, pagenow, tinymce, alert, deleteUserSetting */
2 /* global theList:true, theExtraList:true, getUserSetting, setUserSetting, commentReply */
4 var commentsBox, WPSetThumbnailHTML, WPSetThumbnailID, WPRemoveThumbnail, wptitlehint, makeSlugeditClickable, editPermalink;
5 // Back-compat: prevent fatal errors
6 makeSlugeditClickable = editPermalink = function(){};
8 window.wp = window.wp || {};
11 var titleHasFocus = false;
16 get : function(total, num) {
17 var st = this.st, data;
23 $( '#commentsdiv .spinner' ).addClass( 'is-active' );
26 'action' : 'get-comments',
28 '_ajax_nonce' : $('#add_comment_nonce').val(),
29 'p' : $('#post_ID').val(),
36 r = wpAjax.parseAjaxResponse(r);
37 $('#commentsdiv .widefat').show();
38 $( '#commentsdiv .spinner' ).removeClass( 'is-active' );
40 if ( 'object' == typeof r && r.responses[0] ) {
41 $('#the-comment-list').append( r.responses[0].data );
43 theList = theExtraList = null;
44 $( 'a[className*=\':\']' ).unbind();
46 if ( commentsBox.st > commentsBox.total )
47 $('#show-comments').hide();
49 $('#show-comments').show().children('a').html(postL10n.showcomm);
52 } else if ( 1 == r ) {
53 $('#show-comments').html(postL10n.endcomm);
57 $('#the-comment-list').append('<tr><td colspan="2">'+wpAjax.broken+'</td></tr>');
64 load: function(total){
65 this.st = jQuery('#the-comment-list tr.comment:visible').length;
70 WPSetThumbnailHTML = function(html){
71 $('.inside', '#postimagediv').html(html);
74 WPSetThumbnailID = function(id){
75 var field = $('input[value="_thumbnail_id"]', '#list-table');
76 if ( field.length > 0 ) {
77 $('#meta\\[' + field.attr('id').match(/[0-9]+/) + '\\]\\[value\\]').text(id);
81 WPRemoveThumbnail = function(nonce){
83 action: 'set-post-thumbnail', post_id: $( '#post_ID' ).val(), thumbnail_id: -1, _ajax_nonce: nonce, cookie: encodeURIComponent( document.cookie )
86 alert( setPostThumbnailL10n.error );
88 WPSetThumbnailHTML(str);
94 $(document).on( 'heartbeat-send.refresh-lock', function( e, data ) {
95 var lock = $('#active_post_lock').val(),
96 post_id = $('#post_ID').val(),
99 if ( ! post_id || ! $('#post-lock-dialog').length )
102 send.post_id = post_id;
107 data['wp-refresh-post-lock'] = send;
109 }).on( 'heartbeat-tick.refresh-lock', function( e, data ) {
110 // Post locks: update the lock string or show the dialog if somebody has taken over editing
111 var received, wrap, avatar;
113 if ( data['wp-refresh-post-lock'] ) {
114 received = data['wp-refresh-post-lock'];
116 if ( received.lock_error ) {
117 // show "editing taken over" message
118 wrap = $('#post-lock-dialog');
120 if ( wrap.length && ! wrap.is(':visible') ) {
122 // Save the latest changes and disable
123 $(document).one( 'heartbeat-tick', function() {
124 wp.autosave.server.suspend();
125 wrap.removeClass('saving').addClass('saved');
126 $(window).off( 'beforeunload.edit-post' );
129 wrap.addClass('saving');
130 wp.autosave.server.triggerSave();
133 if ( received.lock_error.avatar_src ) {
134 avatar = $( '<img class="avatar avatar-64 photo" width="64" height="64" alt="" />' ).attr( 'src', received.lock_error.avatar_src.replace( /&/g, '&' ) );
135 wrap.find('div.post-locked-avatar').empty().append( avatar );
138 wrap.show().find('.currently-editing').text( received.lock_error.text );
139 wrap.find('.wp-tab-first').focus();
141 } else if ( received.new_lock ) {
142 $('#active_post_lock').val( received.new_lock );
145 }).on( 'before-autosave.update-post-slug', function() {
146 titleHasFocus = document.activeElement && document.activeElement.id === 'title';
147 }).on( 'after-autosave.update-post-slug', function() {
148 // Create slug area only if not already there
149 // and the title field was not focused (user was not typing a title) when autosave ran
150 if ( ! $('#edit-slug-box > *').length && ! titleHasFocus ) {
152 action: 'sample-permalink',
153 post_id: $('#post_ID').val(),
154 new_title: $('#title').val(),
155 samplepermalinknonce: $('#samplepermalinknonce').val()
158 if ( data != '-1' ) {
159 $('#edit-slug-box').html(data);
171 function schedule() {
173 window.clearTimeout( timeout );
174 timeout = window.setTimeout( function(){ check = true; }, 300000 );
177 $(document).on( 'heartbeat-send.wp-refresh-nonces', function( e, data ) {
179 $authCheck = $('#wp-auth-check-wrap');
181 if ( check || ( $authCheck.length && ! $authCheck.hasClass( 'hidden' ) ) ) {
182 if ( ( post_id = $('#post_ID').val() ) && $('#_wpnonce').val() ) {
183 data['wp-refresh-post-nonces'] = {
188 }).on( 'heartbeat-tick.wp-refresh-nonces', function( e, data ) {
189 var nonces = data['wp-refresh-post-nonces'];
194 if ( nonces.replace ) {
195 $.each( nonces.replace, function( selector, value ) {
196 $( '#' + selector ).val( value );
200 if ( nonces.heartbeatNonce )
201 window.heartbeatSettings.nonce = nonces.heartbeatNonce;
203 }).ready( function() {
208 jQuery(document).ready( function($) {
209 var stamp, visibility, $submitButtons, updateVisibility, updateText,
211 $textarea = $('#content'),
212 $document = $(document),
213 postId = $('#post_ID').val() || 0,
214 $submitpost = $('#submitpost'),
216 $postVisibilitySelect = $('#post-visibility-select'),
217 $timestampdiv = $('#timestampdiv'),
218 $postStatusSelect = $('#post-status-select'),
219 isMac = window.navigator.platform ? window.navigator.platform.indexOf( 'Mac' ) !== -1 : false;
221 postboxes.add_postbox_toggles(pagenow);
223 // Clear the window name. Otherwise if this is a former preview window where the user navigated to edit another post,
224 // and the first post is still being edited, clicking Preview there will use this window to show the preview.
227 // Post locks: contain focus inside the dialog. If the dialog is shown, focus the first item.
228 $('#post-lock-dialog .notification-dialog').on( 'keydown', function(e) {
232 var target = $(e.target);
234 if ( target.hasClass('wp-tab-first') && e.shiftKey ) {
235 $(this).find('.wp-tab-last').focus();
237 } else if ( target.hasClass('wp-tab-last') && ! e.shiftKey ) {
238 $(this).find('.wp-tab-first').focus();
241 }).filter(':visible').find('.wp-tab-first').focus();
243 // Set the heartbeat interval to 15 sec. if post lock dialogs are enabled
244 if ( wp.heartbeat && $('#post-lock-dialog').length ) {
245 wp.heartbeat.interval( 15 );
248 // The form is being submitted by the user
249 $submitButtons = $submitpost.find( ':submit, a.submitdelete, #post-preview' ).on( 'click.edit-post', function( event ) {
250 var $button = $(this);
252 if ( $button.hasClass('disabled') ) {
253 event.preventDefault();
257 if ( $button.hasClass('submitdelete') || $button.is( '#post-preview' ) ) {
261 // The form submission can be blocked from JS or by using HTML 5.0 validation on some fields.
262 // Run this only on an actual 'submit'.
263 $('form#post').off( 'submit.edit-post' ).on( 'submit.edit-post', function( event ) {
264 if ( event.isDefaultPrevented() ) {
270 wp.autosave.server.suspend();
273 if ( typeof commentReply !== 'undefined' ) {
275 * Close the comment edit/reply form if open to stop the form
276 * action from interfering with the post's form action.
278 commentReply.close();
282 $(window).off( 'beforeunload.edit-post' );
284 $submitButtons.addClass( 'disabled' );
286 if ( $button.attr('id') === 'publish' ) {
287 $submitpost.find( '#major-publishing-actions .spinner' ).addClass( 'is-active' );
289 $submitpost.find( '#minor-publishing .spinner' ).addClass( 'is-active' );
294 // Submit the form saving a draft or an autosave, and show a preview in a new tab
295 $('#post-preview').on( 'click.post-preview', function( event ) {
297 $form = $('form#post'),
298 $previewField = $('input#wp-preview'),
299 target = $this.attr('target') || 'wp-preview',
300 ua = navigator.userAgent.toLowerCase();
302 event.preventDefault();
304 if ( $this.hasClass('disabled') ) {
309 wp.autosave.server.tempBlockSave();
312 $previewField.val('dopreview');
313 $form.attr( 'target', target ).submit().attr( 'target', '' );
315 // Workaround for WebKit bug preventing a form submitting twice to the same action.
316 // https://bugs.webkit.org/show_bug.cgi?id=28633
317 if ( ua.indexOf('safari') !== -1 && ua.indexOf('chrome') === -1 ) {
318 $form.attr( 'action', function( index, value ) {
319 return value + '?t=' + ( new Date() ).getTime();
323 $previewField.val('');
326 // This code is meant to allow tabbing from Title to Post content.
327 $('#title').on( 'keydown.editor-focus', function( event ) {
330 if ( event.keyCode === 9 && ! event.ctrlKey && ! event.altKey && ! event.shiftKey ) {
331 editor = typeof tinymce != 'undefined' && tinymce.get('content');
333 if ( editor && ! editor.isHidden() ) {
335 } else if ( $textarea.length ) {
341 event.preventDefault();
345 // Autosave new posts after a title is typed
346 if ( $( '#auto_draft' ).val() ) {
347 $( '#title' ).blur( function() {
350 if ( ! this.value || $('#edit-slug-box > *').length ) {
354 // Cancel the autosave when the blur was triggered by the user submitting the form
355 $('form#post').one( 'submit', function() {
359 window.setTimeout( function() {
360 if ( ! cancel && wp.autosave ) {
361 wp.autosave.server.triggerSave();
367 $document.on( 'autosave-disable-buttons.edit-post', function() {
368 $submitButtons.addClass( 'disabled' );
369 }).on( 'autosave-enable-buttons.edit-post', function() {
370 if ( ! wp.heartbeat || ! wp.heartbeat.hasConnectionError() ) {
371 $submitButtons.removeClass( 'disabled' );
373 }).on( 'before-autosave.edit-post', function() {
374 $( '.autosave-message' ).text( postL10n.savingText );
375 }).on( 'after-autosave.edit-post', function( event, data ) {
376 $( '.autosave-message' ).text( data.message );
378 if ( $( document.body ).hasClass( 'post-new-php' ) ) {
379 $( '.submitbox .submitdelete' ).show();
383 $(window).on( 'beforeunload.edit-post', function() {
384 var editor = typeof tinymce !== 'undefined' && tinymce.get('content');
386 if ( ( editor && ! editor.isHidden() && editor.isDirty() ) ||
387 ( wp.autosave && wp.autosave.server.postChanged() ) ) {
389 return postL10n.saveAlert;
391 }).on( 'unload.edit-post', function( event ) {
392 if ( ! releaseLock ) {
396 // Unload is triggered (by hand) on removing the Thickbox iframe.
397 // Make sure we process only the main document unload.
398 if ( event.target && event.target.nodeName != '#document' ) {
407 action: 'wp-remove-post-lock',
408 _wpnonce: $('#_wpnonce').val(),
409 post_ID: $('#post_ID').val(),
410 active_post_lock: $('#active_post_lock').val()
416 if ( $('#tagsdiv-post_tag').length ) {
417 window.tagBox && window.tagBox.init();
419 $('.meta-box-sortables').children('div.postbox').each(function(){
420 if ( this.id.indexOf('tagsdiv-') === 0 ) {
421 window.tagBox && window.tagBox.init();
428 $('.categorydiv').each( function(){
429 var this_id = $(this).attr('id'), catAddBefore, catAddAfter, taxonomyParts, taxonomy, settingName;
431 taxonomyParts = this_id.split('-');
432 taxonomyParts.shift();
433 taxonomy = taxonomyParts.join('-');
434 settingName = taxonomy + '_tab';
435 if ( taxonomy == 'category' )
436 settingName = 'cats';
438 // TODO: move to jQuery 1.3+, support for multiple hierarchical taxonomies, see wp-lists.js
439 $('a', '#' + taxonomy + '-tabs').click( function( e ) {
441 var t = $(this).attr('href');
442 $(this).parent().addClass('tabs').siblings('li').removeClass('tabs');
443 $('#' + taxonomy + '-tabs').siblings('.tabs-panel').hide();
445 if ( '#' + taxonomy + '-all' == t )
446 deleteUserSetting( settingName );
448 setUserSetting( settingName, 'pop' );
451 if ( getUserSetting( settingName ) )
452 $('a[href="#' + taxonomy + '-pop"]', '#' + taxonomy + '-tabs').click();
455 $( '#new' + taxonomy ).one( 'focus', function() { $( this ).val( '' ).removeClass( 'form-input-tip' ); } );
457 $('#new' + taxonomy).keypress( function(event){
458 if( 13 === event.keyCode ) {
459 event.preventDefault();
460 $('#' + taxonomy + '-add-submit').click();
463 $('#' + taxonomy + '-add-submit').click( function(){ $('#new' + taxonomy).focus(); });
465 catAddBefore = function( s ) {
466 if ( !$('#new'+taxonomy).val() )
468 s.data += '&' + $( ':checked', '#'+taxonomy+'checklist' ).serialize();
469 $( '#' + taxonomy + '-add-submit' ).prop( 'disabled', true );
473 catAddAfter = function( r, s ) {
474 var sup, drop = $('#new'+taxonomy+'_parent');
476 $( '#' + taxonomy + '-add-submit' ).prop( 'disabled', false );
477 if ( 'undefined' != s.parsed.responses[0] && (sup = s.parsed.responses[0].supplemental.newcat_parent) ) {
483 $('#' + taxonomy + 'checklist').wpList({
485 response: taxonomy + '-ajax-response',
486 addBefore: catAddBefore,
487 addAfter: catAddAfter
490 $('#' + taxonomy + '-add-toggle').click( function( e ) {
492 $('#' + taxonomy + '-adder').toggleClass( 'wp-hidden-children' );
493 $('a[href="#' + taxonomy + '-all"]', '#' + taxonomy + '-tabs').click();
494 $('#new'+taxonomy).focus();
497 $('#' + taxonomy + 'checklist, #' + taxonomy + 'checklist-pop').on( 'click', 'li.popular-category > label input[type="checkbox"]', function() {
498 var t = $(this), c = t.is(':checked'), id = t.val();
499 if ( id && t.parents('#taxonomy-'+taxonomy).length )
500 $('#in-' + taxonomy + '-' + id + ', #in-popular-' + taxonomy + '-' + id).prop( 'checked', c );
506 if ( $('#postcustom').length ) {
507 $( '#the-list' ).wpList( { addAfter: function() {
508 $('table#list-table').show();
509 }, addBefore: function( s ) {
510 s.data += '&post_id=' + $('#post_ID').val();
517 if ( $('#submitdiv').length ) {
518 stamp = $('#timestamp').html();
519 visibility = $('#post-visibility-display').html();
521 updateVisibility = function() {
522 if ( $postVisibilitySelect.find('input:radio:checked').val() != 'public' ) {
523 $('#sticky').prop('checked', false);
524 $('#sticky-span').hide();
526 $('#sticky-span').show();
528 if ( $postVisibilitySelect.find('input:radio:checked').val() != 'password' ) {
529 $('#password-span').hide();
531 $('#password-span').show();
535 updateText = function() {
537 if ( ! $timestampdiv.length )
540 var attemptedDate, originalDate, currentDate, publishOn, postStatus = $('#post_status'),
541 optPublish = $('option[value="publish"]', postStatus), aa = $('#aa').val(),
542 mm = $('#mm').val(), jj = $('#jj').val(), hh = $('#hh').val(), mn = $('#mn').val();
544 attemptedDate = new Date( aa, mm - 1, jj, hh, mn );
545 originalDate = new Date( $('#hidden_aa').val(), $('#hidden_mm').val() -1, $('#hidden_jj').val(), $('#hidden_hh').val(), $('#hidden_mn').val() );
546 currentDate = new Date( $('#cur_aa').val(), $('#cur_mm').val() -1, $('#cur_jj').val(), $('#cur_hh').val(), $('#cur_mn').val() );
548 if ( attemptedDate.getFullYear() != aa || (1 + attemptedDate.getMonth()) != mm || attemptedDate.getDate() != jj || attemptedDate.getMinutes() != mn ) {
549 $timestampdiv.find('.timestamp-wrap').addClass('form-invalid');
552 $timestampdiv.find('.timestamp-wrap').removeClass('form-invalid');
555 if ( attemptedDate > currentDate && $('#original_post_status').val() != 'future' ) {
556 publishOn = postL10n.publishOnFuture;
557 $('#publish').val( postL10n.schedule );
558 } else if ( attemptedDate <= currentDate && $('#original_post_status').val() != 'publish' ) {
559 publishOn = postL10n.publishOn;
560 $('#publish').val( postL10n.publish );
562 publishOn = postL10n.publishOnPast;
563 $('#publish').val( postL10n.update );
565 if ( originalDate.toUTCString() == attemptedDate.toUTCString() ) { //hack
566 $('#timestamp').html(stamp);
568 $('#timestamp').html(
569 '\n' + publishOn + ' <b>' +
571 .replace( '%1$s', $( 'option[value="' + mm + '"]', '#mm' ).attr( 'data-text' ) )
572 .replace( '%2$s', parseInt( jj, 10 ) )
573 .replace( '%3$s', aa )
574 .replace( '%4$s', ( '00' + hh ).slice( -2 ) )
575 .replace( '%5$s', ( '00' + mn ).slice( -2 ) ) +
580 if ( $postVisibilitySelect.find('input:radio:checked').val() == 'private' ) {
581 $('#publish').val( postL10n.update );
582 if ( 0 === optPublish.length ) {
583 postStatus.append('<option value="publish">' + postL10n.privatelyPublished + '</option>');
585 optPublish.html( postL10n.privatelyPublished );
587 $('option[value="publish"]', postStatus).prop('selected', true);
588 $('#misc-publishing-actions .edit-post-status').hide();
590 if ( $('#original_post_status').val() == 'future' || $('#original_post_status').val() == 'draft' ) {
591 if ( optPublish.length ) {
593 postStatus.val($('#hidden_post_status').val());
596 optPublish.html( postL10n.published );
598 if ( postStatus.is(':hidden') )
599 $('#misc-publishing-actions .edit-post-status').show();
601 $('#post-status-display').html($('option:selected', postStatus).text());
602 if ( $('option:selected', postStatus).val() == 'private' || $('option:selected', postStatus).val() == 'publish' ) {
603 $('#save-post').hide();
605 $('#save-post').show();
606 if ( $('option:selected', postStatus).val() == 'pending' ) {
607 $('#save-post').show().val( postL10n.savePending );
609 $('#save-post').show().val( postL10n.saveDraft );
615 $( '#visibility .edit-visibility').click( function( e ) {
617 if ( $postVisibilitySelect.is(':hidden') ) {
619 $postVisibilitySelect.slideDown( 'fast', function() {
620 $postVisibilitySelect.find( 'input[type="radio"]' ).first().focus();
626 $postVisibilitySelect.find('.cancel-post-visibility').click( function( event ) {
627 $postVisibilitySelect.slideUp('fast');
628 $('#visibility-radio-' + $('#hidden-post-visibility').val()).prop('checked', true);
629 $('#post_password').val($('#hidden-post-password').val());
630 $('#sticky').prop('checked', $('#hidden-post-sticky').prop('checked'));
631 $('#post-visibility-display').html(visibility);
632 $('#visibility .edit-visibility').show().focus();
634 event.preventDefault();
637 $postVisibilitySelect.find('.save-post-visibility').click( function( event ) { // crazyhorse - multiple ok cancels
638 $postVisibilitySelect.slideUp('fast');
639 $('#visibility .edit-visibility').show().focus();
642 if ( $postVisibilitySelect.find('input:radio:checked').val() != 'public' ) {
643 $('#sticky').prop('checked', false);
646 if ( $('#sticky').prop('checked') ) {
652 $('#post-visibility-display').html( postL10n[ $postVisibilitySelect.find('input:radio:checked').val() + sticky ] );
653 event.preventDefault();
656 $postVisibilitySelect.find('input:radio').change( function() {
660 $timestampdiv.siblings('a.edit-timestamp').click( function( event ) {
661 if ( $timestampdiv.is( ':hidden' ) ) {
662 $timestampdiv.slideDown( 'fast', function() {
663 $( 'input, select', $timestampdiv.find( '.timestamp-wrap' ) ).first().focus();
667 event.preventDefault();
670 $timestampdiv.find('.cancel-timestamp').click( function( event ) {
671 $timestampdiv.slideUp('fast').siblings('a.edit-timestamp').show().focus();
672 $('#mm').val($('#hidden_mm').val());
673 $('#jj').val($('#hidden_jj').val());
674 $('#aa').val($('#hidden_aa').val());
675 $('#hh').val($('#hidden_hh').val());
676 $('#mn').val($('#hidden_mn').val());
678 event.preventDefault();
681 $timestampdiv.find('.save-timestamp').click( function( event ) { // crazyhorse - multiple ok cancels
682 if ( updateText() ) {
683 $timestampdiv.slideUp('fast');
684 $timestampdiv.siblings('a.edit-timestamp').show().focus();
686 event.preventDefault();
689 $('#post').on( 'submit', function( event ) {
690 if ( ! updateText() ) {
691 event.preventDefault();
692 $timestampdiv.show();
695 wp.autosave.enableButtons();
698 $( '#publishing-action .spinner' ).removeClass( 'is-active' );
702 $postStatusSelect.siblings('a.edit-post-status').click( function( event ) {
703 if ( $postStatusSelect.is( ':hidden' ) ) {
704 $postStatusSelect.slideDown( 'fast', function() {
705 $postStatusSelect.find('select').focus();
709 event.preventDefault();
712 $postStatusSelect.find('.save-post-status').click( function( event ) {
713 $postStatusSelect.slideUp( 'fast' ).siblings( 'a.edit-post-status' ).show().focus();
715 event.preventDefault();
718 $postStatusSelect.find('.cancel-post-status').click( function( event ) {
719 $postStatusSelect.slideUp( 'fast' ).siblings( 'a.edit-post-status' ).show().focus();
720 $('#post_status').val( $('#hidden_post_status').val() );
722 event.preventDefault();
727 function editPermalink() {
731 real_slug = $('#post_name'),
732 revert_slug = real_slug.val(),
733 permalink = $( '#sample-permalink' ),
734 permalinkOrig = permalink.html(),
735 permalinkInner = $( '#sample-permalink a' ).html(),
736 buttons = $('#edit-slug-buttons'),
737 buttonsOrig = buttons.html(),
738 full = $('#editable-post-name-full');
740 // Deal with Twemoji in the post-name
741 full.find( 'img' ).replaceWith( function() { return this.alt; } );
744 permalink.html( permalinkInner );
745 $el = $( '#editable-post-name' );
746 revert_e = $el.html();
748 buttons.html( '<button type="button" class="save button button-small">' + postL10n.ok + '</button> <button type="button" class="cancel button-link">' + postL10n.cancel + '</button>' );
749 buttons.children( '.save' ).click( function() {
750 var new_slug = $el.children( 'input' ).val();
752 if ( new_slug == $('#editable-post-name-full').text() ) {
753 buttons.children('.cancel').click();
757 action: 'sample-permalink',
760 new_title: $('#title').val(),
761 samplepermalinknonce: $('#samplepermalinknonce').val()
763 var box = $('#edit-slug-box');
765 if (box.hasClass('hidden')) {
766 box.fadeIn('fast', function () {
767 box.removeClass('hidden');
771 buttons.html(buttonsOrig);
772 permalink.html(permalinkOrig);
773 real_slug.val(new_slug);
774 $( '.edit-slug' ).focus();
775 wp.a11y.speak( postL10n.permalinkSaved );
779 buttons.children( '.cancel' ).click( function() {
780 $('#view-post-btn').show();
782 buttons.html(buttonsOrig);
783 permalink.html(permalinkOrig);
784 real_slug.val(revert_slug);
785 $( '.edit-slug' ).focus();
788 for ( i = 0; i < full.length; ++i ) {
789 if ( '%' == full.charAt(i) )
793 slug_value = ( c > full.length / 4 ) ? '' : full;
794 $el.html( '<input type="text" id="new-post-slug" value="' + slug_value + '" autocomplete="off" />' ).children( 'input' ).keydown( function( e ) {
796 // On enter, just save the new slug, don't save the post.
799 buttons.children( '.save' ).click();
802 buttons.children( '.cancel' ).click();
804 } ).keyup( function() {
805 real_slug.val( this.value );
809 $( '#titlediv' ).on( 'click', '.edit-slug', function() {
813 wptitlehint = function(id) {
816 var title = $('#' + id), titleprompt = $('#' + id + '-prompt-text');
818 if ( '' === title.val() )
819 titleprompt.removeClass('screen-reader-text');
821 titleprompt.click(function(){
822 $(this).addClass('screen-reader-text');
826 title.blur(function(){
827 if ( '' === this.value )
828 titleprompt.removeClass('screen-reader-text');
830 titleprompt.addClass('screen-reader-text');
831 }).keydown(function(e){
832 titleprompt.addClass('screen-reader-text');
839 // Resize the visual and text editors
841 var editor, offset, mce,
842 $handle = $('#post-status-info'),
843 $postdivrich = $('#postdivrich');
845 // No point for touch devices
846 if ( ! $textarea.length || 'ontouchstart' in window ) {
847 // Hide the resize handle
848 $('#content-resize-handle').hide();
852 function dragging( event ) {
853 if ( $postdivrich.hasClass( 'wp-editor-expand' ) ) {
858 editor.theme.resizeTo( null, offset + event.pageY );
860 $textarea.height( Math.max( 50, offset + event.pageY ) );
863 event.preventDefault();
867 var height, toolbarHeight;
869 if ( $postdivrich.hasClass( 'wp-editor-expand' ) ) {
875 toolbarHeight = parseInt( $( '#wp-content-editor-container .mce-toolbar-grp' ).height(), 10 );
877 if ( toolbarHeight < 10 || toolbarHeight > 200 ) {
881 height = parseInt( $('#content_ifr').css('height'), 10 ) + toolbarHeight - 28;
884 height = parseInt( $textarea.css('height'), 10 );
887 $document.off( '.wp-editor-resize' );
890 if ( height && height > 50 && height < 5000 ) {
891 setUserSetting( 'ed_size', height );
895 $handle.on( 'mousedown.wp-editor-resize', function( event ) {
896 if ( typeof tinymce !== 'undefined' ) {
897 editor = tinymce.get('content');
900 if ( editor && ! editor.isHidden() ) {
902 offset = $('#content_ifr').height() - event.pageY;
905 offset = $textarea.height() - event.pageY;
909 $document.on( 'mousemove.wp-editor-resize', dragging )
910 .on( 'mouseup.wp-editor-resize mouseleave.wp-editor-resize', endDrag );
912 event.preventDefault();
913 }).on( 'mouseup.wp-editor-resize', endDrag );
916 if ( typeof tinymce !== 'undefined' ) {
917 // When changing post formats, change the editor body class
918 $( '#post-formats-select input.post-format' ).on( 'change.set-editor-class', function() {
919 var editor, body, format = this.id;
921 if ( format && $( this ).prop( 'checked' ) && ( editor = tinymce.get( 'content' ) ) ) {
922 body = editor.getBody();
923 body.className = body.className.replace( /\bpost-format-[^ ]+/, '' );
924 editor.dom.addClass( body, format == 'post-format-0' ? 'post-format-standard' : format );
925 $( document ).trigger( 'editor-classchange' );
930 // Save on pressing Ctrl/Command + S in the Text editor
931 $textarea.on( 'keydown.wp-autosave', function( event ) {
932 if ( event.which === 83 ) {
933 if ( event.shiftKey || event.altKey || ( isMac && ( ! event.metaKey || event.ctrlKey ) ) || ( ! isMac && ! event.ctrlKey ) ) {
937 wp.autosave && wp.autosave.server.triggerSave();
938 event.preventDefault();
942 if ( $( '#original_post_status' ).val() === 'auto-draft' && window.history.replaceState ) {
945 $( '#publish' ).on( 'click', function() {
946 location = window.location.href;
947 location += ( location.indexOf( '?' ) !== -1 ) ? '&' : '?';
948 location += 'wp-post-new-reload=true';
950 window.history.replaceState( null, null, location );
955 ( function( $, counter ) {
957 var $content = $( '#content' ),
958 $count = $( '#wp-word-count' ).find( '.word-count' ),
965 if ( ! contentEditor || contentEditor.isHidden() ) {
966 text = $content.val();
968 text = contentEditor.getContent( { format: 'raw' } );
971 count = counter.count( text );
973 if ( count !== prevCount ) {
974 $count.text( count );
980 $( document ).on( 'tinymce-editor-init', function( event, editor ) {
981 if ( editor.id !== 'content' ) {
985 contentEditor = editor;
987 editor.on( 'nodechange keyup', _.debounce( update, 1000 ) );
990 $content.on( 'input keyup', _.debounce( update, 1000 ) );
994 } )( jQuery, new wp.utils.WordCounter() );