lc.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. function confirmDelete(url, id) {
  2. if ($(`#confirm_${id}`).length > 0) {
  3. return;
  4. }
  5. let link = $(`#delete_${id}`);
  6. let confirm = link.append(
  7. `<span class="deleteconfirm" id="confirm_${id}">Are you sure?
  8. <a id="do_delete_${id}">yes</a>
  9. <a id="cancel_delete_${id}">no</a>
  10. </span>`);
  11. $(document).on('click', `a#do_delete_${id}`, function() {
  12. var req = new XMLHttpRequest();
  13. req.addEventListener("load", function() {
  14. $(`#link_${id}`).remove();
  15. });
  16. req.open("DELETE", url);
  17. req.send();
  18. });
  19. $(document).on('click', `a#cancel_delete_${id}`, function() {
  20. $(`#confirm_${id}`).remove();
  21. });
  22. }
  23. function removeConfirm(id) {
  24. $(`#confirm_${id}`).remove();
  25. }
  26. $(document).ready(() => {
  27. let input = document.querySelector('.tagtest');
  28. if (input) {
  29. let tags = new Tagify(input);
  30. let form = $("form[name=\"edit_link\"]")
  31. form.submit(event => {
  32. event.preventDefault();
  33. let url = form.attr("action");
  34. let body = {
  35. "url": $('input[name="url"]').val(),
  36. "name": $('input[name="name"]').val(),
  37. "description": $('input[name="description"]').val(),
  38. "private": $('input[name="private"]').is(":checked"),
  39. "tags": tags.value.map(o => o.value),
  40. };
  41. fetch(url, {
  42. method: 'POST',
  43. headers: {'Content-Type': 'application/json'},
  44. body: JSON.stringify(body),
  45. }).then(response => response.json())
  46. .then(body => window.location.href = body['redirect'] || url)
  47. .catch(err => window.location.href = url);
  48. });
  49. }
  50. });