| 1 |
(function () { |
| 2 |
'use strict'; |
| 3 |
|
| 4 |
var activateBtn = document.getElementById('cb-ppg-activate-btn'); |
| 5 |
var installBtn = document.getElementById('cb-ppg-install-btn'); |
| 6 |
|
| 7 |
function setLoading(btn, loadingText) { |
| 8 |
btn.disabled = true; |
| 9 |
btn.innerHTML = ''; |
| 10 |
|
| 11 |
var spinner = document.createElement('span'); |
| 12 |
spinner.className = 'cb-ppg__spinner'; |
| 13 |
btn.appendChild(spinner); |
| 14 |
btn.appendChild(document.createTextNode(loadingText)); |
| 15 |
} |
| 16 |
|
| 17 |
function resetButton(btn, text) { |
| 18 |
btn.disabled = false; |
| 19 |
btn.innerHTML = ''; |
| 20 |
btn.textContent = text; |
| 21 |
} |
| 22 |
|
| 23 |
function ajaxCall(btn, action, loadingText, defaultText, onSuccess) { |
| 24 |
setLoading(btn, loadingText); |
| 25 |
|
| 26 |
var data = new FormData(); |
| 27 |
data.append('action', action); |
| 28 |
data.append('nonce', cookiebot_ppg.nonce); |
| 29 |
|
| 30 |
fetch(cookiebot_ppg.ajax_url, { method: 'POST', body: data, credentials: 'same-origin' }) |
| 31 |
.then(function (response) { |
| 32 |
return response.json(); |
| 33 |
}) |
| 34 |
.then(function (result) { |
| 35 |
if (result.success) { |
| 36 |
onSuccess(); |
| 37 |
return; |
| 38 |
} |
| 39 |
resetButton(btn, defaultText); |
| 40 |
}) |
| 41 |
.catch(function () { |
| 42 |
resetButton(btn, defaultText); |
| 43 |
}); |
| 44 |
} |
| 45 |
|
| 46 |
function bindActivate(btn) { |
| 47 |
btn.addEventListener('click', function () { |
| 48 |
ajaxCall( |
| 49 |
btn, |
| 50 |
'cookiebot_activate_ppg', |
| 51 |
cookiebot_ppg.i18n.activating, |
| 52 |
cookiebot_ppg.i18n.activate, |
| 53 |
function () { |
| 54 |
window.location.href = cookiebot_ppg.redirect_url; |
| 55 |
} |
| 56 |
); |
| 57 |
}); |
| 58 |
} |
| 59 |
|
| 60 |
if (activateBtn) { |
| 61 |
bindActivate(activateBtn); |
| 62 |
} |
| 63 |
|
| 64 |
if (installBtn) { |
| 65 |
installBtn.addEventListener('click', function () { |
| 66 |
ajaxCall( |
| 67 |
installBtn, |
| 68 |
'cookiebot_install_ppg', |
| 69 |
cookiebot_ppg.i18n.installing, |
| 70 |
cookiebot_ppg.i18n.install, |
| 71 |
function () { |
| 72 |
var newBtn = document.createElement('button'); |
| 73 |
newBtn.type = 'button'; |
| 74 |
newBtn.id = 'cb-ppg-activate-btn'; |
| 75 |
newBtn.className = 'cb-btn cb-main-btn'; |
| 76 |
newBtn.textContent = cookiebot_ppg.i18n.activate; |
| 77 |
installBtn.parentNode.replaceChild(newBtn, installBtn); |
| 78 |
bindActivate(newBtn); |
| 79 |
} |
| 80 |
); |
| 81 |
}); |
| 82 |
} |
| 83 |
})(); |
| 84 |
|