[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

title

Body

[close]

/wp-admin/js/ -> inline-edit-post.js (source)

   1  /**
   2   * This file contains the functions needed for the inline editing of posts.
   3   *
   4   * @since 2.7.0
   5   * @output wp-admin/js/inline-edit-post.js
   6   */
   7  
   8  /* global inlineEditL10n, ajaxurl, typenow, inlineEditPost */
   9  
  10  window.wp = window.wp || {};
  11  
  12  /**
  13   * Manages the quick edit and bulk edit windows for editing posts or pages.
  14   *
  15   * @namespace inlineEditPost
  16   *
  17   * @since 2.7.0
  18   *
  19   * @type {Object}
  20   *
  21   * @property {string} type The type of inline editor.
  22   * @property {string} what The prefix before the post id.
  23   *
  24   */
  25  ( function( $, wp ) {
  26  
  27      window.inlineEditPost = {
  28  
  29      /**
  30       * Initializes the inline and bulk post editor.
  31       *
  32       * Binds event handlers to the escape key to close the inline editor
  33       * and to the save and close buttons. Changes DOM to be ready for inline
  34       * editing. Adds event handler to bulk edit.
  35       *
  36       * @memberof inlineEditPost
  37       * @since 2.7.0
  38       *
  39       * @returns {void}
  40       */
  41      init : function(){
  42          var t = this, qeRow = $('#inline-edit'), bulkRow = $('#bulk-edit');
  43  
  44          t.type = $('table.widefat').hasClass('pages') ? 'page' : 'post';
  45          // Post id prefix.
  46          t.what = '#post-';
  47  
  48          /**
  49           * Binds the escape key to revert the changes and close the quick editor.
  50           *
  51           * @returns {boolean} The result of revert.
  52           */
  53          qeRow.keyup(function(e){
  54              // Revert changes if escape key is pressed.
  55              if ( e.which === 27 ) {
  56                  return inlineEditPost.revert();
  57              }
  58          });
  59  
  60          /**
  61           * Binds the escape key to revert the changes and close the bulk editor.
  62           *
  63           * @returns {boolean} The result of revert.
  64           */
  65          bulkRow.keyup(function(e){
  66              // Revert changes if escape key is pressed.
  67              if ( e.which === 27 ) {
  68                  return inlineEditPost.revert();
  69              }
  70          });
  71  
  72          /**
  73           * Reverts changes and close the quick editor if the cancel button is clicked.
  74           *
  75           * @returns {boolean} The result of revert.
  76           */
  77          $( '.cancel', qeRow ).click( function() {
  78              return inlineEditPost.revert();
  79          });
  80  
  81          /**
  82           * Saves changes in the quick editor if the save(named: update) button is clicked.
  83           *
  84           * @returns {boolean} The result of save.
  85           */
  86          $( '.save', qeRow ).click( function() {
  87              return inlineEditPost.save(this);
  88          });
  89  
  90          /**
  91           * If enter is pressed, and the target is not the cancel button, save the post.
  92           *
  93           * @returns {boolean} The result of save.
  94           */
  95          $('td', qeRow).keydown(function(e){
  96              if ( e.which === 13 && ! $( e.target ).hasClass( 'cancel' ) ) {
  97                  return inlineEditPost.save(this);
  98              }
  99          });
 100  
 101          /**
 102           * Reverts changes and close the bulk editor if the cancel button is clicked.
 103           *
 104           * @returns {boolean} The result of revert.
 105           */
 106          $( '.cancel', bulkRow ).click( function() {
 107              return inlineEditPost.revert();
 108          });
 109  
 110          /**
 111           * Disables the password input field when the private post checkbox is checked.
 112           */
 113          $('#inline-edit .inline-edit-private input[value="private"]').click( function(){
 114              var pw = $('input.inline-edit-password-input');
 115              if ( $(this).prop('checked') ) {
 116                  pw.val('').prop('disabled', true);
 117              } else {
 118                  pw.prop('disabled', false);
 119              }
 120          });
 121  
 122          /**
 123           * Binds click event to the .editinline button which opens the quick editor.
 124           */
 125          $( '#the-list' ).on( 'click', '.editinline', function() {
 126              $( this ).attr( 'aria-expanded', 'true' );
 127              inlineEditPost.edit( this );
 128          });
 129  
 130          $('#bulk-edit').find('fieldset:first').after(
 131              $('#inline-edit fieldset.inline-edit-categories').clone()
 132          ).siblings( 'fieldset:last' ).prepend(
 133              $('#inline-edit label.inline-edit-tags').clone()
 134          );
 135  
 136          $('select[name="_status"] option[value="future"]', bulkRow).remove();
 137  
 138          /**
 139           * Adds onclick events to the apply buttons.
 140           */
 141          $('#doaction, #doaction2').click(function(e){
 142              var n;
 143  
 144              t.whichBulkButtonId = $( this ).attr( 'id' );
 145              n = t.whichBulkButtonId.substr( 2 );
 146  
 147              if ( 'edit' === $( 'select[name="' + n + '"]' ).val() ) {
 148                  e.preventDefault();
 149                  t.setBulk();
 150              } else if ( $('form#posts-filter tr.inline-editor').length > 0 ) {
 151                  t.revert();
 152              }
 153          });
 154      },
 155  
 156      /**
 157       * Toggles the quick edit window, hiding it when it's active and showing it when
 158       * inactive.
 159       *
 160       * @memberof inlineEditPost
 161       * @since 2.7.0
 162       *
 163       * @param {Object} el Element within a post table row.
 164       */
 165      toggle : function(el){
 166          var t = this;
 167          $( t.what + t.getId( el ) ).css( 'display' ) === 'none' ? t.revert() : t.edit( el );
 168      },
 169  
 170      /**
 171       * Creates the bulk editor row to edit multiple posts at once.
 172       *
 173       * @memberof inlineEditPost
 174       * @since 2.7.0
 175       */
 176      setBulk : function(){
 177          var te = '', type = this.type, c = true;
 178          this.revert();
 179  
 180          $( '#bulk-edit td' ).attr( 'colspan', $( 'th:visible, td:visible', '.widefat:first thead' ).length );
 181  
 182          // Insert the editor at the top of the table with an empty row above to maintain zebra striping.
 183          $('table.widefat tbody').prepend( $('#bulk-edit') ).prepend('<tr class="hidden"></tr>');
 184          $('#bulk-edit').addClass('inline-editor').show();
 185  
 186          /**
 187           * Create a HTML div with the title and a link(delete-icon) for each selected
 188           * post.
 189           *
 190           * Get the selected posts based on the checked checkboxes in the post table.
 191           */
 192          $( 'tbody th.check-column input[type="checkbox"]' ).each( function() {
 193  
 194              // If the checkbox for a post is selected, add the post to the edit list.
 195              if ( $(this).prop('checked') ) {
 196                  c = false;
 197                  var id = $(this).val(), theTitle;
 198                  theTitle = $('#inline_'+id+' .post_title').html() || inlineEditL10n.notitle;
 199                  te += '<div id="ttle'+id+'"><a id="_'+id+'" class="ntdelbutton" title="'+inlineEditL10n.ntdeltitle+'">X</a>'+theTitle+'</div>';
 200              }
 201          });
 202  
 203          // If no checkboxes where checked, just hide the quick/bulk edit rows.
 204          if ( c ) {
 205              return this.revert();
 206          }
 207  
 208          // Add onclick events to the delete-icons in the bulk editors the post title list.
 209          $('#bulk-titles').html(te);
 210          /**
 211           * Binds on click events to the checkboxes before the posts in the table.
 212           *
 213           * @listens click
 214           */
 215          $('#bulk-titles a').click(function(){
 216              var id = $(this).attr('id').substr(1);
 217  
 218              $('table.widefat input[value="' + id + '"]').prop('checked', false);
 219              $('#ttle'+id).remove();
 220          });
 221  
 222          // Enable auto-complete for tags when editing posts.
 223          if ( 'post' === type ) {
 224              $( 'tr.inline-editor textarea[data-wp-taxonomy]' ).each( function ( i, element ) {
 225                  /*
 226                   * While Quick Edit clones the form each time, Bulk Edit always re-uses
 227                   * the same form. Let's check if an autocomplete instance already exists.
 228                   */
 229                  if ( $( element ).autocomplete( 'instance' ) ) {
 230                      // jQuery equivalent of `continue` within an `each()` loop.
 231                      return;
 232                  }
 233  
 234                  $( element ).wpTagsSuggest();
 235              } );
 236          }
 237  
 238          // Scrolls to the top of the table where the editor is rendered.
 239          $('html, body').animate( { scrollTop: 0 }, 'fast' );
 240      },
 241  
 242      /**
 243       * Creates a quick edit window for the post that has been clicked.
 244       *
 245       * @memberof inlineEditPost
 246       * @since 2.7.0
 247       *
 248       * @param {number|Object} id The id of the clicked post or an element within a post
 249       *                           table row.
 250       * @returns {boolean} Always returns false at the end of execution.
 251       */
 252      edit : function(id) {
 253          var t = this, fields, editRow, rowData, status, pageOpt, pageLevel, nextPage, pageLoop = true, nextLevel, f, val, pw;
 254          t.revert();
 255  
 256          if ( typeof(id) === 'object' ) {
 257              id = t.getId(id);
 258          }
 259  
 260          fields = ['post_title', 'post_name', 'post_author', '_status', 'jj', 'mm', 'aa', 'hh', 'mn', 'ss', 'post_password', 'post_format', 'menu_order', 'page_template'];
 261          if ( t.type === 'page' ) {
 262              fields.push('post_parent');
 263          }
 264  
 265          // Add the new edit row with an extra blank row underneath to maintain zebra striping.
 266          editRow = $('#inline-edit').clone(true);
 267          $( 'td', editRow ).attr( 'colspan', $( 'th:visible, td:visible', '.widefat:first thead' ).length );
 268  
 269          $(t.what+id).removeClass('is-expanded').hide().after(editRow).after('<tr class="hidden"></tr>');
 270  
 271          // Populate fields in the quick edit window.
 272          rowData = $('#inline_'+id);
 273          if ( !$(':input[name="post_author"] option[value="' + $('.post_author', rowData).text() + '"]', editRow).val() ) {
 274  
 275              // The post author no longer has edit capabilities, so we need to add them to the list of authors.
 276              $(':input[name="post_author"]', editRow).prepend('<option value="' + $('.post_author', rowData).text() + '">' + $('#' + t.type + '-' + id + ' .author').text() + '</option>');
 277          }
 278          if ( $( ':input[name="post_author"] option', editRow ).length === 1 ) {
 279              $('label.inline-edit-author', editRow).hide();
 280          }
 281  
 282          for ( f = 0; f < fields.length; f++ ) {
 283              val = $('.'+fields[f], rowData);
 284  
 285              /**
 286               * Replaces the image for a Twemoji(Twitter emoji) with it's alternate text.
 287               *
 288               * @returns Alternate text from the image.
 289               */
 290              val.find( 'img' ).replaceWith( function() { return this.alt; } );
 291              val = val.text();
 292              $(':input[name="' + fields[f] + '"]', editRow).val( val );
 293          }
 294  
 295          if ( $( '.comment_status', rowData ).text() === 'open' ) {
 296              $( 'input[name="comment_status"]', editRow ).prop( 'checked', true );
 297          }
 298          if ( $( '.ping_status', rowData ).text() === 'open' ) {
 299              $( 'input[name="ping_status"]', editRow ).prop( 'checked', true );
 300          }
 301          if ( $( '.sticky', rowData ).text() === 'sticky' ) {
 302              $( 'input[name="sticky"]', editRow ).prop( 'checked', true );
 303          }
 304  
 305          /**
 306           * Creates the select boxes for the categories.
 307           */
 308          $('.post_category', rowData).each(function(){
 309              var taxname,
 310                  term_ids = $(this).text();
 311  
 312              if ( term_ids ) {
 313                  taxname = $(this).attr('id').replace('_'+id, '');
 314                  $('ul.'+taxname+'-checklist :checkbox', editRow).val(term_ids.split(','));
 315              }
 316          });
 317  
 318          /**
 319           * Gets all the taxonomies for live auto-fill suggestions when typing the name
 320           * of a tag.
 321           */
 322          $('.tags_input', rowData).each(function(){
 323              var terms = $(this),
 324                  taxname = $(this).attr('id').replace('_' + id, ''),
 325                  textarea = $('textarea.tax_input_' + taxname, editRow),
 326                  comma = inlineEditL10n.comma;
 327  
 328              terms.find( 'img' ).replaceWith( function() { return this.alt; } );
 329              terms = terms.text();
 330  
 331              if ( terms ) {
 332                  if ( ',' !== comma ) {
 333                      terms = terms.replace(/,/g, comma);
 334                  }
 335                  textarea.val(terms);
 336              }
 337  
 338              textarea.wpTagsSuggest();
 339          });
 340  
 341          // Handle the post status.
 342          status = $('._status', rowData).text();
 343          if ( 'future' !== status ) {
 344              $('select[name="_status"] option[value="future"]', editRow).remove();
 345          }
 346  
 347          pw = $( '.inline-edit-password-input' ).prop( 'disabled', false );
 348          if ( 'private' === status ) {
 349              $('input[name="keep_private"]', editRow).prop('checked', true);
 350              pw.val( '' ).prop( 'disabled', true );
 351          }
 352  
 353          // Remove the current page and children from the parent dropdown.
 354          pageOpt = $('select[name="post_parent"] option[value="' + id + '"]', editRow);
 355          if ( pageOpt.length > 0 ) {
 356              pageLevel = pageOpt[0].className.split('-')[1];
 357              nextPage = pageOpt;
 358              while ( pageLoop ) {
 359                  nextPage = nextPage.next('option');
 360                  if ( nextPage.length === 0 ) {
 361                      break;
 362                  }
 363  
 364                  nextLevel = nextPage[0].className.split('-')[1];
 365  
 366                  if ( nextLevel <= pageLevel ) {
 367                      pageLoop = false;
 368                  } else {
 369                      nextPage.remove();
 370                      nextPage = pageOpt;
 371                  }
 372              }
 373              pageOpt.remove();
 374          }
 375  
 376          $(editRow).attr('id', 'edit-'+id).addClass('inline-editor').show();
 377          $('.ptitle', editRow).focus();
 378  
 379          return false;
 380      },
 381  
 382      /**
 383       * Saves the changes made in the quick edit window to the post.
 384       * AJAX saving is only for Quick Edit and not for bulk edit.
 385       *
 386       * @since 2.7.0
 387       *
 388       * @param   {int}     id The id for the post that has been changed.
 389       * @returns {boolean}    false, so the form does not submit when pressing
 390       *                       Enter on a focused field.
 391       */
 392      save : function(id) {
 393          var params, fields, page = $('.post_status_page').val() || '';
 394  
 395          if ( typeof(id) === 'object' ) {
 396              id = this.getId(id);
 397          }
 398  
 399          $( 'table.widefat .spinner' ).addClass( 'is-active' );
 400  
 401          params = {
 402              action: 'inline-save',
 403              post_type: typenow,
 404              post_ID: id,
 405              edit_date: 'true',
 406              post_status: page
 407          };
 408  
 409          fields = $('#edit-'+id).find(':input').serialize();
 410          params = fields + '&' + $.param(params);
 411  
 412          // Make ajax request.
 413          $.post( ajaxurl, params,
 414              function(r) {
 415                  var $errorNotice = $( '#edit-' + id + ' .inline-edit-save .notice-error' ),
 416                      $error = $errorNotice.find( '.error' );
 417  
 418                  $( 'table.widefat .spinner' ).removeClass( 'is-active' );
 419                  $( '.ac_results' ).hide();
 420  
 421                  if (r) {
 422                      if ( -1 !== r.indexOf( '<tr' ) ) {
 423                          $(inlineEditPost.what+id).siblings('tr.hidden').addBack().remove();
 424                          $('#edit-'+id).before(r).remove();
 425                          $( inlineEditPost.what + id ).hide().fadeIn( 400, function() {
 426                              // Move focus back to the Quick Edit button. $( this ) is the row being animated.
 427                              $( this ).find( '.editinline' )
 428                                  .attr( 'aria-expanded', 'false' )
 429                                  .focus();
 430                              wp.a11y.speak( inlineEditL10n.saved );
 431                          });
 432                      } else {
 433                          r = r.replace( /<.[^<>]*?>/g, '' );
 434                          $errorNotice.removeClass( 'hidden' );
 435                          $error.html( r );
 436                          wp.a11y.speak( $error.text() );
 437                      }
 438                  } else {
 439                      $errorNotice.removeClass( 'hidden' );
 440                      $error.html( inlineEditL10n.error );
 441                      wp.a11y.speak( inlineEditL10n.error );
 442                  }
 443              },
 444          'html');
 445  
 446          // Prevent submitting the form when pressing Enter on a focused field.
 447          return false;
 448      },
 449  
 450      /**
 451       * Hides and empties the Quick Edit and/or Bulk Edit windows.
 452       *
 453       * @memberof    inlineEditPost
 454       * @since 2.7.0
 455       *
 456       * @returns {boolean} Always returns false.
 457       */
 458      revert : function(){
 459          var $tableWideFat = $( '.widefat' ),
 460              id = $( '.inline-editor', $tableWideFat ).attr( 'id' );
 461  
 462          if ( id ) {
 463              $( '.spinner', $tableWideFat ).removeClass( 'is-active' );
 464              $( '.ac_results' ).hide();
 465  
 466              if ( 'bulk-edit' === id ) {
 467  
 468                  // Hide the bulk editor.
 469                  $( '#bulk-edit', $tableWideFat ).removeClass( 'inline-editor' ).hide().siblings( '.hidden' ).remove();
 470                  $('#bulk-titles').empty();
 471  
 472                  // Store the empty bulk editor in a hidden element.
 473                  $('#inlineedit').append( $('#bulk-edit') );
 474  
 475                  // Move focus back to the Bulk Action button that was activated.
 476                  $( '#' + inlineEditPost.whichBulkButtonId ).focus();
 477              } else {
 478  
 479                  // Remove both the inline-editor and its hidden tr siblings.
 480                  $('#'+id).siblings('tr.hidden').addBack().remove();
 481                  id = id.substr( id.lastIndexOf('-') + 1 );
 482  
 483                  // Show the post row and move focus back to the Quick Edit button.
 484                  $( this.what + id ).show().find( '.editinline' )
 485                      .attr( 'aria-expanded', 'false' )
 486                      .focus();
 487              }
 488          }
 489  
 490          return false;
 491      },
 492  
 493      /**
 494       * Gets the id for a the post that you want to quick edit from the row in the quick
 495       * edit table.
 496       *
 497       * @memberof    inlineEditPost
 498       * @since 2.7.0
 499       *
 500       * @param   {Object} o DOM row object to get the id for.
 501       * @returns {string}   The post id extracted from the table row in the object.
 502       */
 503      getId : function(o) {
 504          var id = $(o).closest('tr').attr('id'),
 505              parts = id.split('-');
 506          return parts[parts.length - 1];
 507      }
 508  };
 509  
 510  $( document ).ready( function(){ inlineEditPost.init(); } );
 511  
 512  // Show/hide locks on posts.
 513  $( document ).on( 'heartbeat-tick.wp-check-locked-posts', function( e, data ) {
 514      var locked = data['wp-check-locked-posts'] || {};
 515  
 516      $('#the-list tr').each( function(i, el) {
 517          var key = el.id, row = $(el), lock_data, avatar;
 518  
 519          if ( locked.hasOwnProperty( key ) ) {
 520              if ( ! row.hasClass('wp-locked') ) {
 521                  lock_data = locked[key];
 522                  row.find('.column-title .locked-text').text( lock_data.text );
 523                  row.find('.check-column checkbox').prop('checked', false);
 524  
 525                  if ( lock_data.avatar_src ) {
 526                      avatar = $( '<img class="avatar avatar-18 photo" width="18" height="18" alt="" />' ).attr( 'src', lock_data.avatar_src.replace( /&amp;/g, '&' ) );
 527                      row.find('.column-title .locked-avatar').empty().append( avatar );
 528                  }
 529                  row.addClass('wp-locked');
 530              }
 531          } else if ( row.hasClass('wp-locked') ) {
 532              row.removeClass( 'wp-locked' ).find( '.locked-info span' ).empty();
 533          }
 534      });
 535  }).on( 'heartbeat-send.wp-check-locked-posts', function( e, data ) {
 536      var check = [];
 537  
 538      $('#the-list tr').each( function(i, el) {
 539          if ( el.id ) {
 540              check.push( el.id );
 541          }
 542      });
 543  
 544      if ( check.length ) {
 545          data['wp-check-locked-posts'] = check;
 546      }
 547  }).ready( function() {
 548  
 549      // Set the heartbeat interval to 15 sec.
 550      if ( typeof wp !== 'undefined' && wp.heartbeat ) {
 551          wp.heartbeat.interval( 15 );
 552      }
 553  });
 554  
 555  })( jQuery, window.wp );


Generated: Sat Nov 23 20:47:33 2019 Cross-referenced by PHPXref 0.7