Sha256: 094c1b2d85eb0eedc1f9c41185fa309fae952b5d2de5e45e22682a09e1ad1eea

Contents?: true

Size: 1.99 KB

Versions: 169

Compression:

Stored size: 1.99 KB

Contents

import { Controller } from '@hotwired/stimulus'

export default class extends Controller {
  static targets = ['itemCheckbox', 'checkbox', 'selectAllOverlay', 'unselectedMessage', 'selectedMessage']

  static values = {
    pageCount: Number,
    selectedAll: Boolean,
    selectedAllQuery: String,
  }

  connect() {
    this.resourceName = this.element.dataset.resourceName
  }

  toggle(event) {
    const checked = !!event.target.checked
    document.querySelectorAll(`[data-controller="item-selector"][data-resource-name="${this.resourceName}"] input[type=checkbox]`)
      .forEach((checkbox) => checkbox.checked !== checked && checkbox.click())

    if (this.selectAllEnabled()) {
      this.selectAllOverlay(checked)

      // When de-selecting everything, ensure the selectAll toggle is false and hide overlay.
      if (!checked) {
        this.resetUnselected()
      }
    }
  }

  selectRow() {
    let allSelected = true
    // eslint-disable-next-line no-return-assign
    this.itemCheckboxTargets.forEach((checkbox) => allSelected = allSelected && checkbox.checked)
    this.checkboxTarget.checked = allSelected

    if (this.selectAllEnabled()) {
      this.selectAllOverlay(allSelected)
      this.resetUnselected()
    }
  }

  selectAll(event) {
    event.preventDefault()

    this.selectedAllValue = !this.selectedAllValue
    this.unselectedMessageTarget.classList.toggle('hidden')
    this.selectedMessageTarget.classList.toggle('hidden')
  }

  resetUnselected() {
    this.selectedAllValue = false
    this.unselectedMessageTarget.classList.remove('hidden')
    this.selectedMessageTarget.classList.add('hidden')
  }

  selectAllOverlay(show) {
    if (show) {
      this.selectAllOverlayTarget.classList.remove('hidden')
    } else {
      this.selectAllOverlayTarget.classList.add('hidden')
    }
  }

  // True if there are more pages available and if query encryption run successfully
  selectAllEnabled() {
    return this.pageCountValue > 1 && this.selectedAllQueryValue !== 'select_all_disabled'
  }
}

Version data entries

169 entries across 169 versions & 1 rubygems

Version Path
avo-3.1.4 app/javascript/js/controllers/item_select_all_controller.js
avo-3.1.3 app/javascript/js/controllers/item_select_all_controller.js
avo-3.1.2 app/javascript/js/controllers/item_select_all_controller.js
avo-3.1.1 app/javascript/js/controllers/item_select_all_controller.js
avo-3.1.0 app/javascript/js/controllers/item_select_all_controller.js
avo-3.0.8 app/javascript/js/controllers/item_select_all_controller.js
avo-2.46.0 app/javascript/js/controllers/item_select_all_controller.js
avo-3.0.7 app/javascript/js/controllers/item_select_all_controller.js
avo-3.0.6 app/javascript/js/controllers/item_select_all_controller.js
avo-3.0.5 app/javascript/js/controllers/item_select_all_controller.js
avo-3.0.4 app/javascript/js/controllers/item_select_all_controller.js
avo-3.0.3 app/javascript/js/controllers/item_select_all_controller.js
avo-2.45.0 app/javascript/js/controllers/item_select_all_controller.js
avo-3.0.2 app/javascript/js/controllers/item_select_all_controller.js
avo-2.44.0 app/javascript/js/controllers/item_select_all_controller.js
avo-2.43.0 app/javascript/js/controllers/item_select_all_controller.js
avo-2.42.2 app/javascript/js/controllers/item_select_all_controller.js
avo-2.42.1 app/javascript/js/controllers/item_select_all_controller.js
avo-2.42.0 app/javascript/js/controllers/item_select_all_controller.js
avo-2.41.0 app/javascript/js/controllers/item_select_all_controller.js