Sha256: 02593431c02dbde92a215efbbe8d3629e4aee3386ee76229b4aefaded205dac3

Contents?: true

Size: 1.88 KB

Versions: 133

Compression:

Stored size: 1.88 KB

Contents

import { Controller } from "@hotwired/stimulus";

export default class extends Controller {
  static targets = [ "checkbox", "toggleCheckbox", "toggleLabel", "wrapper" ]
  static classes = [ "unavailable" ]
  
  connect() {
    this.enableSelectAll()
  }
  
  enableSelectAll() {
    if (!this.hasWrapperTarget) { return }
    if (!this.hasUnavailableClass) { return }
    
    this.wrapperTarget.classList.remove(this.unavailableClass)
    this.updateToggle()
  }
  
  selectAllOrNone(event) {
    event.preventDefault()
    event.stopPropagation()
    if (this.allSelected) {
      this.selectNone()
    } else {
      this.selectAll()
    }
    this.updateToggle()
    this.dispatch('toggled')
  }
  
  selectAll() {
    this.checkboxTargets.forEach(checkbox => {
      checkbox.checked = true
    })
  }
  
  selectNone() {
    this.checkboxTargets.forEach(checkbox => {
      checkbox.checked = false
    })
  }
  
  updateToggle() {
    let checkbox = this.toggleCheckboxTarget
    let useAlternateLabel = false
    
    if (this.allSelected) {
      if (checkbox) {
        checkbox.checked = true
        checkbox.indeterminate = false
      }
      useAlternateLabel = true
    } else if (this.selectedValues.length > 0) {
      if (checkbox) {
        checkbox.indeterminate = true
      }
    } else {
      if (checkbox) {
        checkbox.checked = false
        checkbox.indeterminate = false
      }
    }
    
    if (this.hasToggleLabelTarget) {
      this.toggleLabelTarget.dispatchEvent(new CustomEvent(`${this.identifier}:toggle-select-all-label`, { detail: { useAlternate: useAlternateLabel }} ))
    }
  }
  
  get selectedValues() {
    let values = []
    this.checkboxTargets.forEach(checkbox => {
      if (checkbox.checked) {
        values.push(checkbox.value)
      }
    })
    return values
  }
  
  get allSelected() {
    return this.selectedValues.length === this.checkboxTargets.length
  }
}

Version data entries

133 entries across 133 versions & 1 rubygems

Version Path
bullet_train-1.6.2 app/javascript/controllers/select_all_controller.js
bullet_train-1.6.1 app/javascript/controllers/select_all_controller.js
bullet_train-1.6.0 app/javascript/controllers/select_all_controller.js
bullet_train-1.5.2 app/javascript/controllers/select_all_controller.js
bullet_train-1.5.1 app/javascript/controllers/select_all_controller.js
bullet_train-1.5.0 app/javascript/controllers/select_all_controller.js
bullet_train-1.4.11 app/javascript/controllers/select_all_controller.js
bullet_train-1.4.10 app/javascript/controllers/select_all_controller.js
bullet_train-1.4.9 app/javascript/controllers/select_all_controller.js
bullet_train-1.4.8 app/javascript/controllers/select_all_controller.js
bullet_train-1.4.7 app/javascript/controllers/select_all_controller.js
bullet_train-1.4.6 app/javascript/controllers/select_all_controller.js
bullet_train-1.4.5 app/javascript/controllers/select_all_controller.js
bullet_train-1.4.4 app/javascript/controllers/select_all_controller.js
bullet_train-1.4.3 app/javascript/controllers/select_all_controller.js
bullet_train-1.4.2 app/javascript/controllers/select_all_controller.js
bullet_train-1.4.1 app/javascript/controllers/select_all_controller.js
bullet_train-1.4.0 app/javascript/controllers/select_all_controller.js
bullet_train-1.3.25 app/javascript/controllers/select_all_controller.js
bullet_train-1.3.24 app/javascript/controllers/select_all_controller.js