[ 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, tagsl10n, showNotice, validateForm */
  11  
  12  jQuery(document).ready(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       * @returns 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               * @returns {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>' + tagsl10n.noPerm + '</p></div>');
  59                      tr.children().css('backgroundColor', '');
  60  
  61                  } else {
  62                      $('#ajax-response').empty().append('<div class="error"><p>' + tagsl10n.broken + '</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       * @returns {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       * @returns boolean Always returns false to cancel the default event handling.
 100       */
 101      $('#submit').click(function(){
 102          var form = $(this).parents('form');
 103  
 104          if ( ! validateForm( form ) )
 105              return false;
 106  
 107          if ( addingTerm ) {
 108              // If we're adding a term, noop the button to avoid duplicate requests.
 109              return false;
 110          }
 111  
 112          addingTerm = true;
 113          form.find( '.submit .spinner' ).addClass( 'is-active' );
 114  
 115          /**
 116           * Does a request to the server to add a new term to the database
 117           *
 118           * @param {string} r The response from the server.
 119           *
 120           * @returns {void}
 121           */
 122          $.post(ajaxurl, $('#addtag').serialize(), function(r){
 123              var res, parent, term, indent, i;
 124  
 125              addingTerm = false;
 126              form.find( '.submit .spinner' ).removeClass( 'is-active' );
 127  
 128              $('#ajax-response').empty();
 129              res = wpAjax.parseAjaxResponse( r, 'ajax-response' );
 130              if ( ! res || res.errors )
 131                  return;
 132  
 133              parent = form.find( 'select#parent' ).val();
 134  
 135              if ( parent > 0 && $('#tag-' + parent ).length > 0 ) // If the parent exists on this page, insert it below. Else insert it at the top of the list.
 136                  $( '.tags #tag-' + parent ).after( res.responses[0].supplemental.noparents ); // As the parent exists, Insert the version with - - - prefixed
 137              else
 138                  $( '.tags' ).prepend( res.responses[0].supplemental.parents ); // As the parent is not visible, Insert the version with Parent - Child - ThisTerm
 139  
 140              $('.tags .no-items').remove();
 141  
 142              if ( form.find('select#parent') ) {
 143                  // Parents field exists, Add new term to the list.
 144                  term = res.responses[1].supplemental;
 145  
 146                  // Create an indent for the Parent field
 147                  indent = '';
 148                  for ( i = 0; i < res.responses[1].position; i++ )
 149                      indent += '&nbsp;&nbsp;&nbsp;';
 150  
 151                  form.find( 'select#parent option:selected' ).after( '<option value="' + term.term_id + '">' + indent + term.name + '</option>' );
 152              }
 153  
 154              $('input[type="text"]:visible, textarea:visible', form).val('');
 155          });
 156  
 157          return false;
 158      });
 159  
 160  });


Generated: Sat Sep 21 01:00:03 2019 Cross-referenced by PHPXref 0.7.1