From f415703b0c40ddb3a7de44d5f7d6ea87fdabaa97 Mon Sep 17 00:00:00 2001 From: Chris Beer Date: Mon, 10 Mar 2014 13:53:11 -0700 Subject: [PATCH] replace our event handler with a customized version of $.rails.handleMethod --- .../javascripts/blacklight/search_context.js | 46 ++++++++++++++----- 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/app/assets/javascripts/blacklight/search_context.js b/app/assets/javascripts/blacklight/search_context.js index 4e8124c237..abab07c542 100644 --- a/app/assets/javascripts/blacklight/search_context.js +++ b/app/assets/javascripts/blacklight/search_context.js @@ -1,17 +1,39 @@ //= require blacklight/core (function($) { Blacklight.do_search_context_behavior = function() { - function track(event) { - this.href = this.getAttribute('data-context-href'); - this.setAttribute('data-method', 'post'); - if(event.metaKey || event.ctrlKey) { - this.setAttribute('target', '_blank'); - }; - } + $('a[data-context-href]').on('click.search-context', handleSearchContextMethod); + }; - $('a[data-context-href]').on('click', track); - }; -Blacklight.onLoad(function() { - Blacklight.do_search_context_behavior(); -}); + // this is the $.rails.handleMethod with a couple adjustments, described inline: + // first, we're attaching this directly to the event handler, so we can check for meta-keys + Blacklight.handleSearchContextMethod = function(event) { + var link = $(this); + + // instead of using the normal href, we need to use the context href instead + var href = link.data('context-href'), + method = 'post', + target = link.attr('target'), + csrfToken = $('meta[name=csrf-token]').attr('content'), + csrfParam = $('meta[name=csrf-param]').attr('content'), + form = $('
'), + metadataInput = ''; + + // check for meta keys.. if set, we should open in a new table + if(event.metaKey || event.ctrlKey) { + target = '_blank'; + } + + if (csrfParam !== undefined && csrfToken !== undefined) { + metadataInput += ''; + } + + if (target) { form.attr('target', target); } + + form.hide().append(metadataInput).appendTo('body'); + form.submit(); + }; + + Blacklight.onLoad(function() { + Blacklight.do_search_context_behavior(); + }); })(jQuery);