all files / editor-manager/plugin/ audio.js

97.5% Statements 78/80
93.75% Branches 60/64
100% Functions 11/11
97.5% Lines 78/80
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   1907× 1907×   1907× 1907×   1762× 1762×   19× 19× 19×   19×     11× 11×           15× 15× 15× 15×         13× 13× 13× 13× 13× 13× 13× 13× 13×   13× 13× 13× 13×     15×                             15× 14×   15×   15×     15×     13×                         1762×        
define(["require", "exports", "@syncfusion/ej2-base", "./../base/constant", "./../base/classes", "./inserthtml", "./../../common/constant"], function (require, exports, ej2_base_1, CONSTANT, classes, inserthtml_1, EVENTS) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var AudioCommand = (function () {
        function AudioCommand(parent) {
            this.parent = parent;
            this.addEventListener();
        }
        AudioCommand.prototype.addEventListener = function () {
            this.parent.observer.on(CONSTANT.AUDIO, this.audioCommand, this);
            this.parent.observer.on(EVENTS.INTERNAL_DESTROY, this.destroy, this);
        };
        AudioCommand.prototype.removeEventListener = function () {
            this.parent.observer.off(CONSTANT.AUDIO, this.audioCommand);
            this.parent.observer.off(EVENTS.INTERNAL_DESTROY, this.destroy);
        };
        AudioCommand.prototype.audioCommand = function (e) {
            var selectNode;
            var value = e.value.toString().toLowerCase();
            if (value === 'inline' || value === 'break' || value === 'audioremove') {
                selectNode = e.item.selectNode[0];
            }
            switch (value) {
                case 'audio':
                case 'audioreplace':
                    this.createAudio(e);
                    break;
                case 'inline':
                    selectNode.removeAttribute('class');
                    selectNode.closest('.' + classes.CLASS_AUDIO_WRAP).style.display = 'inline-block';
                    ej2_base_1.addClass([selectNode], [classes.CLASS_AUDIO, classes.CLASS_AUDIO_INLINE, classes.CLASS_AUDIO_FOCUS]);
                    this.callBack(e);
                    break;
                case 'break':
                    selectNode.removeAttribute('class');
                    selectNode.closest('.' + classes.CLASS_AUDIO_WRAP).style.display = 'block';
                    ej2_base_1.addClass([selectNode], [classes.CLASS_AUDIO_BREAK, classes.CLASS_AUDIO, classes.CLASS_AUDIO_FOCUS]);
                    this.callBack(e);
                    break;
                case 'audioremove':
                    ej2_base_1.detach(selectNode);
                    this.callBack(e);
                    break;
            }
        };
        AudioCommand.prototype.createAudio = function (e) {
            var _this = this;
            var isReplaced = false;
            var wrapElement;
            if (!ej2_base_1.isNullOrUndefined(e.item.selectParent) && e.item.selectParent[0].classList &&
                (e.item.selectParent[0].classList.contains(classes.CLASS_CLICK_ELEM) ||
                    e.item.selectParent[0].classList.contains(classes.CLASS_AUDIO_WRAP))) {
                var audioEle = e.item.selectParent[0].querySelector('source');
                this.setStyle(audioEle, e);
                isReplaced = true;
            }
            else {
                wrapElement = ej2_base_1.createElement('span', { className: classes.CLASS_AUDIO_WRAP, attrs: { contentEditable: 'false', title: ((!ej2_base_1.isNullOrUndefined(e.item.title)) ? e.item.title : (!ej2_base_1.isNullOrUndefined(e.item.fileName) ? e.item.fileName : '')) } });
                var audElement = ej2_base_1.createElement('audio', { className: classes.CLASS_AUDIO + ' ' + classes.CLASS_AUDIO_INLINE, attrs: { controls: '' } });
                var sourceElement = ej2_base_1.createElement('source');
                var clickElement = ej2_base_1.createElement('span', { className: classes.CLASS_CLICK_ELEM });
                this.setStyle(sourceElement, e);
                audElement.appendChild(sourceElement);
                clickElement.appendChild(audElement);
                wrapElement.appendChild(clickElement);
                if (!ej2_base_1.isNullOrUndefined(e.item.selection)) {
                    e.item.selection.restore();
                }
                inserthtml_1.InsertHtml.Insert(this.parent.currentDocument, wrapElement, this.parent.editableElement);
                Eif (wrapElement.nextElementSibling === null) {
                    var insertElem = ej2_base_1.createElement('br');
                    wrapElement.parentNode.insertBefore(insertElem, wrapElement.nextSibling);
                }
            }
            if (e.callBack && (ej2_base_1.isNullOrUndefined(e.selector) || !ej2_base_1.isNullOrUndefined(e.selector) && e.selector !== 'pasteCleanupModule')) {
                var selectedNode = this.parent.nodeSelection.getSelectedNodes(this.parent.currentDocument)[0];
                var audioElm_1 = (e.value === 'AudioReplace' || isReplaced) ? e.item.selectParent[0].querySelector('audio')
                    : (ej2_base_1.Browser.isIE ? selectedNode : selectedNode.querySelector('audio'));
                audioElm_1.addEventListener('loadeddata', function () {
                    if (e.value !== 'AudioReplace' || !isReplaced) {
                        Iif (!ej2_base_1.isNullOrUndefined(_this.parent.currentDocument)) {
                            e.callBack({
                                requestType: 'Audios',
                                editorMode: 'HTML',
                                event: e.event,
                                range: _this.parent.nodeSelection.getRange(_this.parent.currentDocument),
                                elements: [audioElm_1]
                            });
                        }
                    }
                });
                if (isReplaced) {
                    audioElm_1.load();
                }
            }
        };
        AudioCommand.prototype.setStyle = function (sourceElement, e) {
            if (!ej2_base_1.isNullOrUndefined(e.item.url)) {
                sourceElement.setAttribute('src', e.item.url);
            }
            var fileExtension = e.item.fileName ? e.item.fileName.split('.').pop().toLowerCase() :
                e.item.url ? e.item.url.split('.').pop().toLowerCase() : '';
            Iif (fileExtension === 'opus') {
                sourceElement.type = 'audio/ogg';
            }
            else if (fileExtension === 'm4a') {
                sourceElement.type = 'audio/mp4';
            }
            else {
                sourceElement.type = e.item.fileName && e.item.fileName.split('.').length > 0 ?
                    'audio/' + e.item.fileName.split('.')[e.item.fileName.split('.').length - 1] :
                    e.item.url && e.item.url.split('.').length > 0 ? 'audio/' + e.item.url.split('.')[e.item.url.split('.').length - 1] : '';
            }
        };
        AudioCommand.prototype.callBack = function (e) {
            Eif (e.callBack) {
                e.callBack({
                    requestType: e.item.subCommand,
                    editorMode: 'HTML',
                    event: e.event,
                    range: this.parent.nodeSelection.getRange(this.parent.currentDocument),
                    elements: this.parent.nodeSelection.getSelectedNodes(this.parent.currentDocument)
                });
            }
        };
        AudioCommand.prototype.destroy = function () {
            this.removeEventListener();
        };
        return AudioCommand;
    }());
    exports.AudioCommand = AudioCommand;
});