All Downloads are FREE. Search and download functionalities are using the official Maven repository.

webapp.pdf-exporter.js.bulk-pdf-exporter.js Maven / Gradle / Ivy

Go to download

This Polarion extension provides possibility to convert Polarion Documents to PDF files. This is an alternative to native Polarion's solution.

The newest version!
const BULK_POPUP_ID = 'bulk-pdf-export-modal-popup';
const BULK_POPUP_HTML = `
    
`;
const DISABLED_BUTTON_CLASS = "polarion-TestsExecutionButton-buttons-defaultCursor";
const BULK_EXPORT_IN_PROGRESS = "IN_PROGRESS";
const BULK_EXPORT_INTERRUPTED = "INTERRUPTED";
const BULK_EXPORT_FINISHED = "FINISHED";

const BulkPdfExporter = {
    exportParams: null,
    itemsCount: 0,
    finishedCount: 0,
    state: null,
    registeredButtonClickListener: null,
    errors: false,

    init: function () {
        const popup = document.createElement('div');
        popup.classList.add("modal");
        popup.classList.add("micromodal-slide");
        popup.id = BULK_POPUP_ID;
        popup.setAttribute("aria-hidden", "true");
        popup.innerHTML = BULK_POPUP_HTML;
        document.body.appendChild(popup);
    },

    openPopup: function (bulkExportWidget, exportParams) {
        this.exportParams = exportParams;
        this.itemsCount = 0;
        this.finishedCount = 0;
        this.errors = false;
        this.updateState(BULK_EXPORT_IN_PROGRESS);
        this.renderBulkExportItems(bulkExportWidget);
        MicroModal.show(BULK_POPUP_ID);
        this.startNextItemExport();
    },

    renderBulkExportItems: function (bulkExportWidget) {
        if (bulkExportWidget) {
            const modalContent = document.querySelector("#bulk-pdf-export-popup .modal__content");
            modalContent.innerHTML = "";
            bulkExportWidget.querySelectorAll('input[type="checkbox"]:not(.export-all):checked').forEach((selectedCheckbox) => {
                BulkPdfExporter.itemsCount += 1;

                const div = document.createElement("div");
                div.className = "export-item paused";
                div.dataset["type"] = selectedCheckbox.dataset["type"];
                div.dataset["space"] = selectedCheckbox.dataset["space"];
                div.dataset["id"] = selectedCheckbox.dataset["id"];

                const iconSpan = document.createElement("span");
                iconSpan.className = "icon";
                const fontAwesomeIcon = document.createElement("i");
                fontAwesomeIcon.className = "fa";
                iconSpan.appendChild(fontAwesomeIcon);
                const inProgressIcon = document.createElement("img");
                inProgressIcon.src = '/polarion/ria/images/progress.gif';
                iconSpan.appendChild(inProgressIcon);
                div.appendChild(iconSpan);

                const titleSpan = document.createElement("span");
                titleSpan.className = "title";

                const typeSpan = document.createElement("span");
                typeSpan.className = "type";
                typeSpan.innerText = this.getItemType(selectedCheckbox);
                titleSpan.appendChild(typeSpan);

                const nameSpan = document.createElement("span");
                nameSpan.className = "name";
                nameSpan.innerText = this.getDocumentType(selectedCheckbox.dataset["type"]) === ExportParams.DocumentType.BASELINE_COLLECTION ? selectedCheckbox.dataset["name"] : this.getSpace(selectedCheckbox) + selectedCheckbox.dataset["id"];
                titleSpan.appendChild(nameSpan);

                div.appendChild(titleSpan);

                modalContent.appendChild(div);
            });
            this.updateState(BULK_EXPORT_IN_PROGRESS);
        }
    },

    getItemType: function (selectedCheckbox) {
        switch (selectedCheckbox.dataset["type"]) {
            case "Module": return "Document: ";
            case "RichPage": return "Report: ";
            case "TestRun": return "Test Run: ";
            case "BaselineCollection": return "Collection: ";
            default: return "";
        }
    },

    getSpace: function (selectedCheckbox) {
        if (!selectedCheckbox.dataset["space"] || selectedCheckbox.dataset["space"] === "_default") {
            return "";
        } else {
            return selectedCheckbox.dataset["space"] + " / ";
        }
    },

    updateBulkExportButton: function (clickedElement) {
        const bulkExportWidget = clickedElement?.closest("div.polarion-PdfExporter-BulkExportWidget");
        const button = bulkExportWidget?.querySelector(".polarion-TestsExecutionButton-buttons");
        if (button) {
            if (button.classList.contains(DISABLED_BUTTON_CLASS) && bulkExportWidget.querySelectorAll('input[type="checkbox"]:checked').length > 0) {
                button.classList.remove(DISABLED_BUTTON_CLASS);
                // We need to remember click listener to remove it from button later
                this.registeredButtonClickListener = () => PdfExporter.openPopupForBulkExport(bulkExportWidget);
                button.addEventListener("click", this.registeredButtonClickListener);
            }
            if (!button.classList.contains(DISABLED_BUTTON_CLASS) && bulkExportWidget.querySelectorAll('input[type="checkbox"]:checked').length === 0) {
                button.classList.add(DISABLED_BUTTON_CLASS);
                button.removeEventListener("click", this.registeredButtonClickListener);
            }
            if (bulkExportWidget.querySelectorAll('input[type="checkbox"]:not(.export-all):not(:checked)').length > 0) {
                const exportAllCheckbox = bulkExportWidget.querySelector('input[type="checkbox"].export-all');
                if (exportAllCheckbox) {
                    exportAllCheckbox.checked = false;
                }
            }
        }
    },

    selectAllItems: function (clickedElement) {
        const bulkExportWidget = clickedElement?.closest("div.polarion-PdfExporter-BulkExportWidget");
        if (bulkExportWidget) {
            bulkExportWidget.querySelectorAll('input[type="checkbox"]:not(.export-all)').forEach(checkbox => {
                checkbox.checked = clickedElement.checked;
            });
        }
    },

    stopBulkExport: function () {
        this.updateState(BULK_EXPORT_INTERRUPTED);
    },

    updateState: function (state) {
        this.state = state;

        const popup = document.getElementById("bulk-pdf-export-popup");
        const resultSpan = popup.querySelector(".modal__footer .result");
        const progressBar = popup.querySelector(".modal__footer .progress-bar");
        if (this.state === BULK_EXPORT_IN_PROGRESS) {
            popup.querySelector(".polarion-JSWizardButton-Primary").style.display = "block";
            popup.querySelector(".polarion-JSWizardButton").style.display = "none";
            resultSpan.style.display = "none";
            resultSpan.classList.remove("interrupted");
            resultSpan.classList.remove("finished");
            this.updateProgress(progressBar);
        } else {
            popup.querySelector(".polarion-JSWizardButton-Primary").style.display = "none";
            popup.querySelector(".polarion-JSWizardButton").style.display = "block";
            progressBar.style.display = "none";
            resultSpan.style.display = "block";
            if (this.state === BULK_EXPORT_INTERRUPTED) {
                document.querySelectorAll("#bulk-pdf-export-popup .export-item.paused").forEach(item => {
                    item.classList.remove("paused");
                    item.classList.add("interrupted");
                });

                resultSpan.classList.add("interrupted");
                resultSpan.innerText = "Export interrupted by user";
            } else if (this.state === BULK_EXPORT_FINISHED) {
                resultSpan.classList.add("finished");
                if (this.errors) {
                    resultSpan.classList.add("with-errors");
                } else {
                    resultSpan.classList.remove("with-errors");
                }
                resultSpan.innerText = this.errors ? "Export finished with errors" : "Export successfully finished";
            }
        }
    },

    updateProgress: function (progressBar) {
        progressBar.style.display = this.itemsCount > 1 ? "block" : "none";
        const progressBarSpan = progressBar.querySelector("span");
        const progress = Math.round(BulkPdfExporter.finishedCount / BulkPdfExporter.itemsCount * 100);
        if (progress > 25) {
            progressBarSpan.innerText = `${this.finishedCount} out of ${this.itemsCount} finished`;
        } else {
            progressBarSpan.innerText = "";
        }
        progressBarSpan.style.width = progress + "%";
    },

    startNextItemExport: function () {
        const pausedItems = document.querySelectorAll("#bulk-pdf-export-popup .export-item.paused");
        if (this.exportParams && pausedItems && pausedItems.length > 0) {
            const currentItem = pausedItems[0];
            currentItem.classList.remove("paused");
            currentItem.classList.add("in-progress");

            const documentType = this.getDocumentType(currentItem.dataset["type"]);
            this.exportParams["documentType"] = documentType;
            const documentId = currentItem.dataset["id"];
            if (documentType === ExportParams.DocumentType.TEST_RUN) {
                this.exportParams["urlQueryParameters"] = {id: documentId};
                ExportCommon.downloadTestRunAttachments(this.exportParams.projectId, documentId, this.exportParams.revision, this.exportParams.attachmentsFilter);
            } else if (documentType === ExportParams.DocumentType.BASELINE_COLLECTION) {
                ExportCommon.convertCollectionDocuments(this.exportParams, documentId, () => {
                        currentItem.classList.remove("in-progress");
                        currentItem.classList.add("finished");
                        BulkPdfExporter.finishedCount += 1;
                        if (this.state !== BULK_EXPORT_INTERRUPTED) {
                            BulkPdfExporter.updateState(BULK_EXPORT_IN_PROGRESS);
                        }
                        this.startNextItemExport();
                    },
                    (error) => {
                        this.errors = true;
                        currentItem.classList.remove("in-progress");
                        currentItem.classList.add("error");
                        error.text().then(errorJson => {
                            const error = errorJson && JSON.parse(errorJson);
                            const errorMessage = error && (error.message ? error.message : error.errorMessage);
                            const errorDiv = document.createElement("div");
                            errorDiv.className = "error-message";
                            errorDiv.innerText = errorMessage;
                            currentItem.appendChild(errorDiv);
                        });
                        this.startNextItemExport();
                    });
            } else {
                this.exportParams["locationPath"] = `${currentItem.dataset["space"]}/${documentId}`;
            }

            if (documentType !== ExportParams.DocumentType.BASELINE_COLLECTION) {
                ExportCommon.asyncConvertPdf(this.exportParams.toJSON(), (responseBody, fileName) => {
                    currentItem.classList.remove("in-progress");
                    currentItem.classList.add("finished");

                    BulkPdfExporter.finishedCount += 1;
                    BulkPdfExporter.updateState(BULK_EXPORT_IN_PROGRESS);
                    const downloadFileName = fileName || `${currentItem.dataset["space"] ? currentItem.dataset["space"] + "_" : ""}${documentId}.pdf`; // Fallback if file name wasn't received in response
                    ExportCommon.downloadBlob(responseBody, downloadFileName);
                    this.startNextItemExport();
                }, errorResponse => {
                    this.errors = true;
                    currentItem.classList.remove("in-progress");
                    currentItem.classList.add("error");

                    errorResponse.text().then(errorJson => {
                        const error = errorJson && JSON.parse(errorJson);
                        const errorMessage = error && (error.message ? error.message : error.errorMessage);
                        const errorDiv = document.createElement("div");
                        errorDiv.className = "error-message";
                        errorDiv.innerText = errorMessage;
                        currentItem.appendChild(errorDiv);
                    });
                    this.startNextItemExport();
                });
            }
        } else if (this.state !== BULK_EXPORT_INTERRUPTED) {
            this.updateState(BULK_EXPORT_FINISHED);
        }
    },

    getDocumentType: function (itemType) {
        switch (itemType) {
            case "Module": return ExportParams.DocumentType.LIVE_DOC;
            case "RichPage": return ExportParams.DocumentType.LIVE_REPORT;
            case "TestRun": return ExportParams.DocumentType.TEST_RUN;
            case "BaselineCollection": return ExportParams.DocumentType.BASELINE_COLLECTION;
            default: return "";
        }
    },

}

BulkPdfExporter.init();




© 2015 - 2025 Weber Informatics LLC | Privacy Policy