Sha256: c9d57417872e2de97f8c8bc776f58f2f88562ad8b6b739a560a217263c48c0eb

Contents?: true

Size: 1.63 KB

Versions: 8

Compression:

Stored size: 1.63 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.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

8 entries across 8 versions & 2 rubygems

Version Path
activestorage_legacy-0.2.1 app/javascript/activestorage/ujs.js
activestorage_legacy-0.2.0 app/javascript/activestorage/ujs.js
activestorage_legacy-0.1.3 app/javascript/activestorage/ujs.js
activestorage_legacy-0.1.2 app/javascript/activestorage/ujs.js
activestorage_legacy-0.1.1 app/javascript/activestorage/ujs.js
activestorage_legacy-0.1.1.alpha app/javascript/activestorage/ujs.js
activestorage_legacy-0.1 app/javascript/activestorage/ujs.js
ruby-on-quails-0.1.0 activestorage/app/javascript/activestorage/ujs.js