All files / characters-remaining index.js

100% Statements 29/29
68.42% Branches 13/19
100% Functions 8/8
100% Lines 29/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 971x     7x 7x       7x 7x 7x 7x 7x       7x                                                                         7x 7x 1x         7x 7x 7x 7x 7x 7x 7x 7x 7x         7x 7x         1x       14x 7x         16x 16x 16x          
window.customElements.define('h-characters-remaining', class extends HTMLElement {
 
  constructor() {
    super()
    this.updateCharBind = this.updateChar.bind(this)
  }
 
  generateTemplate() {
    const wrapperDiv = document.createElement('div')
    const el = this.firstElementChild
    this.insertAdjacentElement('afterbegin', wrapperDiv)
    wrapperDiv.appendChild(el)
    wrapperDiv.insertAdjacentHTML('beforeend', '<span></span>')
  }
 
  insertStyle() {
    const style = `
    <style type="text/css" id="characters-remaining">
      characters-remaining div {
          border: 1px solid var(--field-border);
          border-radius: 2px;
          display: flex;
          justify-content: space-between;
        }
 
      characters-remaining span {
        align-self: center;
        color: #232323;
        font-size: 0.65rem;
        font-weight: 200;
        padding-right: 0.5rem;
        white-space: nowrap;
      }
 
      characters-remaining textarea + span {
        align-self: flex-end;
        padding-bottom: 0.8rem;
      }
 
      characters-remaining input,
      characters-remaining textarea {
        border: 0;
        font-size: 0.9rem;
        outline: 0;
        padding: 0.8rem;
        width: 100%;
      }
 
      characters-remaining textarea {
        height: 6.8rem;
        resize: none;
      }
    </style>`
    const elem = document.head || this.parentElement || this
    if (!elem.querySelector('#characters-remaining')) {
      elem.insertAdjacentHTML('afterbegin', style)
    }
  }
 
  connectedCallback() {
    this.insertStyle()
    this.generateTemplate()
    this.$userInput = this.querySelector('textarea') || this.querySelector('input')
    this.$span = this.querySelector('span')
    this.dataset.caption = this.getAttribute('data-caption')
    Eif (this.$userInput && this.$userInput.maxLength > 0) {
      this.dataset.caption = this.dataset.caption || 'chars max'
      this.updateChar()
      this.$userInput.addEventListener('input', this.updateCharBind)
    }
  }
 
  disconnectedCallback() {
    Eif (this.$userInput) {
      this.$userInput.removeEventListener('input', this.updateCharBind)
    }
  }
 
  static get observedAttributes() {
    return ['data-caption']
  }
 
  attributeChangedCallback(attr, oldValue, newValue) {
    if (oldValue !== newValue) {
      this.updateChar()
    }
  }
 
  updateChar() {
    Eif(this.$userInput) {
      const maxLen = this.$userInput.maxLength
      this.$span.textContent = `${maxLen - this.$userInput.value.length} ${this.dataset.caption}`
    }
  }
 
})