]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/js/post.js
WordPress 4.3
[autoinstalls/wordpress.git] / wp-admin / js / post.js
1 /* global postL10n, ajaxurl, wpAjax, setPostThumbnailL10n, postboxes, pagenow, tinymce, alert, deleteUserSetting */
2 /* global theList:true, theExtraList:true, getUserSetting, setUserSetting, commentReply */
3
4 var commentsBox, WPSetThumbnailHTML, WPSetThumbnailID, WPRemoveThumbnail, wptitlehint, makeSlugeditClickable, editPermalink;
5 // Back-compat: prevent fatal errors
6 makeSlugeditClickable = editPermalink = function(){};
7
8 window.wp = window.wp || {};
9
10 ( function( $ ) {
11         var titleHasFocus = false;
12
13 commentsBox = {
14         st : 0,
15
16         get : function(total, num) {
17                 var st = this.st, data;
18                 if ( ! num )
19                         num = 20;
20
21                 this.st += num;
22                 this.total = total;
23                 $( '#commentsdiv .spinner' ).addClass( 'is-active' );
24
25                 data = {
26                         'action' : 'get-comments',
27                         'mode' : 'single',
28                         '_ajax_nonce' : $('#add_comment_nonce').val(),
29                         'p' : $('#post_ID').val(),
30                         'start' : st,
31                         'number' : num
32                 };
33
34                 $.post(ajaxurl, data,
35                         function(r) {
36                                 r = wpAjax.parseAjaxResponse(r);
37                                 $('#commentsdiv .widefat').show();
38                                 $( '#commentsdiv .spinner' ).removeClass( 'is-active' );
39
40                                 if ( 'object' == typeof r && r.responses[0] ) {
41                                         $('#the-comment-list').append( r.responses[0].data );
42
43                                         theList = theExtraList = null;
44                                         $( 'a[className*=\':\']' ).unbind();
45
46                                         if ( commentsBox.st > commentsBox.total )
47                                                 $('#show-comments').hide();
48                                         else
49                                                 $('#show-comments').show().children('a').html(postL10n.showcomm);
50
51                                         return;
52                                 } else if ( 1 == r ) {
53                                         $('#show-comments').html(postL10n.endcomm);
54                                         return;
55                                 }
56
57                                 $('#the-comment-list').append('<tr><td colspan="2">'+wpAjax.broken+'</td></tr>');
58                         }
59                 );
60
61                 return false;
62         }
63 };
64
65 WPSetThumbnailHTML = function(html){
66         $('.inside', '#postimagediv').html(html);
67 };
68
69 WPSetThumbnailID = function(id){
70         var field = $('input[value="_thumbnail_id"]', '#list-table');
71         if ( field.size() > 0 ) {
72                 $('#meta\\[' + field.attr('id').match(/[0-9]+/) + '\\]\\[value\\]').text(id);
73         }
74 };
75
76 WPRemoveThumbnail = function(nonce){
77         $.post(ajaxurl, {
78                 action: 'set-post-thumbnail', post_id: $( '#post_ID' ).val(), thumbnail_id: -1, _ajax_nonce: nonce, cookie: encodeURIComponent( document.cookie )
79         }, function(str){
80                 if ( str == '0' ) {
81                         alert( setPostThumbnailL10n.error );
82                 } else {
83                         WPSetThumbnailHTML(str);
84                 }
85         }
86         );
87 };
88
89 $(document).on( 'heartbeat-send.refresh-lock', function( e, data ) {
90         var lock = $('#active_post_lock').val(),
91                 post_id = $('#post_ID').val(),
92                 send = {};
93
94         if ( ! post_id || ! $('#post-lock-dialog').length )
95                 return;
96
97         send.post_id = post_id;
98
99         if ( lock )
100                 send.lock = lock;
101
102         data['wp-refresh-post-lock'] = send;
103
104 }).on( 'heartbeat-tick.refresh-lock', function( e, data ) {
105         // Post locks: update the lock string or show the dialog if somebody has taken over editing
106         var received, wrap, avatar;
107
108         if ( data['wp-refresh-post-lock'] ) {
109                 received = data['wp-refresh-post-lock'];
110
111                 if ( received.lock_error ) {
112                         // show "editing taken over" message
113                         wrap = $('#post-lock-dialog');
114
115                         if ( wrap.length && ! wrap.is(':visible') ) {
116                                 if ( wp.autosave ) {
117                                         // Save the latest changes and disable
118                                         $(document).one( 'heartbeat-tick', function() {
119                                                 wp.autosave.server.suspend();
120                                                 wrap.removeClass('saving').addClass('saved');
121                                                 $(window).off( 'beforeunload.edit-post' );
122                                         });
123
124                                         wrap.addClass('saving');
125                                         wp.autosave.server.triggerSave();
126                                 }
127
128                                 if ( received.lock_error.avatar_src ) {
129                                         avatar = $('<img class="avatar avatar-64 photo" width="64" height="64" />').attr( 'src', received.lock_error.avatar_src.replace(/&amp;/g, '&') );
130                                         wrap.find('div.post-locked-avatar').empty().append( avatar );
131                                 }
132
133                                 wrap.show().find('.currently-editing').text( received.lock_error.text );
134                                 wrap.find('.wp-tab-first').focus();
135                         }
136                 } else if ( received.new_lock ) {
137                         $('#active_post_lock').val( received.new_lock );
138                 }
139         }
140 }).on( 'before-autosave.update-post-slug', function() {
141         titleHasFocus = document.activeElement && document.activeElement.id === 'title';
142 }).on( 'after-autosave.update-post-slug', function() {
143         // Create slug area only if not already there
144         // and the title field was not focused (user was not typing a title) when autosave ran
145         if ( ! $('#edit-slug-box > *').length && ! titleHasFocus ) {
146                 $.post( ajaxurl, {
147                                 action: 'sample-permalink',
148                                 post_id: $('#post_ID').val(),
149                                 new_title: $('#title').val(),
150                                 samplepermalinknonce: $('#samplepermalinknonce').val()
151                         },
152                         function( data ) {
153                                 if ( data != '-1' ) {
154                                         $('#edit-slug-box').html(data);
155                                 }
156                         }
157                 );
158         }
159 });
160
161 }(jQuery));
162
163 (function($) {
164         var check, timeout;
165
166         function schedule() {
167                 check = false;
168                 window.clearTimeout( timeout );
169                 timeout = window.setTimeout( function(){ check = true; }, 300000 );
170         }
171
172         $(document).on( 'heartbeat-send.wp-refresh-nonces', function( e, data ) {
173                 var post_id,
174                         $authCheck = $('#wp-auth-check-wrap');
175
176                 if ( check || ( $authCheck.length && ! $authCheck.hasClass( 'hidden' ) ) ) {
177                         if ( ( post_id = $('#post_ID').val() ) && $('#_wpnonce').val() ) {
178                                 data['wp-refresh-post-nonces'] = {
179                                         post_id: post_id
180                                 };
181                         }
182                 }
183         }).on( 'heartbeat-tick.wp-refresh-nonces', function( e, data ) {
184                 var nonces = data['wp-refresh-post-nonces'];
185
186                 if ( nonces ) {
187                         schedule();
188
189                         if ( nonces.replace ) {
190                                 $.each( nonces.replace, function( selector, value ) {
191                                         $( '#' + selector ).val( value );
192                                 });
193                         }
194
195                         if ( nonces.heartbeatNonce )
196                                 window.heartbeatSettings.nonce = nonces.heartbeatNonce;
197                 }
198         }).ready( function() {
199                 schedule();
200         });
201 }(jQuery));
202
203 jQuery(document).ready( function($) {
204         var stamp, visibility, $submitButtons, updateVisibility, updateText,
205                 sticky = '',
206                 $textarea = $('#content'),
207                 $document = $(document),
208                 $editSlugWrap = $('#edit-slug-box'),
209                 postId = $('#post_ID').val() || 0,
210                 $submitpost = $('#submitpost'),
211                 releaseLock = true,
212                 $postVisibilitySelect = $('#post-visibility-select'),
213                 $timestampdiv = $('#timestampdiv'),
214                 $postStatusSelect = $('#post-status-select'),
215                 isMac = window.navigator.platform ? window.navigator.platform.indexOf( 'Mac' ) !== -1 : false;
216
217         postboxes.add_postbox_toggles(pagenow);
218
219         // Clear the window name. Otherwise if this is a former preview window where the user navigated to edit another post,
220         // and the first post is still being edited, clicking Preview there will use this window to show the preview.
221         window.name = '';
222
223         // Post locks: contain focus inside the dialog. If the dialog is shown, focus the first item.
224         $('#post-lock-dialog .notification-dialog').on( 'keydown', function(e) {
225                 if ( e.which != 9 )
226                         return;
227
228                 var target = $(e.target);
229
230                 if ( target.hasClass('wp-tab-first') && e.shiftKey ) {
231                         $(this).find('.wp-tab-last').focus();
232                         e.preventDefault();
233                 } else if ( target.hasClass('wp-tab-last') && ! e.shiftKey ) {
234                         $(this).find('.wp-tab-first').focus();
235                         e.preventDefault();
236                 }
237         }).filter(':visible').find('.wp-tab-first').focus();
238
239         // Set the heartbeat interval to 15 sec. if post lock dialogs are enabled
240         if ( wp.heartbeat && $('#post-lock-dialog').length ) {
241                 wp.heartbeat.interval( 15 );
242         }
243
244         // The form is being submitted by the user
245         $submitButtons = $submitpost.find( ':submit, a.submitdelete, #post-preview' ).on( 'click.edit-post', function( event ) {
246                 var $button = $(this);
247
248                 if ( $button.hasClass('disabled') ) {
249                         event.preventDefault();
250                         return;
251                 }
252
253                 if ( $button.hasClass('submitdelete') || $button.is( '#post-preview' ) ) {
254                         return;
255                 }
256
257                 // The form submission can be blocked from JS or by using HTML 5.0 validation on some fields.
258                 // Run this only on an actual 'submit'.
259                 $('form#post').off( 'submit.edit-post' ).on( 'submit.edit-post', function( event ) {
260                         if ( event.isDefaultPrevented() ) {
261                                 return;
262                         }
263
264                         // Stop autosave
265                         if ( wp.autosave ) {
266                                 wp.autosave.server.suspend();
267                         }
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
277                         releaseLock = false;
278                         $(window).off( 'beforeunload.edit-post' );
279
280                         $submitButtons.addClass( 'disabled' );
281
282                         if ( $button.attr('id') === 'publish' ) {
283                                 $submitpost.find( '#major-publishing-actions .spinner' ).addClass( 'is-active' );
284                         } else {
285                                 $submitpost.find( '#minor-publishing .spinner' ).addClass( 'is-active' );
286                         }
287                 });
288         });
289
290         // Submit the form saving a draft or an autosave, and show a preview in a new tab
291         $('#post-preview').on( 'click.post-preview', function( event ) {
292                 var $this = $(this),
293                         $form = $('form#post'),
294                         $previewField = $('input#wp-preview'),
295                         target = $this.attr('target') || 'wp-preview',
296                         ua = navigator.userAgent.toLowerCase();
297
298                 event.preventDefault();
299
300                 if ( $this.hasClass('disabled') ) {
301                         return;
302                 }
303
304                 if ( wp.autosave ) {
305                         wp.autosave.server.tempBlockSave();
306                 }
307
308                 $previewField.val('dopreview');
309                 $form.attr( 'target', target ).submit().attr( 'target', '' );
310
311                 // Workaround for WebKit bug preventing a form submitting twice to the same action.
312                 // https://bugs.webkit.org/show_bug.cgi?id=28633
313                 if ( ua.indexOf('safari') !== -1 && ua.indexOf('chrome') === -1 ) {
314                         $form.attr( 'action', function( index, value ) {
315                                 return value + '?t=' + ( new Date() ).getTime();
316                         });
317                 }
318
319                 $previewField.val('');
320         });
321
322         // This code is meant to allow tabbing from Title to Post content.
323         $('#title').on( 'keydown.editor-focus', function( event ) {
324                 var editor;
325
326                 if ( event.keyCode === 9 && ! event.ctrlKey && ! event.altKey && ! event.shiftKey ) {
327                         editor = typeof tinymce != 'undefined' && tinymce.get('content');
328
329                         if ( editor && ! editor.isHidden() ) {
330                                 editor.focus();
331                         } else if ( $textarea.length ) {
332                                 $textarea.focus();
333                         } else {
334                                 return;
335                         }
336
337                         event.preventDefault();
338                 }
339         });
340
341         // Autosave new posts after a title is typed
342         if ( $( '#auto_draft' ).val() ) {
343                 $( '#title' ).blur( function() {
344                         var cancel;
345
346                         if ( ! this.value || $('#edit-slug-box > *').length ) {
347                                 return;
348                         }
349
350                         // Cancel the autosave when the blur was triggered by the user submitting the form
351                         $('form#post').one( 'submit', function() {
352                                 cancel = true;
353                         });
354
355                         window.setTimeout( function() {
356                                 if ( ! cancel && wp.autosave ) {
357                                         wp.autosave.server.triggerSave();
358                                 }
359                         }, 200 );
360                 });
361         }
362
363         $document.on( 'autosave-disable-buttons.edit-post', function() {
364                 $submitButtons.addClass( 'disabled' );
365         }).on( 'autosave-enable-buttons.edit-post', function() {
366                 if ( ! wp.heartbeat || ! wp.heartbeat.hasConnectionError() ) {
367                         $submitButtons.removeClass( 'disabled' );
368                 }
369         }).on( 'before-autosave.edit-post', function() {
370                 $( '.autosave-message' ).text( postL10n.savingText );
371         }).on( 'after-autosave.edit-post', function( event, data ) {
372                 $( '.autosave-message' ).text( data.message );
373         });
374
375         $(window).on( 'beforeunload.edit-post', function() {
376                 var editor = typeof tinymce !== 'undefined' && tinymce.get('content');
377
378                 if ( ( editor && ! editor.isHidden() && editor.isDirty() ) ||
379                         ( wp.autosave && wp.autosave.server.postChanged() ) ) {
380
381                         return postL10n.saveAlert;
382                 }
383         }).on( 'unload.edit-post', function( event ) {
384                 if ( ! releaseLock ) {
385                         return;
386                 }
387
388                 // Unload is triggered (by hand) on removing the Thickbox iframe.
389                 // Make sure we process only the main document unload.
390                 if ( event.target && event.target.nodeName != '#document' ) {
391                         return;
392                 }
393
394                 $.ajax({
395                         type: 'POST',
396                         url: ajaxurl,
397                         async: false,
398                         data: {
399                                 action: 'wp-remove-post-lock',
400                                 _wpnonce: $('#_wpnonce').val(),
401                                 post_ID: $('#post_ID').val(),
402                                 active_post_lock: $('#active_post_lock').val()
403                         }
404                 });
405         });
406
407         // multi-taxonomies
408         if ( $('#tagsdiv-post_tag').length ) {
409                 window.tagBox && window.tagBox.init();
410         } else {
411                 $('#side-sortables, #normal-sortables, #advanced-sortables').children('div.postbox').each(function(){
412                         if ( this.id.indexOf('tagsdiv-') === 0 ) {
413                                 window.tagBox && window.tagBox.init();
414                                 return false;
415                         }
416                 });
417         }
418
419         // categories
420         $('.categorydiv').each( function(){
421                 var this_id = $(this).attr('id'), catAddBefore, catAddAfter, taxonomyParts, taxonomy, settingName;
422
423                 taxonomyParts = this_id.split('-');
424                 taxonomyParts.shift();
425                 taxonomy = taxonomyParts.join('-');
426                 settingName = taxonomy + '_tab';
427                 if ( taxonomy == 'category' )
428                         settingName = 'cats';
429
430                 // TODO: move to jQuery 1.3+, support for multiple hierarchical taxonomies, see wp-lists.js
431                 $('a', '#' + taxonomy + '-tabs').click( function(){
432                         var t = $(this).attr('href');
433                         $(this).parent().addClass('tabs').siblings('li').removeClass('tabs');
434                         $('#' + taxonomy + '-tabs').siblings('.tabs-panel').hide();
435                         $(t).show();
436                         if ( '#' + taxonomy + '-all' == t )
437                                 deleteUserSetting( settingName );
438                         else
439                                 setUserSetting( settingName, 'pop' );
440                         return false;
441                 });
442
443                 if ( getUserSetting( settingName ) )
444                         $('a[href="#' + taxonomy + '-pop"]', '#' + taxonomy + '-tabs').click();
445
446                 // Ajax Cat
447                 $( '#new' + taxonomy ).one( 'focus', function() { $( this ).val( '' ).removeClass( 'form-input-tip' ); } );
448
449                 $('#new' + taxonomy).keypress( function(event){
450                         if( 13 === event.keyCode ) {
451                                 event.preventDefault();
452                                 $('#' + taxonomy + '-add-submit').click();
453                         }
454                 });
455                 $('#' + taxonomy + '-add-submit').click( function(){ $('#new' + taxonomy).focus(); });
456
457                 catAddBefore = function( s ) {
458                         if ( !$('#new'+taxonomy).val() )
459                                 return false;
460                         s.data += '&' + $( ':checked', '#'+taxonomy+'checklist' ).serialize();
461                         $( '#' + taxonomy + '-add-submit' ).prop( 'disabled', true );
462                         return s;
463                 };
464
465                 catAddAfter = function( r, s ) {
466                         var sup, drop = $('#new'+taxonomy+'_parent');
467
468                         $( '#' + taxonomy + '-add-submit' ).prop( 'disabled', false );
469                         if ( 'undefined' != s.parsed.responses[0] && (sup = s.parsed.responses[0].supplemental.newcat_parent) ) {
470                                 drop.before(sup);
471                                 drop.remove();
472                         }
473                 };
474
475                 $('#' + taxonomy + 'checklist').wpList({
476                         alt: '',
477                         response: taxonomy + '-ajax-response',
478                         addBefore: catAddBefore,
479                         addAfter: catAddAfter
480                 });
481
482                 $('#' + taxonomy + '-add-toggle').click( function() {
483                         $('#' + taxonomy + '-adder').toggleClass( 'wp-hidden-children' );
484                         $('a[href="#' + taxonomy + '-all"]', '#' + taxonomy + '-tabs').click();
485                         $('#new'+taxonomy).focus();
486                         return false;
487                 });
488
489                 $('#' + taxonomy + 'checklist, #' + taxonomy + 'checklist-pop').on( 'click', 'li.popular-category > label input[type="checkbox"]', function() {
490                         var t = $(this), c = t.is(':checked'), id = t.val();
491                         if ( id && t.parents('#taxonomy-'+taxonomy).length )
492                                 $('#in-' + taxonomy + '-' + id + ', #in-popular-' + taxonomy + '-' + id).prop( 'checked', c );
493                 });
494
495         }); // end cats
496
497         // Custom Fields
498         if ( $('#postcustom').length ) {
499                 $( '#the-list' ).wpList( { addAfter: function() {
500                         $('table#list-table').show();
501                 }, addBefore: function( s ) {
502                         s.data += '&post_id=' + $('#post_ID').val();
503                         return s;
504                 }
505                 });
506         }
507
508         // submitdiv
509         if ( $('#submitdiv').length ) {
510                 stamp = $('#timestamp').html();
511                 visibility = $('#post-visibility-display').html();
512
513                 updateVisibility = function() {
514                         if ( $postVisibilitySelect.find('input:radio:checked').val() != 'public' ) {
515                                 $('#sticky').prop('checked', false);
516                                 $('#sticky-span').hide();
517                         } else {
518                                 $('#sticky-span').show();
519                         }
520                         if ( $postVisibilitySelect.find('input:radio:checked').val() != 'password' ) {
521                                 $('#password-span').hide();
522                         } else {
523                                 $('#password-span').show();
524                         }
525                 };
526
527                 updateText = function() {
528
529                         if ( ! $timestampdiv.length )
530                                 return true;
531
532                         var attemptedDate, originalDate, currentDate, publishOn, postStatus = $('#post_status'),
533                                 optPublish = $('option[value="publish"]', postStatus), aa = $('#aa').val(),
534                                 mm = $('#mm').val(), jj = $('#jj').val(), hh = $('#hh').val(), mn = $('#mn').val();
535
536                         attemptedDate = new Date( aa, mm - 1, jj, hh, mn );
537                         originalDate = new Date( $('#hidden_aa').val(), $('#hidden_mm').val() -1, $('#hidden_jj').val(), $('#hidden_hh').val(), $('#hidden_mn').val() );
538                         currentDate = new Date( $('#cur_aa').val(), $('#cur_mm').val() -1, $('#cur_jj').val(), $('#cur_hh').val(), $('#cur_mn').val() );
539
540                         if ( attemptedDate.getFullYear() != aa || (1 + attemptedDate.getMonth()) != mm || attemptedDate.getDate() != jj || attemptedDate.getMinutes() != mn ) {
541                                 $timestampdiv.find('.timestamp-wrap').addClass('form-invalid');
542                                 return false;
543                         } else {
544                                 $timestampdiv.find('.timestamp-wrap').removeClass('form-invalid');
545                         }
546
547                         if ( attemptedDate > currentDate && $('#original_post_status').val() != 'future' ) {
548                                 publishOn = postL10n.publishOnFuture;
549                                 $('#publish').val( postL10n.schedule );
550                         } else if ( attemptedDate <= currentDate && $('#original_post_status').val() != 'publish' ) {
551                                 publishOn = postL10n.publishOn;
552                                 $('#publish').val( postL10n.publish );
553                         } else {
554                                 publishOn = postL10n.publishOnPast;
555                                 $('#publish').val( postL10n.update );
556                         }
557                         if ( originalDate.toUTCString() == attemptedDate.toUTCString() ) { //hack
558                                 $('#timestamp').html(stamp);
559                         } else {
560                                 $('#timestamp').html(
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 ) )
565                                                 .replace( '%3$s', aa )
566                                                 .replace( '%4$s', ( '00' + hh ).slice( -2 ) )
567                                                 .replace( '%5$s', ( '00' + mn ).slice( -2 ) ) +
568                                                 '</b> '
569                                 );
570                         }
571
572                         if ( $postVisibilitySelect.find('input:radio:checked').val() == 'private' ) {
573                                 $('#publish').val( postL10n.update );
574                                 if ( 0 === optPublish.length ) {
575                                         postStatus.append('<option value="publish">' + postL10n.privatelyPublished + '</option>');
576                                 } else {
577                                         optPublish.html( postL10n.privatelyPublished );
578                                 }
579                                 $('option[value="publish"]', postStatus).prop('selected', true);
580                                 $('#misc-publishing-actions .edit-post-status').hide();
581                         } else {
582                                 if ( $('#original_post_status').val() == 'future' || $('#original_post_status').val() == 'draft' ) {
583                                         if ( optPublish.length ) {
584                                                 optPublish.remove();
585                                                 postStatus.val($('#hidden_post_status').val());
586                                         }
587                                 } else {
588                                         optPublish.html( postL10n.published );
589                                 }
590                                 if ( postStatus.is(':hidden') )
591                                         $('#misc-publishing-actions .edit-post-status').show();
592                         }
593                         $('#post-status-display').html($('option:selected', postStatus).text());
594                         if ( $('option:selected', postStatus).val() == 'private' || $('option:selected', postStatus).val() == 'publish' ) {
595                                 $('#save-post').hide();
596                         } else {
597                                 $('#save-post').show();
598                                 if ( $('option:selected', postStatus).val() == 'pending' ) {
599                                         $('#save-post').show().val( postL10n.savePending );
600                                 } else {
601                                         $('#save-post').show().val( postL10n.saveDraft );
602                                 }
603                         }
604                         return true;
605                 };
606
607                 $( '#visibility .edit-visibility').click( function () {
608                         if ( $postVisibilitySelect.is(':hidden') ) {
609                                 updateVisibility();
610                                 $postVisibilitySelect.slideDown( 'fast', function() {
611                                         $postVisibilitySelect.find( 'input[type="radio"]' ).first().focus();
612                                 } );
613                                 $(this).hide();
614                         }
615                         return false;
616                 });
617
618                 $postVisibilitySelect.find('.cancel-post-visibility').click( function( event ) {
619                         $postVisibilitySelect.slideUp('fast');
620                         $('#visibility-radio-' + $('#hidden-post-visibility').val()).prop('checked', true);
621                         $('#post_password').val($('#hidden-post-password').val());
622                         $('#sticky').prop('checked', $('#hidden-post-sticky').prop('checked'));
623                         $('#post-visibility-display').html(visibility);
624                         $('#visibility .edit-visibility').show().focus();
625                         updateText();
626                         event.preventDefault();
627                 });
628
629                 $postVisibilitySelect.find('.save-post-visibility').click( function( event ) { // crazyhorse - multiple ok cancels
630                         $postVisibilitySelect.slideUp('fast');
631                         $('#visibility .edit-visibility').show().focus();
632                         updateText();
633
634                         if ( $postVisibilitySelect.find('input:radio:checked').val() != 'public' ) {
635                                 $('#sticky').prop('checked', false);
636                         } // WEAPON LOCKED
637
638                         if ( $('#sticky').prop('checked') ) {
639                                 sticky = 'Sticky';
640                         } else {
641                                 sticky = '';
642                         }
643
644                         $('#post-visibility-display').html(     postL10n[ $postVisibilitySelect.find('input:radio:checked').val() + sticky ]    );
645                         event.preventDefault();
646                 });
647
648                 $postVisibilitySelect.find('input:radio').change( function() {
649                         updateVisibility();
650                 });
651
652                 $timestampdiv.siblings('a.edit-timestamp').click( function( event ) {
653                         if ( $timestampdiv.is( ':hidden' ) ) {
654                                 $timestampdiv.slideDown( 'fast', function() {
655                                         $( 'input, select', $timestampdiv.find( '.timestamp-wrap' ) ).first().focus();
656                                 } );
657                                 $(this).hide();
658                         }
659                         event.preventDefault();
660                 });
661
662                 $timestampdiv.find('.cancel-timestamp').click( function( event ) {
663                         $timestampdiv.slideUp('fast').siblings('a.edit-timestamp').show().focus();
664                         $('#mm').val($('#hidden_mm').val());
665                         $('#jj').val($('#hidden_jj').val());
666                         $('#aa').val($('#hidden_aa').val());
667                         $('#hh').val($('#hidden_hh').val());
668                         $('#mn').val($('#hidden_mn').val());
669                         updateText();
670                         event.preventDefault();
671                 });
672
673                 $timestampdiv.find('.save-timestamp').click( function( event ) { // crazyhorse - multiple ok cancels
674                         if ( updateText() ) {
675                                 $timestampdiv.slideUp('fast');
676                                 $timestampdiv.siblings('a.edit-timestamp').show().focus();
677                         }
678                         event.preventDefault();
679                 });
680
681                 $('#post').on( 'submit', function( event ) {
682                         if ( ! updateText() ) {
683                                 event.preventDefault();
684                                 $timestampdiv.show();
685
686                                 if ( wp.autosave ) {
687                                         wp.autosave.enableButtons();
688                                 }
689
690                                 $( '#publishing-action .spinner' ).removeClass( 'is-active' );
691                         }
692                 });
693
694                 $postStatusSelect.siblings('a.edit-post-status').click( function( event ) {
695                         if ( $postStatusSelect.is( ':hidden' ) ) {
696                                 $postStatusSelect.slideDown( 'fast', function() {
697                                         $postStatusSelect.find('select').focus();
698                                 } );
699                                 $(this).hide();
700                         }
701                         event.preventDefault();
702                 });
703
704                 $postStatusSelect.find('.save-post-status').click( function( event ) {
705                         $postStatusSelect.slideUp( 'fast' ).siblings( 'a.edit-post-status' ).show().focus();
706                         updateText();
707                         event.preventDefault();
708                 });
709
710                 $postStatusSelect.find('.cancel-post-status').click( function( event ) {
711                         $postStatusSelect.slideUp( 'fast' ).siblings( 'a.edit-post-status' ).show().focus();
712                         $('#post_status').val( $('#hidden_post_status').val() );
713                         updateText();
714                         event.preventDefault();
715                 });
716         } // end submitdiv
717
718         // permalink
719         function editPermalink() {
720                 var i, slug_value,
721                         c = 0,
722                         e = $('#editable-post-name'),
723                         revert_e = e.html(),
724                         real_slug = $('#post_name'),
725                         revert_slug = real_slug.val(),
726                         b = $('#edit-slug-buttons'),
727                         revert_b = b.html(),
728                         full = $('#editable-post-name-full');
729
730                 // Deal with Twemoji in the post-name
731                 full.find( 'img' ).replaceWith( function() { return this.alt; } );
732                 full = full.html();
733
734                 $('#view-post-btn').hide();
735                 b.html('<a href="#" class="save button button-small">'+postL10n.ok+'</a> <a class="cancel" href="#">'+postL10n.cancel+'</a>');
736                 b.children('.save').click(function() {
737                         var new_slug = e.children('input').val();
738                         if ( new_slug == $('#editable-post-name-full').text() ) {
739                                 b.children('.cancel').click();
740                                 return false;
741                         }
742                         $.post(ajaxurl, {
743                                 action: 'sample-permalink',
744                                 post_id: postId,
745                                 new_slug: new_slug,
746                                 new_title: $('#title').val(),
747                                 samplepermalinknonce: $('#samplepermalinknonce').val()
748                         }, function(data) {
749                                 var box = $('#edit-slug-box');
750                                 box.html(data);
751                                 if (box.hasClass('hidden')) {
752                                         box.fadeIn('fast', function () {
753                                                 box.removeClass('hidden');
754                                         });
755                                 }
756
757                                 b.html(revert_b);
758                                 real_slug.val(new_slug);
759                                 $('#view-post-btn').show();
760                         });
761                         return false;
762                 });
763
764                 b.children('.cancel').click(function() {
765                         $('#view-post-btn').show();
766                         e.html(revert_e);
767                         b.html(revert_b);
768                         real_slug.val(revert_slug);
769                         return false;
770                 });
771
772                 for ( i = 0; i < full.length; ++i ) {
773                         if ( '%' == full.charAt(i) )
774                                 c++;
775                 }
776
777                 slug_value = ( c > full.length / 4 ) ? '' : full;
778                 e.html('<input type="text" id="new-post-slug" value="'+slug_value+'" />').children('input').keypress(function(e) {
779                         var key = e.keyCode || 0;
780                         // on enter, just save the new slug, don't save the post
781                         if ( 13 == key ) {
782                                 b.children('.save').click();
783                                 return false;
784                         }
785                         if ( 27 == key ) {
786                                 b.children('.cancel').click();
787                                 return false;
788                         }
789                 } ).keyup( function() {
790                         real_slug.val(this.value);
791                 }).focus();
792         }
793
794         if ( $editSlugWrap.length ) {
795                 $editSlugWrap.on( 'click', function( event ) {
796                         var $target = $( event.target );
797
798                         if ( $target.is('#editable-post-name') || $target.hasClass('edit-slug') ) {
799                                 editPermalink();
800                         }
801                 });
802         }
803
804         wptitlehint = function(id) {
805                 id = id || 'title';
806
807                 var title = $('#' + id), titleprompt = $('#' + id + '-prompt-text');
808
809                 if ( '' === title.val() )
810                         titleprompt.removeClass('screen-reader-text');
811
812                 titleprompt.click(function(){
813                         $(this).addClass('screen-reader-text');
814                         title.focus();
815                 });
816
817                 title.blur(function(){
818                         if ( '' === this.value )
819                                 titleprompt.removeClass('screen-reader-text');
820                 }).focus(function(){
821                         titleprompt.addClass('screen-reader-text');
822                 }).keydown(function(e){
823                         titleprompt.addClass('screen-reader-text');
824                         $(this).unbind(e);
825                 });
826         };
827
828         wptitlehint();
829
830         // Resize the visual and text editors
831         ( function() {
832                 var editor, offset, mce,
833                         $handle = $('#post-status-info'),
834                         $postdivrich = $('#postdivrich');
835
836                 // No point for touch devices
837                 if ( ! $textarea.length || 'ontouchstart' in window ) {
838                         // Hide the resize handle
839                         $('#content-resize-handle').hide();
840                         return;
841                 }
842
843                 function dragging( event ) {
844                         if ( $postdivrich.hasClass( 'wp-editor-expand' ) ) {
845                                 return;
846                         }
847
848                         if ( mce ) {
849                                 editor.theme.resizeTo( null, offset + event.pageY );
850                         } else {
851                                 $textarea.height( Math.max( 50, offset + event.pageY ) );
852                         }
853
854                         event.preventDefault();
855                 }
856
857                 function endDrag() {
858                         var height, toolbarHeight;
859
860                         if ( $postdivrich.hasClass( 'wp-editor-expand' ) ) {
861                                 return;
862                         }
863
864                         if ( mce ) {
865                                 editor.focus();
866                                 toolbarHeight = parseInt( $( '#wp-content-editor-container .mce-toolbar-grp' ).height(), 10 );
867
868                                 if ( toolbarHeight < 10 || toolbarHeight > 200 ) {
869                                         toolbarHeight = 30;
870                                 }
871
872                                 height = parseInt( $('#content_ifr').css('height'), 10 ) + toolbarHeight - 28;
873                         } else {
874                                 $textarea.focus();
875                                 height = parseInt( $textarea.css('height'), 10 );
876                         }
877
878                         $document.off( '.wp-editor-resize' );
879
880                         // sanity check
881                         if ( height && height > 50 && height < 5000 ) {
882                                 setUserSetting( 'ed_size', height );
883                         }
884                 }
885
886                 $handle.on( 'mousedown.wp-editor-resize', function( event ) {
887                         if ( typeof tinymce !== 'undefined' ) {
888                                 editor = tinymce.get('content');
889                         }
890
891                         if ( editor && ! editor.isHidden() ) {
892                                 mce = true;
893                                 offset = $('#content_ifr').height() - event.pageY;
894                         } else {
895                                 mce = false;
896                                 offset = $textarea.height() - event.pageY;
897                                 $textarea.blur();
898                         }
899
900                         $document.on( 'mousemove.wp-editor-resize', dragging )
901                                 .on( 'mouseup.wp-editor-resize mouseleave.wp-editor-resize', endDrag );
902
903                         event.preventDefault();
904                 }).on( 'mouseup.wp-editor-resize', endDrag );
905         })();
906
907         if ( typeof tinymce !== 'undefined' ) {
908                 // When changing post formats, change the editor body class
909                 $( '#post-formats-select input.post-format' ).on( 'change.set-editor-class', function() {
910                         var editor, body, format = this.id;
911
912                         if ( format && $( this ).prop( 'checked' ) && ( editor = tinymce.get( 'content' ) ) ) {
913                                 body = editor.getBody();
914                                 body.className = body.className.replace( /\bpost-format-[^ ]+/, '' );
915                                 editor.dom.addClass( body, format == 'post-format-0' ? 'post-format-standard' : format );
916                                 $( document ).trigger( 'editor-classchange' );
917                         }
918                 });
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         });
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() );