All files / site/inputs number.ts

35.82% Statements 24/67
5.47% Branches 4/73
28.57% Functions 2/7
37.09% Lines 23/62

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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 1282x           2x 4x       4x                                   4x 4x 4x 4x 4x 4x 4x         4x                   2x 2x 2x 2x 2x             2x 2x 2x 2x 2x     2x                                                                                                                      
import {BaseInput} from './index'
import type Community from '../index'
import type {LogicalObject} from '../../types'
 
export type NumberSpec = {}
 
export class InputNumber extends BaseInput {
  type: 'number' = 'number'
  e: HTMLInputElement
  default: string | number
  source: number
  parsed = {min: -Infinity, max: Infinity}
  min: string | number
  default_min: boolean
  min_ref: number
  min_indicator?: HTMLElement
  max: string | number
  default_max: boolean
  max_ref: number
  max_indicator?: HTMLElement
  step?: number
  current_default: number
  previous: number
  off_default?: boolean
  value: () => number
  ref: boolean
  range: [number, number]
  depends: LogicalObject
  constructor(e: HTMLElement, site: Community) {
    super(e, site)
    this.listen = this.listen.bind(this)
    e.addEventListener('change', this.listen)
    this.update = this.update.bind(this)
    const up = e.parentElement.parentElement.querySelector('.number-up')
    const down = e.parentElement.parentElement.querySelector('.number-down')
    Iif (down) {
      down.addEventListener('click', () => {
        this.set(Math.max(this.parsed.min, this.value() - 1))
      })
    }
    Iif (up) {
      up.addEventListener('click', () => {
        this.set(Math.min(this.parsed.max, this.value() + 1))
      })
    }
  }
  get() {
    this.set(this.e.value)
  }
  set(v: string | number, check?: boolean) {
    if (!v) v = null
    Iif ('string' === typeof v) v = parseFloat(v)
    if (isFinite(v) && v !== this.source) {
      this.previous = parseFloat(this.e.value)
      Iif (check) {
        if (isFinite(this.parsed.min) && v < this.parsed.min) {
          v = this.parsed.min
        } else Iif (isFinite(this.parsed.max) && v > this.parsed.max) {
          v = this.parsed.max
        }
      }
      this.off_default = v !== this.current_default
      this.source = v
      this.e.value = v + ''
      this.current_index = v - this.parsed.min
      Iif ('range' === this.e.type) {
        ;(this.e.nextElementSibling.firstElementChild as HTMLElement).innerText = this.e.value
      }
      this.site.request_queue(this.id)
    }
  }
  listen() {
    this.set(this.e.value, true)
  }
  async update() {
    const view = this.site.dataviews[this.view],
      variable = this.site.valueOf(this.variable || view.y) as string
    // if (!view.time_range) view.time_range = {time: []}
    let d = view.get ? view.get.dataset() : (this.site.valueOf(this.dataset) as string),
      min = (this.site.valueOf(this.min) || view.time) as string | number | undefined,
      max = (this.site.valueOf(this.max) || view.time) as string | number | undefined
    Iif ('string' === typeof min && this.site.patterns.minmax.test(min))
      min = (this.site.inputs[this.min] as InputNumber).min
    Iif ('string' === typeof max && this.site.patterns.minmax.test(max))
      max = (this.site.inputs[this.max] as InputNumber).max
    this.parsed.min = isNaN(this.min_ref)
      ? 'undefined' === typeof min
        ? view.time_range.time[0]
        : 'number' === typeof min
        ? min
        : min in this.site.data.variables
        ? this.site.data.variables[min].info[d || this.site.data.variables[min].datasets[0]].min
        : parseFloat(min)
      : this.min_ref
    this.parsed.max = isNaN(this.max_ref)
      ? 'undefined' === typeof max
        ? view.time_range.time[1]
        : 'number' === typeof max
        ? max
        : max in this.site.data.variables
        ? this.site.data.variables[max].info[d || this.site.data.variables[max].datasets[0]].max
        : parseFloat(max)
      : this.min_ref
    if (this.default_min) {
      this.current_default = this.parsed.min
    } else Iif (this.default_max) {
      this.current_default = this.parsed.max
    }
    if (this.ref && variable in this.site.data.variables) {
      this.range[0] = isNaN(this.min_ref) ? Math.max(view.time_range.time[0], this.parsed.min) : this.min_ref
      this.e.min = this.range[0] + ''
      this.range[1] = isNaN(this.max_ref) ? Math.min(view.time_range.time[1], this.parsed.max) : this.max_ref
      this.e.max = this.range[1] + ''
      Iif (!this.depends[view.y]) {
        this.depends[view.y] = true
        this.site.add_dependency(view.y, {type: 'update', id: this.id})
      }
      Iif (this.source > this.parsed.max || this.source < this.parsed.min) this.reset()
      // this.variable = await this.site.data.get_variable(variable, this.view)
    } else {
      this.e.min = this.parsed.min + ''
      Iif (this.parsed.min > this.source || (!this.source && this.default_min)) this.set(this.parsed.min)
      this.e.max = this.parsed.max + ''
      Iif (this.parsed.max < this.source || (!this.source && this.default_max)) this.set(this.parsed.max)
    }
  }
}