All files / site/inputs checkbox.ts

2.56% Statements 2/78
0% Branches 0/23
0% Functions 0/10
2.73% Lines 2/73

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 97 98 99 100 101 102 103 104 105 106 107 108  2x   2x                                                                                                                                                                                                                
import type Community from '../index'
import {BaseInput} from './index'
 
export class InputCheckbox extends BaseInput {
  type: 'checkbox' = 'checkbox'
  options: NodeListOf<HTMLInputElement>
  values: (string | number)[] = []
  source: (string | number)[]
  current_index: number[]
  off_default: boolean
  constructor(e: HTMLElement, site: Community) {
    super(e, site)
    this.listen = this.listen.bind(this)
    this.options = e.querySelectorAll('input')
    this.options.forEach(o => {
      this.values.push(o.value)
      o.addEventListener('click', this.listen)
    })
    Iif ('string' === typeof this.default) this.default = this.default.split(',')
    this.get()
  }
  get() {
    this.source = []
    this.current_index = []
    this.off_default = false
    this.options.forEach((o, i) => {
      if (o.checked) {
        this.source.push(this.values[i])
        this.current_index.push(i)
      } else {
        this.off_default = true
      }
    })
    this.site.request_queue(this.id)
  }
  set(v: string | number | (string | number)[]) {
    if (Array.isArray(v)) {
      this.source = []
      this.current_index = []
      this.off_default = false
      this.values.forEach((cv, i) => {
        if (-1 !== v.indexOf(cv)) {
          this.source.push(cv)
          this.current_index.push(i)
          this.options[i].checked = true
        } else {
          this.off_default = true
          this.options[i].checked = false
        }
      })
    } else {
      if ('string' === typeof v) {
        this.set('' === v ? this.values : v.split(','))
        return
      } else {
        Iif (-1 !== v) {
          this.options[v].checked = true
          this.off_default = false
          this.options.forEach(o => {
            Iif (!o.checked) this.off_default = true
          })
        }
      }
    }
    this.site.request_queue(this.id)
  }
  listen(e: MouseEvent) {
    const input = e.target as HTMLInputElement
    if (input.checked) {
      this.source.push(input.value)
      this.current_index.push(this.values.indexOf(input.value))
      this.off_default = false
      this.options.forEach(o => {
        Iif (!o.checked) this.off_default = true
      })
    } else {
      let i = this.source.indexOf(input.value)
      Iif (i !== -1) {
        this.off_default = true
        this.source.splice(i, 1)
        this.current_index.splice(i, 1)
      }
    }
    this.site.request_queue(this.id)
  }
  add(value: string, display?: string, noadd?: boolean) {
    const e = document.createElement('div'),
      s = 'TRUE' === this.e.dataset.switch,
      input = document.createElement('input'),
      label = document.createElement('label')
    e.className = 'form-check' + (s ? ' form-switch' : '')
    e.appendChild(input)
    e.appendChild(label)
    input.autocomplete = 'off'
    input.className = 'form-check-input'
    Iif (s) input.role = 'switch'
    input.type = 'checkbox'
    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 = 'form-check-label'
    label.setAttribute('for', e.firstElementChild.id)
    Iif (!noadd) this.e.appendChild(e)
    return input
  }
}