Sha256: 5a7a39d539e8b8fb71208b980df5f15b283df38fbf0fee2a4df757c7237a1c0b

Contents?: true

Size: 1.65 KB

Versions: 4

Compression:

Stored size: 1.65 KB

Contents

import { DirectUploadsController } from "./direct_uploads_controller"
import { findElement } from "./helpers"

const processingAttribute = "data-direct-uploads-processing"
let started = false

export function start() {
  if (!started) {
    started = true
    document.addEventListener("submit", didSubmitForm)
    document.addEventListener("ajax:before", didSubmitRemoteElement)
  }
}

function didSubmitForm(event) {
  handleFormSubmissionEvent(event)
}

function didSubmitRemoteElement(event) {
  if (event.target.tagName == "FORM") {
    handleFormSubmissionEvent(event)
  }
}

function handleFormSubmissionEvent(event) {
  const form = event.target

  if (form.hasAttribute(processingAttribute)) {
    event.preventDefault()
    return
  }

  const controller = new DirectUploadsController(form)
  const { inputs } = controller

  if (inputs.length) {
    event.preventDefault()
    form.setAttribute(processingAttribute, "")
    inputs.forEach(disable)
    controller.start(error => {
      form.removeAttribute(processingAttribute)
      if (error) {
        inputs.forEach(enable)
      } else {
        submitForm(form)
      }
    })
  }
}

function submitForm(form) {
  let button = findElement(form, "input[type=submit]")
  if (button) {
    const { disabled } = button
    button.disabled = false
    button.focus()
    button.click()
    button.disabled = disabled
  } else {
    button = document.createElement("input")
    button.type = "submit"
    button.style = "display:none"
    form.appendChild(button)
    button.click()
    form.removeChild(button)
  }
}

function disable(input) {
  input.disabled = true
}

function enable(input) {
  input.disabled = false
}

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
activestorage-5.2.0.rc2 app/javascript/activestorage/ujs.js
activestorage-5.2.0.rc1 app/javascript/activestorage/ujs.js
activestorage-5.2.0.beta2 app/javascript/activestorage/ujs.js
activestorage-5.2.0.beta1 app/javascript/activestorage/ujs.js