(define=>{define((require,exports,module)=>{ 'use strict'; /** * Dependencies */ var component = require('gaia-component'); /** * Locals */ var DEFAULT_LENGTH = 4; /** * Exports */ module.exports = component.register('gaia-text-input-pin', { created() { this.setupShadowRoot(); this.els = { inner: this.shadowRoot.querySelector('.inner'), fields: this.shadowRoot.querySelector('.fields'), input: this.shadowRoot.querySelector('input') }; this.disabled = this.hasAttribute('disabled'); this.length = this.getAttribute('length') || DEFAULT_LENGTH; this.addEventListener('keyup', () => this.updateCells()); }, updateCells() { var l = this.els.input.value.length; this.els.cells.forEach((cell, i) => { cell.classList.toggle('populated', i < l); cell.classList.toggle('focused', i == l); }); }, onBackspace(e) { var input = e.target; var empty = !input.value; var previous = input.previousElementSibling; if (!empty && previous) { previous.clear(); previous.focus(); } else if (empty && previous) { previous.focus(); } }, setupFields() { this.els.fields.innerHTML = ''; this.els.cells = []; for (var i = 0, l = this.length; i < l; i++) { var el = document.createElement('div'); el.className = 'cell'; this.els.fields.appendChild(el); this.els.cells.push(el); } }, clear(e) { this.value = ''; }, /** * Attributes */ attrs: { length: { get: function() { return this._length; }, set: function(value) { value = Number(value); this._length = value; this.els.input.setAttribute('maxlength', this.length); this.setupFields(); } }, value: { get: function() { return this.els.input.value; }, set: function(value) { this.els.input.value = value; } }, disabled: { get: function() { return this.els.input.disabled; }, set: function(value) { value = !!(value === '' || value); this.els.input.disabled = value; } } }, template: `
` }); })})(((n,w)=>{return(typeof define)[0]=='f'&&define.amd?define:(typeof module)[0]=='o'?c =>{c(require,exports,module)}:c=>{var m={exports:{}},r=n=>w[n];w[n]=c(r,m.exports,m)||m.exports;};})('gaia-text-input-pin',this));/*jshint ignore:line*/