Sha256: 7f8ba04abb2b12bbe446c09d59ac844694252dc91a6296094616dc57ad490752

Contents?: true

Size: 1.37 KB

Versions: 27

Compression:

Stored size: 1.37 KB

Contents

import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  static get targets() {
    return [ "filename", "signedBlobId", "imageId", "alt", "thumbnail", "clearButton" ]
  }

  connect() {
    // Hide thumbnail if there's no image
    if (this.hasThumbnailTarget && this.thumbnailTarget.children.length == 0) this.hideThumbnail()
  }

  removeImage() {
    this.imageIdTarget.value = ""
    if (this.hasFilenameTarget) this.filenameTarget.value = ""
    if (this.hasSignedBlobIdTarget) this.signedBlobIdTarget.value = ""
    if (this.hasAltTarget) this.altTarget.value = ""
    this.hideThumbnail()
  }
  
  handleDone(event) {
    // Set fields
    if (this.hasFilenameTarget) this.filenameTarget.value = event.detail.filename
    if (this.hasSignedBlobIdTarget) this.signedBlobIdTarget.value = event.detail.signedBlobId
    this.imageIdTarget.value = event.detail.imageId

    // Set placeholder
    this.setThumbnail(event.detail.thumbnail)
  }
  
  setThumbnail(imageSrc) {
    this.thumbnailTarget.innerHTML = `<img src="${imageSrc}" class="object-contain w-full h-36" />`
    this.showThumbnail()
  }

  showThumbnail() {
    this.thumbnailTarget.classList.remove('hidden')
    this.clearButtonTarget.classList.remove('hidden')
  }

  hideThumbnail() {
    this.thumbnailTarget.classList.add('hidden')
    this.clearButtonTarget.classList.add('hidden')
  }

}

Version data entries

27 entries across 27 versions & 1 rubygems

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