index.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import Tagify from '@yaireo/tagify';
  2. import $ from 'cash-dom';
  3. let confirmDelete = (url, id) => {
  4. if ($(`#confirm_${id}`).length > 0) {
  5. return;
  6. }
  7. let link = $(`#delete_${id}`);
  8. let confirm = link.append(
  9. `<span class="deleteconfirm" id="confirm_${id}">Are you sure?
  10. <a id="do_delete_${id}" class="deletelink yesdelete">yes</a>
  11. <a id="cancel_delete_${id}" class="deletelink">no</a>
  12. </span>`);
  13. $(document).on('click', `a#do_delete_${id}`, function() {
  14. fetch(url, {
  15. method: 'DELETE',
  16. headers: {'Content-Type': 'application/json'},
  17. body: 'null',
  18. }).then(response => response.text())
  19. .then(response => { $(`#link_${id}`).remove()})
  20. .catch(err => console.log(err));
  21. });
  22. $(document).on('click', `a#cancel_delete_${id}`, function() {
  23. $(`#confirm_${id}`).remove();
  24. });
  25. };
  26. document.addEventListener("DOMContentLoaded", () => {
  27. $(".deletelink").each((idx, elem) => {
  28. $(elem).on("click", (event) => {
  29. confirmDelete(event.target.dataset.url, event.target.dataset.linkId);
  30. });
  31. });
  32. // for (let link of document.getElementsByClassName('deletelink')) {
  33. // link.onclick = (event) => {
  34. // confirmDelete(event.target.dataset.url, event.target.dataset.linkId);
  35. // }
  36. // }
  37. let input = document.querySelector('.tagtest');
  38. if (input) {
  39. let tags = new Tagify(input);
  40. let form = $("form[name=\"edit_link\"]")
  41. form.on('submit', event => {
  42. event.preventDefault();
  43. let url = form.attr("action");
  44. let body = {
  45. "url": $('input[name="url"]').val(),
  46. "name": $('input[name="name"]').val(),
  47. "description": $('input[name="description"]').val(),
  48. "private": $('input[name="private"]').is(":checked"),
  49. "tags": tags.value.map(o => o.value),
  50. };
  51. fetch(url, {
  52. method: 'POST',
  53. headers: {'Content-Type': 'application/json'},
  54. body: JSON.stringify(body),
  55. }).then(response => response.json())
  56. .then(body => window.location.href = body['redirect'] || url)
  57. .catch(err => window.location.href = url);
  58. });
  59. }
  60. let searchText = $("#search_text");
  61. searchText.on('keypress', function (e) {
  62. if (e.which == 13) {
  63. let user = searchText.data('user');
  64. let search = searchText.val();
  65. window.location.href = `/u/${user}/search/${search}`
  66. return false;
  67. }
  68. });
  69. });