Skip to content

Commit 31328e0

Browse files
committed
Patch blacklight form-in-modal behavior back in
See projectblacklight/blacklight#3772
1 parent 783fe1a commit 31328e0

File tree

1 file changed

+54
-15
lines changed

1 file changed

+54
-15
lines changed

app/frontend/javascript/blacklight_setup.js

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,63 @@
1-
// Import all blacklight javascript, in BL 8 via a rollup derived combo file
2-
// While not doc'd very well, this seems to be [the/a] way to import all blacklight Javascript.
31

2+
// If we wanted to, we could pick and choose which elements to import in the standard JS way,
3+
// which can work, but BL isn't tested very well for it. We'll just import all of BL for now.
44
import Blacklight from 'blacklight-frontend';
55

6+
//****************
7+
//
8+
// Customize Blacklight modal to add support for form submission in modal
9+
// offered upstream at https://github.com/projectblacklight/blacklight/pull/3772
10+
//
11+
// Note we do still CALL upstream func here, so changes upstream can break this patch.
12+
// We should test locally for sure.
13+
14+
15+
const modal = Blacklight.Modal;
16+
17+
modal.triggerformSelector = 'form[data-blacklight-modal~=trigger]';
18+
modal.preserveFormSelector = modal.modalSelector + ' form[data-blacklight-modal~=preserve]';
19+
20+
21+
modal.modalAjaxFormSubmit = function(e) {
22+
e.preventDefault();
23+
24+
const closest = e.target.closest(`${modal.triggerFormSelector}, ${modal.preserveFormSelector}`);
25+
26+
const method = (closest.getAttribute("method") || "GET").toUpperCase();
27+
const formData = new FormData(closest);
28+
const formAction = closest.getAttribute('action');
29+
30+
const href = (method == "GET") ? `${ formAction }?${ new URLSearchParams(formData).toString() }` : formAction;
31+
const fetchArgs = {
32+
headers: { 'X-Requested-With': 'XMLHttpRequest' }
33+
}
34+
35+
if (method != "GET") {
36+
fetchArgs.body = formData;
37+
fetchArgs.method = method;
38+
}
39+
40+
fetch(href, fetchArgs)
41+
.then(response => {
42+
if (!response.ok) {
43+
throw new TypeError("Request failed");
44+
}
45+
return response.text();
46+
})
47+
.then(data => modal.receiveAjax(data))
48+
.catch(error => modal.onFailure(error));
49+
}
50+
51+
document.addEventListener('submit', (e) => {
52+
if (e.target.closest(`${modal.triggerFormSelector}, ${modal.preserveFormSelector}`)) {
53+
modal.modalAjaxFormSubmit(e)
54+
}
55+
});
656

7-
// We USED to be able to pick-and-choose just the ones we need -- we don't actually need all of them, because
8-
// we don't use all BL parts.
957
//
10-
// As of Blacklight 8, that is not seem possible anymore, but will be again in 9
58+
// End customize Blacklight modal for forms
1159
//
12-
// https://github.com/projectblacklight/blacklight/issues/3050
13-
// We used to import only:
14-
15-
// import 'blacklight-frontend/app/javascript/blacklight/core';
16-
// // import 'blacklight-frontend/app/javascript/blacklight/bookmark_toggle';
17-
// // import 'blacklight-frontend/app/javascript/blacklight/button_focus';
18-
// // import 'blacklight-frontend/app/javascript/blacklight/checkbox_submit';
19-
// import 'blacklight-frontend/app/javascript/blacklight/facet_load';
20-
// import 'blacklight-frontend/app/javascript/blacklight/modal';
21-
// //import 'blacklight-frontend/app/javascript/blacklight/search_context';
60+
// ***********************
2261

2362

2463
import BlacklightRangeLimit from "blacklight-range-limit";

0 commit comments

Comments
 (0)