]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/js/inline-edit-post.js
WordPress 3.6.1
[autoinstalls/wordpress.git] / wp-admin / js / inline-edit-post.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                 $('#the-list').on('click', 'a.editinline', 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                 $('select[name="_status"] option[value="future"]', bulkRow).remove();
57
58                 $('#doaction, #doaction2').click(function(e){
59                         var n = $(this).attr('id').substr(2);
60                         if ( $('select[name="'+n+'"]').val() == 'edit' ) {
61                                 e.preventDefault();
62                                 t.setBulk();
63                         } else if ( $('form#posts-filter tr.inline-editor').length > 0 ) {
64                                 t.revert();
65                         }
66                 });
67
68                 $('#post-query-submit').mousedown(function(e){
69                         t.revert();
70                         $('select[name^="action"]').val('-1');
71                 });
72         },
73
74         toggle : function(el){
75                 var t = this;
76                 $(t.what+t.getId(el)).css('display') == 'none' ? t.revert() : t.edit(el);
77         },
78
79         setBulk : function(){
80                 var te = '', type = this.type, tax, c = true;
81                 this.revert();
82
83                 $('#bulk-edit td').attr('colspan', $('.widefat:first thead th:visible').length);
84                 $('table.widefat tbody').prepend( $('#bulk-edit') );
85                 $('#bulk-edit').addClass('inline-editor').show();
86
87                 $('tbody th.check-column input[type="checkbox"]').each(function(i){
88                         if ( $(this).prop('checked') ) {
89                                 c = false;
90                                 var id = $(this).val(), theTitle;
91                                 theTitle = $('#inline_'+id+' .post_title').html() || inlineEditL10n.notitle;
92                                 te += '<div id="ttle'+id+'"><a id="_'+id+'" class="ntdelbutton" title="'+inlineEditL10n.ntdeltitle+'">X</a>'+theTitle+'</div>';
93                         }
94                 });
95
96                 if ( c )
97                         return this.revert();
98
99                 $('#bulk-titles').html(te);
100                 $('#bulk-titles a').click(function(){
101                         var id = $(this).attr('id').substr(1);
102
103                         $('table.widefat input[value="' + id + '"]').prop('checked', false);
104                         $('#ttle'+id).remove();
105                 });
106
107                 // enable autocomplete for tags
108                 if ( 'post' == type ) {
109                         // support multi taxonomies?
110                         tax = 'post_tag';
111                         $('tr.inline-editor textarea[name="tax_input['+tax+']"]').suggest( ajaxurl + '?action=ajax-tag-search&tax=' + tax, { delay: 500, minchars: 2, multiple: true, multipleSep: inlineEditL10n.comma + ' ' } );
112                 }
113                 $('html, body').animate( { scrollTop: 0 }, 'fast' );
114         },
115
116         edit : function(id) {
117                 var t = this, fields, editRow, rowData, status, pageOpt, pageLevel, nextPage, pageLoop = true, nextLevel, cur_format, f;
118                 t.revert();
119
120                 if ( typeof(id) == 'object' )
121                         id = t.getId(id);
122
123                 fields = ['post_title', 'post_name', 'post_author', '_status', 'jj', 'mm', 'aa', 'hh', 'mn', 'ss', 'post_password', 'post_format', 'menu_order'];
124                 if ( t.type == 'page' )
125                         fields.push('post_parent', 'page_template');
126
127                 // add the new blank row
128                 editRow = $('#inline-edit').clone(true);
129                 $('td', editRow).attr('colspan', $('.widefat:first thead th:visible').length);
130
131                 if ( $(t.what+id).hasClass('alternate') )
132                         $(editRow).addClass('alternate');
133                 $(t.what+id).hide().after(editRow);
134
135                 // populate the data
136                 rowData = $('#inline_'+id);
137                 if ( !$(':input[name="post_author"] option[value="' + $('.post_author', rowData).text() + '"]', editRow).val() ) {
138                         // author no longer has edit caps, so we need to add them to the list of authors
139                         $(':input[name="post_author"]', editRow).prepend('<option value="' + $('.post_author', rowData).text() + '">' + $('#' + t.type + '-' + id + ' .author').text() + '</option>');
140                 }
141                 if ( $(':input[name="post_author"] option', editRow).length == 1 ) {
142                         $('label.inline-edit-author', editRow).hide();
143                 }
144
145                 // hide unsupported formats, but leave the current format alone
146                 cur_format = $('.post_format', rowData).text();
147                 $('option.unsupported', editRow).each(function() {
148                         var $this = $(this);
149                         if ( $this.val() != cur_format )
150                                 $this.remove();
151                 });
152
153                 for ( 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).prop("checked", true);
159                 if ( $('.ping_status', rowData).text() == 'open' )
160                         $('input[name="ping_status"]', editRow).prop("checked", true);
161                 if ( $('.sticky', rowData).text() == 'sticky' )
162                         $('input[name="sticky"]', editRow).prop("checked", true);
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
174                 //flat taxonomies
175                 $('.tags_input', rowData).each(function(){
176                         var terms = $(this).text(),
177                                 taxname = $(this).attr('id').replace('_' + id, ''),
178                                 textarea = $('textarea.tax_input_' + taxname, editRow),
179                                 comma = inlineEditL10n.comma;
180
181                         if ( terms ) {
182                                 if ( ',' !== comma )
183                                         terms = terms.replace(/,/g, comma);
184                                 textarea.val(terms);
185                         }
186
187                         textarea.suggest( ajaxurl + '?action=ajax-tag-search&tax=' + taxname, { delay: 500, minchars: 2, multiple: true, multipleSep: inlineEditL10n.comma + ' ' } );
188                 });
189
190                 // handle the post status
191                 status = $('._status', rowData).text();
192                 if ( 'future' != status )
193                         $('select[name="_status"] option[value="future"]', editRow).remove();
194
195                 if ( 'private' == status ) {
196                         $('input[name="keep_private"]', editRow).prop("checked", true);
197                         $('input.inline-edit-password-input').val('').prop('disabled', true);
198                 }
199
200                 // remove the current page and children from the parent dropdown
201                 pageOpt = $('select[name="post_parent"] option[value="' + id + '"]', editRow);
202                 if ( pageOpt.length > 0 ) {
203                         pageLevel = pageOpt[0].className.split('-')[1];
204                         nextPage = pageOpt;
205                         while ( pageLoop ) {
206                                 nextPage = nextPage.next('option');
207                                 if (nextPage.length == 0) break;
208                                 nextLevel = nextPage[0].className.split('-')[1];
209                                 if ( nextLevel <= pageLevel ) {
210                                         pageLoop = false;
211                                 } else {
212                                         nextPage.remove();
213                                         nextPage = pageOpt;
214                                 }
215                         }
216                         pageOpt.remove();
217                 }
218
219                 $(editRow).attr('id', 'edit-'+id).addClass('inline-editor').show();
220                 $('.ptitle', editRow).focus();
221
222                 return false;
223         },
224
225         save : function(id) {
226                 var params, fields, page = $('.post_status_page').val() || '';
227
228                 if ( typeof(id) == 'object' )
229                         id = this.getId(id);
230
231                 $('table.widefat .spinner').show();
232
233                 params = {
234                         action: 'inline-save',
235                         post_type: typenow,
236                         post_ID: id,
237                         edit_date: 'true',
238                         post_status: page
239                 };
240
241                 fields = $('#edit-'+id+' :input').serialize();
242                 params = fields + '&' + $.param(params);
243
244                 // make ajax request
245                 $.post( ajaxurl, params,
246                         function(r) {
247                                 $('table.widefat .spinner').hide();
248
249                                 if (r) {
250                                         if ( -1 != r.indexOf('<tr') ) {
251                                                 $(inlineEditPost.what+id).remove();
252                                                 $('#edit-'+id).before(r).remove();
253                                                 $(inlineEditPost.what+id).hide().fadeIn();
254                                         } else {
255                                                 r = r.replace( /<.[^<>]*?>/g, '' );
256                                                 $('#edit-'+id+' .inline-edit-save .error').html(r).show();
257                                         }
258                                 } else {
259                                         $('#edit-'+id+' .inline-edit-save .error').html(inlineEditL10n.error).show();
260                                 }
261                         }
262                 , 'html');
263                 return false;
264         },
265
266         revert : function(){
267                 var id = $('table.widefat tr.inline-editor').attr('id');
268
269                 if ( id ) {
270                         $('table.widefat .spinner').hide();
271
272                         if ( 'bulk-edit' == id ) {
273                                 $('table.widefat #bulk-edit').removeClass('inline-editor').hide();
274                                 $('#bulk-titles').html('');
275                                 $('#inlineedit').append( $('#bulk-edit') );
276                         } else {
277                                 $('#'+id).remove();
278                                 id = id.substr( id.lastIndexOf('-') + 1 );
279                                 $(this.what+id).show();
280                         }
281                 }
282
283                 return false;
284         },
285
286         getId : function(o) {
287                 var id = $(o).closest('tr').attr('id'),
288                         parts = id.split('-');
289                 return parts[parts.length - 1];
290         }
291 };
292
293 $( document ).ready( function(){ inlineEditPost.init(); } );
294
295 // Show/hide locks on posts
296 $( document ).on( 'heartbeat-tick.wp-check-locked-posts', function( e, data ) {
297         var locked = data['wp-check-locked-posts'] || {};
298
299         $('#the-list tr').each( function(i, el) {
300                 var key = el.id, row = $(el), lock_data, avatar;
301
302                 if ( locked.hasOwnProperty( key ) ) {
303                         if ( ! row.hasClass('wp-locked') ) {
304                                 lock_data = locked[key];
305                                 row.find('.column-title .locked-text').text( lock_data.text );
306                                 row.find('.check-column checkbox').prop('checked', false);
307
308                                 if ( lock_data.avatar_src ) {
309                                         avatar = $('<img class="avatar avatar-18 photo" width="18" height="18" />').attr( 'src', lock_data.avatar_src.replace(/&amp;/g, '&') );
310                                         row.find('.column-title .locked-avatar').empty().append( avatar );
311                                 }
312                                 row.addClass('wp-locked');
313                         }
314                 } else if ( row.hasClass('wp-locked') ) {
315                         // Make room for the CSS animation
316                         row.removeClass('wp-locked').delay(1000).find('.locked-info span').empty();
317                 }
318         });
319 }).on( 'heartbeat-send.wp-check-locked-posts', function( e, data ) {
320         var check = [];
321
322         $('#the-list tr').each( function(i, el) {
323                 if ( el.id )
324                         check.push( el.id );
325         });
326
327         if ( check.length )
328                 data['wp-check-locked-posts'] = check;
329 });
330
331 }(jQuery));