All files / site/inputs combobox.ts

48.04% Statements 197/410
25.41% Branches 61/240
35.13% Functions 13/37
49.86% Lines 191/383

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 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 5722x     2x 2x 2x                       2x 3x 3x 3x   3x     3x       3x     3x 3x 3x 3x 3x 3x   3x 3x   3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x         3x                               3x 3x 3x 3x 3x 3x 2x                     3x           3x           3x 3x 3x 3x 3x 2x                 3x 2x   3x                                                                                                                     3x                                                                                                                                                                                                   2x                                                                                               2x 2x 2x   2x 2x 2x 4x   2x     2x 2x 2x 2x                   3x 3x 3x 3x 3x 3x 3x 2x 2x   3x 3x 2x 2x 2x 2x   3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 2x 2x 2x 2x 2x   3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x               1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 1x   1x 1x 2x 2x 2x 2x       3x                 2x 2x 2x 2x                                 2x 2x                     2x 2x 2x   2x 1x   2x 2x 2x 2x 2x 2x     2x   2x   2x 2x 2x 2x 1x 1x     2x                       2x         2x                 2x 2x   2x     2x   2x 2x     2x 2x 2x 2x 2x 2x 2x 2x 2x         2x       2x      
import {BaseInput} from './index'
import type Community from '../index'
import type {Generic, ObjectIndex, OptionSets, ResourceField} from '../../types'
import {keymap} from '../static_refs'
import {loader, options_filter, set_current_options} from '../common_methods'
import {patterns} from '../patterns'
 
export type ComboboxSpec = {
  strict?: boolean
  numeric?: boolean
  search?: boolean
  multi?: boolean
  accordion?: boolean
  group?: string
  filters?: Generic
}
 
