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 current_tags = thetags.val().split(',');
47 $.each( current_tags, function( key, val ) {
48 var txt, button_id, id = $(el).attr('id');
51 if ( !val.match(/^\s+$/) && '' != val ) {
52 button_id = id + '-check-num-' + key;
53 txt = '<span><a id="' + button_id + '" class="ntdelbutton">X</a> ' + val + '</span> ';
54 tagchecklist.append(txt);
55 $( '#' + button_id ).click( function(){ tagBox.parseTags(this); });
60 flushTags : function(el, a, f) {
62 var text, tags = $('.the-tags', el), newtag = $('input.newtag', el), newtags;
64 text = a ? $(a).text() : newtag.val();
66 newtags = tagsval ? tagsval + ',' + text : text;
68 newtags = this.clean( newtags );
69 newtags = array_unique_noempty( newtags.split(',') ).join(',');
75 if ( 'undefined' == typeof(f) )
82 var tax = id.substr(id.indexOf('-')+1);
84 $.post(ajaxurl, {'action':'get-tagcloud','tax':tax}, function(r, stat) {
85 if ( 0 == r || 'success' != stat )
88 r = $('<p id="tagcloud-'+tax+'" class="the-tagcloud">'+r+'</p>');
89 $('a', r).click(function(){
90 tagBox.flushTags( $(this).closest('.inside').children('.tagsdiv'), this);
99 var t = this, ajaxtag = $('div.ajaxtag');
101 $('.tagsdiv').each( function() {
102 tagBox.quickClicks(this);
105 $('input.tagadd', ajaxtag).click(function(){
106 t.flushTags( $(this).closest('.tagsdiv') );
109 $('div.taghint', ajaxtag).click(function(){
110 $(this).css('visibility', 'hidden').siblings('.newtag').focus();
113 $('input.newtag', ajaxtag).blur(function() {
114 if ( this.value == '' )
115 $(this).siblings('.taghint').css('visibility', '');
117 $(this).siblings('.taghint').css('visibility', 'hidden');
118 }).keyup(function(e){
119 if ( 13 == e.which ) {
120 tagBox.flushTags( $(this).closest('.tagsdiv') );
123 }).keypress(function(e){
124 if ( 13 == e.which ) {
129 var tax = $(this).closest('div.tagsdiv').attr('id');
130 $(this).suggest( ajaxurl + '?action=ajax-tag-search&tax=' + tax, { delay: 500, minchars: 2, multiple: true, multipleSep: ", " } );
133 // save tags on post save/publish
134 $('#post').submit(function(){
135 $('div.tagsdiv').each( function() {
136 tagBox.flushTags(this, false, 1);
141 $('a.tagcloud-link').click(function(){
142 tagBox.get( $(this).attr('id') );
143 $(this).unbind().click(function(){
144 $(this).siblings('.the-tagcloud').toggle();
155 get : function(total, num) {
156 var st = this.st, data;
162 $('#commentsdiv img.waiting').show();
165 'action' : 'get-comments',
167 '_ajax_nonce' : $('#add_comment_nonce').val(),
168 'post_ID' : $('#post_ID').val(),
173 $.post(ajaxurl, data,
175 r = wpAjax.parseAjaxResponse(r);
176 $('#commentsdiv .widefat').show();
177 $('#commentsdiv img.waiting').hide();
179 if ( 'object' == typeof r && r.responses[0] ) {
180 $('#the-comment-list').append( r.responses[0].data );
182 theList = theExtraList = null;
183 $("a[className*=':']").unbind();
186 if ( commentsBox.st > commentsBox.total )
187 $('#show-comments').hide();
189 $('#show-comments').html(postL10n.showcomm);
191 } else if ( 1 == r ) {
192 $('#show-comments').parent().html(postL10n.endcomm);
196 $('#the-comment-list').append('<tr><td colspan="2">'+wpAjax.broken+'</td></tr>');
204 WPSetThumbnailHTML = function(html){
205 $('.inside', '#postimagediv').html(html);
208 WPSetThumbnailID = function(id){
209 var field = $('input[value=_thumbnail_id]', '#list-table');
210 if ( field.size() > 0 ) {
211 $('#meta\\[' + field.attr('id').match(/[0-9]+/) + '\\]\\[value\\]').text(id);
215 WPRemoveThumbnail = function(){
217 action:"set-post-thumbnail", post_id: $('#post_ID').val(), thumbnail_id: -1, cookie: encodeURIComponent(document.cookie)
220 alert( setPostThumbnailL10n.error );
222 WPSetThumbnailHTML(str);
230 jQuery(document).ready( function($) {
231 var catAddAfter, stamp, visibility, sticky = '', post = 'post' == pagenow || 'post-new' == pagenow, page = 'page' == pagenow || 'page-new' == pagenow;
235 postboxes.add_postbox_toggles('post');
237 postboxes.add_postbox_toggles('page');
240 if ( $('#tagsdiv-post_tag').length ) {
243 $('#side-sortables, #normal-sortables, #advanced-sortables').children('div.postbox').each(function(){
244 if ( this.id.indexOf('tagsdiv-') === 0 ) {
252 if ( $('#categorydiv').length ) {
253 // TODO: move to jQuery 1.3+, support for multiple hierarchical taxonomies, see wp-lists.dev.js
254 $('a', '#category-tabs').click(function(){
255 var t = $(this).attr('href');
256 $(this).parent().addClass('tabs').siblings('li').removeClass('tabs');
257 $('#category-tabs').siblings('.tabs-panel').hide();
259 if ( '#categories-all' == t )
260 deleteUserSetting('cats');
262 setUserSetting('cats','pop');
265 if ( getUserSetting('cats') )
266 $('a[href="#categories-pop"]', '#category-tabs').click();
269 $('#newcat').one( 'focus', function() { $(this).val( '' ).removeClass( 'form-input-tip' ) } );
270 $('#category-add-sumbit').click( function(){ $('#newcat').focus(); } );
272 catAddBefore = function( s ) {
273 if ( !$('#newcat').val() )
275 s.data += '&' + $( ':checked', '#categorychecklist' ).serialize();
279 catAddAfter = function( r, s ) {
280 var sup, drop = $('#newcat_parent');
282 if ( 'undefined' != s.parsed.responses[0] && (sup = s.parsed.responses[0].supplemental.newcat_parent) ) {
288 $('#categorychecklist').wpList({
290 response: 'category-ajax-response',
291 addBefore: catAddBefore,
292 addAfter: catAddAfter
295 $('#category-add-toggle').click( function() {
296 $('#category-adder').toggleClass( 'wp-hidden-children' );
297 $('a[href="#categories-all"]', '#category-tabs').click();
301 $('#categorychecklist').children('li.popular-category').add( $('#categorychecklist-pop').children() ).find(':checkbox').live( 'click', function(){
302 var t = $(this), c = t.is(':checked'), id = t.val();
303 $('#in-category-' + id + ', #in-popular-category-' + id).attr( 'checked', c );
309 if ( $('#postcustom').length ) {
310 $('#the-list').wpList( { addAfter: function( xml, s ) {
311 $('table#list-table').show();
312 if ( typeof( autosave_update_post_ID ) != 'undefined' ) {
313 autosave_update_post_ID(s.parsed.responses[0].supplemental.postid);
315 }, addBefore: function( s ) {
316 s.data += '&post_id=' + $('#post_ID').val();
323 if ( $('#submitdiv').length ) {
324 stamp = $('#timestamp').html();
325 visibility = $('#post-visibility-display').html();
327 function updateVisibility() {
328 var pvSelect = $('#post-visibility-select');
329 if ( $('input:radio:checked', pvSelect).val() != 'public' ) {
330 $('#sticky').attr('checked', false);
331 $('#sticky-span').hide();
333 $('#sticky-span').show();
335 if ( $('input:radio:checked', pvSelect).val() != 'password' ) {
336 $('#password-span').hide();
338 $('#password-span').show();
342 function updateText() {
343 var attemptedDate, originalDate, currentDate, publishOn, postStatus = $('#post_status'),
344 optPublish = $('option[value=publish]', postStatus), aa = $('#aa').val(),
345 mm = $('#mm').val(), jj = $('#jj').val(), hh = $('#hh').val(), mn = $('#mn').val();
347 attemptedDate = new Date( aa, mm - 1, jj, hh, mn );
348 originalDate = new Date( $('#hidden_aa').val(), $('#hidden_mm').val() -1, $('#hidden_jj').val(), $('#hidden_hh').val(), $('#hidden_mn').val() );
349 currentDate = new Date( $('#cur_aa').val(), $('#cur_mm').val() -1, $('#cur_jj').val(), $('#cur_hh').val(), $('#cur_mn').val() );
351 if ( attemptedDate.getFullYear() != aa || (1 + attemptedDate.getMonth()) != mm || attemptedDate.getDate() != jj || attemptedDate.getMinutes() != mn ) {
352 $('.timestamp-wrap', '#timestampdiv').addClass('form-invalid');
355 $('.timestamp-wrap', '#timestampdiv').removeClass('form-invalid');
358 if ( attemptedDate > currentDate && $('#original_post_status').val() != 'future' ) {
359 publishOn = postL10n.publishOnFuture;
360 $('#publish').val( postL10n.schedule );
361 } else if ( attemptedDate <= currentDate && $('#original_post_status').val() != 'publish' ) {
362 publishOn = postL10n.publishOn;
363 $('#publish').val( postL10n.publish );
365 publishOn = postL10n.publishOnPast;
367 $('#publish').val( postL10n.updatePage );
369 $('#publish').val( postL10n.updatePost );
371 if ( originalDate.toUTCString() == attemptedDate.toUTCString() ) { //hack
372 $('#timestamp').html(stamp);
374 $('#timestamp').html(
376 $('option[value=' + $('#mm').val() + ']', '#mm').text() + ' ' +
384 if ( $('input:radio:checked', '#post-visibility-select').val() == 'private' ) {
386 $('#publish').val( postL10n.updatePage );
388 $('#publish').val( postL10n.updatePost );
389 if ( optPublish.length == 0 ) {
390 postStatus.append('<option value="publish">' + postL10n.privatelyPublished + '</option>');
392 optPublish.html( postL10n.privatelyPublished );
394 $('option[value=publish]', postStatus).attr('selected', true);
395 $('.edit-post-status', '#misc-publishing-actions').hide();
397 if ( $('#original_post_status').val() == 'future' || $('#original_post_status').val() == 'draft' ) {
398 if ( optPublish.length ) {
400 postStatus.val($('#hidden_post_status').val());
403 optPublish.html( postL10n.published );
405 if ( postStatus.is(':hidden') )
406 $('.edit-post-status', '#misc-publishing-actions').show();
408 $('#post-status-display').html($('option:selected', postStatus).text());
409 if ( $('option:selected', postStatus).val() == 'private' || $('option:selected', postStatus).val() == 'publish' ) {
410 $('#save-post').hide();
412 $('#save-post').show();
413 if ( $('option:selected', postStatus).val() == 'pending' ) {
414 $('#save-post').show().val( postL10n.savePending );
416 $('#save-post').show().val( postL10n.saveDraft );
422 $('.edit-visibility', '#visibility').click(function () {
423 if ($('#post-visibility-select').is(":hidden")) {
425 $('#post-visibility-select').slideDown("normal");
431 $('.cancel-post-visibility', '#post-visibility-select').click(function () {
432 $('#post-visibility-select').slideUp("normal");
433 $('#visibility-radio-' + $('#hidden-post-visibility').val()).attr('checked', true);
434 $('#post_password').val($('#hidden_post_password').val());
435 $('#sticky').attr('checked', $('#hidden-post-sticky').attr('checked'));
436 $('#post-visibility-display').html(visibility);
437 $('.edit-visibility', '#visibility').show();
442 $('.save-post-visibility', '#post-visibility-select').click(function () { // crazyhorse - multiple ok cancels
443 var pvSelect = $('#post-visibility-select');
445 pvSelect.slideUp("normal");
446 $('.edit-visibility', '#visibility').show();
449 if ( $('input:radio:checked', pvSelect).val() != 'public' ) {
450 $('#sticky').attr('checked', false);
453 if ( true == $('#sticky').attr('checked') ) {
459 $('#post-visibility-display').html( postL10n[$('input:radio:checked', pvSelect).val() + sticky] );
463 $('input:radio', '#post-visibility-select').change(function() {
467 $('#timestampdiv').siblings('a.edit-timestamp').click(function() {
468 if ($('#timestampdiv').is(":hidden")) {
469 $('#timestampdiv').slideDown("normal");
475 $('.cancel-timestamp', '#timestampdiv').click(function() {
476 $('#timestampdiv').slideUp("normal");
477 $('#mm').val($('#hidden_mm').val());
478 $('#jj').val($('#hidden_jj').val());
479 $('#aa').val($('#hidden_aa').val());
480 $('#hh').val($('#hidden_hh').val());
481 $('#mn').val($('#hidden_mn').val());
482 $('#timestampdiv').siblings('a.edit-timestamp').show();
487 $('.save-timestamp', '#timestampdiv').click(function () { // crazyhorse - multiple ok cancels
488 if ( updateText() ) {
489 $('#timestampdiv').slideUp("normal");
490 $('#timestampdiv').siblings('a.edit-timestamp').show();
495 $('#post-status-select').siblings('a.edit-post-status').click(function() {
496 if ($('#post-status-select').is(":hidden")) {
497 $('#post-status-select').slideDown("normal");
503 $('.save-post-status', '#post-status-select').click(function() {
504 $('#post-status-select').slideUp("normal");
505 $('#post-status-select').siblings('a.edit-post-status').show();
510 $('.cancel-post-status', '#post-status-select').click(function() {
511 $('#post-status-select').slideUp("normal");
512 $('#post_status').val($('#hidden_post_status').val());
513 $('#post-status-select').siblings('a.edit-post-status').show();
520 if ( $('#edit-slug-box').length ) {
521 editPermalink = function(post_id) {
522 var i, c = 0, e = $('#editable-post-name'), revert_e = e.html(), real_slug = $('#post_name'), revert_slug = real_slug.html(), b = $('#edit-slug-buttons'), revert_b = b.html(), full = $('#editable-post-name-full').html();
524 $('#view-post-btn').hide();
525 b.html('<a href="#" class="save button">'+postL10n.ok+'</a> <a class="cancel" href="#">'+postL10n.cancel+'</a>');
526 b.children('.save').click(function() {
527 var new_slug = e.children('input').val();
529 action: 'sample-permalink',
532 new_title: $('#title').val(),
533 samplepermalinknonce: $('#samplepermalinknonce').val()
535 $('#edit-slug-box').html(data);
537 real_slug.attr('value', new_slug);
538 makeSlugeditClickable();
539 $('#view-post-btn').show();
544 $('.cancel', '#edit-slug-buttons').click(function() {
545 $('#view-post-btn').show();
548 real_slug.attr('value', revert_slug);
552 for ( i = 0; i < full.length; ++i ) {
553 if ( '%' == full.charAt(i) )
557 slug_value = ( c > full.length / 4 ) ? '' : full;
558 e.html('<input type="text" id="new-post-slug" value="'+slug_value+'" />').children('input').keypress(function(e){
559 var key = e.keyCode || 0;
560 // on enter, just save the new slug, don't save the post
562 b.children('.save').click();
566 b.children('.cancel').click();
569 real_slug.attr('value', this.value);
573 makeSlugeditClickable = function() {
574 $('#editable-post-name').click(function() {
575 $('#edit-slug-buttons').children('.edit-slug').click();
578 makeSlugeditClickable();