(function(define){define(function(require,exports,module){ /*jshint esnext:true*/ 'use strict'; /** * Dependencies */ var component = require('gaia-component'); /** * Locals */ var DEFAULT_LENGTH = 4; /** * Exports */ module.exports = component.register('gaia-text-input-pin', { created: function() { 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: function() { 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: function(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: function() { 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); } }, attributeChangedCallback: function(attr, from, to) { if (attrs[attr]) { this[attr] = to; } }, clear: function(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: `
` }); });})((function(n,w){return typeof define=='function'&&define.amd? define:typeof module=='object'?function(c){c(require,exports,module);}:function(c){ var m={exports:{}},r=function(n){return w[n];};w[n]=c(r,m.exports,m)||m.exports;};})('gaia-text-input-pin',this));