(define=>{define((require,exports,module)=>{ 'use strict'; /** * Dependencies */ var component = require('gaia-component'); require('gaia-icons'); // Load gaia-icons /** * Mini logger * * @type {Function} */ var debug = 0 ? (...args) => console.log('[GaiaTextInput]', ...args) : () => {}; /** * Exports */ module.exports = component.register('gaia-text-input', { created() { this.setupShadowRoot(); this.els = { inner: this.shadowRoot.querySelector('.inner'), input: this.shadowRoot.querySelector('input'), clear: this.shadowRoot.querySelector('.clear') }; this.type = this.getAttribute('type'); this.disabled = this.hasAttribute('disabled'); this.clearable = this.hasAttribute('clearable'); this.placeholder = this.getAttribute('placeholder'); this.required = this.getAttribute('required'); this.value = this.getAttribute('value'); // Don't take focus from the input field this.els.clear.addEventListener('mousedown', e => e.preventDefault()); this.els.clear.addEventListener('click', e => this.clear(e)); this.els.input.addEventListener('input', e => this.onInput(e)); this.els.input.addEventListener('focus', e => this.onFocus(e)); this.els.input.addEventListener('blur', e => this.onBlur(e)); }, /** * Clear the field. * * @public */ clear() { debug('clear'); this.value = ''; this.emit('clear'); }, /** * Focus the field. * * @public */ focus() { debug('focus'); this.els.input.focus(); }, /** * Unfocus the field. * * @public */ blur() { debug('blur'); this.els.input.blur(); }, /** * Runs when the field is focused. * * @private */ onFocus() { debug('on focus'); this.els.inner.classList.add('focused'); this.emit('focus'); }, /** * Runs when the field is unfocused. * * @private */ onBlur() { debug('on blur'); this.els.inner.classList.remove('focused'); this.emit('blur'); }, /** * Runs when the value of the * input is manually changed. * * @private */ onInput() { debug('on input'); this.onValueChanged(); }, /** * Runs when the values changes * programatically or via keystrokes. * * @private */ onValueChanged() { debug('value changed'); var hasValue = !!this.value.length; this.els.inner.classList.toggle('has-value', hasValue); this.emit('input'); }, /** * Emit a DOM event on the component. * * @param {String} name * @param {*} detail * @private */ emit(name, detail) { var e = new CustomEvent(name, { detail: detail }); this.dispatchEvent(e); }, /** * Attributes */ attrs: { type: { get: function() { return this.els.input.getAttribute('type'); }, set: function(value) { if (!value) { return; } this.els.inner.setAttribute('type', value); this.els.input.setAttribute('type', value); } }, placeholder: { get: function() { return this.field.placeholder; }, set: function(value) { if (!value && value !== '') { return; } this.els.input.placeholder = value; } }, clearable: { get: function() { return this._clearable; }, set: function(value) { var clearable = !!(value === '' || value); if (clearable === this.clearable) { return; } if (clearable) this.setAttr('clearable', ''); else this.removeAttr('clearable'); this._clearable = clearable; } }, value: { get: function() { return this.els.input.value; }, set: function(value) { debug('set value', value); this.els.input.value = value; this.onValueChanged(); } }, required: { get: function() { return this.els.input.required; }, set: function(value) { this.els.input.required = value; } }, maxlength: { get: function() { return this.els.input.getAttribute('maxlength'); }, set: function(value) { this.els.input.setAttribute('maxlength', value); } }, disabled: { get: function() { return this.els.input.disabled; }, set: function(value) { value = !!(value === '' || value); this.els.input.disabled = value; } } }, template: `