author | ymh <ymh.work@gmail.com> |
Mon, 14 Oct 2019 17:39:30 +0200 | |
changeset 7 | cf61fcea0001 |
parent 5 | 5e2f62d02dcd |
child 9 | 177826044cd9 |
permissions | -rw-r--r-- |
5 | 1 |
/* global _wpUtilSettings */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3 |
/** @namespace wp */ |
0 | 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 |
* |
|
5 | 13 |
* Fetch a JavaScript template for an id, and return a templating function for it. |
0 | 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, |
|
5 | 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 |
*/ |
|
0 | 27 |
options = { |
28 |
evaluate: /<#([\s\S]+?)#>/g, |
|
29 |
interpolate: /\{\{\{([\s\S]+?)\}\}\}/g, |
|
30 |
escape: /\{\{([^\}]+?)\}\}(?!\})/g, |
|
31 |
variable: 'data' |
|
32 |
}; |
|
33 |
||
34 |
return function ( data ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
35 |
compiled = compiled || _.template( $( '#tmpl-' + id ).html(), options ); |
0 | 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 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
53 |
* @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
|
54 |
* to jQuery.ajax. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
55 |
* @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
|
56 |
* @return {$.promise} A jQuery promise that represents the request, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
57 |
* decorated with an abort() method. |
0 | 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 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
70 |
* @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
|
71 |
* to jQuery.ajax. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
72 |
* @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
|
73 |
* @return {$.promise} A jQuery promise that represents the request, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
74 |
* decorated with an abort() method. |
0 | 75 |
*/ |
76 |
send: function( action, options ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
77 |
var promise, deferred; |
0 | 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 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
91 |
deferred = $.Deferred( function( deferred ) { |
0 | 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() |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
102 |
deferred.jqXHR = $.ajax( options ).done( function( response ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
103 |
// Treat a response of 1 as successful for backward compatibility with existing handlers. |
0 | 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 |
}); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
114 |
}); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
115 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
116 |
promise = deferred.promise(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
117 |
promise.abort = function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
118 |
deferred.jqXHR.abort(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
119 |
return this; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
120 |
}; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
121 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
122 |
return promise; |
0 | 123 |
} |
124 |
}; |
|
125 |
||
126 |
}(jQuery)); |