Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add mouse bindings for search result navigation
- Loading branch information
Showing
5 changed files
with
85 additions
and
16 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 |
---|---|---|
|
@@ -12,4 +12,4 @@ | |
// | ||
//= require jquery | ||
//= require jquery_ujs | ||
//= require_tree . | ||
//= require_tree . |
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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
$(function() { | ||
|
||
var selectedClass = 'selected'; | ||
var searchField = $("#search-form #q"); | ||
|
||
searchField.select(); | ||
|
||
Mousetrap.bind(['down'], function(e) { | ||
navigateList('down', $("ul.search-results li:first")); | ||
haltEvent(e); | ||
}); | ||
|
||
Mousetrap.bind(['up'], function(e) { | ||
navigateList('up', $("ul.search-results li:last")); | ||
haltEvent(e); | ||
}); | ||
|
||
Mousetrap.bind(['enter', 'o'], function(e) { | ||
if(goToSelectedResult()) { | ||
haltEvent(e); | ||
} | ||
}); | ||
|
||
Mousetrap.bind(['/', 's', 'escape'], function(e) { | ||
searchField.focus().select(); | ||
$("ul.search-results li.selected").removeClass(selectedClass); | ||
haltEvent(e); | ||
}); | ||
|
||
var haltEvent = function(event) { | ||
event.stopPropagation(); | ||
event.preventDefault(); | ||
}; | ||
|
||
// direction == up|down | ||
var navigateList = function(direction, defaultEl) { | ||
|
||
previouslySelectedEl = $("ul.search-results li.selected"); | ||
|
||
if(previouslySelectedEl.size() <= 0) { | ||
defaultEl.addClass(selectedClass); | ||
searchField.blur(); | ||
} else { | ||
previouslySelectedEl.removeClass(selectedClass); | ||
newlySelectedEl = direction == 'up' ? previouslySelectedEl.prev() : previouslySelectedEl.next(); | ||
newlySelectedEl.addClass(selectedClass); | ||
if(newlySelectedEl.size() <= 0) { | ||
searchField.focus(); | ||
} | ||
} | ||
} | ||
|
||
var goToSelectedResult = function() { | ||
selectedEl = $("ul.search-results li.selected"); | ||
if(selectedEl.size() >= 1) { | ||
location.href = selectedEl.find("a.gist-url").attr('href'); | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
}; | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,24 +1,23 @@ | ||
%style(type="text/css") | ||
:sass | ||
ul.search-results | ||
list-style-type: none | ||
li:nth-child(even) | ||
background: #EEE | ||
li.selected | ||
background: #999 | ||
|
||
%p | ||
= form_tag(search_gists_path, :method => "get") do | ||
= form_tag(search_gists_path, :method => "get", :id => "search-form") do | ||
= label_tag(:q, "Search your gists:") | ||
= text_field_tag(:q, params[:q]) | ||
= text_field_tag(:q, params[:q], :size => 50, :class => "mousetrap") | ||
= submit_tag("Go") | ||
= "(last indexed: #{current_user.last_gh_fetch ? current_user.last_gh_fetch.to_formatted_s(:short) : "not yet"})" | ||
|
||
- if @results.size > 0 | ||
%ul | ||
%ul.search-results | ||
- for result in @results | ||
%li | ||
%p= search_result_link(result) | ||
/ %p= result.inspect | ||
/ %p.files= files_display(result) | ||
/ - if(result.highlight && result.highlight[:'files.content']) | ||
/ - highlights = result.highlight[:'files.content'] | ||
/ %span.matches= highlights[0..(highlights.size < 2 ? -1 : 1)].join(' . . . ') | ||
- else | ||
%p= "No results found for \"#{params[:q]}\"" | ||
|
||
:javascript | ||
$(function() { | ||
$('#q').select(); | ||
}) | ||
%p= "No results found for \"#{params[:q]}\"" |