document-page.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. class DocumentPage {
  2. static InitItemsSection() {
  3. const $table = $("#document-items-section table");
  4. const $checkboxes = $table.find("input[type=checkbox]");
  5. const $allCheckbox = $("#all-items-checkbox");
  6. const $batchActions = $(".batch-action");
  7. $allCheckbox.on("change", () => {
  8. const checked = $allCheckbox.prop("checked");
  9. $checkboxes.prop("checked", checked);
  10. });
  11. $checkboxes.on("change", () => {
  12. let batchActionsEnabled = $checkboxes.filter(":checked").length > 0;
  13. $batchActions.prop("disabled", !batchActionsEnabled);
  14. });
  15. $table.find(".btn-delete-document-item").on("click", event => {
  16. const documentItemId = $(event.currentTarget).data("document-item-id");
  17. const confirmed = confirm("Are you sure you want to delete this item?");
  18. if(confirmed) {
  19. $.post(window.location + "/delete-item", {
  20. document_item_id: documentItemId
  21. }).then(response => {
  22. toastr.success("Item deleted successfully.");
  23. $("#document-items-section").html(response.html);
  24. DocumentPage.InitItemsSection();
  25. });
  26. }
  27. });
  28. }
  29. static InitPdfActions() {
  30. const $form = $("#document-form");
  31. const action = $form.get(0).action;
  32. const $elements = $(".pdf-action");
  33. $elements.on("click", event => {
  34. event.preventDefault();
  35. const $element = $(event.currentTarget);
  36. const data = $form.serialize();
  37. $.post(action, data).then(() => {
  38. toastr.success("Invoice saved successfully.");
  39. window.open($element.attr("href"));
  40. });
  41. });
  42. }
  43. }
  44. $(() => {
  45. DocumentPage.InitItemsSection();
  46. DocumentPage.InitPdfActions();
  47. });