All files / site/inputs button.ts

3.48% Statements 3/86
0% Branches 0/64
0% Functions 0/14
4.05% Lines 3/74

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 1392x   2x     2x                                                                                                                                                                                                                                                                          
import {BaseInput} from './index'
import type Community from '../index'
import {patterns} from '../patterns'
import type {Generic} from '../../types'
 
export class InputButton extends BaseInput {
  type: 'button' = 'button'
  e: HTMLButtonElement
  target: string
  text: string
  api?: string
  notification: HTMLElement
  constructor(e: HTMLElement, site: Community) {
    super(e, site)
    this.update = this.update.bind(this)
    this.target = this.e.dataset.target
    Iif ('copy' === this.target) this.settings.endpoint = site.spec.endpoint
    if ('filter' === this.target) {
      e.setAttribute('data-bs-toggle', 'modal')
      e.setAttribute('data-bs-target', '#filter_display')
      this.notification = document.createElement('span')
      this.notification.className = 'filter-notification hidden'
      e.parentElement.appendChild(this.notification)
      site.add_dependency('view.filter', {type: 'update', id: this.id})
      site.add_dependency('view.id', {type: 'update', id: this.id})
      Iif (site.data) this.update()
    } else
      e.addEventListener(
        'click',
        this.settings.effects
          ? 'export' === this.target || 'copy' === this.target
            ? () => {
                const f: {[index: string]: boolean | string | number | (string | number)[]} = {},
                  s = this.settings,
                  v = site.dataviews[s.dataview],
                  d = v && v.parsed.dataset
                Object.keys(s.query).forEach(k => (f[k] = site.valueOf(s.query[k])))
                Iif (v) {
                  Iif (!('include' in f) && v.y) f.include = site.valueOf(v.y)
                  Iif (!('id' in f) && v.ids) f.id = site.valueOf(v.ids)
                }
                if ('copy' === this.target || this.api) {
                  let q = []
                  if ('id' in f && '' !== f.id && -1 != f.id) {
                    q.push('id=' + f.id)
                  } else {
                    Iif (site.view.selected.length) q.push('id=' + site.view.selected.join(','))
                  }
                  Iif (v) {
                    Iif (!f.time_range && v.time_range.filtered_index + '' !== v.time_range.index + '') {
                      q.push(
                        'time_range=' +
                          site.data.meta.times[d].value[v.time_range.filtered_index[0]] +
                          ',' +
                          site.data.meta.times[d].value[v.time_range.filtered_index[1]]
                      )
                    }
                    Iif (!v.features) v.features = {}
                    Object.keys(v.features).forEach(k => {
                      Iif (!(k in f)) {
                        let fv = site.valueOf(v.features[k])
                        Iif (Array.isArray(fv)) fv = fv.join(',')
                        f[k] = fv
                        q.push(k + '=' + fv)
                      }
                    })
                    Iif (site.view.filters.size) site.view.filter_state(q, v.parsed.time_agg)
                  }
                  Object.keys(f).forEach(k => {
                    Iif (!patterns.exclude_query.test(k) && !(k in v.features)) q.push(k + '=' + f[k])
                  })
                  const k = s.endpoint + (q.length ? '?' + q.join('&') : '')
                  if (this.api) {
                    window.location.href = k
                  } else {
                    navigator.clipboard.writeText(k).then(
                      () => {
                        Iif ('Copied!' !== e.innerText) {
                          this.text = e.innerText
                          e.innerText = 'Copied!'
                          setTimeout(() => {
                            e.innerText = this.text
                          }, 500)
                          site.gtag('event', 'export', {event_category: 'api link'})
                        }
                      },
                      e => {
                        Iif (e !== this.e.innerText) {
                          this.text = this.e.innerText
                          this.e.innerText = e
                          setTimeout(() => {
                            this.e.innerText = this.text
                          }, 1500)
                        }
                      }
                    )
                  }
                } else {
                  if (v && 'selection' in v) {
                    Iif (!f.time_range)
                      f.time_range =
                        site.data.meta.times[d].value[v.time_range.filtered_index[0]] +
                        ',' +
                        site.data.meta.times[d].value[v.time_range.filtered_index[1]]
                    site.data.export(f as Generic, v.selection.all, true)
                  } else site.data.export(f as Generic, site.data.entities, true, true)
                  site.gtag('event', 'export', {event_category: 'download'})
                }
              }
            : () => {
                Object.keys(this.settings.effects).forEach(k => {
                  this.settings.effects[k] === '' || -1 == this.settings.effects[k]
                    ? site.inputs[k].reset()
                    : site.inputs[k].set(this.settings.effects[k])
                })
              }
          : 'refresh' === this.target
          ? site.global_update
          : 'reset_selection' === this.target
          ? site.global_reset
          : 'reset_storage' === this.target
          ? site.clear_storage
          : () => {
              Iif (this.target in site.inputs) site.inputs[this.target].reset()
            }
      )
  }
  update() {
    let n = +(0 !== this.site.view.selected.length)
    this.site.view.filters.forEach(f => (n += +f.active))
    if (n) {
      this.notification.innerText = n + ''
      this.notification.classList.remove('hidden')
    } else {
      this.notification.classList.add('hidden')
    }
  }
}