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 'p' : $('#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();
200 if ( commentsBox.st > commentsBox.total )
201 $('#show-comments').hide();
203 $('#show-comments').html(postL10n.showcomm);
205 } else if ( 1 == r ) {
206 $('#show-comments').parent().html(postL10n.endcomm);
210 $('#the-comment-list').append('<tr><td colspan="2">'+wpAjax.broken+'</td></tr>');
218 WPSetThumbnailHTML = function(html){
219 $('.inside', '#postimagediv').html(html);
222 WPSetThumbnailID = function(id){
223 var field = $('input[value=_thumbnail_id]', '#list-table');
224 if ( field.size() > 0 ) {
225 $('#meta\\[' + field.attr('id').match(/[0-9]+/) + '\\]\\[value\\]').text(id);
229 WPRemoveThumbnail = function(nonce){
231 action:"set-post-thumbnail", post_id: $('#post_ID').val(), thumbnail_id: -1, _ajax_nonce: nonce, cookie: encodeURIComponent(document.cookie)
234 alert( setPostThumbnailL10n.error );
236 WPSetThumbnailHTML(str);
244 jQuery(document).ready( function($) {
245 var stamp, visibility, sticky = '';
247 postboxes.add_postbox_toggles(pagenow);
250 if ( $('#tagsdiv-post_tag').length ) {
253 $('#side-sortables, #normal-sortables, #advanced-sortables').children('div.postbox').each(function(){
254 if ( this.id.indexOf('tagsdiv-') === 0 ) {
262 $('.categorydiv').each( function(){
263 var this_id = $(this).attr('id'), noSyncChecks = false, syncChecks, catAddAfter, taxonomyParts, taxonomy, settingName;
265 taxonomyParts = this_id.split('-');
266 taxonomyParts.shift();
267 taxonomy = taxonomyParts.join('-');
268 settingName = taxonomy + '_tab';
269 if ( taxonomy == 'category' )
270 settingName = 'cats';
272 // TODO: move to jQuery 1.3+, support for multiple hierarchical taxonomies, see wp-lists.dev.js
273 $('a', '#' + taxonomy + '-tabs').click( function(){
274 var t = $(this).attr('href');
275 $(this).parent().addClass('tabs').siblings('li').removeClass('tabs');
276 $('#' + taxonomy + '-tabs').siblings('.tabs-panel').hide();
278 if ( '#' + taxonomy + '-all' == t )
279 deleteUserSetting(settingName);
281 setUserSetting(settingName, 'pop');
285 if ( getUserSetting(settingName) )
286 $('a[href="#' + taxonomy + '-pop"]', '#' + taxonomy + '-tabs').click();
289 $('#new' + taxonomy).one( 'focus', function() { $(this).val( '' ).removeClass( 'form-input-tip' ) } );
290 $('#' + taxonomy + '-add-submit').click( function(){ $('#new' + taxonomy).focus(); });
292 syncChecks = function() {
296 var th = jQuery(this), c = th.is(':checked'), id = th.val().toString();
297 $('#in-' + taxonomy + '-' + id + ', #in-' + taxonomy + '-category-' + id).attr( 'checked', c );
298 noSyncChecks = false;
301 catAddBefore = function( s ) {
302 if ( !$('#new'+taxonomy).val() )
304 s.data += '&' + $( ':checked', '#'+taxonomy+'checklist' ).serialize();
308 catAddAfter = function( r, s ) {
309 var sup, drop = $('#new'+taxonomy+'_parent');
311 if ( 'undefined' != s.parsed.responses[0] && (sup = s.parsed.responses[0].supplemental.newcat_parent) ) {
317 $('#' + taxonomy + 'checklist').wpList({
319 response: taxonomy + '-ajax-response',
320 addBefore: catAddBefore,
321 addAfter: catAddAfter
324 $('#' + taxonomy + '-add-toggle').click( function() {
325 $('#' + taxonomy + '-adder').toggleClass( 'wp-hidden-children' );
326 $('a[href="#' + taxonomy + '-all"]', '#' + taxonomy + '-tabs').click();
327 $('#new'+taxonomy).focus();
331 $('#' + taxonomy + 'checklist li.popular-category :checkbox, #' + taxonomy + 'checklist-pop :checkbox').live( 'click', function(){
332 var t = $(this), c = t.is(':checked'), id = t.val();
333 if ( id && t.parents('#taxonomy-'+taxonomy).length )
334 $('#in-' + taxonomy + '-' + id + ', #in-popular-' + taxonomy + '-' + id).attr( 'checked', c );
340 if ( $('#postcustom').length ) {
341 $('#the-list').wpList( { addAfter: function( xml, s ) {
342 $('table#list-table').show();
343 }, addBefore: function( s ) {
344 s.data += '&post_id=' + $('#post_ID').val();
351 if ( $('#submitdiv').length ) {
352 stamp = $('#timestamp').html();
353 visibility = $('#post-visibility-display').html();
355 function updateVisibility() {
356 var pvSelect = $('#post-visibility-select');
357 if ( $('input:radio:checked', pvSelect).val() != 'public' ) {
358 $('#sticky').attr('checked', false);
359 $('#sticky-span').hide();
361 $('#sticky-span').show();
363 if ( $('input:radio:checked', pvSelect).val() != 'password' ) {
364 $('#password-span').hide();
366 $('#password-span').show();
370 function updateText() {
371 var attemptedDate, originalDate, currentDate, publishOn, postStatus = $('#post_status'),
372 optPublish = $('option[value=publish]', postStatus), aa = $('#aa').val(),
373 mm = $('#mm').val(), jj = $('#jj').val(), hh = $('#hh').val(), mn = $('#mn').val();
375 attemptedDate = new Date( aa, mm - 1, jj, hh, mn );
376 originalDate = new Date( $('#hidden_aa').val(), $('#hidden_mm').val() -1, $('#hidden_jj').val(), $('#hidden_hh').val(), $('#hidden_mn').val() );
377 currentDate = new Date( $('#cur_aa').val(), $('#cur_mm').val() -1, $('#cur_jj').val(), $('#cur_hh').val(), $('#cur_mn').val() );
379 if ( attemptedDate.getFullYear() != aa || (1 + attemptedDate.getMonth()) != mm || attemptedDate.getDate() != jj || attemptedDate.getMinutes() != mn ) {
380 $('.timestamp-wrap', '#timestampdiv').addClass('form-invalid');
383 $('.timestamp-wrap', '#timestampdiv').removeClass('form-invalid');
386 if ( attemptedDate > currentDate && $('#original_post_status').val() != 'future' ) {
387 publishOn = postL10n.publishOnFuture;
388 $('#publish').val( postL10n.schedule );
389 } else if ( attemptedDate <= currentDate && $('#original_post_status').val() != 'publish' ) {
390 publishOn = postL10n.publishOn;
391 $('#publish').val( postL10n.publish );
393 publishOn = postL10n.publishOnPast;
394 $('#publish').val( postL10n.update );
396 if ( originalDate.toUTCString() == attemptedDate.toUTCString() ) { //hack
397 $('#timestamp').html(stamp);
399 $('#timestamp').html(
401 $('option[value=' + $('#mm').val() + ']', '#mm').text() + ' ' +
409 if ( $('input:radio:checked', '#post-visibility-select').val() == 'private' ) {
410 $('#publish').val( postL10n.update );
411 if ( optPublish.length == 0 ) {
412 postStatus.append('<option value="publish">' + postL10n.privatelyPublished + '</option>');
414 optPublish.html( postL10n.privatelyPublished );
416 $('option[value=publish]', postStatus).attr('selected', true);
417 $('.edit-post-status', '#misc-publishing-actions').hide();
419 if ( $('#original_post_status').val() == 'future' || $('#original_post_status').val() == 'draft' ) {
420 if ( optPublish.length ) {
422 postStatus.val($('#hidden_post_status').val());
425 optPublish.html( postL10n.published );
427 if ( postStatus.is(':hidden') )
428 $('.edit-post-status', '#misc-publishing-actions').show();
430 $('#post-status-display').html($('option:selected', postStatus).text());
431 if ( $('option:selected', postStatus).val() == 'private' || $('option:selected', postStatus).val() == 'publish' ) {
432 $('#save-post').hide();
434 $('#save-post').show();
435 if ( $('option:selected', postStatus).val() == 'pending' ) {
436 $('#save-post').show().val( postL10n.savePending );
438 $('#save-post').show().val( postL10n.saveDraft );
444 $('.edit-visibility', '#visibility').click(function () {
445 if ($('#post-visibility-select').is(":hidden")) {
447 $('#post-visibility-select').slideDown("normal");
453 $('.cancel-post-visibility', '#post-visibility-select').click(function () {
454 $('#post-visibility-select').slideUp("normal");
455 $('#visibility-radio-' + $('#hidden-post-visibility').val()).attr('checked', true);
456 $('#post_password').val($('#hidden_post_password').val());
457 $('#sticky').attr('checked', $('#hidden-post-sticky').attr('checked'));
458 $('#post-visibility-display').html(visibility);
459 $('.edit-visibility', '#visibility').show();
464 $('.save-post-visibility', '#post-visibility-select').click(function () { // crazyhorse - multiple ok cancels
465 var pvSelect = $('#post-visibility-select');
467 pvSelect.slideUp("normal");
468 $('.edit-visibility', '#visibility').show();
471 if ( $('input:radio:checked', pvSelect).val() != 'public' ) {
472 $('#sticky').attr('checked', false);
475 if ( true == $('#sticky').attr('checked') ) {
481 $('#post-visibility-display').html( postL10n[$('input:radio:checked', pvSelect).val() + sticky] );
485 $('input:radio', '#post-visibility-select').change(function() {
489 $('#timestampdiv').siblings('a.edit-timestamp').click(function() {
490 if ($('#timestampdiv').is(":hidden")) {
491 $('#timestampdiv').slideDown("normal");
497 $('.cancel-timestamp', '#timestampdiv').click(function() {
498 $('#timestampdiv').slideUp("normal");
499 $('#mm').val($('#hidden_mm').val());
500 $('#jj').val($('#hidden_jj').val());
501 $('#aa').val($('#hidden_aa').val());
502 $('#hh').val($('#hidden_hh').val());
503 $('#mn').val($('#hidden_mn').val());
504 $('#timestampdiv').siblings('a.edit-timestamp').show();
509 $('.save-timestamp', '#timestampdiv').click(function () { // crazyhorse - multiple ok cancels
510 if ( updateText() ) {
511 $('#timestampdiv').slideUp("normal");
512 $('#timestampdiv').siblings('a.edit-timestamp').show();
517 $('#post-status-select').siblings('a.edit-post-status').click(function() {
518 if ($('#post-status-select').is(":hidden")) {
519 $('#post-status-select').slideDown("normal");
525 $('.save-post-status', '#post-status-select').click(function() {
526 $('#post-status-select').slideUp("normal");
527 $('#post-status-select').siblings('a.edit-post-status').show();
532 $('.cancel-post-status', '#post-status-select').click(function() {
533 $('#post-status-select').slideUp("normal");
534 $('#post_status').val($('#hidden_post_status').val());
535 $('#post-status-select').siblings('a.edit-post-status').show();
542 if ( $('#edit-slug-box').length ) {
543 editPermalink = function(post_id) {
544 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();
546 $('#view-post-btn').hide();
547 b.html('<a href="#" class="save button">'+postL10n.ok+'</a> <a class="cancel" href="#">'+postL10n.cancel+'</a>');
548 b.children('.save').click(function() {
549 var new_slug = e.children('input').val();
551 action: 'sample-permalink',
554 new_title: $('#title').val(),
555 samplepermalinknonce: $('#samplepermalinknonce').val()
557 $('#edit-slug-box').html(data);
559 real_slug.attr('value', new_slug);
560 makeSlugeditClickable();
561 $('#view-post-btn').show();
566 $('.cancel', '#edit-slug-buttons').click(function() {
567 $('#view-post-btn').show();
570 real_slug.attr('value', revert_slug);
574 for ( i = 0; i < full.length; ++i ) {
575 if ( '%' == full.charAt(i) )
579 slug_value = ( c > full.length / 4 ) ? '' : full;
580 e.html('<input type="text" id="new-post-slug" value="'+slug_value+'" />').children('input').keypress(function(e){
581 var key = e.keyCode || 0;
582 // on enter, just save the new slug, don't save the post
584 b.children('.save').click();
588 b.children('.cancel').click();
591 real_slug.attr('value', this.value);
595 makeSlugeditClickable = function() {
596 $('#editable-post-name').click(function() {
597 $('#edit-slug-buttons').children('.edit-slug').click();
600 makeSlugeditClickable();
603 if ( $('#title').val() == '' )
604 $('#title').siblings('#title-prompt-text').css('visibility', '');
605 $('#title-prompt-text').click(function(){
606 $(this).css('visibility', 'hidden').siblings('#title').focus();
608 $('#title').blur(function(){
609 if (this.value == '')
610 $(this).siblings('#title-prompt-text').css('visibility', '');
612 $(this).siblings('#title-prompt-text').css('visibility', 'hidden');
613 }).keydown(function(e){
614 $(this).siblings('#title-prompt-text').css('visibility', 'hidden');