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