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