All files / site/inputs index.ts

78% Statements 39/50
64% Branches 16/25
66.66% Functions 4/6
81.81% Lines 36/44

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 109 110 111 112 113 1142x                                                                           2x   7x         7x                     7x 7x 7x         7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 3x 3x   7x 7x 7x 7x 7x 7x 49x 49x     7x                 7x     14x 14x 14x                  
import {tooltip_trigger} from '../utils'
import type {LogicalObject, SiteRule, SiteSpec} from '../../types'
import type Community from '../index'
import type {InputButton} from './button'
import type {InputButtonGroup} from './buttongroup'
import type {InputCheckbox} from './checkbox'
import type {InputCombobox} from './combobox'
import type {InputNumber} from './number'
import type {InputRadio} from './radio'
import type {InputSelect} from './select'
import type {InputVirtual} from './virtual'
import type {InputSwitch} from './switch'
import type {InputText} from './text'
 
type InputTypes =
  | 'number'
  | 'radio'
  | 'switch'
  | 'checkbox'
  | 'button'
  | 'text'
  | 'buttongroup'
  | 'select'
  | 'combobox'
  | 'virtual'
export type SiteInputs =
  | InputCombobox
  | InputSelect
  | InputNumber
  | InputButton
  | InputVirtual
  | InputRadio
  | InputText
  | InputSwitch
  | InputCheckbox
  | InputButtonGroup
export type RegisteredInputs = {[index: string]: SiteInputs}
 
export abstract class BaseInput {
  type: InputTypes
  input = true
  site: Community
  e: HTMLElement
  wrapper: HTMLElement
  id: string
  settings: {[index: string]: any} = {}
  default: string | string[] | number
  source: boolean | string | number | (string | number)[]
  optionSource: string
  subset: string
  selection_subset: string
  depends: string | LogicalObject
  variable: string
  dataset: string
  view: string
  note: string
  current_index: number | number[] = -1
  previous: boolean | string | number | (string | number)[] = ''
  state = 'initial'
  setting?: string
  deferred?: boolean
  rule?: SiteRule
  constructor(e: HTMLElement, site: Community) {
    this.e = e
    this.site = site
    this.default = e.dataset.default
    this.optionSource = e.dataset.optionsource
    this.subset = e.dataset.subset || 'all'
    this.selection_subset = e.dataset.selectionsubset || this.subset
    this.depends = e.dataset.depends
    this.variable = e.dataset.variable
    this.dataset = e.dataset.dataset
    this.view = e.dataset.view
    this.id = e.id || this.optionSource || 'ui' + site.page.elementCount++
    this.note = e.getAttribute('aria-description') || ''
    this.type = e.dataset.autotype as InputTypes
    if (this.type in site.spec) {
      const spec = site.spec[this.type as keyof SiteSpec] as SiteInputs
      if (this.id in spec) this.settings = (spec as any)[this.id]
    }
    if (e.parentElement)
      this.wrapper = e.parentElement.classList.contains('wrapper') ? e.parentElement : e.parentElement.parentElement
    if (this.wrapper) {
      Iif (this.note) this.wrapper.classList.add('has-note')
      this.wrapper.setAttribute('data-of', this.id)
      ;['div', 'span', 'label', 'fieldset', 'legend', 'input', 'button'].forEach(type => {
        const c = this.wrapper.querySelectorAll(type)
        if (c.length) c.forEach(ci => ci.setAttribute('data-of', this.id))
      })
    }
    Iif (this.note) {
      const trigger = tooltip_trigger.bind(this)
      this.wrapper.addEventListener('mouseover', trigger)
      const p = 'DIV' !== e.tagName ? e : e.querySelector('input')
      Iif (p) {
        p.addEventListener('focus', trigger)
        p.addEventListener('blur', this.site.page.tooltip_clear)
      }
    }
    Iif (site.patterns.number.test(this.default)) this.default = +this.default
  }
  value() {
    Iif (Array.isArray(this.source)) return this.source
    const v = this.site.valueOf(this.source)
    return 'undefined' === typeof v ? this.site.valueOf(this.default) : v
  }
  set(v: any) {
    this.source = v
  }
  reset() {
    this.set(this.site.valueOf(this.default))
  }
}