refactor: Externalize all template
This commit is contained in:
@ -4,10 +4,12 @@
|
||||
|
||||
{% block content %}
|
||||
<br>
|
||||
<div class="" style="margin-left: 20px; margin-right: 20px; margin-bottom: 20px;">
|
||||
<div class="" style="margin-left: 20px; margin-right: 20px; margin-bottom: 20px;"
|
||||
data-get-file-url="{{ url_for('get_file') }}"
|
||||
data-set-file-url="{{ url_for('set_file') }}">
|
||||
<div class="flex justify mb-4 space-x-2">
|
||||
<button id="restore-button" onclick="restoreJson()" class="px-4 py-2 bg-blue text-black rounded-lg">Restore JSON</button>
|
||||
<button id="save-button" onclick="saveJson()" class="px-4 py-2 bg-green text-black rounded-lg duration-200">Save JSON</button>
|
||||
<button id="restore-button" class="px-4 py-2 bg-blue text-black rounded-lg">Restore JSON</button>
|
||||
<button id="save-button" class="px-4 py-2 bg-green text-black rounded-lg duration-200">Save JSON</button>
|
||||
</div>
|
||||
|
||||
<div id="jsoneditor" class="border rounded-lg shadow-lg" style="height: 750px; width: 100%;"></div>
|
||||
@ -18,94 +20,5 @@
|
||||
<script src="https://cdn.jsdelivr.net/npm/jsoneditor@9.1.0/dist/jsoneditor.min.js"></script>
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jsoneditor@9.1.0/dist/jsoneditor.min.css" />
|
||||
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
|
||||
|
||||
<script>
|
||||
const saveButton = document.getElementById("save-button");
|
||||
const container = document.getElementById("jsoneditor");
|
||||
|
||||
const editor = new JSONEditor(container, {
|
||||
mode: "code",
|
||||
onChange: validateJson
|
||||
});
|
||||
|
||||
function validateJson() {
|
||||
try {
|
||||
editor.get();
|
||||
updateSaveButton(true);
|
||||
hideErrorMessage();
|
||||
} catch (error) {
|
||||
updateSaveButton(false);
|
||||
showErrorMessage("Invalid JSON! Please correct the errors.");
|
||||
}
|
||||
}
|
||||
|
||||
function updateSaveButton(isValid) {
|
||||
saveButton.disabled = !isValid;
|
||||
saveButton.style.cursor = isValid ? "pointer" : "not-allowed";
|
||||
|
||||
saveButton.style.setProperty('background-color', isValid ? "#28a745" : "#ccc", 'important');
|
||||
saveButton.style.setProperty('color', isValid ? "#fff" : "#666", 'important');
|
||||
//saveButton.style.setProperty('border-color', isValid ? "#28a745" : "#ccc", 'important');
|
||||
}
|
||||
|
||||
function showErrorMessage(message) {
|
||||
Swal.fire({
|
||||
title: "Error",
|
||||
text: message,
|
||||
icon: "error",
|
||||
showConfirmButton: false,
|
||||
timer: 5000,
|
||||
position: 'top-right',
|
||||
toast: true,
|
||||
showClass: { popup: 'animate__animated animate__fadeInDown' },
|
||||
hideClass: { popup: 'animate__animated animate__fadeOutUp' }
|
||||
});
|
||||
}
|
||||
|
||||
function hideErrorMessage() {
|
||||
Swal.close();
|
||||
}
|
||||
|
||||
function saveJson() {
|
||||
Swal.fire({
|
||||
title: 'Are you sure?',
|
||||
text: 'Do you want to save the changes?',
|
||||
icon: 'warning',
|
||||
showCancelButton: true,
|
||||
confirmButtonText: 'Yes, save it!',
|
||||
cancelButtonText: 'Cancel',
|
||||
reverseButtons: true
|
||||
}).then((result) => {
|
||||
if (result.isConfirmed) {
|
||||
fetch("{{ url_for('set_file') }}", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(editor.get())
|
||||
})
|
||||
.then(() => {
|
||||
Swal.fire('Saved!', 'Your changes have been saved.', 'success');
|
||||
})
|
||||
.catch(error => {
|
||||
Swal.fire('Error!', 'There was an error saving your data.', 'error');
|
||||
console.error("Error saving JSON:", error);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function restoreJson() {
|
||||
fetch("{{ url_for('get_file') }}")
|
||||
.then(response => response.json())
|
||||
.then(json => {
|
||||
editor.set(json);
|
||||
Swal.fire('Success!', 'Your JSON has been loaded.', 'success');
|
||||
})
|
||||
.catch(error => {
|
||||
Swal.fire('Error!', 'There was an error loading your JSON.', 'error');
|
||||
console.error("Error loading JSON:", error);
|
||||
});
|
||||
}
|
||||
|
||||
restoreJson();
|
||||
</script>
|
||||
{% endblock %}
|
||||
<script src="{{ url_for('assets', path='js/config.js') }}"></script>
|
||||
{% endblock %}
|
||||
Reference in New Issue
Block a user