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), tagchecklist = $('.tagchecklist', el), current_tags;
41 if ( !thetags.length )
44 var disabled = thetags.attr('disabled');
46 current_tags = thetags.val().split(',');
49 $.each( current_tags, function( key, val ) {
50 var txt, button_id, id = $(el).attr('id');
53 if ( !val.match(/^\s+$/) && '' != val ) {
54 button_id = id + '-check-num-' + key;
56 txt = '<span>' + val + '</span> ';
58 txt = '<span><a id="' + button_id + '" class="ntdelbutton">X</a> ' + val + '</span> ';
59 tagchecklist.append(txt);
61 $( '#' + button_id ).click( function(){ tagBox.parseTags(this); });
66 flushTags : function(el, a, f) {
68 var text, tags = $('.the-tags', el), newtag = $('input.newtag', el), newtags;
70 text = a ? $(a).text() : newtag.val();
72 newtags = tagsval ? tagsval + ',' + text : text;
74 newtags = this.clean( newtags );
75 newtags = array_unique_noempty( newtags.split(',') ).join(',');
81 if ( 'undefined' == typeof(f) )
88 var tax = id.substr(id.indexOf('-')+1);
90 $.post(ajaxurl, {'action':'get-tagcloud','tax':tax}, function(r, stat) {
91 if ( 0 == r || 'success' != stat )
94 r = $('<p id="tagcloud-'+tax+'" class="the-tagcloud">'+r+'</p>');
95 $('a', r).click(function(){
96 tagBox.flushTags( $(this).closest('.inside').children('.tagsdiv'), this);
105 var t = this, ajaxtag = $('div.ajaxtag');
107 $('.tagsdiv').each( function() {
108 tagBox.quickClicks(this);
111 $('input.tagadd', ajaxtag).click(function(){
112 t.flushTags( $(this).closest('.tagsdiv') );
115 $('div.taghint', ajaxtag).click(function(){
116 $(this).css('visibility', 'hidden').parent().siblings('.newtag').focus();
119 $('input.newtag', ajaxtag).blur(function() {
120 if ( this.value == '' )
121 $(this).parent().siblings('.taghint').css('visibility', '');
123 $(this).parent().siblings('.taghint').css('visibility', 'hidden');
124 }).keyup(function(e){
125 if ( 13 == e.which ) {
126 tagBox.flushTags( $(this).closest('.tagsdiv') );
129 }).keypress(function(e){
130 if ( 13 == e.which ) {
135 var tax = $(this).closest('div.tagsdiv').attr('id');
136 $(this).suggest( ajaxurl + '?action=ajax-tag-search&tax=' + tax, { delay: 500, minchars: 2, multiple: true, multipleSep: "," } );
139 // save tags on post save/publish
140 $('#post').submit(function(){
141 $('div.tagsdiv').each( function() {
142 tagBox.flushTags(this, false, 1);
147 $('a.tagcloud-link').click(function(){
148 tagBox.get( $(this).attr('id') );
149 $(this).unbind().click(function(){
150 $(this).siblings('.the-tagcloud').toggle();
161 get : function(total, num) {
162 var st = this.st, data;
168 $('#commentsdiv img.waiting').show();
171 'action' : 'get-comments',
173 '_ajax_nonce' : $('#add_comment_nonce').val(),
174 'post_ID' : $('#post_ID').val(),
179 $.post(ajaxurl, data,
181 r = wpAjax.parseAjaxResponse(r);
182 $('#commentsdiv .widefat').show();
183 $('#commentsdiv img.waiting').hide();
185 if ( 'object' == typeof r && r.responses[0] ) {
186 $('#the-comment-list').append( r.responses[0].data );
188 theList = theExtraList = null;
189 $("a[className*=':']").unbind();
192 if ( commentsBox.st > commentsBox.total )
193 $('#show-comments').hide();
195 $('#show-comments').html(postL10n.showcomm);
197 } else if ( 1 == r ) {
198 $('#show-comments').parent().html(postL10n.endcomm);
202 $('#the-comment-list').append('<tr><td colspan="2">'+wpAjax.broken+'</td></tr>');
210 WPSetThumbnailHTML = function(html){
211 $('.inside', '#postimagediv').html(html);
214 WPSetThumbnailID = function(id){
215 var field = $('input[value=_thumbnail_id]', '#list-table');
216 if ( field.size() > 0 ) {
217 $('#meta\\[' + field.attr('id').match(/[0-9]+/) + '\\]\\[value\\]').text(id);
221 WPRemoveThumbnail = function(nonce){
223 action:"set-post-thumbnail", post_id: $('#post_ID').val(), thumbnail_id: -1, _ajax_nonce: nonce, cookie: encodeURIComponent(document.cookie)
226 alert( setPostThumbnailL10n.error );
228 WPSetThumbnailHTML(str);
236 jQuery(document).ready( function($) {
237 var stamp, visibility, sticky = '';
239 postboxes.add_postbox_toggles(pagenow);
242 if ( $('#tagsdiv-post_tag').length ) {
245 $('#side-sortables, #normal-sortables, #advanced-sortables').children('div.postbox').each(function(){
246 if ( this.id.indexOf('tagsdiv-') === 0 ) {
254 $('.categorydiv').each( function(){
255 var this_id = $(this).attr('id'), noSyncChecks = false, syncChecks, catAddAfter, taxonomyParts, taxonomy, settingName;
257 taxonomyParts = this_id.split('-');
258 taxonomyParts.shift();
259 taxonomy = taxonomyParts.join('-');
260 settingName = taxonomy + '_tab';
261 if ( taxonomy == 'category' )
262 settingName = 'cats';
264 // TODO: move to jQuery 1.3+, support for multiple hierarchical taxonomies, see wp-lists.dev.js
265 $('a', '#' + taxonomy + '-tabs').click( function(){
266 var t = $(this).attr('href');
267 $(this).parent().addClass('tabs').siblings('li').removeClass('tabs');
268 $('#' + taxonomy + '-tabs').siblings('.tabs-panel').hide();
270 if ( '#' + taxonomy + '-all' == t )
271 deleteUserSetting(settingName);
273 setUserSetting(settingName, 'pop');
277 if ( getUserSetting(settingName) )
278 $('a[href="#' + taxonomy + '-pop"]', '#' + taxonomy + '-tabs').click();
281 $('#new' + taxonomy).one( 'focus', function() { $(this).val( '' ).removeClass( 'form-input-tip' ) } );
282 $('#' + taxonomy + '-add-submit').click( function(){ $('#new' + taxonomy).focus(); });
284 syncChecks = function() {
288 var th = jQuery(this), c = th.is(':checked'), id = th.val().toString();
289 $('#in-' + taxonomy + '-' + id + ', #in-' + taxonomy + '-category-' + id).attr( 'checked', c );
290 noSyncChecks = false;
293 catAddBefore = function( s ) {
294 if ( !$('#new'+taxonomy).val() )
296 s.data += '&' + $( ':checked', '#'+taxonomy+'checklist' ).serialize();
300 catAddAfter = function( r, s ) {
301 var sup, drop = $('#new'+taxonomy+'_parent');
303 if ( 'undefined' != s.parsed.responses[0] && (sup = s.parsed.responses[0].supplemental.newcat_parent) ) {
309 $('#' + taxonomy + 'checklist').wpList({
311 response: taxonomy + '-ajax-response',
312 addBefore: catAddBefore,
313 addAfter: catAddAfter
316 $('#' + taxonomy + '-add-toggle').click( function() {
317 $('#' + taxonomy + '-adder').toggleClass( 'wp-hidden-children' );
318 $('a[href="#' + taxonomy + '-all"]', '#' + taxonomy + '-tabs').click();
319 $('#new'+taxonomy).focus();
323 $('#' + taxonomy + 'checklist li.popular-category :checkbox, #' + taxonomy + 'checklist-pop :checkbox').live( 'click', function(){
324 var t = $(this), c = t.is(':checked'), id = t.val();
325 if ( id && t.parents('#taxonomy-'+taxonomy).length )
326 $('#in-' + taxonomy + '-' + id + ', #in-popular-' + taxonomy + '-' + id).attr( 'checked', c );
332 if ( $('#postcustom').length ) {
333 $('#the-list').wpList( { addAfter: function( xml, s ) {
334 $('table#list-table').show();
335 }, addBefore: function( s ) {
336 s.data += '&post_id=' + $('#post_ID').val();
343 if ( $('#submitdiv').length ) {
344 stamp = $('#timestamp').html();
345 visibility = $('#post-visibility-display').html();
347 function updateVisibility() {
348 var pvSelect = $('#post-visibility-select');
349 if ( $('input:radio:checked', pvSelect).val() != 'public' ) {
350 $('#sticky').attr('checked', false);
351 $('#sticky-span').hide();
353 $('#sticky-span').show();
355 if ( $('input:radio:checked', pvSelect).val() != 'password' ) {
356 $('#password-span').hide();
358 $('#password-span').show();
362 function updateText() {
363 var attemptedDate, originalDate, currentDate, publishOn, page = 'page' == pagenow || 'page-new' == pagenow,
364 postStatus = $('#post_status'), optPublish = $('option[value=publish]', postStatus), aa = $('#aa').val(),
365 mm = $('#mm').val(), jj = $('#jj').val(), hh = $('#hh').val(), mn = $('#mn').val();
367 attemptedDate = new Date( aa, mm - 1, jj, hh, mn );
368 originalDate = new Date( $('#hidden_aa').val(), $('#hidden_mm').val() -1, $('#hidden_jj').val(), $('#hidden_hh').val(), $('#hidden_mn').val() );
369 currentDate = new Date( $('#cur_aa').val(), $('#cur_mm').val() -1, $('#cur_jj').val(), $('#cur_hh').val(), $('#cur_mn').val() );
371 if ( attemptedDate.getFullYear() != aa || (1 + attemptedDate.getMonth()) != mm || attemptedDate.getDate() != jj || attemptedDate.getMinutes() != mn ) {
372 $('.timestamp-wrap', '#timestampdiv').addClass('form-invalid');
375 $('.timestamp-wrap', '#timestampdiv').removeClass('form-invalid');
378 if ( attemptedDate > currentDate && $('#original_post_status').val() != 'future' ) {
379 publishOn = postL10n.publishOnFuture;
380 $('#publish').val( postL10n.schedule );
381 } else if ( attemptedDate <= currentDate && $('#original_post_status').val() != 'publish' ) {
382 publishOn = postL10n.publishOn;
383 $('#publish').val( postL10n.publish );
385 publishOn = postL10n.publishOnPast;
387 $('#publish').val( postL10n.updatePage );
389 $('#publish').val( postL10n.updatePost );
391 if ( originalDate.toUTCString() == attemptedDate.toUTCString() ) { //hack
392 $('#timestamp').html(stamp);
394 $('#timestamp').html(
396 $('option[value=' + $('#mm').val() + ']', '#mm').text() + ' ' +
404 if ( $('input:radio:checked', '#post-visibility-select').val() == 'private' ) {
406 $('#publish').val( postL10n.updatePage );
408 $('#publish').val( postL10n.updatePost );
409 if ( optPublish.length == 0 ) {
410 postStatus.append('<option value="publish">' + postL10n.privatelyPublished + '</option>');
412 optPublish.html( postL10n.privatelyPublished );
414 $('option[value=publish]', postStatus).attr('selected', true);
415 $('.edit-post-status', '#misc-publishing-actions').hide();
417 if ( $('#original_post_status').val() == 'future' || $('#original_post_status').val() == 'draft' ) {
418 if ( optPublish.length ) {
420 postStatus.val($('#hidden_post_status').val());
423 optPublish.html( postL10n.published );
425 if ( postStatus.is(':hidden') )
426 $('.edit-post-status', '#misc-publishing-actions').show();
428 $('#post-status-display').html($('option:selected', postStatus).text());
429 if ( $('option:selected', postStatus).val() == 'private' || $('option:selected', postStatus).val() == 'publish' ) {
430 $('#save-post').hide();
432 $('#save-post').show();
433 if ( $('option:selected', postStatus).val() == 'pending' ) {
434 $('#save-post').show().val( postL10n.savePending );
436 $('#save-post').show().val( postL10n.saveDraft );
442 $('.edit-visibility', '#visibility').click(function () {
443 if ($('#post-visibility-select').is(":hidden")) {
445 $('#post-visibility-select').slideDown("normal");
451 $('.cancel-post-visibility', '#post-visibility-select').click(function () {
452 $('#post-visibility-select').slideUp("normal");
453 $('#visibility-radio-' + $('#hidden-post-visibility').val()).attr('checked', true);
454 $('#post_password').val($('#hidden_post_password').val());
455 $('#sticky').attr('checked', $('#hidden-post-sticky').attr('checked'));
456 $('#post-visibility-display').html(visibility);
457 $('.edit-visibility', '#visibility').show();
462 $('.save-post-visibility', '#post-visibility-select').click(function () { // crazyhorse - multiple ok cancels
463 var pvSelect = $('#post-visibility-select');
465 pvSelect.slideUp("normal");
466 $('.edit-visibility', '#visibility').show();
469 if ( $('input:radio:checked', pvSelect).val() != 'public' ) {
470 $('#sticky').attr('checked', false);
473 if ( true == $('#sticky').attr('checked') ) {
479 $('#post-visibility-display').html( postL10n[$('input:radio:checked', pvSelect).val() + sticky] );
483 $('input:radio', '#post-visibility-select').change(function() {
487 $('#timestampdiv').siblings('a.edit-timestamp').click(function() {
488 if ($('#timestampdiv').is(":hidden")) {
489 $('#timestampdiv').slideDown("normal");
495 $('.cancel-timestamp', '#timestampdiv').click(function() {
496 $('#timestampdiv').slideUp("normal");
497 $('#mm').val($('#hidden_mm').val());
498 $('#jj').val($('#hidden_jj').val());
499 $('#aa').val($('#hidden_aa').val());
500 $('#hh').val($('#hidden_hh').val());
501 $('#mn').val($('#hidden_mn').val());
502 $('#timestampdiv').siblings('a.edit-timestamp').show();
507 $('.save-timestamp', '#timestampdiv').click(function () { // crazyhorse - multiple ok cancels
508 if ( updateText() ) {
509 $('#timestampdiv').slideUp("normal");
510 $('#timestampdiv').siblings('a.edit-timestamp').show();
515 $('#post-status-select').siblings('a.edit-post-status').click(function() {
516 if ($('#post-status-select').is(":hidden")) {
517 $('#post-status-select').slideDown("normal");
523 $('.save-post-status', '#post-status-select').click(function() {
524 $('#post-status-select').slideUp("normal");
525 $('#post-status-select').siblings('a.edit-post-status').show();
530 $('.cancel-post-status', '#post-status-select').click(function() {
531 $('#post-status-select').slideUp("normal");
532 $('#post_status').val($('#hidden_post_status').val());
533 $('#post-status-select').siblings('a.edit-post-status').show();
540 if ( $('#edit-slug-box').length ) {
541 editPermalink = function(post_id) {
542 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();
544 $('#view-post-btn').hide();
545 b.html('<a href="#" class="save button">'+postL10n.ok+'</a> <a class="cancel" href="#">'+postL10n.cancel+'</a>');
546 b.children('.save').click(function() {
547 var new_slug = e.children('input').val();
549 action: 'sample-permalink',
552 new_title: $('#title').val(),
553 samplepermalinknonce: $('#samplepermalinknonce').val()
555 $('#edit-slug-box').html(data);
557 real_slug.attr('value', new_slug);
558 makeSlugeditClickable();
559 $('#view-post-btn').show();
564 $('.cancel', '#edit-slug-buttons').click(function() {
565 $('#view-post-btn').show();
568 real_slug.attr('value', revert_slug);
572 for ( i = 0; i < full.length; ++i ) {
573 if ( '%' == full.charAt(i) )
577 slug_value = ( c > full.length / 4 ) ? '' : full;
578 e.html('<input type="text" id="new-post-slug" value="'+slug_value+'" />').children('input').keypress(function(e){
579 var key = e.keyCode || 0;
580 // on enter, just save the new slug, don't save the post
582 b.children('.save').click();
586 b.children('.cancel').click();
589 real_slug.attr('value', this.value);
593 makeSlugeditClickable = function() {
594 $('#editable-post-name').click(function() {
595 $('#edit-slug-buttons').children('.edit-slug').click();
598 makeSlugeditClickable();
601 if ( $('#title').val() == '' )
602 $('#title').siblings('#title-prompt-text').css('visibility', '');
603 $('#title-prompt-text').click(function(){
604 $(this).css('visibility', 'hidden').siblings('#title').focus();
606 $('#title').blur(function(){
607 if (this.value == '')
608 $(this).siblings('#title-prompt-text').css('visibility', '');
610 $(this).siblings('#title-prompt-text').css('visibility', 'hidden');
611 }).keydown(function(e){
612 $(this).siblings('#title-prompt-text').css('visibility', 'hidden');