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

97.22% Statements 70/72
87.5% Branches 14/16
93.33% Functions 14/15
97.18% Lines 69/71
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   1847× 1847×   1847× 1847× 1847×                                                                               3674× 1828×   1846× 1846× 1846× 1846×     34087×        
define(["require", "exports", "../base/constant", "@syncfusion/ej2-inputs"], function (require, exports, events, ej2_inputs_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
            };
            this.parent.trigger(events.actionBegin, actionBegin, function (actionBeginArgs) {
                Eif (!actionBeginArgs.cancel) {
                    _this.uploaderObj = new ej2_inputs_1.Uploader({
                        allowedExtensions: '.doc,.docx,.rtf,.dot,.dotx,.docm,.dotm',
                        asyncSettings: {
                            saveUrl: _this.parent.importWord.serviceUrl
                        },
                        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 + "</style></head><body>" + rteHtmlData + "</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 + "</style></head><body>" + rteHtmlData + "</body></html>";
            }
            var actionBegin = {
                requestType: args.member,
                exportValue: html,
                cancel: false
            };
            this.parent.trigger(events.actionBegin, actionBegin, function (actionBeginArgs) {
                if (!actionBeginArgs.cancel) {
                    fetch(serviceUrl, {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json'
                        },
                        body: JSON.stringify({ html: actionBeginArgs.exportValue })
                    })
                        .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;
});