Skip to content

Commit c19d334

Browse files
committed
Merge pull request #775 from projectblacklight/ajax_modal_double_submit
ajax_modal: Combine trigger and preserve selectors
2 parents a813d1d + 5c9c939 commit c19d334

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

app/assets/javascripts/blacklight/ajax_modal.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,20 @@ if (Blacklight.ajaxModal === undefined) {
8383
// a Bootstrap modal div that should be already on the page hidden
8484
Blacklight.ajaxModal.modalSelector = "#ajax-modal";
8585

86+
// Trigger selectors identify forms or hyperlinks that should open
87+
// inside a modal dialog.
8688
Blacklight.ajaxModal.triggerLinkSelector = "a[data-ajax-modal~=trigger], a.lightboxLink,a.more_facets_link,.ajax_modal_launch";
8789
Blacklight.ajaxModal.triggerFormSelector = "form[data-ajax-modal~=trigger], form.ajax_form";
8890

89-
// preserve selectors will be scopied inside the modal only
90-
Blacklight.ajaxModal.preserveLinkSelector = 'a[data-ajax-modal~=preserve]';
91-
Blacklight.ajaxModal.preserveFormSelector = 'form[data-ajax-modal~=preserve], form.ajax_form';
91+
// preserve selectors identify forms or hyperlinks that, if activated already
92+
// inside a modal dialog, should have destinations remain inside the modal -- but
93+
// won't trigger a modal if not already in one.
94+
//
95+
// No need to repeat selectors from trigger selectors, those will already
96+
// be preserved. MUST be manually prefixed with the modal selector,
97+
// so they only apply to things inside a modal.
98+
Blacklight.ajaxModal.preserveLinkSelector = Blacklight.ajaxModal.modalSelector + ' a[data-ajax-modal~=preserve]';
99+
Blacklight.ajaxModal.preserveFormSelector = Blacklight.ajaxModal.modalSelector + 'form[data-ajax-modal~=preserve]'
92100

93101
Blacklight.ajaxModal.containerSelector = "[data-ajax-modal~=container]";
94102

@@ -152,12 +160,13 @@ Blacklight.ajaxModal.setup_modal = function() {
152160
$("body").trigger(e);
153161
if (e.isDefaultPrevented()) return;
154162

155-
$("body").on("click", Blacklight.ajaxModal.triggerLinkSelector, Blacklight.ajaxModal.modalAjaxLinkClick);
156-
$("body").on("submit", Blacklight.ajaxModal.triggerFormSelector, Blacklight.ajaxModal.modalAjaxFormSubmit);
157-
158-
// preserve selectors apply just within the existing modal
159-
$("body").on("click", Blacklight.ajaxModal.modalSelector + " " + Blacklight.ajaxModal.preserveLinkSelector, Blacklight.ajaxModal.modalAjaxLinkClick);
160-
$("body").on("submit", Blacklight.ajaxModal.modalSelector + " " + Blacklight.ajaxModal.preserveFormSelector, Blacklight.ajaxModal.modalAjaxFormSubmit);
163+
// Register both trigger and preserve selectors in ONE event handler, combining
164+
// into one selector with a comma, so if something matches BOTH selectors, it
165+
// still only gets the event handler called once.
166+
$("body").on("click", Blacklight.ajaxModal.triggerLinkSelector + ", " + Blacklight.ajaxModal.preserveLinkSelector,
167+
Blacklight.ajaxModal.modalAjaxLinkClick);
168+
$("body").on("submit", Blacklight.ajaxModal.triggerFormSelector + ", " + Blacklight.ajaxModal.preserveFormSelector,
169+
Blacklight.ajaxModal.modalAjaxFormSubmit);
161170

162171
// Catch our own custom loaded event to implement data-ajax-modal=closed
163172
$("body").on("loaded.blacklight.ajax-modal", Blacklight.ajaxModal.check_close_ajax_modal);

0 commit comments

Comments
 (0)