Sha256: c56821a8ae463c9f603f38a364b4f5e6f288ed552b775b49d8fdfc43f13121a8

Contents?: true

Size: 1.37 KB

Versions: 11

Compression:

Stored size: 1.37 KB

Contents

import Combobox from "models/combobox/base"
import { applyFilter, nextFrame, debounce } from "helpers"
import { get } from "vendor/requestjs"

Combobox.Filtering = Base => class extends Base {
  filter(event) {
    if (this._isAsync) {
      this._debouncedFilterAsync(event)
    } else {
      this._filterSync(event)
    }
  }

  _initializeFiltering() {
    this._debouncedFilterAsync = debounce(this._debouncedFilterAsync.bind(this))
  }

  _debouncedFilterAsync(event) {
    this._filterAsync(event)
  }

  async _filterAsync(event) {
    const q = this._actingCombobox.value.trim()

    await get(this.asyncSrcValue, { responseKind: "turbo-stream", query: { q } })

    this._afterTurboStreamRender(() => this._commitFilter(q, event))
  }

  _filterSync(event) {
    const query = this._actingCombobox.value.trim()

    this.open()

    this._allOptionElements.forEach(applyFilter(query, { matching: this.filterableAttributeValue }))

    this._commitFilter(query, event)
  }

  _commitFilter(query, event) {
    const isDeleting = event.inputType === "deleteContentBackward"

    if (this._isValidNewOption(query, { ignoreAutocomplete: isDeleting })) {
      this._selectNew(query)
    } else if (isDeleting) {
      this._deselect()
    } else {
      this._select(this._visibleOptionElements[0])
    }
  }

  async _afterTurboStreamRender(callback) {
    await nextFrame()
    callback()
  }
}

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
hotwire_combobox-0.1.32 app/assets/javascripts/models/combobox/filtering.js
hotwire_combobox-0.1.31 app/assets/javascripts/models/combobox/filtering.js
hotwire_combobox-0.1.30 app/assets/javascripts/models/combobox/filtering.js
hotwire_combobox-0.1.29 app/assets/javascripts/models/combobox/filtering.js
hotwire_combobox-0.1.28 app/assets/javascripts/models/combobox/filtering.js
hotwire_combobox-0.1.27 app/assets/javascripts/models/combobox/filtering.js
hotwire_combobox-0.1.25 app/assets/javascripts/models/combobox/filtering.js
hotwire_combobox-0.1.24 app/assets/javascripts/models/combobox/filtering.js
hotwire_combobox-0.1.23 app/assets/javascripts/models/combobox/filtering.js
hotwire_combobox-0.1.22 app/assets/javascripts/models/combobox/filtering.js
hotwire_combobox-0.1.21 app/assets/javascripts/models/combobox/filtering.js