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