Sha256: 2e909905bbd7a58457240d3f1fe45f60b3bfef162b5e5d4db62a396fb49a2da1

Contents?: true

Size: 1.21 KB

Versions: 4

Compression:

Stored size: 1.21 KB

Contents

import ApplicationController from './application_controller'

const SUBMIT_BUTTON_SELECTOR = 'input[type="submit"], button[type="submit"]'

export default class extends ApplicationController {
  initialize () {
    this.boundSubmit = this.submit.bind(this)
    this.boundReset = this.reset.bind(this)
  }

  connect () {
    this.element.addEventListener('submit', this.boundSubmit)
    this.element.addEventListener('turbo:before-fetch-response', this.boundReset)
  }

  disconnect () {
    this.element.removeEventListener('submit', this.boundSubmit)
    this.element.removeEventListener('turbo:before-fetch-response', this.boundReset)
  }

  submit (e) {
    this.disableButtons()
    this.setLoading(e.submitter || this.element.querySelector(SUBMIT_BUTTON_SELECTOR))
  }

  reset (e) {
    this.restoreButtons()
  }

  setLoading (button) {
    button.classList.add('loading')
  }

  disableButtons (e) {
    this.submitButtons.forEach(button => {
      button.disabled = true
    })
  }

  restoreButtons () {
    this.submitButtons.forEach(button => {
      button.classList.remove('loading')
      button.disabled = false
    })
  }

  get submitButtons () {
    return this.element.querySelectorAll(SUBMIT_BUTTON_SELECTOR)
  }
}

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
trestle-0.10.1 frontend/js/controllers/form_loading_controller.js
trestle-0.10.0 frontend/js/controllers/form_loading_controller.js
trestle-0.10.0.pre2 frontend/js/controllers/form_loading_controller.js
trestle-0.10.0.pre frontend/js/controllers/form_loading_controller.js