author | ymh <ymh.work@gmail.com> |
Mon, 14 Oct 2019 18:28:13 +0200 | |
changeset 9 | 177826044cd9 |
parent 7 | cf61fcea0001 |
child 16 | a86126ab1dd4 |
permissions | -rw-r--r-- |
9 | 1 |
/** |
2 |
* @output wp-includes/js/wp-util.js |
|
3 |
*/ |
|
4 |
||
5 | 5 |
/* global _wpUtilSettings */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7 |
/** @namespace wp */ |
0 | 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 |
* |
|
5 | 17 |
* Fetch a JavaScript template for an id, and return a templating function for it. |
0 | 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, |
|
5 | 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 |
*/ |
|
0 | 31 |
options = { |
32 |
evaluate: /<#([\s\S]+?)#>/g, |
|
33 |
interpolate: /\{\{\{([\s\S]+?)\}\}\}/g, |
|
34 |
escape: /\{\{([^\}]+?)\}\}(?!\})/g, |
|
35 |
variable: 'data' |
|
36 |
}; |
|
37 |
||
38 |
return function ( data ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
39 |
compiled = compiled || _.template( $( '#tmpl-' + id ).html(), options ); |
0 | 40 |
return compiled( data ); |
41 |
}; |
|
42 |
}); |
|
43 |
||
44 |
// wp.ajax |
|
45 |
// ------ |
|
46 |
// |
|
47 |
// Tools for sending ajax requests with JSON responses and built in error handling. |
|
48 |
// Mirrors and wraps jQuery's ajax APIs. |
|
49 |
wp.ajax = { |
|
50 |
settings: settings.ajax || {}, |
|
51 |
||
52 |
/** |
|
53 |
* wp.ajax.post( [action], [data] ) |
|
54 |
* |
|
55 |
* Sends a POST request to WordPress. |
|
56 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
57 |
* @param {(string|object)} action The slug of the action to fire in WordPress or options passed |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
58 |
* to jQuery.ajax. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
59 |
* @param {object=} data Optional. The data to populate $_POST with. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
60 |
* @return {$.promise} A jQuery promise that represents the request, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
61 |
* decorated with an abort() method. |
0 | 62 |
*/ |
63 |
post: function( action, data ) { |
|
64 |
return wp.ajax.send({ |
|
65 |
data: _.isObject( action ) ? action : _.extend( data || {}, { action: action }) |
|
66 |
}); |
|
67 |
}, |
|
68 |
||
69 |
/** |
|
70 |
* wp.ajax.send( [action], [options] ) |
|
71 |
* |
|
72 |
* Sends a POST request to WordPress. |
|
73 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
74 |
* @param {(string|object)} action The slug of the action to fire in WordPress or options passed |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
75 |
* to jQuery.ajax. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
76 |
* @param {object=} options Optional. The options passed to jQuery.ajax. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
77 |
* @return {$.promise} A jQuery promise that represents the request, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
78 |
* decorated with an abort() method. |
0 | 79 |
*/ |
80 |
send: function( action, options ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
81 |
var promise, deferred; |
0 | 82 |
if ( _.isObject( action ) ) { |
83 |
options = action; |
|
84 |
} else { |
|
85 |
options = options || {}; |
|
86 |
options.data = _.extend( options.data || {}, { action: action }); |
|
87 |
} |
|
88 |
||
89 |
options = _.defaults( options || {}, { |
|
90 |
type: 'POST', |
|
91 |
url: wp.ajax.settings.url, |
|
92 |
context: this |
|
93 |
}); |
|
94 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
95 |
deferred = $.Deferred( function( deferred ) { |
0 | 96 |
// Transfer success/error callbacks. |
97 |
if ( options.success ) |
|
98 |
deferred.done( options.success ); |
|
99 |
if ( options.error ) |
|
100 |
deferred.fail( options.error ); |
|
101 |
||
102 |
delete options.success; |
|
103 |
delete options.error; |
|
104 |
||
105 |
// Use with PHP's wp_send_json_success() and wp_send_json_error() |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
106 |
deferred.jqXHR = $.ajax( options ).done( function( response ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
107 |
// Treat a response of 1 as successful for backward compatibility with existing handlers. |
0 | 108 |
if ( response === '1' || response === 1 ) |
109 |
response = { success: true }; |
|
110 |
||
111 |
if ( _.isObject( response ) && ! _.isUndefined( response.success ) ) |
|
112 |
deferred[ response.success ? 'resolveWith' : 'rejectWith' ]( this, [response.data] ); |
|
113 |
else |
|
114 |
deferred.rejectWith( this, [response] ); |
|
115 |
}).fail( function() { |
|
116 |
deferred.rejectWith( this, arguments ); |
|
117 |
}); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
118 |
}); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
119 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
120 |
promise = deferred.promise(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
121 |
promise.abort = function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
122 |
deferred.jqXHR.abort(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
123 |
return this; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
124 |
}; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
125 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
126 |
return promise; |
0 | 127 |
} |
128 |
}; |
|
129 |
||
130 |
}(jQuery)); |