1 // this file contains all the scripts used in the post/edit page
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];
9 jQuery.each( current_tags, function( key, val ) {
10 if ( val && !val.match(/^\s+$/) && '' != val ) {
11 new_tags = new_tags.concat( val );
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();
20 function tag_update_quickclicks() {
21 if ( jQuery( '#tags-input' ).length == 0 )
23 var current_tags = jQuery( '#tags-input' ).val().split(',');
24 jQuery( '#tagchecklist' ).empty();
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> ' + val + '</span> ';
31 jQuery( '#tagchecklist' ).append( txt );
32 jQuery( '#tag-check-' + key ).click( new_tag_remove_tag );
37 jQuery( '#tagchecklist' ).prepend( '<strong>'+postL10n.tagsUsed+'</strong><br />' );
40 function tag_flush_to_text(e,a) {
42 var text = a ? jQuery(a).text() : jQuery('#newtag').val();
43 var newtags = jQuery('#tags-input').val();
45 var t = text.replace( /\s*([^,]+).*/, '$1,' );
48 if ( newtags.indexOf(t) != -1 )
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();
58 jQuery('#newtag').val('');
59 jQuery('#newtag').focus();
64 function tag_save_on_publish() {
65 if ( jQuery('#newtag').val() != postL10n.addTag )
69 function tag_press_key( e ) {
70 if ( 13 == e.keyCode ) {
79 $('#tagcloud-link').click(function(){tagCloud.get(); $(this).unbind().click(function(){return false;}); return false;});
83 $.post('admin-ajax.php', {'action':'get-tagcloud'}, function(r, stat) {
84 if ( 0 == r || 'success' != stat )
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);
98 jQuery(document).ready( function($) {
101 // close postboxes that should be closed
102 jQuery('.if-js-closed').removeClass('if-js-closed').addClass('closed');
105 postboxes.add_postbox_toggles('post');
108 make_slugedit_clickable();
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' );
119 jQuery('#newtag').blur(function() {
120 if ( this.value == '' )
121 jQuery(this).val( postL10n.addTag ).addClass( 'form-input-tip' );
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 );
128 jQuery('#title').blur( function() { if ( (jQuery("#post_ID").val() > 0) || (jQuery("#title").val().length == 0) ) return; autosave(); } );
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 );
135 var categoryTabs =jQuery('#category-tabs').tabs();
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() {
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;
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();
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() )
170 var name = jQuery.trim( th.text() );
171 var o = jQuery( '<option value="' + parseInt( val, 10 ) + '"></option>' ).text( name );
172 newCatParent.prepend( o );
174 newCatParentOption.attr( 'selected', true );
177 jQuery('#categorychecklist').wpList( {
179 response: 'category-ajax-response',
180 addBefore: catAddBefore,
181 addAfter: catAddAfter
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();
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();
196 jQuery('.categorychecklist .popular-category :checkbox').change( syncChecks ).filter( ':checked' ).change();
197 var stamp = $('#timestamp').html();
198 var visibility = $('#post-visibility-display').html();
201 function updateVisibility() {
202 if ( $('#post-visibility-select input:radio:checked').val() != 'public' ) {
203 $('#sticky').attr('checked', false);
204 $('#sticky-span').hide();
206 $('#sticky-span').show();
208 if ( $('#post-visibility-select input:radio:checked').val() != 'password' ) {
209 $('#password-span').hide();
211 $('#password-span').show();
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 );
226 var publishOn = postL10n.publishOnPast;
227 $('#publish').val( postL10n.update );
229 if ( originalDate.toUTCString() == attemptedDate.toUTCString() ) { //hack
230 $('#timestamp').html(stamp);
232 $('#timestamp').html(
234 $( '#mm option[value=' + $('#mm').val() + ']' ).text() + ' ' +
235 $('#jj').val() + ', ' +
236 $('#aa').val() + ' @ ' +
237 $('#hh').val() + ':' +
238 $('#mn').val() + '</b> '
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>');
247 $('#post_status option[value=publish]').html( postL10n.privatelyPublished );
248 $('#post_status option[value=publish]').attr('selected', true);
249 $('.edit-post-status').hide();
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());
257 $('#post_status option[value=publish]').html( postL10n.published );
259 $('.edit-post-status').show();
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();
265 $('#save-post').show();
266 if ( $('#post_status :selected').val() == 'pending' ) {
267 $('#save-post').show().val( postL10n.savePending );
269 $('#save-post').show().val( postL10n.saveDraft );
274 $('.edit-visibility').click(function () {
275 if ($('#post-visibility-select').is(":hidden")) {
277 $('#post-visibility-select').slideDown("normal");
278 $('.edit-visibility').hide();
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();
294 $('.save-post-visibility').click(function () { // crazyhorse - multiple ok cancels
295 $('#post-visibility-select').slideUp("normal");
296 $('.edit-visibility').show();
298 if ( $('#post-visibility-select input:radio:checked').val() != 'public' ) {
299 $('#sticky').attr('checked', false);
302 if ( true == $('#sticky').attr('checked') ) {
308 $('#post-visibility-display').html(
309 postL10n[$('#post-visibility-select input:radio:checked').val() + sticky]
315 $('#post-visibility-select input:radio').change(function() {
319 $('.edit-timestamp').click(function () {
320 if ($('#timestampdiv').is(":hidden")) {
321 $('#timestampdiv').slideDown("normal");
322 $('.edit-timestamp').hide();
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();
340 $('.save-timestamp').click(function () { // crazyhorse - multiple ok cancels
341 $('#timestampdiv').slideUp("normal");
342 $('.edit-timestamp').show();
348 $('.edit-post-status').click(function() {
349 if ($('#post-status-select').is(":hidden")) {
350 $('#post-status-select').slideDown("normal");
357 $('.save-post-status').click(function() {
358 $('#post-status-select').slideUp("normal");
359 $('.edit-post-status').show();
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();
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);
378 }, addBefore: function( s ) {
379 s.data += '&post_id=' + jQuery('#post_ID').val();
385 $('#post-preview').click(function(e){
386 if ( 1 > $('#post_ID').val() && autosaveFirst ) {
387 autosaveDelayPreview = true;
392 $('input#wp-preview').val('dopreview');
393 $('form#post').attr('target', 'wp-preview').submit().attr('target', '');
394 $('input#wp-preview').val('');
404 get : function(total, num) {
411 $('.waiting').show();
414 'action' : 'get-comments',
416 '_ajax_nonce' : $('#add_comment_nonce').val(),
417 'post_ID' : $('#post_ID').val(),
422 $.post('admin-ajax.php', data,
424 var r = wpAjax.parseAjaxResponse(r);
425 $('#commentstatusdiv .widefat').show();
426 $('.waiting').hide();
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');
432 theList = theExtraList = null;
433 $("a[className*=':']").unbind();
436 if ( commentsBox.st > commentsBox.total )
437 $('#show-comments').hide();
439 $('#show-comments').html(postL10n.showcomm);
441 } else if ( 1 == r ) {
442 $('#show-comments').parent().html(postL10n.endcomm);
446 $('#the-comment-list').append('<tr><td colspan="5">'+wpAjax.broken+'</td></tr>');