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