all files / pdfviewer/organize-pdf/organize-core/ organize-importaction.js

6.82% Statements 12/176
0% Branches 0/107
6.67% Functions 1/15
6.82% Lines 12/176
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 2471×   1× 1×                                         1× 1×                                                                                                                                                                                                                                   1× 1×                                                                                                           1× 1×                                                                               1× 1×               1×    
define(["require", "exports", "../organize-pdf", "@syncfusion/ej2-base", "../../index", "./organize-toolbar", "./tile-interaction", "./organize-math-utils", "./organize-initialization", "./organize-undoredoutils"], function (require, exports, organize_pdf_1, ej2_base_1, index_1, organize_toolbar_1, tile_interaction_1, organize_math_utils_1, organize_initialization_1, organize_undoredoutils_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    function importDocument(args) {
        if (this.pdfViewer.pageOrganizerSettings.canImport) {
            var proxy_1 = this;
            var upoadedFiles = args.target.files;
            if (args.target.files[0] !== null) {
                var uploadedFile = upoadedFiles[0];
                if (uploadedFile) {
                    this.importedDocumentName = uploadedFile.name;
                    var reader = new FileReader();
                    reader.readAsDataURL(uploadedFile);
                    reader.onload = function (e) {
                        var uploadedFileUrl = e.currentTarget.result;
                        proxy_1.loadImportDoc(uploadedFileUrl, null, false);
                        if (!ej2_base_1.isNullOrUndefined(proxy_1.importDocInputElement)) {
                            proxy_1.importDocInputElement.value = '';
                        }
                    };
                }
            }
        }
    }
    exports.importDocument = importDocument;
    function loadImportDoc(documentData, password, isPasswordCorrect) {
        var _this = this;
        if (this.pdfViewer.pageOrganizerSettings.canImport) {
            var proxy_2 = null;
            proxy_2 = this;
            var isEncrypted_1 = false;
            this.importedDocumentData = documentData;
            var documentId_1 = this.pdfViewerBase.createGUID();
            var isbase64 = documentData.includes('pdf;base64,');
            var base64DocumentData = documentData;
            documentData = this.pdfViewerBase.checkDocumentData(documentData, false);
            var jsonObject_1 = this.pdfViewerBase.constructJsonObject(documentData, password, isbase64);
            if (this.pdfViewer.serverActionSettings) {
                this.pdfViewerBase.loadRequestHandler = new index_1.AjaxHandler(this.pdfViewer);
                this.pdfViewerBase.loadRequestHandler.url = this.pdfViewer.serviceUrl + '/' + this.pdfViewer.serverActionSettings.validatePassword;
                this.pdfViewerBase.loadRequestHandler.responseType = 'json';
                this.pdfViewerBase.loadRequestHandler.mode = true;
                jsonObject_1['action'] = 'ValidatePassword';
                jsonObject_1['elementId'] = this.pdfViewer.element.id;
                jsonObject_1['isFileName'] = 'false';
                if (this.pdfViewerBase.clientSideRendering) {
                    this.pdfViewerBase.getPdfByteArray(base64DocumentData).then(function (pdfbytearray) {
                        var data = _this.pdfViewer.pdfRendererModule.loadImportDocument(pdfbytearray, documentId_1, password, jsonObject_1);
                        if (data) {
                            if (typeof data !== 'object') {
                                try {
                                    data = JSON.parse(data);
                                }
                                catch (error) {
                                    _this.pdfViewerBase.onControlError(500, data, _this.pdfViewer.serverActionSettings.load);
                                    data = null;
                                }
                            }
                            if (data) {
                                while (typeof data !== 'object') {
                                    data = JSON.parse(data);
                                    if (typeof parseInt(data, 10) === 'number' && !isNaN(parseInt(data, 10))) {
                                        data = parseInt(data, 10);
                                        break;
                                    }
                                }
                                if (data.uniqueId === documentId_1 || (typeof parseInt(data, 10) === 'number' && !isNaN(parseInt(data, 10)))) {
                                    if (data === 4) {
                                        _this.pdfViewerBase.isImportDoc = true;
                                        isEncrypted_1 = true;
                                        _this.pdfViewerBase.renderPasswordPopup(documentData, password, _this.pdfViewerBase.isImportDoc);
                                    }
                                    else if (data === 3) {
                                        _this.pdfViewerBase.isImportDoc = true;
                                        _this.pdfViewerBase.renderCorruptPopup(_this.pdfViewerBase.isImportDoc);
                                    }
                                }
                            }
                            if (isPasswordCorrect && data !== 4) {
                                _this.pdfViewerBase.passwordDialogReset();
                                if (_this.pdfViewerBase.passwordPopup) {
                                    _this.pdfViewerBase.passwordPopup.hide();
                                }
                            }
                            if ((!isEncrypted_1 || (isPasswordCorrect && data !== 4)) && (data !== 3)) {
                                _this.importDocuments(password, _this.importedDocumentName, documentData);
                            }
                        }
                    });
                }
                else {
                    this.pdfViewerBase.loadRequestHandler.send(jsonObject_1);
                    this.pdfViewerBase.loadRequestHandler.onSuccess = function (result) {
                        var data = result.data;
                        if (data) {
                            if (typeof data !== 'object') {
                                try {
                                    data = JSON.parse(data);
                                }
                                catch (error) {
                                    proxy_2.pdfViewerBase.onControlError(500, data, proxy_2.pdfViewer.serverActionSettings.load);
                                    data = null;
                                }
                            }
                            if (data) {
                                while (typeof data !== 'object') {
                                    data = JSON.parse(data);
                                    if (typeof parseInt(data, 10) === 'number' && !isNaN(parseInt(data, 10))) {
                                        data = parseInt(data, 10);
                                        break;
                                    }
                                }
                                if (data.uniqueId === documentId_1 || (typeof parseInt(data, 10) === 'number' && !isNaN(parseInt(data, 10)))) {
                                    if (data === 4) {
                                        proxy_2.pdfViewerBase.isImportDoc = true;
                                        isEncrypted_1 = true;
                                        proxy_2.pdfViewerBase.renderPasswordPopup(documentData, password, proxy_2.pdfViewerBase.isImportDoc);
                                    }
                                    else if (data === 3) {
                                        proxy_2.pdfViewerBase.isImportDoc = true;
                                        proxy_2.pdfViewerBase.renderCorruptPopup(proxy_2.pdfViewerBase.isImportDoc);
                                    }
                                }
                            }
                            if (isPasswordCorrect && data !== 4) {
                                proxy_2.pdfViewerBase.passwordDialogReset();
                                if (proxy_2.pdfViewerBase.passwordPopup) {
                                    proxy_2.pdfViewerBase.passwordPopup.hide();
                                }
                            }
                            if ((!isEncrypted_1 || (isPasswordCorrect && data !== 4)) && (data !== 3)) {
                                proxy_2.importDocuments(password, proxy_2.importedDocumentName, documentData);
                            }
                        }
                    };
                }
            }
        }
    }
    exports.loadImportDoc = loadImportDoc;
    function importDocuments(password, documentName, documentData) {
        if (this.pdfViewer.pageOrganizerSettings.canImport) {
            var proxy = this;
            if (this.tileAreaDiv.querySelectorAll('.e-pv-organize-node-selection-ring').length === 1) {
                var _loop_1 = function (i) {
                    var mainTileElement = proxy.tileAreaDiv.childNodes[parseInt(i.toString(), 10)];
                    if (mainTileElement instanceof HTMLElement && mainTileElement.classList.contains('e-pv-organize-node-selection-ring')) {
                        var pageId = mainTileElement.id.split('anchor_page_')[mainTileElement.id.split('anchor_page_').length - 1];
                        var pageOrder_1 = parseInt(mainTileElement.getAttribute('data-page-order'), 10);
                        var pageIdlist = pageId.split('_');
                        var subIndex = 0;
                        var pageIndex = parseInt(pageIdlist[parseInt((pageIdlist.length - 1).toString(), 10)], 10);
                        if (pageIdlist.length > 1) {
                            pageIndex = parseInt(pageIdlist[parseInt((pageIdlist.length - 2).toString(), 10)], 10);
                        }
                        subIndex = organize_math_utils_1.getNextSubIndex.call(this_1, mainTileElement.parentElement, pageIndex);
                        importPage.call(this_1, pageOrder_1, mainTileElement, password, documentName, false, documentData);
                        this_1.tileImageRender(pageIndex, subIndex, pageOrder_1 + 1, mainTileElement, true, false, false, true, documentName);
                        var clonedCollections = [];
                        clonedCollections.push(organize_undoredoutils_1.clonedCollection.call(this_1, this_1.tempOrganizePagesCollection.
                            find(function (item) { return item.currentPageIndex === (pageOrder_1 + 1); })));
                        this_1.addOrganizeAction(clonedCollections, 'Import Pages', [], [], null, false);
                    }
                };
                var this_1 = this;
                for (var i = 0; i < proxy.tileAreaDiv.childElementCount; i++) {
                    _loop_1(i);
                }
            }
            else {
                var mainTileElement = proxy.tileAreaDiv.childNodes[0];
                var pageId = mainTileElement.id.split('anchor_page_')[mainTileElement.id.split('anchor_page_').length - 1];
                var pageOrder_2 = parseInt(mainTileElement.getAttribute('data-page-order'), 10);
                var pageIdlist = pageId.split('_');
                var subIndex = 0;
                var pageIndex = parseInt(pageIdlist[parseInt((pageIdlist.length - 1).toString(), 10)], 10);
                if (pageIdlist.length > 1) {
                    pageIndex = parseInt(pageIdlist[parseInt((pageIdlist.length - 2).toString(), 10)], 10);
                }
                subIndex = organize_math_utils_1.getNextSubIndex.call(this, mainTileElement.parentElement, pageIndex);
                importPage.call(this, pageOrder_2, mainTileElement, password, documentName, true, documentData);
                this.tileImageRender(pageIndex, subIndex, pageOrder_2, mainTileElement, true, true, false, true, documentName);
                var clonedCollections = [];
                clonedCollections.push(organize_undoredoutils_1.clonedCollection.call(this, this.tempOrganizePagesCollection.
                    find(function (item) { return item.currentPageIndex === pageOrder_2; })));
                this.addOrganizeAction(clonedCollections, 'Import Pages', [], [], null, false);
            }
            organize_initialization_1.updatePageNumber.call(this);
            organize_initialization_1.updateTotalPageCount.call(this);
            organize_toolbar_1.enableDisableToolbarItems.call(this);
            tile_interaction_1.disableTileCopyRotateButton.call(this);
            tile_interaction_1.disableTileDeleteButton.call(this);
        }
    }
    exports.importDocuments = importDocuments;
    function importPage(currentPageIndex, tileDiv, password, documentName, isBefore, documentData) {
        if (this.pdfViewer.pageOrganizerSettings.canImport) {
            var index_2 = this.tempOrganizePagesCollection.findIndex(function (item) {
                return item.currentPageIndex === currentPageIndex;
            });
            var pageIndex = void 0;
            var pageSize = void 0;
            if (index_2 !== -1) {
                pageIndex = this.tempOrganizePagesCollection[parseInt(index_2.toString(), 10)].pageIndex;
                pageSize = JSON.parse(JSON.stringify(this.tempOrganizePagesCollection[parseInt(index_2.toString(), 10)].pageSize));
                if (isBefore) {
                    this.tempOrganizePagesCollection = this.tempOrganizePagesCollection.slice(0, index_2).concat([new organize_pdf_1.OrganizeDetails(currentPageIndex, -1, pageIndex, false, false, false, false, false, false, 0, pageSize, true, documentName, password, documentData)], this.tempOrganizePagesCollection.slice(index_2));
                    this.tempOrganizePagesCollection = this.tempOrganizePagesCollection.map(function (item, mapIndex) {
                        if ((mapIndex !== index_2 && item.currentPageIndex >= currentPageIndex) && item.currentPageIndex != null) {
                            item.currentPageIndex = item.currentPageIndex + 1;
                        }
                        return item;
                    });
                    tileDiv.setAttribute('data-page-order', (currentPageIndex + 1).toString());
                }
                else {
                    this.tempOrganizePagesCollection = this.tempOrganizePagesCollection.slice(0, index_2 + 1).
                        concat([new organize_pdf_1.OrganizeDetails(currentPageIndex + 1, -1, pageIndex, false, false, false, false, false, false, 0, pageSize, true, documentName, password, documentData)], this.tempOrganizePagesCollection.slice(index_2 + 1));
                    this.tempOrganizePagesCollection = this.tempOrganizePagesCollection.map(function (item, mapIndex) {
                        if (mapIndex > index_2 + 1 && item.currentPageIndex != null) {
                            item.currentPageIndex = item.currentPageIndex + 1;
                        }
                        return item;
                    });
                }
                while (!ej2_base_1.isNullOrUndefined(tileDiv.nextElementSibling)) {
                    var nextTileDiv = tileDiv.nextElementSibling;
                    var nextTileIndex = parseInt(nextTileDiv.getAttribute('data-page-order'), 10);
                    nextTileIndex = nextTileIndex + 1;
                    nextTileDiv.setAttribute('data-page-order', nextTileIndex.toString());
                    tileDiv = nextTileDiv;
                }
            }
        }
    }
    exports.importPage = importPage;
    function bindImportDocEvent() {
        if (this.pdfViewer.pageOrganizerSettings.canImport) {
            var importDocElement = document.getElementById(this.pdfViewer.element.id + '_import_pages');
            if (importDocElement) {
                this.importDocInputElement.click();
            }
        }
    }
    exports.bindImportDocEvent = bindImportDocEvent;
});