all files / spreadsheet/integrations/ open.js

58.26% Statements 67/115
35.29% Branches 18/51
73.33% Functions 11/15
58.26% Lines 67/115
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   883× 883× 883× 883× 883×   883× 883× 883×   883× 13× 13× 13×     883×       883× 883× 883×             24× 24×   24×         24×                                                                                                                                                                     24× 24× 24×   24×     24× 24× 24×   24×       24× 24× 24× 24×   24× 24×           39× 31× 31×     883× 883× 883× 883×   883× 883× 883×   126650×        
define(["require", "exports", "../common/event", "../common/index", "../../workbook/index", "@syncfusion/ej2-base"], function (require, exports, event_1, index_1, index_2, ej2_base_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var Open = (function () {
        function Open(parent) {
            this.isImportedFile = false;
            this.unProtectSheetIdx = [];
            this.parent = parent;
            this.addEventListener();
            this.renderFileUpload();
        }
        Open.prototype.addEventListener = function () {
            this.parent.on(index_2.openSuccess, this.openSuccess, this);
            this.parent.on(index_2.openFailure, this.openFailed, this);
            this.parent.on(index_2.sheetsDestroyed, this.sheetsDestroyHandler, this);
        };
        Open.prototype.removeEventListener = function () {
            if (!this.parent.isDestroyed) {
                this.parent.off(index_2.openSuccess, this.openSuccess);
                this.parent.off(index_2.openFailure, this.openFailed);
                this.parent.off(index_2.sheetsDestroyed, this.sheetsDestroyHandler);
            }
        };
        Open.prototype.renderFileUpload = function () {
            var uploadBox = this.parent.element.appendChild(this.parent.createElement('input', {
                id: this.parent.element.id + '_fileUpload',
                attrs: { type: 'file', accept: '.xls, .xlsx, .csv, .xlsm, .xlsb', name: 'fileUpload' }
            }));
            uploadBox.onchange = this.fileSelect.bind(this);
            uploadBox.onerror = this.openFailed.bind(this);
            uploadBox.style.display = 'none';
        };
        Open.prototype.fileSelect = function (args) {
            var filesData = args.target.files[0];
            Iif (filesData && filesData.length < 1) {
                return;
            }
            var impArgs = {
                file: filesData
            };
            this.parent.open(impArgs);
            ej2_base_1.select('#' + this.parent.element.id + '_fileUpload', this.parent.element).value = '';
        };
        Open.prototype.openSuccess = function (response) {
            var _this = this;
            var openError = ['UnsupportedFile', 'InvalidUrl', 'NeedPassword', 'InCorrectPassword', 'InCorrectSheetPassword',
                'CorrectSheetPassword', 'DataLimitExceeded', 'FileSizeLimitExceeded', 'ExternalWorkbook'];
            var openCancelFn = function (action) {
                _this.parent.serviceLocator.getService(index_1.dialog).hide(true);
                var file = new File([], response.guid, { type: action.toLowerCase() });
                _this.parent.open({ file: file, guid: response.guid, password: response.eventArgs.password, orginalFile: response.eventArgs.file });
            };
            Iif (openError.indexOf(response.data) > -1) {
                var l10n = this.parent.serviceLocator.getService(index_1.locale);
                if (openError[2] === response.data) {
                    if (!this.parent.element.querySelector('.e-importprotectworkbook-dlg')) {
                        this.parent.notify(index_1.importProtectWorkbook, response.eventArgs);
                    }
                }
                else if (openError[3] === response.data) {
                    var alertSpan = this.parent.createElement('span', { className: 'e-importprotectpwd-alert-span' });
                    alertSpan.innerText = l10n.getConstant('IncorrectPassword');
                    (this.parent.element.querySelector('.e-importprotectworkbook-dlg').querySelector('.e-dlg-content')).appendChild(alertSpan);
                }
                else if (openError[4] === response.data) {
                    var alertSpan = this.parent.createElement('span', { className: 'e-unprotectsheetpwd-alert-span' });
                    alertSpan.innerText = l10n.getConstant('IncorrectPassword');
                    (this.parent.element.querySelector('.e-unprotectworksheet-dlg').querySelector('.e-dlg-content')).appendChild(alertSpan);
                }
                else if (openError[5] === response.data) {
                    this.parent.isOpen = false;
                    this.parent.notify(event_1.unProtectSheetPassword, null);
                    var dialogInst = this.parent.serviceLocator.getService(index_1.dialog);
                    dialogInst.hide();
                    this.parent.hideSpinner();
                }
                else if (openError[8] === response.data) {
                    var dialogInst = this.parent.serviceLocator.getService(index_1.dialog);
                    dialogInst.hide(true);
                    var externalWorkbook = response.data.includes('ExternalWorkbook');
                    this.parent.serviceLocator.getService(index_1.dialog).show({
                        content: this.parent.serviceLocator.getService('spreadsheetLocale').getConstant('ExternalWorkbook'),
                        width: '350', buttons: externalWorkbook ? [
                            {
                                click: openCancelFn.bind(this, response.data + "Yes"),
                                buttonModel: { content: l10n.getConstant('Yes'), isPrimary: true }
                            },
                            {
                                click: openCancelFn.bind(this, response.data + "No"),
                                buttonModel: { content: l10n.getConstant('No') }
                            }
                        ] : [],
                        beforeOpen: function (args) {
                            var dlgArgs = {
                                dialogName: 'OpenDialog',
                                element: args.element, target: args.target, cancel: args.cancel
                            };
                            _this.parent.trigger('dialogBeforeOpen', dlgArgs);
                            if (dlgArgs.cancel) {
                                args.cancel = true;
                            }
                        }
                    }, externalWorkbook ? true : null);
                    this.parent.hideSpinner();
                    return;
                }
                else {
                    var dialogInst = this.parent.serviceLocator.getService(index_1.dialog);
                    dialogInst.hide(true);
                    var sizeLimitAlert = response.data.includes('LimitExceeded');
                    this.parent.serviceLocator.getService(index_1.dialog).show({
                        content: this.parent.serviceLocator.getService('spreadsheetLocale')
                            .getConstant(response.data),
                        width: '300', buttons: sizeLimitAlert ? [
                            { click: openCancelFn.bind(this, response.data + "Open"),
                                buttonModel: { content: l10n.getConstant('Ok'), isPrimary: true } },
                            { click: openCancelFn.bind(this, response.data + "Cancel"),
                                buttonModel: { content: l10n.getConstant('Cancel') } }
                        ] : [],
                        beforeOpen: function (args) {
                            var dlgArgs = {
                                dialogName: 'OpenDialog',
                                element: args.element, target: args.target, cancel: args.cancel
                            };
                            _this.parent.trigger('dialogBeforeOpen', dlgArgs);
                            if (dlgArgs.cancel) {
                                args.cancel = true;
                            }
                        }
                    }, sizeLimitAlert ? true : null);
                    this.parent.hideSpinner();
                    return;
                }
            }
            else {
                var dialogInst = this.parent.serviceLocator.getService(index_1.dialog);
                dialogInst.hide();
                this.parent.showSpinner();
            }
            Iif (!this.parent.element) {
                return;
            }
            Eif (openError[5] !== response.data) {
                this.parent.notify(index_2.clearFormulaDependentCells, { cellRef: null, isOpen: true });
                if (!response.isOpenFromJson) {
                    this.parent.trigger('openComplete', { response: response });
                    this.parent.notify(event_1.completeAction, { response: response, action: 'import' });
                }
                Iif (this.parent.isProtected && this.parent.showSheetTabs && response.isOpenFromJson) {
                    this.parent.element.querySelector('.e-add-sheet-tab').setAttribute('disabled', 'true');
                    this.parent.element.querySelector('.e-add-sheet-tab').classList.add('e-disabled');
                }
                this.parent.renderModule.refreshSheet(response.isOpenFromJson, false, false, false, response);
                this.parent.notify(event_1.refreshSheetTabs, null);
                this.isImportedFile = true;
                response.context.preventFormatCheck = response.eventArgs && response.eventArgs.file && response.eventArgs.file.name &&
                    !response.eventArgs.file.name.includes('.csv');
                this.unProtectSheetIdx = [];
                this.parent.hideSpinner();
            }
        };
        Open.prototype.openFailed = function (args) {
            this.parent.trigger('openFailure', args);
            this.parent.hideSpinner();
        };
        Open.prototype.sheetsDestroyHandler = function (args) {
            if (ej2_base_1.isNullOrUndefined(args.sheetIndex)) {
                this.isImportedFile = false;
                this.unProtectSheetIdx = [];
            }
        };
        Open.prototype.destroy = function () {
            this.removeEventListener();
            var uploadBox = ej2_base_1.select('#' + this.parent.element.id + '_fileUpload', this.parent.element);
            Eif (uploadBox) {
                ej2_base_1.detach(uploadBox);
            }
            this.isImportedFile = null;
            this.unProtectSheetIdx = null;
            this.parent = null;
        };
        Open.prototype.getModuleName = function () {
            return 'open';
        };
        return Open;
    }());
    exports.Open = Open;
});