[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   1  /**
   2   * @output wp-includes/js/wp-util.js
   3   */
   4  
   5  /* global _wpUtilSettings */
   6  
   7  /** @namespace wp */
   8  window.wp = window.wp || {};
   9  
  10  (function ($) {
  11      // Check for the utility settings.
  12      var settings = typeof _wpUtilSettings === 'undefined' ? {} : _wpUtilSettings;
  13  
  14      /**
  15       * wp.template( id )
  16       *
  17       * Fetch a JavaScript template for an id, and return a templating function for it.
  18       *
  19       * @param {string} id A string that corresponds to a DOM element with an id prefixed with "tmpl-".
  20       *                    For example, "attachment" maps to "tmpl-attachment".
  21       * @return {function} A function that lazily-compiles the template requested.
  22       */
  23      wp.template = _.memoize(function ( id ) {
  24          var compiled,
  25              /*
  26               * Underscore's default ERB-style templates are incompatible with PHP
  27               * when asp_tags is enabled, so WordPress uses Mustache-inspired templating syntax.
  28               *
  29               * @see trac ticket #22344.
  30               */
  31              options = {
  32                  evaluate:    /<#([\s\S]+?)#>/g,
  33                  interpolate: /\{\{\{([\s\S]+?)\}\}\}/g,
  34                  escape:      /\{\{([^\}]+?)\}\}(?!\})/g,
  35                  variable:    'data'
  36              };
  37  
  38          return function ( data ) {
  39              if ( ! document.getElementById( 'tmpl-' + id ) ) {
  40                  throw new Error( 'Template not found: ' + '#tmpl-' + id );
  41              }
  42              compiled = compiled || _.template( $( '#tmpl-' + id ).html(),  options );
  43              return compiled( data );
  44          };
  45      });
  46  
  47      /*
  48       * wp.ajax
  49       * ------
  50       *
  51       * Tools for sending ajax requests with JSON responses and built in error handling.
  52       * Mirrors and wraps jQuery's ajax APIs.
  53       */
  54      wp.ajax = {
  55          settings: settings.ajax || {},
  56  
  57          /**
  58           * wp.ajax.post( [action], [data] )
  59           *
  60           * Sends a POST request to WordPress.
  61           *
  62           * @param {(string|Object)} action The slug of the action to fire in WordPress or options passed
  63           *                                 to jQuery.ajax.
  64           * @param {Object=}         data   Optional. The data to populate $_POST with.
  65           * @return {$.promise} A jQuery promise that represents the request,
  66           *                     decorated with an abort() method.
  67           */
  68          post: function( action, data ) {
  69              return wp.ajax.send({
  70                  data: _.isObject( action ) ? action : _.extend( data || {}, { action: action })
  71              });
  72          },
  73  
  74          /**
  75           * wp.ajax.send( [action], [options] )
  76           *
  77           * Sends a POST request to WordPress.
  78           *
  79           * @param {(string|Object)} action  The slug of the action to fire in WordPress or options passed
  80           *                                  to jQuery.ajax.
  81           * @param {Object=}         options Optional. The options passed to jQuery.ajax.
  82           * @return {$.promise} A jQuery promise that represents the request,
  83           *                     decorated with an abort() method.
  84           */
  85          send: function( action, options ) {
  86              var promise, deferred;
  87              if ( _.isObject( action ) ) {
  88                  options = action;
  89              } else {
  90                  options = options || {};
  91                  options.data = _.extend( options.data || {}, { action: action });
  92              }
  93  
  94              options = _.defaults( options || {}, {
  95                  type:    'POST',
  96                  url:     wp.ajax.settings.url,
  97                  context: this
  98              });
  99  
 100              deferred = $.Deferred( function( deferred ) {
 101                  // Transfer success/error callbacks.
 102                  if ( options.success ) {
 103                      deferred.done( options.success );
 104                  }
 105  
 106                  if ( options.error ) {
 107                      deferred.fail( options.error );
 108                  }
 109  
 110                  delete options.success;
 111                  delete options.error;
 112  
 113                  // Use with PHP's wp_send_json_success() and wp_send_json_error().
 114                  deferred.jqXHR = $.ajax( options ).done( function( response ) {
 115                      // Treat a response of 1 as successful for backward compatibility with existing handlers.
 116                      if ( response === '1' || response === 1 ) {
 117                          response = { success: true };
 118                      }
 119  
 120                      if ( _.isObject( response ) && ! _.isUndefined( response.success ) ) {
 121  
 122                          // When handling a media attachments request, get the total attachments from response headers.
 123                          var context = this;
 124                          deferred.done( function() {
 125                              if (
 126                                  action &&
 127                                  action.data &&
 128                                  'query-attachments' === action.data.action &&
 129                                  deferred.jqXHR.hasOwnProperty( 'getResponseHeader' ) &&
 130                                  deferred.jqXHR.getResponseHeader( 'X-WP-Total' )
 131                              ) {
 132                                  context.totalAttachments = parseInt( deferred.jqXHR.getResponseHeader( 'X-WP-Total' ), 10 );
 133                              } else {
 134                                  context.totalAttachments = 0;
 135                              }
 136                          } );
 137                          deferred[ response.success ? 'resolveWith' : 'rejectWith' ]( this, [response.data] );
 138                      } else {
 139                          deferred.rejectWith( this, [response] );
 140                      }
 141                  }).fail( function() {
 142                      deferred.rejectWith( this, arguments );
 143                  });
 144              });
 145  
 146              promise = deferred.promise();
 147              promise.abort = function() {
 148                  deferred.jqXHR.abort();
 149                  return this;
 150              };
 151  
 152              return promise;
 153          }
 154      };
 155  
 156  }(jQuery));


Generated : Fri Nov 15 08:20:01 2024 Cross-referenced by PHPXref