From 722e61673af494824b585b32bc619200b28729e4 Mon Sep 17 00:00:00 2001 From: Steffen Persch Date: Fri, 15 Dec 2023 15:18:10 +0100 Subject: [PATCH] [SF PROFILER] Use fetch instead of Sfjs.request and minor style adjusments to better match the new profiler layout (#500) * use fetch instead of Sfjs.request * adjust profiler styling to match new profiler theme --- Resources/public/js/symfonyProfiler.js | 148 ++++++++---------- .../views/SymfonyProfiler/edit.html.twig | 2 +- .../SymfonyProfiler/translation.html.twig | 2 +- 3 files changed, 67 insertions(+), 85 deletions(-) diff --git a/Resources/public/js/symfonyProfiler.js b/Resources/public/js/symfonyProfiler.js index 1558c039..6cdd57b1 100644 --- a/Resources/public/js/symfonyProfiler.js +++ b/Resources/public/js/symfonyProfiler.js @@ -19,83 +19,76 @@ function syncMessage(key) { var el = document.getElementById(key).getElementsByClassName("translation"); el[0].innerHTML = getLoaderHTML(); - Sfjs.request( - translationSyncUrl, - function(xhr) { - // Success - el[0].innerHTML = xhr.responseText; - - if (xhr.responseText !== "") { - clearState(key); - } - }, - function(xhr) { - // Error - el[0].innerHTML = "Error - Syncing message " + key + ""; - }, - serializeQueryString({message_id: key}), - { method: 'POST' } - ); + fetch(translationSyncUrl, { + method: 'POST', + body: serializeQueryString({message_id: key}), + headers: { + 'X-Requested-With': 'XMLHttpRequest', + 'Content-Type': 'application/x-www-form-urlencoded', + } + }).then(res => res.text()).then((text) => { + el[0].innerHTML = text; + + if (text !== "") { + clearState(key); + } + }).catch(() => { + el[0].innerHTML = "Error - Syncing message " + key + ""; + }); } function syncAll() { var el = document.getElementById("top-result-area"); el.innerHTML = getLoaderHTML(); - Sfjs.request( - translationSyncAllUrl, - function(xhr) { - // Success - el.innerHTML = xhr.responseText; - }, - function(xhr) { - // Error - el[0].innerHTML = "Error - Syncing all messages"; + fetch(translationSyncAllUrl, { + method: 'POST', + headers: { + 'X-Requested-With': 'XMLHttpRequest', + 'Content-Type': 'application/x-www-form-urlencoded', }, - {}, - { method: 'POST' } - ); + }).then(res => res.text()).then(text => { + el.innerHTML = text; + }).catch(() => { + el[0].innerHTML = "Error - Syncing all messages"; + }); } function getEditForm(key) { var el = document.getElementById(key).getElementsByClassName("translation"); el[0].innerHTML = getLoaderHTML(); - Sfjs.request( - translationEditUrl + "?" + serializeQueryString({message_id: key}), - function(xhr) { - // Success - el[0].innerHTML = xhr.responseText; + fetch(translationEditUrl + "?" + serializeQueryString({message_id: key}), { + headers: { + 'X-Requested-With': 'XMLHttpRequest', }, - function(xhr) { - // Error - el[0].innerHTML = "Error - Getting edit form " + key + ""; - }, - { method: 'GET' } - ); + }).then(res => res.text()).then(text => { + el[0].innerHTML = text; + }).catch(() => { + el[0].innerHTML = "Error - Getting edit form " + key + ""; + }); } function saveEditForm(key, translation) { var el = document.getElementById(key).getElementsByClassName("translation"); el[0].innerHTML = getLoaderHTML(); - Sfjs.request( - translationEditUrl, - function(xhr) { - // Success - el[0].innerHTML = xhr.responseText; - - if (xhr.responseText !== "") { - clearState(key); - } - }, - function(xhr) { - // Error - el[0].innerHTML = "Error - Saving edit form " + key + ""; + fetch(translationEditUrl, { + method: 'POST', + body: serializeQueryString({message_id: key, translation:translation}), + headers: { + 'X-Requested-With': 'XMLHttpRequest', + 'Content-Type': 'application/x-www-form-urlencoded', }, - serializeQueryString({message_id: key, translation:translation}), - { method: 'POST' } - ); + }).then(res => res.text()).then(text => { + el[0].innerHTML = text; + + if (text !== "") { + clearState(key); + } + }).catch(() => { + el[0].innerHTML = "Error - Saving edit form " + key + ""; + }) return false; } @@ -130,17 +123,6 @@ var serializeQueryString = function(obj, prefix) { return str.join("&"); }; -// We need to hack a bit Sfjs.request because it does not support POST requests -// May not work for ActiveXObject('Microsoft.XMLHTTP'); :( -(function(open) { - XMLHttpRequest.prototype.open = function(method, url, async, user, pass) { - open.call(this, method, url, async, user, pass); - if (method.toLowerCase() === 'post') { - this.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); - } - }; -})(XMLHttpRequest.prototype.open); - var saveTranslations = function(form) { "use strict"; @@ -169,22 +151,22 @@ var saveTranslations = function(form) { el.classList.remove('status-error'); el.classList.remove('status-success'); - Sfjs.request( - form.action, - function(xhr) { - // Success - el.classList.add('label'); - el.classList.add('status-success'); - el.innerHTML = xhr.responseText; + fetch(form.action, { + method: 'POST', + body: serializeQueryString({selected: selected}), + headers: { + 'X-Requested-With': 'XMLHttpRequest', + 'Content-Type': 'application/x-www-form-urlencoded', }, - function(xhr) { - // Error - el.classList.add('label'); - el.classList.add('status-error'); - el.innerHTML = xhr.responseText; - }, - serializeQueryString({selected: selected}), - { method: 'POST' } - ); + }).then(res => res.text()).then(text => { + el.classList.add('label'); + el.classList.add('status-success'); + el.innerHTML = text; + }).catch(error => { + el.classList.add('label'); + el.classList.add('status-error'); + el.innerHTML = error; + }) + return false; }; diff --git a/Resources/views/SymfonyProfiler/edit.html.twig b/Resources/views/SymfonyProfiler/edit.html.twig index 40fcea5a..2a528467 100644 --- a/Resources/views/SymfonyProfiler/edit.html.twig +++ b/Resources/views/SymfonyProfiler/edit.html.twig @@ -1,3 +1,3 @@ - + diff --git a/Resources/views/SymfonyProfiler/translation.html.twig b/Resources/views/SymfonyProfiler/translation.html.twig index c307bcd9..517e23e7 100644 --- a/Resources/views/SymfonyProfiler/translation.html.twig +++ b/Resources/views/SymfonyProfiler/translation.html.twig @@ -110,7 +110,7 @@ {{ message.translation }} - + {% apply spaceless %} Edit |