From 7506fc16477db1ee75095509e3dd854daea8c0bc Mon Sep 17 00:00:00 2001 From: Justin Coyne Date: Wed, 30 Aug 2017 13:16:14 -0500 Subject: [PATCH] Request html when you want an html partial. Fixes #1759 --- .../javascripts/blacklight/ajax_modal.js | 74 ++++++++----------- .../concerns/blacklight/catalog.rb | 9 ++- 2 files changed, 37 insertions(+), 46 deletions(-) diff --git a/app/assets/javascripts/blacklight/ajax_modal.js b/app/assets/javascripts/blacklight/ajax_modal.js index c714a8ede8..92cf49aa5b 100644 --- a/app/assets/javascripts/blacklight/ajax_modal.js +++ b/app/assets/javascripts/blacklight/ajax_modal.js @@ -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 = $("
"). - 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 = $("
"). + 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) } diff --git a/app/controllers/concerns/blacklight/catalog.rb b/app/controllers/concerns/blacklight/catalog.rb index 3f8e4ccc0f..0c92377d5b 100644 --- a/app/controllers/concerns/blacklight/catalog.rb +++ b/app/controllers/concerns/blacklight/catalog.rb @@ -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