Sha256: 57050189a4833718fa9a72533c8d34048bec567229949b1a8c54f901e33bcba3

Contents?: true

Size: 1.08 KB

Versions: 27

Compression:

Stored size: 1.08 KB

Contents

import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  static get targets() {
    return [ "item" ]
  }
  
  select(event) {
    let item = event.currentTarget.closest(`[data-selectable-target*="item"]`)
    
    // Deactivate other items
    this.itemTargets.forEach(function(item) {
      this.deactivate(item)
    }.bind(this))

    this.activate(item)
  }
  
  activate(item) {
    this.toggleClasses(item, true)
  }
  
  deactivate(item) {
    this.toggleClasses(item, false)
  }
  
  toggleClasses(item, force) {
    item.querySelectorAll(`[data-selected-class]`).forEach(function(element) {
      let selectedClasses = element.dataset.selectedClass
      if(selectedClasses) {
        selectedClasses.split(" ").forEach(function(cssClass) {
          element.classList.toggle(cssClass, force)
        })
      }
      
      let deselectedClasses = element.dataset.deselectedClass
      if(deselectedClasses) {
        deselectedClasses.split(" ").forEach(function(cssClass) {
          element.classList.toggle(cssClass, !force)
        }) 
      }
    })
  }
}

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
spina-2.18.0 app/assets/javascripts/spina/controllers/selectable_controller.js
spina-2.17.0 app/assets/javascripts/spina/controllers/selectable_controller.js
spina-2.16.0 app/assets/javascripts/spina/controllers/selectable_controller.js
spina-2.15.1 app/assets/javascripts/spina/controllers/selectable_controller.js
spina-2.15.0 app/assets/javascripts/spina/controllers/selectable_controller.js
spina-2.14.0 app/assets/javascripts/spina/controllers/selectable_controller.js
spina-2.13.1 app/assets/javascripts/spina/controllers/selectable_controller.js
spina-2.13.0 app/assets/javascripts/spina/controllers/selectable_controller.js
spina-2.12.0 app/assets/javascripts/spina/controllers/selectable_controller.js
spina-2.11.0 app/assets/javascripts/spina/controllers/selectable_controller.js
spina-2.10.0 app/assets/javascripts/spina/controllers/selectable_controller.js
spina-2.9.1 app/assets/javascripts/spina/controllers/selectable_controller.js
spina-2.9.0 app/assets/javascripts/spina/controllers/selectable_controller.js
spina-2.8.1 app/assets/javascripts/spina/controllers/selectable_controller.js
spina-2.8.0 app/assets/javascripts/spina/controllers/selectable_controller.js
spina-2.7.0 app/assets/javascripts/spina/controllers/selectable_controller.js
spina-2.6.2 app/assets/javascripts/spina/controllers/selectable_controller.js
spina-2.6.1 app/assets/javascripts/spina/controllers/selectable_controller.js
spina-2.6.0 app/assets/javascripts/spina/controllers/selectable_controller.js
spina-2.5.0 app/assets/javascripts/spina/controllers/selectable_controller.js