index.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. if ('serviceWorker' in navigator) {
  28. // TODO(trevor) does it matter if this fails?
  29. navigator.serviceWorker.register('/service-worker.js')
  30. }
  31. $(".deletelink").each((idx, elem) => {
  32. $(elem).on("click", (event) => {
  33. confirmDelete(event.target.dataset.url, event.target.dataset.linkId);
  34. });
  35. });
  36. let input = document.querySelector('.tagtest');
  37. if (input) {
  38. let tags = new Tagify(input);
  39. let form = $("form[name=\"edit_link\"]")
  40. form.on('submit', event => {
  41. event.preventDefault();
  42. let url = form.attr("action");
  43. let body = {
  44. "url": $('input[name="url"]').val(),
  45. "name": $('input[name="name"]').val(),
  46. "description": $('input[name="description"]').val(),
  47. "private": $('input[name="private"]').is(":checked"),
  48. "tags": tags.value.map(o => o.value),
  49. };
  50. fetch(url, {
  51. method: 'POST',
  52. headers: {'Content-Type': 'application/json'},
  53. body: JSON.stringify(body),
  54. }).then(response => response.json())
  55. .then(body => window.location.href = body['redirect'] || url)
  56. .catch(err => window.location.href = url);
  57. });
  58. }
  59. let searchText = $("#search_text");
  60. searchText.on('keypress', function (e) {
  61. if (e.which == 13) {
  62. let user = searchText.data('user');
  63. let search = searchText.val();
  64. window.location.href = `/u/${user}/search/${search}`
  65. return false;
  66. }
  67. });
  68. });