Sha256: 2fa4920f53be1c34f44d86dc44525792284d6f3b919eb0fda1b972fe22c0925b

Contents?: true

Size: 1.91 KB

Versions: 48

Compression:

Stored size: 1.91 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
    this.itemCheckboxTargets.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

48 entries across 48 versions & 1 rubygems

Version Path
avo-3.15.1 app/javascript/js/controllers/item_select_all_controller.js
avo-3.15.0 app/javascript/js/controllers/item_select_all_controller.js
avo-3.14.5 app/javascript/js/controllers/item_select_all_controller.js
avo-3.14.4 app/javascript/js/controllers/item_select_all_controller.js
avo-3.14.3 app/javascript/js/controllers/item_select_all_controller.js
avo-3.14.2 app/javascript/js/controllers/item_select_all_controller.js
avo-3.14.1 app/javascript/js/controllers/item_select_all_controller.js
avo-3.14.0 app/javascript/js/controllers/item_select_all_controller.js
avo-3.13.7 app/javascript/js/controllers/item_select_all_controller.js
avo-3.13.6 app/javascript/js/controllers/item_select_all_controller.js
avo-3.13.5 app/javascript/js/controllers/item_select_all_controller.js
avo-3.13.4 app/javascript/js/controllers/item_select_all_controller.js
avo-3.13.3 app/javascript/js/controllers/item_select_all_controller.js
avo-3.13.2 app/javascript/js/controllers/item_select_all_controller.js
avo-3.13.1 app/javascript/js/controllers/item_select_all_controller.js
avo-3.13.0 app/javascript/js/controllers/item_select_all_controller.js
avo-3.12.0 app/javascript/js/controllers/item_select_all_controller.js
avo-3.11.10 app/javascript/js/controllers/item_select_all_controller.js
avo-3.11.9 app/javascript/js/controllers/item_select_all_controller.js
avo-3.11.8 app/javascript/js/controllers/item_select_all_controller.js