-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1288 from projectblacklight/typeahead
Adds autocomplete typeahead feature
- Loading branch information
Showing
21 changed files
with
344 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/*global Bloodhound */ | ||
|
||
Blacklight.onLoad(function() { | ||
'use strict'; | ||
|
||
$('[data-autocomplete-enabled="true"]').each(function() { | ||
var $el = $(this); | ||
var suggestUrl = $el.data().autocompletePath; | ||
|
||
var terms = new Bloodhound({ | ||
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'), | ||
queryTokenizer: Bloodhound.tokenizers.whitespace, | ||
remote: { | ||
url: suggestUrl + '?q=%QUERY', | ||
wildcard: '%QUERY' | ||
} | ||
}); | ||
|
||
terms.initialize(); | ||
|
||
$el.typeahead({ | ||
hint: true, | ||
highlight: true, | ||
minLength: 2 | ||
}, | ||
{ | ||
name: 'terms', | ||
displayKey: 'term', | ||
source: terms.ttAdapter() | ||
}); | ||
}); | ||
}); |
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
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,30 @@ | ||
.twitter-typeahead { | ||
float: left; | ||
width: 100%; | ||
z-index: 10000; | ||
|
||
.tt-input.form-control { | ||
width: 100%; | ||
} | ||
|
||
.tt-hint.form-control { | ||
width: 100%; | ||
} | ||
|
||
.tt-menu { | ||
@extend .dropdown-menu; | ||
font-family: $font-family-base; | ||
width: 100%; | ||
} | ||
|
||
.tt-cursor { | ||
background-color: $dropdown-link-hover-bg; | ||
color: $dropdown-link-hover-color; | ||
text-decoration: none; | ||
} | ||
|
||
.tt-suggestion { | ||
font-size: 14px; | ||
padding: 5px 5px 5px 10px; | ||
} | ||
} |
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,24 @@ | ||
module Blacklight | ||
module Suggest | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
include Blacklight::Configurable | ||
include Blacklight::SearchHelper | ||
|
||
copy_blacklight_config_from(CatalogController) | ||
end | ||
|
||
def index | ||
respond_to do |format| | ||
format.json do | ||
render json: suggestions_service.suggestions | ||
end | ||
end | ||
end | ||
|
||
def suggestions_service | ||
Blacklight::SuggestSearch.new(params, repository).suggestions | ||
end | ||
end | ||
end |
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,33 @@ | ||
module Blacklight | ||
class SuggestSearch | ||
attr_reader :request_params, :repository | ||
|
||
## | ||
# @param [Hash] params | ||
def initialize(params, repository) | ||
@request_params = { q: params[:q] } | ||
@repository = repository | ||
end | ||
|
||
## | ||
# For now, only use the q parameter to create a | ||
# Blacklight::Suggest::Response | ||
# @return [Blacklight::Suggest::Response] | ||
def suggestions | ||
Blacklight::Suggest::Response.new suggest_results, request_params, suggest_handler_path | ||
end | ||
|
||
## | ||
# Query the suggest handler using RSolr::Client::send_and_receive | ||
# @return [RSolr::HashWithResponse] | ||
def suggest_results | ||
repository.connection.send_and_receive(suggest_handler_path, params: request_params) | ||
end | ||
|
||
## | ||
# @return [String] | ||
def suggest_handler_path | ||
repository.blacklight_config.autocomplete_path | ||
end | ||
end | ||
end |
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,3 @@ | ||
class SuggestController < ApplicationController | ||
include Blacklight::Suggest | ||
end |
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,10 @@ | ||
module Blacklight | ||
module SuggestHelperBehavior | ||
## | ||
# @return [Boolean] should autocomplete be enabled in the UI | ||
def autocomplete_enabled? | ||
blacklight_config.autocomplete_enabled.present? && | ||
blacklight_config.autocomplete_path.present? | ||
end | ||
end | ||
end |
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,3 @@ | ||
module SuggestHelper | ||
include Blacklight::SuggestHelperBehavior | ||
end |
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,26 @@ | ||
module Blacklight | ||
module Suggest | ||
class Response | ||
attr_reader :response, :request_params, :suggest_path | ||
|
||
## | ||
# Creates a suggest response | ||
# @param [RSolr::HashWithResponse] response | ||
# @param [Hash] request_params | ||
# @param [String] suggest_path | ||
def initialize(response, request_params, suggest_path) | ||
@response = response | ||
@request_params = request_params | ||
@suggest_path = suggest_path | ||
end | ||
|
||
## | ||
# Trys the suggestor response to return suggestions if they are | ||
# present | ||
# @return [Array] | ||
def suggestions | ||
response.try(:[], suggest_path).try(:[], 'mySuggester').try(:[], request_params[:q]).try(:[], 'suggestions') || [] | ||
end | ||
end | ||
end | ||
end |
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
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
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
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require 'spec_helper' | ||
|
||
describe Blacklight::SuggestSearch do | ||
let(:params) { {q: 'test'} } | ||
let(:suggest_path) { 'suggest' } | ||
let(:connection) { double('connection', send_and_receive: 'sent')} | ||
let(:repository) { double('repository', connection: connection) } | ||
let(:suggest_search) { described_class.new(params, repository)} | ||
describe '#suggestions' do | ||
it 'returns a Blacklight::Suggest::Response' do | ||
expect(suggest_search).to receive(:suggest_results).and_return([]) | ||
expect(suggest_search).to receive(:suggest_handler_path).and_return(suggest_path) | ||
expect(suggest_search.suggestions).to be_an Blacklight::Suggest::Response | ||
end | ||
end | ||
describe '#suggest_results' do | ||
it 'should call send_and_recieve from a repository connection' do | ||
expect(suggest_search).to receive(:suggest_handler_path).and_return(suggest_path) | ||
expect(suggest_search.suggest_results).to eq 'sent' | ||
end | ||
end | ||
end |
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,17 @@ | ||
require 'spec_helper' | ||
|
||
describe SuggestController do | ||
routes { Blacklight::Engine.routes } | ||
describe 'GET index' do | ||
it 'returns JSON' do | ||
get :index, format: 'json' | ||
expect(response.body).to eq [].to_json | ||
end | ||
it 'returns suggestions' do | ||
get :index, format: 'json', q: 'new' | ||
json = JSON.parse(response.body) | ||
expect(json.count).to eq 3 | ||
expect(json.first['term']).to eq 'new jersey' | ||
end | ||
end | ||
end |
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,41 @@ | ||
require 'spec_helper' | ||
|
||
describe SuggestHelper do | ||
before do | ||
allow(helper).to receive(:blacklight_config).and_return(blacklight_config) | ||
end | ||
describe '#autocomplete_enabled?' do | ||
describe 'with autocomplete config' do | ||
let(:blacklight_config) do | ||
Blacklight::Configuration.new.configure do |config| | ||
config.autocomplete_enabled = true | ||
config.autocomplete_path = 'suggest' | ||
end | ||
end | ||
it 'is enabled' do | ||
expect(helper.autocomplete_enabled?).to be true | ||
end | ||
end | ||
describe 'without disabled config' do | ||
let(:blacklight_config) do | ||
Blacklight::Configuration.new.configure do |config| | ||
config.autocomplete_enabled = false | ||
config.autocomplete_path = 'suggest' | ||
end | ||
end | ||
it 'is disabled' do | ||
expect(helper.autocomplete_enabled?).to be false | ||
end | ||
end | ||
describe 'without path config' do | ||
let(:blacklight_config) do | ||
Blacklight::Configuration.new.configure do |config| | ||
config.autocomplete_enabled = true | ||
end | ||
end | ||
it 'is disabled' do | ||
expect(helper.autocomplete_enabled?).to be false | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.