1 var tagBox, commentsBox, editPermalink, makeSlugeditClickable, WPSetThumbnailHTML, WPSetThumbnailID, WPRemoveThumbnail;
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 return tags.replace(/\s*,\s*/g, ',').replace(/,+/g, ',').replace(/[,\s]+$/, '').replace(/^[,\s]+/, '');
21 parseTags : function(el) {
22 var id = el.id, num = id.split('-check-num-')[1], taxbox = $(el).closest('.tagsdiv'), thetags = taxbox.find('.the-tags'), current_tags = thetags.val().split(','), new_tags = [];
23 delete current_tags[num];
25 $.each( current_tags, function(key, val) {
32 thetags.val( this.clean( new_tags.join(',') ) );
34 this.quickClicks(taxbox);
38 quickClicks : function(el) {
39 var thetags = $('.the-tags', el),
40 tagchecklist = $('.tagchecklist', el),
41 id = $(el).attr('id'),
42 current_tags, disabled;
44 if ( !thetags.length )
47 disabled = thetags.attr('disabled');
49 current_tags = thetags.val().split(',');
52 $.each( current_tags, function( key, val ) {
60 // Create a new span, and ensure the text is properly escaped.
61 span = $('<span />').text( val );
63 // If tags editing isn't disabled, create the X button.
65 xbutton = $( '<a id="' + id + '-check-num-' + key + '" class="ntdelbutton">X</a>' );
66 xbutton.click( function(){ tagBox.parseTags(this); });
67 span.prepend(' ').prepend( xbutton );
70 // Append the span to the tag list.
71 tagchecklist.append( span );
75 flushTags : function(el, a, f) {
77 var text, tags = $('.the-tags', el), newtag = $('input.newtag', el), newtags;
79 text = a ? $(a).text() : newtag.val();
81 newtags = tagsval ? tagsval + ',' + text : text;
83 newtags = this.clean( newtags );
84 newtags = array_unique_noempty( newtags.split(',') ).join(',');
90 if ( 'undefined' == typeof(f) )
97 var tax = id.substr(id.indexOf('-')+1);
99 $.post(ajaxurl, {'action':'get-tagcloud','tax':tax}, function(r, stat) {
100 if ( 0 == r || 'success' != stat )
103 r = $('<p id="tagcloud-'+tax+'" class="the-tagcloud">'+r+'</p>');
104 $('a', r).click(function(){
105 tagBox.flushTags( $(this).closest('.inside').children('.tagsdiv'), this);
114 var t = this, ajaxtag = $('div.ajaxtag');
116 $('.tagsdiv').each( function() {
117 tagBox.quickClicks(this);
120 $('input.tagadd', ajaxtag).click(function(){
121 t.flushTags( $(this).closest('.tagsdiv') );
124 $('div.taghint', ajaxtag).click(function(){
125 $(this).css('visibility', 'hidden').parent().siblings('.newtag').focus();
128 $('input.newtag', ajaxtag).blur(function() {
129 if ( this.value == '' )
130 $(this).parent().siblings('.taghint').css('visibility', '');
132 $(this).parent().siblings('.taghint').css('visibility', 'hidden');
133 }).keyup(function(e){
134 if ( 13 == e.which ) {
135 tagBox.flushTags( $(this).closest('.tagsdiv') );
138 }).keypress(function(e){
139 if ( 13 == e.which ) {
144 var tax = $(this).closest('div.tagsdiv').attr('id');
145 $(this).suggest( ajaxurl + '?action=ajax-tag-search&tax=' + tax, { delay: 500, minchars: 2, multiple: true, multipleSep: "," } );
148 // save tags on post save/publish
149 $('#post').submit(function(){
150 $('div.tagsdiv').each( function() {
151 tagBox.flushTags(this, false, 1);
156 $('a.tagcloud-link').click(function(){
157 tagBox.get( $(this).attr('id') );
158 $(this).unbind().click(function(){
159 $(this).siblings('.the-tagcloud').toggle();
170 get : function(total, num) {
171 var st = this.st, data;
177 $('#commentsdiv img.waiting').show();
180 'action' : 'get-comments',
182 '_ajax_nonce' : $('#add_comment_nonce').val(),
183 'post_ID' : $('#post_ID').val(),
188 $.post(ajaxurl, data,
190 r = wpAjax.parseAjaxResponse(r);
191 $('#commentsdiv .widefat').show();
192 $('#commentsdiv img.waiting').hide();
194 if ( 'object' == typeof r && r.responses[0] ) {
195 $('#the-comment-list').append( r.responses[0].data );
197 theList = theExtraList = null;
198 $("a[className*=':']").unbind();
201 if ( commentsBox.st > commentsBox.total )
202 $('#show-comments').hide();
204 $('#show-comments').html(postL10n.showcomm);
206 } else if ( 1 == r ) {
207 $('#show-comments').parent().html(postL10n.endcomm);
211 $('#the-comment-list').append('<tr><td colspan="2">'+wpAjax.broken+'</td></tr>');
219 WPSetThumbnailHTML = function(html){
220 $('.inside', '#postimagediv').html(html);
223 WPSetThumbnailID = function(id){
224 var field = $('input[value=_thumbnail_id]', '#list-table');
225 if ( field.size() > 0 ) {
226 $('#meta\\[' + field.attr('id').match(/[0-9]+/) + '\\]\\[value\\]').text(id);
230 WPRemoveThumbnail = function(nonce){
232 action:"set-post-thumbnail", post_id: $('#post_ID').val(), thumbnail_id: -1, _ajax_nonce: nonce, cookie: encodeURIComponent(document.cookie)
235 alert( setPostThumbnailL10n.error );
237 WPSetThumbnailHTML(str);
245 jQuery(document).ready( function($) {
246 var stamp, visibility, sticky = '';
248 postboxes.add_postbox_toggles(pagenow);
251 if ( $('#tagsdiv-post_tag').length ) {
254 $('#side-sortables, #normal-sortables, #advanced-sortables').children('div.postbox').each(function(){
255 if ( this.id.indexOf('tagsdiv-') === 0 ) {
263 $('.categorydiv').each( function(){
264 var this_id = $(this).attr('id'), noSyncChecks = false, syncChecks, catAddAfter, taxonomyParts, taxonomy, settingName;
266 taxonomyParts = this_id.split('-');
267 taxonomyParts.shift();
268 taxonomy = taxonomyParts.join('-');
269 settingName = taxonomy + '_tab';
270 if ( taxonomy == 'category' )
271 settingName = 'cats';
273 // TODO: move to jQuery 1.3+, support for multiple hierarchical taxonomies, see wp-lists.dev.js
274 $('a', '#' + taxonomy + '-tabs').click( function(){
275 var t = $(this).attr('href');
276 $(this).parent().addClass('tabs').siblings('li').removeClass('tabs');
277 $('#' + taxonomy + '-tabs').siblings('.tabs-panel').hide();
279 if ( '#' + taxonomy + '-all' == t )
280 deleteUserSetting(settingName);
282 setUserSetting(settingName, 'pop');
286 if ( getUserSetting(settingName) )
287 $('a[href="#' + taxonomy + '-pop"]', '#' + taxonomy + '-tabs').click();
290 $('#new' + taxonomy).one( 'focus', function() { $(this).val( '' ).removeClass( 'form-input-tip' ) } );
291 $('#' + taxonomy + '-add-submit').click( function(){ $('#new' + taxonomy).focus(); });
293 syncChecks = function() {
297 var th = jQuery(this), c = th.is(':checked'), id = th.val().toString();
298 $('#in-' + taxonomy + '-' + id + ', #in-' + taxonomy + '-category-' + id).attr( 'checked', c );
299 noSyncChecks = false;
302 catAddBefore = function( s ) {
303 if ( !$('#new'+taxonomy).val() )
305 s.data += '&' + $( ':checked', '#'+taxonomy+'checklist' ).serialize();
309 catAddAfter = function( r, s ) {
310 var sup, drop = $('#new'+taxonomy+'_parent');
312 if ( 'undefined' != s.parsed.responses[0] && (sup = s.parsed.responses[0].supplemental.newcat_parent) ) {
318 $('#' + taxonomy + 'checklist').wpList({
320 response: taxonomy + '-ajax-response',
321 addBefore: catAddBefore,
322 addAfter: catAddAfter
325 $('#' + taxonomy + '-add-toggle').click( function() {
326 $('#' + taxonomy + '-adder').toggleClass( 'wp-hidden-children' );
327 $('a[href="#' + taxonomy + '-all"]', '#' + taxonomy + '-tabs').click();
328 $('#new'+taxonomy).focus();
332 $('#' + taxonomy + 'checklist li.popular-category :checkbox, #' + taxonomy + 'checklist-pop :checkbox').live( 'click', function(){
333 var t = $(this), c = t.is(':checked'), id = t.val();
334 if ( id && t.parents('#taxonomy-'+taxonomy).length )
335 $('#in-' + taxonomy + '-' + id + ', #in-popular-' + taxonomy + '-' + id).attr( 'checked', c );
341 if ( $('#postcustom').length ) {
342 $('#the-list').wpList( { addAfter: function( xml, s ) {
343 $('table#list-table').show();
344 }, addBefore: function( s ) {
345 s.data += '&post_id=' + $('#post_ID').val();
352 if ( $('#submitdiv').length ) {
353 stamp = $('#timestamp').html();
354 visibility = $('#post-visibility-display').html();
356 function updateVisibility() {
357 var pvSelect = $('#post-visibility-select');
358 if ( $('input:radio:checked', pvSelect).val() != 'public' ) {
359 $('#sticky').attr('checked', false);
360 $('#sticky-span').hide();
362 $('#sticky-span').show();
364 if ( $('input:radio:checked', pvSelect).val() != 'password' ) {
365 $('#password-span').hide();
367 $('#password-span').show();
371 function updateText() {
372 var attemptedDate, originalDate, currentDate, publishOn, page = 'page' == pagenow || 'page-new' == pagenow,
373 postStatus = $('#post_status'), optPublish = $('option[value=publish]', postStatus), aa = $('#aa').val(),
374 mm = $('#mm').val(), jj = $('#jj').val(), hh = $('#hh').val(), mn = $('#mn').val();
376 attemptedDate = new Date( aa, mm - 1, jj, hh, mn );
377 originalDate = new Date( $('#hidden_aa').val(), $('#hidden_mm').val() -1, $('#hidden_jj').val(), $('#hidden_hh').val(), $('#hidden_mn').val() );
378 currentDate = new Date( $('#cur_aa').val(), $('#cur_mm').val() -1, $('#cur_jj').val(), $('#cur_hh').val(), $('#cur_mn').val() );
380 if ( attemptedDate.getFullYear() != aa || (1 + attemptedDate.getMonth()) != mm || attemptedDate.getDate() != jj || attemptedDate.getMinutes() != mn ) {
381 $('.timestamp-wrap', '#timestampdiv').addClass('form-invalid');
384 $('.timestamp-wrap', '#timestampdiv').removeClass('form-invalid');
387 if ( attemptedDate > currentDate && $('#original_post_status').val() != 'future' ) {
388 publishOn = postL10n.publishOnFuture;
389 $('#publish').val( postL10n.schedule );
390 } else if ( attemptedDate <= currentDate && $('#original_post_status').val() != 'publish' ) {
391 publishOn = postL10n.publishOn;
392 $('#publish').val( postL10n.publish );
394 publishOn = postL10n.publishOnPast;
396 $('#publish').val( postL10n.updatePage );
398 $('#publish').val( postL10n.updatePost );
400 if ( originalDate.toUTCString() == attemptedDate.toUTCString() ) { //hack
401 $('#timestamp').html(stamp);
403 $('#timestamp').html(
405 $('option[value=' + $('#mm').val() + ']', '#mm').text() + ' ' +
413 if ( $('input:radio:checked', '#post-visibility-select').val() == 'private' ) {
415 $('#publish').val( postL10n.updatePage );
417 $('#publish').val( postL10n.updatePost );
418 if ( optPublish.length == 0 ) {
419 postStatus.append('<option value="publish">' + postL10n.privatelyPublished + '</option>');
421 optPublish.html( postL10n.privatelyPublished );
423 $('option[value=publish]', postStatus).attr('selected', true);
424 $('.edit-post-status', '#misc-publishing-actions').hide();
426 if ( $('#original_post_status').val() == 'future' || $('#original_post_status').val() == 'draft' ) {
427 if ( optPublish.length ) {
429 postStatus.val($('#hidden_post_status').val());
432 optPublish.html( postL10n.published );
434 if ( postStatus.is(':hidden') )
435 $('.edit-post-status', '#misc-publishing-actions').show();
437 $('#post-status-display').html($('option:selected', postStatus).text());
438 if ( $('option:selected', postStatus).val() == 'private' || $('option:selected', postStatus).val() == 'publish' ) {
439 $('#save-post').hide();
441 $('#save-post').show();
442 if ( $('option:selected', postStatus).val() == 'pending' ) {
443 $('#save-post').show().val( postL10n.savePending );
445 $('#save-post').show().val( postL10n.saveDraft );
451 $('.edit-visibility', '#visibility').click(function () {
452 if ($('#post-visibility-select').is(":hidden")) {
454 $('#post-visibility-select').slideDown("normal");
460 $('.cancel-post-visibility', '#post-visibility-select').click(function () {
461 $('#post-visibility-select').slideUp("normal");
462 $('#visibility-radio-' + $('#hidden-post-visibility').val()).attr('checked', true);
463 $('#post_password').val($('#hidden_post_password').val());
464 $('#sticky').attr('checked', $('#hidden-post-sticky').attr('checked'));
465 $('#post-visibility-display').html(visibility);
466 $('.edit-visibility', '#visibility').show();
471 $('.save-post-visibility', '#post-visibility-select').click(function () { // crazyhorse - multiple ok cancels
472 var pvSelect = $('#post-visibility-select');
474 pvSelect.slideUp("normal");
475 $('.edit-visibility', '#visibility').show();
478 if ( $('input:radio:checked', pvSelect).val() != 'public' ) {
479 $('#sticky').attr('checked', false);
482 if ( true == $('#sticky').attr('checked') ) {
488 $('#post-visibility-display').html( postL10n[$('input:radio:checked', pvSelect).val() + sticky] );
492 $('input:radio', '#post-visibility-select').change(function() {
496 $('#timestampdiv').siblings('a.edit-timestamp').click(function() {
497 if ($('#timestampdiv').is(":hidden")) {
498 $('#timestampdiv').slideDown("normal");
504 $('.cancel-timestamp', '#timestampdiv').click(function() {
505 $('#timestampdiv').slideUp("normal");
506 $('#mm').val($('#hidden_mm').val());
507 $('#jj').val($('#hidden_jj').val());
508 $('#aa').val($('#hidden_aa').val());
509 $('#hh').val($('#hidden_hh').val());
510 $('#mn').val($('#hidden_mn').val());
511 $('#timestampdiv').siblings('a.edit-timestamp').show();
516 $('.save-timestamp', '#timestampdiv').click(function () { // crazyhorse - multiple ok cancels
517 if ( updateText() ) {
518 $('#timestampdiv').slideUp("normal");
519 $('#timestampdiv').siblings('a.edit-timestamp').show();
524 $('#post-status-select').siblings('a.edit-post-status').click(function() {
525 if ($('#post-status-select').is(":hidden")) {
526 $('#post-status-select').slideDown("normal");
532 $('.save-post-status', '#post-status-select').click(function() {
533 $('#post-status-select').slideUp("normal");
534 $('#post-status-select').siblings('a.edit-post-status').show();
539 $('.cancel-post-status', '#post-status-select').click(function() {
540 $('#post-status-select').slideUp("normal");
541 $('#post_status').val($('#hidden_post_status').val());
542 $('#post-status-select').siblings('a.edit-post-status').show();
549 if ( $('#edit-slug-box').length ) {
550 editPermalink = function(post_id) {
551 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();
553 $('#view-post-btn').hide();
554 b.html('<a href="#" class="save button">'+postL10n.ok+'</a> <a class="cancel" href="#">'+postL10n.cancel+'</a>');
555 b.children('.save').click(function() {
556 var new_slug = e.children('input').val();
558 action: 'sample-permalink',
561 new_title: $('#title').val(),
562 samplepermalinknonce: $('#samplepermalinknonce').val()
564 $('#edit-slug-box').html(data);
566 real_slug.attr('value', new_slug);
567 makeSlugeditClickable();
568 $('#view-post-btn').show();
573 $('.cancel', '#edit-slug-buttons').click(function() {
574 $('#view-post-btn').show();
577 real_slug.attr('value', revert_slug);
581 for ( i = 0; i < full.length; ++i ) {
582 if ( '%' == full.charAt(i) )
586 slug_value = ( c > full.length / 4 ) ? '' : full;
587 e.html('<input type="text" id="new-post-slug" value="'+slug_value+'" />').children('input').keypress(function(e){
588 var key = e.keyCode || 0;
589 // on enter, just save the new slug, don't save the post
591 b.children('.save').click();
595 b.children('.cancel').click();
598 real_slug.attr('value', this.value);
602 makeSlugeditClickable = function() {
603 $('#editable-post-name').click(function() {
604 $('#edit-slug-buttons').children('.edit-slug').click();
607 makeSlugeditClickable();
610 if ( $('#title').val() == '' )
611 $('#title').siblings('#title-prompt-text').css('visibility', '');
612 $('#title-prompt-text').click(function(){
613 $(this).css('visibility', 'hidden').siblings('#title').focus();
615 $('#title').blur(function(){
616 if (this.value == '')
617 $(this).siblings('#title-prompt-text').css('visibility', '');
619 $(this).siblings('#title-prompt-text').css('visibility', 'hidden');
620 }).keydown(function(e){
621 $(this).siblings('#title-prompt-text').css('visibility', 'hidden');