| 1 |
console.log('🟢 Code Engine is running with the options: ', code_engine ); |
| 2 |
|
| 3 |
// #region Constants |
| 4 |
|
| 5 |
const rest_url = code_engine.rest_url; |
| 6 |
|
| 7 |
// #endregion Constants |
| 8 |
|
| 9 |
// #region Dialog |
| 10 |
|
| 11 |
(function ($) { |
| 12 |
$(window).load(function() { |
| 13 |
$('#action-result-dialog').dialog({ |
| 14 |
title: 'Action Result', |
| 15 |
dialogClass: 'wp-dialog', |
| 16 |
autoOpen: false, |
| 17 |
draggable: false, |
| 18 |
width: 'auto', |
| 19 |
modal: true, |
| 20 |
resizable: false, |
| 21 |
closeOnEscape: true, |
| 22 |
position: { |
| 23 |
my: "center", |
| 24 |
at: "center", |
| 25 |
of: window |
| 26 |
}, |
| 27 |
open: function () { |
| 28 |
$('.ui-widget-overlay').bind('click', function(){ |
| 29 |
$('#action-result-dialog').dialog('close'); |
| 30 |
}) |
| 31 |
}, |
| 32 |
create: function () { |
| 33 |
$('.ui-dialog-titlebar-close').addClass('ui-button'); |
| 34 |
}, |
| 35 |
}); |
| 36 |
}); |
| 37 |
})(jQuery); |
| 38 |
|
| 39 |
// #endregion Dialog |
| 40 |
|
| 41 |
// #region Main |
| 42 |
|
| 43 |
document.addEventListener('DOMContentLoaded', function() { |
| 44 |
const registerButtons = document.querySelectorAll('.cdegn-register'); |
| 45 |
const unregisterButtons = document.querySelectorAll('.cdegn-unregister'); |
| 46 |
const refreshButtons = document.querySelectorAll('.cdegn-refresh'); |
| 47 |
const checkQualityButtons = document.querySelectorAll('.cdegn-check-quality'); |
| 48 |
|
| 49 |
const rest_nonce = document.querySelector('#_wpnonce_rest').value; |
| 50 |
|
| 51 |
// Register events to buttons |
| 52 |
registerButtons.forEach(function(element) { |
| 53 |
element.addEventListener('click', async function(e) { |
| 54 |
const snippet_id = e.target.dataset.id; |
| 55 |
const snippet_src = e.target.dataset.src; |
| 56 |
if (!snippet_id) { |
| 57 |
console.error('No snippet ID found'); |
| 58 |
return; |
| 59 |
} |
| 60 |
if (!snippet_src) { |
| 61 |
console.error('No snippet src found'); |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
const response = await postFetch('/register', { snippet_id, snippet_src }); |
| 66 |
showResult(response.message); |
| 67 |
|
| 68 |
e.target.style.display = 'none'; |
| 69 |
e.target.parentNode.querySelector('.cdegn-unregister').style.display = 'block'; |
| 70 |
e.target.parentNode.querySelector('.cdegn-refresh').style.display = 'block'; |
| 71 |
e.target.parentNode.querySelector('.cdegn-check-quality').style.display = 'block'; |
| 72 |
e.target.closest('tr').querySelector('.function-overview').textContent = response.overview; |
| 73 |
}); |
| 74 |
}); |
| 75 |
|
| 76 |
unregisterButtons.forEach(function(element) { |
| 77 |
element.addEventListener('click', async function(e) { |
| 78 |
const snippet_id = e.target.dataset.id; |
| 79 |
const snippet_src = e.target.dataset.src; |
| 80 |
if (!snippet_id) { |
| 81 |
console.error('No snippet ID found'); |
| 82 |
return; |
| 83 |
} |
| 84 |
if (!snippet_src) { |
| 85 |
console.error('No snippet src found'); |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
const response = await postFetch('/unregister', { snippet_id, snippet_src }); |
| 90 |
showResult(response.message); |
| 91 |
|
| 92 |
e.target.style.display = 'none'; |
| 93 |
e.target.parentNode.querySelector('.cdegn-register').style.display = 'block'; |
| 94 |
e.target.parentNode.querySelector('.cdegn-refresh').style.display = 'none'; |
| 95 |
e.target.parentNode.querySelector('.cdegn-check-quality').style.display = 'none'; |
| 96 |
e.target.closest('tr').querySelector('.function-overview').textContent = ''; |
| 97 |
}); |
| 98 |
}); |
| 99 |
|
| 100 |
refreshButtons.forEach(function(element) { |
| 101 |
element.addEventListener('click', async function(e) { |
| 102 |
const snippet_id = e.target.dataset.id; |
| 103 |
const snippet_src = e.target.dataset.src; |
| 104 |
if (!snippet_id) { |
| 105 |
console.error('No snippet ID found'); |
| 106 |
return; |
| 107 |
} |
| 108 |
if (!snippet_src) { |
| 109 |
console.error('No snippet src found'); |
| 110 |
return; |
| 111 |
} |
| 112 |
|
| 113 |
const response = await postFetch('/refresh', { snippet_id, snippet_src }); |
| 114 |
showResult(response.message); |
| 115 |
|
| 116 |
e.target.closest('tr').querySelector('.function-overview').textContent = response.overview; |
| 117 |
}); |
| 118 |
}); |
| 119 |
|
| 120 |
checkQualityButtons.forEach(function(element) { |
| 121 |
element.addEventListener('click', async function(e) { |
| 122 |
const snippet_id = e.target.dataset.id; |
| 123 |
const snippet_src = e.target.dataset.src; |
| 124 |
if (!snippet_id) { |
| 125 |
console.error('No snippet ID found'); |
| 126 |
return; |
| 127 |
} |
| 128 |
if (!snippet_src) { |
| 129 |
console.error('No snippet src found'); |
| 130 |
return; |
| 131 |
} |
| 132 |
|
| 133 |
showSpinner(snippet_id, snippet_src); |
| 134 |
|
| 135 |
const response = await postFetch('/check-quality', { snippet_id, snippet_src }); |
| 136 |
|
| 137 |
hideSpinner(snippet_id, snippet_src); |
| 138 |
showResult(response.detail); |
| 139 |
}); |
| 140 |
}); |
| 141 |
|
| 142 |
document.querySelector('#btn-display-functions-json').addEventListener('click', async function() { |
| 143 |
const response = await getFetch('/functions'); |
| 144 |
showResult("<?php esc_html_e( 'Success! Check the Developer Tools Console for the JSON content.', 'code-engine' ); ?>"); |
| 145 |
console.log(response.functions); |
| 146 |
}); |
| 147 |
|
| 148 |
/** |
| 149 |
* Show the result of an action. |
| 150 |
* @param {string} message |
| 151 |
* @returns {void} |
| 152 |
*/ |
| 153 |
const showResult = (message) => { |
| 154 |
(function ($) { |
| 155 |
if ($('#action-result-dialog').length > 0) { |
| 156 |
$('#action-result-dialog').find('p').html(message); |
| 157 |
$('#action-result-dialog').dialog('open'); |
| 158 |
} else { |
| 159 |
alert(message); |
| 160 |
} |
| 161 |
})(jQuery); |
| 162 |
}; |
| 163 |
|
| 164 |
/** |
| 165 |
* Show the spinner. |
| 166 |
* @param {string} snippet_id |
| 167 |
* @param {string} snippet_src |
| 168 |
* @returns {void} |
| 169 |
*/ |
| 170 |
const showSpinner = (snippet_id, snippet_src) => { |
| 171 |
const spinner = document.querySelector('.spinner[data-id="' + snippet_id + '"][data-src="' + snippet_src + '"]'); |
| 172 |
if (spinner) { |
| 173 |
spinner.classList.add('is-active'); |
| 174 |
document.querySelector('.wrap').querySelectorAll('input').forEach(function(element) { |
| 175 |
element.disabled = true; |
| 176 |
}); |
| 177 |
} |
| 178 |
}; |
| 179 |
|
| 180 |
/** |
| 181 |
* Hide the spinner. |
| 182 |
* @param {string} snippet_id |
| 183 |
* @param {string} snippet_src |
| 184 |
* @returns {void} |
| 185 |
*/ |
| 186 |
const hideSpinner = (snippet_id, snippet_src) => { |
| 187 |
const spinner = document.querySelector('.spinner[data-id="' + snippet_id + '"][data-src="' + snippet_src + '"]'); |
| 188 |
if (spinner) { |
| 189 |
spinner.classList.remove('is-active'); |
| 190 |
document.querySelector('.wrap').querySelectorAll('input').forEach(function(element) { |
| 191 |
element.disabled = false; |
| 192 |
}); |
| 193 |
} |
| 194 |
}; |
| 195 |
|
| 196 |
/** |
| 197 |
* Fetch data using POST method. |
| 198 |
* @param {string} path |
| 199 |
* @param {object} data |
| 200 |
* @returns {Promise<object>} |
| 201 |
*/ |
| 202 |
const postFetch = async function(path, data) { |
| 203 |
const response = await fetch(rest_url + path, { |
| 204 |
method: 'POST', |
| 205 |
headers: { |
| 206 |
'X-WP-Nonce': rest_nonce, |
| 207 |
'Content-Type': 'application/json', |
| 208 |
}, |
| 209 |
body: JSON.stringify(data) |
| 210 |
}); |
| 211 |
const body = await response.json(); |
| 212 |
if (!response.ok || body?.success === false ) { |
| 213 |
showResult(body.message); |
| 214 |
return; |
| 215 |
} |
| 216 |
return body; |
| 217 |
}; |
| 218 |
|
| 219 |
/** |
| 220 |
* Fetch data using GET method. |
| 221 |
* @param {string} path |
| 222 |
* @returns {Promise<object>} |
| 223 |
*/ |
| 224 |
const getFetch = async function(path) { |
| 225 |
const response = await fetch(rest_url + path, { |
| 226 |
method: 'GET', |
| 227 |
headers: { |
| 228 |
'X-WP-Nonce': rest_nonce, |
| 229 |
'Content-Type': 'application/json', |
| 230 |
}, |
| 231 |
}); |
| 232 |
const body = await response.json(); |
| 233 |
if (!response.ok || body?.success === false ) { |
| 234 |
showResult(body.message); |
| 235 |
return; |
| 236 |
} |
| 237 |
return body; |
| 238 |
}; |
| 239 |
}); |
| 240 |
|
| 241 |
// #endregion Main |