Sha256: 164e6f4f0a10fd634b8c7f65c44c1ab41dcabd3a0ba145e2ac4cc21aeaca83f9

Contents?: true

Size: 1.36 KB

Versions: 36

Compression:

Stored size: 1.36 KB

Contents

/* eslint-disable no-alert */
import { Controller } from '@hotwired/stimulus'

export default class extends Controller {
  spinnerMarkup = `<div class="button-spinner">
  <div class="double-bounce1"></div>
  <div class="double-bounce2"></div>
</div>`;

  confirmed = false

  connect() {
    this.context.scope.element.addEventListener('click', (e) => {
      // If the user has to confirm the action
      if (this.confirmationMessage) {
        // Intervene only if not confirmed
        if (!this.confirmed) {
          e.preventDefault()
          if (window.confirm(this.confirmationMessage)) {
            this.applyLoader()
          }
        }
      } else {
        this.applyLoader()
      }
    })
  }

  get button() {
    return this.context.scope.element
  }

  get confirmationMessage() {
    return this.context.scope.element.getAttribute('data-avo-confirm')
  }

  applyLoader() {
    const { button } = this

    button.style.width = `${button.getBoundingClientRect().width}px`
    button.style.height = `${button.getBoundingClientRect().height}px`
    button.innerHTML = this.spinnerMarkup
    button.classList.add('justify-center')

    setTimeout(() => {
      this.markConfirmed()
      button.click()
      button.setAttribute('disabled', 'disabled')
    }, 1)
  }

  markConfirmed() {
    this.confirmed = true
  }

  markUnconfirmed() {
    this.confirmed = false
  }
}

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
avo-2.9.2.pre1 app/javascript/js/controllers/loading_button_controller.js
avo-2.9.1.pre7 app/javascript/js/controllers/loading_button_controller.js
avo-2.9.1.pre6 app/javascript/js/controllers/loading_button_controller.js
avo-2.9.1.pre5 app/javascript/js/controllers/loading_button_controller.js
avo-2.9.1.pre4 app/javascript/js/controllers/loading_button_controller.js
avo-2.9.1.pre3 app/javascript/js/controllers/loading_button_controller.js
avo-2.9.1.pre2 app/javascript/js/controllers/loading_button_controller.js
avo-2.9.1.pre1 app/javascript/js/controllers/loading_button_controller.js
avo-2.9.0 app/javascript/js/controllers/loading_button_controller.js
avo-2.8.0 app/javascript/js/controllers/loading_button_controller.js
avo-2.7.1.pre.1 app/javascript/js/controllers/loading_button_controller.js
avo-2.7.0 app/javascript/js/controllers/loading_button_controller.js
avo-2.6.1.pre.2 app/javascript/js/controllers/loading_button_controller.js
avo-2.6.1.pre.1 app/javascript/js/controllers/loading_button_controller.js
avo-2.6.0 app/javascript/js/controllers/loading_button_controller.js
avo-2.5.2.pre.7 app/javascript/js/controllers/loading_button_controller.js
avo-2.5.2.pre.6 app/javascript/js/controllers/loading_button_controller.js
avo-2.5.2.pre.5 app/javascript/js/controllers/loading_button_controller.js
avo-2.5.2.pre.4 app/javascript/js/controllers/loading_button_controller.js
avo-2.5.2.pre.3 app/javascript/js/controllers/loading_button_controller.js