]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/js/post.js
Wordpress 2.5.1-scripts
[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         var current_tags = jQuery( '#tags-input' ).val().split(',');
22         jQuery( '#tagchecklist' ).empty();
23         shown = false;
24 //      jQuery.merge( current_tags, current_tags ); // this doesn't work anymore, need something to array_unique
25         jQuery.each( current_tags, function( key, val ) {
26                 val = val.replace( /^\s+/, '' ).replace( /\s+$/, '' ); // trim
27                 if ( !val.match(/^\s+$/) && '' != val ) {
28                         txt = '<span><a id="tag-check-' + key + '" class="ntdelbutton">X</a>&nbsp;' + val + '</span> ';
29                         jQuery( '#tagchecklist' ).append( txt );
30                         jQuery( '#tag-check-' + key ).click( new_tag_remove_tag );
31                         shown = true;
32                 }
33         });
34         if ( shown )
35                 jQuery( '#tagchecklist' ).prepend( '<strong>'+postL10n.tagsUsed+'</strong><br />' );
36 }
37
38 function tag_flush_to_text() {
39         var newtags = jQuery('#tags-input').val() + ',' + jQuery('#newtag').val();
40         // massage
41         newtags = newtags.replace( /\s+,+\s*/g, ',' ).replace( /,+/g, ',' ).replace( /,+\s+,+/g, ',' ).replace( /,+\s*$/g, '' ).replace( /^\s*,+/g, '' );
42         jQuery('#tags-input').val( newtags );
43         tag_update_quickclicks();
44         jQuery('#newtag').val('');
45         jQuery('#newtag').focus();
46         return false;
47 }
48
49 function tag_save_on_publish() {
50         if ( jQuery('#newtag').val() != postL10n.addTag )
51                 tag_flush_to_text();
52 }
53
54 function tag_press_key( e ) {
55         if ( 13 == e.keyCode ) {
56                 tag_flush_to_text();
57                 return false;
58         }
59 }
60
61 jQuery(document).ready( function() {
62         // close postboxes that should be closed
63         jQuery('.if-js-closed').removeClass('if-js-closed').addClass('closed');
64
65         // show things that should be visible, hide what should be hidden
66         jQuery('.hide-if-no-js').show();
67         jQuery('.hide-if-js').hide();
68
69         // postboxes
70         add_postbox_toggles('post');
71
72         // Editable slugs
73         make_slugedit_clickable();
74
75         // hide advanced slug field
76         jQuery('#slugdiv').hide();
77
78         jQuery('#tags-input').hide();
79         tag_update_quickclicks();
80         // add the quickadd form
81         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>');
82         jQuery('#tagadd').click( tag_flush_to_text );
83         jQuery('#newtag').focus(function() {
84                 if ( this.value == postL10n.addTag )
85                         jQuery(this).val( '' ).removeClass( 'form-input-tip' );
86         });
87         jQuery('#newtag').blur(function() {
88                 if ( this.value == '' )
89                         jQuery(this).val( postL10n.addTag ).addClass( 'form-input-tip' );
90         });
91
92         // auto-save tags on post save/publish
93         jQuery('#publish').click( tag_save_on_publish );
94         jQuery('#save-post').click( tag_save_on_publish );
95
96         jQuery('#title').blur( function() { if ( (jQuery("#post_ID").val() > 0) || (jQuery("#title").val().length == 0) ) return; autosave(); } );
97
98         // auto-suggest stuff
99         jQuery('#newtag').suggest( 'admin-ajax.php?action=ajax-tag-search', { delay: 500, minchars: 2 } );
100         jQuery('#newtag').keypress( tag_press_key );
101
102         // category tabs
103         var categoryTabs =jQuery('#category-tabs').tabs();
104
105         // Ajax Cat
106         var newCat = jQuery('#newcat').one( 'focus', function() { jQuery(this).val( '' ).removeClass( 'form-input-tip' ) } );
107         jQuery('#category-add-sumbit').click( function() { newCat.focus(); } );
108         var newCatParent = false;
109         var newCatParentOption = false;
110         var noSyncChecks = false; // prophylactic. necessary?
111         var syncChecks = function() {
112                 if ( noSyncChecks )
113                         return;
114                 noSyncChecks = true;
115                 var th = jQuery(this);
116                 var c = th.is(':checked');
117                 var id = th.val().toString();
118                 jQuery('#in-category-' + id + ', #in-popular-category-' + id).attr( 'checked', c );
119                 noSyncChecks = false;
120         };
121         var catAddBefore = function( s ) {
122                 s.data += '&' + jQuery( '#categorychecklist :checked' ).serialize();
123                 return s;
124         };
125         var catAddAfter = function( r, s ) {
126                 if ( !newCatParent ) newCatParent = jQuery('#newcat_parent');
127                 if ( !newCatParentOption ) newCatParentOption = newCatParent.find( 'option[value=-1]' );
128                 jQuery(s.what + ' response_data', r).each( function() {
129                         var t = jQuery(jQuery(this).text());
130                         t.find( 'label' ).each( function() {
131                                 var th = jQuery(this);
132                                 var val = th.find('input').val();
133                                 var id = th.find('input')[0].id
134                                 jQuery('#' + id).change( syncChecks ).change();
135                                 if ( newCatParent.find( 'option[value=' + val + ']' ).size() )
136                                         return;
137                                 var name = jQuery.trim( th.text() );
138                                 var o = jQuery( '<option value="' +  parseInt( val, 10 ) + '"></option>' ).text( name );
139                                 newCatParent.prepend( o );
140                         } );
141                         newCatParentOption.attr( 'selected', true );
142                 } );
143         };
144         jQuery('#categorychecklist').wpList( {
145                 alt: '',
146                 response: 'category-ajax-response',
147                 addBefore: catAddBefore,
148                 addAfter: catAddAfter
149         } );
150         jQuery('#category-add-toggle').click( function() {
151                 jQuery(this).parents('div:first').toggleClass( 'wp-hidden-children' );
152                 categoryTabs.tabsClick( 1 );
153                 jQuery('#newcat').focus();
154                 return false;
155         } );
156         jQuery('.categorychecklist .popular-category :checkbox').change( syncChecks ).filter( ':checked' ).change();
157
158         jQuery('.edit-timestamp').click(function () {
159                 if (jQuery('#timestampdiv').is(":hidden")) {
160                         jQuery('#timestampdiv').slideDown("normal");
161                         jQuery('.edit-timestamp').text(postL10n.cancel);
162                 } else {
163                         jQuery('#timestampdiv').hide();
164                         jQuery('#mm').val(jQuery('#hidden_mm').val());
165                         jQuery('#jj').val(jQuery('#hidden_jj').val());
166                         jQuery('#aa').val(jQuery('#hidden_aa').val());
167                         jQuery('#hh').val(jQuery('#hidden_hh').val());
168                         jQuery('#mn').val(jQuery('#hidden_mn').val());
169                         jQuery('.edit-timestamp').text(postL10n.edit);
170                 }
171                 return false;
172
173         });
174
175         // Custom Fields
176         jQuery('#the-list').wpList( { addAfter: function( xml, s ) {
177                 if ( jQuery.isFunction( autosave_update_post_ID ) ) {
178                         autosave_update_post_ID(s.parsed.responses[0].supplemental.postid);
179                 }
180         } });
181 });