| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- 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"));
- });
- });
- const $emailActions = $(".email-pdf-action");
- $emailActions.on("click", event => {
- const $element = $(event.currentTarget);
- const { variant } = $element.data();
- DocumentPage.SaveForm().then(() => {
- toastr.info("Emailing PDF...");
- $.post(window.location + "/email-pdf", {
- variant: variant
- }).then(() => {
- toastr.success("Email sent successfully.");
- }).catch(error => {
- const { responseJSON } = error;
- if(responseJSON && responseJSON.message) {
- toastr.error(responseJSON.message);
- }
- });
- });
- });
- }
- 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("Sales Order saved successfully.");
- });
- }
- }
- $(() => {
- DocumentPage.InitForm();
- DocumentPage.InitItemsSection();
- DocumentPage.InitPdfActions();
- });
|