You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

375 lines
11 KiB

  1. /* global inlineEditL10n, ajaxurl, typenow */
  2. window.wp = window.wp || {};
  3. var inlineEditPost;
  4. ( function( $, wp ) {
  5. inlineEditPost = {
  6. init : function(){
  7. var t = this, qeRow = $('#inline-edit'), bulkRow = $('#bulk-edit');
  8. t.type = $('table.widefat').hasClass('pages') ? 'page' : 'post';
  9. t.what = '#post-';
  10. // prepare the edit rows
  11. qeRow.keyup(function(e){
  12. if ( e.which === 27 ) {
  13. return inlineEditPost.revert();
  14. }
  15. });
  16. bulkRow.keyup(function(e){
  17. if ( e.which === 27 ) {
  18. return inlineEditPost.revert();
  19. }
  20. });
  21. $( '.cancel', qeRow ).click( function() {
  22. return inlineEditPost.revert();
  23. });
  24. $( '.save', qeRow ).click( function() {
  25. return inlineEditPost.save(this);
  26. });
  27. $('td', qeRow).keydown(function(e){
  28. if ( e.which === 13 && ! $( e.target ).hasClass( 'cancel' ) ) {
  29. return inlineEditPost.save(this);
  30. }
  31. });
  32. $( '.cancel', bulkRow ).click( function() {
  33. return inlineEditPost.revert();
  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. // add events
  44. $('#the-list').on( 'click', 'a.editinline', function( e ) {
  45. e.preventDefault();
  46. inlineEditPost.edit(this);
  47. });
  48. $('#bulk-edit').find('fieldset:first').after(
  49. $('#inline-edit fieldset.inline-edit-categories').clone()
  50. ).siblings( 'fieldset:last' ).prepend(
  51. $('#inline-edit label.inline-edit-tags').clone()
  52. );
  53. $('select[name="_status"] option[value="future"]', bulkRow).remove();
  54. $('#doaction, #doaction2').click(function(e){
  55. var n;
  56. t.whichBulkButtonId = $( this ).attr( 'id' );
  57. n = t.whichBulkButtonId.substr( 2 );
  58. if ( 'edit' === $( 'select[name="' + n + '"]' ).val() ) {
  59. e.preventDefault();
  60. t.setBulk();
  61. } else if ( $('form#posts-filter tr.inline-editor').length > 0 ) {
  62. t.revert();
  63. }
  64. });
  65. },
  66. toggle : function(el){
  67. var t = this;
  68. $( t.what + t.getId( el ) ).css( 'display' ) === 'none' ? t.revert() : t.edit( el );
  69. },
  70. setBulk : function(){
  71. var te = '', type = this.type, c = true;
  72. this.revert();
  73. $( '#bulk-edit td' ).attr( 'colspan', $( 'th:visible, td:visible', '.widefat:first thead' ).length );
  74. // Insert the editor at the top of the table with an empty row above to maintain zebra striping.
  75. $('table.widefat tbody').prepend( $('#bulk-edit') ).prepend('<tr class="hidden"></tr>');
  76. $('#bulk-edit').addClass('inline-editor').show();
  77. $( 'tbody th.check-column input[type="checkbox"]' ).each( function() {
  78. if ( $(this).prop('checked') ) {
  79. c = false;
  80. var id = $(this).val(), theTitle;
  81. theTitle = $('#inline_'+id+' .post_title').html() || inlineEditL10n.notitle;
  82. te += '<div id="ttle'+id+'"><a id="_'+id+'" class="ntdelbutton" title="'+inlineEditL10n.ntdeltitle+'">X</a>'+theTitle+'</div>';
  83. }
  84. });
  85. if ( c ) {
  86. return this.revert();
  87. }
  88. $('#bulk-titles').html(te);
  89. $('#bulk-titles a').click(function(){
  90. var id = $(this).attr('id').substr(1);
  91. $('table.widefat input[value="' + id + '"]').prop('checked', false);
  92. $('#ttle'+id).remove();
  93. });
  94. // enable autocomplete for tags
  95. if ( 'post' === type ) {
  96. $( 'tr.inline-editor textarea[data-wp-taxonomy]' ).each( function ( i, element ) {
  97. $( element ).wpTagsSuggest();
  98. } );
  99. }
  100. $('html, body').animate( { scrollTop: 0 }, 'fast' );
  101. },
  102. edit : function(id) {
  103. var t = this, fields, editRow, rowData, status, pageOpt, pageLevel, nextPage, pageLoop = true, nextLevel, f, val, pw;
  104. t.revert();
  105. if ( typeof(id) === 'object' ) {
  106. id = t.getId(id);
  107. }
  108. fields = ['post_title', 'post_name', 'post_author', '_status', 'jj', 'mm', 'aa', 'hh', 'mn', 'ss', 'post_password', 'post_format', 'menu_order', 'page_template'];
  109. if ( t.type === 'page' ) {
  110. fields.push('post_parent');
  111. }
  112. // add the new edit row with an extra blank row underneath to maintain zebra striping.
  113. editRow = $('#inline-edit').clone(true);
  114. $( 'td', editRow ).attr( 'colspan', $( 'th:visible, td:visible', '.widefat:first thead' ).length );
  115. $(t.what+id).removeClass('is-expanded').hide().after(editRow).after('<tr class="hidden"></tr>');
  116. // populate the data
  117. rowData = $('#inline_'+id);
  118. if ( !$(':input[name="post_author"] option[value="' + $('.post_author', rowData).text() + '"]', editRow).val() ) {
  119. // author no longer has edit caps, so we need to add them to the list of authors
  120. $(':input[name="post_author"]', editRow).prepend('<option value="' + $('.post_author', rowData).text() + '">' + $('#' + t.type + '-' + id + ' .author').text() + '</option>');
  121. }
  122. if ( $( ':input[name="post_author"] option', editRow ).length === 1 ) {
  123. $('label.inline-edit-author', editRow).hide();
  124. }
  125. for ( f = 0; f < fields.length; f++ ) {
  126. val = $('.'+fields[f], rowData);
  127. // Deal with Twemoji
  128. val.find( 'img' ).replaceWith( function() { return this.alt; } );
  129. val = val.text();
  130. $(':input[name="' + fields[f] + '"]', editRow).val( val );
  131. }
  132. if ( $( '.comment_status', rowData ).text() === 'open' ) {
  133. $( 'input[name="comment_status"]', editRow ).prop( 'checked', true );
  134. }
  135. if ( $( '.ping_status', rowData ).text() === 'open' ) {
  136. $( 'input[name="ping_status"]', editRow ).prop( 'checked', true );
  137. }
  138. if ( $( '.sticky', rowData ).text() === 'sticky' ) {
  139. $( 'input[name="sticky"]', editRow ).prop( 'checked', true );
  140. }
  141. // hierarchical taxonomies
  142. $('.post_category', rowData).each(function(){
  143. var taxname,
  144. term_ids = $(this).text();
  145. if ( term_ids ) {
  146. taxname = $(this).attr('id').replace('_'+id, '');
  147. $('ul.'+taxname+'-checklist :checkbox', editRow).val(term_ids.split(','));
  148. }
  149. });
  150. //flat taxonomies
  151. $('.tags_input', rowData).each(function(){
  152. var terms = $(this),
  153. taxname = $(this).attr('id').replace('_' + id, ''),
  154. textarea = $('textarea.tax_input_' + taxname, editRow),
  155. comma = inlineEditL10n.comma;
  156. terms.find( 'img' ).replaceWith( function() { return this.alt; } );
  157. terms = terms.text();
  158. if ( terms ) {
  159. if ( ',' !== comma ) {
  160. terms = terms.replace(/,/g, comma);
  161. }
  162. textarea.val(terms);
  163. }
  164. textarea.wpTagsSuggest();
  165. });
  166. // handle the post status
  167. status = $('._status', rowData).text();
  168. if ( 'future' !== status ) {
  169. $('select[name="_status"] option[value="future"]', editRow).remove();
  170. }
  171. pw = $( '.inline-edit-password-input' ).prop( 'disabled', false );
  172. if ( 'private' === status ) {
  173. $('input[name="keep_private"]', editRow).prop('checked', true);
  174. pw.val( '' ).prop( 'disabled', true );
  175. }
  176. // remove the current page and children from the parent dropdown
  177. pageOpt = $('select[name="post_parent"] option[value="' + id + '"]', editRow);
  178. if ( pageOpt.length > 0 ) {
  179. pageLevel = pageOpt[0].className.split('-')[1];
  180. nextPage = pageOpt;
  181. while ( pageLoop ) {
  182. nextPage = nextPage.next('option');
  183. if ( nextPage.length === 0 ) {
  184. break;
  185. }
  186. nextLevel = nextPage[0].className.split('-')[1];
  187. if ( nextLevel <= pageLevel ) {
  188. pageLoop = false;
  189. } else {
  190. nextPage.remove();
  191. nextPage = pageOpt;
  192. }
  193. }
  194. pageOpt.remove();
  195. }
  196. $(editRow).attr('id', 'edit-'+id).addClass('inline-editor').show();
  197. $('.ptitle', editRow).focus();
  198. return false;
  199. },
  200. // Ajax saving is only for Quick Edit.
  201. save : function(id) {
  202. var params, fields, page = $('.post_status_page').val() || '';
  203. if ( typeof(id) === 'object' ) {
  204. id = this.getId(id);
  205. }
  206. $( 'table.widefat .spinner' ).addClass( 'is-active' );
  207. params = {
  208. action: 'inline-save',
  209. post_type: typenow,
  210. post_ID: id,
  211. edit_date: 'true',
  212. post_status: page
  213. };
  214. fields = $('#edit-'+id).find(':input').serialize();
  215. params = fields + '&' + $.param(params);
  216. // make ajax request
  217. $.post( ajaxurl, params,
  218. function(r) {
  219. var $errorSpan = $( '#edit-' + id + ' .inline-edit-save .error' );
  220. $( 'table.widefat .spinner' ).removeClass( 'is-active' );
  221. $( '.ac_results' ).hide();
  222. if (r) {
  223. if ( -1 !== r.indexOf( '<tr' ) ) {
  224. $(inlineEditPost.what+id).siblings('tr.hidden').addBack().remove();
  225. $('#edit-'+id).before(r).remove();
  226. $( inlineEditPost.what + id ).hide().fadeIn( 400, function() {
  227. // Move focus back to the Quick Edit link. $( this ) is the row being animated.
  228. $( this ).find( '.editinline' ).focus();
  229. wp.a11y.speak( inlineEditL10n.saved );
  230. });
  231. } else {
  232. r = r.replace( /<.[^<>]*?>/g, '' );
  233. $errorSpan.html( r ).show();
  234. wp.a11y.speak( $errorSpan.text() );
  235. }
  236. } else {
  237. $errorSpan.html( inlineEditL10n.error ).show();
  238. wp.a11y.speak( inlineEditL10n.error );
  239. }
  240. },
  241. 'html');
  242. // Prevent submitting the form when pressing Enter on a focused field.
  243. return false;
  244. },
  245. // Revert is for both Quick Edit and Bulk Edit.
  246. revert : function(){
  247. var $tableWideFat = $( '.widefat' ),
  248. id = $( '.inline-editor', $tableWideFat ).attr( 'id' );
  249. if ( id ) {
  250. $( '.spinner', $tableWideFat ).removeClass( 'is-active' );
  251. $( '.ac_results' ).hide();
  252. if ( 'bulk-edit' === id ) {
  253. $( '#bulk-edit', $tableWideFat ).removeClass( 'inline-editor' ).hide().siblings( '.hidden' ).remove();
  254. $('#bulk-titles').empty();
  255. $('#inlineedit').append( $('#bulk-edit') );
  256. // Move focus back to the Bulk Action button that was activated.
  257. $( '#' + inlineEditPost.whichBulkButtonId ).focus();
  258. } else {
  259. $('#'+id).siblings('tr.hidden').addBack().remove();
  260. id = id.substr( id.lastIndexOf('-') + 1 );
  261. // Show the post row and move focus back to the Quick Edit link.
  262. $( this.what + id ).show().find( '.editinline' ).focus();
  263. }
  264. }
  265. return false;
  266. },
  267. getId : function(o) {
  268. var id = $(o).closest('tr').attr('id'),
  269. parts = id.split('-');
  270. return parts[parts.length - 1];
  271. }
  272. };
  273. $( document ).ready( function(){ inlineEditPost.init(); } );
  274. // Show/hide locks on posts
  275. $( document ).on( 'heartbeat-tick.wp-check-locked-posts', function( e, data ) {
  276. var locked = data['wp-check-locked-posts'] || {};
  277. $('#the-list tr').each( function(i, el) {
  278. var key = el.id, row = $(el), lock_data, avatar;
  279. if ( locked.hasOwnProperty( key ) ) {
  280. if ( ! row.hasClass('wp-locked') ) {
  281. lock_data = locked[key];
  282. row.find('.column-title .locked-text').text( lock_data.text );
  283. row.find('.check-column checkbox').prop('checked', false);
  284. if ( lock_data.avatar_src ) {
  285. avatar = $( '<img class="avatar avatar-18 photo" width="18" height="18" alt="" />' ).attr( 'src', lock_data.avatar_src.replace( /&amp;/g, '&' ) );
  286. row.find('.column-title .locked-avatar').empty().append( avatar );
  287. }
  288. row.addClass('wp-locked');
  289. }
  290. } else if ( row.hasClass('wp-locked') ) {
  291. // Make room for the CSS animation
  292. row.removeClass('wp-locked').delay(1000).find('.locked-info span').empty();
  293. }
  294. });
  295. }).on( 'heartbeat-send.wp-check-locked-posts', function( e, data ) {
  296. var check = [];
  297. $('#the-list tr').each( function(i, el) {
  298. if ( el.id ) {
  299. check.push( el.id );
  300. }
  301. });
  302. if ( check.length ) {
  303. data['wp-check-locked-posts'] = check;
  304. }
  305. }).ready( function() {
  306. // Set the heartbeat interval to 15 sec.
  307. if ( typeof wp !== 'undefined' && wp.heartbeat ) {
  308. wp.heartbeat.interval( 15 );
  309. }
  310. });
  311. })( jQuery, window.wp );