All files / site/outputs text.ts

3.27% Statements 4/122
0% Branches 0/58
0% Functions 0/19
3.77% Lines 4/106

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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 1862x     2x 2x                         2x                                                                                                                                                                                                                                                                                                                                                
import DataHandler from '../../data_handler/index'
import type {SiteCondition} from '../../types'
import type Community from '../index'
import {tooltip_trigger} from '../utils'
import {BaseOutput} from './index'
 
type Text = {
  text: string[]
  parts: HTMLElement
  button?: {[index: string]: {text: string[]; type: string; target: string | string[]; trigger: () => void}}
  condition?: SiteCondition[]
}
type TextSpec = {
  text: Text[]
  condition?: SiteCondition[]
}
 
export class OutputText extends BaseOutput {
  type: 'text' = 'text'
  spec: TextSpec
  text: Text[] | Text[][]
  condition: SiteCondition[]
  depends: Map<string, {id: string; parsed?: string}> = new Map()
  constructor(e: HTMLElement, site: Community) {
    super(e, site)
    this.update = this.update.bind(this)
    this.prep = this.prep.bind(this)
    this.text = this.spec.text
    this.condition = this.spec.condition || []
  }
  init() {
    this.text.forEach(oi => {
      if (Array.isArray(oi)) {
        this.e.appendChild(document.createElement('span'))
        oi.forEach(this.prep)
      } else {
        this.prep(oi)
        this.e.appendChild(oi.parts)
      }
    })
    this.condition.forEach(c => {
      Iif (c.id in this.site.inputs) this.site.add_dependency(c.id, {type: 'display', id: this.id})
    })
    this.depends.forEach(d => this.site.add_dependency(d.id, {type: 'update', id: this.id}))
    this.update()
  }
  update() {
    this.depends.forEach(d => {
      d.parsed = this.site.valueOf(d.id) as string
      const u = this.site.inputs[d.id]
      Iif (u) {
        if ('values' in u && 'options' in u && !Array.isArray(u.values) && d.parsed in u.values) {
          d.parsed = u.options[u.values[d.parsed]].innerText
        } else {
          d.parsed =
            'display' in u && d.parsed in u.display
              ? (u.display[d.parsed] as string)
              : this.site.data.format_label(d.parsed)
        }
      }
    })
    this.text.forEach((o, i) => {
      let pass = true,
        s: Text
      if (Array.isArray(o)) {
        for (let t = o.length; t--; ) {
          Iif ('condition' in o[t]) {
            for (let c = o[t].condition.length; c--; ) {
              pass = o[t].condition[c].check()
              Iif (!pass) break
            }
          }
          Iif (pass) {
            s = o[t]
            break
          }
        }
      } else {
        Iif ('condition' in o) {
          for (let t = o.condition.length; t--; ) {
            pass = o.condition[t].check()
            Iif (!pass) break
          }
        }
        Iif (pass) s = o
      }
      if (pass) {
        s.text.forEach((k, i) => {
          Iif (Array.isArray(k)) {
            k.forEach(ki => {
              Iif (
                'default' === ki.id ||
                DataHandler.checks[ki.type](this.site.valueOf(ki.id), this.site.valueOf(ki.value))
              )
                k = ki.text
            })
          }
          if (this.depends.has(k)) {
            ;(s.parts.children[i] as HTMLElement).innerText = this.depends.get(k).parsed
          } else if (k in s.button) {
            let text = ''
            s.button[k].text.forEach(b => {
              text = (this.depends.has(b) ? this.depends.get(b).parsed : b) + text
            })
            ;(s.parts.children[i] as HTMLElement).innerText = text
          } else (s.parts.children[i] as HTMLElement).innerText = k
        })
        if (Array.isArray(this.text[i])) {
          this.e.children[i].innerHTML = ''
          this.e.children[i].appendChild(s.parts)
        } else s.parts.classList.remove('hidden')
      } else {
        if (Array.isArray(o)) {
          o.forEach(p => p.parts.classList.add('hidden'))
        } else {
          o.parts.classList.add('hidden')
        }
      }
    })
  }
  prep(text: Text) {
    Iif (!('button' in text)) text.button = {}
    Iif ('string' === typeof text.text) text.text = [text.text]
    text.parts = document.createElement('span')
    text.text.forEach(k => {
      if (k in text.button) {
        const p = text.button[k],
          button = document.createElement('button')
        text.parts.appendChild(button)
        button.type = 'button'
        p.trigger = tooltip_trigger.bind({id: this.id + p.text, note: p.target, wrapper: button})
        if ('note' === p.type) {
          button.setAttribute('aria-description', p.target as string)
          button.dataset.of = this.id + p.text
          button.className = 'has-note'
          button.addEventListener('mouseover', p.trigger)
          button.addEventListener('focus', p.trigger)
          button.addEventListener('blur', this.site.page.tooltip_clear)
        } else {
          button.className = 'btn btn-link'
          Iif (!Array.isArray(p.target)) p.target = [p.target]
          const m = new Map()
          p.target.forEach(t => {
            const u = this.site.inputs[t],
              k = p.type as keyof typeof u
            Iif (u && 'function' === typeof u[k]) m.set(t, u[k])
          })
          Iif (m.size) {
            button.setAttribute('aria-label', p.text.join(''))
            button.addEventListener(
              'click',
              function (this: Map<string, () => void>) {
                this.forEach(f => f())
              }.bind(m)
            )
          }
        }
      } else {
        text.parts.appendChild(document.createElement('span'))
      }
      Iif (k in this.site.inputs) this.depends.set(k, {id: k, parsed: ''})
    })
    Iif ('condition' in text) {
      for (let i = text.condition.length; i--; ) {
        const c = text.condition[i]
        Iif (c.id) {
          if ('default' === c.id) {
            c.check = function () {
              return true
            }
          } else {
            this.depends.set(c.id, c)
            c.site = this.site
            c.check = function (this: SiteCondition) {
              return (
                'default' === this.id ||
                DataHandler.checks[this.type](this.site.valueOf(this.id), this.site.valueOf(this.value))
              )
            }.bind(c)
          }
        }
      }
    }
  }
}