Sha256: 78afd87296b335ead233ef6e47a6f7ec1b2ca9318bc790baef4a6d45e9753721

Contents?: true

Size: 1.51 KB

Versions: 76

Compression:

Stored size: 1.51 KB

Contents

import { Controller } from 'stimulus'

export default class extends Controller {
  static targets = ['panel']

  checkbox = {}

  get actionsPanelPresent() {
    return this.actionsButtonElement !== null
  }

  get currentIds() {
    try {
      return JSON.parse(this.stateHolderElement.dataset.selectedResources)
    } catch (error) {
      return []
    }
  }

  set currentIds(value) {
    this.stateHolderElement.dataset.selectedResources = JSON.stringify(value)

    if (this.actionsPanelPresent) {
      if (value.length > 0) {
        this.enableActionsPanel()
      } else {
        this.disableActionsPanel()
      }
    }
  }

  connect() {
    this.resourceName = this.element.dataset.resourceName
    this.resourceId = this.element.dataset.resourceId
    this.actionsButtonElement = document.querySelector(`[data-actions-dropdown-button="${this.resourceName}"]`)
    this.stateHolderElement = document.querySelector(`[data-selected-resources-name="${this.resourceName}"]`)
  }

  addToSelected() {
    const ids = this.currentIds

    ids.push(this.resourceId)

    this.currentIds = ids
  }

  removeFromSelected() {
    this.currentIds = this.currentIds.filter((item) => item.toString() !== this.resourceId)
  }

  toggle(event) {
    this.checkbox = event.target

    if (this.checkbox.checked) {
      this.addToSelected()
    } else {
      this.removeFromSelected()
    }
  }

  enableActionsPanel() {
    this.actionsButtonElement.disabled = false
  }

  disableActionsPanel() {
    this.actionsButtonElement.disabled = true
  }
}

Version data entries

76 entries across 76 versions & 1 rubygems

Version Path
avo-1.9.0 app/packs/js/controllers/item_selector_controller.js
avo-1.8.4 app/packs/js/controllers/item_selector_controller.js
avo-1.8.3 app/packs/js/controllers/item_selector_controller.js
avo-1.8.2 app/packs/js/controllers/item_selector_controller.js
avo-1.8.1 app/packs/js/controllers/item_selector_controller.js
avo-1.8.0 app/packs/js/controllers/item_selector_controller.js
avo-1.7.3 app/packs/js/controllers/item_selector_controller.js
avo-1.7.3.pre.1 app/packs/js/controllers/item_selector_controller.js
avo-1.7.2 app/packs/js/controllers/item_selector_controller.js
avo-1.7.1 app/packs/js/controllers/item_selector_controller.js
avo-1.7.0 app/packs/js/controllers/item_selector_controller.js
avo-1.6.4.pre.1 app/packs/js/controllers/item_selector_controller.js
avo-1.6.3.pre.3 app/packs/js/controllers/item_selector_controller.js
avo-1.6.3.pre.2 app/packs/js/controllers/item_selector_controller.js
avo-1.6.3.pre.1 app/packs/js/controllers/item_selector_controller.js
avo-1.6.2.pre.1 app/packs/js/controllers/item_selector_controller.js
avo-1.6.1 app/packs/js/controllers/item_selector_controller.js
avo-1.6.0 app/packs/js/controllers/item_selector_controller.js
avo-1.5.5 app/packs/js/controllers/item_selector_controller.js
avo-1.5.4 app/packs/js/controllers/item_selector_controller.js