]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/js/inline-edit-post.dev.js
Wordpress 3.1
[autoinstalls/wordpress.git] / wp-admin / js / inline-edit-post.dev.js
1 (function($) {
2 inlineEditPost = {
3
4         init : function(){
5                 var t = this, qeRow = $('#inline-edit'), bulkRow = $('#bulk-edit');
6
7                 t.type = $('table.widefat').hasClass('pages') ? 'page' : 'post';
8                 t.what = '#post-';
9
10                 // prepare the edit rows
11                 qeRow.keyup(function(e){
12                         if (e.which == 27)
13                                 return inlineEditPost.revert();
14                 });
15                 bulkRow.keyup(function(e){
16                         if (e.which == 27)
17                                 return inlineEditPost.revert();
18                 });
19
20                 $('a.cancel', qeRow).click(function(){
21                         return inlineEditPost.revert();
22                 });
23                 $('a.save', qeRow).click(function(){
24                         return inlineEditPost.save(this);
25                 });
26                 $('td', qeRow).keydown(function(e){
27                         if ( e.which == 13 )
28                                 return inlineEditPost.save(this);
29                 });
30
31                 $('a.cancel', bulkRow).click(function(){
32                         return inlineEditPost.revert();
33                 });
34
35                 $('#inline-edit .inline-edit-private input[value=private]').click( function(){
36                         var pw = $('input.inline-edit-password-input');
37                         if ( $(this).attr('checked') ) {
38                                 pw.val('').attr('disabled', 'disabled');
39                         } else {
40                                 pw.attr('disabled', '');
41                         }
42                 });
43
44                 // add events
45                 $('a.editinline').live('click', function(){
46                         inlineEditPost.edit(this);
47                         return false;
48                 });
49
50                 $('#bulk-title-div').parents('fieldset').after(
51                         $('#inline-edit fieldset.inline-edit-categories').clone()
52                 ).siblings( 'fieldset:last' ).prepend(
53                         $('#inline-edit label.inline-edit-tags').clone()
54                 );
55
56                 // hiearchical taxonomies expandable?
57                 $('span.catshow').click(function(){
58                         $(this).hide().next().show().parent().next().addClass("cat-hover");
59                 });
60
61                 $('span.cathide').click(function(){
62                         $(this).hide().prev().show().parent().next().removeClass("cat-hover");
63                 });
64
65                 $('select[name="_status"] option[value="future"]', bulkRow).remove();
66
67                 $('#doaction, #doaction2').click(function(e){
68                         var n = $(this).attr('id').substr(2);
69                         if ( $('select[name="'+n+'"]').val() == 'edit' ) {
70                                 e.preventDefault();
71                                 t.setBulk();
72                         } else if ( $('form#posts-filter tr.inline-editor').length > 0 ) {
73                                 t.revert();
74                         }
75                 });
76
77                 $('#post-query-submit').click(function(e){
78                         if ( $('form#posts-filter tr.inline-editor').length > 0 )
79                                 t.revert();
80                 });
81         },
82
83         toggle : function(el){
84                 var t = this;
85                 $(t.what+t.getId(el)).css('display') == 'none' ? t.revert() : t.edit(el);
86         },
87
88         setBulk : function(){
89                 var te = '', type = this.type, tax, c = true;
90                 this.revert();
91
92                 $('#bulk-edit td').attr('colspan', $('.widefat:first thead th:visible').length);
93                 $('table.widefat tbody').prepend( $('#bulk-edit') );
94                 $('#bulk-edit').addClass('inline-editor').show();
95
96                 $('tbody th.check-column input[type="checkbox"]').each(function(i){
97                         if ( $(this).attr('checked') ) {
98                                 c = false;
99                                 var id = $(this).val(), theTitle;
100                                 theTitle = $('#inline_'+id+' .post_title').text() || inlineEditL10n.notitle;
101                                 te += '<div id="ttle'+id+'"><a id="_'+id+'" class="ntdelbutton" title="'+inlineEditL10n.ntdeltitle+'">X</a>'+theTitle+'</div>';
102                         }
103                 });
104
105                 if ( c )
106                         return this.revert();
107
108                 $('#bulk-titles').html(te);
109                 $('#bulk-titles a').click(function(){
110                         var id = $(this).attr('id').substr(1);
111
112                         $('table.widefat input[value="'+id+'"]').attr('checked', '');
113                         $('#ttle'+id).remove();
114                 });
115
116                 // enable autocomplete for tags
117                 if ( 'post' == type ) {
118                         // support multi taxonomies?
119                         tax = 'post_tag';
120                         $('tr.inline-editor textarea[name="tags_input"]').suggest( 'admin-ajax.php?action=ajax-tag-search&tax='+tax, { delay: 500, minchars: 2, multiple: true, multipleSep: ", " } );
121                 }
122         },
123
124         edit : function(id) {
125                 var t = this, fields, editRow, rowData, cats, status, pageOpt, pageLevel, nextPage, pageLoop = true, nextLevel, tax;
126                 t.revert();
127
128                 if ( typeof(id) == 'object' )
129                         id = t.getId(id);
130
131                 fields = ['post_title', 'post_name', 'post_author', '_status', 'jj', 'mm', 'aa', 'hh', 'mn', 'ss', 'post_password'];
132                 if ( t.type == 'page' )
133                         fields.push('post_parent', 'menu_order', 'page_template');
134
135                 // add the new blank row
136                 editRow = $('#inline-edit').clone(true);
137                 $('td', editRow).attr('colspan', $('.widefat:first thead th:visible').length);
138
139                 if ( $(t.what+id).hasClass('alternate') )
140                         $(editRow).addClass('alternate');
141                 $(t.what+id).hide().after(editRow);
142
143                 // populate the data
144                 rowData = $('#inline_'+id);
145                 if ( !$(':input[name="post_author"] option[value=' + $('.post_author', rowData).text() + ']', editRow).val() ) {
146                         // author no longer has edit caps, so we need to add them to the list of authors
147                         $(':input[name="post_author"]', editRow).prepend('<option value="' + $('.post_author', rowData).text() + '">' + $('#' + t.type + '-' + id + ' .author').text() + '</option>');
148                 }
149                 if ( $(':input[name="post_author"] option', editRow).length == 1 ) {
150                         $('label.inline-edit-author', editRow).hide();
151                 }
152
153                 for ( var f = 0; f < fields.length; f++ ) {
154                         $(':input[name="'+fields[f]+'"]', editRow).val( $('.'+fields[f], rowData).text() );
155                 }
156
157                 if ( $('.comment_status', rowData).text() == 'open' )
158                         $('input[name="comment_status"]', editRow).attr("checked", "checked");
159                 if ( $('.ping_status', rowData).text() == 'open' )
160                         $('input[name="ping_status"]', editRow).attr("checked", "checked");
161                 if ( $('.sticky', rowData).text() == 'sticky' )
162                         $('input[name="sticky"]', editRow).attr("checked", "checked");
163
164                 // hierarchical taxonomies
165                 $('.post_category', rowData).each(function(){
166                         var term_ids = $(this).text();
167
168                         if ( term_ids ) {
169                                 taxname = $(this).attr('id').replace('_'+id, '');
170                                 $('ul.'+taxname+'-checklist :checkbox', editRow).val(term_ids.split(','));
171                         }
172                 });
173                 //flat taxonomies
174                 $('.tags_input', rowData).each(function(){
175                         var terms = $(this).text();
176
177                         if ( terms ) {
178                                 taxname = $(this).attr('id').replace('_'+id, '');
179                                 $('textarea.tax_input_'+taxname, editRow).val(terms);
180                                 $('textarea.tax_input_'+taxname, editRow).suggest( 'admin-ajax.php?action=ajax-tag-search&tax='+taxname, { delay: 500, minchars: 2, multiple: true, multipleSep: ", " } );
181                         }
182                 });
183
184
185                 // handle the post status
186                 status = $('._status', rowData).text();
187                 if ( 'future' != status )
188                         $('select[name="_status"] option[value="future"]', editRow).remove();
189
190                 if ( 'private' == status ) {
191                         $('input[name="keep_private"]', editRow).attr("checked", "checked");
192                         $('input.inline-edit-password-input').val('').attr('disabled', 'disabled');
193                 }
194
195                 // remove the current page and children from the parent dropdown
196                 pageOpt = $('select[name="post_parent"] option[value="'+id+'"]', editRow);
197                 if ( pageOpt.length > 0 ) {
198                         pageLevel = pageOpt[0].className.split('-')[1];
199                         nextPage = pageOpt;
200                         while ( pageLoop ) {
201                                 nextPage = nextPage.next('option');
202                                 if (nextPage.length == 0) break;
203                                 nextLevel = nextPage[0].className.split('-')[1];
204                                 if ( nextLevel <= pageLevel ) {
205                                         pageLoop = false;
206                                 } else {
207                                         nextPage.remove();
208                                         nextPage = pageOpt;
209                                 }
210                         }
211                         pageOpt.remove();
212                 }
213
214                 $(editRow).attr('id', 'edit-'+id).addClass('inline-editor').show();
215                 $('.ptitle', editRow).focus();
216
217                 return false;
218         },
219
220         save : function(id) {
221                 var params, fields, page = $('.post_status_page').val() || '';
222
223                 if ( typeof(id) == 'object' )
224                         id = this.getId(id);
225
226                 $('table.widefat .inline-edit-save .waiting').show();
227
228                 params = {
229                         action: 'inline-save',
230                         post_type: typenow,
231                         post_ID: id,
232                         edit_date: 'true',
233                         post_status: page
234                 };
235
236                 fields = $('#edit-'+id+' :input').serialize();
237                 params = fields + '&' + $.param(params);
238
239                 // make ajax request
240                 $.post('admin-ajax.php', params,
241                         function(r) {
242                                 $('table.widefat .inline-edit-save .waiting').hide();
243
244                                 if (r) {
245                                         if ( -1 != r.indexOf('<tr') ) {
246                                                 $(inlineEditPost.what+id).remove();
247                                                 $('#edit-'+id).before(r).remove();
248                                                 $(inlineEditPost.what+id).hide().fadeIn();
249                                         } else {
250                                                 r = r.replace( /<.[^<>]*?>/g, '' );
251                                                 $('#edit-'+id+' .inline-edit-save').append('<span class="error">'+r+'</span>');
252                                         }
253                                 } else {
254                                         $('#edit-'+id+' .inline-edit-save').append('<span class="error">'+inlineEditL10n.error+'</span>');
255                                 }
256                         }
257                 , 'html');
258                 return false;
259         },
260
261         revert : function(){
262                 var id = $('table.widefat tr.inline-editor').attr('id');
263
264                 if ( id ) {
265                         $('table.widefat .inline-edit-save .waiting').hide();
266
267                         if ( 'bulk-edit' == id ) {
268                                 $('table.widefat #bulk-edit').removeClass('inline-editor').hide();
269                                 $('#bulk-titles').html('');
270                                 $('#inlineedit').append( $('#bulk-edit') );
271                         } else {
272                                 $('#'+id).remove();
273                                 id = id.substr( id.lastIndexOf('-') + 1 );
274                                 $(this.what+id).show();
275                         }
276                 }
277
278                 return false;
279         },
280
281         getId : function(o) {
282                 var id = $(o).closest('tr').attr('id'),
283                         parts = id.split('-');
284                 return parts[parts.length - 1];
285         }
286 };
287
288 $(document).ready(function(){inlineEditPost.init();});
289 })(jQuery);