]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/js/post.js
WordPress 4.4.1-scripts
[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         load: function(total){
65                 this.st = jQuery('#the-comment-list tr.comment:visible').length;
66                 this.get(total);
67         }
68 };
69
70 WPSetThumbnailHTML = function(html){
71         $('.inside', '#postimagediv').html(html);
72 };
73
74 WPSetThumbnailID = function(id){
75         var field = $('input[value="_thumbnail_id"]', '#list-table');
76         if ( field.size() > 0 ) {
77                 $('#meta\\[' + field.attr('id').match(/[0-9]+/) + '\\]\\[value\\]').text(id);
78         }
79 };
80
81 WPRemoveThumbnail = function(nonce){
82         $.post(ajaxurl, {
83                 action: 'set-post-thumbnail', post_id: $( '#post_ID' ).val(), thumbnail_id: -1, _ajax_nonce: nonce, cookie: encodeURIComponent( document.cookie )
84         }, function(str){
85                 if ( str == '0' ) {
86                         alert( setPostThumbnailL10n.error );
87                 } else {
88                         WPSetThumbnailHTML(str);
89                 }
90         }
91         );
92 };
93
94 $(document).on( 'heartbeat-send.refresh-lock', function( e, data ) {
95         var lock = $('#active_post_lock').val(),
96                 post_id = $('#post_ID').val(),
97                 send = {};
98
99         if ( ! post_id || ! $('#post-lock-dialog').length )
100                 return;
101
102         send.post_id = post_id;
103
104         if ( lock )
105                 send.lock = lock;
106
107         data['wp-refresh-post-lock'] = send;
108
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;
112
113         if ( data['wp-refresh-post-lock'] ) {
114                 received = data['wp-refresh-post-lock'];
115
116                 if ( received.lock_error ) {
117                         // show "editing taken over" message
118                         wrap = $('#post-lock-dialog');
119
120                         if ( wrap.length && ! wrap.is(':visible') ) {
121                                 if ( wp.autosave ) {
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' );
127                                         });
128
129                                         wrap.addClass('saving');
130                                         wp.autosave.server.triggerSave();
131                                 }
132
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( /&amp;/g, '&' ) );
135                                         wrap.find('div.post-locked-avatar').empty().append( avatar );
136                                 }
137
138                                 wrap.show().find('.currently-editing').text( received.lock_error.text );
139                                 wrap.find('.wp-tab-first').focus();
140                         }
141                 } else if ( received.new_lock ) {
142                         $('#active_post_lock').val( received.new_lock );
143                 }
144         }
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 ) {
151                 $.post( ajaxurl, {
152                                 action: 'sample-permalink',
153                                 post_id: $('#post_ID').val(),
154                                 new_title: $('#title').val(),
155                                 samplepermalinknonce: $('#samplepermalinknonce').val()
156                         },
157                         function( data ) {
158                                 if ( data != '-1' ) {
159                                         $('#edit-slug-box').html(data);
160                                 }
161                         }
162                 );
163         }
164 });
165
166 }(jQuery));
167
168 (function($) {
169         var check, timeout;
170
171         function schedule() {
172                 check = false;
173                 window.clearTimeout( timeout );
174                 timeout = window.setTimeout( function(){ check = true; }, 300000 );
175         }
176
177         $(document).on( 'heartbeat-send.wp-refresh-nonces', function( e, data ) {
178                 var post_id,
179                         $authCheck = $('#wp-auth-check-wrap');
180
181                 if ( check || ( $authCheck.length && ! $authCheck.hasClass( 'hidden' ) ) ) {
182                         if ( ( post_id = $('#post_ID').val() ) && $('#_wpnonce').val() ) {
183                                 data['wp-refresh-post-nonces'] = {
184                                         post_id: post_id
185                                 };
186                         }
187                 }
188         }).on( 'heartbeat-tick.wp-refresh-nonces', function( e, data ) {
189                 var nonces = data['wp-refresh-post-nonces'];
190
191                 if ( nonces ) {
192                         schedule();
193
194                         if ( nonces.replace ) {
195                                 $.each( nonces.replace, function( selector, value ) {
196                                         $( '#' + selector ).val( value );
197                                 });
198                         }
199
200                         if ( nonces.heartbeatNonce )
201                                 window.heartbeatSettings.nonce = nonces.heartbeatNonce;
202                 }
203         }).ready( function() {
204                 schedule();
205         });
206 }(jQuery));
207
208 jQuery(document).ready( function($) {
209         var stamp, visibility, $submitButtons, updateVisibility, updateText,
210                 sticky = '',
211                 $textarea = $('#content'),
212                 $document = $(document),
213                 postId = $('#post_ID').val() || 0,
214                 $submitpost = $('#submitpost'),
215                 releaseLock = true,
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;
220
221         postboxes.add_postbox_toggles(pagenow);
222
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.
225         window.name = '';
226
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) {
229                 if ( e.which != 9 )
230                         return;
231
232                 var target = $(e.target);
233
234                 if ( target.hasClass('wp-tab-first') && e.shiftKey ) {
235                         $(this).find('.wp-tab-last').focus();
236                         e.preventDefault();
237                 } else if ( target.hasClass('wp-tab-last') && ! e.shiftKey ) {
238                         $(this).find('.wp-tab-first').focus();
239                         e.preventDefault();
240                 }
241         }).filter(':visible').find('.wp-tab-first').focus();
242
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 );
246         }
247
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);
251
252                 if ( $button.hasClass('disabled') ) {
253                         event.preventDefault();
254                         return;
255                 }
256
257                 if ( $button.hasClass('submitdelete') || $button.is( '#post-preview' ) ) {
258                         return;
259                 }
260
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() ) {
265                                 return;
266                         }
267
268                         // Stop autosave
269                         if ( wp.autosave ) {
270                                 wp.autosave.server.suspend();
271                         }
272
273                         if ( typeof commentReply !== 'undefined' ) {
274                                 /*
275                                  * Close the comment edit/reply form if open to stop the form
276                                  * action from interfering with the post's form action.
277                                  */
278                                 commentReply.close();
279                         }
280
281                         releaseLock = false;
282                         $(window).off( 'beforeunload.edit-post' );
283
284                         $submitButtons.addClass( 'disabled' );
285
286                         if ( $button.attr('id') === 'publish' ) {
287                                 $submitpost.find( '#major-publishing-actions .spinner' ).addClass( 'is-active' );
288                         } else {
289                                 $submitpost.find( '#minor-publishing .spinner' ).addClass( 'is-active' );
290                         }
291                 });
292         });
293
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 ) {
296                 var $this = $(this),
297                         $form = $('form#post'),
298                         $previewField = $('input#wp-preview'),
299                         target = $this.attr('target') || 'wp-preview',
300                         ua = navigator.userAgent.toLowerCase();
301
302                 event.preventDefault();
303
304                 if ( $this.hasClass('disabled') ) {
305                         return;
306                 }
307
308                 if ( wp.autosave ) {
309                         wp.autosave.server.tempBlockSave();
310                 }
311
312                 $previewField.val('dopreview');
313                 $form.attr( 'target', target ).submit().attr( 'target', '' );
314
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();
320                         });
321                 }
322
323                 $previewField.val('');
324         });
325
326         // This code is meant to allow tabbing from Title to Post content.
327         $('#title').on( 'keydown.editor-focus', function( event ) {
328                 var editor;
329
330                 if ( event.keyCode === 9 && ! event.ctrlKey && ! event.altKey && ! event.shiftKey ) {
331                         editor = typeof tinymce != 'undefined' && tinymce.get('content');
332
333                         if ( editor && ! editor.isHidden() ) {
334                                 editor.focus();
335                         } else if ( $textarea.length ) {
336                                 $textarea.focus();
337                         } else {
338                                 return;
339                         }
340
341                         event.preventDefault();
342                 }
343         });
344
345         // Autosave new posts after a title is typed
346         if ( $( '#auto_draft' ).val() ) {
347                 $( '#title' ).blur( function() {
348                         var cancel;
349
350                         if ( ! this.value || $('#edit-slug-box > *').length ) {
351                                 return;
352                         }
353
354                         // Cancel the autosave when the blur was triggered by the user submitting the form
355                         $('form#post').one( 'submit', function() {
356                                 cancel = true;
357                         });
358
359                         window.setTimeout( function() {
360                                 if ( ! cancel && wp.autosave ) {
361                                         wp.autosave.server.triggerSave();
362                                 }
363                         }, 200 );
364                 });
365         }
366
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' );
372                 }
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 );
377         });
378
379         $(window).on( 'beforeunload.edit-post', function() {
380                 var editor = typeof tinymce !== 'undefined' && tinymce.get('content');
381
382                 if ( ( editor && ! editor.isHidden() && editor.isDirty() ) ||
383                         ( wp.autosave && wp.autosave.server.postChanged() ) ) {
384
385                         return postL10n.saveAlert;
386                 }
387         }).on( 'unload.edit-post', function( event ) {
388                 if ( ! releaseLock ) {
389                         return;
390                 }
391
392                 // Unload is triggered (by hand) on removing the Thickbox iframe.
393                 // Make sure we process only the main document unload.
394                 if ( event.target && event.target.nodeName != '#document' ) {
395                         return;
396                 }
397
398                 $.ajax({
399                         type: 'POST',
400                         url: ajaxurl,
401                         async: false,
402                         data: {
403                                 action: 'wp-remove-post-lock',
404                                 _wpnonce: $('#_wpnonce').val(),
405                                 post_ID: $('#post_ID').val(),
406                                 active_post_lock: $('#active_post_lock').val()
407                         }
408                 });
409         });
410
411         // multi-taxonomies
412         if ( $('#tagsdiv-post_tag').length ) {
413                 window.tagBox && window.tagBox.init();
414         } else {
415                 $('#side-sortables, #normal-sortables, #advanced-sortables').children('div.postbox').each(function(){
416                         if ( this.id.indexOf('tagsdiv-') === 0 ) {
417                                 window.tagBox && window.tagBox.init();
418                                 return false;
419                         }
420                 });
421         }
422
423         // categories
424         $('.categorydiv').each( function(){
425                 var this_id = $(this).attr('id'), catAddBefore, catAddAfter, taxonomyParts, taxonomy, settingName;
426
427                 taxonomyParts = this_id.split('-');
428                 taxonomyParts.shift();
429                 taxonomy = taxonomyParts.join('-');
430                 settingName = taxonomy + '_tab';
431                 if ( taxonomy == 'category' )
432                         settingName = 'cats';
433
434                 // TODO: move to jQuery 1.3+, support for multiple hierarchical taxonomies, see wp-lists.js
435                 $('a', '#' + taxonomy + '-tabs').click( function( e ) {
436                         e.preventDefault();
437                         var t = $(this).attr('href');
438                         $(this).parent().addClass('tabs').siblings('li').removeClass('tabs');
439                         $('#' + taxonomy + '-tabs').siblings('.tabs-panel').hide();
440                         $(t).show();
441                         if ( '#' + taxonomy + '-all' == t )
442                                 deleteUserSetting( settingName );
443                         else
444                                 setUserSetting( settingName, 'pop' );
445                 });
446
447                 if ( getUserSetting( settingName ) )
448                         $('a[href="#' + taxonomy + '-pop"]', '#' + taxonomy + '-tabs').click();
449
450                 // Ajax Cat
451                 $( '#new' + taxonomy ).one( 'focus', function() { $( this ).val( '' ).removeClass( 'form-input-tip' ); } );
452
453                 $('#new' + taxonomy).keypress( function(event){
454                         if( 13 === event.keyCode ) {
455                                 event.preventDefault();
456                                 $('#' + taxonomy + '-add-submit').click();
457                         }
458                 });
459                 $('#' + taxonomy + '-add-submit').click( function(){ $('#new' + taxonomy).focus(); });
460
461                 catAddBefore = function( s ) {
462                         if ( !$('#new'+taxonomy).val() )
463                                 return false;
464                         s.data += '&' + $( ':checked', '#'+taxonomy+'checklist' ).serialize();
465                         $( '#' + taxonomy + '-add-submit' ).prop( 'disabled', true );
466                         return s;
467                 };
468
469                 catAddAfter = function( r, s ) {
470                         var sup, drop = $('#new'+taxonomy+'_parent');
471
472                         $( '#' + taxonomy + '-add-submit' ).prop( 'disabled', false );
473                         if ( 'undefined' != s.parsed.responses[0] && (sup = s.parsed.responses[0].supplemental.newcat_parent) ) {
474                                 drop.before(sup);
475                                 drop.remove();
476                         }
477                 };
478
479                 $('#' + taxonomy + 'checklist').wpList({
480                         alt: '',
481                         response: taxonomy + '-ajax-response',
482                         addBefore: catAddBefore,
483                         addAfter: catAddAfter
484                 });
485
486                 $('#' + taxonomy + '-add-toggle').click( function( e ) {
487                         e.preventDefault();
488                         $('#' + taxonomy + '-adder').toggleClass( 'wp-hidden-children' );
489                         $('a[href="#' + taxonomy + '-all"]', '#' + taxonomy + '-tabs').click();
490                         $('#new'+taxonomy).focus();
491                 });
492
493                 $('#' + taxonomy + 'checklist, #' + taxonomy + 'checklist-pop').on( 'click', 'li.popular-category > label input[type="checkbox"]', function() {
494                         var t = $(this), c = t.is(':checked'), id = t.val();
495                         if ( id && t.parents('#taxonomy-'+taxonomy).length )
496                                 $('#in-' + taxonomy + '-' + id + ', #in-popular-' + taxonomy + '-' + id).prop( 'checked', c );
497                 });
498
499         }); // end cats
500
501         // Custom Fields
502         if ( $('#postcustom').length ) {
503                 $( '#the-list' ).wpList( { addAfter: function() {
504                         $('table#list-table').show();
505                 }, addBefore: function( s ) {
506                         s.data += '&post_id=' + $('#post_ID').val();
507                         return s;
508                 }
509                 });
510         }
511
512         // submitdiv
513         if ( $('#submitdiv').length ) {
514                 stamp = $('#timestamp').html();
515                 visibility = $('#post-visibility-display').html();
516
517                 updateVisibility = function() {
518                         if ( $postVisibilitySelect.find('input:radio:checked').val() != 'public' ) {
519                                 $('#sticky').prop('checked', false);
520                                 $('#sticky-span').hide();
521                         } else {
522                                 $('#sticky-span').show();
523                         }
524                         if ( $postVisibilitySelect.find('input:radio:checked').val() != 'password' ) {
525                                 $('#password-span').hide();
526                         } else {
527                                 $('#password-span').show();
528                         }
529                 };
530
531                 updateText = function() {
532
533                         if ( ! $timestampdiv.length )
534                                 return true;
535
536                         var attemptedDate, originalDate, currentDate, publishOn, postStatus = $('#post_status'),
537                                 optPublish = $('option[value="publish"]', postStatus), aa = $('#aa').val(),
538                                 mm = $('#mm').val(), jj = $('#jj').val(), hh = $('#hh').val(), mn = $('#mn').val();
539
540                         attemptedDate = new Date( aa, mm - 1, jj, hh, mn );
541                         originalDate = new Date( $('#hidden_aa').val(), $('#hidden_mm').val() -1, $('#hidden_jj').val(), $('#hidden_hh').val(), $('#hidden_mn').val() );
542                         currentDate = new Date( $('#cur_aa').val(), $('#cur_mm').val() -1, $('#cur_jj').val(), $('#cur_hh').val(), $('#cur_mn').val() );
543
544                         if ( attemptedDate.getFullYear() != aa || (1 + attemptedDate.getMonth()) != mm || attemptedDate.getDate() != jj || attemptedDate.getMinutes() != mn ) {
545                                 $timestampdiv.find('.timestamp-wrap').addClass('form-invalid');
546                                 return false;
547                         } else {
548                                 $timestampdiv.find('.timestamp-wrap').removeClass('form-invalid');
549                         }
550
551                         if ( attemptedDate > currentDate && $('#original_post_status').val() != 'future' ) {
552                                 publishOn = postL10n.publishOnFuture;
553                                 $('#publish').val( postL10n.schedule );
554                         } else if ( attemptedDate <= currentDate && $('#original_post_status').val() != 'publish' ) {
555                                 publishOn = postL10n.publishOn;
556                                 $('#publish').val( postL10n.publish );
557                         } else {
558                                 publishOn = postL10n.publishOnPast;
559                                 $('#publish').val( postL10n.update );
560                         }
561                         if ( originalDate.toUTCString() == attemptedDate.toUTCString() ) { //hack
562                                 $('#timestamp').html(stamp);
563                         } else {
564                                 $('#timestamp').html(
565                                         '\n' + publishOn + ' <b>' +
566                                         postL10n.dateFormat
567                                                 .replace( '%1$s', $( 'option[value="' + mm + '"]', '#mm' ).attr( 'data-text' ) )
568                                                 .replace( '%2$s', parseInt( jj, 10 ) )
569                                                 .replace( '%3$s', aa )
570                                                 .replace( '%4$s', ( '00' + hh ).slice( -2 ) )
571                                                 .replace( '%5$s', ( '00' + mn ).slice( -2 ) ) +
572                                                 '</b> '
573                                 );
574                         }
575
576                         if ( $postVisibilitySelect.find('input:radio:checked').val() == 'private' ) {
577                                 $('#publish').val( postL10n.update );
578                                 if ( 0 === optPublish.length ) {
579                                         postStatus.append('<option value="publish">' + postL10n.privatelyPublished + '</option>');
580                                 } else {
581                                         optPublish.html( postL10n.privatelyPublished );
582                                 }
583                                 $('option[value="publish"]', postStatus).prop('selected', true);
584                                 $('#misc-publishing-actions .edit-post-status').hide();
585                         } else {
586                                 if ( $('#original_post_status').val() == 'future' || $('#original_post_status').val() == 'draft' ) {
587                                         if ( optPublish.length ) {
588                                                 optPublish.remove();
589                                                 postStatus.val($('#hidden_post_status').val());
590                                         }
591                                 } else {
592                                         optPublish.html( postL10n.published );
593                                 }
594                                 if ( postStatus.is(':hidden') )
595                                         $('#misc-publishing-actions .edit-post-status').show();
596                         }
597                         $('#post-status-display').html($('option:selected', postStatus).text());
598                         if ( $('option:selected', postStatus).val() == 'private' || $('option:selected', postStatus).val() == 'publish' ) {
599                                 $('#save-post').hide();
600                         } else {
601                                 $('#save-post').show();
602                                 if ( $('option:selected', postStatus).val() == 'pending' ) {
603                                         $('#save-post').show().val( postL10n.savePending );
604                                 } else {
605                                         $('#save-post').show().val( postL10n.saveDraft );
606                                 }
607                         }
608                         return true;
609                 };
610
611                 $( '#visibility .edit-visibility').click( function( e ) {
612                         e.preventDefault();
613                         if ( $postVisibilitySelect.is(':hidden') ) {
614                                 updateVisibility();
615                                 $postVisibilitySelect.slideDown( 'fast', function() {
616                                         $postVisibilitySelect.find( 'input[type="radio"]' ).first().focus();
617                                 } );
618                                 $(this).hide();
619                         }
620                 });
621
622                 $postVisibilitySelect.find('.cancel-post-visibility').click( function( event ) {
623                         $postVisibilitySelect.slideUp('fast');
624                         $('#visibility-radio-' + $('#hidden-post-visibility').val()).prop('checked', true);
625                         $('#post_password').val($('#hidden-post-password').val());
626                         $('#sticky').prop('checked', $('#hidden-post-sticky').prop('checked'));
627                         $('#post-visibility-display').html(visibility);
628                         $('#visibility .edit-visibility').show().focus();
629                         updateText();
630                         event.preventDefault();
631                 });
632
633                 $postVisibilitySelect.find('.save-post-visibility').click( function( event ) { // crazyhorse - multiple ok cancels
634                         $postVisibilitySelect.slideUp('fast');
635                         $('#visibility .edit-visibility').show().focus();
636                         updateText();
637
638                         if ( $postVisibilitySelect.find('input:radio:checked').val() != 'public' ) {
639                                 $('#sticky').prop('checked', false);
640                         } // WEAPON LOCKED
641
642                         if ( $('#sticky').prop('checked') ) {
643                                 sticky = 'Sticky';
644                         } else {
645                                 sticky = '';
646                         }
647
648                         $('#post-visibility-display').html(     postL10n[ $postVisibilitySelect.find('input:radio:checked').val() + sticky ]    );
649                         event.preventDefault();
650                 });
651
652                 $postVisibilitySelect.find('input:radio').change( function() {
653                         updateVisibility();
654                 });
655
656                 $timestampdiv.siblings('a.edit-timestamp').click( function( event ) {
657                         if ( $timestampdiv.is( ':hidden' ) ) {
658                                 $timestampdiv.slideDown( 'fast', function() {
659                                         $( 'input, select', $timestampdiv.find( '.timestamp-wrap' ) ).first().focus();
660                                 } );
661                                 $(this).hide();
662                         }
663                         event.preventDefault();
664                 });
665
666                 $timestampdiv.find('.cancel-timestamp').click( function( event ) {
667                         $timestampdiv.slideUp('fast').siblings('a.edit-timestamp').show().focus();
668                         $('#mm').val($('#hidden_mm').val());
669                         $('#jj').val($('#hidden_jj').val());
670                         $('#aa').val($('#hidden_aa').val());
671                         $('#hh').val($('#hidden_hh').val());
672                         $('#mn').val($('#hidden_mn').val());
673                         updateText();
674                         event.preventDefault();
675                 });
676
677                 $timestampdiv.find('.save-timestamp').click( function( event ) { // crazyhorse - multiple ok cancels
678                         if ( updateText() ) {
679                                 $timestampdiv.slideUp('fast');
680                                 $timestampdiv.siblings('a.edit-timestamp').show().focus();
681                         }
682                         event.preventDefault();
683                 });
684
685                 $('#post').on( 'submit', function( event ) {
686                         if ( ! updateText() ) {
687                                 event.preventDefault();
688                                 $timestampdiv.show();
689
690                                 if ( wp.autosave ) {
691                                         wp.autosave.enableButtons();
692                                 }
693
694                                 $( '#publishing-action .spinner' ).removeClass( 'is-active' );
695                         }
696                 });
697
698                 $postStatusSelect.siblings('a.edit-post-status').click( function( event ) {
699                         if ( $postStatusSelect.is( ':hidden' ) ) {
700                                 $postStatusSelect.slideDown( 'fast', function() {
701                                         $postStatusSelect.find('select').focus();
702                                 } );
703                                 $(this).hide();
704                         }
705                         event.preventDefault();
706                 });
707
708                 $postStatusSelect.find('.save-post-status').click( function( event ) {
709                         $postStatusSelect.slideUp( 'fast' ).siblings( 'a.edit-post-status' ).show().focus();
710                         updateText();
711                         event.preventDefault();
712                 });
713
714                 $postStatusSelect.find('.cancel-post-status').click( function( event ) {
715                         $postStatusSelect.slideUp( 'fast' ).siblings( 'a.edit-post-status' ).show().focus();
716                         $('#post_status').val( $('#hidden_post_status').val() );
717                         updateText();
718                         event.preventDefault();
719                 });
720         } // end submitdiv
721
722         // permalink
723         function editPermalink() {
724                 var i, slug_value,
725                         $el, revert_e,
726                         c = 0,
727                         real_slug = $('#post_name'),
728                         revert_slug = real_slug.val(),
729                         permalink = $( '#sample-permalink' ),
730                         permalinkOrig = permalink.html(),
731                         permalinkInner = $( '#sample-permalink a' ).html(),
732                         buttons = $('#edit-slug-buttons'),
733                         buttonsOrig = buttons.html(),
734                         full = $('#editable-post-name-full');
735
736                 // Deal with Twemoji in the post-name
737                 full.find( 'img' ).replaceWith( function() { return this.alt; } );
738                 full = full.html();
739
740                 permalink.html( permalinkInner );
741                 $el = $( '#editable-post-name' );
742                 revert_e = $el.html();
743
744                 buttons.html( '<button type="button" class="save button button-small">' + postL10n.ok + '</button> <button type="button" class="cancel button-link">' + postL10n.cancel + '</button>' );
745                 buttons.children( '.save' ).click( function() {
746                         var new_slug = $el.children( 'input' ).val();
747
748                         if ( new_slug == $('#editable-post-name-full').text() ) {
749                                 buttons.children('.cancel').click();
750                                 return;
751                         }
752                         $.post(ajaxurl, {
753                                 action: 'sample-permalink',
754                                 post_id: postId,
755                                 new_slug: new_slug,
756                                 new_title: $('#title').val(),
757                                 samplepermalinknonce: $('#samplepermalinknonce').val()
758                         }, function(data) {
759                                 var box = $('#edit-slug-box');
760                                 box.html(data);
761                                 if (box.hasClass('hidden')) {
762                                         box.fadeIn('fast', function () {
763                                                 box.removeClass('hidden');
764                                         });
765                                 }
766
767                                 buttons.html(buttonsOrig);
768                                 permalink.html(permalinkOrig);
769                                 real_slug.val(new_slug);
770                                 $( '.edit-slug' ).focus();
771                                 wp.a11y.speak( postL10n.permalinkSaved );
772                         });
773                 });
774
775                 buttons.children( '.cancel' ).click( function() {
776                         $('#view-post-btn').show();
777                         $el.html(revert_e);
778                         buttons.html(buttonsOrig);
779                         permalink.html(permalinkOrig);
780                         real_slug.val(revert_slug);
781                         $( '.edit-slug' ).focus();
782                 });
783
784                 for ( i = 0; i < full.length; ++i ) {
785                         if ( '%' == full.charAt(i) )
786                                 c++;
787                 }
788
789                 slug_value = ( c > full.length / 4 ) ? '' : full;
790                 $el.html( '<input type="text" id="new-post-slug" value="' + slug_value + '" autocomplete="off" />' ).children( 'input' ).keydown( function( e ) {
791                         var key = e.which;
792                         // On enter, just save the new slug, don't save the post.
793                         if ( 13 === key ) {
794                                 e.preventDefault();
795                                 buttons.children( '.save' ).click();
796                         }
797                         if ( 27 === key ) {
798                                 buttons.children( '.cancel' ).click();
799                         }
800                 } ).keyup( function() {
801                         real_slug.val( this.value );
802                 }).focus();
803         }
804
805         $( '#titlediv' ).on( 'click', '.edit-slug', function() {
806                 editPermalink();
807         });
808
809         wptitlehint = function(id) {
810                 id = id || 'title';
811
812                 var title = $('#' + id), titleprompt = $('#' + id + '-prompt-text');
813
814                 if ( '' === title.val() )
815                         titleprompt.removeClass('screen-reader-text');
816
817                 titleprompt.click(function(){
818                         $(this).addClass('screen-reader-text');
819                         title.focus();
820                 });
821
822                 title.blur(function(){
823                         if ( '' === this.value )
824                                 titleprompt.removeClass('screen-reader-text');
825                 }).focus(function(){
826                         titleprompt.addClass('screen-reader-text');
827                 }).keydown(function(e){
828                         titleprompt.addClass('screen-reader-text');
829                         $(this).unbind(e);
830                 });
831         };
832
833         wptitlehint();
834
835         // Resize the visual and text editors
836         ( function() {
837                 var editor, offset, mce,
838                         $handle = $('#post-status-info'),
839                         $postdivrich = $('#postdivrich');
840
841                 // No point for touch devices
842                 if ( ! $textarea.length || 'ontouchstart' in window ) {
843                         // Hide the resize handle
844                         $('#content-resize-handle').hide();
845                         return;
846                 }
847
848                 function dragging( event ) {
849                         if ( $postdivrich.hasClass( 'wp-editor-expand' ) ) {
850                                 return;
851                         }
852
853                         if ( mce ) {
854                                 editor.theme.resizeTo( null, offset + event.pageY );
855                         } else {
856                                 $textarea.height( Math.max( 50, offset + event.pageY ) );
857                         }
858
859                         event.preventDefault();
860                 }
861
862                 function endDrag() {
863                         var height, toolbarHeight;
864
865                         if ( $postdivrich.hasClass( 'wp-editor-expand' ) ) {
866                                 return;
867                         }
868
869                         if ( mce ) {
870                                 editor.focus();
871                                 toolbarHeight = parseInt( $( '#wp-content-editor-container .mce-toolbar-grp' ).height(), 10 );
872
873                                 if ( toolbarHeight < 10 || toolbarHeight > 200 ) {
874                                         toolbarHeight = 30;
875                                 }
876
877                                 height = parseInt( $('#content_ifr').css('height'), 10 ) + toolbarHeight - 28;
878                         } else {
879                                 $textarea.focus();
880                                 height = parseInt( $textarea.css('height'), 10 );
881                         }
882
883                         $document.off( '.wp-editor-resize' );
884
885                         // sanity check
886                         if ( height && height > 50 && height < 5000 ) {
887                                 setUserSetting( 'ed_size', height );
888                         }
889                 }
890
891                 $handle.on( 'mousedown.wp-editor-resize', function( event ) {
892                         if ( typeof tinymce !== 'undefined' ) {
893                                 editor = tinymce.get('content');
894                         }
895
896                         if ( editor && ! editor.isHidden() ) {
897                                 mce = true;
898                                 offset = $('#content_ifr').height() - event.pageY;
899                         } else {
900                                 mce = false;
901                                 offset = $textarea.height() - event.pageY;
902                                 $textarea.blur();
903                         }
904
905                         $document.on( 'mousemove.wp-editor-resize', dragging )
906                                 .on( 'mouseup.wp-editor-resize mouseleave.wp-editor-resize', endDrag );
907
908                         event.preventDefault();
909                 }).on( 'mouseup.wp-editor-resize', endDrag );
910         })();
911
912         if ( typeof tinymce !== 'undefined' ) {
913                 // When changing post formats, change the editor body class
914                 $( '#post-formats-select input.post-format' ).on( 'change.set-editor-class', function() {
915                         var editor, body, format = this.id;
916
917                         if ( format && $( this ).prop( 'checked' ) && ( editor = tinymce.get( 'content' ) ) ) {
918                                 body = editor.getBody();
919                                 body.className = body.className.replace( /\bpost-format-[^ ]+/, '' );
920                                 editor.dom.addClass( body, format == 'post-format-0' ? 'post-format-standard' : format );
921                                 $( document ).trigger( 'editor-classchange' );
922                         }
923                 });
924         }
925
926         // Save on pressing Ctrl/Command + S in the Text editor
927         $textarea.on( 'keydown.wp-autosave', function( event ) {
928                 if ( event.which === 83 ) {
929                         if ( event.shiftKey || event.altKey || ( isMac && ( ! event.metaKey || event.ctrlKey ) ) || ( ! isMac && ! event.ctrlKey ) ) {
930                                 return;
931                         }
932
933                         wp.autosave && wp.autosave.server.triggerSave();
934                         event.preventDefault();
935                 }
936         });
937
938         if ( $( '#original_post_status' ).val() === 'auto-draft' && window.history.replaceState ) {
939                 var location;
940
941                 $( '#publish' ).on( 'click', function() {
942                         location = window.location.href;
943                         location += ( location.indexOf( '?' ) !== -1 ) ? '&' : '?';
944                         location += 'wp-post-new-reload=true';
945
946                         window.history.replaceState( null, null, location );
947                 });
948         }
949 });
950
951 ( function( $, counter ) {
952         $( function() {
953                 var $content = $( '#content' ),
954                         $count = $( '#wp-word-count' ).find( '.word-count' ),
955                         prevCount = 0,
956                         contentEditor;
957
958                 function update() {
959                         var text, count;
960
961                         if ( ! contentEditor || contentEditor.isHidden() ) {
962                                 text = $content.val();
963                         } else {
964                                 text = contentEditor.getContent( { format: 'raw' } );
965                         }
966
967                         count = counter.count( text );
968
969                         if ( count !== prevCount ) {
970                                 $count.text( count );
971                         }
972
973                         prevCount = count;
974                 }
975
976                 $( document ).on( 'tinymce-editor-init', function( event, editor ) {
977                         if ( editor.id !== 'content' ) {
978                                 return;
979                         }
980
981                         contentEditor = editor;
982
983                         editor.on( 'nodechange keyup', _.debounce( update, 1000 ) );
984                 } );
985
986                 $content.on( 'input keyup', _.debounce( update, 1000 ) );
987
988                 update();
989         } );
990 } )( jQuery, new wp.utils.WordCounter() );