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