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