-
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 #3369 from projectblacklight/backport-facet-sugges…
…t-to-8 Backport #3367 to 8.x (Add a way to filter facets in the facets "more" modal)
- Loading branch information
Showing
31 changed files
with
304 additions
and
5 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
9 changes: 9 additions & 0 deletions
9
app/components/blacklight/search/facet_suggest_input.html.erb
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,9 @@ | ||
<label for="facet_suggest_<%= facet.key %>"> | ||
<%= I18n.t('blacklight.search.facets.suggest.label', field_label: presenter&.label) %> | ||
</label> | ||
<%= text_field_tag "facet_suggest_#{facet.key}", | ||
nil, | ||
class: "facet-suggest form-control", | ||
data: {facet_field: facet.key}, | ||
placeholder: I18n.t('blacklight.search.form.search.placeholder') | ||
%> |
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,20 @@ | ||
# frozen_string_literal: true | ||
|
||
module Blacklight | ||
module Search | ||
class FacetSuggestInput < Blacklight::Component | ||
def initialize(facet:, presenter:) | ||
@facet = facet | ||
@presenter = presenter | ||
end | ||
|
||
private | ||
|
||
attr_accessor :facet, :presenter | ||
|
||
def render? | ||
facet&.suggest | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Usage: | ||
// ``` | ||
// const basicFunction = (entry) => console.log(entry) | ||
// const debounced = debounce(basicFunction("I should only be called once")); | ||
// | ||
// debounced // does NOT print to the screen because it is invoked again less than 200 milliseconds later | ||
// debounced // does print to the screen | ||
// ``` | ||
export default function debounce(func, timeout = 200) { | ||
let timer; | ||
return (...args) => { | ||
clearTimeout(timer); | ||
timer = setTimeout(() => { func.apply(this, args); }, timeout); | ||
}; | ||
} |
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 @@ | ||
import debounce from "blacklight/debounce"; | ||
|
||
const FacetSuggest = async (e) => { | ||
if (e.target.matches('.facet-suggest')) { | ||
const queryFragment = e.target.value?.trim(); | ||
const facetField = e.target.dataset.facetField; | ||
if (!facetField) { return; } | ||
|
||
const urlToFetch = `/catalog/facet_suggest/${facetField}/${queryFragment}` | ||
const response = await fetch(urlToFetch); | ||
if (response.ok) { | ||
const blob = await response.blob() | ||
const text = await blob.text() | ||
|
||
const facetArea = document.querySelector('.facet-extended-list'); | ||
|
||
if (text && facetArea) { | ||
facetArea.innerHTML = text | ||
} | ||
} | ||
} | ||
}; | ||
|
||
document.addEventListener('input', debounce(FacetSuggest)); | ||
|
||
export default FacetSuggest |
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,3 @@ | ||
<%= render Blacklight::FacetComponent.new(display_facet: @display_facet, | ||
blacklight_config: blacklight_config, | ||
layout: false) %> |
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
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
49 changes: 49 additions & 0 deletions
49
spec/components/blacklight/search/facet_suggest_input_spec.rb
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,49 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
RSpec.describe Blacklight::Search::FacetSuggestInput, type: :component do | ||
let(:facet) { Blacklight::Configuration::FacetField.new key: 'language_facet', suggest: true } | ||
let(:presenter) { instance_double(Blacklight::FacetFieldPresenter) } | ||
|
||
before do | ||
allow(presenter).to receive(:label).and_return 'Language' | ||
end | ||
|
||
it 'has an input with the facet-suggest class, which the javascript needs to find it' do | ||
rendered = render_inline(described_class.new(facet: facet, presenter: presenter)) | ||
expect(rendered.css("input.facet-suggest").count).to eq 1 | ||
end | ||
|
||
it 'has an input with the data-facet-field attribute, which the javascript needs to determine the correct query' do | ||
rendered = render_inline(described_class.new(facet: facet, presenter: presenter)) | ||
expect(rendered.css('input[data-facet-field="language_facet"]').count).to eq 1 | ||
end | ||
|
||
it 'has a visible label that is associated with the input' do | ||
rendered = render_inline(described_class.new(facet: facet, presenter: presenter)) | ||
label = rendered.css('label').first | ||
expect(label.text.strip).to eq 'Filter Language' | ||
|
||
id_in_label_for = label.attribute('for').text | ||
expect(id_in_label_for).to eq('facet_suggest_language_facet') | ||
|
||
expect(rendered.css('input').first.attribute('id').text).to eq id_in_label_for | ||
end | ||
|
||
context 'when the facet is explicitly configured to suggest: false' do | ||
let(:facet) { Blacklight::Configuration::FacetField.new key: 'language_facet', suggest: false } | ||
|
||
it 'does not display' do | ||
expect(render_inline(described_class.new(facet: facet, presenter: presenter)).to_s).to eq '' | ||
end | ||
end | ||
|
||
context 'when the facet is not explicitly configured with a suggest key' do | ||
let(:facet) { Blacklight::Configuration::FacetField.new key: 'language_facet' } | ||
|
||
it 'does not display' do | ||
expect(render_inline(described_class.new(facet: facet, presenter: presenter)).to_s).to eq '' | ||
end | ||
end | ||
end |
Oops, something went wrong.