Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple submit buttons in Active Storage forms #33413

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion activestorage/app/assets/javascripts/activestorage.js
Expand Up @@ -855,14 +855,21 @@
return DirectUploadsController;
}();
var processingAttribute = "data-direct-uploads-processing";
var submitButtonsByForm = new WeakMap;
var started = false;
function start() {
if (!started) {
started = true;
document.addEventListener("click", didClick, true);
document.addEventListener("submit", didSubmitForm);
document.addEventListener("ajax:before", didSubmitRemoteElement);
}
}
function didClick(event) {
if (event.target.tagName == "INPUT" && event.target.type == "submit" && event.target.form) {
submitButtonsByForm.set(event.target.form, event.target);
}
}
function didSubmitForm(event) {
handleFormSubmissionEvent(event);
}
Expand Down Expand Up @@ -894,7 +901,8 @@
}
}
function submitForm(form) {
var button = findElement(form, "input[type=submit]");
var button = submitButtonsByForm.get(form) || findElement(form, "input[type=submit]");

if (button) {
var _button = button, disabled = _button.disabled;
button.disabled = false;
Expand All @@ -909,6 +917,7 @@
button.click();
form.removeChild(button);
}
submitButtonsByForm.delete(form);
}
function disable(input) {
input.disabled = true;
Expand Down
13 changes: 12 additions & 1 deletion activestorage/app/javascript/activestorage/ujs.js
Expand Up @@ -2,16 +2,25 @@ import { DirectUploadsController } from "./direct_uploads_controller"
import { findElement } from "./helpers"

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

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

function didClick(event) {
const { target } = event
if (target.tagName == "INPUT" && target.type == "submit" && target.form) {
submitButtonsByForm.set(target.form, target)
}
}

function didSubmitForm(event) {
handleFormSubmissionEvent(event)
}
Expand Down Expand Up @@ -49,7 +58,8 @@ function handleFormSubmissionEvent(event) {
}

function submitForm(form) {
let button = findElement(form, "input[type=submit]")
let button = submitButtonsByForm.get(form) || findElement(form, "input[type=submit]")

if (button) {
const { disabled } = button
button.disabled = false
Expand All @@ -64,6 +74,7 @@ function submitForm(form) {
button.click()
form.removeChild(button)
}
submitButtonsByForm.delete(form)
}

function disable(input) {
Expand Down