all files / rich-text-editor/renderer/ popup-uploader-renderer.js

100% Statements 220/220
85.92% Branches 122/142
100% Functions 24/24
100% Lines 220/220
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 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385   2831× 2831× 2831×   31× 31× 31× 31× 31×       31×                     31× 31× 31× 31×   14× 14×   10× 10×     31×   31×   31× 31× 31× 31× 31× 31× 31×   15× 15× 15× 15× 15×       31×                                             32× 32× 32× 31×       20× 20× 20× 20× 19×                                                                                                 31× 31× 31×                 31× 31× 31×   10× 10× 10×         19× 19×   18× 18×       12× 12× 12×     18× 18× 15× 15× 15×                       18× 18× 18×   18×           12×               15×   15× 15×     14× 14× 14×       33× 33×       36× 36× 36× 36× 36× 36×   33× 33× 32×       35× 35×                      
define(["require", "exports", "@syncfusion/ej2-base", "@syncfusion/ej2-popups", "@syncfusion/ej2-inputs", "../base/classes", "../base/constant", "../../common/enum", "../../common/constant"], function (require, exports, ej2_base_1, ej2_popups_1, ej2_inputs_1, classes, events, enum_1, constant_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var PopupUploader = (function () {
        function PopupUploader(parent) {
            this.isDestroyed = false;
            this.parent = parent;
            this.rteID = parent.element.id;
        }
        PopupUploader.prototype.renderPopup = function (type, element) {
            var popupElement = this.parent.createElement('div');
            popupElement.setAttribute('id', ej2_base_1.getUniqueID(this.rteID + '_' + type.toLowerCase() + '_upload_popup'));
            this.parent.rootContainer.appendChild(popupElement);
            var boundObj = { popupRoot: popupElement, self: this };
            var uploadEle = this.parent.createElement('input', {
                id: ej2_base_1.getUniqueID(this.rteID + '_' + type.toLowerCase() + '_upload'),
                attrs: { type: 'File', name: 'UploadFiles' }
            });
            var popup = new ej2_popups_1.Popup(popupElement, {
                relateTo: element,
                viewPortElement: this.parent.inputElement,
                zIndex: 10001,
                content: uploadEle,
                enableRtl: this.parent.enableRtl,
                height: '85px',
                width: '300px',
                actionOnScroll: 'none',
                close: this.onPopupClose.bind(boundObj)
            });
            popup.element.style.display = 'none';
            ej2_base_1.addClass([popup.element], classes.CLS_POPUP_OPEN);
            ej2_base_1.addClass([popup.element], classes.CLS_RTE_UPLOAD_POPUP);
            switch (type) {
                case 'Images':
                    popup.element.classList.add(classes.CLS_RTE_IMAGE_UPLOAD_POPUP);
                    break;
                case 'Videos':
                    popup.element.classList.add(classes.CLS_RTE_VIDEO_UPLOAD_POPUP);
                    break;
                case 'Audios':
                    popup.element.classList.add(classes.CLS_RTE_AUDIO_UPLOAD_POPUP);
                    break;
            }
            if (!ej2_base_1.isNullOrUndefined(this.parent.cssClass)) {
                ej2_base_1.addClass([popup.element], this.parent.cssClass.replace(/\s+/g, ' ').trim().split(' '));
            }
            return popup;
        };
        PopupUploader.prototype.createUploader = function (type, dragEvent, mediaElement, target, popup) {
            var _this = this;
            var isUploading = false;
            var allowedExtensions = '';
            var saveUrl;
            var removeUrl;
            var maxFileSize;
            switch (type) {
                case 'Images':
                    allowedExtensions = this.parent.insertImageSettings.allowedTypes.toString();
                    saveUrl = this.parent.insertImageSettings.saveUrl;
                    removeUrl = this.parent.insertImageSettings.removeUrl;
                    maxFileSize = this.parent.insertImageSettings.maxFileSize;
                    break;
                case 'Videos':
                    allowedExtensions = this.parent.insertVideoSettings.allowedTypes.toString();
                    saveUrl = this.parent.insertVideoSettings.saveUrl;
                    removeUrl = this.parent.insertVideoSettings.removeUrl;
                    maxFileSize = this.parent.insertVideoSettings.maxFileSize;
                    break;
                case 'Audios':
                    allowedExtensions = this.parent.insertAudioSettings.allowedTypes.toString();
                    saveUrl = this.parent.insertAudioSettings.saveUrl;
                    removeUrl = this.parent.insertAudioSettings.removeUrl;
                    maxFileSize = this.parent.insertAudioSettings.maxFileSize;
                    break;
            }
            var uploader = new ej2_inputs_1.Uploader({
                asyncSettings: {
                    saveUrl: saveUrl,
                    removeUrl: removeUrl
                },
                cssClass: classes.CLS_RTE_DIALOG_UPLOAD + this.parent.getCssClass(true),
                allowedExtensions: allowedExtensions,
                maxFileSize: maxFileSize,
                multiple: false,
                enableRtl: this.parent.enableRtl,
                removing: function () {
                    isUploading = false;
                    Eif (mediaElement) {
                        ej2_base_1.detach(mediaElement);
                    }
                    Eif (popup) {
                        popup.close();
                        _this.enableToolbarItems();
                    }
                },
                canceling: function () {
                    isUploading = false;
                    Eif (mediaElement) {
                        ej2_base_1.detach(mediaElement);
                    }
                    Eif (popup) {
                        popup.close();
                        _this.enableToolbarItems();
                    }
                    Eif (_this.parent.quickToolbarModule) {
                        if (type === 'Images' && _this.parent.quickToolbarModule.imageQTBar) {
                            _this.parent.quickToolbarModule.imageQTBar.hidePopup();
                        }
                        else if (type === 'Videos' && _this.parent.quickToolbarModule.videoQTBar) {
                            _this.parent.quickToolbarModule.videoQTBar.hidePopup();
                        }
                        else if (type === 'Audios' && _this.parent.quickToolbarModule.audioQTBar) {
                            _this.parent.quickToolbarModule.audioQTBar.hidePopup();
                        }
                    }
                },
                beforeUpload: function (args) {
                    var eventName = type === 'Images' ? events.beforeImageUpload : events.beforeFileUpload;
                    _this.parent.trigger(eventName, args);
                    if (!_this.parent.inlineMode.enable && _this.parent.toolbarModule && _this.parent.toolbarModule.baseToolbar) {
                        _this.parent.toolbarModule.baseToolbar.toolbarObj.disable(true);
                    }
                },
                uploading: function (args) {
                    Eif (!_this.parent.isServerRendered) {
                        isUploading = true;
                        var eventName = type === 'Images' ? events.imageUploading : events.fileUploading;
                        _this.parent.trigger(eventName, args, function (uploadingArgs) {
                            if (uploadingArgs.cancel) {
                                Eif (mediaElement && !ej2_base_1.isNullOrUndefined(mediaElement)) {
                                    ej2_base_1.detach(mediaElement);
                                }
                                Eif (popup && popup.element && !ej2_base_1.isNullOrUndefined(popup.element)) {
                                    ej2_base_1.detach(popup.element);
                                }
                            }
                        });
                    }
                },
                selected: function (args) {
                    Eif (isUploading) {
                        args.cancel = true;
                    }
                },
                failure: function (args) {
                    isUploading = false;
                    var popupArgs = {
                        args: dragEvent,
                        type: type,
                        isNotify: undefined,
                        elements: mediaElement
                    };
                    _this.uploadFailureTime = setTimeout(function () {
                        _this.uploadFailure(mediaElement, popupArgs, args, popup);
                    }, 900);
                },
                success: function (args) {
                    if (args.operation === 'cancel') {
                        return;
                    }
                    isUploading = false;
                    var popupArgs = {
                        args: dragEvent,
                        type: type,
                        isNotify: undefined,
                        elements: mediaElement
                    };
                    var imageArgs;
                    var mediaArgs;
                    if (type === 'Images') {
                        imageArgs = {
                            e: args.e,
                            file: args.file,
                            statusText: args.statusText,
                            operation: args.operation,
                            response: args.response,
                            element: mediaElement,
                            detectImageSource: enum_1.ImageInputSource.Dropped
                        };
                    }
                    else {
                        mediaArgs = {
                            e: args.e,
                            file: args.file,
                            statusText: args.statusText,
                            operation: args.operation,
                            response: args.response,
                            element: mediaElement,
                            detectMediaSource: enum_1.MediaInputSource.Dropped
                        };
                    }
                    _this.uploadSuccessTime = setTimeout(function () {
                        if (type === 'Images') {
                            _this.uploadSuccess(mediaElement, dragEvent, popupArgs, imageArgs, popup);
                        }
                        else Eif (type === 'Videos' || type === 'Audios') {
                            _this.uploadSuccess(mediaElement, dragEvent, popupArgs, mediaArgs, popup);
                        }
                    }, 900);
                }
            });
            uploader.appendTo(target);
            var file = dragEvent.dataTransfer.files[0];
            var fileInfo = [{
                    name: file.name,
                    rawFile: file,
                    size: file.size,
                    type: file.type,
                    status: 'Ready to Upload',
                    validationMessages: { minSize: '', maxSize: '' },
                    statusCode: '1'
                }];
            uploader.createFileList(fileInfo);
            uploader.upload(fileInfo);
            return uploader;
        };
        PopupUploader.prototype.uploadFailure = function (mediaEle, args, e, popup) {
            var eventName = args.type === 'Images' ? events.imageUploadFailed : events.fileUploadFailed;
            this.parent.trigger(eventName, e);
            if (!ej2_base_1.isNullOrUndefined(mediaEle) && !ej2_base_1.isNullOrUndefined(popup)) {
                ej2_base_1.detach(mediaEle);
                popup.close();
                this.enableToolbarItems();
            }
            else {
                return;
            }
        };
        PopupUploader.prototype.uploadSuccess = function (mediaElement, dragEvent, args, e, popup) {
            var _this = this;
            if (e.operation === 'cancel') {
                return;
            }
            mediaElement.style.opacity = '1';
            switch (args.type) {
                case 'Images':
                    mediaElement.classList.add(constant_1.CLS_IMG_FOCUS);
                    break;
                case 'Videos':
                case 'Audios': {
                    var focusClass = args.type === 'Videos' ? constant_1.CLS_VID_FOCUS : constant_1.CLS_AUD_FOCUS;
                    mediaElement.classList.add(focusClass);
                    break;
                }
            }
            var eventName = args.type === 'Images' ? events.imageUploadSuccess : events.fileUploadSuccess;
            this.parent.trigger(eventName, e, function (responseData) {
                Eif (mediaElement && responseData.file) {
                    var fileName = responseData.file.name;
                    switch (args.type) {
                        case 'Images':
                            if (!ej2_base_1.isNullOrUndefined(_this.parent.insertImageSettings.path)) {
                                var url = _this.parent.insertImageSettings.path + fileName;
                                mediaElement.src = url;
                                mediaElement.setAttribute('alt', fileName);
                            }
                            break;
                        case 'Videos':
                            if (!ej2_base_1.isNullOrUndefined(_this.parent.insertVideoSettings.path)) {
                                var url = _this.parent.insertVideoSettings.path + fileName;
                                var sourceElement = mediaElement.querySelector('source');
                                Eif (sourceElement) {
                                    sourceElement.setAttribute('src', url);
                                    var fileExtension = fileName.split('.').pop().toLowerCase();
                                    sourceElement.setAttribute('type', 'video/' + fileExtension);
                                    mediaElement.style.opacity = '1';
                                }
                            }
                            break;
                        case 'Audios':
                            if (!ej2_base_1.isNullOrUndefined(_this.parent.insertAudioSettings.path)) {
                                var url = _this.parent.insertAudioSettings.path + fileName;
                                var sourceElement = mediaElement.querySelector('source');
                                Eif (sourceElement) {
                                    sourceElement.setAttribute('src', url);
                                    var fileExtension = fileName.split('.').pop().toLowerCase();
                                    sourceElement.setAttribute('type', 'audio/' + fileExtension);
                                    mediaElement.style.opacity = '1';
                                    mediaElement.load();
                                }
                            }
                            break;
                    }
                }
            });
            Eif (popup) {
                popup.close();
                this.enableToolbarItems();
            }
            if (args.type === 'Images') {
                this.parent.notify(events.insertCompleted, args);
                Eif (this.parent.insertImageSettings.resize) {
                    this.parent.notify(events.resizeStart, {
                        event: dragEvent,
                        target: mediaElement
                    });
                }
            }
            else if (args.type === 'Videos') {
                this.parent.notify(events.insertCompleted, args);
                setTimeout(function () {
                    _this.parent.notify(events.resizeStart, {
                        event: dragEvent,
                        target: mediaElement
                    });
                }, 100);
            }
            else Eif (args.type === 'Audios') {
                this.parent.notify(events.insertCompleted, args);
            }
        };
        PopupUploader.prototype.refreshPopup = function (targetElement, popup) {
            var targetPosition = this.parent.iframeSettings.enable ?
                this.parent.element.offsetTop + targetElement.offsetTop : targetElement.offsetTop;
            var rtePosition = this.parent.element.offsetTop + this.parent.element.offsetHeight;
            if (targetPosition > rtePosition) {
                popup.offsetY = this.parent.iframeSettings.enable ? -30 : -65;
                popup.element.style.display = 'block';
            }
            else {
                Eif (popup) {
                    popup.refreshPosition(targetElement);
                    popup.element.style.display = 'block';
                }
            }
        };
        PopupUploader.prototype.enableToolbarItems = function () {
            var mediaPopups = this.parent.element.querySelectorAll('.e-rte-upload-popup');
            if (!this.parent.inlineMode.enable && this.parent.toolbarModule &&
                this.parent.toolbarModule.baseToolbar && mediaPopups.length === 0) {
                this.parent.toolbarModule.baseToolbar.toolbarObj.disable(false);
            }
        };
        PopupUploader.prototype.onPopupClose = function () {
            var currentPopupElem = this.popupRoot;
            var currentPopupUploderObj = this.self;
            Eif (!ej2_base_1.isNullOrUndefined(currentPopupElem) && !ej2_base_1.isNullOrUndefined(currentPopupUploderObj)) {
                var popupObj = ej2_base_1.getComponent(currentPopupElem, 'popup');
                currentPopupUploderObj.parent.isBlur = false;
                if (ej2_base_1.isNullOrUndefined(popupObj)) {
                    return;
                }
                var uploaderObj = currentPopupUploderObj.getUploaderInstance(currentPopupElem);
                if (ej2_base_1.isNullOrUndefined(uploaderObj)) {
                    return;
                }
                uploaderObj.destroy();
                popupObj.destroy();
                ej2_base_1.detach(currentPopupElem);
            }
        };
        PopupUploader.prototype.getUploaderInstance = function (element) {
            var currentUploader = element.querySelector('.e-uploader');
            return ej2_base_1.getComponent(currentUploader, 'uploader');
        };
        PopupUploader.prototype.destroy = function () {
            if (this.isDestroyed) {
                return;
            }
            if (!ej2_base_1.isNullOrUndefined(this.uploadFailureTime)) {
                clearTimeout(this.uploadFailureTime);
                this.uploadFailureTime = null;
            }
            if (!ej2_base_1.isNullOrUndefined(this.uploadSuccessTime)) {
                clearTimeout(this.uploadSuccessTime);
                this.uploadSuccessTime = null;
            }
            var mediaPopups = this.parent.element.querySelectorAll('.e-rte-upload-popup');
            for (var i = 0; i < mediaPopups.length; i++) {
                var uploader = this.getUploaderInstance(mediaPopups[i]);
                if (uploader && !uploader.isDestroyed) {
                    uploader.destroy();
                }
                var popup = ej2_base_1.getComponent(mediaPopups[i], 'popup');
                Eif (popup && !popup.isDestroyed) {
                    popup.destroy();
                }
            }
            this.isDestroyed = true;
        };
        return PopupUploader;
    }());
    exports.PopupUploader = PopupUploader;
});