All files / site/inputs virtual.ts

5% Statements 3/60
0% Branches 0/26
0% Functions 0/8
5.66% Lines 3/53

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 982x     2x                       2x                                                                                                                                                                    
import DataHandler from '../../data_handler/index'
import type {Generic, SiteCondition} from '../../types'
import type Community from '../index'
import {BaseInput} from './index'
 
export type VirtualSpec = {
  id: string
  states: {
    condition: SiteCondition[]
    value: string
  }[]
  default: string
  display: Generic
}
 
export class InputVirtual extends BaseInput {
  type: 'virtual' = 'virtual'
  spec: VirtualSpec
  states: {
    condition: SiteCondition[]
    value: string | number
  }[] = []
  source: string | number
  values: (string | number)[] = []
  display: Generic
  constructor(spec: VirtualSpec, site: Community) {
    const e = document.createElement('input')
    e.id = spec.id
    super(e, site)
    this.source = this.default = spec.default
    Iif (this.source) this.values.push(this.source)
    Iif (spec.states) this.states = spec.states
    Iif (spec.display) this.display = spec.display
    const p: {[index: string]: {type: 'update'; id: string}} = {}
    this.states.forEach(si => {
      this.values.push(si.value)
      si.condition.forEach(c => {
        p[c.id] = {type: 'update', id: this.id}
        this.site.add_dependency(c.id, p[c.id])
      })
    })
    this.update()
  }
  init() {
    this.values.forEach(id => {
      Iif ('string' === typeof id && id in this.site.inputs) this.site.add_dependency(id, {type: 'update', id: this.id})
    })
  }
  update() {
    this.source = void 0
    for (let p, i = this.states.length; i--; ) {
      p = true
      for (let c = this.states[i].condition.length; c--; ) {
        const r = this.states[i].condition[c]
        if (DataHandler.checks[r.type](this.site.valueOf(r.id), this.site.valueOf(r.value))) {
          Iif (r.any) {
            p = true
            break
          }
        } else p = false
      }
      Iif (p) {
        this.source = this.states[i].value
        break
      }
    }
    Iif (!this.source) this.source = this.default as string
    if (this.source !== this.previous) {
      this.previous = this.source
      this.site.request_queue(this.id)
    } else Iif (this.source in this.site.inputs) {
      const r = this.site.inputs[this.source].value()
      Iif (r !== this.previous) {
        this.previous = r
        this.site.request_queue(this.id)
      }
    }
  }
  value() {
    return this.site.valueOf(this.source)
  }
  set(v: string | number) {
    if (-1 !== this.values.indexOf(v)) {
      this.previous = this.source
      this.source = v
    } else Iif (this.source in this.site.inputs) {
      const c = this.site.inputs[this.source]
      Iif (
        'values' in c &&
        (Array.isArray(c.values) ? -1 !== c.values.indexOf(v) : v in c.values || ('display' in c && v in c.display))
      ) {
        c.set(v)
      }
    }
    this.site.request_queue(this.id)
  }
}