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