| 1 |
(function ($, window, document) { |
| 2 |
|
| 3 |
/** |
| 4 |
* Class handling for the admin portion of ForceReloadAdmin |
| 5 |
* |
| 6 |
* @type {Object} |
| 7 |
* |
| 8 |
* @class |
| 9 |
* @global |
| 10 |
* |
| 11 |
* @since 1.0.0 |
| 12 |
*/ |
| 13 |
var ForceReloadAdmin = { |
| 14 |
|
| 15 |
class_name : "ForceReloadAdmin", |
| 16 |
|
| 17 |
default_options : { |
| 18 |
|
| 19 |
// Global variable from WordPress |
| 20 |
api_url : ajaxurl, |
| 21 |
|
| 22 |
elements : { |
| 23 |
|
| 24 |
force_refresh_admin_form : "#force-refresh-admin", |
| 25 |
|
| 26 |
force_refresh_admin_notice_container : "#alert-container" |
| 27 |
} |
| 28 |
|
| 29 |
}, |
| 30 |
|
| 31 |
/** |
| 32 |
* Our init method |
| 33 |
* |
| 34 |
* @return {void} |
| 35 |
*/ |
| 36 |
init : function () { |
| 37 |
|
| 38 |
// Add options to the class |
| 39 |
this.options = this.default_options; |
| 40 |
|
| 41 |
// Bind the form updates button |
| 42 |
this.bindFormUpdates(this.options.elements.force_refresh_admin_form); |
| 43 |
|
| 44 |
}, |
| 45 |
|
| 46 |
/** |
| 47 |
* Method for binding actions to the admin form for Force Refresh. |
| 48 |
* |
| 49 |
* @param {string} element The form element |
| 50 |
* |
| 51 |
* @return {void} |
| 52 |
*/ |
| 53 |
bindFormUpdates : function(element){ |
| 54 |
|
| 55 |
// Declare base outside of block |
| 56 |
var base = this; |
| 57 |
|
| 58 |
$(element) |
| 59 |
.submit(function(e){ |
| 60 |
|
| 61 |
// Remove any existing alerts |
| 62 |
$(base.options.elements.force_refresh_admin_notice_container) |
| 63 |
.empty(); |
| 64 |
|
| 65 |
// We're handling the action ourselves, so prevent the page from refreshing |
| 66 |
e.preventDefault(); |
| 67 |
|
| 68 |
base.ajaxCall( |
| 69 |
base.options.api_url, |
| 70 |
"POST", |
| 71 |
{ |
| 72 |
success: base.refreshSiteCallbackSuccess, |
| 73 |
|
| 74 |
fail: base.refreshSiteCallbackFailure |
| 75 |
|
| 76 |
}, |
| 77 |
{ |
| 78 |
action : "force_refresh_update_site_version", |
| 79 |
|
| 80 |
nonce : force_refresh_local_js.nonce |
| 81 |
|
| 82 |
} |
| 83 |
); |
| 84 |
|
| 85 |
}); |
| 86 |
|
| 87 |
}, |
| 88 |
|
| 89 |
/** |
| 90 |
* Method used as a callback for succesful requests |
| 91 |
* |
| 92 |
* @param {object} return_data The return data |
| 93 |
* |
| 94 |
* @return {void} |
| 95 |
*/ |
| 96 |
refreshSiteCallbackSuccess: function(return_data){ |
| 97 |
|
| 98 |
// Declare base outside of block |
| 99 |
var base = this; |
| 100 |
|
| 101 |
// Do the spin |
| 102 |
this.spinLogo(); |
| 103 |
|
| 104 |
// Wait unti the logo is done spinning to alert the user |
| 105 |
setTimeout(function(){ |
| 106 |
|
| 107 |
// Get the ajax data |
| 108 |
var ajax_data = return_data.ajax_data; |
| 109 |
|
| 110 |
// Add the notice |
| 111 |
base.addNotice(return_data.ajax_data.status_text); |
| 112 |
|
| 113 |
}, 1000); |
| 114 |
|
| 115 |
|
| 116 |
}, |
| 117 |
|
| 118 |
/** |
| 119 |
* Method used as a callback for failed requests |
| 120 |
* |
| 121 |
* @param {object} return_data The return data |
| 122 |
* |
| 123 |
* @return {void} |
| 124 |
*/ |
| 125 |
refreshSiteCallbackFailure : function(return_data){ |
| 126 |
|
| 127 |
// Get the ajax data |
| 128 |
var ajax_data = return_data.ajax_data; |
| 129 |
|
| 130 |
// Add the notice |
| 131 |
this.addNotice(return_data.ajax_data.status_text, "error"); |
| 132 |
|
| 133 |
}, |
| 134 |
|
| 135 |
/** |
| 136 |
* Method to add alerts to the admin screen |
| 137 |
* |
| 138 |
* @param {string} alert_text The text to alert the user |
| 139 |
* @param {string} alert_type The type of alert |
| 140 |
* |
| 141 |
* @return {void} |
| 142 |
*/ |
| 143 |
addNotice : function(notice_text, notice_type){ |
| 144 |
|
| 145 |
// Declaure our default |
| 146 |
notice_type = notice_type ? notice_type : "success"; |
| 147 |
|
| 148 |
// Create the notice |
| 149 |
var notice_html = "<div class=\"notice notice-invisible notice-hidden notice-" + notice_type + " is-dismissible\"><p><strong>" + notice_text + "</strong></p></div>"; |
| 150 |
|
| 151 |
// We need to get the height for this element. To do this, append it to the HTML and get the height (before removing it, of course) |
| 152 |
$("body") |
| 153 |
.append(notice_html) |
| 154 |
.find(".notice") |
| 155 |
.removeClass("notice-invisible") |
| 156 |
.addClass("notice-force-refresh-tmp"); |
| 157 |
|
| 158 |
// Get the height (it can change depending on the return message) |
| 159 |
var alert_height = $("body").find(".notice-force-refresh-tmp").outerHeight(); |
| 160 |
|
| 161 |
// Remove the alert |
| 162 |
$("body") |
| 163 |
.find(".notice-force-refresh-tmp") |
| 164 |
.remove(); |
| 165 |
|
| 166 |
// Add the real alert |
| 167 |
$(this.options.elements.force_refresh_admin_notice_container) |
| 168 |
.empty() |
| 169 |
.html(notice_html) |
| 170 |
.find(".notice") |
| 171 |
.css("height", 0) |
| 172 |
.removeClass("notice-hidden") |
| 173 |
.animate({ |
| 174 |
"height": alert_height + "px" |
| 175 |
}, 500, function(){ |
| 176 |
|
| 177 |
// Fade in after height animation completes |
| 178 |
$(this) |
| 179 |
.removeClass("notice-invisible") |
| 180 |
// Set the heigh to auto |
| 181 |
.css("height", "auto"); |
| 182 |
|
| 183 |
}); |
| 184 |
|
| 185 |
}, |
| 186 |
|
| 187 |
/** |
| 188 |
* Method used for animating the logo. |
| 189 |
* |
| 190 |
* @return {void} |
| 191 |
*/ |
| 192 |
spinLogo : function(){ |
| 193 |
|
| 194 |
// Add the class to make it spin |
| 195 |
$(".site-refresh-inner .logo") |
| 196 |
.addClass("logo-spin"); |
| 197 |
|
| 198 |
// After the animation is done, remove the class |
| 199 |
setTimeout(function(){ |
| 200 |
|
| 201 |
$(".site-refresh-inner .logo.logo-spin") |
| 202 |
.removeClass("logo-spin"); |
| 203 |
|
| 204 |
}, 1000); |
| 205 |
|
| 206 |
}, |
| 207 |
|
| 208 |
/** |
| 209 |
* Method used to submit AJAX calls and execute promises |
| 210 |
* |
| 211 |
* @param {string} action The action for the call to execute |
| 212 |
* @param {object} callback_object An object of done and always callbacks |
| 213 |
* @param {object} data_object An object of data to send to the API |
| 214 |
* @param {object} additional_arguments An object of data to send to the done callback |
| 215 |
* after the call is complete |
| 216 |
* @return {void} |
| 217 |
* |
| 218 |
* |
| 219 |
* @instance |
| 220 |
*/ |
| 221 |
ajaxCall : function(api_url, method, callback_object, data_object, additional_arguments){ |
| 222 |
|
| 223 |
// If data_object is null, than create an empty object |
| 224 |
data_object = data_object ? data_object : {}; |
| 225 |
|
| 226 |
// Make additional arguments an object if null |
| 227 |
additional_arguments = additional_arguments ? additional_arguments : {}; |
| 228 |
|
| 229 |
// Add data object to additional arguments |
| 230 |
additional_arguments.data_object = data_object; |
| 231 |
|
| 232 |
this.debug("Submitting AJAX call..."); |
| 233 |
|
| 234 |
$.ajax({ |
| 235 |
type : method, |
| 236 |
url : api_url, |
| 237 |
context : this, |
| 238 |
data : data_object, |
| 239 |
dataType : 'JSON' |
| 240 |
}) |
| 241 |
|
| 242 |
// Done promise |
| 243 |
.done(function(ajax_data) { |
| 244 |
|
| 245 |
this.debug("AJAX call finished."); |
| 246 |
|
| 247 |
}) |
| 248 |
|
| 249 |
// Success promise |
| 250 |
.success(function(ajax_data){ |
| 251 |
|
| 252 |
// Check to make sure the call was successful |
| 253 |
if (ajax_data.success){ |
| 254 |
|
| 255 |
// If there's a specified callback |
| 256 |
if (callback_object.success){ |
| 257 |
|
| 258 |
// Execute the callback and include the object context, data, and arguments |
| 259 |
callback_object.success.call(this, {"ajax_data" : ajax_data, "additional_arguments" : additional_arguments}); |
| 260 |
|
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
else { |
| 265 |
|
| 266 |
this.error(ajax_data); |
| 267 |
} |
| 268 |
|
| 269 |
}) |
| 270 |
|
| 271 |
// Always promise |
| 272 |
.always(function(){ |
| 273 |
|
| 274 |
if (callback_object.always){ |
| 275 |
|
| 276 |
callback_object.always.call(this); |
| 277 |
|
| 278 |
} |
| 279 |
|
| 280 |
}) |
| 281 |
|
| 282 |
// Fail promise |
| 283 |
.fail(function(ajax_data){ |
| 284 |
|
| 285 |
// If there's an error |
| 286 |
this.error("AJAX error. Error below:"); |
| 287 |
|
| 288 |
// Log the error |
| 289 |
this.error(ajax_data); |
| 290 |
|
| 291 |
// If there's a specified callback for failing |
| 292 |
if (callback_object.fail){ |
| 293 |
|
| 294 |
// Execute the callback and include the object context, data, and arguments |
| 295 |
callback_object.fail.call(this, {"ajax_data" : ajax_data.responseJSON, "additional_arguments" : additional_arguments}); |
| 296 |
|
| 297 |
} |
| 298 |
}) |
| 299 |
; |
| 300 |
}, |
| 301 |
|
| 302 |
/** |
| 303 |
* Method used to debug info to console |
| 304 |
* |
| 305 |
* @param {string | object } message The message or object to log |
| 306 |
* |
| 307 |
* @return {void} |
| 308 |
*/ |
| 309 |
debug: function(message){ |
| 310 |
|
| 311 |
message = typeof(message) === "string" ? message : JSON.stringify(message); |
| 312 |
|
| 313 |
console.debug(this.class_name + " - " + message); |
| 314 |
|
| 315 |
}, |
| 316 |
|
| 317 |
/** |
| 318 |
* Method used to log errors to console |
| 319 |
* |
| 320 |
* @param {string | object} message The message or object to log |
| 321 |
* |
| 322 |
* @return {void} |
| 323 |
* |
| 324 |
* @instance |
| 325 |
*/ |
| 326 |
error: function(message){ |
| 327 |
|
| 328 |
message = typeof(message) === "string" ? message : JSON.stringify(message); |
| 329 |
|
| 330 |
console.error(this.class_name + " - " + message); |
| 331 |
}, |
| 332 |
|
| 333 |
/** |
| 334 |
* Method used to log warnings to console |
| 335 |
* |
| 336 |
* @param {string | object} message The message or object to log |
| 337 |
* |
| 338 |
* @return {void} |
| 339 |
* |
| 340 |
* @instance |
| 341 |
*/ |
| 342 |
warn: function(message){ |
| 343 |
|
| 344 |
message = typeof(message) === "string" ? message : JSON.stringify(message); |
| 345 |
|
| 346 |
console.warn(this.class_name + " - " + message); |
| 347 |
} |
| 348 |
|
| 349 |
}; |
| 350 |
|
| 351 |
$(function(){ |
| 352 |
|
| 353 |
try { |
| 354 |
|
| 355 |
var force_reload_admin_object = Object.create(ForceReloadAdmin); |
| 356 |
|
| 357 |
force_reload_admin_object.init(); |
| 358 |
|
| 359 |
} |
| 360 |
|
| 361 |
catch(e){ |
| 362 |
|
| 363 |
console.error("Error creating object (" + ForceReloadAdmin.class_name + ")! See error below for details."); |
| 364 |
|
| 365 |
console.error(e); |
| 366 |
|
| 367 |
} |
| 368 |
|
| 369 |
}); |
| 370 |
|
| 371 |
|
| 372 |
}(jQuery, window, document)); |