PluginProbe
Force Refresh / 1.1.2
Force Refresh v1.1.2
3.2.1 3.2.0 3.1.2 3.1.1 3.1.0 trunk 1.0.0 1.1.0 1.1.1 1.1.2 1.2.0 2.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.11.0 2.11.1 2.12.0 All 54 releases
force-refresh / library / src / js / force-refresh.js

force-refresh.js in Force Refresh 1.1.2, at library/src/js/force-refresh.js

303 lines 8.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 (function ($, window, document) {
2
3 /**
4 * Class for handeling forced site reloads.
5 *
6 * @type {Object}
7 *
8 * @class
9 * @global
10 */
11 var ForceReload = {
12
13 class_name : "ForceReload",
14
15 default_options : {
16
17 // Global variable from WordPress
18 api_url : force_refresh_js_object.ajax_url,
19
20 elements : {
21
22 force_refresh_admin_form : "#force-refresh-admin",
23
24 force_refresh_admin_notice_container : "#alert-container"
25 },
26 timing : {
27
28 update_site_version_interval_in_seconds : 120
29
30 }
31
32
33 },
34
35 init : function () {
36
37 // Add our options to the object
38 this.options = this.default_options;
39
40 console.log(force_refresh_js_object);
41
42 // Update the current version of the site
43 this.bindGetSiteVersion();
44
45 },
46
47 /**
48 * Method to bind actions to get the current site version.
49 *
50 * @return {void}
51 */
52 bindGetSiteVersion : function(){
53
54 // Localize this since we'll be putting it inside of setInterval
55 var base = this;
56
57 // Do the initial call
58 base.getSiteVersion();
59
60 // We need this to run on a regular interval
61 setInterval(function(){
62
63 base.getSiteVersion();
64
65 }, base.options.timing.update_site_version_interval_in_seconds * 1000);
66
67 },
68
69 /**
70 * Method to make call to get the current site version
71 *
72 * @return {void}
73 */
74 getSiteVersion : function(){
75
76 console.log(this.options.api_url);
77
78 // Make the call
79 this.ajaxCall(
80 this.options.api_url,
81 "GET",
82 {
83
84 // The success callback
85 success : this.getSiteVersionCallbackSuccess,
86
87 // The failure callback
88 fail : this.getSiteVersionCallbackSuccess
89 },
90 {
91 action : "force_refresh_get_site_version"
92 }
93 );
94
95 },
96
97 /**
98 * Method used to handle successfull calls to the API to request the current site version.
99 *
100 * @param {object} return_object The return object from the ajax call
101 *
102 * @return {void}
103 */
104 getSiteVersionCallbackSuccess : function(return_object){
105
106 // Get the return data
107 var return_data = return_object.ajax_data.return_data;
108
109 // Declare the site version we just found
110 var current_site_version = return_data.current_site_version;
111
112 // Retrieve the current site version
113 var stored_site_version = $("html").data("site-version");
114
115 // If there isn't a stored site version, we haven't attached it to the html
116 if (!stored_site_version){
117
118 this.debug("No stored version. Storing new version (ver. " + current_site_version + ")");
119
120 $("html")
121 .data("site-version", current_site_version);
122
123 }
124
125 // Otherwise, there is a stored site version we can compare
126 else {
127
128 // Now, compare the two versions. If the current version is different than the stored version, we need to refresh the page
129 if (current_site_version !== stored_site_version){
130
131 this.debug("New version available. Refreshing...");
132
133 // Reload the page
134 location.reload();
135
136 }
137
138 // Otherwise, we're up to date!
139 else {
140
141 this.debug("Site up-to-date (ver. " + current_site_version + ")");
142
143 }
144 }
145
146 },
147
148 /**
149 * Method used to submit AJAX calls and execute promises
150 *
151 * @param {string} action The action for the call to execute
152 * @param {object} callback_object An object of done and always callbacks
153 * @param {object} data_object An object of data to send to the API
154 * @param {object} additional_arguments An object of data to send to the done callback
155 * after the call is complete
156 * @return {void}
157 *
158 *
159 * @instance
160 */
161 ajaxCall : function(api_url, method, callback_object, data_object, additional_arguments){
162
163 // If data_object is null, than create an empty object
164 data_object = data_object ? data_object : {};
165
166 // Make additional arguments an object if null
167 additional_arguments = additional_arguments ? additional_arguments : {};
168
169 // Add data object to additional arguments
170 additional_arguments.data_object = data_object;
171
172 this.debug("Submitting AJAX call...");
173
174 $.ajax({
175 type : method,
176 url : api_url,
177 context : this,
178 data : data_object,
179 dataType : 'JSON'
180 })
181
182 // Done promise
183 .done(function(ajax_data) {
184
185 this.debug("AJAX call finished.");
186
187 })
188
189 // Success promise
190 .success(function(ajax_data){
191
192 // Check to make sure the call was successful
193 if (ajax_data.success){
194
195 // If there's a specified callback
196 if (callback_object.success){
197
198 // Execute the callback and include the object context, data, and arguments
199 callback_object.success.call(this, {"ajax_data" : ajax_data, "additional_arguments" : additional_arguments});
200
201 }
202 }
203
204 else {
205
206 this.error(ajax_data);
207 }
208
209 })
210
211 // Always promise
212 .always(function(){
213
214 if (callback_object.always){
215
216 callback_object.always.call(this);
217
218 }
219
220 })
221
222 // Fail promise
223 .fail(function(ajax_data){
224
225 // If there's an error
226 this.error("AJAX error. Error below:");
227
228 // Log the error
229 this.error(ajax_data);
230
231 // If there's a specified callback for failing
232 if (callback_object.fail){
233
234 // Execute the callback and include the object context, data, and arguments
235 callback_object.fail.call(this, {"ajax_data" : ajax_data.responseJSON, "additional_arguments" : additional_arguments});
236
237 }
238 })
239 ;
240 },
241
242
243 /**
244 * Method used to debug info to console
245 *
246 * @param {string | object} message The message or object to log
247 *
248 * @return {void}
249 *
250 * @instance
251 */
252 debug: function(message){
253
254 message = typeof(message) === "string" ? message : JSON.stringify(message);
255
256 console.log(this.class_name + " - " + message);
257
258 },
259
260 /**
261 * Method used to log errors to console
262 *
263 * @param {string | object} message The message or object to log
264 *
265 * @return {void}
266 *
267 * @instance
268 */
269 error: function(message){
270
271 message = typeof(message) === "string" ? message : JSON.stringify(message);
272
273 console.error(this.class_name + " - " + message);
274 },
275
276 /**
277 * Method used to log warnings to console
278 *
279 * @param {string | object} message The message or object to log
280 *
281 * @return {void}
282 *
283 * @instance
284 */
285 warn: function(message){
286
287 message = typeof(message) === "string" ? message : JSON.stringify(message);
288
289 console.warn(this.class_name + " - " + message);
290 }
291
292 };
293
294 $(function(){
295
296 var force_reload_object = Object.create(ForceReload);
297
298 force_reload_object.init();
299
300 });
301
302
303 }(jQuery, window, document));