PluginProbe
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce / 1.1.10
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce v1.1.10
3.3.0 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.1 trunk 1.0.4 1.1.0 1.1.0.1 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 All 162 releases
cartflows / assets / js / rest-api.js

rest-api.js in CartFlows – Funnel Builder & Checkout Plugin for WooCommerce 1.1.10, at assets/js/rest-api.js

207 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1
2 (function($){
3
4 var CartFlowsAPILocalCache = {
5
6 /**
7 * timeout for cache in millis
8 * @type {number}
9 */
10 timeout: 300000,
11
12 /**
13 * @type {{_: number, data: {}}}
14 **/
15 data: {},
16
17 /**
18 * Remove cache
19 *
20 * @param {[type]} url [description]
21 * @return {[type]} [description]
22 */
23 remove: function (url) {
24 delete CartFlowsAPILocalCache.data[url];
25 },
26
27 /**
28 * Check cache
29 *
30 * @param {[type]} url [description]
31 * @return {[type]} [description]
32 */
33 exist: function (url) {
34 return !! CartFlowsAPILocalCache.data[url] && ((new Date().getTime() - CartFlowsAPILocalCache.data[url]._) < CartFlowsAPILocalCache.timeout);
35 },
36
37 /**
38 * Get cache
39 *
40 * @param {[type]} url [description]
41 * @return {[type]} [description]
42 */
43 get: function (url) {
44 return CartFlowsAPILocalCache.data[url].data;
45 },
46
47 /**
48 * Set cache
49 *
50 * @param {[type]} url [description]
51 * @param {[type]} cachedData [description]
52 * @param {Function} callback [description]
53 */
54 set: function (url, cachedData, callback) {
55
56 CartFlowsAPILocalCache.remove(url);
57
58 CartFlowsAPILocalCache.data[url] = {
59 _ : new Date().getTime(),
60 data : cachedData
61 };
62
63 if( callback && typeof callback == "function"){
64 callback( cachedData );
65 }
66 }
67 };
68
69 $.ajaxPrefilter(function (options, originalOptions, jqXHR) {
70
71 if( 'brainstormforce' !== options.author ) {
72 return;
73 }
74
75 var complete = originalOptions.complete || $.noop,
76 url = originalOptions.url;
77
78 options.beforeSend = function () {
79
80 if (CartFlowsAPILocalCache.exist(url)) {
81
82 var data = CartFlowsAPILocalCache.get(url);
83
84 // Load from cache.
85 CartFlowsAPI._api_cached_request( data );
86 return false;
87 }
88 return true;
89 };
90 options.complete = function (XHR, status) {
91
92 var data = {
93 args : options.args,
94 items : ( XHR.responseText ) ? JSON.parse( XHR.responseText ) : '',
95 items_count : XHR.getResponseHeader('x-wp-total') || 0,
96 callback : options.callback,
97
98 // AJAX response.
99 status : status,
100 XHR : XHR,
101 };
102
103 CartFlowsAPILocalCache.set(url, data, complete);
104 };
105 });
106
107 CartFlowsAPI = {
108
109 /**
110 * Debugging.
111 *
112 * @param {mixed} data Mixed data.
113 */
114 _log: function( data, format ) {
115
116 if( CartFlowsImportVars.debug ) {
117
118 if( 'table' === format ) {
119 console.table( data );
120 } else {
121 var date = new Date();
122 var time = date.toLocaleTimeString();
123
124 if (typeof data == 'object') {
125 console.log('%c ' + JSON.stringify( data ) + ' ' + time, 'background: #ededed; color: #444');
126 } else {
127 console.log('%c ' + data + ' ' + time, 'background: #ededed; color: #444');
128 }
129 }
130 }
131 },
132
133 _api_url : CartFlowsImportVars.server_rest_url,
134
135 _api_cached_request: function( data ) {
136
137 CartFlowsAPI._log( CartFlowsAPILocalCache.data, 'table' );
138 CartFlowsAPI._log( 'Current time ' + new Date().getTime() );
139 CartFlowsAPI._log( 'Cache expired in ' + parseInt( CartFlowsAPILocalCache.timeout ) / 1000 + ' seconds.' );
140
141 if( undefined !== data.args.trigger && '' !== data.args.trigger ) {
142 $(document).trigger( data.args.trigger, [data] );
143 }
144
145 if( data.callback && typeof data.callback == "function"){
146 data.callback( data );
147 }
148 },
149
150 _api_request: function( args, callback ) {
151
152 // Set API Request Data.
153 var data = {
154 url : CartFlowsAPI._api_url + args.slug,
155 args : args,
156 callback : callback,
157
158 author : 'brainstormforce',
159 };
160
161 // Set headers.
162 data.headers = CartFlowsImportVars.headers;
163
164 $.ajax( data )
165 .done(function( items, status, XHR ) {
166
167 if( 'success' === status && XHR.getResponseHeader('x-wp-total') ) {
168
169 var data = {
170 args : args,
171 items : items,
172 items_count : XHR.getResponseHeader('x-wp-total') || 0,
173 callback : callback,
174
175 // AJAX response.
176 status : status,
177 XHR : XHR,
178 };
179
180 if( undefined !== args.trigger && '' !== args.trigger ) {
181 $(document).trigger( args.trigger, [data] );
182 }
183
184 } else {
185 $(document).trigger( 'cartflows-api-request-error' );
186 }
187
188 if( callback && typeof callback == "function"){
189 callback( data );
190 }
191 })
192 .fail(function( jqXHR, textStatus ) {
193
194 $(document).trigger( 'cartflows-api-request-fail', [data, jqXHR, textStatus] );
195
196 })
197 .always(function() {
198
199 $(document).trigger( 'cartflows-api-request-always' );
200
201 });
202
203 },
204
205 };
206
207 })(jQuery);