home *** CD-ROM | disk | FTP | other *** search
/ HTML Examples / WP.iso / wordpress / wp-includes / js / wp-util.js < prev    next >
Encoding:
JavaScript  |  2017-09-08  |  3.8 KB  |  127 lines

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