| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- 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");
- swal.fire({
- icon: "question",
- title: "Deleting Item",
- text: "Are you sure you want to delete this item?",
- confirmButtonText: "Delete",
- showCancelButton: true
- }).then(result => {
- if(result.isConfirmed) {
- $.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(() => {
- const showErrorAlert = (error) => {
- swal.fire("Error", error, "error");
- };
- const contactName = $("#contact-name").val();
- if(!contactName) {
- showErrorAlert("Contact Name must be set.")
- return;
- }
- const contactEmail = $("#contact-email").val();
- if(!contactEmail) {
- showErrorAlert("Contact Email must be set.")
- return;
- }
- const $p1 = $("<p>").text("Are you sure you want to email the PDF to:");
- const $p2 = $("<div>").text(`${contactName} <${contactEmail}>`);
- const checked = (variant === "commercial") ? 0 : 1;
- swal.fire({
- icon: "question",
- title: "Emailing PDF",
- html: $p1.get(0).outerHTML + $p2.get(0).outerHTML,
- confirmButtonText: "Send Email",
- showCancelButton: true,
- input: "checkbox",
- inputValue: checked,
- inputPlaceholder: "Attach brochures",
- preConfirm: value => {
- return $.post(window.location + "/email-pdf", {
- variant: variant,
- include_brochures: value
- }).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();
- });
|