[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/js/ -> wp-lists.js (source)

   1  /**
   2   * @output wp-includes/js/wp-lists.js
   3   */
   4  
   5  /* global ajaxurl, wpAjax */
   6  
   7  /**
   8   * Handles the dynamic functionality needed for lists.
   9   *
  10   * @param {JQueryStatic} $ The jQuery object.
  11   */
  12  ( function( $ ) {
  13  var functions = {
  14      add:     'ajaxAdd',
  15      del:     'ajaxDel',
  16      dim:     'ajaxDim',
  17      process: 'process',
  18      recolor: 'recolor'
  19  }, wpList;
  20  
  21  /**
  22   * @namespace
  23   */
  24  wpList = {
  25  
  26      /**
  27       * @member {Object}
  28       */
  29      settings: {
  30  
  31          /**
  32           * URL for Ajax requests.
  33           *
  34           * @member {string}
  35           */
  36          url: ajaxurl,
  37  
  38          /**
  39           * The HTTP method to use for Ajax requests.
  40           *
  41           * @member {string}
  42           */
  43          type: 'POST',
  44  
  45          /**
  46           * ID of the element the parsed Ajax response will be stored in.
  47           *
  48           * @member {string}
  49           */
  50          response: 'ajax-response',
  51  
  52          /**
  53           * The type of list.
  54           *
  55           * @member {string}
  56           */
  57          what: '',
  58  
  59          /**
  60           * CSS class name for alternate styling.
  61           *
  62           * @member {string}
  63           */
  64          alt: 'alternate',
  65  
  66          /**
  67           * Offset to start alternate styling from.
  68           *
  69           * @member {number}
  70           */
  71          altOffset: 0,
  72  
  73          /**
  74           * Color used in animation when adding an element.
  75           *
  76           * Can be 'none' to disable the animation.
  77           *
  78           * @member {string}
  79           */
  80          addColor: '#ffff33',
  81  
  82          /**
  83           * Color used in animation when deleting an element.
  84           *
  85           * Can be 'none' to disable the animation.
  86           *
  87           * @member {string}
  88           */
  89          delColor: '#faafaa',
  90  
  91          /**
  92           * Color used in dim add animation.
  93           *
  94           * Can be 'none' to disable the animation.
  95           *
  96           * @member {string}
  97           */
  98          dimAddColor: '#ffff33',
  99  
 100          /**
 101           * Color used in dim delete animation.
 102           *
 103           * Can be 'none' to disable the animation.
 104           *
 105           * @member {string}
 106           */
 107          dimDelColor: '#ff3333',
 108  
 109          /**
 110           * Callback that's run before a request is made.
 111           *
 112           * @callback wpList~confirm
 113           * @param {Object}      this
 114           * @param {HTMLElement} list            The list DOM element.
 115           * @param {Object}      settings        Settings for the current list.
 116           * @param {string}      action          The type of action to perform: 'add', 'delete', or 'dim'.
 117           * @param {string}      backgroundColor Background color of the list's DOM element.
 118           * @return {boolean} Whether to proceed with the action or not.
 119           */
 120          confirm: null,
 121  
 122          /**
 123           * Callback that's run before an item gets added to the list.
 124           *
 125           * Allows to cancel the request.
 126           *
 127           * @callback wpList~addBefore
 128           * @param {Object} settings Settings for the Ajax request.
 129           * @return {object|boolean} Settings for the Ajax request or false to abort.
 130           */
 131          addBefore: null,
 132  
 133          /**
 134           * Callback that's run after an item got added to the list.
 135           *
 136           * @callback wpList~addAfter
 137           * @param {XML}    returnedResponse Raw response returned from the server.
 138           * @param {Object} settings         Settings for the Ajax request.
 139           * @param {jqXHR}  settings.xml     jQuery XMLHttpRequest object.
 140           * @param {string} settings.status  Status of the request: 'success', 'notmodified', 'nocontent', 'error',
 141           *                                  'timeout', 'abort', or 'parsererror'.
 142           * @param {Object} settings.parsed  Parsed response object.
 143           */
 144          addAfter: null,
 145  
 146          /**
 147           * Callback that's run before an item gets deleted from the list.
 148           *
 149           * Allows to cancel the request.
 150           *
 151           * @callback wpList~delBefore
 152           * @param {Object}      settings Settings for the Ajax request.
 153           * @param {HTMLElement} list     The list DOM element.
 154           * @return {object|boolean} Settings for the Ajax request or false to abort.
 155           */
 156          delBefore: null,
 157  
 158          /**
 159           * Callback that's run after an item got deleted from the list.
 160           *
 161           * @callback wpList~delAfter
 162           * @param {XML}    returnedResponse Raw response returned from the server.
 163           * @param {Object} settings         Settings for the Ajax request.
 164           * @param {jqXHR}  settings.xml     jQuery XMLHttpRequest object.
 165           * @param {string} settings.status  Status of the request: 'success', 'notmodified', 'nocontent', 'error',
 166           *                                  'timeout', 'abort', or 'parsererror'.
 167           * @param {Object} settings.parsed  Parsed response object.
 168           */
 169          delAfter: null,
 170  
 171          /**
 172           * Callback that's run before an item gets dim'd.
 173           *
 174           * Allows to cancel the request.
 175           *
 176           * @callback wpList~dimBefore
 177           * @param {Object} settings Settings for the Ajax request.
 178           * @return {object|boolean} Settings for the Ajax request or false to abort.
 179           */
 180          dimBefore: null,
 181  
 182          /**
 183           * Callback that's run after an item got dim'd.
 184           *
 185           * @callback wpList~dimAfter
 186           * @param {XML}    returnedResponse Raw response returned from the server.
 187           * @param {Object} settings         Settings for the Ajax request.
 188           * @param {jqXHR}  settings.xml     jQuery XMLHttpRequest object.
 189           * @param {string} settings.status  Status of the request: 'success', 'notmodified', 'nocontent', 'error',
 190           *                                  'timeout', 'abort', or 'parsererror'.
 191           * @param {Object} settings.parsed  Parsed response object.
 192           */
 193          dimAfter: null
 194      },
 195  
 196      /**
 197       * Finds a nonce.
 198       *
 199       * 1. Nonce in settings.
 200       * 2. `_ajax_nonce` value in element's href attribute.
 201       * 3. `_ajax_nonce` input field that is a descendant of element.
 202       * 4. `_wpnonce` value in element's href attribute.
 203       * 5. `_wpnonce` input field that is a descendant of element.
 204       * 6. 0 if none can be found.
 205       *
 206       * @param {jQuery} element  Element that triggered the request.
 207       * @param {Object} settings Settings for the Ajax request.
 208       * @return {string|number} Nonce
 209       */
 210      nonce: function( element, settings ) {
 211          var url      = wpAjax.unserialize( element.attr( 'href' ) ),
 212              $element = $( '#' + settings.element );
 213  
 214          return settings.nonce || url._ajax_nonce || $element.find( 'input[name="_ajax_nonce"]' ).val() || url._wpnonce || $element.find( 'input[name="_wpnonce"]' ).val() || 0;
 215      },
 216  
 217      /**
 218       * Extract list item data from a DOM element.
 219       *
 220       * Example 1: data-wp-lists="delete:the-comment-list:comment-{comment_ID}:66cc66:unspam=1"
 221       * Example 2: data-wp-lists="dim:the-comment-list:comment-{comment_ID}:unapproved:e7e7d3:e7e7d3:new=approved"
 222       *
 223       * Returns an unassociative array with the following data:
 224       * data[0] - Data identifier: 'list', 'add', 'delete', or 'dim'.
 225       * data[1] - ID of the corresponding list. If data[0] is 'list', the type of list ('comment', 'category', etc).
 226       * data[2] - ID of the parent element of all inputs necessary for the request.
 227       * data[3] - Hex color to be used in this request. If data[0] is 'dim', dim class.
 228       * data[4] - Additional arguments in query syntax that are added to the request. Example: 'post_id=1234'.
 229       * If data[0] is 'dim', dim add color.
 230       * data[5] - Only available if data[0] is 'dim', dim delete color.
 231       * data[6] - Only available if data[0] is 'dim', additional arguments in query syntax that are added to the request.
 232       *
 233       * Result for Example 1:
 234       * data[0] - delete
 235       * data[1] - the-comment-list
 236       * data[2] - comment-{comment_ID}
 237       * data[3] - 66cc66
 238       * data[4] - unspam=1
 239       *
 240       * @param {HTMLElement} element The DOM element.
 241       * @param {string}      type    The type of data to look for: 'list', 'add', 'delete', or 'dim'.
 242       * @return {Array} Extracted list item data.
 243       */
 244      parseData: function( element, type ) {
 245          var data = [], wpListsData;
 246  
 247          try {
 248              wpListsData = $( element ).data( 'wp-lists' ) || '';
 249              wpListsData = wpListsData.match( new RegExp( type + ':[\\S]+' ) );
 250  
 251              if ( wpListsData ) {
 252                  data = wpListsData[0].split( ':' );
 253              }
 254          } catch ( error ) {}
 255  
 256          return data;
 257      },
 258  
 259      /**
 260       * Calls a confirm callback to verify the action that is about to be performed.
 261       *
 262       * @param {HTMLElement} list     The DOM element.
 263       * @param {Object}      settings Settings for this list.
 264       * @param {string}      action   The type of action to perform: 'add', 'delete', or 'dim'.
 265       * @return {Object|boolean} Settings if confirmed, false if not.
 266       */
 267      pre: function( list, settings, action ) {
 268          var $element, backgroundColor, confirmed;
 269  
 270          settings = $.extend( {}, this.wpList.settings, {
 271              element: null,
 272              nonce:   0,
 273              target:  list.get( 0 )
 274          }, settings || {} );
 275  
 276          if ( typeof settings.confirm === 'function' ) {
 277              $element = $( '#' + settings.element );
 278  
 279              if ( 'add' !== action ) {
 280                  backgroundColor = $element.css( 'backgroundColor' );
 281                  $element.css( 'backgroundColor', '#ff9966' );
 282              }
 283  
 284              confirmed = settings.confirm.call( this, list, settings, action, backgroundColor );
 285  
 286              if ( 'add' !== action ) {
 287                  $element.css( 'backgroundColor', backgroundColor );
 288              }
 289  
 290              if ( ! confirmed ) {
 291                  return false;
 292              }
 293          }
 294  
 295          return settings;
 296      },
 297  
 298      /**
 299       * Adds an item to the list via Ajax.
 300       *
 301       * @param {HTMLElement} element  The DOM element.
 302       * @param {Object}      settings Settings for this list.
 303       * @return {boolean} Whether the item was added.
 304       */
 305      ajaxAdd: function( element, settings ) {
 306          var list     = this,
 307              $element = $( element ),
 308              data     = wpList.parseData( $element, 'add' ),
 309              formValues, formData, parsedResponse, returnedResponse;
 310  
 311          settings = settings || {};
 312          settings = wpList.pre.call( list, $element, settings, 'add' );
 313  
 314          settings.element  = data[2] || $element.prop( 'id' ) || settings.element || null;
 315          settings.addColor = data[3] ? '#' + data[3] : settings.addColor;
 316  
 317          if ( ! settings ) {
 318              return false;
 319          }
 320  
 321          if ( ! $element.is( '[id="' + settings.element + '-submit"]' ) ) {
 322              return ! wpList.add.call( list, $element, settings );
 323          }
 324  
 325          if ( ! settings.element ) {
 326              return true;
 327          }
 328  
 329          settings.action = 'add-' + settings.what;
 330          settings.nonce  = wpList.nonce( $element, settings );
 331  
 332          if ( ! wpAjax.validateForm( '#' + settings.element ) ) {
 333              return false;
 334          }
 335  
 336          settings.data = $.param( $.extend( {
 337              _ajax_nonce: settings.nonce,
 338              action:      settings.action
 339          }, wpAjax.unserialize( data[4] || '' ) ) );
 340  
 341          formValues = $( '#' + settings.element + ' :input' ).not( '[name="_ajax_nonce"], [name="_wpnonce"], [name="action"]' );
 342          formData   = typeof formValues.fieldSerialize === 'function' ? formValues.fieldSerialize() : formValues.serialize();
 343  
 344          if ( formData ) {
 345              settings.data += '&' + formData;
 346          }
 347  
 348          if ( typeof settings.addBefore === 'function' ) {
 349              settings = settings.addBefore( settings );
 350  
 351              if ( ! settings ) {
 352                  return true;
 353              }
 354          }
 355  
 356          if ( ! settings.data.match( /_ajax_nonce=[a-f0-9]+/ ) ) {
 357              return true;
 358          }
 359  
 360          settings.success = function( response ) {
 361              parsedResponse   = wpAjax.parseAjaxResponse( response, settings.response, settings.element );
 362              returnedResponse = response;
 363  
 364              if ( ! parsedResponse || parsedResponse.errors ) {
 365                  return false;
 366              }
 367  
 368              if ( true === parsedResponse ) {
 369                  return true;
 370              }
 371  
 372              $.each( parsedResponse.responses, function() {
 373                  wpList.add.call( list, this.data, $.extend( {}, settings, { // this.firstChild.nodevalue
 374                      position: this.position || 0,
 375                      id:       this.id || 0,
 376                      oldId:    this.oldId || null
 377                  } ) );
 378              } );
 379  
 380              list.wpList.recolor();
 381              $( list ).trigger( 'wpListAddEnd', [ settings, list.wpList ] );
 382              wpList.clear.call( list, '#' + settings.element );
 383          };
 384  
 385          settings.complete = function( jqXHR, status ) {
 386              if ( typeof settings.addAfter === 'function' ) {
 387                  settings.addAfter( returnedResponse, $.extend( {
 388                      xml:    jqXHR,
 389                      status: status,
 390                      parsed: parsedResponse
 391                  }, settings ) );
 392              }
 393          };
 394  
 395          $.ajax( settings );
 396  
 397          return false;
 398      },
 399  
 400      /**
 401       * Delete an item in the list via Ajax.
 402       *
 403       * @param {HTMLElement} element  A DOM element containing item data.
 404       * @param {Object}      settings Settings for this list.
 405       * @return {boolean} Whether the item was deleted.
 406       */
 407      ajaxDel: function( element, settings ) {
 408          var list     = this,
 409              $element = $( element ),
 410              data     = wpList.parseData( $element, 'delete' ),
 411              $eventTarget, parsedResponse, returnedResponse;
 412  
 413          settings = settings || {};
 414          settings = wpList.pre.call( list, $element, settings, 'delete' );
 415  
 416          settings.element  = data[2] || settings.element || null;
 417          settings.delColor = data[3] ? '#' + data[3] : settings.delColor;
 418  
 419          if ( ! settings || ! settings.element ) {
 420              return false;
 421          }
 422  
 423          settings.action = 'delete-' + settings.what;
 424          settings.nonce  = wpList.nonce( $element, settings );
 425  
 426          settings.data = $.extend( {
 427              _ajax_nonce: settings.nonce,
 428              action:      settings.action,
 429              id:          settings.element.split( '-' ).pop()
 430          }, wpAjax.unserialize( data[4] || '' ) );
 431  
 432          if ( typeof settings.delBefore === 'function' ) {
 433              settings = settings.delBefore( settings, list );
 434  
 435              if ( ! settings ) {
 436                  return true;
 437              }
 438          }
 439  
 440          if ( ! settings.data._ajax_nonce ) {
 441              return true;
 442          }
 443  
 444          $eventTarget = $( '#' + settings.element );
 445  
 446          if ( 'none' !== settings.delColor ) {
 447              $eventTarget.css( 'backgroundColor', settings.delColor ).fadeOut( 350, function() {
 448                  list.wpList.recolor();
 449                  $( list ).trigger( 'wpListDelEnd', [ settings, list.wpList ] );
 450              } );
 451          } else {
 452              list.wpList.recolor();
 453              $( list ).trigger( 'wpListDelEnd', [ settings, list.wpList ] );
 454          }
 455  
 456          settings.success = function( response ) {
 457              parsedResponse   = wpAjax.parseAjaxResponse( response, settings.response, settings.element );
 458              returnedResponse = response;
 459  
 460              if ( ! parsedResponse || parsedResponse.errors ) {
 461                  $eventTarget.stop().stop().css( 'backgroundColor', '#faa' ).show().queue( function() {
 462                      list.wpList.recolor();
 463                      $( this ).dequeue();
 464                  } );
 465  
 466                  return false;
 467              }
 468          };
 469  
 470          settings.complete = function( jqXHR, status ) {
 471              if ( typeof settings.delAfter === 'function' ) {
 472                  $eventTarget.queue( function() {
 473                      settings.delAfter( returnedResponse, $.extend( {
 474                          xml:    jqXHR,
 475                          status: status,
 476                          parsed: parsedResponse
 477                      }, settings ) );
 478                  } ).dequeue();
 479              }
 480          };
 481  
 482          $.ajax( settings );
 483  
 484          return false;
 485      },
 486  
 487      /**
 488       * Dim an item in the list via Ajax.
 489       *
 490       * @param {HTMLElement} element  A DOM element containing item data.
 491       * @param {Object}      settings Settings for this list.
 492       * @return {boolean} Whether the item was dim'ed.
 493       */
 494      ajaxDim: function( element, settings ) {
 495          var list     = this,
 496              $element = $( element ),
 497              data     = wpList.parseData( $element, 'dim' ),
 498              $eventTarget, isClass, color, dimColor, parsedResponse, returnedResponse;
 499  
 500          // Prevent hidden links from being clicked by hotkeys.
 501          if ( 'none' === $element.parent().css( 'display' ) ) {
 502              return false;
 503          }
 504  
 505          settings = settings || {};
 506          settings = wpList.pre.call( list, $element, settings, 'dim' );
 507  
 508          settings.element     = data[2] || settings.element || null;
 509          settings.dimClass    = data[3] || settings.dimClass || null;
 510          settings.dimAddColor = data[4] ? '#' + data[4] : settings.dimAddColor;
 511          settings.dimDelColor = data[5] ? '#' + data[5] : settings.dimDelColor;
 512  
 513          if ( ! settings || ! settings.element || ! settings.dimClass ) {
 514              return true;
 515          }
 516  
 517          settings.action = 'dim-' + settings.what;
 518          settings.nonce  = wpList.nonce( $element, settings );
 519  
 520          settings.data = $.extend( {
 521              _ajax_nonce: settings.nonce,
 522              action:      settings.action,
 523              id:          settings.element.split( '-' ).pop(),
 524              dimClass:    settings.dimClass
 525          }, wpAjax.unserialize( data[6] || '' ) );
 526  
 527          if ( typeof settings.dimBefore === 'function' ) {
 528              settings = settings.dimBefore( settings );
 529  
 530              if ( ! settings ) {
 531                  return true;
 532              }
 533          }
 534  
 535          $eventTarget = $( '#' + settings.element );
 536          isClass      = $eventTarget.toggleClass( settings.dimClass ).is( '.' + settings.dimClass );
 537          color        = wpList.getColor( $eventTarget );
 538          dimColor     = isClass ? settings.dimAddColor : settings.dimDelColor;
 539          $eventTarget.toggleClass( settings.dimClass );
 540  
 541          if ( 'none' !== dimColor ) {
 542              $eventTarget
 543                  .animate( { backgroundColor: dimColor }, 'fast' )
 544                  .queue( function() {
 545                      $eventTarget.toggleClass( settings.dimClass );
 546                      $( this ).dequeue();
 547                  } )
 548                  .animate( { backgroundColor: color }, {
 549                      complete: function() {
 550                          $( this ).css( 'backgroundColor', '' );
 551                          $( list ).trigger( 'wpListDimEnd', [ settings, list.wpList ] );
 552                      }
 553                  } );
 554          } else {
 555              $( list ).trigger( 'wpListDimEnd', [ settings, list.wpList ] );
 556          }
 557  
 558          if ( ! settings.data._ajax_nonce ) {
 559              return true;
 560          }
 561  
 562          settings.success = function( response ) {
 563              parsedResponse   = wpAjax.parseAjaxResponse( response, settings.response, settings.element );
 564              returnedResponse = response;
 565  
 566              if ( true === parsedResponse ) {
 567                  return true;
 568              }
 569  
 570              if ( ! parsedResponse || parsedResponse.errors ) {
 571                  $eventTarget.stop().stop().css( 'backgroundColor', '#ff3333' )[isClass ? 'removeClass' : 'addClass']( settings.dimClass ).show().queue( function() {
 572                      list.wpList.recolor();
 573                      $( this ).dequeue();
 574                  } );
 575  
 576                  return false;
 577              }
 578  
 579              /** @property {string} comment_link Link of the comment to be dimmed. */
 580              if ( 'undefined' !== typeof parsedResponse.responses[0].supplemental.comment_link ) {
 581                  var $submittedOn = $element.find( '.submitted-on' ),
 582                      $commentLink = $submittedOn.find( 'a' );
 583  
 584                  // Comment is approved; link the date field.
 585                  if ( '' !== parsedResponse.responses[0].supplemental.comment_link ) {
 586                      $submittedOn.html( $('<a></a>').text( $submittedOn.text() ).prop( 'href', parsedResponse.responses[0].supplemental.comment_link ) );
 587  
 588                  // Comment is not approved; unlink the date field.
 589                  } else if ( $commentLink.length ) {
 590                      $submittedOn.text( $commentLink.text() );
 591                  }
 592              }
 593          };
 594  
 595          settings.complete = function( jqXHR, status ) {
 596              if ( typeof settings.dimAfter === 'function' ) {
 597                  $eventTarget.queue( function() {
 598                      settings.dimAfter( returnedResponse, $.extend( {
 599                          xml:    jqXHR,
 600                          status: status,
 601                          parsed: parsedResponse
 602                      }, settings ) );
 603                  } ).dequeue();
 604              }
 605          };
 606  
 607          $.ajax( settings );
 608  
 609          return false;
 610      },
 611  
 612      /**
 613       * Returns the background color of the passed element.
 614       *
 615       * @param {jQuery|string} element Element to check.
 616       * @return {string} Background color value in HEX. Default: '#ffffff'.
 617       */
 618      getColor: function( element ) {
 619          return $( element ).css( 'backgroundColor' ) || '#ffffff';
 620      },
 621  
 622      /**
 623       * Adds something.
 624       *
 625       * @param {HTMLElement} element  A DOM element containing item data.
 626       * @param {Object}      settings Settings for this list.
 627       * @return {boolean} Whether the item was added.
 628       */
 629      add: function( element, settings ) {
 630          var $list    = $( this ),
 631              $element = $( element ),
 632              old      = false,
 633              position, reference;
 634  
 635          if ( 'string' === typeof settings ) {
 636              settings = { what: settings };
 637          }
 638  
 639          settings = $.extend( { position: 0, id: 0, oldId: null }, this.wpList.settings, settings );
 640  
 641          if ( ! $element.length || ! settings.what ) {
 642              return false;
 643          }
 644  
 645          if ( settings.oldId ) {
 646              old = $( '#' + settings.what + '-' + settings.oldId );
 647          }
 648  
 649          if ( settings.id && ( settings.id !== settings.oldId || ! old || ! old.length ) ) {
 650              $( '#' + settings.what + '-' + settings.id ).remove();
 651          }
 652  
 653          if ( old && old.length ) {
 654              old.before( $element );
 655              old.remove();
 656  
 657          } else if ( isNaN( settings.position ) ) {
 658              position = 'after';
 659  
 660              if ( '-' === settings.position.substr( 0, 1 ) ) {
 661                  settings.position = settings.position.substr( 1 );
 662                  position = 'before';
 663              }
 664  
 665              reference = $list.find( '#' + settings.position );
 666  
 667              if ( 1 === reference.length ) {
 668                  reference[position]( $element );
 669              } else {
 670                  $list.append( $element );
 671              }
 672  
 673          } else if ( 'comment' !== settings.what || 0 === $( '#' + settings.element ).length ) {
 674              if ( settings.position < 0 ) {
 675                  $list.prepend( $element );
 676              } else {
 677                  $list.append( $element );
 678              }
 679          }
 680  
 681          if ( settings.alt ) {
 682              $element.toggleClass( settings.alt, ( $list.children( ':visible' ).index( $element[0] ) + settings.altOffset ) % 2 );
 683          }
 684  
 685          if ( 'none' !== settings.addColor ) {
 686              $element.css( 'backgroundColor', settings.addColor ).animate( { backgroundColor: wpList.getColor( $element ) }, {
 687                  complete: function() {
 688                      $( this ).css( 'backgroundColor', '' );
 689                  }
 690              } );
 691          }
 692  
 693          // Add event handlers.
 694          $list.each( function( index, list ) {
 695              list.wpList.process( $element );
 696          } );
 697  
 698          return $element;
 699      },
 700  
 701      /**
 702       * Clears all input fields within the element passed.
 703       *
 704       * @param {string} elementId ID of the element to check, including leading #.
 705       */
 706      clear: function( elementId ) {
 707          var list     = this,
 708              $element = $( elementId ),
 709              type, tagName;
 710  
 711          // Bail if we're within the list.
 712          if ( list.wpList && $element.parents( '#' + list.id ).length ) {
 713              return;
 714          }
 715  
 716          // Check each input field.
 717          $element.find( ':input' ).each( function( index, input ) {
 718  
 719              // Bail if the form was marked to not to be cleared.
 720              if ( $( input ).parents( '.form-no-clear' ).length ) {
 721                  return;
 722              }
 723  
 724              type    = input.type.toLowerCase();
 725              tagName = input.tagName.toLowerCase();
 726  
 727              if ( 'text' === type || 'password' === type || 'textarea' === tagName ) {
 728                  input.value = '';
 729  
 730              } else if ( 'checkbox' === type || 'radio' === type ) {
 731                  input.checked = false;
 732  
 733              } else if ( 'select' === tagName ) {
 734                  input.selectedIndex = null;
 735              }
 736          } );
 737      },
 738  
 739      /**
 740       * Registers event handlers to add, delete, and dim items.
 741       *
 742       * @param {string} elementId ID of the element to process, including leading #.
 743       */
 744      process: function( elementId ) {
 745          var list     = this,
 746              $element = $( elementId || document );
 747  
 748          $element.on( 'submit', 'form[data-wp-lists^="add:' + list.id + ':"]', function() {
 749              return list.wpList.add( this );
 750          } );
 751  
 752          $element.on( 'click', '[data-wp-lists^="add:' + list.id + ':"], input[data-wp-lists^="add:' + list.id + ':"]', function() {
 753              return list.wpList.add( this );
 754          } );
 755  
 756          $element.on( 'click', '[data-wp-lists^="delete:' + list.id + ':"]', function() {
 757              return list.wpList.del( this );
 758          } );
 759  
 760          $element.on( 'click', '[data-wp-lists^="dim:' + list.id + ':"]', function() {
 761              return list.wpList.dim( this );
 762          } );
 763      },
 764  
 765      /**
 766       * Updates list item background colors.
 767       */
 768      recolor: function() {
 769          var list    = this,
 770              evenOdd = [':even', ':odd'],
 771              items;
 772  
 773          // Bail if there is no alternate class name specified.
 774          if ( ! list.wpList.settings.alt ) {
 775              return;
 776          }
 777  
 778          items = $( '.list-item:visible', list );
 779  
 780          if ( ! items.length ) {
 781              items = $( list ).children( ':visible' );
 782          }
 783  
 784          if ( list.wpList.settings.altOffset % 2 ) {
 785              evenOdd.reverse();
 786          }
 787  
 788          items.filter( evenOdd[0] ).addClass( list.wpList.settings.alt ).end();
 789          items.filter( evenOdd[1] ).removeClass( list.wpList.settings.alt );
 790      },
 791  
 792      /**
 793       * Sets up `process()` and `recolor()` functions.
 794       */
 795      init: function() {
 796          var $list = this;
 797  
 798          $list.wpList.process = function( element ) {
 799              $list.each( function() {
 800                  this.wpList.process( element );
 801              } );
 802          };
 803  
 804          $list.wpList.recolor = function() {
 805              $list.each( function() {
 806                  this.wpList.recolor();
 807              } );
 808          };
 809      }
 810  };
 811  
 812  /**
 813   * Initializes wpList object.
 814   *
 815   * @param {Object}           settings             The settings for the wpList instance.
 816   * @param {string}           settings.url         URL for ajax calls. Default: ajaxurl.
 817   * @param {string}           settings.type        The HTTP method to use for Ajax requests. Default: 'POST'.
 818   * @param {string}           settings.response    ID of the element the parsed ajax response will be stored in.
 819   *                                                Default: 'ajax-response'.
 820   *
 821   * @param {string}           settings.what        Default: ''.
 822   * @param {string}           settings.alt         CSS class name for alternate styling. Default: 'alternate'.
 823   * @param {number}           settings.altOffset   Offset to start alternate styling from. Default: 0.
 824   * @param {string}           settings.addColor    Hex code or 'none' to disable animation. Default: '#ffff33'.
 825   * @param {string}           settings.delColor    Hex code or 'none' to disable animation. Default: '#faafaa'.
 826   * @param {string}           settings.dimAddColor Hex code or 'none' to disable animation. Default: '#ffff33'.
 827   * @param {string}           settings.dimDelColor Hex code or 'none' to disable animation. Default: '#ff3333'.
 828   *
 829   * @param {wpList~confirm}   settings.confirm     Callback that's run before a request is made. Default: null.
 830   * @param {wpList~addBefore} settings.addBefore   Callback that's run before an item gets added to the list.
 831   *                                                Default: null.
 832   * @param {wpList~addAfter}  settings.addAfter    Callback that's run after an item got added to the list.
 833   *                                                Default: null.
 834   * @param {wpList~delBefore} settings.delBefore   Callback that's run before an item gets deleted from the list.
 835   *                                                Default: null.
 836   * @param {wpList~delAfter}  settings.delAfter    Callback that's run after an item got deleted from the list.
 837   *                                                Default: null.
 838   * @param {wpList~dimBefore} settings.dimBefore   Callback that's run before an item gets dim'd. Default: null.
 839   * @param {wpList~dimAfter}  settings.dimAfter    Callback that's run after an item got dim'd. Default: null.
 840   * @return {$.fn} wpList API function.
 841   */
 842  $.fn.wpList = function( settings ) {
 843      this.each( function( index, list ) {
 844          list.wpList = {
 845              settings: $.extend( {}, wpList.settings, { what: wpList.parseData( list, 'list' )[1] || '' }, settings )
 846          };
 847  
 848          $.each( functions, function( func, callback ) {
 849              list.wpList[func] = function( element, setting ) {
 850                  return wpList[callback].call( list, element, setting );
 851              };
 852          } );
 853      } );
 854  
 855      wpList.init.call( this );
 856      this.wpList.process();
 857  
 858      return this;
 859  };
 860  } ) ( jQuery );


Generated : Wed Sep 16 08:20:31 2026 Cross-referenced by PHPXref