all files / rich-text-editor/renderer/ audio-module.js

89.98% Statements 494/549
85.04% Branches 381/448
91.07% Functions 51/56
89.98% Lines 494/549
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 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867   1826× 1826× 1826× 1826× 1826× 1826× 1826× 1826× 1826× 1826× 1826×   1826×     1826× 1826× 1826× 1826× 1826× 1826× 1826× 1826× 1826× 1826× 1826× 1826× 1826× 1826×   1825× 1825× 1825× 1825× 1825× 1825× 1825× 1825× 1825× 1825× 1825× 1825× 1825× 1825× 1825× 1825× 1825× 1825× 1825×     1839× 1839× 1839× 1839×   32×     30×         21×       21×               358×     358×             126×             616× 616× 616× 616× 616× 616× 616× 616×   616× 598× 598×   616× 161× 161× 619×       616×   71× 71× 32×   39× 21×     616×   582× 582× 582× 582×       616×   616× 65×                             616×   14×   14×     66×   66× 66× 42× 28×         14×           24×           66×       616×                                     12×     22× 18×                                                                                         161× 16×     14× 13×       161× 15× 15×     622× 622×     622× 11×   622×                 622×       622×     285× 285× 281×                               344×       344× 344× 344× 21×     21×   323× 212× 212× 212× 212× 212× 23×     23× 23× 23×   23× 23×     189×       2056×   110×     1946×     71× 71×   48×   23× 23× 23× 23× 23× 23×     23× 23× 23×   23× 23×               23×       194×         39× 39× 35× 35× 35× 35×             1847× 39× 39× 39×   1847× 39× 39× 39×   1847× 39× 39× 39×     43× 43×   39× 39× 39× 39× 39× 39× 39× 39×                                     22×           22× 22× 11× 11×     22× 22×     39× 39× 39×   39× 39× 39× 39×   39× 39× 39×     34×   39× 39× 39× 39×   39× 39×             39× 39× 39× 39×       39× 13× 13×     12×       39×   39× 39×   39× 39× 39× 39× 39× 39×         38× 38×   39× 39× 39× 39× 39×     39× 39×     39× 39× 39× 39× 39× 39× 39× 39× 39× 39×     39× 39× 39× 39× 39×         12× 12× 12× 12× 12× 11× 11× 11×                         15×                                                     39× 39× 39× 39×   11× 11×                   10× 10× 10× 10×   10×                         3623× 1798×   1825× 1825×       1825× 1825× 1825×   32823×        
define(["require", "exports", "@syncfusion/ej2-base", "@syncfusion/ej2-base", "../base/constant", "../base/classes", "@syncfusion/ej2-inputs", "@syncfusion/ej2-buttons", "../base/enum", "../base/util", "../../common/util"], function (require, exports, ej2_base_1, ej2_base_2, events, classes, ej2_inputs_1, ej2_buttons_1, enum_1, util_1, util_2) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var Audio = (function () {
        function Audio(parent, serviceLocator) {
            this.isAudioUploaded = false;
            this.isAllowedTypes = true;
            this.deletedAudio = [];
            this.parent = parent;
            this.rteID = parent.element.id;
            this.i10n = serviceLocator.getService('rteLocale');
            this.rendererFactory = serviceLocator.getService('rendererFactory');
            this.dialogRenderObj = serviceLocator.getService('dialogRenderObject');
            this.addEventListener();
            this.docClick = this.onDocumentClick.bind(this);
            this.isDestroyed = false;
        }
        Audio.prototype.addEventListener = function () {
            Iif (this.parent.isDestroyed) {
                return;
            }
            this.parent.on(events.keyDown, this.onKeyDown, this);
            this.parent.on(events.keyUp, this.onKeyUp, this);
            this.parent.on(events.insertAudio, this.insertingAudio, this);
            this.parent.on(events.initialEnd, this.afterRender, this);
            this.parent.on(events.dynamicModule, this.afterRender, this);
            this.parent.on(events.showAudioDialog, this.showDialog, this);
            this.parent.on(events.closeAudioDialog, this.closeDialog, this);
            this.parent.on(events.audioToolbarAction, this.onToolbarAction, this);
            this.parent.on(events.dropDownSelect, this.alignmentSelect, this);
            this.parent.on(events.audioDelete, this.deleteAudio, this);
            this.parent.on(events.editAreaClick, this.editAreaClickHandler, this);
            this.parent.on(events.insertCompleted, this.showAudioQuickToolbar, this);
            this.parent.on(events.destroy, this.destroy, this);
            this.parent.on(events.iframeMouseDown, this.closeDialog, this);
        };
        Audio.prototype.removeEventListener = function () {
            this.parent.off(events.keyDown, this.onKeyDown);
            this.parent.off(events.keyUp, this.onKeyUp);
            this.parent.off(events.insertAudio, this.insertingAudio);
            this.parent.off(events.initialEnd, this.afterRender);
            this.parent.off(events.dynamicModule, this.afterRender);
            this.parent.off(events.showAudioDialog, this.showDialog);
            this.parent.off(events.closeAudioDialog, this.closeDialog);
            this.parent.off(events.audioToolbarAction, this.onToolbarAction);
            this.parent.off(events.dropDownSelect, this.alignmentSelect);
            this.parent.off(events.audioDelete, this.deleteAudio);
            this.parent.off(events.editAreaClick, this.editAreaClickHandler);
            this.parent.off(events.insertCompleted, this.showAudioQuickToolbar);
            this.parent.off(events.destroy, this.destroy);
            this.parent.off(events.iframeMouseDown, this.closeDialog);
            Eif (!ej2_base_1.isNullOrUndefined(this.contentModule)) {
                ej2_base_1.EventHandler.remove(this.parent.contentModule.getEditPanel(), ej2_base_2.Browser.touchStartEvent, this.touchStart);
                ej2_base_1.EventHandler.remove(this.contentModule.getEditPanel(), ej2_base_2.Browser.touchEndEvent, this.audioClick);
                this.parent.element.ownerDocument.removeEventListener('mousedown', this.docClick);
                this.docClick = null;
            }
        };
        Audio.prototype.afterRender = function () {
            this.contentModule = this.rendererFactory.getRenderer(enum_1.RenderType.Content);
            ej2_base_1.EventHandler.add(this.parent.contentModule.getEditPanel(), ej2_base_2.Browser.touchStartEvent, this.touchStart, this);
            ej2_base_1.EventHandler.add(this.contentModule.getEditPanel(), ej2_base_2.Browser.touchEndEvent, this.audioClick, this);
            this.parent.element.ownerDocument.addEventListener('mousedown', this.docClick);
        };
        Audio.prototype.checkAudioBack = function (range) {
            if (range.startContainer.nodeName === '#text' && range.startOffset === 0 &&
                !ej2_base_2.isNullOrUndefined(range.startContainer.previousSibling) && this.isAudioElem(range.startContainer.previousSibling)) {
                this.deletedAudio.push(range.startContainer.previousSibling);
            }
            else Iif (range.startContainer.nodeName !== '#text' && !ej2_base_2.isNullOrUndefined(range.startContainer.childNodes[range.startOffset - 1]) &&
                this.isAudioElem(range.startContainer.childNodes[range.startOffset - 1])) {
                this.deletedAudio.push(range.startContainer.childNodes[range.startOffset - 1]);
            }
        };
        Audio.prototype.checkAudioDel = function (range) {
            Iif (range.startContainer.nodeName === '#text' && range.startOffset === range.startContainer.textContent.length &&
                !ej2_base_2.isNullOrUndefined(range.startContainer.nextSibling) && range.startContainer.nextSibling.nodeName === 'AUDIO') {
                this.deletedAudio.push(range.startContainer.nextSibling);
            }
            else Iif (range.startContainer.nodeName !== '#text' && !ej2_base_2.isNullOrUndefined(range.startContainer.childNodes[range.startOffset]) &&
                this.isAudioElem(range.startContainer.childNodes[range.startOffset])) {
                this.deletedAudio.push(range.startContainer.childNodes[range.startOffset]);
            }
        };
        Audio.prototype.undoStack = function (args) {
            Eif ((args.subCommand.toLowerCase() === 'undo' || args.subCommand.toLowerCase() === 'redo') && this.parent.editorMode === 'HTML') {
                for (var i = 0; i < this.parent.formatter.getUndoRedoStack().length; i++) {
                    var temp = this.parent.createElement('div');
                    var contentElem = this.parent.formatter.getUndoRedoStack()[i].text;
                    temp.appendChild(contentElem.cloneNode(true));
                }
            }
        };
        Audio.prototype.touchStart = function (e, ele) {
            Iif (this.parent.readonly) {
                return;
            }
            this.prevSelectedAudEle = this.audEle;
        };
        Audio.prototype.onToolbarAction = function (args) {
            if (util_2.isIDevice()) {
                this.parent.notify(events.selectionRestore, {});
            }
            var item = args.args.item;
            switch (item.subCommand) {
                case 'AudioReplace':
                    this.parent.notify(events.insertAudio, args);
                    break;
                case 'AudioRemove':
                    this.parent.notify(events.audioDelete, args);
                    break;
            }
        };
        Audio.prototype.onKeyUp = function (event) {
            if (!ej2_base_2.isNullOrUndefined(this.deletedAudio) && this.deletedAudio.length > 0) {
                for (var i = 0; i < this.deletedAudio.length; i++) {
                    var args = {
                        element: this.deletedAudio[i].querySelector('audio'),
                        src: this.deletedAudio[i].querySelector('source').getAttribute('src')
                    };
                    this.parent.trigger(events.afterMediaDelete, args);
                }
            }
        };
        Audio.prototype.onKeyDown = function (event) {
            var originalEvent = event.args;
            var range;
            var save;
            var selectNodeEle;
            var selectParentEle;
            this.deletedAudio = [];
            var isCursor;
            var keyCodeValues = [27, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123,
                44, 45, 9, 16, 17, 18, 19, 20, 33, 34, 35, 36, 37, 38, 39, 40, 91, 92, 93, 144, 145, 182, 183];
            if (this.parent.editorMode === 'HTML') {
                range = this.parent.formatter.editorManager.nodeSelection.getRange(this.parent.contentModule.getDocument());
                isCursor = range.startContainer === range.endContainer && range.startOffset === range.endOffset;
            }
            if (!isCursor && this.parent.editorMode === 'HTML' && keyCodeValues.indexOf(originalEvent.which) < 0) {
                var nodes = this.parent.formatter.editorManager.nodeSelection.getNodeCollection(range);
                for (var i = 0; i < nodes.length; i++) {
                    if (this.isAudioElem(nodes[i])) {
                        this.deletedAudio.push(nodes[i]);
                    }
                }
            }
            if (this.parent.editorMode === 'HTML' && ((originalEvent.which === 8 && originalEvent.code === 'Backspace') ||
                (originalEvent.which === 46 && originalEvent.code === 'Delete'))) {
                var isCursor_1 = range.startContainer === range.endContainer && range.startOffset === range.endOffset;
                if ((originalEvent.which === 8 && originalEvent.code === 'Backspace' && isCursor_1)) {
                    this.checkAudioBack(range);
                }
                else if ((originalEvent.which === 46 && originalEvent.code === 'Delete' && isCursor_1)) {
                    this.checkAudioDel(range);
                }
            }
            if (!ej2_base_1.isNullOrUndefined(this.parent.formatter.editorManager.nodeSelection) &&
                originalEvent.code !== 'KeyK') {
                range = this.parent.formatter.editorManager.nodeSelection.getRange(this.parent.contentModule.getDocument());
                selectNodeEle = this.parent.formatter.editorManager.nodeSelection.getNodeCollection(range);
                selectParentEle = this.parent.formatter.editorManager.nodeSelection.getParentNodeCollection(range);
                if (!originalEvent.ctrlKey && originalEvent.key && (originalEvent.key.length === 1 || originalEvent.action === 'enter') &&
                    (this.isAudioElem(selectParentEle[0])) && selectParentEle[0].parentElement) {
                    var prev = selectParentEle[0].parentElement.childNodes[0];
                    this.parent.formatter.editorManager.nodeSelection.setSelectionText(this.contentModule.getDocument(), prev, prev, prev.textContent.length, prev.textContent.length);
                    ej2_base_2.removeClass([selectParentEle[0]], classes.CLS_AUD_FOCUS);
                    this.quickToolObj.audioQTBar.hidePopup();
                }
            }
            if (originalEvent.ctrlKey && (originalEvent.keyCode === 89 || originalEvent.keyCode === 90)) {
                this.undoStack({ subCommand: (originalEvent.keyCode === 90 ? 'undo' : 'redo') });
            }
            if (originalEvent.keyCode === 8 || originalEvent.keyCode === 46) {
                Iif (selectNodeEle && this.isAudioElem(selectNodeEle[0]) && selectNodeEle.length < 1) {
                    if (!ej2_base_1.isNullOrUndefined(this.parent.formatter.editorManager.nodeSelection)) {
                        save = this.parent.formatter.editorManager.nodeSelection.save(range, this.parent.contentModule.getDocument());
                    }
                    originalEvent.preventDefault();
                    var event_1 = {
                        selectNode: selectNodeEle, selection: save, selectParent: selectParentEle,
                        args: {
                            item: { command: 'Audios', subCommand: 'AudioRemove' },
                            originalEvent: originalEvent
                        }
                    };
                    this.deleteAudio(event_1, originalEvent.keyCode);
                }
            }
            switch (originalEvent.action) {
                case 'escape':
                    if (!ej2_base_1.isNullOrUndefined(this.dialogObj)) {
                        this.dialogObj.close();
                    }
                    break;
                case 'backspace':
                case 'delete':
                    for (var i = 0; i < this.deletedAudio.length; i++) {
                        var src = this.deletedAudio[i].src;
                        this.audioRemovePost(src);
                    }
                    Eif (this.parent.editorMode !== 'Markdown') {
                        if (range.startContainer.nodeType === 3) {
                            if (originalEvent.code === 'Backspace') {
                                if (range.startContainer.previousElementSibling && range.startOffset === 0 &&
                                    range.startContainer.previousElementSibling.classList.contains(classes.CLS_AUDIOWRAP)) {
                                    ej2_base_1.detach(range.startContainer.previousElementSibling);
                                }
                            }
                            else {
                                if (range.startContainer.nextElementSibling &&
                                    range.endContainer.textContent.length === range.endOffset &&
                                    range.startContainer.nextElementSibling.classList.contains(classes.CLS_AUDIOWRAP)) {
                                    ej2_base_1.detach(range.startContainer.nextElementSibling);
                                }
                            }
                        }
                        else if (range.startContainer.nodeType === 1 && (range.startContainer.classList &&
                            (range.startContainer.classList.contains(classes.CLS_AUDIOWRAP) ||
                                range.startContainer.classList.contains(classes.CLS_CLICKELEM) ||
                                range.startContainer.classList.contains(classes.CLS_VID_CLICK_ELEM)))) {
                            ej2_base_1.detach(range.startContainer);
                        }
                    }
                    break;
                case 'insert-audio':
                    Eif (!ej2_base_1.isNullOrUndefined(this.parent.formatter.editorManager.nodeSelection)) {
                        save = this.parent.formatter.editorManager.nodeSelection.save(range, this.parent.contentModule.getDocument());
                    }
                    this.openDialog(true, originalEvent, save, selectNodeEle, selectParentEle);
                    originalEvent.preventDefault();
                    break;
            }
            if (originalEvent.ctrlKey && originalEvent.key === 'a') {
                this.handleSelectAll();
            }
        };
        Audio.prototype.handleSelectAll = function () {
            var audioFocusNodes = this.parent.inputElement.querySelectorAll('.' + classes.CLS_AUD_FOCUS);
            ej2_base_2.removeClass(audioFocusNodes, classes.CLS_AUD_FOCUS);
        };
        Audio.prototype.openDialog = function (isInternal, event, selection, ele, parentEle) {
            var range;
            var save;
            var selectNodeEle;
            var selectParentEle;
            if (!isInternal && !ej2_base_2.isNullOrUndefined(this.parent.formatter.editorManager.nodeSelection)) {
                range = this.parent.formatter.editorManager.nodeSelection.getRange(this.parent.contentModule.getDocument());
                save = this.parent.formatter.editorManager.nodeSelection.save(range, this.parent.contentModule.getDocument());
                selectNodeEle = this.parent.formatter.editorManager.nodeSelection.getNodeCollection(range);
                selectParentEle = this.parent.formatter.editorManager.nodeSelection.getParentNodeCollection(range);
            }
            else {
                save = selection;
                selectNodeEle = ele;
                selectParentEle = parentEle;
            }
            Eif (this.parent.editorMode === 'HTML') {
                this.insertAudio({
                    args: {
                        item: { command: 'Audios', subCommand: 'Audio' },
                        originalEvent: event,
                        name: !isInternal ? 'showDialog' : null
                    },
                    selectNode: selectNodeEle,
                    selection: save,
                    selectParent: selectParentEle
                });
            }
        };
        Audio.prototype.showDialog = function () {
            this.openDialog(false);
        };
        Audio.prototype.closeDialog = function () {
            if (this.dialogObj) {
                this.dialogObj.hide({ returnValue: true });
            }
        };
        Audio.prototype.deleteAudio = function (e, keyCode) {
            if (!this.isAudioElem(e.selectNode[0])) {
                return;
            }
            if (this.audEle) {
                Iif (e.selectNode[0].nodeType === 3) {
                    e.selectNode[0] = this.audEle;
                }
                else Eif (this.isAudioElem(e.selectNode[0])) {
                    e.selectNode[0] = e.selectNode[0].classList.contains(classes.CLS_AUDIOWRAP) ? e.selectNode[0] :
                        e.selectNode[0].parentElement;
                }
            }
            var args = {
                element: e.selectNode[0].querySelector('audio'),
                src: e.selectNode[0].querySelector('source').getAttribute('src')
            };
            Eif (this.parent.formatter.getUndoRedoStack().length === 0) {
                this.parent.formatter.saveData();
            }
            e.selection.restore();
            this.parent.formatter.process(this.parent, e.args, e.args.originalEvent, {
                selectNode: e.selectNode,
                subCommand: e.args.item.subCommand
            });
            this.audioRemovePost(args.src);
            if (this.quickToolObj && document.body.contains(this.quickToolObj.audioQTBar.element)) {
                this.quickToolObj.audioQTBar.hidePopup();
            }
            Eif (ej2_base_1.isNullOrUndefined(keyCode)) {
                this.parent.trigger(events.afterMediaDelete, args);
            }
        };
        Audio.prototype.audioRemovePost = function (src) {
            var proxy = this;
            var absoluteUrl = '';
            Eif (ej2_base_2.isNullOrUndefined(this.parent.insertAudioSettings.removeUrl) || this.parent.insertAudioSettings.removeUrl === '') {
                return;
            }
            if (src.indexOf('http://') > -1 || src.indexOf('https://') > -1) {
                absoluteUrl = src;
            }
            else {
                absoluteUrl = new URL(src, document.baseURI).href;
            }
            this.removingAudioName = absoluteUrl.replace(/^.*[\\/]/, '');
            var xhr = new XMLHttpRequest();
            xhr.addEventListener('readystatechange', function () {
                if (this.readyState === 4 && this.status === 200) {
                    proxy.triggerPost(this.response);
                }
            });
            xhr.open('GET', absoluteUrl);
            xhr.responseType = 'blob';
            xhr.send();
        };
        Audio.prototype.triggerPost = function (response) {
            var removeUrl = this.parent.insertAudioSettings.removeUrl;
            if (ej2_base_2.isNullOrUndefined(removeUrl) || removeUrl === '') {
                return;
            }
            var file = new File([response], this.removingAudioName);
            var ajax = new ej2_base_1.Ajax(removeUrl, 'POST', true, null);
            var formData = new FormData();
            formData.append('UploadFiles', file);
            ajax.send(formData);
        };
        Audio.prototype.audioClick = function (e) {
            if (ej2_base_2.Browser.isDevice) {
                if (this.isAudioElem(e.target)) {
                    this.contentModule.getEditPanel().setAttribute('contenteditable', 'false');
                    e.target.focus();
                }
                else {
                    if (!this.parent.readonly) {
                        this.contentModule.getEditPanel().setAttribute('contenteditable', 'true');
                    }
                }
            }
            if (this.isAudioElem(e.target)) {
                this.audEle = e.target.querySelector('audio');
                e.preventDefault();
            }
        };
        Audio.prototype.onDocumentClick = function (e) {
            var target = e.target;
            Iif (ej2_base_2.isNullOrUndefined(this.contentModule.getEditPanel())) {
                return;
            }
            if (this.isAudioElem(target)) {
                this.audEle = target.querySelector('audio');
            }
            if (!ej2_base_1.isNullOrUndefined(this.dialogObj) && ((!ej2_base_2.closest(target, '[id=' + "'" + this.dialogObj.element.id + "'" + ']') && this.parent.toolbarSettings.enable && this.parent.getToolbarElement() &&
                !this.parent.getToolbarElement().contains(e.target)) ||
                (this.parent.getToolbarElement() && this.parent.getToolbarElement().contains(e.target) &&
                    !ej2_base_2.closest(target, '#' + this.parent.getID() + '_toolbar_Audio') &&
                    !target.querySelector('#' + this.parent.getID() + '_toolbar_Audio')))) {
                Iif (e.offsetX > e.target.clientWidth || e.offsetY > e.target.clientHeight) {
                }
                else {
                    this.parent.notify(events.documentClickClosedBy, { closedBy: "outside click" });
                    this.dialogObj.hide({ returnValue: true });
                    this.parent.isBlur = true;
                    util_1.dispatchEvent(this.parent.element, 'focusout');
                }
            }
            if (this.contentModule.getEditPanel().querySelector('.' + classes.CLS_AUD_FOCUS)) {
                if (!this.isAudioElem(e.target) && !ej2_base_2.isNullOrUndefined(this.audEle)) {
                    this.audEle.style.outline = '';
                }
                else if (!ej2_base_2.isNullOrUndefined(this.prevSelectedAudEle) && this.prevSelectedAudEle !== target) {
                    this.prevSelectedAudEle.style.outline = '';
                }
            }
            if (this.parent.inlineMode.enable && target && this.dialogObj && !ej2_base_2.closest(target, '#' + this.dialogObj.element.id)) {
                this.dialogObj.hide();
            }
        };
        Audio.prototype.alignmentSelect = function (e) {
            var item = e.item;
            if (!document.body.contains(document.body.querySelector('.e-rte-quick-toolbar')) || item.command !== 'Audios') {
                return;
            }
            var range = this.parent.formatter.editorManager.nodeSelection.getRange(this.parent.contentModule.getDocument());
            var selectNodeEle = this.parent.formatter.editorManager.nodeSelection.getNodeCollection(range);
            selectNodeEle = [this.audEle];
            var args = { args: e, selectNode: selectNodeEle };
            Eif (this.parent.formatter.getUndoRedoStack().length === 0) {
                this.parent.formatter.saveData();
            }
            switch (item.subCommand) {
                case 'Inline':
                    this.inline(args);
                    break;
                case 'Break':
                    this.break(args);
                    break;
            }
            if (this.quickToolObj && document.body.contains(this.quickToolObj.audioQTBar.element)) {
                this.quickToolObj.audioQTBar.hidePopup();
                ej2_base_2.removeClass([selectNodeEle[0]], classes.CLS_AUD_FOCUS);
            }
        };
        Audio.prototype.break = function (e) {
            Iif (e.selectNode[0].nodeName !== 'AUDIO') {
                return;
            }
            var subCommand = (e.args.item && e.args.item.subCommand) ?
                e.args.item.subCommand : 'Break';
            this.parent.formatter.process(this.parent, e.args, e.args.originalEvent, { selectNode: e.selectNode, subCommand: subCommand });
        };
        Audio.prototype.inline = function (e) {
            Iif (e.selectNode[0].nodeName !== 'AUDIO') {
                return;
            }
            var subCommand = (e.args.item && e.args.item.subCommand) ?
                e.args.item.subCommand : 'Inline';
            this.parent.formatter.process(this.parent, e.args, e.args.originalEvent, { selectNode: e.selectNode, subCommand: subCommand });
        };
        Audio.prototype.editAreaClickHandler = function (e) {
            Iif (this.parent.readonly) {
                this.hideAudioQuickToolbar();
                return;
            }
            var args = e.args;
            var showOnRightClick = this.parent.quickToolbarSettings.showOnRightClick;
            if (args.which === 2 || (showOnRightClick && args.which === 1) || (!showOnRightClick && args.which === 3)) {
                if ((showOnRightClick && args.which === 1) && !ej2_base_1.isNullOrUndefined(args.target) &&
                    this.isAudioElem(args.target)) {
                    this.parent.formatter.editorManager.nodeSelection.Clear(this.contentModule.getDocument());
                    this.parent.formatter.editorManager.nodeSelection.setSelectionContents(this.contentModule.getDocument(), args.target);
                }
                return;
            }
            if (this.parent.editorMode === 'HTML' && this.parent.quickToolbarModule && this.parent.quickToolbarModule.audioQTBar) {
                this.quickToolObj = this.parent.quickToolbarModule;
                var target = args.target;
                this.contentModule = this.rendererFactory.getRenderer(enum_1.RenderType.Content);
                var isPopupOpen = this.quickToolObj.audioQTBar.element.classList.contains('e-rte-pop');
                if (this.isAudioElem(target) && this.parent.quickToolbarModule) {
                    Iif (isPopupOpen) {
                        return;
                    }
                    this.parent.formatter.editorManager.nodeSelection.Clear(this.contentModule.getDocument());
                    this.parent.formatter.editorManager.nodeSelection.setSelectionContents(this.contentModule.getDocument(), target);
                    if (util_2.isIDevice()) {
                        this.parent.notify(events.selectionSave, e);
                    }
                    target.querySelector('audio').style.outline = '2px solid #4a90e2';
                    this.showAudioQuickToolbar({ args: args, type: 'Audios', elements: [args.target] });
                }
                else {
                    this.hideAudioQuickToolbar();
                }
            }
        };
        Audio.prototype.isAudioElem = function (target) {
            if (target && target.nodeType !== 3 && target.nodeName !== 'BR' && (target.classList &&
                (target.classList.contains(classes.CLS_AUDIOWRAP) || target.classList.contains(classes.CLS_CLICKELEM)))) {
                return true;
            }
            else {
                return false;
            }
        };
        Audio.prototype.showAudioQuickToolbar = function (e) {
            var _this = this;
            if (e.type !== 'Audios' || ej2_base_1.isNullOrUndefined(this.parent.quickToolbarModule)
                || ej2_base_1.isNullOrUndefined(this.parent.quickToolbarModule.audioQTBar) || ej2_base_1.isNullOrUndefined(e.args)) {
                return;
            }
            this.quickToolObj = this.parent.quickToolbarModule;
            var args = e.args;
            var target = e.elements;
            [].forEach.call(e.elements, function (element, index) {
                Eif (index === 0) {
                    target = element;
                }
            });
            Eif (this.isAudioElem(target)) {
                ej2_base_1.addClass([target.querySelector('audio')], [classes.CLS_AUD_FOCUS]);
                target.querySelector('audio').style.outline = '2px solid #4a90e2';
            }
            Eif (this.parent.quickToolbarModule.audioQTBar) {
                Iif (e.isNotify) {
                    this.showPopupTime = setTimeout(function () {
                        _this.parent.formatter.editorManager.nodeSelection.Clear(_this.contentModule.getDocument());
                        _this.parent.formatter.editorManager.nodeSelection.setSelectionContents(_this.contentModule.getDocument(), target);
                        _this.quickToolObj.audioQTBar.showPopup(args.pageX - 50, target.getBoundingClientRect().top + 34, target);
                    }, 400);
                }
                else {
                    this.quickToolObj.audioQTBar.showPopup(args.pageX - 50, target.getBoundingClientRect().top + 34, target);
                }
            }
        };
        Audio.prototype.hideAudioQuickToolbar = function () {
            if (!ej2_base_1.isNullOrUndefined(this.contentModule.getEditPanel().querySelector('.' + classes.CLS_AUD_FOCUS))) {
                ej2_base_2.removeClass([this.contentModule.getEditPanel().querySelector('.' + classes.CLS_AUD_FOCUS)], classes.CLS_AUD_FOCUS);
                Eif (!ej2_base_2.isNullOrUndefined(this.audEle)) {
                    this.audEle.style.outline = '';
                }
                Eif (this.quickToolObj && this.quickToolObj.audioQTBar && document.body.contains(this.quickToolObj.audioQTBar.element)) {
                    this.quickToolObj.audioQTBar.hidePopup();
                }
            }
        };
        Audio.prototype.insertingAudio = function (e) {
            this.insertAudio(e);
            if (!ej2_base_1.isNullOrUndefined(this.dialogObj)) {
                this.dialogObj.element.style.maxHeight = 'inherit';
                var dialogContent = this.dialogObj.element.querySelector('.e-audio-content');
                Eif (!ej2_base_1.isNullOrUndefined(this.parent.insertAudioSettings.path) || this.parent.editorMode === 'HTML') {
                    document.getElementById(this.rteID + '_insertAudio').focus();
                }
                else {
                    dialogContent.querySelector('.e-audio-url').focus();
                }
            }
        };
        Audio.prototype.clearDialogObj = function () {
            if (this.uploadObj && !this.uploadObj.isDestroyed) {
                this.uploadObj.destroy();
                ej2_base_1.detach(this.uploadObj.element);
                this.uploadObj = null;
            }
            if (this.button && !this.button.isDestroyed) {
                this.button.destroy();
                ej2_base_1.detach(this.button.element);
                this.button = null;
            }
            if (this.dialogObj && !this.dialogObj.isDestroyed) {
                this.dialogObj.destroy();
                ej2_base_1.detach(this.dialogObj.element);
                this.dialogObj = null;
            }
        };
        Audio.prototype.insertAudio = function (e) {
            var _this = this;
            if (this.dialogObj) {
                this.dialogObj.hide({ returnValue: true });
                return;
            }
            var audioDialog = this.parent.createElement('div', { className: 'e-rte-audio-dialog', id: this.rteID + '_audio' });
            this.parent.rootContainer.appendChild(audioDialog);
            var audioInsert = this.i10n.getConstant('dialogInsert');
            var audiolinkCancel = this.i10n.getConstant('dialogCancel');
            var audioHeader = this.i10n.getConstant('audioHeader');
            var selection = e.selection;
            var selectObj = { selfAudio: this, selection: e.selection, args: e.args, selectParent: e.selectParent };
            var dialogModel = {
                header: audioHeader,
                cssClass: classes.CLS_RTE_ELEMENTS,
                enableRtl: this.parent.enableRtl,
                locale: this.parent.locale,
                showCloseIcon: true, closeOnEscape: true, width: (ej2_base_2.Browser.isDevice) ? '290px' : '340px',
                isModal: ej2_base_2.Browser.isDevice,
                buttons: [{
                        click: this.insertAudioUrl.bind(selectObj),
                        buttonModel: { content: audioInsert, cssClass: 'e-flat e-insertAudio', isPrimary: true, disabled: true }
                    },
                    {
                        click: function (e) {
                            _this.cancelDialog(e);
                        },
                        buttonModel: { cssClass: 'e-flat e-cancel', content: audiolinkCancel }
                    }],
                target: (ej2_base_2.Browser.isDevice) ? document.body : this.parent.element,
                animationSettings: { effect: 'None' },
                close: function (event) {
                    if (_this.isAudioUploaded) {
                        Iif (_this.dialogObj.element.querySelector('.e-file-abort-btn')) {
                            _this.dialogObj.element.querySelector('.e-file-abort-btn').click();
                        }
                        else {
                            _this.uploadObj.remove();
                        }
                    }
                    _this.parent.isBlur = false;
                    if (event && !ej2_base_2.isNullOrUndefined(event.event) && event.event.returnValue) {
                        Eif (_this.parent.editorMode === 'HTML') {
                            selection.restore();
                        }
                    }
                    _this.clearDialogObj();
                    _this.dialogRenderObj.close(event);
                }
            };
            var dialogContent = this.parent.createElement('div', { className: 'e-audio-content' });
            Eif (!ej2_base_1.isNullOrUndefined(this.parent.insertAudioSettings.path) || this.parent.editorMode === 'HTML') {
                dialogContent.appendChild(this.audioUpload(e));
            }
            var linkHeader = this.parent.createElement('div', { className: 'e-audioheader' });
            var linkHeaderText = this.i10n.getConstant('audioLinkHeader');
            Eif (this.parent.editorMode === 'HTML') {
                linkHeader.innerHTML = linkHeaderText;
            }
            dialogContent.appendChild(linkHeader);
            dialogContent.appendChild(this.audioUrlPopup(e));
            if (e.selectNode && e.selectNode[0].nodeType === 1 && this.isAudioElem(e.selectNode[0])) {
                dialogModel.header = this.parent.localeObj.getConstant('editAudioHeader');
                dialogModel.content = dialogContent;
            }
            else {
                dialogModel.content = dialogContent;
            }
            this.dialogObj = this.dialogRenderObj.render(dialogModel);
            this.dialogObj.createElement = this.parent.createElement;
            this.dialogObj.appendTo(audioDialog);
            if (e.selectNode && e.selectNode[0].nodeType === 1 && this.isAudioElem(e.selectNode[0]) && (e.name === 'insertAudio')) {
                this.dialogObj.element.querySelector('.e-insertAudio').textContent = this.parent.localeObj.getConstant('dialogUpdate');
            }
            audioDialog.style.maxHeight = 'inherit';
            if (this.quickToolObj) {
                if (this.quickToolObj.audioQTBar && document.body.contains(this.quickToolObj.audioQTBar.element)) {
                    this.quickToolObj.audioQTBar.hidePopup();
                }
                Iif (this.quickToolObj.inlineQTBar && document.body.contains(this.quickToolObj.inlineQTBar.element)) {
                    this.quickToolObj.inlineQTBar.hidePopup();
                }
                if (this.quickToolObj.textQTBar && this.parent.element.ownerDocument.body.contains(this.quickToolObj.textQTBar.element)) {
                    this.quickToolObj.textQTBar.hidePopup();
                }
            }
        };
        Audio.prototype.audioUrlPopup = function (e) {
            var _this = this;
            var audioUrl = this.parent.createElement('div', { className: 'audioUrl' });
            var placeUrl = this.i10n.getConstant('audioUrl');
            this.inputUrl = this.parent.createElement('input', {
                className: 'e-input e-audio-url',
                attrs: { placeholder: placeUrl, spellcheck: 'false', 'aria-label': this.i10n.getConstant('audioLinkHeader') }
            });
            this.inputUrl.addEventListener('input', function () {
                Eif (!ej2_base_2.isNullOrUndefined(_this.inputUrl)) {
                    if (_this.inputUrl.value.length === 0) {
                        _this.dialogObj.getButtons(0).element.disabled = true;
                    }
                    else {
                        _this.dialogObj.getButtons(0).element.removeAttribute('disabled');
                    }
                }
            });
            if (e.selectNode && this.isAudioElem(e.selectNode[0])) {
                var regex = new RegExp(/([^\S]|^)(((https?:\/\/)|(www\.))(\S+))/gi);
                var sourceElement = e.selectNode[0].querySelector('source');
                this.inputUrl.value = sourceElement.src.match(regex) ? sourceElement.src : '';
            }
            audioUrl.appendChild(this.inputUrl);
            return audioUrl;
        };
        Audio.prototype.audioUpload = function (e) {
            var _this = this;
            var save;
            var selectParent;
            var proxy = this;
            var iframe = proxy.parent.iframeSettings.enable;
            if (proxy.parent.editorMode === 'HTML' && (!iframe && ej2_base_1.isNullOrUndefined(ej2_base_2.closest(e.selection.range.startContainer.parentNode, '[id='
                + "'" + this.parent.contentModule.getPanel().id + "'" + ']'))
                || (iframe && !util_1.hasClass(e.selection.range.startContainer.parentNode.ownerDocument.querySelector('body'), 'e-lib')))) {
                this.contentModule.getEditPanel().focus();
                var range = this.parent.formatter.editorManager.nodeSelection.getRange(this.parent.contentModule.getDocument());
                save = this.parent.formatter.editorManager.nodeSelection.save(range, this.parent.contentModule.getDocument());
                selectParent = this.parent.formatter.editorManager.nodeSelection.getParentNodeCollection(range);
            }
            else {
                save = e.selection;
                selectParent = e.selectParent;
            }
            var uploadParentEle = this.parent.createElement('div', { className: 'e-aud-uploadwrap e-droparea' });
            var deviceAudioUpMsg = this.i10n.getConstant('audioDeviceUploadMessage');
            var audioUpMsg = this.i10n.getConstant('audioUploadMessage');
            var span = this.parent.createElement('span', { className: 'e-droptext' });
            var spanMsg = this.parent.createElement('span', {
                className: 'e-rte-upload-text', innerHTML: ((ej2_base_2.Browser.isDevice) ? deviceAudioUpMsg : audioUpMsg)
            });
            span.appendChild(spanMsg);
            var btnEle = this.parent.createElement('button', {
                className: 'e-browsebtn', id: this.rteID + '_insertAudio', attrs: { autofocus: 'true', type: 'button' }
            });
            span.appendChild(btnEle);
            uploadParentEle.appendChild(span);
            var browserMsg = this.i10n.getConstant('browse');
            this.button = new ej2_buttons_1.Button({ content: browserMsg, enableRtl: this.parent.enableRtl });
            this.button.isStringTemplate = true;
            this.button.createElement = this.parent.createElement;
            this.button.appendTo(btnEle);
            var btnClick = (ej2_base_2.Browser.isDevice) ? span : btnEle;
            ej2_base_1.EventHandler.add(btnClick, 'click', this.fileSelect, this);
            var uploadEle = this.parent.createElement('input', {
                id: this.rteID + '_upload', attrs: { type: 'File', name: 'UploadFiles' }
            });
            uploadParentEle.appendChild(uploadEle);
            var fileName;
            var selectArgs;
            var filesData;
            this.uploadObj = new ej2_inputs_1.Uploader({
                asyncSettings: { saveUrl: this.parent.insertAudioSettings.saveUrl, removeUrl: this.parent.insertAudioSettings.removeUrl },
                dropArea: span, multiple: false, enableRtl: this.parent.enableRtl,
                allowedExtensions: this.parent.insertAudioSettings.allowedTypes.toString(),
                selected: function (e) {
                    proxy.isAudioUploaded = true;
                    selectArgs = e;
                    filesData = e.filesData;
                    _this.parent.trigger(events.fileSelected, selectArgs, function (selectArgs) {
                        if (!selectArgs.cancel) {
                            _this.checkExtension(selectArgs.filesData[0]);
                            fileName = selectArgs.filesData[0].name;
                            if (_this.parent.editorMode === 'HTML' && ej2_base_1.isNullOrUndefined(_this.parent.insertAudioSettings.path)) {
                                var reader_1 = new FileReader();
                                reader_1.addEventListener('load', function (e) {
                                    var url = _this.parent.insertAudioSettings.saveFormat === 'Base64' ? reader_1.result :
                                        URL.createObjectURL(util_1.convertToBlob(reader_1.result));
                                    proxy.uploadUrl = {
                                        url: url, selection: save, fileName: fileName,
                                        selectParent: selectParent
                                    };
                                    proxy.inputUrl.setAttribute('disabled', 'true');
                                    if (ej2_base_1.isNullOrUndefined(proxy.parent.insertAudioSettings.saveUrl) && _this.isAllowedTypes
                                        && !ej2_base_1.isNullOrUndefined(_this.dialogObj)) {
                                        _this.dialogObj.getButtons(0).element.removeAttribute('disabled');
                                    }
                                });
                                reader_1.readAsDataURL(selectArgs.filesData[0].rawFile);
                            }
                        }
                    });
                },
                beforeUpload: function (args) {
                    _this.parent.trigger(events.beforeFileUpload, args);
                },
                uploading: function (e) {
                    Eif (!_this.parent.isServerRendered) {
                        _this.parent.trigger(events.fileUploading, e);
                    }
                },
                success: function (e) {
                    _this.parent.trigger(events.fileUploadSuccess, e, function (e) {
                        if (!ej2_base_1.isNullOrUndefined(_this.parent.insertAudioSettings.path)) {
                            var url = _this.parent.insertAudioSettings.path + e.file.name;
                            var value = { url: url, selection: save };
                            proxy.uploadUrl = {
                                url: url, selection: save, fileName: fileName, selectParent: selectParent
                            };
                            proxy.inputUrl.setAttribute('disabled', 'true');
                        }
                        if (e.operation === 'upload' && !ej2_base_1.isNullOrUndefined(_this.dialogObj)) {
                            _this.dialogObj.getButtons(0).element.removeAttribute('disabled');
                        }
                    });
                },
                failure: function (e) {
                    _this.parent.trigger(events.fileUploadFailed, e);
                },
                removing: function () {
                    _this.parent.trigger(events.fileRemoving, e, function (e) {
                        proxy.isAudioUploaded = false;
                        _this.dialogObj.getButtons(0).element.disabled = true;
                        proxy.inputUrl.removeAttribute('disabled');
                        Eif (proxy.uploadUrl) {
                            proxy.uploadUrl.url = '';
                        }
                    });
                }
            });
            this.uploadObj.isStringTemplate = true;
            this.uploadObj.createElement = this.parent.createElement;
            this.uploadObj.appendTo(uploadEle);
            return uploadParentEle;
        };
        Audio.prototype.checkExtension = function (e) {
            Eif (this.uploadObj.allowedExtensions) {
                if (this.uploadObj.allowedExtensions.toLocaleLowerCase().indexOf(('.' + e.type).toLocaleLowerCase()) === -1) {
                    this.dialogObj.getButtons(0).element.setAttribute('disabled', 'disabled');
                    this.isAllowedTypes = false;
                }
                else {
                    this.isAllowedTypes = true;
                }
            }
        };
        Audio.prototype.fileSelect = function () {
            this.dialogObj.element.getElementsByClassName('e-file-select-wrap')[0].querySelector('button').click();
            return false;
        };
        Audio.prototype.cancelDialog = function (e) {
            this.parent.isBlur = false;
            this.dialogObj.hide({ returnValue: true });
            Iif (this.isAudioUploaded) {
                this.uploadObj.removing();
            }
        };
        Audio.prototype.insertAudioUrl = function (e) {
            var proxy = this.selfAudio;
            proxy.isAudioUploaded = false;
            var url = proxy.inputUrl.value;
            if (proxy.parent.formatter.getUndoRedoStack().length === 0) {
                proxy.parent.formatter.saveData();
            }
            if (!ej2_base_1.isNullOrUndefined(proxy.uploadUrl) && proxy.uploadUrl.url !== '') {
                proxy.uploadUrl.cssClass = (proxy.parent.insertAudioSettings.layoutOption === 'Inline' ?
                    classes.CLS_AUDIOINLINE : classes.CLS_AUDIOBREAK);
                proxy.dialogObj.hide({ returnValue: false });
                Iif (proxy.dialogObj !== null) {
                    return;
                }
                proxy.parent.formatter.process(proxy.parent, this.args, this.args.originalEvent, proxy.uploadUrl);
                proxy.uploadUrl.url = '';
            }
            else Eif (url !== '') {
                if (proxy.parent.editorMode === 'HTML' && ej2_base_1.isNullOrUndefined(ej2_base_2.closest(this.selection.range.startContainer.parentNode, '[id=' + "'" + proxy.contentModule.getPanel().id + "'" + ']'))) {
                    proxy.contentModule.getEditPanel().focus();
                    var range = proxy.parent.formatter.editorManager.nodeSelection.getRange(proxy.contentModule.getDocument());
                    this.selection = proxy.parent.formatter.editorManager.nodeSelection.save(range, proxy.contentModule.getDocument());
                    this.selectParent = proxy.parent.formatter.editorManager.nodeSelection.getParentNodeCollection(range);
                }
                var name_1 = proxy.parent.editorMode === 'HTML' ? url.split('/')[url.split('/').length - 1] : '';
                var value = {
                    cssClass: (proxy.parent.insertAudioSettings.layoutOption === 'Inline' ? classes.CLS_AUDIOINLINE : classes.CLS_AUDIOBREAK),
                    url: url, selection: this.selection, fileName: name_1,
                    selectParent: this.selectParent
                };
                proxy.dialogObj.hide({ returnValue: false });
                if (proxy.dialogObj !== null) {
                    return;
                }
                proxy.parent.formatter.process(proxy.parent, this.args, this.args.originalEvent, value);
            }
        };
        Audio.prototype.destroy = function () {
            if (this.isDestroyed) {
                return;
            }
            this.prevSelectedAudEle = undefined;
            Iif (this.showPopupTime) {
                clearTimeout(this.showPopupTime);
                this.showPopupTime = null;
            }
            this.removeEventListener();
            this.clearDialogObj();
            this.isDestroyed = true;
        };
        Audio.prototype.getModuleName = function () {
            return 'audio';
        };
        return Audio;
    }());
    exports.Audio = Audio;
});