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 tagBox.get( $(this).attr('id') );
169 $(this).unbind().click(function(){
170 $(this).siblings('.the-tagcloud').toggle();
181 get : function(total, num) {
182 var st = this.st, data;
188 $('#commentsdiv img.waiting').show();
191 'action' : 'get-comments',
193 '_ajax_nonce' : $('#add_comment_nonce').val(),
194 'p' : $('#post_ID').val(),
199 $.post(ajaxurl, data,
201 r = wpAjax.parseAjaxResponse(r);
202 $('#commentsdiv .widefat').show();
203 $('#commentsdiv img.waiting').hide();
205 if ( 'object' == typeof r && r.responses[0] ) {
206 $('#the-comment-list').append( r.responses[0].data );
208 theList = theExtraList = null;
209 $("a[className*=':']").unbind();
211 if ( commentsBox.st > commentsBox.total )
212 $('#show-comments').hide();
214 $('#show-comments').show().children('a').html(postL10n.showcomm);
217 } else if ( 1 == r ) {
218 $('#show-comments').html(postL10n.endcomm);
222 $('#the-comment-list').append('<tr><td colspan="2">'+wpAjax.broken+'</td></tr>');
230 WPSetThumbnailHTML = function(html){
231 $('.inside', '#postimagediv').html(html);
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);
241 WPRemoveThumbnail = function(nonce){
243 action:"set-post-thumbnail", post_id: $('#post_ID').val(), thumbnail_id: -1, _ajax_nonce: nonce, cookie: encodeURIComponent(document.cookie)
246 alert( setPostThumbnailL10n.error );
248 WPSetThumbnailHTML(str);
256 jQuery(document).ready( function($) {
257 var stamp, visibility, sticky = '', last = 0, co = $('#content');
259 postboxes.add_postbox_toggles(pagenow);
262 if ( $('#tagsdiv-post_tag').length ) {
265 $('#side-sortables, #normal-sortables, #advanced-sortables').children('div.postbox').each(function(){
266 if ( this.id.indexOf('tagsdiv-') === 0 ) {
274 $('.categorydiv').each( function(){
275 var this_id = $(this).attr('id'), noSyncChecks = false, syncChecks, catAddAfter, taxonomyParts, taxonomy, settingName;
277 taxonomyParts = this_id.split('-');
278 taxonomyParts.shift();
279 taxonomy = taxonomyParts.join('-');
280 settingName = taxonomy + '_tab';
281 if ( taxonomy == 'category' )
282 settingName = 'cats';
284 // TODO: move to jQuery 1.3+, support for multiple hierarchical taxonomies, see wp-lists.dev.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();
290 if ( '#' + taxonomy + '-all' == t )
291 deleteUserSetting(settingName);
293 setUserSetting(settingName, 'pop');
297 if ( getUserSetting(settingName) )
298 $('a[href="#' + taxonomy + '-pop"]', '#' + taxonomy + '-tabs').click();
301 $('#new' + taxonomy).one( 'focus', function() { $(this).val( '' ).removeClass( 'form-input-tip' ) } );
302 $('#' + taxonomy + '-add-submit').click( function(){ $('#new' + taxonomy).focus(); });
304 syncChecks = function() {
308 var th = jQuery(this), c = th.is(':checked'), id = th.val().toString();
309 $('#in-' + taxonomy + '-' + id + ', #in-' + taxonomy + '-category-' + id).prop( 'checked', c );
310 noSyncChecks = false;
313 catAddBefore = function( s ) {
314 if ( !$('#new'+taxonomy).val() )
316 s.data += '&' + $( ':checked', '#'+taxonomy+'checklist' ).serialize();
317 $( '#' + taxonomy + '-add-submit' ).prop( 'disabled', true );
321 catAddAfter = function( r, s ) {
322 var sup, drop = $('#new'+taxonomy+'_parent');
324 $( '#' + taxonomy + '-add-submit' ).prop( 'disabled', false );
325 if ( 'undefined' != s.parsed.responses[0] && (sup = s.parsed.responses[0].supplemental.newcat_parent) ) {
331 $('#' + taxonomy + 'checklist').wpList({
333 response: taxonomy + '-ajax-response',
334 addBefore: catAddBefore,
335 addAfter: catAddAfter
338 $('#' + taxonomy + '-add-toggle').click( function() {
339 $('#' + taxonomy + '-adder').toggleClass( 'wp-hidden-children' );
340 $('a[href="#' + taxonomy + '-all"]', '#' + taxonomy + '-tabs').click();
341 $('#new'+taxonomy).focus();
345 $('#' + taxonomy + 'checklist li.popular-category :checkbox, #' + taxonomy + 'checklist-pop :checkbox').live( 'click', function(){
346 var t = $(this), c = t.is(':checked'), id = t.val();
347 if ( id && t.parents('#taxonomy-'+taxonomy).length )
348 $('#in-' + taxonomy + '-' + id + ', #in-popular-' + taxonomy + '-' + id).prop( 'checked', c );
354 if ( $('#postcustom').length ) {
355 $('#the-list').wpList( { addAfter: function( xml, s ) {
356 $('table#list-table').show();
357 }, addBefore: function( s ) {
358 s.data += '&post_id=' + $('#post_ID').val();
365 if ( $('#submitdiv').length ) {
366 stamp = $('#timestamp').html();
367 visibility = $('#post-visibility-display').html();
369 function updateVisibility() {
370 var pvSelect = $('#post-visibility-select');
371 if ( $('input:radio:checked', pvSelect).val() != 'public' ) {
372 $('#sticky').prop('checked', false);
373 $('#sticky-span').hide();
375 $('#sticky-span').show();
377 if ( $('input:radio:checked', pvSelect).val() != 'password' ) {
378 $('#password-span').hide();
380 $('#password-span').show();
384 function updateText() {
385 var attemptedDate, originalDate, currentDate, publishOn, postStatus = $('#post_status'),
386 optPublish = $('option[value="publish"]', postStatus), aa = $('#aa').val(),
387 mm = $('#mm').val(), jj = $('#jj').val(), hh = $('#hh').val(), mn = $('#mn').val();
389 attemptedDate = new Date( aa, mm - 1, jj, hh, mn );
390 originalDate = new Date( $('#hidden_aa').val(), $('#hidden_mm').val() -1, $('#hidden_jj').val(), $('#hidden_hh').val(), $('#hidden_mn').val() );
391 currentDate = new Date( $('#cur_aa').val(), $('#cur_mm').val() -1, $('#cur_jj').val(), $('#cur_hh').val(), $('#cur_mn').val() );
393 if ( attemptedDate.getFullYear() != aa || (1 + attemptedDate.getMonth()) != mm || attemptedDate.getDate() != jj || attemptedDate.getMinutes() != mn ) {
394 $('.timestamp-wrap', '#timestampdiv').addClass('form-invalid');
397 $('.timestamp-wrap', '#timestampdiv').removeClass('form-invalid');
400 if ( attemptedDate > currentDate && $('#original_post_status').val() != 'future' ) {
401 publishOn = postL10n.publishOnFuture;
402 $('#publish').val( postL10n.schedule );
403 } else if ( attemptedDate <= currentDate && $('#original_post_status').val() != 'publish' ) {
404 publishOn = postL10n.publishOn;
405 $('#publish').val( postL10n.publish );
407 publishOn = postL10n.publishOnPast;
408 $('#publish').val( postL10n.update );
410 if ( originalDate.toUTCString() == attemptedDate.toUTCString() ) { //hack
411 $('#timestamp').html(stamp);
413 $('#timestamp').html(
415 $('option[value="' + $('#mm').val() + '"]', '#mm').text() + ' ' +
423 if ( $('input:radio:checked', '#post-visibility-select').val() == 'private' ) {
424 $('#publish').val( postL10n.update );
425 if ( optPublish.length == 0 ) {
426 postStatus.append('<option value="publish">' + postL10n.privatelyPublished + '</option>');
428 optPublish.html( postL10n.privatelyPublished );
430 $('option[value="publish"]', postStatus).prop('selected', true);
431 $('.edit-post-status', '#misc-publishing-actions').hide();
433 if ( $('#original_post_status').val() == 'future' || $('#original_post_status').val() == 'draft' ) {
434 if ( optPublish.length ) {
436 postStatus.val($('#hidden_post_status').val());
439 optPublish.html( postL10n.published );
441 if ( postStatus.is(':hidden') )
442 $('.edit-post-status', '#misc-publishing-actions').show();
444 $('#post-status-display').html($('option:selected', postStatus).text());
445 if ( $('option:selected', postStatus).val() == 'private' || $('option:selected', postStatus).val() == 'publish' ) {
446 $('#save-post').hide();
448 $('#save-post').show();
449 if ( $('option:selected', postStatus).val() == 'pending' ) {
450 $('#save-post').show().val( postL10n.savePending );
452 $('#save-post').show().val( postL10n.saveDraft );
458 $('.edit-visibility', '#visibility').click(function () {
459 if ($('#post-visibility-select').is(":hidden")) {
461 $('#post-visibility-select').slideDown('fast');
467 $('.cancel-post-visibility', '#post-visibility-select').click(function () {
468 $('#post-visibility-select').slideUp('fast');
469 $('#visibility-radio-' + $('#hidden-post-visibility').val()).prop('checked', true);
470 $('#post_password').val($('#hidden_post_password').val());
471 $('#sticky').prop('checked', $('#hidden-post-sticky').prop('checked'));
472 $('#post-visibility-display').html(visibility);
473 $('.edit-visibility', '#visibility').show();
478 $('.save-post-visibility', '#post-visibility-select').click(function () { // crazyhorse - multiple ok cancels
479 var pvSelect = $('#post-visibility-select');
481 pvSelect.slideUp('fast');
482 $('.edit-visibility', '#visibility').show();
485 if ( $('input:radio:checked', pvSelect).val() != 'public' ) {
486 $('#sticky').prop('checked', false);
489 if ( true == $('#sticky').prop('checked') ) {
495 $('#post-visibility-display').html( postL10n[$('input:radio:checked', pvSelect).val() + sticky] );
499 $('input:radio', '#post-visibility-select').change(function() {
503 $('#timestampdiv').siblings('a.edit-timestamp').click(function() {
504 if ($('#timestampdiv').is(":hidden")) {
505 $('#timestampdiv').slideDown('fast');
511 $('.cancel-timestamp', '#timestampdiv').click(function() {
512 $('#timestampdiv').slideUp('fast');
513 $('#mm').val($('#hidden_mm').val());
514 $('#jj').val($('#hidden_jj').val());
515 $('#aa').val($('#hidden_aa').val());
516 $('#hh').val($('#hidden_hh').val());
517 $('#mn').val($('#hidden_mn').val());
518 $('#timestampdiv').siblings('a.edit-timestamp').show();
523 $('.save-timestamp', '#timestampdiv').click(function () { // crazyhorse - multiple ok cancels
524 if ( updateText() ) {
525 $('#timestampdiv').slideUp('fast');
526 $('#timestampdiv').siblings('a.edit-timestamp').show();
531 $('#post-status-select').siblings('a.edit-post-status').click(function() {
532 if ($('#post-status-select').is(":hidden")) {
533 $('#post-status-select').slideDown('fast');
539 $('.save-post-status', '#post-status-select').click(function() {
540 $('#post-status-select').slideUp('fast');
541 $('#post-status-select').siblings('a.edit-post-status').show();
546 $('.cancel-post-status', '#post-status-select').click(function() {
547 $('#post-status-select').slideUp('fast');
548 $('#post_status').val($('#hidden_post_status').val());
549 $('#post-status-select').siblings('a.edit-post-status').show();
556 if ( $('#edit-slug-box').length ) {
557 editPermalink = function(post_id) {
558 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();
560 $('#view-post-btn').hide();
561 b.html('<a href="#" class="save button">'+postL10n.ok+'</a> <a class="cancel" href="#">'+postL10n.cancel+'</a>');
562 b.children('.save').click(function() {
563 var new_slug = e.children('input').val();
564 if ( new_slug == $('#editable-post-name-full').text() ) {
565 return $('.cancel', '#edit-slug-buttons').click();
568 action: 'sample-permalink',
571 new_title: $('#title').val(),
572 samplepermalinknonce: $('#samplepermalinknonce').val()
574 $('#edit-slug-box').html(data);
576 real_slug.val(new_slug);
577 makeSlugeditClickable();
578 $('#view-post-btn').show();
583 $('.cancel', '#edit-slug-buttons').click(function() {
584 $('#view-post-btn').show();
587 real_slug.val(revert_slug);
591 for ( i = 0; i < full.length; ++i ) {
592 if ( '%' == full.charAt(i) )
596 slug_value = ( c > full.length / 4 ) ? '' : full;
597 e.html('<input type="text" id="new-post-slug" value="'+slug_value+'" />').children('input').keypress(function(e){
598 var key = e.keyCode || 0;
599 // on enter, just save the new slug, don't save the post
601 b.children('.save').click();
605 b.children('.cancel').click();
608 real_slug.val(this.value);
612 makeSlugeditClickable = function() {
613 $('#editable-post-name').click(function() {
614 $('#edit-slug-buttons').children('.edit-slug').click();
617 makeSlugeditClickable();
621 if ( typeof(wpWordCount) != 'undefined' ) {
622 $(document).triggerHandler('wpcountwords', [ co.val() ]);
624 co.keyup( function(e) {
625 var k = e.keyCode || e.charCode;
630 if ( 13 == k || 8 == last || 46 == last )
631 $(document).triggerHandler('wpcountwords', [ co.val() ]);
638 wptitlehint = function(id) {
641 var title = $('#' + id), titleprompt = $('#' + id + '-prompt-text');
643 if ( title.val() == '' )
644 titleprompt.css('visibility', '');
646 titleprompt.click(function(){
647 $(this).css('visibility', 'hidden');
651 title.blur(function(){
652 if ( this.value == '' )
653 titleprompt.css('visibility', '');
655 titleprompt.css('visibility', 'hidden');
656 }).keydown(function(e){
657 titleprompt.css('visibility', 'hidden');