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

11.01% Statements 24/218
0% Branches 0/107
5.56% Functions 1/18
11.16% Lines 24/215
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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 3341×   1× 1×                   1× 1×                     1× 1×                 1× 1×                 1× 1×                                                                                                                                                                                                                                                                                                 1× 1×                                                                                                                                                                 1× 1×                     1× 1×                     1× 1×           1× 1×                             1× 1×                   1×    
define(["require", "exports", "./organize-toolbar", "./organizepages-editor", "./tile-interaction", "./organize-initialization", "./organize-undoredoutils", "./organize-extract"], function (require, exports, organize_toolbar_1, organizepages_editor_1, tile_interaction_1, organize_initialization_1, organize_undoredoutils_1, organize_extract_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    function addOrganizeAction(UndoRedoTileActions, actionString, toolbarActions, selectedPageIndexes, dropIndex, isRightInsertion) {
        var action = {
            UndoRedoTileActions: UndoRedoTileActions, action: actionString, toolbarActions: toolbarActions,
            selectedPagesIndexes: selectedPageIndexes, dropIndex: dropIndex,
            isRightInsertion: isRightInsertion
        };
        this.undoOrganizeCollection.push(action);
        this.redoOrganizeCollection = [];
        updateUndoRedoButtons.call(this);
    }
    exports.addOrganizeAction = addOrganizeAction;
    function updateUndoRedoButtons() {
        var _this = this;
        this.toolbar.items.forEach(function (item) {
            if (item.id === _this.pdfViewer.element.id + '_undo_organize_Pages') {
                organize_toolbar_1.enableToolbarItem.call(_this, item.id, (_this.undoOrganizeCollection.length > 0));
            }
            else if (item.id === _this.pdfViewer.element.id + '_redo_organize_Pages') {
                organize_toolbar_1.enableToolbarItem.call(_this, item.id, (_this.redoOrganizeCollection.length > 0));
            }
        });
    }
    exports.updateUndoRedoButtons = updateUndoRedoButtons;
    function restorePagesBeforeZoom() {
        var undoCollectionLength = this.undoOrganizeCollection.length;
        if (undoCollectionLength > 0) {
            for (var index = undoCollectionLength - 1; index >= 0; index--) {
                var undoActionObject = this.undoOrganizeCollection[index];
                undoActionHandler.call(this, undoActionObject);
            }
        }
    }
    exports.restorePagesBeforeZoom = restorePagesBeforeZoom;
    function restorePagesAfterZoom() {
        var undoCollectionLength = this.undoOrganizeCollection.length;
        if (undoCollectionLength > 0) {
            for (var index = 0; index < undoCollectionLength; index++) {
                var redoActionObject = this.undoOrganizeCollection[index];
                redoActionHandler.call(this, redoActionObject);
            }
        }
    }
    exports.restorePagesAfterZoom = restorePagesAfterZoom;
    function undoActionHandler(undoActionObject) {
        if (undoActionObject) {
            var actionObject_1 = JSON.parse(JSON.stringify(undoActionObject));
            switch (actionObject_1.action) {
                case 'Insert Right':
                case 'Insert Left':
                case 'Import Pages':
                case 'Copy':
                    removePage.call(this, actionObject_1.UndoRedoTileActions[0].currentPageIndex);
                    break;
                case 'Rotate Right':
                    rotateImage.call(this, actionObject_1.UndoRedoTileActions[0].currentPageIndex, -90);
                    break;
                case 'Rotate Left':
                    rotateImage.call(this, actionObject_1.UndoRedoTileActions[0].currentPageIndex, 90);
                    break;
                case 'Delete':
                    {
                        var mainTileElement = this.tileAreaDiv.childNodes[parseInt(actionObject_1.UndoRedoTileActions[0].
                            currentPageIndex.toString(), 10)];
                        if (actionObject_1.UndoRedoTileActions[0].isCopied) {
                            organize_undoredoutils_1.insertRemovedPages.call(this, actionObject_1.UndoRedoTileActions[0], actionObject_1.UndoRedoTileActions[0].currentPageIndex, mainTileElement);
                            this.tileImageRender(actionObject_1.UndoRedoTileActions[0].copiedPageIndex, 0, actionObject_1.UndoRedoTileActions[0].currentPageIndex, mainTileElement, true, true, false);
                        }
                        else if (actionObject_1.UndoRedoTileActions[0].isInserted) {
                            organize_undoredoutils_1.insertRemovedPages.call(this, actionObject_1.UndoRedoTileActions[0], actionObject_1.UndoRedoTileActions[0].currentPageIndex, mainTileElement);
                            this.tileImageRender(actionObject_1.UndoRedoTileActions[0].copiedPageIndex, 0, actionObject_1.UndoRedoTileActions[0].currentPageIndex, mainTileElement, true, true, true);
                        }
                        else if (actionObject_1.UndoRedoTileActions[0].isImportedDoc) {
                            organize_undoredoutils_1.insertRemovedPages.call(this, actionObject_1.UndoRedoTileActions[0], actionObject_1.UndoRedoTileActions[0].currentPageIndex, mainTileElement);
                            this.tileImageRender(actionObject_1.UndoRedoTileActions[0].copiedPageIndex, 0, actionObject_1.UndoRedoTileActions[0].currentPageIndex, mainTileElement, true, true, false, true, actionObject_1.UndoRedoTileActions[0].documentName);
                        }
                        else if (!actionObject_1.UndoRedoTileActions[0].isCopied && !actionObject_1.UndoRedoTileActions[0].isInserted
                            && !actionObject_1.UndoRedoTileActions[0].isImportedDoc) {
                            organize_undoredoutils_1.undoDeletedPage.call(this, actionObject_1.UndoRedoTileActions[0].currentPageIndex, actionObject_1.UndoRedoTileActions[0].pageIndex, actionObject_1.UndoRedoTileActions[0].rotateAngle, mainTileElement);
                            this.tileImageRender(actionObject_1.UndoRedoTileActions[0].pageIndex, 0, actionObject_1.UndoRedoTileActions[0].currentPageIndex, mainTileElement, true, true, false);
                        }
                        organize_initialization_1.updatePageDetail.call(this);
                    }
                    break;
                case 'Move Pages':
                    {
                        var dropIndex_1 = actionObject_1.dropIndex;
                        var beforeDropIndex = [];
                        var afterDropIndex = [];
                        var processedIndexes_1 = new Set();
                        var isInRange_1 = function (start, end, value) { return value >= start && value <= end; };
                        var _loop_1 = function (i) {
                            var action = actionObject_1.UndoRedoTileActions[parseInt(i.toString(), 10)];
                            var selectedItem = this_1.tempOrganizePagesCollection
                                .find(function (item) {
                                if (action.isCopied) {
                                    return item.copiedPageIndex === action.copiedPageIndex && isInRange_1(dropIndex_1 - actionObject_1.selectedPagesIndexes.length, dropIndex_1 + actionObject_1.selectedPagesIndexes.length, item.currentPageIndex) && !processedIndexes_1.has(item.currentPageIndex);
                                }
                                else if (action.isInserted) {
                                    return item.copiedPageIndex === action.copiedPageIndex && item.isInserted && isInRange_1(dropIndex_1 - actionObject_1.selectedPagesIndexes.length, dropIndex_1 + actionObject_1.selectedPagesIndexes.length, item.currentPageIndex) && !processedIndexes_1.has(item.currentPageIndex);
                                }
                                else if (action.isImportedDoc) {
                                    return item.copiedPageIndex === action.copiedPageIndex && item.isImportedDoc && isInRange_1(dropIndex_1 - actionObject_1.selectedPagesIndexes.length, dropIndex_1 + actionObject_1.selectedPagesIndexes.length, item.currentPageIndex) && !processedIndexes_1.has(item.currentPageIndex);
                                }
                                else {
                                    return item.pageIndex === action.pageIndex;
                                }
                            });
                            if (selectedItem) {
                                var selectedIndexes = [selectedItem.currentPageIndex];
                                processedIndexes_1.add(selectedItem.currentPageIndex);
                                if (dropIndex_1 < action.currentPageIndex) {
                                    afterDropIndex.push({ currentPageIndex: action.currentPageIndex, selectedIndexes: selectedIndexes });
                                }
                                else {
                                    beforeDropIndex.push({ currentPageIndex: action.currentPageIndex, selectedIndexes: selectedIndexes });
                                }
                            }
                        };
                        var this_1 = this;
                        for (var i = 0; i < actionObject_1.UndoRedoTileActions.length; i++) {
                            _loop_1(i);
                        }
                        if (beforeDropIndex.length > 0) {
                            beforeDropIndex.sort(function (a, b) { return a.currentPageIndex - b.currentPageIndex; });
                            for (var j = 0; j < beforeDropIndex.length; j++) {
                                organizepages_editor_1.rearrangePages.call(this, beforeDropIndex[parseInt(j.toString(), 10)].selectedIndexes, beforeDropIndex[parseInt(j.toString(), 10)].currentPageIndex, beforeDropIndex[parseInt(j.toString(), 10)].currentPageIndex > beforeDropIndex[parseInt(j.toString(), 10)].selectedIndexes[0]);
                            }
                        }
                        if (afterDropIndex.length > 0) {
                            afterDropIndex.sort(function (a, b) { return b.currentPageIndex - a.currentPageIndex; });
                            for (var j = 0; j < afterDropIndex.length; j++) {
                                organizepages_editor_1.rearrangePages.call(this, afterDropIndex[parseInt(j.toString(), 10)].selectedIndexes, afterDropIndex[parseInt(j.toString(), 10)].currentPageIndex, afterDropIndex[parseInt(j.toString(), 10)].currentPageIndex >
                                    afterDropIndex[parseInt(j.toString(), 10)].selectedIndexes[0]);
                            }
                        }
                        break;
                    }
                case 'Toolbar Rotate Right':
                    rotateImages.call(this, actionObject_1, -90);
                    break;
                case 'Toolbar Rotate Left':
                    rotateImages.call(this, actionObject_1, 90);
                    break;
                case 'Toolbar Copy':
                    if (actionObject_1.toolbarActions.length > 0) {
                        for (var i = actionObject_1.toolbarActions.length - 1; i >= 0; i--) {
                            var mainTileElement = this.tileAreaDiv.childNodes[parseInt(actionObject_1.toolbarActions[parseInt(i.toString(), 10)].
                                currentPageIndex.toString(), 10)];
                            organize_undoredoutils_1.deleteTempPage.call(this, actionObject_1.toolbarActions[parseInt(i.toString(), 10)].currentPageIndex, mainTileElement);
                            this.tileAreaDiv.removeChild(mainTileElement);
                            organize_initialization_1.updatePageDetail.call(this);
                        }
                    }
                    tile_interaction_1.disableTileDeleteButton.call(this);
                    break;
                case 'Toolbar Delete':
                    {
                        if (actionObject_1.toolbarActions.length > 0) {
                            for (var i = 0; i < actionObject_1.toolbarActions.length; i++) {
                                var mainTileElement = this.tileAreaDiv.childNodes[parseInt(actionObject_1.toolbarActions[parseInt(i.toString(), 10)].
                                    currentPageIndex.toString(), 10)];
                                if (actionObject_1.toolbarActions[parseInt(i.toString(), 10)].isCopied) {
                                    organize_undoredoutils_1.insertRemovedPages.call(this, actionObject_1.toolbarActions[parseInt(i.toString(), 10)], actionObject_1.toolbarActions[parseInt(i.toString(), 10)].currentPageIndex, mainTileElement);
                                    this.tileImageRender(actionObject_1.toolbarActions[parseInt(i.toString(), 10)].copiedPageIndex, 0, actionObject_1.toolbarActions[parseInt(i.toString(), 10)].currentPageIndex, mainTileElement, true, true, false);
                                }
                                else if (actionObject_1.toolbarActions[parseInt(i.toString(), 10)].isInserted) {
                                    organize_undoredoutils_1.insertRemovedPages.call(this, actionObject_1.toolbarActions[parseInt(i.toString(), 10)], actionObject_1.toolbarActions[parseInt(i.toString(), 10)].currentPageIndex, mainTileElement);
                                    this.tileImageRender(actionObject_1.toolbarActions[parseInt(i.toString(), 10)].copiedPageIndex, 0, actionObject_1.toolbarActions[parseInt(i.toString(), 10)].currentPageIndex, mainTileElement, true, true, true);
                                }
                                else if (actionObject_1.toolbarActions[parseInt(i.toString(), 10)].isImportedDoc) {
                                    organize_undoredoutils_1.insertRemovedPages.call(this, actionObject_1.toolbarActions[parseInt(i.toString(), 10)], actionObject_1.toolbarActions[parseInt(i.toString(), 10)].currentPageIndex, mainTileElement);
                                    this.tileImageRender(actionObject_1.toolbarActions[parseInt(i.toString(), 10)].copiedPageIndex, 0, actionObject_1.toolbarActions[parseInt(i.toString(), 10)].currentPageIndex, mainTileElement, true, true, false, true, actionObject_1.toolbarActions[parseInt(i.toString(), 10)].documentName);
                                }
                                else if (!actionObject_1.toolbarActions[parseInt(i.toString(), 10)].isCopied &&
                                    !actionObject_1.toolbarActions[parseInt(i.toString(), 10)].isInserted &&
                                    !actionObject_1.toolbarActions[parseInt(i.toString(), 10)].isImportedDoc) {
                                    organize_undoredoutils_1.undoDeletedPage.call(this, actionObject_1.toolbarActions[parseInt(i.toString(), 10)].currentPageIndex, actionObject_1.toolbarActions[parseInt(i.toString(), 10)].pageIndex, actionObject_1.toolbarActions[parseInt(i.toString(), 10)].rotateAngle, mainTileElement);
                                    this.tileImageRender(actionObject_1.toolbarActions[parseInt(i.toString(), 10)].pageIndex, 0, actionObject_1.toolbarActions[parseInt(i.toString(), 10)].currentPageIndex, mainTileElement, true, true, false);
                                }
                                organize_initialization_1.updatePageDetail.call(this);
                            }
                        }
                        tile_interaction_1.disableTileDeleteButton.call(this);
                    }
                    break;
            }
        }
    }
    exports.undoActionHandler = undoActionHandler;
    function redoActionHandler(redoActionObject) {
        if (redoActionObject) {
            var actionObject = JSON.parse(JSON.stringify(redoActionObject));
            switch (actionObject.action) {
                case 'Insert Right':
                case 'Insert Left':
                    {
                        var mainTileElement = this.tileAreaDiv.childNodes[parseInt(actionObject.UndoRedoTileActions[0].
                            currentPageIndex.toString(), 10)];
                        organize_undoredoutils_1.insertRemovedPages.call(this, actionObject.UndoRedoTileActions[0], actionObject.UndoRedoTileActions[0].currentPageIndex, mainTileElement);
                        this.tileImageRender(actionObject.UndoRedoTileActions[0].copiedPageIndex, 0, actionObject.UndoRedoTileActions[0].currentPageIndex, mainTileElement, true, true, true);
                        tile_interaction_1.disableTileCopyButton.call(this);
                        organize_initialization_1.updatePageDetail.call(this);
                    }
                    break;
                case 'Rotate Right':
                    rotateImage.call(this, actionObject.UndoRedoTileActions[0].currentPageIndex, 90);
                    break;
                case 'Rotate Left':
                    rotateImage.call(this, actionObject.UndoRedoTileActions[0].currentPageIndex, -90);
                    break;
                case 'Copy':
                    {
                        var mainTileElement = this.tileAreaDiv.childNodes[parseInt(actionObject.UndoRedoTileActions[0].
                            currentPageIndex.toString(), 10)];
                        organize_undoredoutils_1.insertRemovedPages.call(this, actionObject.UndoRedoTileActions[0], actionObject.UndoRedoTileActions[0].currentPageIndex, mainTileElement);
                        this.tileImageRender(actionObject.UndoRedoTileActions[0].copiedPageIndex, 0, actionObject.UndoRedoTileActions[0].currentPageIndex, mainTileElement, true, true, false);
                        organize_initialization_1.updatePageDetail.call(this);
                    }
                    break;
                case 'Import Pages':
                    {
                        var mainTileElement = this.tileAreaDiv.childNodes[parseInt(actionObject.UndoRedoTileActions[0].
                            currentPageIndex.toString(), 10)];
                        organize_undoredoutils_1.insertRemovedPages.call(this, actionObject.UndoRedoTileActions[0], actionObject.UndoRedoTileActions[0].currentPageIndex, mainTileElement);
                        this.tileImageRender(actionObject.UndoRedoTileActions[0].copiedPageIndex, 0, actionObject.UndoRedoTileActions[0].currentPageIndex, mainTileElement, true, true, false, true, actionObject.UndoRedoTileActions[0].documentName);
                        tile_interaction_1.disableTileCopyRotateButton.call(this);
                        organize_initialization_1.updatePageDetail.call(this);
                    }
                    break;
                case 'Delete':
                    removePage.call(this, actionObject.UndoRedoTileActions[0].currentPageIndex);
                    break;
                case 'Move Pages':
                    organizepages_editor_1.rearrangePages.call(this, actionObject.selectedPagesIndexes, actionObject.dropIndex, actionObject.isRightInsertion);
                    break;
                case 'Toolbar Rotate Right':
                    rotateImages.call(this, actionObject, 90);
                    break;
                case 'Toolbar Rotate Left':
                    rotateImages.call(this, actionObject, -90);
                    break;
                case 'Toolbar Copy':
                    {
                        if (actionObject.toolbarActions.length > 0) {
                            for (var i = 0; i < actionObject.toolbarActions.length; i++) {
                                var mainTileElement = this.tileAreaDiv.childNodes[parseInt(actionObject.toolbarActions[parseInt(i.toString(), 10)].
                                    currentPageIndex.toString(), 10)];
                                organize_undoredoutils_1.insertRemovedPages.call(this, actionObject.toolbarActions[parseInt(i.toString(), 10)], actionObject.toolbarActions[parseInt(i.toString(), 10)].currentPageIndex, mainTileElement);
                                this.tileImageRender(actionObject.toolbarActions[parseInt(i.toString(), 10)].copiedPageIndex, 0, actionObject.toolbarActions[parseInt(i.toString(), 10)].currentPageIndex, mainTileElement, true, true, false);
                                organize_initialization_1.updatePageDetail.call(this);
                            }
                        }
                        tile_interaction_1.disableTileDeleteButton.call(this);
                    }
                    break;
                case 'Toolbar Delete':
                    if (actionObject.toolbarActions.length > 0) {
                        for (var i = actionObject.toolbarActions.length - 1; i >= 0; i--) {
                            var mainTileElement = this.tileAreaDiv.childNodes[parseInt(actionObject.toolbarActions[parseInt(i.toString(), 10)].
                                currentPageIndex.toString(), 10)];
                            organize_undoredoutils_1.deleteTempPage.call(this, actionObject.toolbarActions[parseInt(i.toString(), 10)].currentPageIndex, mainTileElement);
                            this.tileAreaDiv.removeChild(mainTileElement);
                            organize_initialization_1.updatePageDetail.call(this);
                        }
                    }
                    tile_interaction_1.disableTileDeleteButton.call(this);
                    break;
            }
        }
    }
    exports.redoActionHandler = redoActionHandler;
    function undo() {
        var undoActionObject = this.undoOrganizeCollection.pop();
        undoActionHandler.call(this, undoActionObject);
        if (this.isExtractToolbarVisible) {
            organize_extract_1.inputTextboxUpdate.call(this);
        }
        this.redoOrganizeCollection.push(undoActionObject);
        organize_toolbar_1.updateSelectAllCheckbox.call(this);
        organize_toolbar_1.enableDisableToolbarItems.call(this);
        updateUndoRedoButtons.call(this);
    }
    exports.undo = undo;
    function redo() {
        var redoActionObject = this.redoOrganizeCollection.pop();
        redoActionHandler.call(this, redoActionObject);
        if (this.isExtractToolbarVisible) {
            organize_extract_1.inputTextboxUpdate.call(this);
        }
        this.undoOrganizeCollection.push(redoActionObject);
        organize_toolbar_1.updateSelectAllCheckbox.call(this);
        organize_toolbar_1.enableDisableToolbarItems.call(this);
        updateUndoRedoButtons.call(this);
    }
    exports.redo = redo;
    function removePage(pageOrder) {
        var mainTileElement = this.tileAreaDiv.childNodes[parseInt(pageOrder.toString(), 10)];
        organize_undoredoutils_1.deleteTempPage.call(this, pageOrder, mainTileElement);
        this.tileAreaDiv.removeChild(mainTileElement);
        organize_initialization_1.updatePageDetail.call(this);
    }
    exports.removePage = removePage;
    function rotateImages(actionObject, rotationAngle) {
        if (actionObject.toolbarActions.length > 0) {
            for (var i = 0; i < actionObject.toolbarActions.length; i++) {
                var mainTileElement = this.tileAreaDiv.childNodes[parseInt(actionObject.toolbarActions[parseInt(i.toString(), 10)].
                    currentPageIndex.toString(), 10)];
                var imageContainer = mainTileElement.querySelector('.e-pv-organize-image');
                if (imageContainer) {
                    var currentRotation = parseFloat(imageContainer.style.transform.replace('rotate(', '').replace('deg)', '')) || 0;
                    currentRotation = (currentRotation + rotationAngle + 360) % 360;
                    imageContainer.style.transform = "rotate(" + currentRotation + "deg)";
                    tile_interaction_1.updateTempRotationDetail.call(this, actionObject.toolbarActions[parseInt(i.toString(), 10)].currentPageIndex, rotationAngle);
                }
            }
        }
    }
    exports.rotateImages = rotateImages;
    function rotateImage(currentPageIndex, rotationAngle) {
        var mainTileElement = this.tileAreaDiv.childNodes[parseInt(currentPageIndex.toString(), 10)];
        var imageContainer = mainTileElement.querySelector('.e-pv-organize-image');
        if (imageContainer) {
            var currentRotation = parseFloat(imageContainer.style.transform.replace('rotate(', '').replace('deg)', '')) || 0;
            currentRotation = (currentRotation + rotationAngle + 360) % 360;
            imageContainer.style.transform = "rotate(" + currentRotation + "deg)";
            tile_interaction_1.updateTempRotationDetail.call(this, currentPageIndex, rotationAngle);
        }
    }
    exports.rotateImage = rotateImage;
});