1 var tagBox, commentsBox, editPermalink, makeSlugeditClickable, WPSetThumbnailHTML, WPSetThumbnailID, WPRemoveThumbnail, wptitlehint;
3 // return an array with any duplicate, whitespace or values removed
4 function array_unique_noempty(a) {
6 jQuery.each( a, function(key, val) {
7 val = jQuery.trim(val);
8 if ( val && jQuery.inArray(val, out) == -1 )
17 clean : function(tags) {
18 var comma = postL10n.comma;
20 tags = tags.replace(new RegExp(comma, 'g'), ',');
21 tags = tags.replace(/\s*,\s*/g, ',').replace(/,+/g, ',').replace(/[,\s]+$/, '').replace(/^[,\s]+/, '');
23 tags = tags.replace(/,/g, comma);
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];
33 $.each( current_tags, function(key, val) {
40 thetags.val( this.clean( new_tags.join(comma) ) );
42 this.quickClicks(taxbox);
46 quickClicks : function(el) {
47 var thetags = $('.the-tags', el),
48 tagchecklist = $('.tagchecklist', el),
49 id = $(el).attr('id'),
50 current_tags, disabled;
52 if ( !thetags.length )
55 disabled = thetags.prop('disabled');
57 current_tags = thetags.val().split(postL10n.comma);
60 $.each( current_tags, function( key, val ) {
68 // Create a new span, and ensure the text is properly escaped.
69 span = $('<span />').text( val );
71 // If tags editing isn't disabled, create the X button.
73 xbutton = $( '<a id="' + id + '-check-num-' + key + '" class="ntdelbutton">X</a>' );
74 xbutton.click( function(){ tagBox.parseTags(this); });
75 span.prepend(' ').prepend( xbutton );
78 // Append the span to the tag list.
79 tagchecklist.append( span );
83 flushTags : function(el, a, f) {
85 var tags = $('.the-tags', el),
86 newtag = $('input.newtag', el),
87 comma = postL10n.comma,
90 text = a ? $(a).text() : newtag.val();
92 newtags = tagsval ? tagsval + comma + text : text;
94 newtags = this.clean( newtags );
95 newtags = array_unique_noempty( newtags.split(comma) ).join(comma);
101 if ( 'undefined' == typeof(f) )
108 var tax = id.substr(id.indexOf('-')+1);
110 $.post(ajaxurl, {'action':'get-tagcloud', 'tax':tax}, function(r, stat) {
111 if ( 0 == r || 'success' != stat )
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);
125 var t = this, ajaxtag = $('div.ajaxtag');
127 $('.tagsdiv').each( function() {
128 tagBox.quickClicks(this);
131 $('input.tagadd', ajaxtag).click(function(){
132 t.flushTags( $(this).closest('.tagsdiv') );
135 $('div.taghint', ajaxtag).click(function(){
136 $(this).css('visibility', 'hidden').parent().siblings('.newtag').focus();
139 $('input.newtag', ajaxtag).blur(function() {
140 if ( this.value == '' )
141 $(this).parent().siblings('.taghint').css('visibility', '');
143 $(this).parent().siblings('.taghint').css('visibility', 'hidden');
144 }).keyup(function(e){
145 if ( 13 == e.which ) {
146 tagBox.flushTags( $(this).closest('.tagsdiv') );
149 }).keypress(function(e){
150 if ( 13 == e.which ) {
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 + ' ' } );
159 // save tags on post save/publish
160 $('#post').submit(function(){
161 $('div.tagsdiv').each( function() {
162 tagBox.flushTags(this, false, 1);
167 $('a.tagcloud-link').click(function(){
168 if ( ! $('.the-tagcloud').length )
169 tagBox.get( $(this).attr('id') );
170 $(this).siblings('.the-tagcloud').toggle();
179 get : function(total, num) {
180 var st = this.st, data;
186 $('#commentsdiv .spinner').show();
189 'action' : 'get-comments',
191 '_ajax_nonce' : $('#add_comment_nonce').val(),
192 'p' : $('#post_ID').val(),
197 $.post(ajaxurl, data,
199 r = wpAjax.parseAjaxResponse(r);
200 $('#commentsdiv .widefat').show();
201 $('#commentsdiv .spinner').hide();
203 if ( 'object' == typeof r && r.responses[0] ) {
204 $('#the-comment-list').append( r.responses[0].data );
206 theList = theExtraList = null;
207 $("a[className*=':']").unbind();
209 if ( commentsBox.st > commentsBox.total )
210 $('#show-comments').hide();
212 $('#show-comments').show().children('a').html(postL10n.showcomm);
215 } else if ( 1 == r ) {
216 $('#show-comments').html(postL10n.endcomm);
220 $('#the-comment-list').append('<tr><td colspan="2">'+wpAjax.broken+'</td></tr>');
228 WPSetThumbnailHTML = function(html){
229 $('.inside', '#postimagediv').html(html);
232 WPSetThumbnailID = function(id){
233 var field = $('input[value="_thumbnail_id"]', '#list-table');
234 if ( field.size() > 0 ) {
235 $('#meta\\[' + field.attr('id').match(/[0-9]+/) + '\\]\\[value\\]').text(id);
239 WPRemoveThumbnail = function(nonce){
241 action:"set-post-thumbnail", post_id: $('#post_ID').val(), thumbnail_id: -1, _ajax_nonce: nonce, cookie: encodeURIComponent(document.cookie)
244 alert( setPostThumbnailL10n.error );
246 WPSetThumbnailHTML(str);
254 jQuery(document).ready( function($) {
255 var stamp, visibility, sticky = '', last = 0, co = $('#content');
257 postboxes.add_postbox_toggles(pagenow);
260 if ( $('#tagsdiv-post_tag').length ) {
263 $('#side-sortables, #normal-sortables, #advanced-sortables').children('div.postbox').each(function(){
264 if ( this.id.indexOf('tagsdiv-') === 0 ) {
272 $('.categorydiv').each( function(){
273 var this_id = $(this).attr('id'), noSyncChecks = false, syncChecks, catAddAfter, taxonomyParts, taxonomy, settingName;
275 taxonomyParts = this_id.split('-');
276 taxonomyParts.shift();
277 taxonomy = taxonomyParts.join('-');
278 settingName = taxonomy + '_tab';
279 if ( taxonomy == 'category' )
280 settingName = 'cats';
282 // TODO: move to jQuery 1.3+, support for multiple hierarchical taxonomies, see wp-lists.js
283 $('a', '#' + taxonomy + '-tabs').click( function(){
284 var t = $(this).attr('href');
285 $(this).parent().addClass('tabs').siblings('li').removeClass('tabs');
286 $('#' + taxonomy + '-tabs').siblings('.tabs-panel').hide();
288 if ( '#' + taxonomy + '-all' == t )
289 deleteUserSetting(settingName);
291 setUserSetting(settingName, 'pop');
295 if ( getUserSetting(settingName) )
296 $('a[href="#' + taxonomy + '-pop"]', '#' + taxonomy + '-tabs').click();
299 $('#new' + taxonomy).one( 'focus', function() { $(this).val( '' ).removeClass( 'form-input-tip' ) } );
301 $('#new' + taxonomy).keypress( function(event){
302 if( 13 === event.keyCode ) {
303 event.preventDefault();
304 $('#' + taxonomy + '-add-submit').click();
307 $('#' + taxonomy + '-add-submit').click( function(){ $('#new' + taxonomy).focus(); });
309 syncChecks = function() {
313 var th = jQuery(this), c = th.is(':checked'), id = th.val().toString();
314 $('#in-' + taxonomy + '-' + id + ', #in-' + taxonomy + '-category-' + id).prop( 'checked', c );
315 noSyncChecks = false;
318 catAddBefore = function( s ) {
319 if ( !$('#new'+taxonomy).val() )
321 s.data += '&' + $( ':checked', '#'+taxonomy+'checklist' ).serialize();
322 $( '#' + taxonomy + '-add-submit' ).prop( 'disabled', true );
326 catAddAfter = function( r, s ) {
327 var sup, drop = $('#new'+taxonomy+'_parent');
329 $( '#' + taxonomy + '-add-submit' ).prop( 'disabled', false );
330 if ( 'undefined' != s.parsed.responses[0] && (sup = s.parsed.responses[0].supplemental.newcat_parent) ) {
336 $('#' + taxonomy + 'checklist').wpList({
338 response: taxonomy + '-ajax-response',
339 addBefore: catAddBefore,
340 addAfter: catAddAfter
343 $('#' + taxonomy + '-add-toggle').click( function() {
344 $('#' + taxonomy + '-adder').toggleClass( 'wp-hidden-children' );
345 $('a[href="#' + taxonomy + '-all"]', '#' + taxonomy + '-tabs').click();
346 $('#new'+taxonomy).focus();
350 $('#' + taxonomy + 'checklist li.popular-category input[type="checkbox"], #' + taxonomy + 'checklist-pop input[type="checkbox"]').live( 'click', function(){
351 var t = $(this), c = t.is(':checked'), id = t.val();
352 if ( id && t.parents('#taxonomy-'+taxonomy).length )
353 $('#in-' + taxonomy + '-' + id + ', #in-popular-' + taxonomy + '-' + id).prop( 'checked', c );
359 if ( $('#postcustom').length ) {
360 $('#the-list').wpList( { addAfter: function( xml, s ) {
361 $('table#list-table').show();
362 }, addBefore: function( s ) {
363 s.data += '&post_id=' + $('#post_ID').val();
370 if ( $('#submitdiv').length ) {
371 stamp = $('#timestamp').html();
372 visibility = $('#post-visibility-display').html();
374 function updateVisibility() {
375 var pvSelect = $('#post-visibility-select');
376 if ( $('input:radio:checked', pvSelect).val() != 'public' ) {
377 $('#sticky').prop('checked', false);
378 $('#sticky-span').hide();
380 $('#sticky-span').show();
382 if ( $('input:radio:checked', pvSelect).val() != 'password' ) {
383 $('#password-span').hide();
385 $('#password-span').show();
389 function updateText() {
391 if ( ! $('#timestampdiv').length )
394 var attemptedDate, originalDate, currentDate, publishOn, postStatus = $('#post_status'),
395 optPublish = $('option[value="publish"]', postStatus), aa = $('#aa').val(),
396 mm = $('#mm').val(), jj = $('#jj').val(), hh = $('#hh').val(), mn = $('#mn').val();
398 attemptedDate = new Date( aa, mm - 1, jj, hh, mn );
399 originalDate = new Date( $('#hidden_aa').val(), $('#hidden_mm').val() -1, $('#hidden_jj').val(), $('#hidden_hh').val(), $('#hidden_mn').val() );
400 currentDate = new Date( $('#cur_aa').val(), $('#cur_mm').val() -1, $('#cur_jj').val(), $('#cur_hh').val(), $('#cur_mn').val() );
402 if ( attemptedDate.getFullYear() != aa || (1 + attemptedDate.getMonth()) != mm || attemptedDate.getDate() != jj || attemptedDate.getMinutes() != mn ) {
403 $('.timestamp-wrap', '#timestampdiv').addClass('form-invalid');
406 $('.timestamp-wrap', '#timestampdiv').removeClass('form-invalid');
409 if ( attemptedDate > currentDate && $('#original_post_status').val() != 'future' ) {
410 publishOn = postL10n.publishOnFuture;
411 $('#publish').val( postL10n.schedule );
412 } else if ( attemptedDate <= currentDate && $('#original_post_status').val() != 'publish' ) {
413 publishOn = postL10n.publishOn;
414 $('#publish').val( postL10n.publish );
416 publishOn = postL10n.publishOnPast;
417 $('#publish').val( postL10n.update );
419 if ( originalDate.toUTCString() == attemptedDate.toUTCString() ) { //hack
420 $('#timestamp').html(stamp);
422 $('#timestamp').html(
424 $('option[value="' + $('#mm').val() + '"]', '#mm').text() + ' ' +
432 if ( $('input:radio:checked', '#post-visibility-select').val() == 'private' ) {
433 $('#publish').val( postL10n.update );
434 if ( optPublish.length == 0 ) {
435 postStatus.append('<option value="publish">' + postL10n.privatelyPublished + '</option>');
437 optPublish.html( postL10n.privatelyPublished );
439 $('option[value="publish"]', postStatus).prop('selected', true);
440 $('.edit-post-status', '#misc-publishing-actions').hide();
442 if ( $('#original_post_status').val() == 'future' || $('#original_post_status').val() == 'draft' ) {
443 if ( optPublish.length ) {
445 postStatus.val($('#hidden_post_status').val());
448 optPublish.html( postL10n.published );
450 if ( postStatus.is(':hidden') )
451 $('.edit-post-status', '#misc-publishing-actions').show();
453 $('#post-status-display').html($('option:selected', postStatus).text());
454 if ( $('option:selected', postStatus).val() == 'private' || $('option:selected', postStatus).val() == 'publish' ) {
455 $('#save-post').hide();
457 $('#save-post').show();
458 if ( $('option:selected', postStatus).val() == 'pending' ) {
459 $('#save-post').show().val( postL10n.savePending );
461 $('#save-post').show().val( postL10n.saveDraft );
467 $('.edit-visibility', '#visibility').click(function () {
468 if ($('#post-visibility-select').is(":hidden")) {
470 $('#post-visibility-select').slideDown('fast');
476 $('.cancel-post-visibility', '#post-visibility-select').click(function () {
477 $('#post-visibility-select').slideUp('fast');
478 $('#visibility-radio-' + $('#hidden-post-visibility').val()).prop('checked', true);
479 $('#post_password').val($('#hidden_post_password').val());
480 $('#sticky').prop('checked', $('#hidden-post-sticky').prop('checked'));
481 $('#post-visibility-display').html(visibility);
482 $('.edit-visibility', '#visibility').show();
487 $('.save-post-visibility', '#post-visibility-select').click(function () { // crazyhorse - multiple ok cancels
488 var pvSelect = $('#post-visibility-select');
490 pvSelect.slideUp('fast');
491 $('.edit-visibility', '#visibility').show();
494 if ( $('input:radio:checked', pvSelect).val() != 'public' ) {
495 $('#sticky').prop('checked', false);
498 if ( true == $('#sticky').prop('checked') ) {
504 $('#post-visibility-display').html( postL10n[$('input:radio:checked', pvSelect).val() + sticky] );
508 $('input:radio', '#post-visibility-select').change(function() {
512 $('#timestampdiv').siblings('a.edit-timestamp').click(function() {
513 if ($('#timestampdiv').is(":hidden")) {
514 $('#timestampdiv').slideDown('fast');
521 $('.cancel-timestamp', '#timestampdiv').click(function() {
522 $('#timestampdiv').slideUp('fast');
523 $('#mm').val($('#hidden_mm').val());
524 $('#jj').val($('#hidden_jj').val());
525 $('#aa').val($('#hidden_aa').val());
526 $('#hh').val($('#hidden_hh').val());
527 $('#mn').val($('#hidden_mn').val());
528 $('#timestampdiv').siblings('a.edit-timestamp').show();
533 $('.save-timestamp', '#timestampdiv').click(function () { // crazyhorse - multiple ok cancels
534 if ( updateText() ) {
535 $('#timestampdiv').slideUp('fast');
536 $('#timestampdiv').siblings('a.edit-timestamp').show();
541 $('#post').on( 'submit', function(e){
542 if ( ! updateText() ) {
544 $('#timestampdiv').show();
545 $('#publishing-action .spinner').hide();
546 $('#publish').prop('disabled', false).removeClass('button-primary-disabled');
551 $('#post-status-select').siblings('a.edit-post-status').click(function() {
552 if ($('#post-status-select').is(":hidden")) {
553 $('#post-status-select').slideDown('fast');
559 $('.save-post-status', '#post-status-select').click(function() {
560 $('#post-status-select').slideUp('fast');
561 $('#post-status-select').siblings('a.edit-post-status').show();
566 $('.cancel-post-status', '#post-status-select').click(function() {
567 $('#post-status-select').slideUp('fast');
568 $('#post_status').val($('#hidden_post_status').val());
569 $('#post-status-select').siblings('a.edit-post-status').show();
576 if ( $('#edit-slug-box').length ) {
577 editPermalink = function(post_id) {
578 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();
580 $('#view-post-btn').hide();
581 b.html('<a href="#" class="save button button-small">'+postL10n.ok+'</a> <a class="cancel" href="#">'+postL10n.cancel+'</a>');
582 b.children('.save').click(function() {
583 var new_slug = e.children('input').val();
584 if ( new_slug == $('#editable-post-name-full').text() ) {
585 return $('.cancel', '#edit-slug-buttons').click();
588 action: 'sample-permalink',
591 new_title: $('#title').val(),
592 samplepermalinknonce: $('#samplepermalinknonce').val()
594 $('#edit-slug-box').html(data);
596 real_slug.val(new_slug);
597 makeSlugeditClickable();
598 $('#view-post-btn').show();
603 $('.cancel', '#edit-slug-buttons').click(function() {
604 $('#view-post-btn').show();
607 real_slug.val(revert_slug);
611 for ( i = 0; i < full.length; ++i ) {
612 if ( '%' == full.charAt(i) )
616 slug_value = ( c > full.length / 4 ) ? '' : full;
617 e.html('<input type="text" id="new-post-slug" value="'+slug_value+'" />').children('input').keypress(function(e){
618 var key = e.keyCode || 0;
619 // on enter, just save the new slug, don't save the post
621 b.children('.save').click();
625 b.children('.cancel').click();
628 real_slug.val(this.value);
632 makeSlugeditClickable = function() {
633 $('#editable-post-name').click(function() {
634 $('#edit-slug-buttons').children('.edit-slug').click();
637 makeSlugeditClickable();
641 if ( typeof(wpWordCount) != 'undefined' ) {
642 $(document).triggerHandler('wpcountwords', [ co.val() ]);
644 co.keyup( function(e) {
645 var k = e.keyCode || e.charCode;
650 if ( 13 == k || 8 == last || 46 == last )
651 $(document).triggerHandler('wpcountwords', [ co.val() ]);
658 wptitlehint = function(id) {
661 var title = $('#' + id), titleprompt = $('#' + id + '-prompt-text');
663 if ( title.val() == '' )
664 titleprompt.removeClass('screen-reader-text');
666 titleprompt.click(function(){
667 $(this).addClass('screen-reader-text');
671 title.blur(function(){
672 if ( this.value == '' )
673 titleprompt.removeClass('screen-reader-text');
675 titleprompt.addClass('screen-reader-text');
676 }).keydown(function(e){
677 titleprompt.addClass('screen-reader-text');
684 // resizable textarea#content
686 var textarea = $('textarea#content'), offset = null, el;
687 // No point for touch devices
688 if ( 'ontouchstart' in window )
691 function dragging(e) {
692 textarea.height( Math.max(50, offset + e.pageY) + 'px' );
696 function endDrag(e) {
697 var height = $('#wp-content-editor-container').height();
700 $(document).unbind('mousemove', dragging).unbind('mouseup', endDrag);
702 height -= 33; // compensate for toolbars, padding...
704 if ( height > 50 && height < 5000 && height != getUserSetting( 'ed_size' ) )
705 setUserSetting( 'ed_size', height );
708 textarea.css('resize', 'none');
709 el = $('<div id="content-resize-handle"><br></div>');
710 $('#wp-content-wrap').append(el);
711 el.on('mousedown', function(e) {
712 offset = textarea.height() - e.pageY;
714 $(document).mousemove(dragging).mouseup(endDrag);
719 if ( typeof(tinymce) != 'undefined' ) {
720 tinymce.onAddEditor.add(function(mce, ed){
721 // iOS expands the iframe to full height and the user cannot adjust it.
722 if ( ed.id != 'content' || tinymce.isIOS5 )
725 // resize TinyMCE to match the textarea height when switching Text -> Visual
726 ed.onLoadContent.add( function(ed, o) {
727 var ifr_height, height = parseInt( $('#content').css('height'), 10 ),
728 tb_height = $('#content_tbl tr.mceFirst').height();
730 if ( height && !isNaN(height) && tb_height ) {
731 ifr_height = (height - tb_height) + 12; // compensate for padding in the textarea
733 if ( ifr_height > 50 && ifr_height < 5000 ) {
734 $('#content_tbl').css('height', '' );
735 $('#content_ifr').css('height', ifr_height + 'px' );
740 // resize the textarea to match TinyMCE's height when switching Visual -> Text
741 ed.onSaveContent.add( function(ed, o) {
742 var height = $('#content_tbl').height();
744 if ( height && height > 83 && height < 5000 ) {
747 $('#content').css( 'height', height + 'px' );
751 // save on resizing TinyMCE
752 ed.onPostRender.add(function() {
753 $('#content_resize').on('mousedown.wp-mce-resize', function(e){
754 $(document).on('mouseup.wp-mce-resize', function(e){
755 var height = $('#wp-content-editor-container').height();
759 if ( height > 50 && height < 5000 && height != getUserSetting( 'ed_size' ) )
760 setUserSetting( 'ed_size', height );
762 $(document).off('mouseup.wp-mce-resize');