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