]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/js/post.dev.js
Wordpress 3.0
[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), tagchecklist = $('.tagchecklist', el), current_tags;
40
41                 if ( !thetags.length )
42                         return;
43
44                 var disabled = thetags.attr('disabled');
45
46                 current_tags = thetags.val().split(',');
47                 tagchecklist.empty();
48
49                 $.each( current_tags, function( key, val ) {
50                         var txt, button_id, id = $(el).attr('id');
51
52                         val = $.trim(val);
53                         if ( !val.match(/^\s+$/) && '' != val ) {
54                                 button_id = id + '-check-num-' + key;
55                                 if ( disabled )
56                                         txt = '<span>' + val + '</span> ';
57                                 else
58                                         txt = '<span><a id="' + button_id + '" class="ntdelbutton">X</a>&nbsp;' + val + '</span> ';
59                                 tagchecklist.append(txt);
60                                 if ( ! disabled )
61                                         $( '#' + button_id ).click( function(){ tagBox.parseTags(this); });
62                         }
63                 });
64         },
65
66         flushTags : function(el, a, f) {
67                 a = a || false;
68                 var text, tags = $('.the-tags', el), newtag = $('input.newtag', el), newtags;
69
70                 text = a ? $(a).text() : newtag.val();
71                 tagsval = tags.val();
72                 newtags = tagsval ? tagsval + ',' + text : text;
73
74                 newtags = this.clean( newtags );
75                 newtags = array_unique_noempty( newtags.split(',') ).join(',');
76                 tags.val(newtags);
77                 this.quickClicks(el);
78
79                 if ( !a )
80                         newtag.val('');
81                 if ( 'undefined' == typeof(f) )
82                         newtag.focus();
83
84                 return false;
85         },
86
87         get : function(id) {
88                 var tax = id.substr(id.indexOf('-')+1);
89
90                 $.post(ajaxurl, {'action':'get-tagcloud','tax':tax}, function(r, stat) {
91                         if ( 0 == r || 'success' != stat )
92                                 r = wpAjax.broken;
93
94                         r = $('<p id="tagcloud-'+tax+'" class="the-tagcloud">'+r+'</p>');
95                         $('a', r).click(function(){
96                                 tagBox.flushTags( $(this).closest('.inside').children('.tagsdiv'), this);
97                                 return false;
98                         });
99
100                         $('#'+id).after(r);
101                 });
102         },
103
104         init : function() {
105                 var t = this, ajaxtag = $('div.ajaxtag');
106
107             $('.tagsdiv').each( function() {
108                 tagBox.quickClicks(this);
109             });
110
111                 $('input.tagadd', ajaxtag).click(function(){
112                         t.flushTags( $(this).closest('.tagsdiv') );
113                 });
114
115                 $('div.taghint', ajaxtag).click(function(){
116                         $(this).css('visibility', 'hidden').parent().siblings('.newtag').focus();
117                 });
118
119                 $('input.newtag', ajaxtag).blur(function() {
120                         if ( this.value == '' )
121                     $(this).parent().siblings('.taghint').css('visibility', '');
122             }).focus(function(){
123                         $(this).parent().siblings('.taghint').css('visibility', 'hidden');
124                 }).keyup(function(e){
125                         if ( 13 == e.which ) {
126                                 tagBox.flushTags( $(this).closest('.tagsdiv') );
127                                 return false;
128                         }
129                 }).keypress(function(e){
130                         if ( 13 == e.which ) {
131                                 e.preventDefault();
132                                 return false;
133                         }
134                 }).each(function(){
135                         var tax = $(this).closest('div.tagsdiv').attr('id');
136                         $(this).suggest( ajaxurl + '?action=ajax-tag-search&tax=' + tax, { delay: 500, minchars: 2, multiple: true, multipleSep: "," } );
137                 });
138
139             // save tags on post save/publish
140             $('#post').submit(function(){
141                         $('div.tagsdiv').each( function() {
142                         tagBox.flushTags(this, false, 1);
143                         });
144                 });
145
146                 // tag cloud
147                 $('a.tagcloud-link').click(function(){
148                         tagBox.get( $(this).attr('id') );
149                         $(this).unbind().click(function(){
150                                 $(this).siblings('.the-tagcloud').toggle();
151                                 return false;
152                         });
153                         return false;
154                 });
155         }
156 };
157
158 commentsBox = {
159         st : 0,
160
161         get : function(total, num) {
162                 var st = this.st, data;
163                 if ( ! num )
164                         num = 20;
165
166                 this.st += num;
167                 this.total = total;
168                 $('#commentsdiv img.waiting').show();
169
170                 data = {
171                         'action' : 'get-comments',
172                         'mode' : 'single',
173                         '_ajax_nonce' : $('#add_comment_nonce').val(),
174                         'post_ID' : $('#post_ID').val(),
175                         'start' : st,
176                         'num' : num
177                 };
178
179                 $.post(ajaxurl, data,
180                         function(r) {
181                                 r = wpAjax.parseAjaxResponse(r);
182                                 $('#commentsdiv .widefat').show();
183                                 $('#commentsdiv img.waiting').hide();
184
185                                 if ( 'object' == typeof r && r.responses[0] ) {
186                                         $('#the-comment-list').append( r.responses[0].data );
187
188                                         theList = theExtraList = null;
189                                         $("a[className*=':']").unbind();
190                                         setCommentsList();
191
192                                         if ( commentsBox.st > commentsBox.total )
193                                                 $('#show-comments').hide();
194                                         else
195                                                 $('#show-comments').html(postL10n.showcomm);
196                                         return;
197                                 } else if ( 1 == r ) {
198                                         $('#show-comments').parent().html(postL10n.endcomm);
199                                         return;
200                                 }
201
202                                 $('#the-comment-list').append('<tr><td colspan="2">'+wpAjax.broken+'</td></tr>');
203                         }
204                 );
205
206                 return false;
207         }
208 };
209
210 WPSetThumbnailHTML = function(html){
211         $('.inside', '#postimagediv').html(html);
212 };
213
214 WPSetThumbnailID = function(id){
215         var field = $('input[value=_thumbnail_id]', '#list-table');
216         if ( field.size() > 0 ) {
217                 $('#meta\\[' + field.attr('id').match(/[0-9]+/) + '\\]\\[value\\]').text(id);
218         }
219 };
220
221 WPRemoveThumbnail = function(nonce){
222         $.post(ajaxurl, {
223                 action:"set-post-thumbnail", post_id: $('#post_ID').val(), thumbnail_id: -1, _ajax_nonce: nonce, cookie: encodeURIComponent(document.cookie)
224         }, function(str){
225                 if ( str == '0' ) {
226                         alert( setPostThumbnailL10n.error );
227                 } else {
228                         WPSetThumbnailHTML(str);
229                 }
230         }
231         );
232 };
233
234 })(jQuery);
235
236 jQuery(document).ready( function($) {
237         var stamp, visibility, sticky = '';
238
239         postboxes.add_postbox_toggles(pagenow);
240
241         // multi-taxonomies
242         if ( $('#tagsdiv-post_tag').length ) {
243                 tagBox.init();
244         } else {
245                 $('#side-sortables, #normal-sortables, #advanced-sortables').children('div.postbox').each(function(){
246                         if ( this.id.indexOf('tagsdiv-') === 0 ) {
247                                 tagBox.init();
248                                 return false;
249                         }
250                 });
251         }
252
253         // categories
254         $('.categorydiv').each( function(){
255                 var this_id = $(this).attr('id'), noSyncChecks = false, syncChecks, catAddAfter, taxonomyParts, taxonomy, settingName;
256
257                 taxonomyParts = this_id.split('-');
258                 taxonomyParts.shift();
259                 taxonomy = taxonomyParts.join('-');
260                 settingName = taxonomy + '_tab';
261                 if ( taxonomy == 'category' )
262                         settingName = 'cats';
263
264                 // TODO: move to jQuery 1.3+, support for multiple hierarchical taxonomies, see wp-lists.dev.js
265                 $('a', '#' + taxonomy + '-tabs').click( function(){
266                         var t = $(this).attr('href');
267                         $(this).parent().addClass('tabs').siblings('li').removeClass('tabs');
268                         $('#' + taxonomy + '-tabs').siblings('.tabs-panel').hide();
269                         $(t).show();
270                         if ( '#' + taxonomy + '-all' == t )
271                                 deleteUserSetting(settingName);
272                         else
273                                 setUserSetting(settingName, 'pop');
274                         return false;
275                 });
276
277                 if ( getUserSetting(settingName) )
278                         $('a[href="#' + taxonomy + '-pop"]', '#' + taxonomy + '-tabs').click();
279
280                 // Ajax Cat
281                 $('#new' + taxonomy).one( 'focus', function() { $(this).val( '' ).removeClass( 'form-input-tip' ) } );
282                 $('#' + taxonomy + '-add-submit').click( function(){ $('#new' + taxonomy).focus(); });
283
284                 syncChecks = function() {
285                         if ( noSyncChecks )
286                                 return;
287                         noSyncChecks = true;
288                         var th = jQuery(this), c = th.is(':checked'), id = th.val().toString();
289                         $('#in-' + taxonomy + '-' + id + ', #in-' + taxonomy + '-category-' + id).attr( 'checked', c );
290                         noSyncChecks = false;
291                 };
292
293                 catAddBefore = function( s ) {
294                         if ( !$('#new'+taxonomy).val() )
295                                 return false;
296                         s.data += '&' + $( ':checked', '#'+taxonomy+'checklist' ).serialize();
297                         return s;
298                 };
299
300                 catAddAfter = function( r, s ) {
301                         var sup, drop = $('#new'+taxonomy+'_parent');
302
303                         if ( 'undefined' != s.parsed.responses[0] && (sup = s.parsed.responses[0].supplemental.newcat_parent) ) {
304                                 drop.before(sup);
305                                 drop.remove();
306                         }
307                 };
308
309                 $('#' + taxonomy + 'checklist').wpList({
310                         alt: '',
311                         response: taxonomy + '-ajax-response',
312                         addBefore: catAddBefore,
313                         addAfter: catAddAfter
314                 });
315
316                 $('#' + taxonomy + '-add-toggle').click( function() {
317                         $('#' + taxonomy + '-adder').toggleClass( 'wp-hidden-children' );
318                         $('a[href="#' + taxonomy + '-all"]', '#' + taxonomy + '-tabs').click();
319                         $('#new'+taxonomy).focus();
320                         return false;
321                 });
322
323                 $('#' + taxonomy + 'checklist li.popular-category :checkbox, #' + taxonomy + 'checklist-pop :checkbox').live( 'click', function(){
324                         var t = $(this), c = t.is(':checked'), id = t.val();
325                         if ( id && t.parents('#taxonomy-'+taxonomy).length )
326                                 $('#in-' + taxonomy + '-' + id + ', #in-popular-' + taxonomy + '-' + id).attr( 'checked', c );
327                 });
328
329         }); // end cats
330
331         // Custom Fields
332         if ( $('#postcustom').length ) {
333                 $('#the-list').wpList( { addAfter: function( xml, s ) {
334                         $('table#list-table').show();
335                 }, addBefore: function( s ) {
336                         s.data += '&post_id=' + $('#post_ID').val();
337                         return s;
338                 }
339                 });
340         }
341
342         // submitdiv
343         if ( $('#submitdiv').length ) {
344                 stamp = $('#timestamp').html();
345                 visibility = $('#post-visibility-display').html();
346
347                 function updateVisibility() {
348                         var pvSelect = $('#post-visibility-select');
349                         if ( $('input:radio:checked', pvSelect).val() != 'public' ) {
350                                 $('#sticky').attr('checked', false);
351                                 $('#sticky-span').hide();
352                         } else {
353                                 $('#sticky-span').show();
354                         }
355                         if ( $('input:radio:checked', pvSelect).val() != 'password' ) {
356                                 $('#password-span').hide();
357                         } else {
358                                 $('#password-span').show();
359                         }
360                 }
361
362                 function updateText() {
363                         var attemptedDate, originalDate, currentDate, publishOn, page = 'page' == pagenow || 'page-new' == pagenow,
364                                 postStatus = $('#post_status'), optPublish = $('option[value=publish]', postStatus), aa = $('#aa').val(),
365                                 mm = $('#mm').val(), jj = $('#jj').val(), hh = $('#hh').val(), mn = $('#mn').val();
366
367                         attemptedDate = new Date( aa, mm - 1, jj, hh, mn );
368                         originalDate = new Date( $('#hidden_aa').val(), $('#hidden_mm').val() -1, $('#hidden_jj').val(), $('#hidden_hh').val(), $('#hidden_mn').val() );
369                         currentDate = new Date( $('#cur_aa').val(), $('#cur_mm').val() -1, $('#cur_jj').val(), $('#cur_hh').val(), $('#cur_mn').val() );
370
371                         if ( attemptedDate.getFullYear() != aa || (1 + attemptedDate.getMonth()) != mm || attemptedDate.getDate() != jj || attemptedDate.getMinutes() != mn ) {
372                                 $('.timestamp-wrap', '#timestampdiv').addClass('form-invalid');
373                                 return false;
374                         } else {
375                                 $('.timestamp-wrap', '#timestampdiv').removeClass('form-invalid');
376                         }
377
378                         if ( attemptedDate > currentDate && $('#original_post_status').val() != 'future' ) {
379                                 publishOn = postL10n.publishOnFuture;
380                                 $('#publish').val( postL10n.schedule );
381                         } else if ( attemptedDate <= currentDate && $('#original_post_status').val() != 'publish' ) {
382                                 publishOn = postL10n.publishOn;
383                                 $('#publish').val( postL10n.publish );
384                         } else {
385                                 publishOn = postL10n.publishOnPast;
386                                 if ( page )
387                                         $('#publish').val( postL10n.updatePage );
388                                 else
389                                         $('#publish').val( postL10n.updatePost );
390                         }
391                         if ( originalDate.toUTCString() == attemptedDate.toUTCString() ) { //hack
392                                 $('#timestamp').html(stamp);
393                         } else {
394                                 $('#timestamp').html(
395                                         publishOn + ' <b>' +
396                                         $('option[value=' + $('#mm').val() + ']', '#mm').text() + ' ' +
397                                         jj + ', ' +
398                                         aa + ' @ ' +
399                                         hh + ':' +
400                                         mn + '</b> '
401                                 );
402                         }
403
404                         if ( $('input:radio:checked', '#post-visibility-select').val() == 'private' ) {
405                                 if ( page )
406                                         $('#publish').val( postL10n.updatePage );
407                                 else
408                                         $('#publish').val( postL10n.updatePost );
409                                 if ( optPublish.length == 0 ) {
410                                         postStatus.append('<option value="publish">' + postL10n.privatelyPublished + '</option>');
411                                 } else {
412                                         optPublish.html( postL10n.privatelyPublished );
413                                 }
414                                 $('option[value=publish]', postStatus).attr('selected', true);
415                                 $('.edit-post-status', '#misc-publishing-actions').hide();
416                         } else {
417                                 if ( $('#original_post_status').val() == 'future' || $('#original_post_status').val() == 'draft' ) {
418                                         if ( optPublish.length ) {
419                                                 optPublish.remove();
420                                                 postStatus.val($('#hidden_post_status').val());
421                                         }
422                                 } else {
423                                         optPublish.html( postL10n.published );
424                                 }
425                                 if ( postStatus.is(':hidden') )
426                                         $('.edit-post-status', '#misc-publishing-actions').show();
427                         }
428                         $('#post-status-display').html($('option:selected', postStatus).text());
429                         if ( $('option:selected', postStatus).val() == 'private' || $('option:selected', postStatus).val() == 'publish' ) {
430                                 $('#save-post').hide();
431                         } else {
432                                 $('#save-post').show();
433                                 if ( $('option:selected', postStatus).val() == 'pending' ) {
434                                         $('#save-post').show().val( postL10n.savePending );
435                                 } else {
436                                         $('#save-post').show().val( postL10n.saveDraft );
437                                 }
438                         }
439                         return true;
440                 }
441
442                 $('.edit-visibility', '#visibility').click(function () {
443                         if ($('#post-visibility-select').is(":hidden")) {
444                                 updateVisibility();
445                                 $('#post-visibility-select').slideDown("normal");
446                                 $(this).hide();
447                         }
448                         return false;
449                 });
450
451                 $('.cancel-post-visibility', '#post-visibility-select').click(function () {
452                         $('#post-visibility-select').slideUp("normal");
453                         $('#visibility-radio-' + $('#hidden-post-visibility').val()).attr('checked', true);
454                         $('#post_password').val($('#hidden_post_password').val());
455                         $('#sticky').attr('checked', $('#hidden-post-sticky').attr('checked'));
456                         $('#post-visibility-display').html(visibility);
457                         $('.edit-visibility', '#visibility').show();
458                         updateText();
459                         return false;
460                 });
461
462                 $('.save-post-visibility', '#post-visibility-select').click(function () { // crazyhorse - multiple ok cancels
463                         var pvSelect = $('#post-visibility-select');
464
465                         pvSelect.slideUp("normal");
466                         $('.edit-visibility', '#visibility').show();
467                         updateText();
468
469                         if ( $('input:radio:checked', pvSelect).val() != 'public' ) {
470                                 $('#sticky').attr('checked', false);
471                         } // WEAPON LOCKED
472
473                         if ( true == $('#sticky').attr('checked') ) {
474                                 sticky = 'Sticky';
475                         } else {
476                                 sticky = '';
477                         }
478
479                         $('#post-visibility-display').html(     postL10n[$('input:radio:checked', pvSelect).val() + sticky]     );
480                         return false;
481                 });
482
483                 $('input:radio', '#post-visibility-select').change(function() {
484                         updateVisibility();
485                 });
486
487                 $('#timestampdiv').siblings('a.edit-timestamp').click(function() {
488                         if ($('#timestampdiv').is(":hidden")) {
489                                 $('#timestampdiv').slideDown("normal");
490                                 $(this).hide();
491                         }
492                         return false;
493                 });
494
495                 $('.cancel-timestamp', '#timestampdiv').click(function() {
496                         $('#timestampdiv').slideUp("normal");
497                         $('#mm').val($('#hidden_mm').val());
498                         $('#jj').val($('#hidden_jj').val());
499                         $('#aa').val($('#hidden_aa').val());
500                         $('#hh').val($('#hidden_hh').val());
501                         $('#mn').val($('#hidden_mn').val());
502                         $('#timestampdiv').siblings('a.edit-timestamp').show();
503                         updateText();
504                         return false;
505                 });
506
507                 $('.save-timestamp', '#timestampdiv').click(function () { // crazyhorse - multiple ok cancels
508                         if ( updateText() ) {
509                                 $('#timestampdiv').slideUp("normal");
510                                 $('#timestampdiv').siblings('a.edit-timestamp').show();
511                         }
512                         return false;
513                 });
514
515                 $('#post-status-select').siblings('a.edit-post-status').click(function() {
516                         if ($('#post-status-select').is(":hidden")) {
517                                 $('#post-status-select').slideDown("normal");
518                                 $(this).hide();
519                         }
520                         return false;
521                 });
522
523                 $('.save-post-status', '#post-status-select').click(function() {
524                         $('#post-status-select').slideUp("normal");
525                         $('#post-status-select').siblings('a.edit-post-status').show();
526                         updateText();
527                         return false;
528                 });
529
530                 $('.cancel-post-status', '#post-status-select').click(function() {
531                         $('#post-status-select').slideUp("normal");
532                         $('#post_status').val($('#hidden_post_status').val());
533                         $('#post-status-select').siblings('a.edit-post-status').show();
534                         updateText();
535                         return false;
536                 });
537         } // end submitdiv
538
539         // permalink
540         if ( $('#edit-slug-box').length ) {
541                 editPermalink = function(post_id) {
542                         var i, c = 0, e = $('#editable-post-name'), revert_e = e.html(), real_slug = $('#post_name'), revert_slug = real_slug.val(), b = $('#edit-slug-buttons'), revert_b = b.html(), full = $('#editable-post-name-full').html();
543
544                         $('#view-post-btn').hide();
545                         b.html('<a href="#" class="save button">'+postL10n.ok+'</a> <a class="cancel" href="#">'+postL10n.cancel+'</a>');
546                         b.children('.save').click(function() {
547                                 var new_slug = e.children('input').val();
548                                 $.post(ajaxurl, {
549                                         action: 'sample-permalink',
550                                         post_id: post_id,
551                                         new_slug: new_slug,
552                                         new_title: $('#title').val(),
553                                         samplepermalinknonce: $('#samplepermalinknonce').val()
554                                 }, function(data) {
555                                         $('#edit-slug-box').html(data);
556                                         b.html(revert_b);
557                                         real_slug.attr('value', new_slug);
558                                         makeSlugeditClickable();
559                                         $('#view-post-btn').show();
560                                 });
561                                 return false;
562                         });
563
564                         $('.cancel', '#edit-slug-buttons').click(function() {
565                                 $('#view-post-btn').show();
566                                 e.html(revert_e);
567                                 b.html(revert_b);
568                                 real_slug.attr('value', revert_slug);
569                                 return false;
570                         });
571
572                         for ( i = 0; i < full.length; ++i ) {
573                                 if ( '%' == full.charAt(i) )
574                                         c++;
575                         }
576
577                         slug_value = ( c > full.length / 4 ) ? '' : full;
578                         e.html('<input type="text" id="new-post-slug" value="'+slug_value+'" />').children('input').keypress(function(e){
579                                 var key = e.keyCode || 0;
580                                 // on enter, just save the new slug, don't save the post
581                                 if ( 13 == key ) {
582                                         b.children('.save').click();
583                                         return false;
584                                 }
585                                 if ( 27 == key ) {
586                                         b.children('.cancel').click();
587                                         return false;
588                                 }
589                                 real_slug.attr('value', this.value);
590                         }).focus();
591                 }
592
593                 makeSlugeditClickable = function() {
594                         $('#editable-post-name').click(function() {
595                                 $('#edit-slug-buttons').children('.edit-slug').click();
596                         });
597                 }
598                 makeSlugeditClickable();
599         }
600
601         if ( $('#title').val() == '' )
602                 $('#title').siblings('#title-prompt-text').css('visibility', '');
603         $('#title-prompt-text').click(function(){
604                 $(this).css('visibility', 'hidden').siblings('#title').focus();
605         });
606         $('#title').blur(function(){
607                 if (this.value == '')
608                         $(this).siblings('#title-prompt-text').css('visibility', '');
609         }).focus(function(){
610                 $(this).siblings('#title-prompt-text').css('visibility', 'hidden');
611         }).keydown(function(e){
612                 $(this).siblings('#title-prompt-text').css('visibility', 'hidden');
613                 $(this).unbind(e);
614         });
615 });