]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/js/post.dev.js
Wordpress 2.8
[autoinstalls/wordpress.git] / wp-admin / js / post.dev.js
1 // return an array with any duplicate, whitespace or values removed
2 function array_unique_noempty(a) {
3         var out = [];
4         jQuery.each( a, function(key, val) {
5                 val = jQuery.trim(val);
6                 if ( val && jQuery.inArray(val, out) == -1 )
7                         out.push(val);
8                 } );
9         return out;
10 }
11
12 function new_tag_remove_tag() {
13         var id = jQuery( this ).attr( 'id' ), num = id.split('-check-num-')[1], taxbox = jQuery(this).parents('.tagsdiv'), current_tags = taxbox.find( '.the-tags' ).val().split(','), new_tags = [];
14         delete current_tags[num];
15
16         jQuery.each( current_tags, function(key, val) {
17                 val = jQuery.trim(val);
18                 if ( val ) {
19                         new_tags.push(val);
20                 }
21         });
22
23         taxbox.find('.the-tags').val( new_tags.join(',').replace(/\s*,+\s*/, ',').replace(/,+/, ',').replace(/,+\s+,+/, ',').replace(/,+\s*$/, '').replace(/^\s*,+/, '') );
24
25         tag_update_quickclicks(taxbox);
26         return false;
27 }
28
29 function tag_update_quickclicks(taxbox) {
30         if ( jQuery(taxbox).find('.the-tags').length == 0 )
31                 return;
32
33         var current_tags = jQuery(taxbox).find('.the-tags').val().split(',');
34         jQuery(taxbox).find('.tagchecklist').empty();
35         shown = false;
36
37         jQuery.each( current_tags, function( key, val ) {
38                 var txt, button_id;
39
40                 val = jQuery.trim(val);
41                 if ( !val.match(/^\s+$/) && '' != val ) {
42                         button_id = jQuery(taxbox).attr('id') + '-check-num-' + key;
43                         txt = '<span><a id="' + button_id + '" class="ntdelbutton">X</a>&nbsp;' + val + '</span> ';
44                         jQuery(taxbox).find('.tagchecklist').append(txt);
45                         jQuery( '#' + button_id ).click( new_tag_remove_tag );
46                 }
47         });
48         if ( shown )
49                 jQuery(taxbox).find('.tagchecklist').prepend('<strong>'+postL10n.tagsUsed+'</strong><br />');
50 }
51
52 function tag_flush_to_text(id, a) {
53         a = a || false;
54         var taxbox, text, tags, newtags;
55
56         taxbox = jQuery('#'+id);
57         text = a ? jQuery(a).text() : taxbox.find('input.newtag').val();
58
59         // is the input box empty (i.e. showing the 'Add new tag' tip)?
60         if ( taxbox.find('input.newtag').hasClass('form-input-tip') && ! a )
61                 return false;
62
63         tags = taxbox.find('.the-tags').val();
64         newtags = tags ? tags + ',' + text : text;
65
66         // massage
67         newtags = newtags.replace(/\s+,+\s*/g, ',').replace(/,+/g, ',').replace(/,+\s+,+/g, ',').replace(/,+\s*$/g, '').replace(/^\s*,+/g, '');
68         newtags = array_unique_noempty(newtags.split(',')).join(',');
69         taxbox.find('.the-tags').val(newtags);
70         tag_update_quickclicks(taxbox);
71
72         if ( ! a )
73                 taxbox.find('input.newtag').val('').focus();
74
75         return false;
76 }
77
78 function tag_save_on_publish() {
79         jQuery('.tagsdiv').each( function(i) {
80                 if ( !jQuery(this).find('input.newtag').hasClass('form-input-tip') )
81                 tag_flush_to_text(jQuery(this).parents('.tagsdiv').attr('id'));
82                 } );
83 }
84
85 function tag_press_key( e ) {
86         if ( 13 == e.which ) {
87                 tag_flush_to_text(jQuery(e.target).parents('.tagsdiv').attr('id'));
88                 return false;
89         }
90 };
91
92 function tag_init() {
93
94         jQuery('.ajaxtag').show();
95     jQuery('.tagsdiv').each( function(i) {
96         tag_update_quickclicks(this);
97     } );
98
99     // add the quickadd form
100     jQuery('.ajaxtag input.tagadd').click(function(){tag_flush_to_text(jQuery(this).parents('.tagsdiv').attr('id'));});
101     jQuery('.ajaxtag input.newtag').focus(function() {
102         if ( !this.cleared ) {
103             this.cleared = true;
104             jQuery(this).val( '' ).removeClass( 'form-input-tip' );
105         }
106     });
107
108     jQuery('.ajaxtag input.newtag').blur(function() {
109         if ( this.value == '' ) {
110             this.cleared = false;
111             jQuery(this).val( postL10n.addTag ).addClass( 'form-input-tip' );
112         }
113     });
114
115     // auto-save tags on post save/publish
116     jQuery('#publish').click( tag_save_on_publish );
117     jQuery('#save-post').click( tag_save_on_publish );
118
119     // catch the enter key
120     jQuery('.ajaxtag input.newtag').keypress( tag_press_key );
121 }
122
123 var commentsBox, tagCloud;
124 (function($){
125
126         commentsBox = {
127                 st : 0,
128
129                 get : function(total, num) {
130                         var st = this.st, data;
131                         if ( ! num )
132                                 num = 20;
133
134                         this.st += num;
135                         this.total = total;
136                         $('#commentsdiv img.waiting').show();
137
138                         data = {
139                                 'action' : 'get-comments',
140                                 'mode' : 'single',
141                                 '_ajax_nonce' : $('#add_comment_nonce').val(),
142                                 'post_ID' : $('#post_ID').val(),
143                                 'start' : st,
144                                 'num' : num
145                         };
146
147                         $.post(ajaxurl, data,
148                                 function(r) {
149                                         r = wpAjax.parseAjaxResponse(r);
150                                         $('#commentsdiv .widefat').show();
151                                         $('#commentsdiv img.waiting').hide();
152
153                                         if ( 'object' == typeof r && r.responses[0] ) {
154                                                 $('#the-comment-list').append( r.responses[0].data );
155
156                                                 theList = theExtraList = null;
157                                                 $("a[className*=':']").unbind();
158                                                 setCommentsList();
159
160                                                 if ( commentsBox.st > commentsBox.total )
161                                                         $('#show-comments').hide();
162                                                 else
163                                                         $('#show-comments').html(postL10n.showcomm);
164                                                 return;
165                                         } else if ( 1 == r ) {
166                                                 $('#show-comments').parent().html(postL10n.endcomm);
167                                                 return;
168                                         }
169
170                                         $('#the-comment-list').append('<tr><td colspan="5">'+wpAjax.broken+'</td></tr>');
171                                 }
172                         );
173
174                         return false;
175                 }
176         };
177
178         tagCloud = {
179                 init : function() {
180                         $('.tagcloud-link').click(function(){
181                                 tagCloud.get($(this).attr('id'));
182                                 $(this).unbind().click(function(){
183                                         $(this).siblings('.the-tagcloud').toggle();
184                                         return false;
185                                 });
186                                 return false;
187                         });
188                 },
189
190                 get : function(id) {
191                         var tax = id.substr(id.indexOf('-')+1);
192
193                         $.post(ajaxurl, {'action':'get-tagcloud','tax':tax}, function(r, stat) {
194                                 if ( 0 == r || 'success' != stat )
195                                         r = wpAjax.broken;
196
197                                 r = $('<p id="tagcloud-'+tax+'" class="the-tagcloud">'+r+'</p>');
198                                 $('a', r).click(function(){
199                                         var id = $(this).parents('p').attr('id');
200                                         tag_flush_to_text(id.substr(id.indexOf('-')+1), this);
201                                         return false;
202                                 });
203
204                                 $('#'+id).after(r);
205                         });
206                 }
207         };
208
209         $(document).ready(function(){tagCloud.init();});
210 })(jQuery);
211
212 jQuery(document).ready( function($) {
213         var noSyncChecks = false, syncChecks, catAddAfter, stamp = $('#timestamp').html(), visibility = $('#post-visibility-display').html(), sticky = '';
214
215         // for Press This
216         if ( typeof autosave != 'function' )
217                 autosave = function(){};
218
219         // postboxes
220         postboxes.add_postbox_toggles('post');
221
222         // Editable slugs
223         make_slugedit_clickable();
224
225         // prepare the tag UI
226         tag_init();
227
228         $('#title').blur( function() { if ( ($("#post_ID").val() > 0) || ($("#title").val().length == 0) ) return; autosave(); } );
229
230         // auto-suggest stuff
231         $('.newtag').each(function(){
232                 var tax = $(this).parents('div.tagsdiv').attr('id');
233                 $(this).suggest( 'admin-ajax.php?action=ajax-tag-search&tax='+tax, { delay: 500, minchars: 2, multiple: true, multipleSep: ", " } );
234         });
235
236         // category tabs
237         $('#category-tabs a').click(function(){
238                 var t = $(this).attr('href');
239                 $(this).parent().addClass('tabs').siblings('li').removeClass('tabs');
240                 $('.tabs-panel').hide();
241                 $(t).show();
242                 if ( '#categories-all' == t )
243                         deleteUserSetting('cats');
244                 else
245                         setUserSetting('cats','pop');
246                 return false;
247         });
248         if ( getUserSetting('cats') )
249                 $('#category-tabs a[href="#categories-pop"]').click();
250
251         // Ajax Cat
252         $('#newcat').one( 'focus', function() { $(this).val( '' ).removeClass( 'form-input-tip' ) } );
253         $('#category-add-sumbit').click(function(){$('#newcat').focus();});
254
255         syncChecks = function() {
256                 if ( noSyncChecks )
257                         return;
258                 noSyncChecks = true;
259                 var th = jQuery(this), c = th.is(':checked'), id = th.val().toString();
260                 $('#in-category-' + id + ', #in-popular-category-' + id).attr( 'checked', c );
261                 noSyncChecks = false;
262         };
263
264         popularCats = $('#categorychecklist-pop :checkbox').map( function() { return parseInt(jQuery(this).val(), 10); } ).get().join(',');
265         catAddBefore = function( s ) {
266                 if ( !$('#newcat').val() )
267                         return false;
268                 s.data += '&popular_ids=' + popularCats + '&' + jQuery( '#categorychecklist :checked' ).serialize();
269                 return s;
270         };
271
272         catAddAfter = function( r, s ) {
273                 var newCatParent = jQuery('#newcat_parent'), newCatParentOption = newCatParent.find( 'option[value="-1"]' );
274                 $(s.what + ' response_data', r).each( function() {
275                         var t = $($(this).text());
276                         t.find( 'label' ).each( function() {
277                                 var th = $(this), val = th.find('input').val(), id = th.find('input')[0].id, name, o;
278                                 $('#' + id).change( syncChecks ).change();
279                                 if ( newCatParent.find( 'option[value="' + val + '"]' ).size() )
280                                         return;
281                                 name = $.trim( th.text() );
282                                 o = $( '<option value="' +  parseInt( val, 10 ) + '"></option>' ).text( name );
283                                 newCatParent.prepend( o );
284                         } );
285                         newCatParentOption.attr( 'selected', 'selected' );
286                 } );
287         };
288
289         $('#categorychecklist').wpList( {
290                 alt: '',
291                 response: 'category-ajax-response',
292                 addBefore: catAddBefore,
293                 addAfter: catAddAfter
294         } );
295
296         $('#category-add-toggle').click( function() {
297                 $('#category-adder').toggleClass( 'wp-hidden-children' );
298                 $('#category-tabs a[href="#categories-all"]').click();
299                 return false;
300         } );
301
302         $('.categorychecklist .popular-category :checkbox').change( syncChecks ).filter( ':checked' ).change(), sticky = '';
303
304         function updateVisibility() {
305                 if ( $('#post-visibility-select input:radio:checked').val() != 'public' ) {
306                         $('#sticky').attr('checked', false);
307                         $('#sticky-span').hide();
308                 } else {
309                         $('#sticky-span').show();
310                 }
311                 if ( $('#post-visibility-select input:radio:checked').val() != 'password' ) {
312                         $('#password-span').hide();
313                 } else {
314                         $('#password-span').show();
315                 }
316         }
317
318         function updateText() {
319                 var attemptedDate, originalDate, currentDate, publishOn;
320
321                 attemptedDate = new Date( $('#aa').val(), $('#mm').val() -1, $('#jj').val(), $('#hh').val(), $('#mn').val());
322                 originalDate = new Date( $('#hidden_aa').val(), $('#hidden_mm').val() -1, $('#hidden_jj').val(), $('#hidden_hh').val(), $('#hidden_mn').val());
323                 currentDate = new Date( $('#cur_aa').val(), $('#cur_mm').val() -1, $('#cur_jj').val(), $('#cur_hh').val(), $('#cur_mn').val());
324                 if ( attemptedDate > currentDate && $('#original_post_status').val() != 'future' ) {
325                         publishOn = postL10n.publishOnFuture;
326                         $('#publish').val( postL10n.schedule );
327                 } else if ( attemptedDate <= currentDate && $('#original_post_status').val() != 'publish' ) {
328                         publishOn = postL10n.publishOn;
329                         $('#publish').val( postL10n.publish );
330                 } else {
331                         publishOn = postL10n.publishOnPast;
332                         $('#publish').val( postL10n.update );
333                 }
334                 if ( originalDate.toUTCString() == attemptedDate.toUTCString() ) { //hack
335                         $('#timestamp').html(stamp);
336                 } else {
337                         $('#timestamp').html(
338                                 publishOn + ' <b>' +
339                                 $( '#mm option[value=' + $('#mm').val() + ']' ).text() + ' ' +
340                                 $('#jj').val() + ', ' +
341                                 $('#aa').val() + ' @ ' +
342                                 $('#hh').val() + ':' +
343                                 $('#mn').val() + '</b> '
344                         );
345                 }
346
347                 if ( $('#post-visibility-select input:radio:checked').val() == 'private' ) {
348                         $('#publish').val( postL10n.update );
349                         if ( $('#post_status option[value=publish]').length == 0 ) {
350                                 $('#post_status').append('<option value="publish">' + postL10n.privatelyPublished + '</option>');
351                         }
352                         $('#post_status option[value=publish]').html( postL10n.privatelyPublished );
353                         $('#post_status option[value=publish]').attr('selected', true);
354                         $('.edit-post-status').hide();
355                 } else {
356                         if ( $('#original_post_status').val() == 'future' || $('#original_post_status').val() == 'draft' ) {
357                                 if ( $('#post_status option[value=publish]').length != 0 ) {
358                                         $('#post_status option[value=publish]').remove();
359                                         $('#post_status').val($('#hidden_post_status').val());
360                                 }
361                         } else {
362                                 $('#post_status option[value=publish]').html( postL10n.published );
363                         }
364                         $('.edit-post-status').show();
365                 }
366                 $('#post-status-display').html($('#post_status :selected').text());
367                 if ( $('#post_status :selected').val() == 'private' || $('#post_status :selected').val() == 'publish' ) {
368                         $('#save-post').hide();
369                 } else {
370                         $('#save-post').show();
371                         if ( $('#post_status :selected').val() == 'pending' ) {
372                                 $('#save-post').show().val( postL10n.savePending );
373                         } else {
374                                 $('#save-post').show().val( postL10n.saveDraft );
375                         }
376                 }
377         }
378
379         $('.edit-visibility').click(function () {
380                 if ($('#post-visibility-select').is(":hidden")) {
381                         updateVisibility();
382                         $('#post-visibility-select').slideDown("normal");
383                         $('.edit-visibility').hide();
384                 }
385                 return false;
386         });
387
388         $('.cancel-post-visibility').click(function () {
389                 $('#post-visibility-select').slideUp("normal");
390                 $('#visibility-radio-' + $('#hidden-post-visibility').val()).attr('checked', true);
391                 $('#post_password').val($('#hidden_post_password').val());
392                 $('#sticky').attr('checked', $('#hidden-post-sticky').attr('checked'));
393                 $('#post-visibility-display').html(visibility);
394                 $('.edit-visibility').show();
395                 updateText();
396                 return false;
397         });
398
399         $('.save-post-visibility').click(function () { // crazyhorse - multiple ok cancels
400                 $('#post-visibility-select').slideUp("normal");
401                 $('.edit-visibility').show();
402                 updateText();
403                 if ( $('#post-visibility-select input:radio:checked').val() != 'public' ) {
404                         $('#sticky').attr('checked', false);
405                 }
406
407                 if ( true == $('#sticky').attr('checked') ) {
408                         sticky = 'Sticky';
409                 } else {
410                         sticky = '';
411                 }
412
413                 $('#post-visibility-display').html(
414                         postL10n[$('#post-visibility-select input:radio:checked').val() + sticky]
415                 );
416
417                 return false;
418         });
419
420         $('#post-visibility-select input:radio').change(function() {
421                 updateVisibility();
422         });
423
424         $('.edit-timestamp').click(function () {
425                 if ($('#timestampdiv').is(":hidden")) {
426                         $('#timestampdiv').slideDown("normal");
427                         $('.edit-timestamp').hide();
428                 }
429
430                 return false;
431         });
432
433         $('.cancel-timestamp').click(function() {
434                 $('#timestampdiv').slideUp("normal");
435                 $('#mm').val($('#hidden_mm').val());
436                 $('#jj').val($('#hidden_jj').val());
437                 $('#aa').val($('#hidden_aa').val());
438                 $('#hh').val($('#hidden_hh').val());
439                 $('#mn').val($('#hidden_mn').val());
440                 $('.edit-timestamp').show();
441                 updateText();
442                 return false;
443         });
444
445         $('.save-timestamp').click(function () { // crazyhorse - multiple ok cancels
446                 $('#timestampdiv').slideUp("normal");
447                 $('.edit-timestamp').show();
448                 updateText();
449
450                 return false;
451         });
452
453         $('.edit-post-status').click(function() {
454                 if ($('#post-status-select').is(":hidden")) {
455                         $('#post-status-select').slideDown("normal");
456                         $(this).hide();
457                 }
458
459                 return false;
460         });
461
462         $('.save-post-status').click(function() {
463                 $('#post-status-select').slideUp("normal");
464                 $('.edit-post-status').show();
465                 updateText();
466                 return false;
467         });
468
469         $('.cancel-post-status').click(function() {
470                 $('#post-status-select').slideUp("normal");
471                 $('#post_status').val($('#hidden_post_status').val());
472                 $('.edit-post-status').show();
473                 updateText();
474                 return false;
475         });
476
477         // Custom Fields
478         $('#the-list').wpList( { addAfter: function( xml, s ) {
479                 $('table#list-table').show();
480                 if ( $.isFunction( autosave_update_post_ID ) ) {
481                         autosave_update_post_ID(s.parsed.responses[0].supplemental.postid);
482                 }
483         }, addBefore: function( s ) {
484                 s.data += '&post_id=' + $('#post_ID').val();
485                 return s;
486         }
487         });
488 });