Skip to content

Commit

Permalink
Use jQuery.length rather than the removed jQuery.size()
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Aug 30, 2017
1 parent aa4025f commit 704ac94
Showing 1 changed file with 27 additions and 27 deletions.
54 changes: 27 additions & 27 deletions app/assets/javascripts/blacklight/ajax_modal.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//= require blacklight/core

/*
/*
The ajax_modal plugin can display some interactions inside a Bootstrap
modal window, including some multi-page interactions.
modal window, including some multi-page interactions.
It supports unobtrusive Javascript, where a link or form that would have caused
a new page load is changed to display it's results inside a modal dialog,
by this plugin. The plugin assumes there is a Bootstrap modal div
on the page with id #ajax-modal to use as the modal -- the standard Blacklight
layout provides this.
layout provides this.
To make a link or form have their results display inside a modal, add
`data-ajax-modal="trigger"` to the link or form. (Note, form itself not submit input)
Expand All @@ -23,18 +23,18 @@
the layout when a JS AJAX request is detected, OR the response
can include a `<div data-ajax-modal="container">` -- only the contents
of the container will be placed inside the modal, the rest of the
page will be ignored.
page will be ignored.
If you'd like to have a link or button that closes the modal,
you can just add a `data-dismiss="modal"` to the link,
standard Bootstrap convention. But you can also have
an href on this link for non-JS contexts, we'll make sure
inside the modal it closes the modal and the link is NOT followed.
inside the modal it closes the modal and the link is NOT followed.
Link or forms inside the modal will ordinarily cause page loads
when they are triggered. However, if you'd like their results
to stay within the modal, just add `data-ajax-modal="preserve"`
to the link or form.
to the link or form.
Here's an example of what might be returned, demonstrating most of the devices available:
Expand All @@ -43,7 +43,7 @@
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 class="modal-title">Request Placed</h3>
</div>
<div class="modal-body">
<p>Some message</p>
<%= link_to "This result will still be within modal", some_link, :data => {:ajax_modal => "preserve"} %>
Expand All @@ -59,22 +59,22 @@
One additional feature. If the content returned from the AJAX modal load
has an element with `data-ajax-modal=close`, that will trigger the modal
to be closed. And if this element includes a node with class "flash_messages",
the flash-messages node will be added to the main page inside #main-flahses.
the flash-messages node will be added to the main page inside #main-flahses.
== Events
We'll send out an event 'loaded.blacklight.ajax-modal' with the #ajax-modal
dialog as the target, right after content is loaded into the modal but before
it is shown (if not already a shown modal). In an event handler, you can
it is shown (if not already a shown modal). In an event handler, you can
inspect loaded content by looking inside $(this). If you call event.preventDefault(),
we won't 'show' the dialog (although it may already have been shown, you may want to
$(this).modal("hide") if you want to ensure hidden/closed.
$(this).modal("hide") if you want to ensure hidden/closed.
The data-ajax-modal=close behavior is implemented with this event, see for example.
The data-ajax-modal=close behavior is implemented with this event, see for example.
*/

// We keep all our data in Blacklight.ajaxModal object.
// Create lazily if someone else created first.
// We keep all our data in Blacklight.ajaxModal object.
// Create lazily if someone else created first.
if (Blacklight.ajaxModal === undefined) {
Blacklight.ajaxModal = {};
}
Expand Down Expand Up @@ -104,29 +104,29 @@ Blacklight.ajaxModal.modalCloseSelector = "[data-ajax-modal~=close], span.ajax

// Called on fatal failure of ajax load, function returns content
// to show to user in modal. Right now called only for extreme
// network errors.
// network errors.
Blacklight.ajaxModal.onFailure = function(data) {
var contents = "<div class='modal-header'>" +
"<button type='button' class='close' data-dismiss='modal' aria-hidden='true'>×</button>" +
"Network Error</div>";
$(Blacklight.ajaxModal.modalSelector).find('.modal-content').html(contents);
$(Blacklight.ajaxModal.modalSelector).modal('show');
$(Blacklight.ajaxModal.modalSelector).modal('show');
}

Blacklight.ajaxModal.receiveAjax = function (data) {
Blacklight.ajaxModal.receiveAjax = function (data) {
if (data.readyState == 0) {
// Network error, could not contact server.
// Network error, could not contact server.
Blacklight.ajaxModal.onFailure(data)
}
else {
var contents = data.responseText;

// does it have a data- selector for container?
// important we don't execute script tags, we shouldn't.
// important we don't execute script tags, we shouldn't.
// code modelled off of JQuery ajax.load. https://github.com/jquery/jquery/blob/master/src/ajax/load.js?source=c#L62
var container = $("<div>").
append( jQuery.parseHTML(contents) ).find( Blacklight.ajaxModal.containerSelector ).first();
if (container.size() !== 0) {
if (container.length !== 0) {
contents = container.html();
}

Expand All @@ -138,7 +138,7 @@ Blacklight.ajaxModal.receiveAjax = function (data) {
// if they did preventDefault, don't show the dialog
if (e.isDefaultPrevented()) return;

$(Blacklight.ajaxModal.modalSelector).modal('show');
$(Blacklight.ajaxModal.modalSelector).modal('show');
}
};

Expand Down Expand Up @@ -172,14 +172,14 @@ Blacklight.ajaxModal.modalAjaxFormSubmit = function(e) {
Blacklight.ajaxModal.setup_modal = function() {
// Event indicating blacklight is setting up a modal link,
// you can catch it and call e.preventDefault() to abort
// setup.
// setup.
var e = $.Event('setup.blacklight.ajax-modal');
$("body").trigger(e);
if (e.isDefaultPrevented()) return;

// Register both trigger and preserve selectors in ONE event handler, combining
// into one selector with a comma, so if something matches BOTH selectors, it
// still only gets the event handler called once.
// still only gets the event handler called once.
$("body").on("click", Blacklight.ajaxModal.triggerLinkSelector + ", " + Blacklight.ajaxModal.preserveLinkSelector,
Blacklight.ajaxModal.modalAjaxLinkClick);
$("body").on("submit", Blacklight.ajaxModal.triggerFormSelector + ", " + Blacklight.ajaxModal.preserveFormSelector,
Expand All @@ -190,15 +190,15 @@ Blacklight.ajaxModal.setup_modal = function() {

// we support doing data-dismiss=modal on a <a> with a href for non-ajax
// use, we need to suppress following the a's href that's there for
// non-JS contexts.
// non-JS contexts.
$("body ").on("click", Blacklight.ajaxModal.modalSelector + " a[data-dismiss~=modal]", function (e) {
e.preventDefault();
});
};

// A function used as an event handler on loaded.blacklight.ajax-modal
// to catch contained data-ajax-modal=closed directions
Blacklight.ajaxModal.check_close_ajax_modal = function(event) {
Blacklight.ajaxModal.check_close_ajax_modal = function(event) {
if ($(event.target).find(Blacklight.ajaxModal.modalCloseSelector).length) {
modal_flashes = $(this).find('.flash_messages');

Expand All @@ -207,10 +207,10 @@ Blacklight.ajaxModal.check_close_ajax_modal = function(event) {

main_flashes = $('#main-flashes');
main_flashes.append(modal_flashes);
modal_flashes.fadeIn(500);
modal_flashes.fadeIn(500);
}
}

Blacklight.onLoad(function() {
Blacklight.onLoad(function() {
Blacklight.ajaxModal.setup_modal();
});

0 comments on commit 704ac94

Please sign in to comment.