class DocumentPage { static InitForm() { const $form = $("#document-form"); $form.on("submit", event => { event.preventDefault(); DocumentPage.SaveForm(); }) } static InitItemsSection() { const $table = $("#document-items-section table"); const $checkboxes = $table.find("input[type=checkbox]"); const $allCheckbox = $("#all-items-checkbox"); const $batchActions = $(".batch-action"); $allCheckbox.on("change", () => { const checked = $allCheckbox.prop("checked"); $checkboxes.prop("checked", checked); }); $checkboxes.on("change", () => { let batchActionsEnabled = $checkboxes.filter(":checked").length > 0; $batchActions.prop("disabled", !batchActionsEnabled); }); $table.find(".btn-delete-document-item").on("click", event => { const documentItemId = $(event.currentTarget).data("document-item-id"); const confirmed = confirm("Are you sure you want to delete this item?"); if(confirmed) { $.post(window.location + "/delete-item", { document_item_id: documentItemId }).then(response => { toastr.success("Item deleted successfully."); $("#document-items-section").html(response.html); DocumentPage.InitItemsSection(); }); } }); } static InitPdfActions() { const $elements = $(".pdf-action"); $elements.on("click", event => { event.preventDefault(); const $element = $(event.currentTarget); DocumentPage.SaveForm().then(() => { window.open($element.attr("href")); }); }); } static SaveForm() { const $form = $("#document-form"); const action = $form.get(0).action; const data = $form.serialize(); const $submitElements = $form.find("[type=submit]"); $submitElements.prop("disabled", true); return $.post(action, data).always(() => { $submitElements.prop("disabled", false); }).then(() => { toastr.success("Invoice saved successfully."); }); } } $(() => { DocumentPage.InitForm(); DocumentPage.InitItemsSection(); DocumentPage.InitPdfActions(); });