lc.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. let searchText = $("#search_text");
  51. console.log(`search text: ${searchText}`);
  52. searchText.on('keypress', function (e) {
  53. if (e.which == 13) {
  54. let user = searchText.data('user');
  55. let search = searchText.val();
  56. window.location.href = `/u/${user}/search/${search}`
  57. return false;
  58. }
  59. });
  60. });