all files / textbox/ textbox.js

97.35% Statements 184/189
96.51% Branches 83/86
100% Functions 27/27
97.27% Lines 178/183
14 statements, 2 functions, 12 branches Ignored     
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  13× 13× 13×       174× 174× 174× 174×                                                                                   11×             13× 13×   12×   11×                                                                                                                                                                                                                                                            
/* istanbul ignore next */ 
var __extends = (this && this.__extends) || (function () {
    var extendStatics = Object.setPrototypeOf ||
        ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
        function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
/* istanbul ignore next */ 
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    Iif (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) Eif (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
define(["require", "exports", "@syncfusion/ej2-base", "@syncfusion/ej2-base", "../input/input"], function (require, exports, ej2_base_1, ej2_base_2, input_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var ROOT = 'e-textbox';
    var CONTROL = 'e-control';
    var HIDE_CLEAR = 'e-clear-icon-hide';
    /**
     * Represents the TextBox component that allows the user to enter the values based on it's type.
     * ```html
     * <input name='images' id='textbox'/>
     * ```
     * ```typescript
     * <script>
     *   var textboxObj = new TextBox();
     *   textboxObj.appendTo('#textbox');
     * </script>
     * ```
     */
    var TextBox = /** @class */ (function (_super) {
        __extends(TextBox, _super);
        function TextBox(options, element) {
            var _this = _super.call(this, options, element) || this;
            _this.previousValue = null;
            return _this;
        }
        /**
         * Calls internally if any of the property value is changed.
         * @private
         */
        TextBox.prototype.onPropertyChanged = function (newProp, oldProp) {
            for (var _i = 0, _a = Object.keys(newProp); _i < _a.length; _i++) {
                var prop = _a[_i];
                switch (prop) {
                    case 'floatLabelType':
                        input_1.Input.removeFloating(this.textboxWrapper);
                        input_1.Input.addFloating(this.element, this.floatLabelType, this.placeholder);
                        break;
                    case 'enabled':
                        input_1.Input.setEnabled(this.enabled, this.element);
                        break;
                    case 'value':
                        input_1.Input.setValue(this.value, this.element, this.floatLabelType, this.showClearButton);
                        this.raiseChangeEvent();
                        break;
                    case 'readonly':
                        input_1.Input.setReadonly(this.readonly, this.element);
                        break;
                    case 'type':
                        this.element.setAttribute('type', this.type);
                        this.raiseChangeEvent();
                        break;
                    case 'showClearButton':
                        input_1.Input.setClearButton(this.showClearButton, this.element, this.textboxWrapper);
                        this.bindClearEvent();
                        break;
                    case 'enableRtl':
                        input_1.Input.setEnableRtl(this.enableRtl, [this.textboxWrapper.container]);
                        break;
                    case 'placeholder':
                        input_1.Input.setPlaceholder(this.placeholder, this.element);
                        break;
                    case 'cssClass':
                        input_1.Input.setCssClass(this.cssClass, [this.textboxWrapper.container]);
                        break;
                    case 'locale':
                        this.globalize = new ej2_base_2.Internationalization(this.locale);
                        this.l10n.setLocale(this.locale);
                        this.setProperties({ placeholder: this.l10n.getConstant('placeholder') }, true);
                        input_1.Input.setPlaceholder(this.placeholder, this.element);
                        break;
                }
            }
        };
        /**
         * Gets the component name
         * @private
         */
        TextBox.prototype.getModuleName = function () {
            return 'textbox';
        };
        TextBox.prototype.preRender = function () {
            this.cloneElement = this.element.cloneNode(true);
            /* istanbul ignore next */
            Iif (this.element.tagName === 'EJS-TEXTBOX') {
                var ejInstance = ej2_base_1.getValue('ej2_instances', this.element);
                var inputElement = ej2_base_1.createElement('input');
                var index = 0;
                for (index; index < this.element.attributes.length; index++) {
                    inputElement.setAttribute(this.element.attributes[index].nodeName, this.element.attributes[index].nodeValue);
                    inputElement.innerHTML = this.element.innerHTML;
                }
                this.element.appendChild(inputElement);
                this.element = inputElement;
                ej2_base_1.setValue('ej2_instances', ejInstance, this.element);
            }
            var attributes = this.element.attributes;
            this.checkAttributes(attributes);
            this.element.setAttribute('type', this.type);
            this.globalize = new ej2_base_2.Internationalization(this.locale);
            var localeText = { placeholder: this.placeholder };
            this.l10n = new ej2_base_1.L10n('textbox', localeText, this.locale);
            if (this.l10n.getConstant('placeholder') !== '') {
                this.setProperties({ placeholder: this.placeholder || this.l10n.getConstant('placeholder') }, true);
            }
        };
        TextBox.prototype.checkAttributes = function (attrs) {
            for (var i = 0; i < attrs.length; i++) {
                var key = attrs[i].nodeName;
                if (key === 'disabled') {
                    this.setProperties({ enabled: false }, true);
                }
                else if (key === 'readonly') {
                    this.setProperties({ readonly: true }, true);
                }
                else if (key === 'placeholder') {
                    this.setProperties({ placeholder: attrs[i].nodeValue }, true);
                }
            }
        };
        /**
         * To Initialize the control rendering
         * @private
         */
        TextBox.prototype.render = function () {
            this.textboxWrapper = input_1.Input.createInput({
                element: this.element,
                floatLabelType: this.floatLabelType,
                properties: {
                    enabled: this.enabled,
                    enableRtl: this.enableRtl,
                    cssClass: this.cssClass,
                    readonly: this.readonly,
                    placeholder: this.placeholder,
                    showClearButton: this.showClearButton
                }
            });
            this.wireEvents();
        };
        TextBox.prototype.wireEvents = function () {
            ej2_base_1.EventHandler.add(this.element, 'focus', this.focusHandler, this);
            ej2_base_1.EventHandler.add(this.element, 'blur', this.focusOutHandler, this);
            ej2_base_1.EventHandler.add(this.element, 'input', this.inputHandler, this);
            ej2_base_1.EventHandler.add(this.element, 'change', this.changeHandler, this);
            if (this.enabled) {
                this.bindClearEvent();
            }
        };
        TextBox.prototype.focusHandler = function (args) {
            var eventArgs = {
                container: this.textboxWrapper.container,
                event: args,
                value: this.value
            };
            this.trigger('focus', eventArgs);
        };
        TextBox.prototype.focusOutHandler = function (args) {
            if (!(this.previousValue === null && this.value === null && this.element.value === '') &&
                (this.previousValue !== this.element.value)) {
                this.raiseChangeEvent(args, true);
            }
            var eventArgs = {
                container: this.textboxWrapper.container,
                event: args,
                value: this.value
            };
            this.trigger('blur', eventArgs);
        };
        TextBox.prototype.inputHandler = function (args) {
            var eventArgs = {
                event: args,
                value: this.element.value,
                previousValue: this.value,
                container: this.textboxWrapper.container
            };
            this.trigger('input', eventArgs);
        };
        TextBox.prototype.changeHandler = function (args) {
            this.setProperties({ value: this.element.value }, true);
            this.raiseChangeEvent(args, true);
        };
        TextBox.prototype.raiseChangeEvent = function (event, interaction) {
            var eventArgs = {
                event: event,
                value: this.value,
                previousValue: this.previousValue,
                container: this.textboxWrapper.container,
                isInteraction: interaction ? interaction : false
            };
            this.trigger('change', eventArgs);
            this.previousValue = this.value;
        };
        TextBox.prototype.bindClearEvent = function () {
            if (this.showClearButton) {
                ej2_base_1.EventHandler.add(this.textboxWrapper.clearButton, 'mousedown touchstart', this.resetInputHandler, this);
            }
        };
        TextBox.prototype.resetInputHandler = function (event) {
            event.preventDefault();
            if (!(this.textboxWrapper.clearButton.classList.contains(HIDE_CLEAR))) {
                input_1.Input.setValue('', this.element, this.floatLabelType, this.showClearButton);
            }
        };
        TextBox.prototype.unWireEvents = function () {
            ej2_base_1.EventHandler.remove(this.element, 'focus', this.focusHandler);
            ej2_base_1.EventHandler.remove(this.element, 'blur', this.focusOutHandler);
            ej2_base_1.EventHandler.remove(this.element, 'input', this.inputHandler);
            ej2_base_1.EventHandler.remove(this.element, 'change', this.changeHandler);
        };
        /**
         * Removes the component from the DOM and detaches all its related event handlers.
         * Also it maintains the initial input element from the DOM.
         * @method destroy
         * @return {void}
         */
        TextBox.prototype.destroy = function () {
            this.unWireEvents();
            this.textboxWrapper.container.parentElement.appendChild(this.cloneElement);
            ej2_base_2.detach(this.textboxWrapper.container);
            this.textboxWrapper = null;
            this.cloneElement.classList.remove(ROOT, CONTROL);
            _super.prototype.destroy.call(this);
        };
        /**
         * Gets the properties to be maintained in the persisted state.
         * @return {string}
         */
        TextBox.prototype.getPersistData = function () {
            var keyEntity = ['value'];
            return this.addOnPersist(keyEntity);
        };
        /**
         * Adding the multiple attributes as key value pair to the input element.
         * @param { { [key: string]: string } } attributes - Specifies the attributes to be add to input element.
         * @return {void}
         */
        TextBox.prototype.addAttributes = function (attributes) {
            for (var _i = 0, _a = Object.keys(attributes); _i < _a.length; _i++) {
                var key = _a[_i];
                if (key === 'disabled') {
                    this.setProperties({ enabled: false }, true);
                    input_1.Input.setEnabled(this.enabled, this.element);
                }
                else if (key === 'readonly') {
                    this.setProperties({ readonly: true }, true);
                    input_1.Input.setReadonly(this.readonly, this.element);
                }
                else if (key === 'class') {
                    this.element.classList.add(attributes[key]);
                }
                else if (key === 'placeholder') {
                    this.setProperties({ placeholder: attributes[key] }, true);
                    input_1.Input.setPlaceholder(this.placeholder, this.element);
                }
                else {
                    this.element.setAttribute(key, attributes[key]);
                }
            }
        };
        /**
         * Removing the multiple attributes as key value pair to the input element.
         * @param { string[] } attributes - Specifies the attributes name to be removed from input element.
         * @return {void}
         */
        TextBox.prototype.removeAttributes = function (attributes) {
            for (var _i = 0, attributes_1 = attributes; _i < attributes_1.length; _i++) {
                var key = attributes_1[_i];
                if (key === 'disabled') {
                    this.setProperties({ enabled: true }, true);
                    input_1.Input.setEnabled(this.enabled, this.element);
                }
                else if (key === 'readonly') {
                    this.setProperties({ readonly: false }, true);
                    input_1.Input.setReadonly(this.readonly, this.element);
                }
                else if (key === 'placeholder') {
                    this.setProperties({ placeholder: null }, true);
                    input_1.Input.setPlaceholder(this.placeholder, this.element);
                }
                else {
                    this.element.removeAttribute(key);
                }
            }
        };
        __decorate([
            ej2_base_1.Property('text')
        ], TextBox.prototype, "type", void 0);
        __decorate([
            ej2_base_1.Property(false)
        ], TextBox.prototype, "readonly", void 0);
        __decorate([
            ej2_base_1.Property(null)
        ], TextBox.prototype, "value", void 0);
        __decorate([
            ej2_base_1.Property('Never')
        ], TextBox.prototype, "floatLabelType", void 0);
        __decorate([
            ej2_base_1.Property('')
        ], TextBox.prototype, "cssClass", void 0);
        __decorate([
            ej2_base_1.Property(null)
        ], TextBox.prototype, "placeholder", void 0);
        __decorate([
            ej2_base_1.Property(false)
        ], TextBox.prototype, "enableRtl", void 0);
        __decorate([
            ej2_base_1.Property(true)
        ], TextBox.prototype, "enabled", void 0);
        __decorate([
            ej2_base_1.Property(false)
        ], TextBox.prototype, "showClearButton", void 0);
        __decorate([
            ej2_base_1.Property(false)
        ], TextBox.prototype, "enablePersistence", void 0);
        __decorate([
            ej2_base_1.Event()
        ], TextBox.prototype, "created", void 0);
        __decorate([
            ej2_base_1.Event()
        ], TextBox.prototype, "destroyed", void 0);
        __decorate([
            ej2_base_1.Event()
        ], TextBox.prototype, "change", void 0);
        __decorate([
            ej2_base_1.Event()
        ], TextBox.prototype, "blur", void 0);
        __decorate([
            ej2_base_1.Event()
        ], TextBox.prototype, "focus", void 0);
        __decorate([
            ej2_base_1.Event()
        ], TextBox.prototype, "input", void 0);
        TextBox = __decorate([
            ej2_base_2.NotifyPropertyChanges
        ], TextBox);
        return TextBox;
    }(ej2_base_1.Component));
    exports.TextBox = TextBox;
});