Skip to content

Commit 9b94a0a

Browse files
committed
Restore long-lost form-handling ability to modal.js interactivity in modal dialog
1 parent 8c1d0e1 commit 9b94a0a

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

app/assets/builds/blacklight.css

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,6 @@ main {
205205

206206
.bookmark-toggle {
207207
--bl-icon-color: var(--bs-primary);
208-
/* override for line 21.
209-
Creates weird spacing in toolbar when min-width is set to 8rem */
210208
}
211209
.no-js .bookmark-toggle input[type=submit] {
212210
display: inline;
@@ -239,6 +237,10 @@ main {
239237
.bookmark-toggle .toggle-bookmark .toggle-bookmark-input:checked + span svg.bookmark-unchecked {
240238
display: none;
241239
}
240+
.bookmark-toggle {
241+
/* override for line 21.
242+
Creates weird spacing in toolbar when min-width is set to 8rem */
243+
}
242244
.bookmark-toggle .header-tools .toggle-bookmark-label {
243245
min-width: 2rem;
244246
}
@@ -371,13 +373,13 @@ main {
371373
float: left;
372374
}
373375

376+
.facet-field-heading a {
377+
color: inherit;
378+
}
374379
.facet-field-heading {
375380
/* This prevents the contained stretch link from covering the panel body */
376381
position: relative;
377382
}
378-
.facet-field-heading a {
379-
color: inherit;
380-
}
381383

382384
/* Pivot Facets
383385
-------------------------------------------------- */

app/javascript/blacklight-frontend/modal.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ const Modal = (() => {
6262
// Trigger selectors identify forms or hyperlinks that should open
6363
// inside a modal dialog.
6464
modal.triggerLinkSelector = 'a[data-blacklight-modal~=trigger]';
65+
modal.triggerformSelector = 'form[data-blacklight-modal~=trigger]';
6566

6667
// preserve selectors identify forms or hyperlinks that, if activated already
6768
// inside a modal dialog, should have destinations remain inside the modal -- but
@@ -71,6 +72,7 @@ const Modal = (() => {
7172
// be preserved. MUST be manually prefixed with the modal selector,
7273
// so they only apply to things inside a modal.
7374
modal.preserveLinkSelector = modal.modalSelector + ' a[data-blacklight-modal~=preserve]';
75+
modal.preserveFormSelector = modal.modalSelector + ' form[data-blacklight-modal~=preserve]';
7476

7577
modal.containerSelector = '[data-blacklight-modal~=container]';
7678

@@ -145,6 +147,35 @@ const Modal = (() => {
145147
.catch(error => modal.onFailure(error))
146148
};
147149

150+
modal.modalAjaxFormSubmit = function(e) {
151+
e.preventDefault();
152+
153+
const closest = e.target.closest(`${modal.triggerFormSelector}, ${modal.preserveFormSelector}`);
154+
155+
const method = (closest.getAttribute("method") || "GET").toUpperCase();
156+
const formData = new FormData(closest);
157+
const formAction = closest.getAttribute('action');
158+
159+
const href = (method == "GET") ? `${ formAction }?${ new URLSearchParams(formData).toString() }` : formAction;
160+
const fetchArgs = {
161+
headers: { 'X-Requested-With': 'XMLHttpRequest' }
162+
}
163+
if (method != "GET") {
164+
fetchArgs.body = formData;
165+
fetchArgs.method = method;
166+
}
167+
168+
fetch(href, fetchArgs)
169+
.then(response => {
170+
if (!response.ok) {
171+
throw new TypeError("Request failed");
172+
}
173+
return response.text();
174+
})
175+
.then(data => modal.receiveAjax(data))
176+
.catch(error => modal.onFailure(error));
177+
}
178+
148179
modal.setupModal = function() {
149180
// Register several click handlers in ONE event handler for efficiency
150181
//
@@ -154,8 +185,16 @@ const Modal = (() => {
154185
document.addEventListener('click', (e) => {
155186
if (e.target.closest(`${modal.triggerLinkSelector}, ${modal.preserveLinkSelector}`))
156187
modal.modalAjaxLinkClick(e)
157-
else if (e.target.matches(`${modal.modalSelector}`) || e.target.closest('[data-bl-dismiss="modal"]'))
188+
else if (e.target.matches(`${modal.modalSelector}`) || e.target.closest('[data-bl-dismiss="modal"]')) {
189+
e.preventDefault()
158190
modal.hide()
191+
}
192+
})
193+
194+
document.addEventListener('submit', (e) => {
195+
if (e.target.closest(`${modal.triggerFormSelector}, ${modal.preserveFormSelector}`)) {
196+
modal.modalAjaxFormSubmit(e)
197+
}
159198
})
160199

161200
// Make sure user-agent dismissal of html 'dialog', etc `esc` key, triggers

0 commit comments

Comments
 (0)