]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/js/post.dev.js
Wordpress 3.1
[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                         'p' : $('#post_ID').val(),
184                         'start' : st,
185                         'number' : 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
200                                         if ( commentsBox.st > commentsBox.total )
201                                                 $('#show-comments').hide();
202                                         else
203                                                 $('#show-comments').html(postL10n.showcomm);
204                                         return;
205                                 } else if ( 1 == r ) {
206                                         $('#show-comments').parent().html(postL10n.endcomm);
207                                         return;
208                                 }
209
210                                 $('#the-comment-list').append('<tr><td colspan="2">'+wpAjax.broken+'</td></tr>');
211                         }
212                 );
213
214                 return false;
215         }
216 };
217
218 WPSetThumbnailHTML = function(html){
219         $('.inside', '#postimagediv').html(html);
220 };
221
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);
226         }
227 };
228
229 WPRemoveThumbnail = function(nonce){
230         $.post(ajaxurl, {
231                 action:"set-post-thumbnail", post_id: $('#post_ID').val(), thumbnail_id: -1, _ajax_nonce: nonce, cookie: encodeURIComponent(document.cookie)
232         }, function(str){
233                 if ( str == '0' ) {
234                         alert( setPostThumbnailL10n.error );
235                 } else {
236                         WPSetThumbnailHTML(str);
237                 }
238         }
239         );
240 };
241
242 })(jQuery);
243
244 jQuery(document).ready( function($) {
245         var stamp, visibility, sticky = '';
246
247         postboxes.add_postbox_toggles(pagenow);
248
249         // multi-taxonomies
250         if ( $('#tagsdiv-post_tag').length ) {
251                 tagBox.init();
252         } else {
253                 $('#side-sortables, #normal-sortables, #advanced-sortables').children('div.postbox').each(function(){
254                         if ( this.id.indexOf('tagsdiv-') === 0 ) {
255                                 tagBox.init();
256                                 return false;
257                         }
258                 });
259         }
260
261         // categories
262         $('.categorydiv').each( function(){
263                 var this_id = $(this).attr('id'), noSyncChecks = false, syncChecks, catAddAfter, taxonomyParts, taxonomy, settingName;
264
265                 taxonomyParts = this_id.split('-');
266                 taxonomyParts.shift();
267                 taxonomy = taxonomyParts.join('-');
268                 settingName = taxonomy + '_tab';
269                 if ( taxonomy == 'category' )
270                         settingName = 'cats';
271
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();
277                         $(t).show();
278                         if ( '#' + taxonomy + '-all' == t )
279                                 deleteUserSetting(settingName);
280                         else
281                                 setUserSetting(settingName, 'pop');
282                         return false;
283                 });
284
285                 if ( getUserSetting(settingName) )
286                         $('a[href="#' + taxonomy + '-pop"]', '#' + taxonomy + '-tabs').click();
287
288                 // Ajax Cat
289                 $('#new' + taxonomy).one( 'focus', function() { $(this).val( '' ).removeClass( 'form-input-tip' ) } );
290                 $('#' + taxonomy + '-add-submit').click( function(){ $('#new' + taxonomy).focus(); });
291
292                 syncChecks = function() {
293                         if ( noSyncChecks )
294                                 return;
295                         noSyncChecks = true;
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;
299                 };
300
301                 catAddBefore = function( s ) {
302                         if ( !$('#new'+taxonomy).val() )
303                                 return false;
304                         s.data += '&' + $( ':checked', '#'+taxonomy+'checklist' ).serialize();
305                         return s;
306                 };
307
308                 catAddAfter = function( r, s ) {
309                         var sup, drop = $('#new'+taxonomy+'_parent');
310
311                         if ( 'undefined' != s.parsed.responses[0] && (sup = s.parsed.responses[0].supplemental.newcat_parent) ) {
312                                 drop.before(sup);
313                                 drop.remove();
314                         }
315                 };
316
317                 $('#' + taxonomy + 'checklist').wpList({
318                         alt: '',
319                         response: taxonomy + '-ajax-response',
320                         addBefore: catAddBefore,
321                         addAfter: catAddAfter
322                 });
323
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();
328                         return false;
329                 });
330
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 );
335                 });
336
337         }); // end cats
338
339         // Custom Fields
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();
345                         return s;
346                 }
347                 });
348         }
349
350         // submitdiv
351         if ( $('#submitdiv').length ) {
352                 stamp = $('#timestamp').html();
353                 visibility = $('#post-visibility-display').html();
354
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();
360                         } else {
361                                 $('#sticky-span').show();
362                         }
363                         if ( $('input:radio:checked', pvSelect).val() != 'password' ) {
364                                 $('#password-span').hide();
365                         } else {
366                                 $('#password-span').show();
367                         }
368                 }
369
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();
374
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() );
378
379                         if ( attemptedDate.getFullYear() != aa || (1 + attemptedDate.getMonth()) != mm || attemptedDate.getDate() != jj || attemptedDate.getMinutes() != mn ) {
380                                 $('.timestamp-wrap', '#timestampdiv').addClass('form-invalid');
381                                 return false;
382                         } else {
383                                 $('.timestamp-wrap', '#timestampdiv').removeClass('form-invalid');
384                         }
385
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 );
392                         } else {
393                                 publishOn = postL10n.publishOnPast;
394                                 $('#publish').val( postL10n.update );
395                         }
396                         if ( originalDate.toUTCString() == attemptedDate.toUTCString() ) { //hack
397                                 $('#timestamp').html(stamp);
398                         } else {
399                                 $('#timestamp').html(
400                                         publishOn + ' <b>' +
401                                         $('option[value=' + $('#mm').val() + ']', '#mm').text() + ' ' +
402                                         jj + ', ' +
403                                         aa + ' @ ' +
404                                         hh + ':' +
405                                         mn + '</b> '
406                                 );
407                         }
408
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>');
413                                 } else {
414                                         optPublish.html( postL10n.privatelyPublished );
415                                 }
416                                 $('option[value=publish]', postStatus).attr('selected', true);
417                                 $('.edit-post-status', '#misc-publishing-actions').hide();
418                         } else {
419                                 if ( $('#original_post_status').val() == 'future' || $('#original_post_status').val() == 'draft' ) {
420                                         if ( optPublish.length ) {
421                                                 optPublish.remove();
422                                                 postStatus.val($('#hidden_post_status').val());
423                                         }
424                                 } else {
425                                         optPublish.html( postL10n.published );
426                                 }
427                                 if ( postStatus.is(':hidden') )
428                                         $('.edit-post-status', '#misc-publishing-actions').show();
429                         }
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();
433                         } else {
434                                 $('#save-post').show();
435                                 if ( $('option:selected', postStatus).val() == 'pending' ) {
436                                         $('#save-post').show().val( postL10n.savePending );
437                                 } else {
438                                         $('#save-post').show().val( postL10n.saveDraft );
439                                 }
440                         }
441                         return true;
442                 }
443
444                 $('.edit-visibility', '#visibility').click(function () {
445                         if ($('#post-visibility-select').is(":hidden")) {
446                                 updateVisibility();
447                                 $('#post-visibility-select').slideDown("normal");
448                                 $(this).hide();
449                         }
450                         return false;
451                 });
452
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();
460                         updateText();
461                         return false;
462                 });
463
464                 $('.save-post-visibility', '#post-visibility-select').click(function () { // crazyhorse - multiple ok cancels
465                         var pvSelect = $('#post-visibility-select');
466
467                         pvSelect.slideUp("normal");
468                         $('.edit-visibility', '#visibility').show();
469                         updateText();
470
471                         if ( $('input:radio:checked', pvSelect).val() != 'public' ) {
472                                 $('#sticky').attr('checked', false);
473                         } // WEAPON LOCKED
474
475                         if ( true == $('#sticky').attr('checked') ) {
476                                 sticky = 'Sticky';
477                         } else {
478                                 sticky = '';
479                         }
480
481                         $('#post-visibility-display').html(     postL10n[$('input:radio:checked', pvSelect).val() + sticky]     );
482                         return false;
483                 });
484
485                 $('input:radio', '#post-visibility-select').change(function() {
486                         updateVisibility();
487                 });
488
489                 $('#timestampdiv').siblings('a.edit-timestamp').click(function() {
490                         if ($('#timestampdiv').is(":hidden")) {
491                                 $('#timestampdiv').slideDown("normal");
492                                 $(this).hide();
493                         }
494                         return false;
495                 });
496
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();
505                         updateText();
506                         return false;
507                 });
508
509                 $('.save-timestamp', '#timestampdiv').click(function () { // crazyhorse - multiple ok cancels
510                         if ( updateText() ) {
511                                 $('#timestampdiv').slideUp("normal");
512                                 $('#timestampdiv').siblings('a.edit-timestamp').show();
513                         }
514                         return false;
515                 });
516
517                 $('#post-status-select').siblings('a.edit-post-status').click(function() {
518                         if ($('#post-status-select').is(":hidden")) {
519                                 $('#post-status-select').slideDown("normal");
520                                 $(this).hide();
521                         }
522                         return false;
523                 });
524
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();
528                         updateText();
529                         return false;
530                 });
531
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();
536                         updateText();
537                         return false;
538                 });
539         } // end submitdiv
540
541         // permalink
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();
545
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();
550                                 $.post(ajaxurl, {
551                                         action: 'sample-permalink',
552                                         post_id: post_id,
553                                         new_slug: new_slug,
554                                         new_title: $('#title').val(),
555                                         samplepermalinknonce: $('#samplepermalinknonce').val()
556                                 }, function(data) {
557                                         $('#edit-slug-box').html(data);
558                                         b.html(revert_b);
559                                         real_slug.attr('value', new_slug);
560                                         makeSlugeditClickable();
561                                         $('#view-post-btn').show();
562                                 });
563                                 return false;
564                         });
565
566                         $('.cancel', '#edit-slug-buttons').click(function() {
567                                 $('#view-post-btn').show();
568                                 e.html(revert_e);
569                                 b.html(revert_b);
570                                 real_slug.attr('value', revert_slug);
571                                 return false;
572                         });
573
574                         for ( i = 0; i < full.length; ++i ) {
575                                 if ( '%' == full.charAt(i) )
576                                         c++;
577                         }
578
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
583                                 if ( 13 == key ) {
584                                         b.children('.save').click();
585                                         return false;
586                                 }
587                                 if ( 27 == key ) {
588                                         b.children('.cancel').click();
589                                         return false;
590                                 }
591                                 real_slug.attr('value', this.value);
592                         }).focus();
593                 }
594
595                 makeSlugeditClickable = function() {
596                         $('#editable-post-name').click(function() {
597                                 $('#edit-slug-buttons').children('.edit-slug').click();
598                         });
599                 }
600                 makeSlugeditClickable();
601         }
602
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();
607         });
608         $('#title').blur(function(){
609                 if (this.value == '')
610                         $(this).siblings('#title-prompt-text').css('visibility', '');
611         }).focus(function(){
612                 $(this).siblings('#title-prompt-text').css('visibility', 'hidden');
613         }).keydown(function(e){
614                 $(this).siblings('#title-prompt-text').css('visibility', 'hidden');
615                 $(this).unbind(e);
616         });
617 });