export class InputCombobox extends BaseInput {
  type: 'combobox' = 'combobox'
  hover_index = -1
  cleared_selection = ''
  default: string | number
  expanded = false
  listbox: HTMLElement
  groups: {e: HTMLElement[]; by_name: {[index: string]: HTMLElement}}
  container = document.createElement('div')
  selection: HTMLElement
  input_element: HTMLInputElement
  filter_index: number[]
  current_filter: {[index: string]: string} = {}
  onchange: Function
  value_type: string
  values: ObjectIndex = {}
  display: ObjectIndex = {}
  options: HTMLDivElement[] = []
  option_sets: OptionSets = {}
  current_set = ''
  sensitive = false
  loader?: (...args: any) => void
  filter = options_filter
  reformat_label = false
  constructor(e: HTMLElement, site: Community) {
    super(e, site)
    this.set = this.set.bind(this)
    this.reset = this.reset.bind(this)
    this.resize = this.resize.bind(this)
    this.toggle = this.toggle.bind(this)
    this.highlight = this.highlight.bind(this)
    this.filterer = this.filterer.bind(this)
    this.close = this.close.bind(this)
    this.set_selected = this.set_selected.bind(this)
    this.settings.use_display = this.settings.search || this.settings.multi
    this.listbox = this.e.parentElement.children[1] as HTMLElement
    this.options = [...this.listbox.querySelectorAll('.combobox-option')] as HTMLDivElement[]
    Iif (this.optionSource && this.site.patterns.ids.test(this.optionSource)) {
      this.loader = loader.bind(this)
      e.addEventListener('click', this.loader)
      this.deferred = true
    }
    Iif (this.options.length) {
      this.reformat_label = true
      this.options.forEach((e, i) => {
        this.values[e.dataset.value] = i
        Iif (this.reformat_label) this.reformat_label = e.dataset.value === e.innerText
      })
      const group: NodeListOf<HTMLElement> = this.listbox.querySelectorAll('.combobox-group')
      Iif (group.length) {
        this.groups = {e: [], by_name: {}}
        group.forEach(e => {
          const name = e.dataset.group
          this.groups.e.push(e)
          this.groups.by_name[name] = e
        })
      }
    }
    this.container.className = 'combobox-options-container combobox-component hidden'
    this.site.page.overlay.appendChild(this.container)
    this.container.appendChild(this.listbox)
    this.selection = this.e.firstElementChild.firstElementChild as HTMLElement
    this.input_element = this.e.firstElementChild.lastElementChild as HTMLInputElement
    if (2 === this.e.childElementCount) {
      this.e.lastElementChild.addEventListener(
        'click',
        function (this: InputCombobox) {
          Iif (!this.e.classList.contains('locked')) {
            this.cleared_selection = ''
            this.set([])
            this.input_element.focus()
          }
        }.bind(this)
      )
    }
    this.input_element.addEventListener(
      'focus',
      function (this: HTMLElement) {
        this.classList.add('focused')
      }.bind(this.e)
    )
    this.input_element.addEventListener(
      'blur',
      function (this: HTMLElement) {
        this.classList.remove('focused')
      }.bind(this.e)
    )
    this.listbox.addEventListener('click', this.set)
    window.addEventListener('resize', this.resize)
    this.e.addEventListener('mousedown', this.toggle)
    this.listbox.addEventListener('mouseover', this.highlight)
    if (this.settings.accordion) {
      this.listbox.addEventListener('show.bs.collapse', e => {
        const group = this.hover_index === -1 ? '' : this.options[this.hover_index].dataset.group
        const et = e.target as HTMLElement
        Iif (group !== et.dataset.group) {
          et.firstElementChild.firstElementChild.dispatchEvent(new MouseEvent('mouseover'))
          this.input_element.focus()
        }
      })
    }
    if (this.settings.search) {
      this.input_element.addEventListener('keyup', this.filterer)
    }
    this.input_element.addEventListener(
      'keydown',
      function (this: InputCombobox, e: KeyboardEvent) {
        const action = keymap[e.code as keyof typeof keymap]
        if (action) {
          if ('close' === action) {
            this.close()
          } else if ('select' === action) {
            Iif (!this.expanded) return this.toggle(void 0, this.input_element)
            this.set(e)
          } else Iif ('move' === action) {
            const value = this.input_element.value
            if (this.settings.strict || (this.expanded && '' === value)) {
              e.preventDefault()
              if ('ArrowUp' === e.code) {
                if (this.filter_index && this.filter_index.length) {
                  this.hover_index = this.filter_index.indexOf(this.hover_index) - 1
                  this.hover_index = this.filter_index[0 > this.hover_index ? 0 : this.hover_index]
                } else {
                  this.hover_index = Math.max(0, this.hover_index - 1)
                }
              } else if ('ArrowDown' === e.code) {
                if (this.filter_index && this.filter_index.length) {
                  this.hover_index = this.filter_index.indexOf(this.hover_index) + 1
                  this.hover_index =
                    this.filter_index[
                      this.filter_index.length - 1 < this.hover_index ? this.filter_index.length - 1 : this.hover_index
                    ]
                } else {
                  this.hover_index = Math.min(this.options.length - 1, this.hover_index + 1)
                }
              } else if ('Home' === e.code) {
                this.hover_index = 0
              } else Iif ('End' === e.code) {
                this.hover_index = this.options.length - 1
              }
              if (this.expanded) {
                this.highlight()
              } else {
                this.set(this.hover_index)
              }
            } else Iif (this.site.patterns.number.test(value)) {
              this.set(+value + ('ArrowUp' === e.code ? 1 : -1))
            }
          }
        } else if (!this.expanded) {
          this.toggle(void 0, this.input_element)
        } else {
          this.clear_highlight()
        }
      }.bind(this)
    )
  }
  init() {
    this.options.forEach((e, i) => {
      Iif (this.reformat_label) e.innerText = this.site.data.format_label(e.dataset.value)
      this.display[e.innerText] = i
    })
  }
  set_current = set_current_options
  filterer() {
    const q = this.input_element.value.toLowerCase()
    if ('' === q) {
      this.filter_reset()
    } else {
      this.filter_index = []
      Iif (this.groups) {
        this.groups.e.forEach(g => g.firstElementChild.firstElementChild.classList.add('hidden'))
      }
      this.options.forEach((o, i) => {
        if (!o.classList.contains('hidden') && o.innerText.toLowerCase().includes(q)) {
          o.classList.remove('filter-hidden')
          this.filter_index.push(i)
          const group = o.dataset.group
          Iif (group) {
            this.groups.by_name[group].firstElementChild.firstElementChild.classList.remove('hidden')
            Iif (this.settings.accordion) {
              const g = this.groups.by_name[group]
              g.firstElementChild.nextElementSibling.classList.add('show')
              g.firstElementChild.firstElementChild.classList.remove('collapsed')
              g.firstElementChild.firstElementChild.setAttribute('aria-expanded', 'true')
            }
          }
        } else {
          o.classList.add('filter-hidden')
        }
      })
    }
  }
  highlight(e?: MouseEvent, target?: HTMLElement) {
    Iif (e && !target) target = e.target as HTMLElement
    Iif (!target || target.dataset.value in this.values) {
      Iif (!this.groups) this.settings.accordion = false
      if (target && target.dataset.value) {
        this.hover_index = this.values[target.dataset.value]
      } else Iif (-1 === this.hover_index && Array.isArray(this.source)) {
        this.hover_index = this.values[this.source[0]]
      }
      Iif ('undefined' === typeof this.hover_index) this.hover_index = -1
      const o = this.options[this.hover_index]
      Iif (o) {
        const previous = this.listbox.querySelector('.highlighted')
        Iif (previous) previous.classList.remove('highlighted')
        if (e && 'mouseover' === e.type) {
          target.classList.add('highlighted')
        } else {
          o.classList.add('highlighted')
          Iif (this.settings.accordion) {
            const c = o.parentElement.parentElement
            Iif (!c.classList.contains('show')) {
              c.classList.add('show')
              c.previousElementSibling.firstElementChild.classList.remove('collapsed')
              c.previousElementSibling.firstElementChild.setAttribute('aria-expanded', 'true')
            }
          }
          this.input_element.setAttribute('aria-activedescendant', o.id)
          const port = this.container.getBoundingClientRect(),
            item = o.getBoundingClientRect()
          let top = port.top
          Iif (this.groups && o.dataset.group)
            top += this.groups.by_name[o.dataset.group].firstElementChild.getBoundingClientRect().height
          if (top > item.top) {
            this.container.scrollTo(0, this.container.scrollTop + item.top - top)
          } else Iif (port.bottom < item.bottom) {
            this.container.scrollTo(0, this.container.scrollTop + item.bottom - port.bottom)
          }
        }
      }
    }
  }
  toggle(e?: MouseEvent, target?: HTMLElement) {
    Iif (e && !target) target = e.target as HTMLElement
    Iif (target && (!e || !e.button) && !this.e.classList.contains('disabled') && 'BUTTON' !== target.tagName) {
      if (this.expanded) {
        Iif (target !== this.input_element) this.close()
      } else {
        Iif (this.site.spec.combobox)
          Object.keys(this.site.spec.combobox).forEach(id => {
            Iif (id !== this.id) {
              const ou = this.site.inputs[id] as InputCombobox
              ou.expanded && ou.close()
            }
          })
        this.container.classList.remove('hidden')
        Iif ('' !== this.selection.innerText) this.cleared_selection = this.selection.innerText
        Iif (this.cleared_selection in this.display)
          this.highlight(void 0, this.options[this.display[this.cleared_selection]])
        Iif (this.settings.use_display) this.selection.innerText = ''
        window.addEventListener('click', this.close)
        this.e.setAttribute('aria-expanded', 'true')
        Iif (!e || e.target !== this.input_element) setTimeout(() => this.input_element.focus(), 0)
        this.resize()
        this.expanded = true
      }
    }
  }
  value() {
    return this.source
      ? this.settings.multi
        ? this.source
        : Array.isArray(this.source) && this.source.length
        ? this.source[0]
        : ''
      : this.site.valueOf(this.default)
  }
  close(e?: MouseEvent) {
    Iif (
      this.expanded &&
      (!e ||
        !(e.target as HTMLElement).classList ||
        !(e.target as HTMLElement).classList.contains('combobox-component'))
    ) {
      Iif (
        this.settings.use_display &&
        '' === this.selection.innerText &&
        Array.isArray(this.source) &&
        this.source.length
      )
        this.selection.innerText = this.cleared_selection
      Iif ('' !== this.input_element.value) setTimeout(this.set, 0)
      this.e.setAttribute('aria-expanded', 'false')
      this.expanded = false
      this.container.classList.add('hidden')
      window.removeEventListener('click', this.close)
    }
  }
  resize() {
    const s = this.e.getBoundingClientRect()
    this.container.style.left = s.x + 'px'
    this.container.style.width = s.width + 'px'
    if (window.screen.height / 2 > s.y) {
      this.container.style.top = s.y + s.height + 'px'
      this.container.style.bottom = ''
    } else {
      this.container.style.top = ''
      this.container.style.bottom = -s.y + 'px'
    }
  }
  clear_highlight() {
    Iif (-1 !== this.hover_index) {
      this.options[this.hover_index].classList.remove('highlighted')
      this.hover_index = -1
    }
  }
  filter_reset() {
    if (this.groups)
      this.groups.e.forEach(g => {
        ;(this.settings.accordion ? g.firstElementChild.firstElementChild : g).classList.remove('hidden')
      })
    this.input_element.value = ''
    this.filter_index = []
    this.options.forEach((o, i) => {
      if (!o.classList.contains('hidden')) this.filter_index.push(i)
    })
    this.listbox.querySelectorAll('.filter-hidden').forEach(o => o.classList.remove('filter-hidden'))
  }
  set_selected(value: string | number) {
    if (value in this.values) {
      const option = this.options[this.values[value] as number]
      option.classList.add('selected')
      option.setAttribute('aria-selected', 'true')
    }
  }
  static create(
    site: Community,
    label: string,
    options?: string[] | {[index: string]: string[]},
    settings?: Generic,
    id?: string
  ) {
    id = id || 'created_combobox_' + ++site.page.elementCount
    const main = document.createElement('div')
    let e = document.createElement('div'),
      div = document.createElement('div'),
      input = document.createElement('input'),
      lab = document.createElement('label')
    if (settings) {
      if (!site.spec.combobox) site.spec.combobox = {}
      site.spec.combobox[id] = settings
    }
    e.className = 'wrapper combobox-wrapper'
    if (settings && settings.floating) {
      e.appendChild(div)
      div.className = 'form-floating'
      div.dataset.of = id
      e = div
    }
    e.appendChild(main)
    main.id = id
    main.dataset.autotype = 'combobox'
    main.className = 'auto-input form-select combobox combobox-component'
    main.role = 'combobox'
    main.appendChild((div = document.createElement('div')))
    div.className = 'combobox-selection combobox-component'
    div.appendChild(document.createElement('span'))
    div.lastElementChild.className = 'combobox-component'
    div.lastElementChild.setAttribute('aria-live', 'assertive')
    div.lastElementChild.setAttribute('aria-atomic', 'true')
    div.lastElementChild.role = 'log'
    div.appendChild(input)
    input.setAttribute('aria-haspopup', 'listbox')
    input.setAttribute('aria-expanded', 'false')
    input.setAttribute('aria-controls', id + '-listbox')
    input.className = 'combobox-input combobox-component'
    input.type = 'text'
    input.role = 'combobox'
    input.id = id + '-input'
    input.autocomplete = 'off'
    if (settings && settings.clearable) {
      const button = document.createElement('button')
      e.firstElementChild.appendChild(button)
      button.type = 'button'
      button.className = 'btn-close'
      button.title = 'clear selection'
    }
    e.appendChild((div = document.createElement('div')))
    div.className = 'combobox-options combobox-component'
    div.setAttribute('aria-labelledby', id + '-label')
    div.role = 'listbox'
    div.tabIndex = -1
    div.id = id + '-listbox'
    e.appendChild(lab)
    lab.id = id + '-label'
    lab.innerText = label
    lab.setAttribute('for', id + '-input')
    const u = new InputCombobox(main, site)
    site.inputs[id] = u
    let n = 0
    if (options)
      Iif (Array.isArray(options)) {
        options.forEach(o => {
          const l = site.data.format_label(o)
          u.display[l] = n
          u.values[o] = n++
          u.add(o, l)
        })
      } else {
        u.groups = {e: [], by_name: {}}
        Object.keys(options).forEach(k => {
          const g = options[k]
          const e = document.createElement('div'),
            id = u.id + '_group_' + k.replace(patterns.seps, '-')
          e.dataset.group = k
          e.className = 'combobox-group combobox-component'
          e.setAttribute('aria-labelledby', id)
          e.appendChild((lab = document.createElement('label')))
          lab.dataset.for = id
          lab.innerText = k
          lab.id = id
          lab.className = 'combobox-group-label combobox-component'
          u.groups.by_name[k] = e
          u.groups.e.push(e)
          g.forEach(o => e.appendChild(u.add(o, o, true)))
          u.listbox.appendChild(e)
        })
        Object.keys(u.groups.by_name).forEach(g => {
          u.groups.by_name[g].querySelectorAll('.combobox-option').forEach((c: HTMLElement) => {
            ;(u.options as HTMLElement[]).push(c)
            c.dataset.group = g
            u.values[c.dataset.value] = n
            u.display[c.innerText] = n++
          })
        })
      }
    return u
  }
  get() {
    const s: string[] = []
    this.listbox.querySelectorAll('.selected').forEach((o: HTMLElement) => s.push(o.dataset.value))
    this.source = s
    this.site.request_queue(false, this.id)
  }
  set(v?: MouseEvent | KeyboardEvent | string | number | (string | number)[], toggle?: boolean): void {
    Iif ('number' !== typeof v && !v) v = this.input_element.value
    let update = false,
      i = -1
    Iif (!Array.isArray(v) && 'object' === typeof v) {
      const t = v.target as HTMLElement
      Iif ((this.settings.accordion ? 'BUTTON' : 'LABEL') === t.tagName) return void 0
      i = this.hover_index
      Iif (
        -1 !== i &&
        (this.options[i].classList.contains('hidden') || this.options[i].classList.contains('filter-hidden'))
      )
        i = -1
      v =
        -1 === i
          ? 'INPUT' === t.tagName
            ? this.input_element.value
            : t.dataset.value || t.innerText
          : this.options[i].dataset.value || this.options[i].innerText
      toggle = this.settings.multi
    }
    this.filter_reset()
    Iif (Array.isArray(v)) {
      if (this.settings.multi) {
        this.listbox.querySelectorAll('.selected').forEach(e => {
          e.classList.remove('selected')
          e.setAttribute('aria-selected', 'false')
        })
        this.source = -1 === v[0] ? [] : v
        v.forEach(this.set_selected)
        update = true
      } else v = v[0]
    }
    if (!Array.isArray(this.source)) this.source = []
    if (!Array.isArray(v)) {
      Iif (this.settings.strict && 'string' === typeof v && !(v in this.values) && this.site.patterns.number.test(v))
        v = +v
      if ('number' !== this.value_type && 'number' === typeof v && this.options[v]) {
        v = this.options[v].dataset.value
      }
      if ('string' === typeof v && v in this.display) v = this.options[this.display[v]].dataset.value
      Iif (this.settings.strict && !(v in this.values)) v = this.default
      i = this.source.indexOf(v)
      if (-1 === i) {
        update = true
        Iif (-1 === v || '' === v) {
          this.source = []
        } else {
          Iif (this.settings.multi) {
            this.source.push(v)
          } else this.source[0] = v
        }
        if (v in this.values) {
          if (!this.settings.multi) {
            const selected = this.listbox.querySelector('.selected')
            if (selected) {
              selected.classList.remove('selected')
              selected.setAttribute('aria-selected', 'false')
            }
          }
          this.set_selected(v)
        }
      } else IEif (toggle) {
        update = true
        this.source.splice(i, 1)
        Iif (v in this.values) {
          const selection = this.options[this.values[v]]
          selection.classList.remove('selected')
          selection.setAttribute('aria-selected', 'false')
        }
      }
    }
    Iif (!this.settings.multi && this.expanded) {
      this.input_element.focus()
      this.close()
      this.filter_reset()
    }
    let display: string | HTMLElement = this.source.length
      ? this.settings.multi
        ? this.source.length + ' of ' + this.options.length + ' selected'
        : this.source[0] in this.values
        ? this.options[this.values[this.source[0]]]
        : this.settings.strict || -1 === this.source[0]
        ? ''
        : this.source[0] + ''
      : ''
    if ('string' !== typeof display) {
      display = display.firstChild ? display.firstChild.textContent : display.innerText
    }
    Iif (this.settings.use_display) {
      this.selection.innerText = display
    } else {
      this.input_element.value = display
    }
    Iif (this.onchange) this.onchange()
    if (update) this.site.request_queue(false, this.id)
  }
  add(value: string, display?: string, noadd?: boolean, meta?: ResourceField) {
    const e = document.createElement('div')
    e.id = this.id + '_' + value
    e.role = 'option'
    e.setAttribute('aria-selected', 'false')
    e.tabIndex = 0
    e.className = 'combobox-option combobox-component'
    e.dataset.value = value
    e.innerText = display || this.site.data.format_label(value)
    Iif (meta && meta.info) {
      e.appendChild(document.createElement('p'))
      e.lastElementChild.className = 'combobox-option-description combobox-component'
      ;(e.lastElementChild as HTMLElement).innerText = meta.info.description || meta.info.short_description || ''
    }
    Iif (!noadd) {
      this.listbox.appendChild(e)
      this.options.push(e)
    }
    return e
  }
}