all files / rich-text-editor/actions/ count.js

100% Statements 86/86
91.11% Branches 41/45
100% Functions 13/13
100% Lines 86/86
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   63× 63× 63× 63× 63×   63× 63× 63×   63× 63× 63× 63× 63×   63× 63× 45×     144×   144× 15×     129×   144× 144×   87× 87× 52× 52×   35×   34× 34× 34×     144× 81× 81× 42×       121× 58×   63× 63×   63×   63× 62×   63× 63× 63×   63× 63× 63×   63×     126× 126× 126× 126× 126× 126× 126× 126×   63× 63× 63× 63× 63× 63× 63× 63×   32470×        
define(["require", "exports", "@syncfusion/ej2-base", "../base/constant", "../base/enum", "../base/classes"], function (require, exports, ej2_base_1, events, enum_1, classes_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var Count = (function () {
        function Count(parent, serviceLocator) {
            this.parent = parent;
            this.locator = serviceLocator;
            this.renderFactory = this.locator.getService('rendererFactory');
            this.addEventListener();
            this.isDestroyed = false;
        }
        Count.prototype.initializeInstance = function () {
            this.contentRenderer = this.renderFactory.getRenderer(enum_1.RenderType.Content);
            this.editPanel = this.contentRenderer.getEditPanel();
            this.addEventListener();
        };
        Count.prototype.renderCount = function () {
            this.initializeInstance();
            this.element = this.parent.createElement('span', { className: classes_1.CLS_COUNT });
            this.parent.rootContainer.appendChild(this.element);
            this.parent.rootContainer.classList.add('e-count-enabled');
            if (this.parent.iframeSettings.enable) {
                this.parent.inputElement.classList.add('e-count-enabled');
            }
            this.appendCount();
            if (this.parent.maxLength !== -1) {
                this.charCountBackground(this.htmlLength);
            }
        };
        Count.prototype.appendCount = function () {
            var htmlText = this.parent.editorMode === 'Markdown' ? this.editPanel.value
                : (this.parent.getText().replace(/(\r\n|\n|\r|\t)/gm, ''));
            if (this.parent.editorMode !== 'Markdown' && htmlText.indexOf('\u200B') !== -1) {
                this.htmlLength = htmlText.replace(/\u200B/g, '').length;
            }
            else {
                this.htmlLength = htmlText.length;
            }
            var string = this.parent.maxLength === -1 ? this.htmlLength : this.htmlLength + ' / ' + this.parent.maxLength;
            this.element.innerHTML = string;
        };
        Count.prototype.charCountBackground = function (htmlLength) {
            var percentage = (htmlLength / this.parent.maxLength) * 100;
            if (percentage < 85) {
                this.element.classList.remove(classes_1.CLS_WARNING);
                this.element.classList.remove(classes_1.CLS_ERROR);
            }
            else if (percentage > 85 && percentage <= 90) {
                this.element.classList.remove(classes_1.CLS_ERROR);
                this.element.classList.add(classes_1.CLS_WARNING);
            }
            else Eif (percentage > 90) {
                this.element.classList.remove(classes_1.CLS_WARNING);
                this.element.classList.add(classes_1.CLS_ERROR);
            }
        };
        Count.prototype.refresh = function () {
            if (!ej2_base_1.isNullOrUndefined(this.editPanel)) {
                this.appendCount();
                if (this.parent.maxLength !== -1) {
                    this.charCountBackground(this.htmlLength);
                }
            }
        };
        Count.prototype.destroy = function () {
            if (this.isDestroyed) {
                return;
            }
            Eif (this.parent && this.parent.rootContainer && this.parent.rootContainer.classList.contains('e-count-enabled')) {
                this.parent.rootContainer.classList.remove('e-count-enabled');
            }
            if (this.parent.iframeSettings.enable && !ej2_base_1.isNullOrUndefined(this.parent.inputElement)) {
                this.parent.inputElement.classList.remove('e-count-enabled');
            }
            if (this.element && !ej2_base_1.isNullOrUndefined(this.parent.element.querySelector('.' + classes_1.CLS_COUNT))) {
                ej2_base_1.detach(this.element);
            }
            this.removeEventListener();
            Eif (this.editPanel) {
                this.editPanel = null;
            }
            Eif (this.element) {
                ej2_base_1.detach(this.element);
                this.element = null;
            }
            this.isDestroyed = true;
        };
        Count.prototype.toggle = function (e) {
            this.element.style.display = (e.member === 'viewSource') ? 'none' : 'block';
        };
        Count.prototype.addEventListener = function () {
            this.parent.on(events.initialEnd, this.renderCount, this);
            this.parent.on(events.keyUp, this.refresh, this);
            this.parent.on(events.count, this.refresh, this);
            this.parent.on(events.refreshBegin, this.refresh, this);
            this.parent.on(events.mouseDown, this.refresh, this);
            this.parent.on(events.destroy, this.destroy, this);
            this.parent.on(events.sourceCode, this.toggle, this);
            this.parent.on(events.updateSource, this.toggle, this);
        };
        Count.prototype.removeEventListener = function () {
            this.parent.off(events.initialEnd, this.renderCount);
            this.parent.off(events.keyUp, this.refresh);
            this.parent.off(events.refreshBegin, this.refresh);
            this.parent.off(events.count, this.refresh);
            this.parent.off(events.mouseDown, this.refresh);
            this.parent.off(events.destroy, this.destroy);
            this.parent.off(events.sourceCode, this.toggle);
            this.parent.off(events.updateSource, this.toggle);
        };
        Count.prototype.getModuleName = function () {
            return 'count';
        };
        return Count;
    }());
    exports.Count = Count;
});