1 /* global inlineEditL10n, ajaxurl, typenow */
2 window.wp = window.wp || {};
9 var t = this, qeRow = $('#inline-edit'), bulkRow = $('#bulk-edit');
11 t.type = $('table.widefat').hasClass('pages') ? 'page' : 'post';
14 // prepare the edit rows
15 qeRow.keyup(function(e){
16 if ( e.which === 27 ) {
17 return inlineEditPost.revert();
20 bulkRow.keyup(function(e){
21 if ( e.which === 27 ) {
22 return inlineEditPost.revert();
26 $( '.cancel', qeRow ).click( function() {
27 return inlineEditPost.revert();
29 $( '.save', qeRow ).click( function() {
30 return inlineEditPost.save(this);
32 $('td', qeRow).keydown(function(e){
33 if ( e.which === 13 && ! $( e.target ).hasClass( 'cancel' ) ) {
34 return inlineEditPost.save(this);
38 $( '.cancel', bulkRow ).click( function() {
39 return inlineEditPost.revert();
42 $('#inline-edit .inline-edit-private input[value="private"]').click( function(){
43 var pw = $('input.inline-edit-password-input');
44 if ( $(this).prop('checked') ) {
45 pw.val('').prop('disabled', true);
47 pw.prop('disabled', false);
52 $('#the-list').on( 'click', 'a.editinline', function( e ) {
54 inlineEditPost.edit(this);
57 $('#bulk-edit').find('fieldset:first').after(
58 $('#inline-edit fieldset.inline-edit-categories').clone()
59 ).siblings( 'fieldset:last' ).prepend(
60 $('#inline-edit label.inline-edit-tags').clone()
63 $('select[name="_status"] option[value="future"]', bulkRow).remove();
65 $('#doaction, #doaction2').click(function(e){
68 t.whichBulkButtonId = $( this ).attr( 'id' );
69 n = t.whichBulkButtonId.substr( 2 );
71 if ( 'edit' === $( 'select[name="' + n + '"]' ).val() ) {
74 } else if ( $('form#posts-filter tr.inline-editor').length > 0 ) {
80 toggle : function(el){
82 $( t.what + t.getId( el ) ).css( 'display' ) === 'none' ? t.revert() : t.edit( el );
86 var te = '', type = this.type, c = true;
89 $( '#bulk-edit td' ).attr( 'colspan', $( 'th:visible, td:visible', '.widefat:first thead' ).length );
90 // Insert the editor at the top of the table with an empty row above to maintain zebra striping.
91 $('table.widefat tbody').prepend( $('#bulk-edit') ).prepend('<tr class="hidden"></tr>');
92 $('#bulk-edit').addClass('inline-editor').show();
94 $( 'tbody th.check-column input[type="checkbox"]' ).each( function() {
95 if ( $(this).prop('checked') ) {
97 var id = $(this).val(), theTitle;
98 theTitle = $('#inline_'+id+' .post_title').html() || inlineEditL10n.notitle;
99 te += '<div id="ttle'+id+'"><a id="_'+id+'" class="ntdelbutton" title="'+inlineEditL10n.ntdeltitle+'">X</a>'+theTitle+'</div>';
104 return this.revert();
107 $('#bulk-titles').html(te);
108 $('#bulk-titles a').click(function(){
109 var id = $(this).attr('id').substr(1);
111 $('table.widefat input[value="' + id + '"]').prop('checked', false);
112 $('#ttle'+id).remove();
115 // enable autocomplete for tags
116 if ( 'post' === type ) {
117 $( 'tr.inline-editor textarea[data-wp-taxonomy]' ).each( function ( i, element ) {
118 $( element ).wpTagsSuggest();
121 $('html, body').animate( { scrollTop: 0 }, 'fast' );
124 edit : function(id) {
125 var t = this, fields, editRow, rowData, status, pageOpt, pageLevel, nextPage, pageLoop = true, nextLevel, f, val, pw;
128 if ( typeof(id) === 'object' ) {
132 fields = ['post_title', 'post_name', 'post_author', '_status', 'jj', 'mm', 'aa', 'hh', 'mn', 'ss', 'post_password', 'post_format', 'menu_order', 'page_template'];
133 if ( t.type === 'page' ) {
134 fields.push('post_parent');
137 // add the new edit row with an extra blank row underneath to maintain zebra striping.
138 editRow = $('#inline-edit').clone(true);
139 $( 'td', editRow ).attr( 'colspan', $( 'th:visible, td:visible', '.widefat:first thead' ).length );
141 $(t.what+id).removeClass('is-expanded').hide().after(editRow).after('<tr class="hidden"></tr>');
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>');
149 if ( $( ':input[name="post_author"] option', editRow ).length === 1 ) {
150 $('label.inline-edit-author', editRow).hide();
153 for ( f = 0; f < fields.length; f++ ) {
154 val = $('.'+fields[f], rowData);
156 val.find( 'img' ).replaceWith( function() { return this.alt; } );
158 $(':input[name="' + fields[f] + '"]', editRow).val( val );
161 if ( $( '.comment_status', rowData ).text() === 'open' ) {
162 $( 'input[name="comment_status"]', editRow ).prop( 'checked', true );
164 if ( $( '.ping_status', rowData ).text() === 'open' ) {
165 $( 'input[name="ping_status"]', editRow ).prop( 'checked', true );
167 if ( $( '.sticky', rowData ).text() === 'sticky' ) {
168 $( 'input[name="sticky"]', editRow ).prop( 'checked', true );
171 // hierarchical taxonomies
172 $('.post_category', rowData).each(function(){
174 term_ids = $(this).text();
177 taxname = $(this).attr('id').replace('_'+id, '');
178 $('ul.'+taxname+'-checklist :checkbox', editRow).val(term_ids.split(','));
183 $('.tags_input', rowData).each(function(){
185 taxname = $(this).attr('id').replace('_' + id, ''),
186 textarea = $('textarea.tax_input_' + taxname, editRow),
187 comma = inlineEditL10n.comma;
189 terms.find( 'img' ).replaceWith( function() { return this.alt; } );
190 terms = terms.text();
193 if ( ',' !== comma ) {
194 terms = terms.replace(/,/g, comma);
199 textarea.wpTagsSuggest();
202 // handle the post status
203 status = $('._status', rowData).text();
204 if ( 'future' !== status ) {
205 $('select[name="_status"] option[value="future"]', editRow).remove();
208 pw = $( '.inline-edit-password-input' ).prop( 'disabled', false );
209 if ( 'private' === status ) {
210 $('input[name="keep_private"]', editRow).prop('checked', true);
211 pw.val( '' ).prop( 'disabled', true );
214 // remove the current page and children from the parent dropdown
215 pageOpt = $('select[name="post_parent"] option[value="' + id + '"]', editRow);
216 if ( pageOpt.length > 0 ) {
217 pageLevel = pageOpt[0].className.split('-')[1];
220 nextPage = nextPage.next('option');
221 if ( nextPage.length === 0 ) {
225 nextLevel = nextPage[0].className.split('-')[1];
227 if ( nextLevel <= pageLevel ) {
237 $(editRow).attr('id', 'edit-'+id).addClass('inline-editor').show();
238 $('.ptitle', editRow).focus();
243 // Ajax saving is only for Quick Edit.
244 save : function(id) {
245 var params, fields, page = $('.post_status_page').val() || '';
247 if ( typeof(id) === 'object' ) {
251 $( 'table.widefat .spinner' ).addClass( 'is-active' );
254 action: 'inline-save',
261 fields = $('#edit-'+id).find(':input').serialize();
262 params = fields + '&' + $.param(params);
265 $.post( ajaxurl, params,
267 var $errorSpan = $( '#edit-' + id + ' .inline-edit-save .error' );
269 $( 'table.widefat .spinner' ).removeClass( 'is-active' );
270 $( '.ac_results' ).hide();
273 if ( -1 !== r.indexOf( '<tr' ) ) {
274 $(inlineEditPost.what+id).siblings('tr.hidden').addBack().remove();
275 $('#edit-'+id).before(r).remove();
276 $( inlineEditPost.what + id ).hide().fadeIn( 400, function() {
277 // Move focus back to the Quick Edit link. $( this ) is the row being animated.
278 $( this ).find( '.editinline' ).focus();
279 wp.a11y.speak( inlineEditL10n.saved );
282 r = r.replace( /<.[^<>]*?>/g, '' );
283 $errorSpan.html( r ).show();
284 wp.a11y.speak( $errorSpan.text() );
287 $errorSpan.html( inlineEditL10n.error ).show();
288 wp.a11y.speak( inlineEditL10n.error );
292 // Prevent submitting the form when pressing Enter on a focused field.
296 // Revert is for both Quick Edit and Bulk Edit.
298 var $tableWideFat = $( '.widefat' ),
299 id = $( '.inline-editor', $tableWideFat ).attr( 'id' );
302 $( '.spinner', $tableWideFat ).removeClass( 'is-active' );
303 $( '.ac_results' ).hide();
305 if ( 'bulk-edit' === id ) {
306 $( '#bulk-edit', $tableWideFat ).removeClass( 'inline-editor' ).hide().siblings( '.hidden' ).remove();
307 $('#bulk-titles').empty();
308 $('#inlineedit').append( $('#bulk-edit') );
309 // Move focus back to the Bulk Action button that was activated.
310 $( '#' + inlineEditPost.whichBulkButtonId ).focus();
312 $('#'+id).siblings('tr.hidden').addBack().remove();
313 id = id.substr( id.lastIndexOf('-') + 1 );
314 // Show the post row and move focus back to the Quick Edit link.
315 $( this.what + id ).show().find( '.editinline' ).focus();
322 getId : function(o) {
323 var id = $(o).closest('tr').attr('id'),
324 parts = id.split('-');
325 return parts[parts.length - 1];
329 $( document ).ready( function(){ inlineEditPost.init(); } );
331 // Show/hide locks on posts
332 $( document ).on( 'heartbeat-tick.wp-check-locked-posts', function( e, data ) {
333 var locked = data['wp-check-locked-posts'] || {};
335 $('#the-list tr').each( function(i, el) {
336 var key = el.id, row = $(el), lock_data, avatar;
338 if ( locked.hasOwnProperty( key ) ) {
339 if ( ! row.hasClass('wp-locked') ) {
340 lock_data = locked[key];
341 row.find('.column-title .locked-text').text( lock_data.text );
342 row.find('.check-column checkbox').prop('checked', false);
344 if ( lock_data.avatar_src ) {
345 avatar = $( '<img class="avatar avatar-18 photo" width="18" height="18" alt="" />' ).attr( 'src', lock_data.avatar_src.replace( /&/g, '&' ) );
346 row.find('.column-title .locked-avatar').empty().append( avatar );
348 row.addClass('wp-locked');
350 } else if ( row.hasClass('wp-locked') ) {
351 // Make room for the CSS animation
352 row.removeClass('wp-locked').delay(1000).find('.locked-info span').empty();
355 }).on( 'heartbeat-send.wp-check-locked-posts', function( e, data ) {
358 $('#the-list tr').each( function(i, el) {
364 if ( check.length ) {
365 data['wp-check-locked-posts'] = check;
367 }).ready( function() {
368 // Set the heartbeat interval to 15 sec.
369 if ( typeof wp !== 'undefined' && wp.heartbeat ) {
370 wp.heartbeat.interval( 15 );
374 })( jQuery, window.wp );