Sha256: 33017090ffe12cd96ba7cd37c720bd99090badfaf494dc8dd95997ea1f574780

Contents?: true

Size: 1.09 KB

Versions: 2

Compression:

Stored size: 1.09 KB

Contents

import { Controller } from 'stimulus'

export default class extends Controller {
  static targets = ['all', 'selectable']

  selectAll (event) {
    const checked = event.target.checked
    this.allTarget.indeterminate = false
    this._setAllCheckboxes(checked)
    this._dispatch('change', { count: this.selectedCount })
  }

  onSelected () {
    this.allTarget.indeterminate = !!this._indeterminate
    this._dispatch('change', { count: this.selectedCount })
  }

  get selectedCount () {
    return this.selected.length
  }

  get selected () {
    return this.selectables.filter((c) => c.checked)
  }

  get selectables () {
    return new Array(...this.selectableTargets)
  }

  _setAllCheckboxes (checked) {
    this.selectables.forEach((el) => {
      const checkbox = el

      if (!checkbox.disabled) {
        checkbox.checked = checked
      }
    })
  }

  get _indeterminate () {
    return this.selected.length !== this.selectableTargets.length && this.selected.length > 0
  }

  _dispatch (name, detail) {
    window.dispatchEvent(new CustomEvent(`rmp:select:${name}`, { bubbles: true, detail }))
  }
}

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rails_mini_profiler-0.5.0 app/javascript/js/select_controller.js
rails_mini_profiler-0 app/javascript/js/select_controller.js