Sha256: 60a87f2a877e5338304992ff29917648e98ace8a512706697706960d33be55e9

Contents?: true

Size: 1.58 KB

Versions: 46

Compression:

Stored size: 1.58 KB

Contents

// Shows spinner while loading images and
// fades the image after its been loaded

const DEFAULT_SPINNER_OPTIONS = { fill: "#fff" }

export default class ImageLoader {
  static init(scope = document, spinnerOptions = DEFAULT_SPINNER_OPTIONS) {
    if (typeof scope === "string") {
      scope = document.querySelector(scope)
    }
    scope.querySelectorAll("img").forEach((image) => {
      const loader = new ImageLoader(image, spinnerOptions)
      loader.load()
    })
  }

  constructor(image, spinnerOptions = DEFAULT_SPINNER_OPTIONS) {
    this.image = image
    this.parent = image.parentNode
    this.spinner = new Alchemy.Spinner("small", spinnerOptions)
    this.bind()
  }

  bind() {
    this.image.addEventListener("load", this.onLoaded.bind(this))
    this.image.addEventListener("error", this.onError.bind(this))
  }

  load(force = false) {
    if (!force && this.image.complete) return

    this.image.classList.add("loading")
    this.spinner.spin(this.image.parentElement)
  }

  onLoaded() {
    this.removeSpinner()
    this.image.classList.remove("loading")
    this.unbind()
  }

  onError(evt) {
    const message = `Could not load "${this.image.src}"`
    this.removeSpinner()
    this.parent.innerHTML = `<span class="icon error fas fa-exclamation-triangle" title="${message}" />`
    console.error(message, evt)
    this.unbind()
  }

  unbind() {
    this.image.removeEventListener("load", this.onLoaded)
    this.image.removeEventListener("error", this.onError)
  }

  removeSpinner() {
    this.parent.querySelectorAll(".spinner").forEach((spinner) => {
      spinner.remove()
    })
  }
}

Version data entries

46 entries across 46 versions & 1 rubygems

Version Path
alchemy_cms-7.0.15 app/javascript/alchemy_admin/image_loader.js
alchemy_cms-6.1.10 package/src/image_loader.js
alchemy_cms-7.0.14 app/javascript/alchemy_admin/image_loader.js
alchemy_cms-7.0.13 app/javascript/alchemy_admin/image_loader.js
alchemy_cms-7.0.12 app/javascript/alchemy_admin/image_loader.js
alchemy_cms-7.0.11 app/javascript/alchemy_admin/image_loader.js
alchemy_cms-7.0.10 app/javascript/alchemy_admin/image_loader.js
alchemy_cms-7.0.9 app/javascript/alchemy_admin/image_loader.js
alchemy_cms-7.0.8 app/javascript/alchemy_admin/image_loader.js
alchemy_cms-7.0.7 app/javascript/alchemy_admin/image_loader.js
alchemy_cms-7.0.6 app/javascript/alchemy_admin/image_loader.js
alchemy_cms-7.0.5 app/javascript/alchemy_admin/image_loader.js
alchemy_cms-7.0.4 app/javascript/alchemy_admin/image_loader.js
alchemy_cms-7.0.3 app/javascript/alchemy_admin/image_loader.js
alchemy_cms-6.1.9 package/src/image_loader.js
alchemy_cms-7.0.2 app/javascript/alchemy_admin/image_loader.js
alchemy_cms-6.1.8 package/src/image_loader.js
alchemy_cms-6.1.7 package/src/image_loader.js
alchemy_cms-7.0.1 app/javascript/alchemy_admin/image_loader.js
alchemy_cms-7.0.0 app/javascript/alchemy_admin/image_loader.js