dashboard.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. const init = require('./init');
  2. // When document is ready launch js script on the DOM
  3. $(document).ready(() => {init.exec(print)});
  4. // Refresh data when update
  5. ipcRenderer.on('refresh-card', (event, arg) => {
  6. init.scope.card = DataService.data;
  7. print(init.scope);
  8. });
  9. /* --- Functions --- */
  10. function print(scope) {
  11. for(element of scope.card) {
  12. element.content = element.content.toHTML();
  13. }
  14. looper(scope);
  15. for (cardEdit of $('.card-edit')) {
  16. const id = $($($(cardEdit).parents()[1]).children()[0]).children()[0].innerHTML;
  17. $(cardEdit).attr('href', RouterService.path('edit', {id: id}));
  18. }
  19. PR.prettyPrint();
  20. }
  21. function saveUserName(input) {
  22. UserService.username = input.value;
  23. }
  24. function selectCard(card) {
  25. const id = card.querySelector('[data-val=id]').innerText;
  26. const data = DataService.getById(id);
  27. console.log(id, data);
  28. $('#modal-title').html(data.title);
  29. $('#modal-content').html(data.content.toHTML())
  30. $('#modal-delete').attr('data-id', id);
  31. $('#modal-edit').attr('href', RouterService.path('edit', {id: id}));
  32. PR.prettyPrint();
  33. init.modal.open();
  34. }
  35. async function deleteCard(span, fromCard) {
  36. let card;
  37. let id;
  38. // Get id and card element to remove
  39. if (fromCard) {
  40. card = $(span).parents();
  41. id = $($(card[1]).children()[0]).children()[0].innerHTML;
  42. } else {
  43. id = $(span).attr('data-id');
  44. idElements = document.querySelectorAll('[data-val=id]');
  45. for (idElement of idElements) {
  46. if (idElement.innerHTML === id) {
  47. card = $(idElement).parents();
  48. }
  49. }
  50. }
  51. // Remove data
  52. const result = await DataService.remove(id);
  53. if (result) {
  54. // Remove card on the DOM
  55. card[2].remove();
  56. M.toast({
  57. html: 'Carte supprimée',
  58. });
  59. }
  60. }
  61. function exportCard(card, event) {
  62. // Stop default event
  63. event.preventDefault();
  64. // Get card
  65. const id = $(card).children()[0].innerText;
  66. // Send info to the main process
  67. ipcRenderer.send('ondragstart', JSON.parse(localStorage[id]));
  68. }