toolbar.js
115 lines
| 1 | /* The code is an immediately invoked function expression (IIFE) that creates a loader element and |
| 2 | defines an asynchronous function called `ahscBtnPurger`. */ |
| 3 | (() => { |
| 4 | ("use strict"); |
| 5 | |
| 6 | /* The code is creating a new `div` element and assigning it to the `loader` variable. It then sets the |
| 7 | `id` attribute of the `div` element to "ahsc-loader-toolbar". Finally, it appends the `div` element |
| 8 | to the `body` of the document. This code is creating a loader element that can be used to indicate |
| 9 | that a process is in progress. */ |
| 10 | const loader = document.createElement("div"); |
| 11 | loader.setAttribute("id", "ahsc-loader-toolbar"); |
| 12 | document.body.append(loader); |
| 13 | |
| 14 | /** |
| 15 | * The above function is an asynchronous JavaScript function that purges the cache using an AJAX |
| 16 | * request. |
| 17 | * @returns nothing (undefined). |
| 18 | */ |
| 19 | const ahscBtnPurger = async () => { |
| 20 | if (typeof AHSC_TOOLBAR.ahsc_nonce == "undefined") { |
| 21 | console.warn("No nonce is set for this action. This action has been aborted."); |
| 22 | return; |
| 23 | } |
| 24 | |
| 25 | loader.style.display = "block"; |
| 26 | |
| 27 | let to_purge = "current-url" === AHSC_TOOLBAR.ahsc_topurge ? window.location.pathname : "all"; |
| 28 | |
| 29 | const data = new FormData(); |
| 30 | data.append("action", "ahcs_clear_cache"); |
| 31 | data.append("ahsc_nonce", AHSC_TOOLBAR.ahsc_nonce); |
| 32 | data.append("ahsc_to_purge", encodeURIComponent(to_purge)); |
| 33 | |
| 34 | const request = await fetch(AHSC_TOOLBAR.ahsc_ajax_url, { |
| 35 | method: "POST", |
| 36 | credentials: "same-origin", |
| 37 | body: data, |
| 38 | }) |
| 39 | .then((r) => r.json()) |
| 40 | .then((result) => { |
| 41 | if (result.code >= 200) { |
| 42 | let style = ""; |
| 43 | loader.style.removeProperty("display"); |
| 44 | switch (result.type) { |
| 45 | case "success": |
| 46 | style = "color:green"; |
| 47 | break; |
| 48 | case "error": |
| 49 | style = "color:red"; |
| 50 | break; |
| 51 | default: |
| 52 | style = "color:blue"; |
| 53 | break; |
| 54 | } |
| 55 | console.log(`%c${result.message}`, style); |
| 56 | } |
| 57 | }) |
| 58 | .catch((error) => { |
| 59 | console.log("[Aruba HiSpeed Cache Plugin]"); |
| 60 | console.error(error); |
| 61 | }); |
| 62 | |
| 63 | return; |
| 64 | }; |
| 65 | |
| 66 | /* The line `window.ahscBtnPurger = ahscBtnPurger;` is assigning the `ahscBtnPurger` function to the |
| 67 | `ahscBtnPurger` property of the `window` object. This makes the function accessible globally in the |
| 68 | browser environment. */ |
| 69 | window.ahscBtnPurger = ahscBtnPurger; |
| 70 | |
| 71 | const ahscBtnTransientPurger = async () => { |
| 72 | loader.style.display = "block"; |
| 73 | |
| 74 | |
| 75 | |
| 76 | const data = new FormData(); |
| 77 | data.append("action", "ahsc_clear_expired_transient"); |
| 78 | data.append("ahsc_nonce", AHSC_TOOLBAR.ahsc_nonce); |
| 79 | |
| 80 | const request = await fetch(AHSC_TOOLBAR.ahsc_ajax_url, { |
| 81 | method: "POST", |
| 82 | credentials: "same-origin", |
| 83 | body: data, |
| 84 | }) |
| 85 | .then((r) => r.json()) |
| 86 | .then((result) => { |
| 87 | if (result.code >= 200) { |
| 88 | let style = ""; |
| 89 | loader.style.removeProperty("display"); |
| 90 | switch (result.type) { |
| 91 | case "success": |
| 92 | style = "color:green"; |
| 93 | break; |
| 94 | case "error": |
| 95 | style = "color:red"; |
| 96 | break; |
| 97 | default: |
| 98 | style = "color:blue"; |
| 99 | break; |
| 100 | } |
| 101 | console.log(`%c${result.message}`, style); |
| 102 | } |
| 103 | }) |
| 104 | .catch((error) => { |
| 105 | console.log("[Aruba HiSpeed Cache Plugin]"); |
| 106 | console.error(error); |
| 107 | }); |
| 108 | |
| 109 | |
| 110 | } |
| 111 | |
| 112 | window.ahscBtnTransientPurger = ahscBtnTransientPurger; |
| 113 | |
| 114 | })(); |
| 115 |