all files / rich-text-editor/actions/ import-export.js

97.59% Statements 81/83
88.46% Branches 23/26
93.75% Functions 15/16
97.56% Lines 80/82
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   2746× 2746×   2746× 2746× 2746×                                         10× 10× 10× 10× 10× 10× 10×     10×         10× 10×                                                 5466× 2721×   2745× 2745× 2745× 2745×     62201×        
define(["require", "exports", "../base/constant", "@syncfusion/ej2-inputs", "../../common/export-styles", "@syncfusion/ej2-base"], function (require, exports, events, ej2_inputs_1, export_styles_1, ej2_base_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var ImportExport = (function () {
        function ImportExport(parent) {
            this.parent = parent;
            this.addEventListener();
        }
        ImportExport.prototype.addEventListener = function () {
            this.parent.on(events.onImport, this.onImport, this);
            this.parent.on(events.onExport, this.onExport, this);
            this.parent.on(events.destroy, this.destroy, this);
        };
        ImportExport.prototype.onImport = function () {
            var _this = this;
            var actionBegin = {
                cancel: false,
                requestType: 'Import'
            };
            this.parent.trigger(events.actionBegin, actionBegin, function (actionBeginArgs) {
                if (!actionBeginArgs.cancel) {
                    Eif (ej2_base_1.isNullOrUndefined(_this.uploaderObj)) {
                        _this.uploaderObj = new ej2_inputs_1.Uploader({
                            allowedExtensions: '.doc,.docx,.rtf,.dot,.dotx,.docm,.dotm',
                            asyncSettings: {
                                saveUrl: _this.parent.importWord.serviceUrl
                            },
                            uploading: function (e) {
                                Eif (!_this.parent.isServerRendered) {
                                    _this.parent.trigger(events.wordImporting, e);
                                }
                            },
                            success: function (args) {
                                _this.parent.executeCommand('importWord', args.e.currentTarget.response, { undo: true });
                                _this.parent.trigger(events.actionComplete, { requestType: 'Import' });
                            }
                        });
                    }
                    _this.parent.setProperties({ enableXhtml: true }, true);
                    var uploadParentEle = _this.parent.createElement('div', { className: 'e-import-uploadwrap e-droparea' + _this.parent.getCssClass(true) });
                    var uploadEle = _this.parent.createElement('input', {
                        id: _this.rteID + '_upload', attrs: { type: 'File', name: 'UploadFiles' }, className: _this.parent.getCssClass()
                    });
                    uploadParentEle.appendChild(uploadEle);
                    _this.uploaderObj.appendTo(uploadEle);
                    _this.uploaderObj.element.click();
                    _this.uploaderObj.element.closest('.e-upload').style.display = 'none';
                }
            });
        };
        ImportExport.prototype.onExport = function (args) {
            var _this = this;
            var filename;
            var serviceUrl;
            this.parent.setProperties({ enableXhtml: true }, true);
            var rteHtmlData = this.parent.getHtml();
            var html;
            if (args.member === 'ExportWord') {
                filename = this.parent.exportWord.fileName;
                serviceUrl = this.parent.exportWord.serviceUrl;
                html = "<html><head><style>" + (this.parent.exportWord.stylesheet + export_styles_1.EXPORT_STYLES) + "</style></head><body><div class=\"e-content\">" + rteHtmlData + "</div></body></html>";
            }
            else if (args.member === 'ExportPdf') {
                filename = this.parent.exportPdf.fileName;
                serviceUrl = this.parent.exportPdf.serviceUrl;
                html = "<html><head><style>" + (this.parent.exportPdf.stylesheet + export_styles_1.EXPORT_STYLES) + "</style></head><body><div class=\"e-content\">" + rteHtmlData + "</div></body></html>";
            }
            var actionBegin = {
                requestType: args.member,
                exportValue: html,
                cancel: false
            };
            this.parent.trigger(events.actionBegin, actionBegin, function (actionBeginArgs) {
                if (!actionBeginArgs.cancel) {
                    var exportArgs = {
                        exportType: (args.member === 'ExportPdf' ? 'PDF' : 'Word')
                    };
                    _this.parent.trigger(events.documentExporting, exportArgs);
                    var headers = {
                        'Content-Type': 'application/json'
                    };
                    if (exportArgs.currentRequest) {
                        for (var _i = 0, _a = exportArgs.currentRequest; _i < _a.length; _i++) {
                            var h = _a[_i];
                            Object.assign(headers, h);
                        }
                    }
                    var payload = exportArgs.customFormData
                        ? { html: actionBeginArgs.exportValue, formData: exportArgs.customFormData }
                        : { html: actionBeginArgs.exportValue };
                    fetch(serviceUrl, {
                        method: 'POST',
                        headers: headers,
                        body: JSON.stringify(payload)
                    })
                        .then(function (response) {
                        Iif (!response.ok) {
                            throw new Error("HTTP error! Status: " + response.status);
                        }
                        return response.blob().then(function (blob) { return ({ blob: blob, filename: filename }); });
                    })
                        .then(function (_a) {
                        var blob = _a.blob, filename = _a.filename;
                        var url = window.URL.createObjectURL(blob);
                        var a = document.createElement('a');
                        a.href = url;
                        a.download = filename;
                        document.body.appendChild(a);
                        a.click();
                        document.body.removeChild(a);
                        window.URL.revokeObjectURL(url);
                        _this.parent.trigger(events.actionComplete, { requestType: args.member });
                    })
                        .catch(function (error) {
                        console.error('Fetch error:', error);
                    });
                }
            });
        };
        ImportExport.prototype.destroy = function () {
            if (this.parent.isDestroyed) {
                return;
            }
            this.parent.off(events.onImport, this.onImport);
            this.parent.off(events.onExport, this.onExport);
            this.parent.off(events.destroy, this.destroy);
            if (this.uploaderObj && !this.uploaderObj.isDestroyed) {
                this.uploaderObj.destroy();
                this.uploaderObj = null;
            }
        };
        ImportExport.prototype.getModuleName = function () {
            return 'importExport';
        };
        return ImportExport;
    }());
    exports.ImportExport = ImportExport;
});