Sha256: 936c876916a455d5175936c0181d363a1a7a7c0c619d99a79fe81bdf704915cc

Contents?: true

Size: 1.45 KB

Versions: 14

Compression:

Stored size: 1.45 KB

Contents

import Spinner from "alchemy_admin/spinner"

class Button extends HTMLButtonElement {
  connectedCallback() {
    if (this.form) {
      this.form.addEventListener("submit", this)

      if (this.form.dataset.remote == "true") {
        this.form.addEventListener("ajax:complete", this)
      }
    } else {
      console.warn("No form for button found!", this)
    }
  }

  handleEvent(event) {
    switch (event.type) {
      case "submit":
        const isDisabled = this.getAttribute("disabled") === "disabled"

        if (isDisabled) {
          event.preventDefault()
          event.stopPropagation()
        } else {
          this.disable()
        }
        break
      case "ajax:complete":
        this.enable()
        break
    }
  }

  disable() {
    const spinner = new Spinner("small")
    const rect = this.getBoundingClientRect()

    this.dataset.initialButtonText = this.innerHTML
    this.setAttribute("disabled", "disabled")
    this.setAttribute("tabindex", "-1")
    this.classList.add("disabled")
    this.style.width = `${rect.width}px`
    this.style.height = `${rect.height}px`
    this.innerHTML = " "

    spinner.spin(this)
  }

  enable() {
    this.classList.remove("disabled")
    this.removeAttribute("disabled")
    this.removeAttribute("tabindex")
    this.style.width = null
    this.style.height = null
    this.innerHTML = this.dataset.initialButtonText
  }
}

customElements.define("alchemy-button", Button, { extends: "button" })

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
alchemy_cms-7.1.12 app/javascript/alchemy_admin/components/button.js
alchemy_cms-7.1.11 app/javascript/alchemy_admin/components/button.js
alchemy_cms-7.1.10 app/javascript/alchemy_admin/components/button.js
alchemy_cms-7.1.9 app/javascript/alchemy_admin/components/button.js
alchemy_cms-7.1.8 app/javascript/alchemy_admin/components/button.js
alchemy_cms-7.1.7 app/javascript/alchemy_admin/components/button.js
alchemy_cms-7.1.6 app/javascript/alchemy_admin/components/button.js
alchemy_cms-7.1.5 app/javascript/alchemy_admin/components/button.js
alchemy_cms-7.1.4 app/javascript/alchemy_admin/components/button.js
alchemy_cms-7.1.3 app/javascript/alchemy_admin/components/button.js
alchemy_cms-7.1.2 app/javascript/alchemy_admin/components/button.js
alchemy_cms-7.1.1 app/javascript/alchemy_admin/components/button.js
alchemy_cms-7.1.0 app/javascript/alchemy_admin/components/button.js
alchemy_cms-7.1.0.pre.rc1 app/javascript/alchemy_admin/components/button.js