Skip to content

Commit

Permalink
replace our event handler with a customized version of $.rails.handle…
Browse files Browse the repository at this point in the history
…Method
  • Loading branch information
cbeer committed Mar 10, 2014
1 parent 48557d4 commit f415703
Showing 1 changed file with 34 additions and 12 deletions.
46 changes: 34 additions & 12 deletions app/assets/javascripts/blacklight/search_context.js
Original file line number Diff line number Diff line change
@@ -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 = $('<form method="post" action="' + href + '"></form>'),
metadataInput = '<input name="_method" value="' + method + '" type="hidden" />';

// 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 += '<input name="' + csrfParam + '" value="' + csrfToken + '" type="hidden" />';
}

if (target) { form.attr('target', target); }

form.hide().append(metadataInput).appendTo('body');
form.submit();
};

Blacklight.onLoad(function() {
Blacklight.do_search_context_behavior();
});
})(jQuery);

0 comments on commit f415703

Please sign in to comment.