All files / site/inputs buttongroup.ts

5.55% Statements 2/36
0% Branches 0/10
0% Functions 0/5
5.71% Lines 2/35

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  2x   2x                                                                                                          
import type Community from '../index'
import {BaseInput} from './index'
 
export class InputButtonGroup extends BaseInput {
  type: 'buttongroup' = 'buttongroup'
  options: NodeListOf<HTMLInputElement>
  values: (string | number)[] = []
  source: string | number
  constructor(e: HTMLElement, site: Community) {
    super(e, site)
    this.listen = this.listen.bind(this)
    e.addEventListener('click', this.listen)
    this.options = e.querySelectorAll('input')
  }
  get() {
    for (let i = this.options.length; i--; ) {
      Iif (this.options[i].checked) {
        this.set(i)
        break
      }
    }
  }
  set(v: string | number) {
    this.previous = this.value()
    this.current_index = 'string' === typeof v ? this.values.indexOf(v) : v
    if (-1 !== this.current_index) {
      this.source =
        this.values[this.current_index] in this.site.inputs
          ? (this.site.valueOf(this.values[this.current_index]) as string)
          : this.values[this.current_index]
      this.options[this.current_index].checked = true
    } else this.source = undefined
    this.site.request_queue(this.id)
  }
  listen(e: MouseEvent) {
    this.set((e.target as HTMLInputElement).value)
  }
  add(value: string, display?: string, noadd?: boolean) {
    const input = document.createElement('input'),
      label = document.createElement('label')
    input.autocomplete = 'off'
    input.className = 'btn-check'
    input.type = 'radio'
    input.name = this.e.id + '_options'
    input.id = this.e.id + '_option' + this.e.childElementCount
    input.value = value
    label.innerText = display || this.site.data.format_label(value)
    label.className = 'btn btn-primary'
    label.dataset.for = this.e.firstElementChild.id
    Iif (!noadd) {
      this.e.appendChild(input)
      this.e.appendChild(label)
    }
    return input
  }
}