index.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import Tagify from '@yaireo/tagify';
  2. import $ from 'jquery';
  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. var req = new XMLHttpRequest();
  15. req.addEventListener("load", function() {
  16. $(`#link_${id}`).remove();
  17. });
  18. req.open("DELETE", url);
  19. req.send();
  20. });
  21. $(document).on('click', `a#cancel_delete_${id}`, function() {
  22. $(`#confirm_${id}`).remove();
  23. });
  24. };
  25. document.addEventListener("DOMContentLoaded", () => {
  26. for (let link of document.getElementsByClassName('deletelink')) {
  27. link.onclick = (event) => {
  28. confirmDelete(event.target.dataset.url, event.target.dataset.linkId);
  29. }
  30. }
  31. let input = document.querySelector('.tagtest');
  32. if (input) {
  33. let tags = new Tagify(input);
  34. let form = $("form[name=\"edit_link\"]")
  35. form.submit(event => {
  36. event.preventDefault();
  37. let url = form.attr("action");
  38. let body = {
  39. "url": $('input[name="url"]').val(),
  40. "name": $('input[name="name"]').val(),
  41. "description": $('input[name="description"]').val(),
  42. "private": $('input[name="private"]').is(":checked"),
  43. "tags": tags.value.map(o => o.value),
  44. };
  45. fetch(url, {
  46. method: 'POST',
  47. headers: {'Content-Type': 'application/json'},
  48. body: JSON.stringify(body),
  49. }).then(response => response.json())
  50. .then(body => window.location.href = body['redirect'] || url)
  51. .catch(err => window.location.href = url);
  52. });
  53. }
  54. let searchText = $("#search_text");
  55. searchText.on('keypress', function (e) {
  56. if (e.which == 13) {
  57. let user = searchText.data('user');
  58. let search = searchText.val();
  59. window.location.href = `/u/${user}/search/${search}`
  60. return false;
  61. }
  62. });
  63. });