Convert comment editing to vanilla JavaScript

Signed-off-by: Eli Schwartz <eschwartz@archlinux.org>
This commit is contained in:
Jelle van der Waa 2021-06-14 22:13:07 +02:00 committed by Eli Schwartz
parent d7603fa4d3
commit 06fa8ab5f3
No known key found for this signature in database
GPG key ID: CEB167EFB5722BD6

View file

@ -169,37 +169,71 @@ if ($comment_section == "package") {
</div> </div>
<script> <script>
$(document).ready(function() { function add_busy_indicator(sibling) {
$('.edit-comment').click(function () { const img = document.createElement('img');
var parent_element = this.parentElement, img.src = "/images/ajax-loader.gif";
parent_id = parent_element.id, img.classList.add('ajax-loader');
comment_id = parent_id.substr(parent_id.indexOf('-') + 1), img.style.height = 11;
edit_form = $(parent_element).next(), img.style.width = 16;
_this = $(this); img.alt = "Busy…";
add_busy_indicator(_this);
$.getJSON('<?= get_uri('/rpc') ?>', { sibling.insertAdjacentElement('afterend', img);
type: 'get-comment-form', }
function remove_busy_indicator(sibling) {
const elem = sibling.nextElementSibling;
elem.parentNode.removeChild(elem);
}
function getParentsUntil(elem, className) {
// Limit to 10 depth
for ( ; elem && elem !== document; elem = elem.parentNode) {
if (elem.matches(className)) {
break;
}
}
return elem;
}
function handleEditCommentClick(event) {
event.preventDefault();
const parent_element = getParentsUntil(event.target, '.comment-header');
const parent_id = parent_element.id;
const comment_id = parent_id.substr(parent_id.indexOf('-') + 1);
// The div class="article-content" which contains the comment
const edit_form = parent_element.nextElementSibling;
const params = new URLSearchParams({
type: "get-comment-form",
arg: comment_id, arg: comment_id,
base_id: <?= intval($row["PackageBaseID"]) ?>, base_id: <?= intval($row["PackageBaseID"]) ?>,
pkgbase_name: <?= json_encode($pkgbase_name) ?> pkgbase_name: <?= json_encode($pkgbase_name) ?>
}, function (data) { });
remove_busy_indicator(_this);
const url = '<?= get_uri('/rpc') ?>' + '?' + params.toString();
add_busy_indicator(event.target);
fetch(url, {
method: 'GET'
})
.then(function(response) { return response.json(); })
.then(function(data) {
remove_busy_indicator(event.target);
if (data.success) { if (data.success) {
edit_form.html(data.form); edit_form.innerHTML = data.form;
edit_form.find('textarea').focus(); edit_form.querySelector('textarea').focus();
} else { } else {
alert(data.error); alert(data.error);
} }
}); });
return false; }
});
function add_busy_indicator(sibling) { document.addEventListener('DOMContentLoaded', function() {
sibling.after('<img src="/images/ajax-loader.gif" class="ajax-loader" width="16" height="11" alt="Busy…" />'); const divs = document.querySelectorAll('.edit-comment');;
} for (let div of divs) {
div.addEventListener('click', handleEditCommentClick);
function remove_busy_indicator(sibling) {
sibling.next().remove();
} }
}); });
</script> </script>