Sha256: 8704d81c0f2cf347e5c13e1736c3ed7534868677c9d5898a7bbf495256786830

Contents?: true

Size: 1.47 KB

Versions: 11

Compression:

Stored size: 1.47 KB

Contents

(() => {
  const application = Stimulus.Application.start()

  application.register("gallery-select", class extends Stimulus.Controller {
    static get targets() {
      return ["singleImage", "multipleImages", "multiple", "counter"]
    }

    select(event) {
      let item = event.currentTarget
      let imageId = item.dataset.imageId
      if (this.multipleTarget.checked) {
        item.classList.toggle('selected')
        if (item.classList.contains('selected')) {
          this.addToMultipleImages(imageId)  
        } else {
          this.removeFromMultipleImages(imageId)
        }
        this.counterTarget.innerText = `(${this.imageCount})`
      } else {
        item.parentElement.querySelectorAll('.item').forEach(function(item) {
          item.classList.remove('selected')
        })
        item.classList.add('selected')
        this.singleImageTarget.value = imageId
      }
    }

    addToMultipleImages(imageId) {
      let imageIds = this.multipleImages
      imageIds.push(imageId)
      this.multipleImagesTarget.value = [...new Set(imageIds)].join("-")
    }

    removeFromMultipleImages(imageId) {
      let imageIds = this.multipleImages
      let newImageIds = imageIds.filter(function(id) {
        return id != imageId
      })
      this.multipleImagesTarget.value = newImageIds.join("-")
    }

    get multipleImages() {
      return this.multipleImagesTarget.value.split("-")
    }

    get imageCount() {
      return this.multipleImages.length
    }
    
  })
})()

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
spina-2.0.2 app/assets/javascripts/spina/admin/controllers/gallery_select_controller.js
spina-2.0.1 app/assets/javascripts/spina/admin/controllers/gallery_select_controller.js
spina-2.0.0 app/assets/javascripts/spina/admin/controllers/gallery_select_controller.js
spina-2.0.0.beta app/assets/javascripts/spina/admin/controllers/gallery_select_controller.js
spina-2.0.0.alpha app/assets/javascripts/spina/admin/controllers/gallery_select_controller.js
spina-1.2.0 app/assets/javascripts/spina/admin/controllers/gallery_select_controller.js
spina-1.1.4 app/assets/javascripts/spina/admin/controllers/gallery_select_controller.js
spina-1.1.3 app/assets/javascripts/spina/admin/controllers/gallery_select_controller.js
spina-1.1.2 app/assets/javascripts/spina/admin/controllers/gallery_select_controller.js
spina-1.1.1 app/assets/javascripts/spina/admin/controllers/gallery_select_controller.js
spina-1.1.0 app/assets/javascripts/spina/admin/controllers/gallery_select_controller.js