[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-admin/js/ -> tags.js (source)

   1  /**
   2   * Contains logic for deleting and adding tags.
   3   *
   4   * For deleting tags it makes a request to the server to delete the tag.
   5   * For adding tags it makes a request to the server to add the tag.
   6   *
   7   * @output wp-admin/js/tags.js
   8   */
   9  
  10   /* global ajaxurl, wpAjax, showNotice, validateForm */
  11  
  12  jQuery( function($) {
  13  
  14      var addingTerm = false;
  15  
  16      /**
  17       * Adds an event handler to the delete term link on the term overview page.
  18       *
  19       * Cancels default event handling and event bubbling.
  20       *
  21       * @since 2.8.0
  22       *
  23       * @return {boolean} Always returns false to cancel the default event handling.
  24       */
  25      $( '#the-list' ).on( 'click', '.delete-tag', function() {
  26          var t = $(this), tr = t.parents('tr'), r = true, data;
  27  
  28          if ( 'undefined' != showNotice )
  29              r = showNotice.warn();
  30  
  31          if ( r ) {
  32              data = t.attr('href').replace(/[^?]*\?/, '').replace(/action=delete/, 'action=delete-tag');
  33  
  34              /**
  35               * Makes a request to the server to delete the term that corresponds to the
  36               * delete term button.
  37               *
  38               * @param {string} r The response from the server.
  39               *
  40               * @return {void}
  41               */
  42              $.post(ajaxurl, data, function(r){
  43                  if ( '1' == r ) {
  44                      $('#ajax-response').empty();
  45                      tr.fadeOut('normal', function(){ tr.remove(); });
  46  
  47                      /**
  48                       * Removes the term from the parent box and the tag cloud.
  49                       *
  50                       * `data.match(/tag_ID=(\d+)/)[1]` matches the term ID from the data variable.
  51                       * This term ID is then used to select the relevant HTML elements:
  52                       * The parent box and the tag cloud.
  53                       */
  54                      $('select#parent option[value="' + data.match(/tag_ID=(\d+)/)[1] + '"]').remove();
  55                      $('a.tag-link-' + data.match(/tag_ID=(\d+)/)[1]).remove();
  56  
  57                  } else if ( '-1' == r ) {
  58                      $('#ajax-response').empty().append('<div class="error"><p>' + wp.i18n.__( 'Sorry, you are not allowed to do that.' ) + '</p></div>');
  59                      tr.children().css('backgroundColor', '');
  60  
  61                  } else {
  62                      $('#ajax-response').empty().append('<div class="error"><p>' + wp.i18n.__( 'Something went wrong.' ) + '</p></div>');
  63                      tr.children().css('backgroundColor', '');
  64                  }
  65              });
  66  
  67              tr.children().css('backgroundColor', '#f33');
  68          }
  69  
  70          return false;
  71      });
  72  
  73      /**
  74       * Adds a deletion confirmation when removing a tag.
  75       *
  76       * @since 4.8.0
  77       *
  78       * @return {void}
  79       */
  80      $( '#edittag' ).on( 'click', '.delete', function( e ) {
  81          if ( 'undefined' === typeof showNotice ) {
  82              return true;
  83          }
  84  
  85          // Confirms the deletion, a negative response means the deletion must not be executed.
  86          var response = showNotice.warn();
  87          if ( ! response ) {
  88              e.preventDefault();
  89          }
  90      });
  91  
  92      /**
  93       * Adds an event handler to the form submit on the term overview page.
  94       *
  95       * Cancels default event handling and event bubbling.
  96       *
  97       * @since 2.8.0
  98       *
  99       * @return {boolean} Always returns false to cancel the default event handling.
 100       */
 101      $('#submit').on( 'click', function(){
 102          var form = $(this).parents('form');
 103  
 104          if ( addingTerm ) {
 105              // If we're adding a term, noop the button to avoid duplicate requests.
 106              return false;
 107          }
 108  
 109          addingTerm = true;
 110          form.find( '.submit .spinner' ).addClass( 'is-active' );
 111  
 112          /**
 113           * Does a request to the server to add a new term to the database
 114           *
 115           * @param {string} r The response from the server.
 116           *
 117           * @return {void}
 118           */
 119          $.post(ajaxurl, $('#addtag').serialize(), function(r){
 120              var res, parent, term, indent, i;
 121  
 122              addingTerm = false;
 123              form.find( '.submit .spinner' ).removeClass( 'is-active' );
 124  
 125              $('#ajax-response').empty();
 126              res = wpAjax.parseAjaxResponse( r, 'ajax-response' );
 127  
 128              if ( res.errors && res.responses[0].errors[0].code === 'empty_term_name' ) {
 129                  validateForm( form );
 130              }
 131  
 132              if ( ! res || res.errors ) {
 133                  return;
 134              }
 135  
 136              parent = form.find( 'select#parent' ).val();
 137  
 138              // If the parent exists on this page, insert it below. Else insert it at the top of the list.
 139              if ( parent > 0 && $('#tag-' + parent ).length > 0 ) {
 140                  // As the parent exists, insert the version with - - - prefixed.
 141                  $( '.tags #tag-' + parent ).after( res.responses[0].supplemental.noparents );
 142              } else {
 143                  // As the parent is not visible, insert the version with Parent - Child - ThisTerm.
 144                  $( '.tags' ).prepend( res.responses[0].supplemental.parents );
 145              }
 146  
 147              $('.tags .no-items').remove();
 148  
 149              if ( form.find('select#parent') ) {
 150                  // Parents field exists, Add new term to the list.
 151                  term = res.responses[1].supplemental;
 152  
 153                  // Create an indent for the Parent field.
 154                  indent = '';
 155                  for ( i = 0; i < res.responses[1].position; i++ )
 156                      indent += '&nbsp;&nbsp;&nbsp;';
 157  
 158                  form.find( 'select#parent option:selected' ).after( '<option value="' + term.term_id + '">' + indent + term.name + '</option>' );
 159              }
 160  
 161              $('input:not([type="checkbox"]):not([type="radio"]):not([type="button"]):not([type="submit"]):not([type="reset"]):visible, textarea:visible', form).val('');
 162          });
 163  
 164          return false;
 165      });
 166  
 167  });


Generated: Sun Aug 14 01:00:02 2022 Cross-referenced by PHPXref 0.7.1