-
Notifications
You must be signed in to change notification settings - Fork 254
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replace our event handler with a customized version of $.rails.handle…
…Method
- Loading branch information
Showing
1 changed file
with
34 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |