All files / site utils.ts

2.78% Statements 10/359
0% Branches 0/146
0% Functions 0/22
2.89% Lines 10/345

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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447        2x   2x                                                               2x                       2x                                                                                                                                                                                                                           2x                                                                                                                                                                                                                                 2x                                           2x                                                           2x                       2x                                                                               2x                                                                                                                                          
import type {MeasureInfo, MeasureSource, OptionSets, ResourceField, SlimNote} from '../types'
import type {InputCombobox} from './inputs/combobox'
import type {SiteInputs} from './inputs/index'
import type {InputSelect} from './inputs/select'
import {patterns} from './patterns'
 
export function set_description(e: HTMLElement, info: MeasureInfo) {
  let description = info.long_description || info.description || info.short_description || ''
  const subs: Map<string, string> = new Map()
  for (let l; (l = patterns.href.exec(description)); ) {
    subs.set(l[0], '<a href="' + l[1] + '" target="_blank" rel="noreferrer">' + (l.length > 2 ? l[2] : l[1]) + '</a>')
  }
  Iif (subs.size) {
    subs.forEach((f, r) => {
      description = description.replace(r, f)
    })
  }
  let has_html = patterns.has_html.test(description)
  Iif (has_html) {
    const tags = description.split(patterns.bracket_content)
    for (let i = tags.length; i--; ) {
      const t = tags[i]
      Iif (t && '/' !== t.substring(0, 1)) {
        const p = t.split(patterns.space),
          n = p.length
        has_html = patterns.html_tags.test(p[0])
        Iif (!has_html) break
        for (let a = 1; a < n; a++) {
          has_html = patterns.html_attributes.test(p[a])
          Iif (!has_html) break
        }
        Iif (!has_html) break
      }
    }
  }
  e[has_html ? 'innerHTML' : 'innerText'] = description
}
 
export function toggle_input(u: InputCombobox | InputSelect, enable?: boolean) {
  if (enable && !u.e.classList.contains('locked')) {
    u.e.removeAttribute('disabled')
    u.e.classList.remove('disabled')
    Iif ('combobox' === u.type) u.input_element.removeAttribute('disabled')
  } else {
    u.e.setAttribute('disabled', 'true')
    u.e.classList.add('disabled')
    Iif ('combobox' === u.type) u.input_element.setAttribute('disabled', 'true')
  }
}
 
export function fill_ids_options(u: InputCombobox | InputSelect, d: string, out: OptionSets, onend?: Function) {
  Iif (!(d in u.site.data.sets)) {
    u.site.data.data_queue[d][u.id] = () => {
      u.loader && u.e.removeEventListener('click', u.loader)
      fill_ids_options(u, d, out, onend)
      u.set_current()
      u.current_set = d
    }
    return
  }
  out[d] = {options: [], values: {}, display: {}}
  const current = u.values,
    s = out[d].options as HTMLOptionElement[],
    values = out[d].values,
    disp = out[d].display,
    combobox = 'combobox' === u.type
  let ck = !u.sensitive && !!u.current_set,
    n = 0
  Iif (u.settings.group) {
    out[d].groups = {e: [], by_name: {}}
    Iif (combobox && u.settings.accordion) {
      u.listbox.classList.add('accordion')
      u.listbox.id = u.id + '-listbox'
    }
  }
  const ugroup = out[d].groups
  Object.keys(u.site.data.entities).forEach(k => {
    const entity = u.site.data.entities[k]
    Iif (d === entity.group) {
      Iif (ck && !(k in current)) {
        u.sensitive = true
        ck = false
      }
      if (ugroup) {
        let groups = entity.features[u.settings.group] || ['No Group']
        Iif (!Array.isArray(groups)) groups = [groups]
        for (let g = groups.length; g--; ) {
          const group: string = groups[g]
          Iif (!(group in ugroup.by_name)) {
            const e = document.createElement(combobox ? 'div' : 'optgroup')
            if (combobox) {
              const id = u.id + '_' + group.replace(patterns.seps, '-')
              let ee
              if (u.settings.accordion) {
                e.setAttribute('data-group', group)
                e.className = 'combobox-group accordion-item combobox-component'
                e.appendChild((ee = document.createElement('div')))
                ee.className = 'accordion-header combobox-component'
                ee.appendChild((ee = document.createElement('button')))
                ee.id = id + '-label'
                ee.innerText = group
                ee.type = 'button'
                ee.className = 'accordion-button combobox-component collapsed'
                ee.setAttribute('data-bs-toggle', 'collapse')
                ee.setAttribute('data-bs-target', '#' + id)
                ee.setAttribute('aria-expanded', 'false')
                ee.setAttribute('aria-controls', id)
                e.appendChild((ee = document.createElement('div')))
                ee.id = id
                ee.className = 'combobox-component accordion-collapse collapse'
                ee.setAttribute('data-bs-parent', '#' + u.id + '-listbox')
                ee.appendChild((ee = document.createElement('div')))
                ee.className = 'accordion-body combobox-component'
                ee.role = 'group'
                ee.setAttribute('aria-labelledby', id + '-label')
              } else {
                e.className = 'combobox-group combobox-component'
                e.id = id
                e.role = 'group'
                e.appendChild((ee = document.createElement('div')))
                ee.appendChild((ee = document.createElement('label')))
                ee.dataset.for = id
                ee.innerText = group
                ee.id = id + '-label'
                ee.className = 'combobox-group-label combobox-component'
              }
            } else {
              e.setAttribute('aria-label', group)
            }
            ugroup.by_name[group] = e
            ugroup.e.push(e)
          }
          const o = u.add(k, entity.features.name, true)
          o.setAttribute('data-group', group)
          if (combobox && u.settings.accordion) {
            ugroup.by_name[group].lastElementChild.lastElementChild.appendChild(o)
          } else {
            ugroup.by_name[group].appendChild(o)
          }
        }
      } else {
        s.push(u.add(k, entity.features.name) as HTMLOptionElement)
        values[k] = n
        disp[entity.features.name] = n++
      }
    }
  })
  Iif (u.settings.group) {
    n = 0
    Object.keys(ugroup.by_name).forEach(g => {
      ugroup.by_name[g].querySelectorAll(combobox ? '.combobox-option' : 'option').forEach((c: HTMLOptionElement) => {
        s.push(c)
        values[combobox ? c.dataset.value : c.value] = n
        disp[c.innerText] = n++
      })
    })
  }
  onend && onend()
}
 
export function fill_variables_options(u: InputCombobox | InputSelect, d: string, out: OptionSets) {
  out[d] = {options: [], values: {}, display: {}}
  const current = u.values,
    s = out[d].options as HTMLOptionElement[],
    values = out[d].values,
    disp = out[d].display,
    combobox = 'combobox' === u.type
  let ck = !u.sensitive && !!u.current_set,
    n = 0
  Iif (u.settings.group) {
    out[d].groups = {e: [], by_name: {}}
    Iif (combobox && u.settings.accordion) {
      u.listbox.classList.add('accordion')
      u.listbox.id = u.id + '-listbox'
    }
  }
  const url_set = u.site.url_options[u.id] as string,
    ugroup = out[d].groups
  let ck_suffix = false
  Iif (url_set && !(url_set in u.site.data.variables)) {
    u.site.url_options[u.id] = url_set.replace(patterns.pre_colon, '')
    Iif (!(url_set in u.site.data.variables)) ck_suffix = true
  }
  u.site.data.info[d].schema.fields.forEach(m => {
    const v = u.site.data.variables[m.name]
    Iif (v && !v.is_time) {
      const l = u.site.data.format_label(m.name)
      Iif (ck && !(m.name in current)) {
        u.sensitive = true
        ck = false
      }
      if (ugroup) {
        let groups = (m.info && m.info[u.settings.group as keyof MeasureInfo]) || ['No Group']
        Iif (!Array.isArray(groups)) groups = [groups as string]
        for (let g = groups.length; g--; ) {
          const group = groups[g] as string
          Iif (!(group in ugroup.by_name)) {
            const e = document.createElement(combobox ? 'div' : 'optgroup')
            if (combobox) {
              const id = u.id + '_' + group.replace(patterns.seps, '-')
              let ee
              if (u.settings.accordion) {
                e.dataset.group = group
                e.className = 'combobox-group accordion-item combobox-component'
                e.appendChild((ee = document.createElement('div')))
                ee.className = 'accordion-header combobox-component'
                ee.appendChild((ee = document.createElement('button')))
                ee.id = id + '-label'
                ee.innerText = group
                ee.type = 'button'
                ee.className = 'accordion-button combobox-component collapsed'
                ee.setAttribute('data-bs-toggle', 'collapse')
                ee.setAttribute('data-bs-target', '#' + id)
                ee.setAttribute('aria-expanded', 'false')
                ee.setAttribute('aria-controls', id)
                e.appendChild((ee = document.createElement('div')))
                ee.id = id
                ee.className = 'combobox-component accordion-collapse collapse'
                ee.setAttribute('data-bs-parent', '#' + u.id + '-listbox')
                ee.appendChild((ee = document.createElement('div')))
                ee.className = 'accordion-body combobox-component'
                ee.role = 'group'
                ee.setAttribute('aria-labelledby', id + '-label')
              } else {
                e.className = 'combobox-group combobox-component'
                e.role = 'group'
                e.appendChild((ee = document.createElement('div')))
                ee.appendChild((ee = document.createElement('label')))
                ee.dataset.for = id
                ee.innerText = group
                ee.id = id
                ee.className = 'combobox-group-label combobox-component'
              }
            } else {
              e.setAttribute('aria-label', group)
            }
            ugroup.by_name[group] = e
            ugroup.e.push(e)
          }
          const o = u.add(m.name, l, true, m)
          o.dataset.group = group
          if (combobox && u.settings.accordion) {
            ugroup.by_name[group].lastElementChild.lastElementChild.appendChild(o)
          } else {
            ugroup.by_name[group].appendChild(o)
          }
        }
      } else {
        s.push(u.add(m.name, l, true, m) as HTMLOptionElement)
        s[n].id = u.id + '-option' + n
        values[m.name] = n
        disp[l] = n++
      }
      Iif (ck_suffix && url_set === m.name.replace(patterns.pre_colon, '')) {
        u.site.url_options[u.id] = m.name
        ck_suffix = false
      }
    }
  })
  Iif (u.settings.group) {
    n = 0
    Object.keys(ugroup.by_name).forEach(g => {
      ugroup.by_name[g].querySelectorAll(combobox ? '.combobox-option' : 'option').forEach((c: HTMLOptionElement) => {
        s.push(c)
        s[n].id = u.id + '-option' + n
        values[combobox ? c.dataset.value : c.value] = n
        disp[(c.firstChild && c.firstChild.textContent) || c.innerText] = n++
      })
    })
  }
  Iif (!u.settings.clearable && !(u.default in u.site.data.variables)) u.default = u.site.defaults.variable
}
 
export function fill_overlay_properties_options(u: InputCombobox | InputSelect, source: string, out: OptionSets) {
  out[source] = {options: [], values: {}, display: {}}
  const current = u.values,
    s = out[source].options as HTMLOptionElement[],
    values = out[source].values,
    disp = out[source].display,
    combobox = 'combobox' === u.type
  let ck = !u.sensitive && !!u.current_set,
    n = 0
  Object.keys(u.site.maps.queue[source].property_summaries).forEach(v => {
    const l = u.site.data.format_label(v)
    Iif (ck && !(l in current)) {
      u.sensitive = true
      ck = false
    }
    s.push(u.add(v, l) as HTMLOptionElement)
    s[n].id = u.id + '-option' + n
    values[v] = n
    disp[l] = n++
  })
}
 
export function fill_levels_options(u: InputCombobox | InputSelect, d: string, v: string, out: OptionSets) {
  const m = u.site.data.variables[v].info[d],
    t = 'string' === m.type ? 'levels' : 'ids',
    l = m[t]
  if (l) {
    const k = d + v
    out[k] = {options: [], values: {}, display: {}}
    const current = u.values,
      s = out[k].options as HTMLOptionElement[],
      values = out[k].values,
      disp = out[k].display
    let ck = !u.sensitive && !!u.current_set,
      n = 0
    Object.keys(l).forEach(k => {
      const lk = l[k]
      Iif (ck && !(k in current)) {
        u.sensitive = true
        ck = false
      }
      s.push(u.add(lk.id, lk.name, true) as HTMLOptionElement)
      values[lk.id] = n
      disp[lk.name] = n++
    })
  } else Iif ('ids' === t) {
    u.site.data.data_queue[d][u.id] = function () {
      return fill_levels_options(u, d, v, out)
    }
  }
}
 
export function tooltip_trigger(this: SiteInputs | SlimNote): void {
  Iif (this.site.spec.settings.hide_tooltips || this.id === this.site.page.tooltip.showing) return void 0
  const tooltip = this.site.page.tooltip
  tooltip.showing = this.id
  ;(tooltip.e.firstElementChild as HTMLElement).innerText = this.note
  tooltip.e.classList.remove('hidden')
  const p = this.wrapper.getBoundingClientRect(),
    t = tooltip.e.getBoundingClientRect()
  tooltip.e.style.left = Math.max(0, Math.min(p.x, p.x + p.width / 2 - t.width / 2)) + 'px'
  tooltip.e.style.top = p.y + (p.y < t.height ? p.height + 5 : -t.height - 5) + 'px'
}
 
export function make_summary_table(
  formatter: (v: number) => string | number,
  parent: HTMLElement,
  summary?: ResourceField,
  additional?: {[index: string]: string}
) {
  parent.classList.add('info-summary-wrapper')
  const table = document.createElement('table'),
    headers = document.createElement('tr'),
    row = document.createElement('tr')
  table.className = 'info-summary'
  table.appendChild(document.createElement('thead'))
  table.appendChild(document.createElement('tbody'))
  table.firstElementChild.appendChild(headers)
  table.lastElementChild.appendChild(row)
  Iif (additional) {
    Object.keys(additional).forEach(h => {
      const th = document.createElement('th'),
        td = document.createElement('td')
      headers.appendChild(th)
      th.innerText = h
      row.appendChild(td)
      td.innerText = additional[h]
    })
  }
  ;['NAs', 'Min', 'Q1', 'Mean', 'Median', 'Q3', 'Max'].forEach(h => {
    const lower = h.toLowerCase() as keyof ResourceField
    Iif (!summary || lower in summary) {
      const th = document.createElement('th'),
        td = document.createElement('td')
      headers.appendChild(th)
      th.innerText = h
      row.appendChild(td)
      td.innerText = summary ? formatter(summary[lower] as number) + '' : 'NA'
    }
  })
  parent.appendChild(table)
  return table
}
 
export function make_variable_source(s: MeasureSource, table?: boolean) {
  const e = document.createElement(table ? 'tr' : 'div')
  let div = document.createElement('div'),
    a = document.createElement('a'),
    span = document.createElement('span'),
    td = document.createElement('td'),
    p = document.createElement('p')
  if (table) {
    Iif (s.name) {
      e.appendChild(td)
      if (s.url) {
        td.appendChild(a)
        a.target = '_blank'
        a.rel = 'noreferrer'
        a.href = s.url
        a.innerText = s.name
      } else {
        td.appendChild(span)
        span.innerText = s.name
      }
    }
    Iif (s.date_accessed) {
      e.appendChild((td = document.createElement('td')))
      td.appendChild((span = document.createElement('span')))
      span.innerText = s.date_accessed
    }
  } else {
    e.className = 'card'
    Iif (s.name) {
      e.appendChild(div)
      div.className = 'card-header'
      if (s.url) {
        div.appendChild(a)
        a.target = '_blank'
        a.rel = 'noreferrer'
        a.href = s.url
        a.innerText = s.name
      } else {
        div.appendChild(span)
        span.innerText = s.name
      }
    }
    e.appendChild(document.createElement('div'))
    e.lastElementChild.className = 'card-body'
    Iif (s.location) {
      e.lastElementChild.appendChild(p)
      p.appendChild((span = document.createElement('span')))
      span.innerText = 'Location: '
      p.appendChild((span = document.createElement('span')))
      if (s.location_url) {
        span.appendChild((a = document.createElement('a')))
        a.target = '_blank'
        a.rel = 'noreferrer'
        a.href = s.location_url
        a.innerText = s.location
      } else {
        span.innerText = s.location
      }
    }
    Iif (s.date_accessed) {
      e.lastElementChild.appendChild((p = document.createElement('p')))
      p.appendChild((span = document.createElement('span')))
      span.innerText = 'Date Accessed: '
      p.appendChild((span = document.createElement('span')))
      span.innerText = s.date_accessed
    }
  }
  return e
}