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.8.4 app/javascript/controllers/select_all_controller.js
bullet_train-1.8.3 app/javascript/controllers/select_all_controller.js
bullet_train-1.8.2 app/javascript/controllers/select_all_controller.js
bullet_train-1.8.1 app/javascript/controllers/select_all_controller.js
bullet_train-1.8.0 app/javascript/controllers/select_all_controller.js
bullet_train-1.7.23 app/javascript/controllers/select_all_controller.js
bullet_train-1.7.22 app/javascript/controllers/select_all_controller.js
bullet_train-1.7.21 app/javascript/controllers/select_all_controller.js
bullet_train-1.7.20 app/javascript/controllers/select_all_controller.js
bullet_train-1.7.19 app/javascript/controllers/select_all_controller.js
bullet_train-1.7.18 app/javascript/controllers/select_all_controller.js
bullet_train-1.7.17 app/javascript/controllers/select_all_controller.js
bullet_train-1.7.16 app/javascript/controllers/select_all_controller.js
bullet_train-1.7.15 app/javascript/controllers/select_all_controller.js
bullet_train-1.7.14 app/javascript/controllers/select_all_controller.js
bullet_train-1.7.13 app/javascript/controllers/select_all_controller.js
bullet_train-1.7.12 app/javascript/controllers/select_all_controller.js
bullet_train-1.7.11 app/javascript/controllers/select_all_controller.js
bullet_train-1.7.10 app/javascript/controllers/select_all_controller.js
bullet_train-1.7.9 app/javascript/controllers/select_all_controller.js