]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/js/post.js
WordPress 4.2.2-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 */
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 nonce, post_id;
174
175                 if ( check ) {
176                         if ( ( post_id = $('#post_ID').val() ) && ( nonce = $('#_wpnonce').val() ) ) {
177                                 data['wp-refresh-post-nonces'] = {
178                                         post_id: post_id,
179                                         post_nonce: nonce
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                 last = 0,
207                 co = $('#content'),
208                 $document = $(document),
209                 $editSlugWrap = $('#edit-slug-box'),
210                 postId = $('#post_ID').val() || 0,
211                 $submitpost = $('#submitpost'),
212                 releaseLock = true,
213                 $postVisibilitySelect = $('#post-visibility-select'),
214                 $timestampdiv = $('#timestampdiv'),
215                 $postStatusSelect = $('#post-status-select');
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                         releaseLock = false;
270                         $(window).off( 'beforeunload.edit-post' );
271
272                         $submitButtons.addClass( 'disabled' );
273
274                         if ( $button.attr('id') === 'publish' ) {
275                                 $submitpost.find( '#major-publishing-actions .spinner' ).addClass( 'is-active' );
276                         } else {
277                                 $submitpost.find( '#minor-publishing .spinner' ).addClass( 'is-active' );
278                         }
279                 });
280         });
281
282         // Submit the form saving a draft or an autosave, and show a preview in a new tab
283         $('#post-preview').on( 'click.post-preview', function( event ) {
284                 var $this = $(this),
285                         $form = $('form#post'),
286                         $previewField = $('input#wp-preview'),
287                         target = $this.attr('target') || 'wp-preview',
288                         ua = navigator.userAgent.toLowerCase();
289
290                 event.preventDefault();
291
292                 if ( $this.hasClass('disabled') ) {
293                         return;
294                 }
295
296                 if ( wp.autosave ) {
297                         wp.autosave.server.tempBlockSave();
298                 }
299
300                 $previewField.val('dopreview');
301                 $form.attr( 'target', target ).submit().attr( 'target', '' );
302
303                 // Workaround for WebKit bug preventing a form submitting twice to the same action.
304                 // https://bugs.webkit.org/show_bug.cgi?id=28633
305                 if ( ua.indexOf('safari') !== -1 && ua.indexOf('chrome') === -1 ) {
306                         $form.attr( 'action', function( index, value ) {
307                                 return value + '?t=' + ( new Date() ).getTime();
308                         });
309                 }
310
311                 $previewField.val('');
312         });
313
314         // This code is meant to allow tabbing from Title to Post content.
315         $('#title').on( 'keydown.editor-focus', function( event ) {
316                 var editor, $textarea;
317
318                 if ( event.keyCode === 9 && ! event.ctrlKey && ! event.altKey && ! event.shiftKey ) {
319                         editor = typeof tinymce != 'undefined' && tinymce.get('content');
320                         $textarea = $('#content');
321
322                         if ( editor && ! editor.isHidden() ) {
323                                 editor.focus();
324                         } else if ( $textarea.length ) {
325                                 $textarea.focus();
326                         } else {
327                                 return;
328                         }
329
330                         event.preventDefault();
331                 }
332         });
333
334         // Autosave new posts after a title is typed
335         if ( $( '#auto_draft' ).val() ) {
336                 $( '#title' ).blur( function() {
337                         var cancel;
338
339                         if ( ! this.value || $('#edit-slug-box > *').length ) {
340                                 return;
341                         }
342
343                         // Cancel the autosave when the blur was triggered by the user submitting the form
344                         $('form#post').one( 'submit', function() {
345                                 cancel = true;
346                         });
347
348                         window.setTimeout( function() {
349                                 if ( ! cancel && wp.autosave ) {
350                                         wp.autosave.server.triggerSave();
351                                 }
352                         }, 200 );
353                 });
354         }
355
356         $document.on( 'autosave-disable-buttons.edit-post', function() {
357                 $submitButtons.addClass( 'disabled' );
358         }).on( 'autosave-enable-buttons.edit-post', function() {
359                 if ( ! wp.heartbeat || ! wp.heartbeat.hasConnectionError() ) {
360                         $submitButtons.removeClass( 'disabled' );
361                 }
362         }).on( 'before-autosave.edit-post', function() {
363                 $( '.autosave-message' ).text( postL10n.savingText );
364         }).on( 'after-autosave.edit-post', function( event, data ) {
365                 $( '.autosave-message' ).text( data.message );
366         });
367
368         $(window).on( 'beforeunload.edit-post', function() {
369                 var editor = typeof tinymce !== 'undefined' && tinymce.get('content');
370
371                 if ( ( editor && ! editor.isHidden() && editor.isDirty() ) ||
372                         ( wp.autosave && wp.autosave.server.postChanged() ) ) {
373
374                         return postL10n.saveAlert;
375                 }
376         }).on( 'unload.edit-post', function( event ) {
377                 if ( ! releaseLock ) {
378                         return;
379                 }
380
381                 // Unload is triggered (by hand) on removing the Thickbox iframe.
382                 // Make sure we process only the main document unload.
383                 if ( event.target && event.target.nodeName != '#document' ) {
384                         return;
385                 }
386
387                 $.ajax({
388                         type: 'POST',
389                         url: ajaxurl,
390                         async: false,
391                         data: {
392                                 action: 'wp-remove-post-lock',
393                                 _wpnonce: $('#_wpnonce').val(),
394                                 post_ID: $('#post_ID').val(),
395                                 active_post_lock: $('#active_post_lock').val()
396                         }
397                 });
398         });
399
400         // multi-taxonomies
401         if ( $('#tagsdiv-post_tag').length ) {
402                 window.tagBox && window.tagBox.init();
403         } else {
404                 $('#side-sortables, #normal-sortables, #advanced-sortables').children('div.postbox').each(function(){
405                         if ( this.id.indexOf('tagsdiv-') === 0 ) {
406                                 window.tagBox && window.tagBox.init();
407                                 return false;
408                         }
409                 });
410         }
411
412         // categories
413         $('.categorydiv').each( function(){
414                 var this_id = $(this).attr('id'), catAddBefore, catAddAfter, taxonomyParts, taxonomy, settingName;
415
416                 taxonomyParts = this_id.split('-');
417                 taxonomyParts.shift();
418                 taxonomy = taxonomyParts.join('-');
419                 settingName = taxonomy + '_tab';
420                 if ( taxonomy == 'category' )
421                         settingName = 'cats';
422
423                 // TODO: move to jQuery 1.3+, support for multiple hierarchical taxonomies, see wp-lists.js
424                 $('a', '#' + taxonomy + '-tabs').click( function(){
425                         var t = $(this).attr('href');
426                         $(this).parent().addClass('tabs').siblings('li').removeClass('tabs');
427                         $('#' + taxonomy + '-tabs').siblings('.tabs-panel').hide();
428                         $(t).show();
429                         if ( '#' + taxonomy + '-all' == t )
430                                 deleteUserSetting( settingName );
431                         else
432                                 setUserSetting( settingName, 'pop' );
433                         return false;
434                 });
435
436                 if ( getUserSetting( settingName ) )
437                         $('a[href="#' + taxonomy + '-pop"]', '#' + taxonomy + '-tabs').click();
438
439                 // Ajax Cat
440                 $( '#new' + taxonomy ).one( 'focus', function() { $( this ).val( '' ).removeClass( 'form-input-tip' ); } );
441
442                 $('#new' + taxonomy).keypress( function(event){
443                         if( 13 === event.keyCode ) {
444                                 event.preventDefault();
445                                 $('#' + taxonomy + '-add-submit').click();
446                         }
447                 });
448                 $('#' + taxonomy + '-add-submit').click( function(){ $('#new' + taxonomy).focus(); });
449
450                 catAddBefore = function( s ) {
451                         if ( !$('#new'+taxonomy).val() )
452                                 return false;
453                         s.data += '&' + $( ':checked', '#'+taxonomy+'checklist' ).serialize();
454                         $( '#' + taxonomy + '-add-submit' ).prop( 'disabled', true );
455                         return s;
456                 };
457
458                 catAddAfter = function( r, s ) {
459                         var sup, drop = $('#new'+taxonomy+'_parent');
460
461                         $( '#' + taxonomy + '-add-submit' ).prop( 'disabled', false );
462                         if ( 'undefined' != s.parsed.responses[0] && (sup = s.parsed.responses[0].supplemental.newcat_parent) ) {
463                                 drop.before(sup);
464                                 drop.remove();
465                         }
466                 };
467
468                 $('#' + taxonomy + 'checklist').wpList({
469                         alt: '',
470                         response: taxonomy + '-ajax-response',
471                         addBefore: catAddBefore,
472                         addAfter: catAddAfter
473                 });
474
475                 $('#' + taxonomy + '-add-toggle').click( function() {
476                         $('#' + taxonomy + '-adder').toggleClass( 'wp-hidden-children' );
477                         $('a[href="#' + taxonomy + '-all"]', '#' + taxonomy + '-tabs').click();
478                         $('#new'+taxonomy).focus();
479                         return false;
480                 });
481
482                 $('#' + taxonomy + 'checklist, #' + taxonomy + 'checklist-pop').on( 'click', 'li.popular-category > label input[type="checkbox"]', function() {
483                         var t = $(this), c = t.is(':checked'), id = t.val();
484                         if ( id && t.parents('#taxonomy-'+taxonomy).length )
485                                 $('#in-' + taxonomy + '-' + id + ', #in-popular-' + taxonomy + '-' + id).prop( 'checked', c );
486                 });
487
488         }); // end cats
489
490         // Custom Fields
491         if ( $('#postcustom').length ) {
492                 $( '#the-list' ).wpList( { addAfter: function() {
493                         $('table#list-table').show();
494                 }, addBefore: function( s ) {
495                         s.data += '&post_id=' + $('#post_ID').val();
496                         return s;
497                 }
498                 });
499         }
500
501         // submitdiv
502         if ( $('#submitdiv').length ) {
503                 stamp = $('#timestamp').html();
504                 visibility = $('#post-visibility-display').html();
505
506                 updateVisibility = function() {
507                         if ( $postVisibilitySelect.find('input:radio:checked').val() != 'public' ) {
508                                 $('#sticky').prop('checked', false);
509                                 $('#sticky-span').hide();
510                         } else {
511                                 $('#sticky-span').show();
512                         }
513                         if ( $postVisibilitySelect.find('input:radio:checked').val() != 'password' ) {
514                                 $('#password-span').hide();
515                         } else {
516                                 $('#password-span').show();
517                         }
518                 };
519
520                 updateText = function() {
521
522                         if ( ! $timestampdiv.length )
523                                 return true;
524
525                         var attemptedDate, originalDate, currentDate, publishOn, postStatus = $('#post_status'),
526                                 optPublish = $('option[value="publish"]', postStatus), aa = $('#aa').val(),
527                                 mm = $('#mm').val(), jj = $('#jj').val(), hh = $('#hh').val(), mn = $('#mn').val();
528
529                         attemptedDate = new Date( aa, mm - 1, jj, hh, mn );
530                         originalDate = new Date( $('#hidden_aa').val(), $('#hidden_mm').val() -1, $('#hidden_jj').val(), $('#hidden_hh').val(), $('#hidden_mn').val() );
531                         currentDate = new Date( $('#cur_aa').val(), $('#cur_mm').val() -1, $('#cur_jj').val(), $('#cur_hh').val(), $('#cur_mn').val() );
532
533                         if ( attemptedDate.getFullYear() != aa || (1 + attemptedDate.getMonth()) != mm || attemptedDate.getDate() != jj || attemptedDate.getMinutes() != mn ) {
534                                 $timestampdiv.find('.timestamp-wrap').addClass('form-invalid');
535                                 return false;
536                         } else {
537                                 $timestampdiv.find('.timestamp-wrap').removeClass('form-invalid');
538                         }
539
540                         if ( attemptedDate > currentDate && $('#original_post_status').val() != 'future' ) {
541                                 publishOn = postL10n.publishOnFuture;
542                                 $('#publish').val( postL10n.schedule );
543                         } else if ( attemptedDate <= currentDate && $('#original_post_status').val() != 'publish' ) {
544                                 publishOn = postL10n.publishOn;
545                                 $('#publish').val( postL10n.publish );
546                         } else {
547                                 publishOn = postL10n.publishOnPast;
548                                 $('#publish').val( postL10n.update );
549                         }
550                         if ( originalDate.toUTCString() == attemptedDate.toUTCString() ) { //hack
551                                 $('#timestamp').html(stamp);
552                         } else {
553                                 $('#timestamp').html(
554                                         publishOn + ' <b>' +
555                                         postL10n.dateFormat.replace( '%1$s', $('option[value="' + $('#mm').val() + '"]', '#mm').text() )
556                                                 .replace( '%2$s', jj )
557                                                 .replace( '%3$s', aa )
558                                                 .replace( '%4$s', hh )
559                                                 .replace( '%5$s', mn ) +
560                                                 '</b> '
561                                 );
562                         }
563
564                         if ( $postVisibilitySelect.find('input:radio:checked').val() == 'private' ) {
565                                 $('#publish').val( postL10n.update );
566                                 if ( 0 === optPublish.length ) {
567                                         postStatus.append('<option value="publish">' + postL10n.privatelyPublished + '</option>');
568                                 } else {
569                                         optPublish.html( postL10n.privatelyPublished );
570                                 }
571                                 $('option[value="publish"]', postStatus).prop('selected', true);
572                                 $('#misc-publishing-actions .edit-post-status').hide();
573                         } else {
574                                 if ( $('#original_post_status').val() == 'future' || $('#original_post_status').val() == 'draft' ) {
575                                         if ( optPublish.length ) {
576                                                 optPublish.remove();
577                                                 postStatus.val($('#hidden_post_status').val());
578                                         }
579                                 } else {
580                                         optPublish.html( postL10n.published );
581                                 }
582                                 if ( postStatus.is(':hidden') )
583                                         $('#misc-publishing-actions .edit-post-status').show();
584                         }
585                         $('#post-status-display').html($('option:selected', postStatus).text());
586                         if ( $('option:selected', postStatus).val() == 'private' || $('option:selected', postStatus).val() == 'publish' ) {
587                                 $('#save-post').hide();
588                         } else {
589                                 $('#save-post').show();
590                                 if ( $('option:selected', postStatus).val() == 'pending' ) {
591                                         $('#save-post').show().val( postL10n.savePending );
592                                 } else {
593                                         $('#save-post').show().val( postL10n.saveDraft );
594                                 }
595                         }
596                         return true;
597                 };
598
599                 $( '#visibility .edit-visibility').click( function () {
600                         if ( $postVisibilitySelect.is(':hidden') ) {
601                                 updateVisibility();
602                                 $postVisibilitySelect.slideDown('fast').find('input[type="radio"]').first().focus();
603                                 $(this).hide();
604                         }
605                         return false;
606                 });
607
608                 $postVisibilitySelect.find('.cancel-post-visibility').click( function( event ) {
609                         $postVisibilitySelect.slideUp('fast');
610                         $('#visibility-radio-' + $('#hidden-post-visibility').val()).prop('checked', true);
611                         $('#post_password').val($('#hidden-post-password').val());
612                         $('#sticky').prop('checked', $('#hidden-post-sticky').prop('checked'));
613                         $('#post-visibility-display').html(visibility);
614                         $('#visibility .edit-visibility').show().focus();
615                         updateText();
616                         event.preventDefault();
617                 });
618
619                 $postVisibilitySelect.find('.save-post-visibility').click( function( event ) { // crazyhorse - multiple ok cancels
620                         $postVisibilitySelect.slideUp('fast');
621                         $('#visibility .edit-visibility').show();
622                         updateText();
623
624                         if ( $postVisibilitySelect.find('input:radio:checked').val() != 'public' ) {
625                                 $('#sticky').prop('checked', false);
626                         } // WEAPON LOCKED
627
628                         if ( $('#sticky').prop('checked') ) {
629                                 sticky = 'Sticky';
630                         } else {
631                                 sticky = '';
632                         }
633
634                         $('#post-visibility-display').html(     postL10n[ $postVisibilitySelect.find('input:radio:checked').val() + sticky ]    );
635                         event.preventDefault();
636                 });
637
638                 $postVisibilitySelect.find('input:radio').change( function() {
639                         updateVisibility();
640                 });
641
642                 $timestampdiv.siblings('a.edit-timestamp').click( function( event ) {
643                         if ( $timestampdiv.is( ':hidden' ) ) {
644                                 $timestampdiv.slideDown('fast');
645                                 $('#mm').focus();
646                                 $(this).hide();
647                         }
648                         event.preventDefault();
649                 });
650
651                 $timestampdiv.find('.cancel-timestamp').click( function( event ) {
652                         $timestampdiv.slideUp('fast').siblings('a.edit-timestamp').show().focus();
653                         $('#mm').val($('#hidden_mm').val());
654                         $('#jj').val($('#hidden_jj').val());
655                         $('#aa').val($('#hidden_aa').val());
656                         $('#hh').val($('#hidden_hh').val());
657                         $('#mn').val($('#hidden_mn').val());
658                         updateText();
659                         event.preventDefault();
660                 });
661
662                 $timestampdiv.find('.save-timestamp').click( function( event ) { // crazyhorse - multiple ok cancels
663                         if ( updateText() ) {
664                                 $timestampdiv.slideUp('fast');
665                                 $timestampdiv.siblings('a.edit-timestamp').show();
666                         }
667                         event.preventDefault();
668                 });
669
670                 $('#post').on( 'submit', function( event ) {
671                         if ( ! updateText() ) {
672                                 event.preventDefault();
673                                 $timestampdiv.show();
674
675                                 if ( wp.autosave ) {
676                                         wp.autosave.enableButtons();
677                                 }
678
679                                 $( '#publishing-action .spinner' ).removeClass( 'is-active' );
680                         }
681                 });
682
683                 $postStatusSelect.siblings('a.edit-post-status').click( function( event ) {
684                         if ( $postStatusSelect.is( ':hidden' ) ) {
685                                 $postStatusSelect.slideDown('fast').find('select').focus();
686                                 $(this).hide();
687                         }
688                         event.preventDefault();
689                 });
690
691                 $postStatusSelect.find('.save-post-status').click( function( event ) {
692                         $postStatusSelect.slideUp('fast').siblings('a.edit-post-status').show();
693                         updateText();
694                         event.preventDefault();
695                 });
696
697                 $postStatusSelect.find('.cancel-post-status').click( function( event ) {
698                         $('#post-status-select').slideUp('fast').siblings( 'a.edit-post-status' ).show().focus();
699                         $('#post_status').val( $('#hidden_post_status').val() );
700                         updateText();
701                         event.preventDefault();
702                 });
703         } // end submitdiv
704
705         // permalink
706         function editPermalink() {
707                 var i, slug_value,
708                         c = 0,
709                         e = $('#editable-post-name'),
710                         revert_e = e.html(),
711                         real_slug = $('#post_name'),
712                         revert_slug = real_slug.val(),
713                         b = $('#edit-slug-buttons'),
714                         revert_b = b.html(),
715                         full = $('#editable-post-name-full');
716
717                 // Deal with Twemoji in the post-name
718                 full.find( 'img' ).replaceWith( function() { return this.alt; } );
719                 full = full.html();
720
721                 $('#view-post-btn').hide();
722                 b.html('<a href="#" class="save button button-small">'+postL10n.ok+'</a> <a class="cancel" href="#">'+postL10n.cancel+'</a>');
723                 b.children('.save').click(function() {
724                         var new_slug = e.children('input').val();
725                         if ( new_slug == $('#editable-post-name-full').text() ) {
726                                 b.children('.cancel').click();
727                                 return false;
728                         }
729                         $.post(ajaxurl, {
730                                 action: 'sample-permalink',
731                                 post_id: postId,
732                                 new_slug: new_slug,
733                                 new_title: $('#title').val(),
734                                 samplepermalinknonce: $('#samplepermalinknonce').val()
735                         }, function(data) {
736                                 var box = $('#edit-slug-box');
737                                 box.html(data);
738                                 if (box.hasClass('hidden')) {
739                                         box.fadeIn('fast', function () {
740                                                 box.removeClass('hidden');
741                                         });
742                                 }
743
744                                 b.html(revert_b);
745                                 real_slug.val(new_slug);
746                                 $('#view-post-btn').show();
747                         });
748                         return false;
749                 });
750
751                 b.children('.cancel').click(function() {
752                         $('#view-post-btn').show();
753                         e.html(revert_e);
754                         b.html(revert_b);
755                         real_slug.val(revert_slug);
756                         return false;
757                 });
758
759                 for ( i = 0; i < full.length; ++i ) {
760                         if ( '%' == full.charAt(i) )
761                                 c++;
762                 }
763
764                 slug_value = ( c > full.length / 4 ) ? '' : full;
765                 e.html('<input type="text" id="new-post-slug" value="'+slug_value+'" />').children('input').keypress(function(e) {
766                         var key = e.keyCode || 0;
767                         // on enter, just save the new slug, don't save the post
768                         if ( 13 == key ) {
769                                 b.children('.save').click();
770                                 return false;
771                         }
772                         if ( 27 == key ) {
773                                 b.children('.cancel').click();
774                                 return false;
775                         }
776                 } ).keyup( function() {
777                         real_slug.val(this.value);
778                 }).focus();
779         }
780
781         if ( $editSlugWrap.length ) {
782                 $editSlugWrap.on( 'click', function( event ) {
783                         var $target = $( event.target );
784
785                         if ( $target.is('#editable-post-name') || $target.hasClass('edit-slug') ) {
786                                 editPermalink();
787                         }
788                 });
789         }
790
791         // word count
792         if ( typeof(wpWordCount) != 'undefined' ) {
793                 $document.triggerHandler('wpcountwords', [ co.val() ]);
794
795                 co.keyup( function(e) {
796                         var k = e.keyCode || e.charCode;
797
798                         if ( k == last )
799                                 return true;
800
801                         if ( 13 == k || 8 == last || 46 == last )
802                                 $document.triggerHandler('wpcountwords', [ co.val() ]);
803
804                         last = k;
805                         return true;
806                 });
807         }
808
809         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                         $textarea = $('textarea#content'),
839                         $handle = $('#post-status-info'),
840                         $postdivrich = $('#postdivrich');
841
842                 // No point for touch devices
843                 if ( ! $textarea.length || 'ontouchstart' in window ) {
844                         // Hide the resize handle
845                         $('#content-resize-handle').hide();
846                         return;
847                 }
848
849                 function dragging( event ) {
850                         if ( $postdivrich.hasClass( 'wp-editor-expand' ) ) {
851                                 return;
852                         }
853
854                         if ( mce ) {
855                                 editor.theme.resizeTo( null, offset + event.pageY );
856                         } else {
857                                 $textarea.height( Math.max( 50, offset + event.pageY ) );
858                         }
859
860                         event.preventDefault();
861                 }
862
863                 function endDrag() {
864                         var height, toolbarHeight;
865
866                         if ( $postdivrich.hasClass( 'wp-editor-expand' ) ) {
867                                 return;
868                         }
869
870                         if ( mce ) {
871                                 editor.focus();
872                                 toolbarHeight = parseInt( $( '#wp-content-editor-container .mce-toolbar-grp' ).height(), 10 );
873
874                                 if ( toolbarHeight < 10 || toolbarHeight > 200 ) {
875                                         toolbarHeight = 30;
876                                 }
877
878                                 height = parseInt( $('#content_ifr').css('height'), 10 ) + toolbarHeight - 28;
879                         } else {
880                                 $textarea.focus();
881                                 height = parseInt( $textarea.css('height'), 10 );
882                         }
883
884                         $document.off( '.wp-editor-resize' );
885
886                         // sanity check
887                         if ( height && height > 50 && height < 5000 ) {
888                                 setUserSetting( 'ed_size', height );
889                         }
890                 }
891
892                 $handle.on( 'mousedown.wp-editor-resize', function( event ) {
893                         if ( typeof tinymce !== 'undefined' ) {
894                                 editor = tinymce.get('content');
895                         }
896
897                         if ( editor && ! editor.isHidden() ) {
898                                 mce = true;
899                                 offset = $('#content_ifr').height() - event.pageY;
900                         } else {
901                                 mce = false;
902                                 offset = $textarea.height() - event.pageY;
903                                 $textarea.blur();
904                         }
905
906                         $document.on( 'mousemove.wp-editor-resize', dragging )
907                                 .on( 'mouseup.wp-editor-resize mouseleave.wp-editor-resize', endDrag );
908
909                         event.preventDefault();
910                 }).on( 'mouseup.wp-editor-resize', endDrag );
911         })();
912
913         if ( typeof tinymce !== 'undefined' ) {
914                 // When changing post formats, change the editor body class
915                 $( '#post-formats-select input.post-format' ).on( 'change.set-editor-class', function() {
916                         var editor, body, format = this.id;
917
918                         if ( format && $( this ).prop( 'checked' ) && ( editor = tinymce.get( 'content' ) ) ) {
919                                 body = editor.getBody();
920                                 body.className = body.className.replace( /\bpost-format-[^ ]+/, '' );
921                                 editor.dom.addClass( body, format == 'post-format-0' ? 'post-format-standard' : format );
922                                 $( document ).trigger( 'editor-classchange' );
923                         }
924                 });
925         }
926 });