PluginProbe
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce / 1.1.0.1
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce v1.1.0.1
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.0.1, at assets/js/rest-api.js

209 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 : {},
95 items : JSON.parse( XHR.responseText ),
96 items_count : XHR.getResponseHeader('x-wp-total') || 0,
97 callback : options.callback,
98
99 // AJAX response.
100 status : status,
101 XHR : XHR,
102 };
103
104 CartFlowsAPILocalCache.set(url, data, complete);
105 };
106 });
107
108 CartFlowsAPI = {
109
110 /**
111 * Debugging.
112 *
113 * @param {mixed} data Mixed data.
114 */
115 _log: function( data, format ) {
116
117 if( CartFlowsImportVars.debug ) {
118
119 if( 'table' === format ) {
120 console.table( data );
121 } else {
122 var date = new Date();
123 var time = date.toLocaleTimeString();
124
125 if (typeof data == 'object') {
126 console.log('%c ' + JSON.stringify( data ) + ' ' + time, 'background: #ededed; color: #444');
127 } else {
128 console.log('%c ' + data + ' ' + time, 'background: #ededed; color: #444');
129 }
130 }
131 }
132 },
133
134 _api_url : CartFlowsImportVars.server_rest_url,
135
136 _api_cached_request: function( data ) {
137
138 CartFlowsAPI._log( CartFlowsAPILocalCache.data, 'table' );
139 CartFlowsAPI._log( 'Current time ' + new Date().getTime() );
140 CartFlowsAPI._log( 'Cache expired in ' + parseInt( CartFlowsAPILocalCache.timeout ) / 1000 + ' seconds.' );
141
142 if( undefined !== data.args.trigger && '' !== data.args.trigger ) {
143 $(document).trigger( data.args.trigger, [data] );
144 }
145
146 if( data.callback && typeof data.callback == "function"){
147 data.callback( data );
148 }
149 },
150
151 _api_request: function( args, callback ) {
152
153 // Set API Request Data.
154 var data = {
155 url : CartFlowsAPI._api_url + args.slug,
156 cache : false,
157 args : args,
158 callback : callback,
159
160 author : 'brainstormforce',
161 };
162
163 // Set headers.
164 data.headers = CartFlowsImportVars.headers;
165
166 $.ajax( data )
167 .done(function( items, status, XHR ) {
168
169 if( 'success' === status && XHR.getResponseHeader('x-wp-total') ) {
170
171 var data = {
172 args : args,
173 items : items,
174 items_count : XHR.getResponseHeader('x-wp-total') || 0,
175 callback : callback,
176
177 // AJAX response.
178 status : status,
179 XHR : XHR,
180 };
181
182 if( undefined !== args.trigger && '' !== args.trigger ) {
183 $(document).trigger( args.trigger, [data] );
184 }
185
186 } else {
187 $(document).trigger( 'cartflows-api-request-error' );
188 }
189
190 if( callback && typeof callback == "function"){
191 callback( data );
192 }
193 })
194 .fail(function( jqXHR, textStatus ) {
195
196 $(document).trigger( 'cartflows-api-request-fail' );
197
198 })
199 .always(function() {
200
201 $(document).trigger( 'cartflows-api-request-always' );
202
203 });
204
205 },
206
207 };
208
209 })(jQuery);