Skip to content

Commit

Permalink
Request html when you want an html partial. Fixes #1759
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Aug 30, 2017
1 parent 704ac94 commit 7506fc1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 46 deletions.
74 changes: 32 additions & 42 deletions app/assets/javascripts/blacklight/ajax_modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,58 +113,48 @@ Blacklight.ajaxModal.onFailure = function(data) {
$(Blacklight.ajaxModal.modalSelector).modal('show');
}

Blacklight.ajaxModal.receiveAjax = function (data) {
if (data.readyState == 0) {
// 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.
// 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.length !== 0) {
contents = container.html();
}

$(Blacklight.ajaxModal.modalSelector).find('.modal-content').html(contents);

// send custom event with the modal dialog div as the target
var e = $.Event('loaded.blacklight.ajax-modal')
$(Blacklight.ajaxModal.modalSelector).trigger(e);
// if they did preventDefault, don't show the dialog
if (e.isDefaultPrevented()) return;

$(Blacklight.ajaxModal.modalSelector).modal('show');
}
Blacklight.ajaxModal.receiveAjax = function (contents) {
// does it have a data- selector for container?
// 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.length !== 0) {
contents = container.html();
}

$(Blacklight.ajaxModal.modalSelector).find('.modal-content').html(contents);

// send custom event with the modal dialog div as the target
var e = $.Event('loaded.blacklight.blacklight-modal')
$(Blacklight.ajaxModal.modalSelector).trigger(e);
// if they did preventDefault, don't show the dialog
if (e.isDefaultPrevented()) return;

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


Blacklight.ajaxModal.modalAjaxLinkClick = function(e) {
e.preventDefault();

var jqxhr = $.ajax({
url: $(this).attr('href'),
dataType: 'script'
});

jqxhr.always( Blacklight.ajaxModal.receiveAjax );
$.ajax({
url: $(this).attr('href')
})
.fail(Blacklight.ajaxModal.onFailure)
.done(Blacklight.ajaxModal.receiveAjax)
};

Blacklight.ajaxModal.modalAjaxFormSubmit = function(e) {
e.preventDefault();

var jqxhr = $.ajax({
url: $(this).attr('action'),
data: $(this).serialize(),
type: $(this).attr('method'), //POST',
dataType: 'script'
});
e.preventDefault();

jqxhr.always(Blacklight.ajaxModal.receiveAjax);
$.ajax({
url: $(this).attr('action'),
data: $(this).serialize(),
type: $(this).attr('method') // POST
})
.fail(Blacklight.ajaxModal.onFailure)
.done(Blacklight.ajaxModal.receiveAjax)
}


Expand Down
9 changes: 5 additions & 4 deletions app/controllers/concerns/blacklight/catalog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ def facet
@display_facet = @response.aggregations[@facet.field]
@pagination = facet_paginator(@facet, @display_facet)
respond_to do |format|
# Draw the facet selector for users who have javascript disabled:
format.html
format.html do
# Draw the partial for the "more" facet modal window:
return render layout: false if request.xhr?
# Otherwise draw the facet selector for users who have javascript disabled.
end
format.json
# Draw the partial for the "more" facet modal window:
format.js { render :layout => false }
end
end

Expand Down

0 comments on commit 7506fc1

Please sign in to comment.