]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/js/post.dev.js
Wordpress 3.0.5
[autoinstalls/wordpress.git] / wp-admin / js / post.dev.js
1 var tagBox, commentsBox, editPermalink, makeSlugeditClickable, WPSetThumbnailHTML, WPSetThumbnailID, WPRemoveThumbnail;
2
3 // return an array with any duplicate, whitespace or values removed
4 function array_unique_noempty(a) {
5         var out = [];
6         jQuery.each( a, function(key, val) {
7                 val = jQuery.trim(val);
8                 if ( val && jQuery.inArray(val, out) == -1 )
9                         out.push(val);
10                 } );
11         return out;
12 }
13
14 (function($){
15
16 tagBox = {
17         clean : function(tags) {
18                 return tags.replace(/\s*,\s*/g, ',').replace(/,+/g, ',').replace(/[,\s]+$/, '').replace(/^[,\s]+/, '');
19         },
20
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];
24
25                 $.each( current_tags, function(key, val) {
26                         val = $.trim(val);
27                         if ( val ) {
28                                 new_tags.push(val);
29                         }
30                 });
31
32                 thetags.val( this.clean( new_tags.join(',') ) );
33
34                 this.quickClicks(taxbox);
35                 return false;
36         },
37
38         quickClicks : function(el) {
39                 var thetags = $('.the-tags', el),
40                         tagchecklist = $('.tagchecklist', el),
41                         id = $(el).attr('id'),
42                         current_tags, disabled;
43
44                 if ( !thetags.length )
45                         return;
46
47                 disabled = thetags.attr('disabled');
48
49                 current_tags = thetags.val().split(',');
50                 tagchecklist.empty();
51
52                 $.each( current_tags, function( key, val ) {
53                         var span, xbutton;
54
55                         val = $.trim( val );
56
57                         if ( ! val )
58                                 return;
59
60                         // Create a new span, and ensure the text is properly escaped.
61                         span = $('<span />').text( val );
62
63                         // If tags editing isn't disabled, create the X button.
64                         if ( ! disabled ) {
65                                 xbutton = $( '<a id="' + id + '-check-num-' + key + '" class="ntdelbutton">X</a>' );
66                                 xbutton.click( function(){ tagBox.parseTags(this); });
67                                 span.prepend('&nbsp;').prepend( xbutton );
68                         }
69
70                         // Append the span to the tag list.
71                         tagchecklist.append( span );
72                 });
73         },
74
75         flushTags : function(el, a, f) {
76                 a = a || false;
77                 var text, tags = $('.the-tags', el), newtag = $('input.newtag', el), newtags;
78
79                 text = a ? $(a).text() : newtag.val();
80                 tagsval = tags.val();
81                 newtags = tagsval ? tagsval + ',' + text : text;
82
83                 newtags = this.clean( newtags );
84                 newtags = array_unique_noempty( newtags.split(',') ).join(',');
85                 tags.val(newtags);
86                 this.quickClicks(el);
87
88                 if ( !a )
89                         newtag.val('');
90                 if ( 'undefined' == typeof(f) )
91                         newtag.focus();
92
93                 return false;
94         },
95
96         get : function(id) {
97                 var tax = id.substr(id.indexOf('-')+1);
98
99                 $.post(ajaxurl, {'action':'get-tagcloud','tax':tax}, function(r, stat) {
100                         if ( 0 == r || 'success' != stat )
101                                 r = wpAjax.broken;
102
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);
106                                 return false;
107                         });
108
109                         $('#'+id).after(r);
110                 });
111         },
112
113         init : function() {
114                 var t = this, ajaxtag = $('div.ajaxtag');
115
116             $('.tagsdiv').each( function() {
117                 tagBox.quickClicks(this);
118             });
119
120                 $('input.tagadd', ajaxtag).click(function(){
121                         t.flushTags( $(this).closest('.tagsdiv') );
122                 });
123
124                 $('div.taghint', ajaxtag).click(function(){
125                         $(this).css('visibility', 'hidden').parent().siblings('.newtag').focus();
126                 });
127
128                 $('input.newtag', ajaxtag).blur(function() {
129                         if ( this.value == '' )
130                     $(this).parent().siblings('.taghint').css('visibility', '');
131             }).focus(function(){
132                         $(this).parent().siblings('.taghint').css('visibility', 'hidden');
133                 }).keyup(function(e){
134                         if ( 13 == e.which ) {
135                                 tagBox.flushTags( $(this).closest('.tagsdiv') );
136                                 return false;
137                         }
138                 }).keypress(function(e){
139                         if ( 13 == e.which ) {
140                                 e.preventDefault();
141                                 return false;
142                         }
143                 }).each(function(){
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: "," } );
146                 });
147
148             // save tags on post save/publish
149             $('#post').submit(function(){
150                         $('div.tagsdiv').each( function() {
151                         tagBox.flushTags(this, false, 1);
152                         });
153                 });
154
155                 // tag cloud
156                 $('a.tagcloud-link').click(function(){
157                         tagBox.get( $(this).attr('id') );
158                         $(this).unbind().click(function(){
159                                 $(this).siblings('.the-tagcloud').toggle();
160                                 return false;
161                         });
162                         return false;
163                 });
164         }
165 };
166
167 commentsBox = {
168         st : 0,
169
170         get : function(total, num) {
171                 var st = this.st, data;
172                 if ( ! num )
173                         num = 20;
174
175                 this.st += num;
176                 this.total = total;
177                 $('#commentsdiv img.waiting').show();
178
179                 data = {
180                         'action' : 'get-comments',
181                         'mode' : 'single',
182                         '_ajax_nonce' : $('#add_comment_nonce').val(),
183                         'post_ID' : $('#post_ID').val(),
184                         'start' : st,
185                         'num' : num
186                 };
187
188                 $.post(ajaxurl, data,
189                         function(r) {
190                                 r = wpAjax.parseAjaxResponse(r);
191                                 $('#commentsdiv .widefat').show();
192                                 $('#commentsdiv img.waiting').hide();
193
194                                 if ( 'object' == typeof r && r.responses[0] ) {
195                                         $('#the-comment-list').append( r.responses[0].data );
196
197                                         theList = theExtraList = null;
198                                         $("a[className*=':']").unbind();
199                                         setCommentsList();
200
201                                         if ( commentsBox.st > commentsBox.total )
202                                                 $('#show-comments').hide();
203                                         else
204                                                 $('#show-comments').html(postL10n.showcomm);
205                                         return;
206                                 } else if ( 1 == r ) {
207                                         $('#show-comments').parent().html(postL10n.endcomm);
208                                         return;
209                                 }
210
211                                 $('#the-comment-list').append('<tr><td colspan="2">'+wpAjax.broken+'</td></tr>');
212                         }
213                 );
214
215                 return false;
216         }
217 };
218
219 WPSetThumbnailHTML = function(html){
220         $('.inside', '#postimagediv').html(html);
221 };
222
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);
227         }
228 };
229
230 WPRemoveThumbnail = function(nonce){
231         $.post(ajaxurl, {
232                 action:"set-post-thumbnail", post_id: $('#post_ID').val(), thumbnail_id: -1, _ajax_nonce: nonce, cookie: encodeURIComponent(document.cookie)
233         }, function(str){
234                 if ( str == '0' ) {
235                         alert( setPostThumbnailL10n.error );
236                 } else {
237                         WPSetThumbnailHTML(str);
238                 }
239         }
240         );
241 };
242
243 })(jQuery);
244
245 jQuery(document).ready( function($) {
246         var stamp, visibility, sticky = '';
247
248         postboxes.add_postbox_toggles(pagenow);
249
250         // multi-taxonomies
251         if ( $('#tagsdiv-post_tag').length ) {
252                 tagBox.init();
253         } else {
254                 $('#side-sortables, #normal-sortables, #advanced-sortables').children('div.postbox').each(function(){
255                         if ( this.id.indexOf('tagsdiv-') === 0 ) {
256                                 tagBox.init();
257                                 return false;
258                         }
259                 });
260         }
261
262         // categories
263         $('.categorydiv').each( function(){
264                 var this_id = $(this).attr('id'), noSyncChecks = false, syncChecks, catAddAfter, taxonomyParts, taxonomy, settingName;
265
266                 taxonomyParts = this_id.split('-');
267                 taxonomyParts.shift();
268                 taxonomy = taxonomyParts.join('-');
269                 settingName = taxonomy + '_tab';
270                 if ( taxonomy == 'category' )
271                         settingName = 'cats';
272
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();
278                         $(t).show();
279                         if ( '#' + taxonomy + '-all' == t )
280                                 deleteUserSetting(settingName);
281                         else
282                                 setUserSetting(settingName, 'pop');
283                         return false;
284                 });
285
286                 if ( getUserSetting(settingName) )
287                         $('a[href="#' + taxonomy + '-pop"]', '#' + taxonomy + '-tabs').click();
288
289                 // Ajax Cat
290                 $('#new' + taxonomy).one( 'focus', function() { $(this).val( '' ).removeClass( 'form-input-tip' ) } );
291                 $('#' + taxonomy + '-add-submit').click( function(){ $('#new' + taxonomy).focus(); });
292
293                 syncChecks = function() {
294                         if ( noSyncChecks )
295                                 return;
296                         noSyncChecks = true;
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;
300                 };
301
302                 catAddBefore = function( s ) {
303                         if ( !$('#new'+taxonomy).val() )
304                                 return false;
305                         s.data += '&' + $( ':checked', '#'+taxonomy+'checklist' ).serialize();
306                         return s;
307                 };
308
309                 catAddAfter = function( r, s ) {
310                         var sup, drop = $('#new'+taxonomy+'_parent');
311
312                         if ( 'undefined' != s.parsed.responses[0] && (sup = s.parsed.responses[0].supplemental.newcat_parent) ) {
313                                 drop.before(sup);
314                                 drop.remove();
315                         }
316                 };
317
318                 $('#' + taxonomy + 'checklist').wpList({
319                         alt: '',
320                         response: taxonomy + '-ajax-response',
321                         addBefore: catAddBefore,
322                         addAfter: catAddAfter
323                 });
324
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();
329                         return false;
330                 });
331
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 );
336                 });
337
338         }); // end cats
339
340         // Custom Fields
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();
346                         return s;
347                 }
348                 });
349         }
350
351         // submitdiv
352         if ( $('#submitdiv').length ) {
353                 stamp = $('#timestamp').html();
354                 visibility = $('#post-visibility-display').html();
355
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();
361                         } else {
362                                 $('#sticky-span').show();
363                         }
364                         if ( $('input:radio:checked', pvSelect).val() != 'password' ) {
365                                 $('#password-span').hide();
366                         } else {
367                                 $('#password-span').show();
368                         }
369                 }
370
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();
375
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() );
379
380                         if ( attemptedDate.getFullYear() != aa || (1 + attemptedDate.getMonth()) != mm || attemptedDate.getDate() != jj || attemptedDate.getMinutes() != mn ) {
381                                 $('.timestamp-wrap', '#timestampdiv').addClass('form-invalid');
382                                 return false;
383                         } else {
384                                 $('.timestamp-wrap', '#timestampdiv').removeClass('form-invalid');
385                         }
386
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 );
393                         } else {
394                                 publishOn = postL10n.publishOnPast;
395                                 if ( page )
396                                         $('#publish').val( postL10n.updatePage );
397                                 else
398                                         $('#publish').val( postL10n.updatePost );
399                         }
400                         if ( originalDate.toUTCString() == attemptedDate.toUTCString() ) { //hack
401                                 $('#timestamp').html(stamp);
402                         } else {
403                                 $('#timestamp').html(
404                                         publishOn + ' <b>' +
405                                         $('option[value=' + $('#mm').val() + ']', '#mm').text() + ' ' +
406                                         jj + ', ' +
407                                         aa + ' @ ' +
408                                         hh + ':' +
409                                         mn + '</b> '
410                                 );
411                         }
412
413                         if ( $('input:radio:checked', '#post-visibility-select').val() == 'private' ) {
414                                 if ( page )
415                                         $('#publish').val( postL10n.updatePage );
416                                 else
417                                         $('#publish').val( postL10n.updatePost );
418                                 if ( optPublish.length == 0 ) {
419                                         postStatus.append('<option value="publish">' + postL10n.privatelyPublished + '</option>');
420                                 } else {
421                                         optPublish.html( postL10n.privatelyPublished );
422                                 }
423                                 $('option[value=publish]', postStatus).attr('selected', true);
424                                 $('.edit-post-status', '#misc-publishing-actions').hide();
425                         } else {
426                                 if ( $('#original_post_status').val() == 'future' || $('#original_post_status').val() == 'draft' ) {
427                                         if ( optPublish.length ) {
428                                                 optPublish.remove();
429                                                 postStatus.val($('#hidden_post_status').val());
430                                         }
431                                 } else {
432                                         optPublish.html( postL10n.published );
433                                 }
434                                 if ( postStatus.is(':hidden') )
435                                         $('.edit-post-status', '#misc-publishing-actions').show();
436                         }
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();
440                         } else {
441                                 $('#save-post').show();
442                                 if ( $('option:selected', postStatus).val() == 'pending' ) {
443                                         $('#save-post').show().val( postL10n.savePending );
444                                 } else {
445                                         $('#save-post').show().val( postL10n.saveDraft );
446                                 }
447                         }
448                         return true;
449                 }
450
451                 $('.edit-visibility', '#visibility').click(function () {
452                         if ($('#post-visibility-select').is(":hidden")) {
453                                 updateVisibility();
454                                 $('#post-visibility-select').slideDown("normal");
455                                 $(this).hide();
456                         }
457                         return false;
458                 });
459
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();
467                         updateText();
468                         return false;
469                 });
470
471                 $('.save-post-visibility', '#post-visibility-select').click(function () { // crazyhorse - multiple ok cancels
472                         var pvSelect = $('#post-visibility-select');
473
474                         pvSelect.slideUp("normal");
475                         $('.edit-visibility', '#visibility').show();
476                         updateText();
477
478                         if ( $('input:radio:checked', pvSelect).val() != 'public' ) {
479                                 $('#sticky').attr('checked', false);
480                         } // WEAPON LOCKED
481
482                         if ( true == $('#sticky').attr('checked') ) {
483                                 sticky = 'Sticky';
484                         } else {
485                                 sticky = '';
486                         }
487
488                         $('#post-visibility-display').html(     postL10n[$('input:radio:checked', pvSelect).val() + sticky]     );
489                         return false;
490                 });
491
492                 $('input:radio', '#post-visibility-select').change(function() {
493                         updateVisibility();
494                 });
495
496                 $('#timestampdiv').siblings('a.edit-timestamp').click(function() {
497                         if ($('#timestampdiv').is(":hidden")) {
498                                 $('#timestampdiv').slideDown("normal");
499                                 $(this).hide();
500                         }
501                         return false;
502                 });
503
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();
512                         updateText();
513                         return false;
514                 });
515
516                 $('.save-timestamp', '#timestampdiv').click(function () { // crazyhorse - multiple ok cancels
517                         if ( updateText() ) {
518                                 $('#timestampdiv').slideUp("normal");
519                                 $('#timestampdiv').siblings('a.edit-timestamp').show();
520                         }
521                         return false;
522                 });
523
524                 $('#post-status-select').siblings('a.edit-post-status').click(function() {
525                         if ($('#post-status-select').is(":hidden")) {
526                                 $('#post-status-select').slideDown("normal");
527                                 $(this).hide();
528                         }
529                         return false;
530                 });
531
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();
535                         updateText();
536                         return false;
537                 });
538
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();
543                         updateText();
544                         return false;
545                 });
546         } // end submitdiv
547
548         // permalink
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();
552
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();
557                                 $.post(ajaxurl, {
558                                         action: 'sample-permalink',
559                                         post_id: post_id,
560                                         new_slug: new_slug,
561                                         new_title: $('#title').val(),
562                                         samplepermalinknonce: $('#samplepermalinknonce').val()
563                                 }, function(data) {
564                                         $('#edit-slug-box').html(data);
565                                         b.html(revert_b);
566                                         real_slug.attr('value', new_slug);
567                                         makeSlugeditClickable();
568                                         $('#view-post-btn').show();
569                                 });
570                                 return false;
571                         });
572
573                         $('.cancel', '#edit-slug-buttons').click(function() {
574                                 $('#view-post-btn').show();
575                                 e.html(revert_e);
576                                 b.html(revert_b);
577                                 real_slug.attr('value', revert_slug);
578                                 return false;
579                         });
580
581                         for ( i = 0; i < full.length; ++i ) {
582                                 if ( '%' == full.charAt(i) )
583                                         c++;
584                         }
585
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
590                                 if ( 13 == key ) {
591                                         b.children('.save').click();
592                                         return false;
593                                 }
594                                 if ( 27 == key ) {
595                                         b.children('.cancel').click();
596                                         return false;
597                                 }
598                                 real_slug.attr('value', this.value);
599                         }).focus();
600                 }
601
602                 makeSlugeditClickable = function() {
603                         $('#editable-post-name').click(function() {
604                                 $('#edit-slug-buttons').children('.edit-slug').click();
605                         });
606                 }
607                 makeSlugeditClickable();
608         }
609
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();
614         });
615         $('#title').blur(function(){
616                 if (this.value == '')
617                         $(this).siblings('#title-prompt-text').css('visibility', '');
618         }).focus(function(){
619                 $(this).siblings('#title-prompt-text').css('visibility', 'hidden');
620         }).keydown(function(e){
621                 $(this).siblings('#title-prompt-text').css('visibility', 'hidden');
622                 $(this).unbind(e);
623         });
624 });