]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/js/inline-edit-post.dev.js
Wordpress 3.3
[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).prop('checked') ) {
38                                 pw.val('').prop('disabled', true);
39                         } else {
40                                 pw.prop('disabled', false);
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').mousedown(function(e){
78                         t.revert();
79                         $('select[name^="action"]').val('-1');
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).prop('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 + '"]').prop('checked', false);
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="tax_input['+tax+']"]').suggest( 'admin-ajax.php?action=ajax-tag-search&tax='+tax, { delay: 500, minchars: 2, multiple: true, multipleSep: ", " } );
121                 }
122                 $('html, body').animate( { scrollTop: 0 }, 'fast' );
123         },
124
125         edit : function(id) {
126                 var t = this, fields, editRow, rowData, status, pageOpt, pageLevel, nextPage, pageLoop = true, nextLevel, cur_format, f;
127                 t.revert();
128
129                 if ( typeof(id) == 'object' )
130                         id = t.getId(id);
131
132                 fields = ['post_title', 'post_name', 'post_author', '_status', 'jj', 'mm', 'aa', 'hh', 'mn', 'ss', 'post_password', 'post_format'];
133                 if ( t.type == 'page' )
134                         fields.push('post_parent', 'menu_order', 'page_template');
135
136                 // add the new blank row
137                 editRow = $('#inline-edit').clone(true);
138                 $('td', editRow).attr('colspan', $('.widefat:first thead th:visible').length);
139
140                 if ( $(t.what+id).hasClass('alternate') )
141                         $(editRow).addClass('alternate');
142                 $(t.what+id).hide().after(editRow);
143
144                 // populate the data
145                 rowData = $('#inline_'+id);
146                 if ( !$(':input[name="post_author"] option[value="' + $('.post_author', rowData).text() + '"]', editRow).val() ) {
147                         // author no longer has edit caps, so we need to add them to the list of authors
148                         $(':input[name="post_author"]', editRow).prepend('<option value="' + $('.post_author', rowData).text() + '">' + $('#' + t.type + '-' + id + ' .author').text() + '</option>');
149                 }
150                 if ( $(':input[name="post_author"] option', editRow).length == 1 ) {
151                         $('label.inline-edit-author', editRow).hide();
152                 }
153
154                 // hide unsupported formats, but leave the current format alone
155                 cur_format = $('.post_format', rowData).text();
156                 $('option.unsupported', editRow).each(function() {
157                         var $this = $(this);
158                         if ( $this.val() != cur_format )
159                                 $this.remove();
160                 });
161
162                 for ( f = 0; f < fields.length; f++ ) {
163                         $(':input[name="' + fields[f] + '"]', editRow).val( $('.'+fields[f], rowData).text() );
164                 }
165
166                 if ( $('.comment_status', rowData).text() == 'open' )
167                         $('input[name="comment_status"]', editRow).prop("checked", true);
168                 if ( $('.ping_status', rowData).text() == 'open' )
169                         $('input[name="ping_status"]', editRow).prop("checked", true);
170                 if ( $('.sticky', rowData).text() == 'sticky' )
171                         $('input[name="sticky"]', editRow).prop("checked", true);
172
173                 // hierarchical taxonomies
174                 $('.post_category', rowData).each(function(){
175                         var term_ids = $(this).text();
176
177                         if ( term_ids ) {
178                                 taxname = $(this).attr('id').replace('_'+id, '');
179                                 $('ul.'+taxname+'-checklist :checkbox', editRow).val(term_ids.split(','));
180                         }
181                 });
182
183                 //flat taxonomies
184                 $('.tags_input', rowData).each(function(){
185                         var terms = $(this).text(),
186                                 taxname = $(this).attr('id').replace('_' + id, ''),
187                                 textarea = $('textarea.tax_input_' + taxname, editRow);
188
189                         if ( terms )
190                                 textarea.val(terms);
191
192                         textarea.suggest( 'admin-ajax.php?action=ajax-tag-search&tax='+taxname, { delay: 500, minchars: 2, multiple: true, multipleSep: ", " } );
193                 });
194
195                 // handle the post status
196                 status = $('._status', rowData).text();
197                 if ( 'future' != status )
198                         $('select[name="_status"] option[value="future"]', editRow).remove();
199
200                 if ( 'private' == status ) {
201                         $('input[name="keep_private"]', editRow).prop("checked", true);
202                         $('input.inline-edit-password-input').val('').prop('disabled', true);
203                 }
204
205                 // remove the current page and children from the parent dropdown
206                 pageOpt = $('select[name="post_parent"] option[value="' + id + '"]', editRow);
207                 if ( pageOpt.length > 0 ) {
208                         pageLevel = pageOpt[0].className.split('-')[1];
209                         nextPage = pageOpt;
210                         while ( pageLoop ) {
211                                 nextPage = nextPage.next('option');
212                                 if (nextPage.length == 0) break;
213                                 nextLevel = nextPage[0].className.split('-')[1];
214                                 if ( nextLevel <= pageLevel ) {
215                                         pageLoop = false;
216                                 } else {
217                                         nextPage.remove();
218                                         nextPage = pageOpt;
219                                 }
220                         }
221                         pageOpt.remove();
222                 }
223
224                 $(editRow).attr('id', 'edit-'+id).addClass('inline-editor').show();
225                 $('.ptitle', editRow).focus();
226
227                 return false;
228         },
229
230         save : function(id) {
231                 var params, fields, page = $('.post_status_page').val() || '';
232
233                 if ( typeof(id) == 'object' )
234                         id = this.getId(id);
235
236                 $('table.widefat .inline-edit-save .waiting').show();
237
238                 params = {
239                         action: 'inline-save',
240                         post_type: typenow,
241                         post_ID: id,
242                         edit_date: 'true',
243                         post_status: page
244                 };
245
246                 fields = $('#edit-'+id+' :input').serialize();
247                 params = fields + '&' + $.param(params);
248
249                 // make ajax request
250                 $.post('admin-ajax.php', params,
251                         function(r) {
252                                 $('table.widefat .inline-edit-save .waiting').hide();
253
254                                 if (r) {
255                                         if ( -1 != r.indexOf('<tr') ) {
256                                                 $(inlineEditPost.what+id).remove();
257                                                 $('#edit-'+id).before(r).remove();
258                                                 $(inlineEditPost.what+id).hide().fadeIn();
259                                         } else {
260                                                 r = r.replace( /<.[^<>]*?>/g, '' );
261                                                 $('#edit-'+id+' .inline-edit-save .error').html(r).show();
262                                         }
263                                 } else {
264                                         $('#edit-'+id+' .inline-edit-save .error').html(inlineEditL10n.error).show();
265                                 }
266                         }
267                 , 'html');
268                 return false;
269         },
270
271         revert : function(){
272                 var id = $('table.widefat tr.inline-editor').attr('id');
273
274                 if ( id ) {
275                         $('table.widefat .inline-edit-save .waiting').hide();
276
277                         if ( 'bulk-edit' == id ) {
278                                 $('table.widefat #bulk-edit').removeClass('inline-editor').hide();
279                                 $('#bulk-titles').html('');
280                                 $('#inlineedit').append( $('#bulk-edit') );
281                         } else {
282                                 $('#'+id).remove();
283                                 id = id.substr( id.lastIndexOf('-') + 1 );
284                                 $(this.what+id).show();
285                         }
286                 }
287
288                 return false;
289         },
290
291         getId : function(o) {
292                 var id = $(o).closest('tr').attr('id'),
293                         parts = id.split('-');
294                 return parts[parts.length - 1];
295         }
296 };
297
298 $(document).ready(function(){inlineEditPost.init();});
299 })(jQuery);