blacklight-hierarchy 4.3.0 → 5.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +2 -2
- data/README.md +1 -1
- data/app/components/blacklight/hierarchy/facet_field_list_component.rb +4 -25
- data/app/helpers/blacklight/hierarchy_helper.rb +1 -183
- data/app/models/blacklight/hierarchy/facet_tree.rb +42 -0
- data/lib/blacklight/hierarchy/version.rb +1 -1
- data/package.json +1 -1
- data/spec/helpers/hierarchy_helper_spec.rb +0 -9
- metadata +3 -3
- data/.travis.yml +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 697c2ee0eb4208a5849d3d4ac87a06649926581ff2bceebf4286251a6502cb31
|
4
|
+
data.tar.gz: 243b6ae6bcb6a0f9f25335e251fee8d45b2ee0020337f4187728c0a20b2fd643
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 575224c30019574f9cd0396676e1dc402a3c5d4743538bcb04cd0eeefbb97106102f35deb02f342063d6d8c51da5f719a86d5bcb290998124ea0e0d81423599b
|
7
|
+
data.tar.gz: 1ca5e9d6fe3a6aae330a2bb4d6815e40b8923df00e9559a7dcb6ac3145837f02df1d844bd0bed964d7c245a78bf33c3e0dee4cc3ee905ec9f313b7e35a8e86b0
|
data/.github/workflows/ruby.yml
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# Blacklight::Hierarchy
|
2
|
-
[![Build Status](https://
|
2
|
+
[![Build Status](https://github.com/sul-dlss/blacklight-hierarchy/workflows/CI/badge.svg)](https://github.com/sul-dlss/blacklight-hierarchy/actions?query=branch%3Amain) [![Coverage Status](https://coveralls.io/repos/sul-dlss/blacklight-hierarchy/badge.png)](https://coveralls.io/r/sul-dlss/blacklight-hierarchy) [![Gem Version](https://badge.fury.io/rb/blacklight-hierarchy.svg)](http://badge.fury.io/rb/blacklight-hierarchy)
|
3
3
|
|
4
4
|
This plugin provides hierarchical facets for [Blacklight](https://github.com/projectblacklight/blacklight).
|
5
5
|
|
@@ -8,8 +8,11 @@ module Blacklight
|
|
8
8
|
# @param [Blacklight::Configuration::FacetField] as defined in controller with config.add_facet_field (and with :partial => 'blacklight/hierarchy/facet_hierarchy')
|
9
9
|
# @return [String] html for the facet tree
|
10
10
|
def tree
|
11
|
+
|
11
12
|
@tree ||= begin
|
12
|
-
facet_tree_for_prefix =
|
13
|
+
facet_tree_for_prefix = FacetTree.build(prefix: prefix,
|
14
|
+
facet_display: blacklight_config.facet_display,
|
15
|
+
facet_field: @facet_field)
|
13
16
|
facet_tree_for_prefix ? facet_tree_for_prefix[field_name] : nil
|
14
17
|
end
|
15
18
|
end
|
@@ -42,31 +45,7 @@ module Blacklight
|
|
42
45
|
@prefix ||= field_name.gsub("#{DELIMETER}#{field_name.split(/#{DELIMETER}/).last}", '')
|
43
46
|
end
|
44
47
|
|
45
|
-
|
46
48
|
delegate :blacklight_config, to: :helpers
|
47
|
-
|
48
|
-
def facet_tree
|
49
|
-
@facet_tree ||= {}
|
50
|
-
return @facet_tree[prefix] unless @facet_tree[prefix].nil?
|
51
|
-
return @facet_tree[prefix] unless blacklight_config.facet_display[:hierarchy] && blacklight_config.facet_display[:hierarchy][prefix]
|
52
|
-
@facet_tree[prefix] = {}
|
53
|
-
facet_config = blacklight_config.facet_display[:hierarchy][prefix]
|
54
|
-
split_regex = Regexp.new("\s*#{Regexp.escape(facet_config.length >= 2 ? facet_config[1] : ':')}\s*")
|
55
|
-
facet_config.first.each do |key|
|
56
|
-
# TODO: remove baked in notion of underscores being part of the blacklight facet field names
|
57
|
-
facet_field = [prefix, key].compact.join('_')
|
58
|
-
@facet_tree[prefix][facet_field] ||= {}
|
59
|
-
data = @facet_field.display_facet
|
60
|
-
next if data.nil?
|
61
|
-
data.items.each do |facet_item|
|
62
|
-
path = facet_item.value.split(split_regex)
|
63
|
-
loc = @facet_tree[prefix][facet_field]
|
64
|
-
loc = loc[path.shift] ||= {} while path.length > 0
|
65
|
-
loc[:_] = HierarchicalFacetItem.new(facet_item.value, facet_item.value.split(split_regex).last, facet_item.hits)
|
66
|
-
end
|
67
|
-
end
|
68
|
-
@facet_tree[prefix]
|
69
|
-
end
|
70
49
|
end
|
71
50
|
end
|
72
51
|
end
|
@@ -1,128 +1,6 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Blacklight::HierarchyHelper
|
4
|
-
extend Deprecation
|
5
|
-
|
6
|
-
# Putting bare HTML strings in a helper sucks. But in this case, with a
|
7
|
-
# lot of recursive tree-walking going on, it's an order of magnitude faster
|
8
|
-
# than either render(:partial) or content_tag
|
9
|
-
def render_facet_hierarchy_item(field_name, data, key)
|
10
|
-
item = data[:_]
|
11
|
-
subset = data.reject { |k, _v| !k.is_a?(String) }
|
12
|
-
|
13
|
-
Deprecation.silence(Blacklight::HierarchyHelper) do
|
14
|
-
li_class = subset.empty? ? 'h-leaf' : 'h-node'
|
15
|
-
id = SecureRandom.uuid
|
16
|
-
ul = ''
|
17
|
-
li = ''
|
18
|
-
li << facet_toggle_button(field_name, id) if subset.any?
|
19
|
-
li << if item.nil?
|
20
|
-
key
|
21
|
-
elsif qfacet_selected?(field_name, item)
|
22
|
-
render_selected_qfacet_value(field_name, item)
|
23
|
-
else
|
24
|
-
render_qfacet_value(field_name, item, id: id)
|
25
|
-
end
|
26
|
-
|
27
|
-
unless subset.empty?
|
28
|
-
subul = subset.keys.sort.collect do |subkey|
|
29
|
-
render_facet_hierarchy_item(field_name, subset[subkey], subkey)
|
30
|
-
end.join('')
|
31
|
-
ul = "<ul role=\"group\">#{subul}</ul>".html_safe
|
32
|
-
end
|
33
|
-
|
34
|
-
%(<li class="#{li_class}" role="treeitem">#{li.html_safe}#{ul.html_safe}</li>).html_safe
|
35
|
-
end
|
36
|
-
end
|
37
|
-
deprecation_deprecate :render_facet_hierarchy_item
|
38
|
-
|
39
|
-
def qfacet_selected?(field_name, item)
|
40
|
-
config = facet_configuration_for_field(field_name)
|
41
|
-
search_state.has_facet?(config, value: facet_value_for_facet_item(item.qvalue))
|
42
|
-
end
|
43
|
-
private :qfacet_selected?
|
44
|
-
|
45
|
-
# @param [Blacklight::Configuration::FacetField] as defined in controller with config.add_facet_field (and with :partial => 'blacklight/hierarchy/facet_hierarchy')
|
46
|
-
# @return [String] html for the facet tree
|
47
|
-
def render_hierarchy(bl_facet_field, delim = '_')
|
48
|
-
field_name = bl_facet_field.field
|
49
|
-
prefix = field_name.gsub("#{delim}#{field_name.split(/#{delim}/).last}", '')
|
50
|
-
facet_tree_for_prefix = facet_tree(prefix)
|
51
|
-
tree = facet_tree_for_prefix ? facet_tree_for_prefix[field_name] : nil
|
52
|
-
|
53
|
-
return '' unless tree
|
54
|
-
tree.keys.sort.collect do |key|
|
55
|
-
render_facet_hierarchy_item(field_name, tree[key], key)
|
56
|
-
end.join("\n").html_safe
|
57
|
-
end
|
58
|
-
deprecation_deprecate :render_hierarchy
|
59
|
-
|
60
|
-
def render_qfacet_value(facet_solr_field, item, options = {})
|
61
|
-
Deprecation.silence(Blacklight::FacetsHelperBehavior) do
|
62
|
-
id = options.delete(:id)
|
63
|
-
facet_config = facet_configuration_for_field(facet_solr_field)
|
64
|
-
path_for_facet = facet_item_presenter(facet_config, item.qvalue, facet_solr_field).href
|
65
|
-
(link_to_unless(options[:suppress_link], item.value, path_for_facet, id: id, class: 'facet_select') + ' ' + render_facet_count(item.hits)).html_safe
|
66
|
-
end
|
67
|
-
end
|
68
|
-
deprecation_deprecate :render_qfacet_value
|
69
|
-
|
70
|
-
# Standard display of a SELECTED facet value, no link, special span with class, and 'remove' button.
|
71
|
-
def render_selected_qfacet_value(facet_solr_field, item)
|
72
|
-
remove_href = search_action_path(search_state.remove_facet_params(facet_solr_field, item.qvalue))
|
73
|
-
content_tag(:span, render_qfacet_value(facet_solr_field, item, suppress_link: true), class: 'selected') + ' ' +
|
74
|
-
link_to(content_tag(:span, '', class: 'glyphicon glyphicon-remove') +
|
75
|
-
content_tag(:span, '[remove]', class: 'sr-only'),
|
76
|
-
remove_href,
|
77
|
-
class: 'remove'
|
78
|
-
)
|
79
|
-
end
|
80
|
-
deprecation_deprecate :render_selected_qfacet_value
|
81
|
-
|
82
|
-
# @param [String] hkey - a key to access the rest of the hierarchy tree, as defined in controller config.facet_display[:hierarchy] declaration.
|
83
|
-
# e.g. if you had this in controller:
|
84
|
-
# config.facet_display = {
|
85
|
-
# :hierarchy => {
|
86
|
-
# 'wf' => [['wps','wsp','swp'], ':'],
|
87
|
-
# 'callnum_top' => [['facet'], '/'],
|
88
|
-
# 'exploded_tag' => [['ssim'], ':']
|
89
|
-
# }
|
90
|
-
# }
|
91
|
-
# then possible hkey values would be 'wf', 'callnum_top', and 'exploded_tag'.
|
92
|
-
#
|
93
|
-
# the key in the :hierarchy hash is the "prefix" for the solr field with the hierarchy info. the value
|
94
|
-
# in the hash is a list, where the first element is a list of suffixes, and the second element is the delimiter
|
95
|
-
# used to break up the sections of hierarchical data in the solr field being read. when joined, the prefix and
|
96
|
-
# suffix should form the field name. so, for example, 'wf_wps', 'wf_wsp', 'wf_swp', 'callnum_top_facet', and
|
97
|
-
# 'exploded_tag_ssim' would be the solr fields with blacklight-hierarchy related configuration according to the
|
98
|
-
# hash above. ':' would be the delimiter used in all of those fields except for 'callnum_top_facet', which would
|
99
|
-
# use '/'. exploded_tag_ssim might contain values like ['Book', 'Book : Multi-Volume Work'], and callnum_top_facet
|
100
|
-
# might contain values like ['LB', 'LB/2395', 'LB/2395/.C65', 'LB/2395/.C65/1991'].
|
101
|
-
# note: the suffixes (e.g. 'ssim' for 'exploded_tag' in the above example) can't have underscores, otherwise things break.
|
102
|
-
def facet_tree(hkey)
|
103
|
-
@facet_tree ||= {}
|
104
|
-
return @facet_tree[hkey] unless @facet_tree[hkey].nil?
|
105
|
-
return @facet_tree[hkey] unless blacklight_config.facet_display[:hierarchy] && blacklight_config.facet_display[:hierarchy][hkey]
|
106
|
-
@facet_tree[hkey] = {}
|
107
|
-
facet_config = blacklight_config.facet_display[:hierarchy][hkey]
|
108
|
-
split_regex = Regexp.new("\s*#{Regexp.escape(facet_config.length >= 2 ? facet_config[1] : ':')}\s*")
|
109
|
-
facet_config.first.each do |key|
|
110
|
-
# TODO: remove baked in notion of underscores being part of the blacklight facet field names
|
111
|
-
facet_field = [hkey, key].compact.join('_')
|
112
|
-
@facet_tree[hkey][facet_field] ||= {}
|
113
|
-
data = @response.aggregations[facet_field]
|
114
|
-
next if data.nil?
|
115
|
-
data.items.each do |facet_item|
|
116
|
-
path = facet_item.value.split(split_regex)
|
117
|
-
loc = @facet_tree[hkey][facet_field]
|
118
|
-
loc = loc[path.shift] ||= {} while path.length > 0
|
119
|
-
loc[:_] = HierarchicalFacetItem.new(facet_item.value, facet_item.value.split(split_regex).last, facet_item.hits)
|
120
|
-
end
|
121
|
-
end
|
122
|
-
@facet_tree[hkey]
|
123
|
-
end
|
124
|
-
deprecation_deprecate :facet_tree
|
125
|
-
|
126
4
|
def facet_toggle_button(field_name, described_by)
|
127
5
|
aria_label = I18n.t(
|
128
6
|
"blacklight.hierarchy.#{field_name}_toggle_aria_label",
|
@@ -143,68 +21,8 @@ module Blacklight::HierarchyHelper
|
|
143
21
|
# below are methods pertaining to the "rotate" notion where you may want to look at the same tree data organized another way
|
144
22
|
# --------------------------------------------------------------------------------------------------------------------------------
|
145
23
|
|
146
|
-
# FIXME: remove baked in underscore separator in field name
|
147
|
-
def is_hierarchical?(field_name)
|
148
|
-
(prefix, order) = field_name.split(/_/, 2)
|
149
|
-
(list = blacklight_config.facet_display[:hierarchy][prefix]) && list.include?(order)
|
150
|
-
end
|
151
|
-
deprecation_deprecate :is_hierarchical?
|
152
|
-
|
153
|
-
def facet_order(prefix)
|
154
|
-
param_name = "#{prefix}_facet_order".to_sym
|
155
|
-
params[param_name] || blacklight_config.facet_display[:hierarchy][prefix].first
|
156
|
-
end
|
157
|
-
deprecation_deprecate :facet_order
|
158
|
-
|
159
24
|
def facet_after(prefix, order)
|
160
25
|
orders = blacklight_config.facet_display[:hierarchy][prefix]
|
161
26
|
orders[orders.index(order) + 1] || orders.first
|
162
27
|
end
|
163
|
-
|
164
|
-
# FIXME: remove baked in underscore separator in field name
|
165
|
-
def hide_facet?(field_name)
|
166
|
-
return false unless is_hierarchical?(field_name)
|
167
|
-
prefix = field_name.split(/_/).first
|
168
|
-
field_name != "#{prefix}_#{facet_order(prefix)}"
|
169
|
-
end
|
170
|
-
deprecation_deprecate :hide_facet?
|
171
|
-
|
172
|
-
|
173
|
-
# FIXME: remove baked in colon separator
|
174
|
-
def rotate_facet_value(val, from, to)
|
175
|
-
components = Hash[from.split(//).zip(val.split(/:/))]
|
176
|
-
new_values = components.values_at(*to.split(//))
|
177
|
-
new_values.pop while new_values.last.nil?
|
178
|
-
return nil if new_values.include?(nil)
|
179
|
-
new_values.compact.join(':')
|
180
|
-
end
|
181
|
-
deprecation_deprecate :rotate_facet_value
|
182
|
-
|
183
|
-
# FIXME: remove baked in underscore separator in field name
|
184
|
-
def rotate_facet_params(prefix, from, to, p = params.dup)
|
185
|
-
return p if from == to
|
186
|
-
from_field = "#{prefix}_#{from}"
|
187
|
-
to_field = "#{prefix}_#{to}"
|
188
|
-
p[:f] = (p[:f] || {}).dup # the command above is not deep in rails3, !@#$!@#$
|
189
|
-
p[:f][from_field] = (p[:f][from_field] || []).dup
|
190
|
-
p[:f][to_field] = (p[:f][to_field] || []).dup
|
191
|
-
p[:f][from_field].reject! { |v| p[:f][to_field] << rotate_facet_value(v, from, to); true }
|
192
|
-
p[:f].delete(from_field)
|
193
|
-
p[:f][to_field].compact!
|
194
|
-
p[:f].delete(to_field) if p[:f][to_field].empty?
|
195
|
-
p
|
196
|
-
end
|
197
|
-
deprecation_deprecate :rotate_facet_params
|
198
|
-
|
199
|
-
# FIXME: remove baked in underscore separator in field name
|
200
|
-
def render_facet_rotate(field_name)
|
201
|
-
return unless is_hierarchical?(field_name)
|
202
|
-
(prefix, order) = field_name.split(/_/, 2)
|
203
|
-
return if blacklight_config.facet_display[:hierarchy][prefix].length < 2
|
204
|
-
new_order = facet_after(prefix, order)
|
205
|
-
new_params = rotate_facet_params(prefix, order, new_order)
|
206
|
-
new_params["#{prefix}_facet_order"] = new_order
|
207
|
-
link_to image_tag('icons/rotate.png', title: new_order.upcase).html_safe, new_params, class: 'no-underline'
|
208
|
-
end
|
209
|
-
deprecation_deprecate :render_facet_rotate
|
210
28
|
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Blacklight
|
4
|
+
module Hierarchy
|
5
|
+
class FacetTree
|
6
|
+
def self.build(prefix:, facet_display:, facet_field:)
|
7
|
+
new(prefix: prefix, facet_display: facet_display, facet_field: facet_field).build
|
8
|
+
end
|
9
|
+
|
10
|
+
def initialize(prefix:, facet_display:, facet_field:)
|
11
|
+
@prefix = prefix
|
12
|
+
@facet_config = facet_display.dig(:hierarchy, prefix)
|
13
|
+
@facet_field = facet_field
|
14
|
+
end
|
15
|
+
|
16
|
+
attr_reader :prefix, :facet_config, :data
|
17
|
+
|
18
|
+
def build
|
19
|
+
return unless facet_config
|
20
|
+
{}.tap do |tree|
|
21
|
+
facet_config.first.each do |key|
|
22
|
+
# TODO: remove baked in notion of underscores being part of the blacklight facet field names
|
23
|
+
facet_field = [prefix, key].compact.join('_')
|
24
|
+
tree[facet_field] ||= {}
|
25
|
+
data = @facet_field.display_facet
|
26
|
+
next if data.nil?
|
27
|
+
data.items.each do |facet_item|
|
28
|
+
path = facet_item.value.split(split_regex)
|
29
|
+
loc = tree[facet_field]
|
30
|
+
loc = loc[path.shift] ||= {} while path.length > 0
|
31
|
+
loc[:_] = HierarchicalFacetItem.new(facet_item.value, facet_item.value.split(split_regex).last, facet_item.hits)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def split_regex
|
38
|
+
@split_regex ||= Regexp.new("\s*#{Regexp.escape(facet_config.length >= 2 ? facet_config[1] : ':')}\s*")
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "blacklight-hierarchy",
|
3
3
|
"version": "4.1.0",
|
4
|
-
"description": "[![Build Status](https://
|
4
|
+
"description": "[![Build Status](https://github.com/sul-dlss/blacklight-hierarchy/workflows/CI/badge.svg)](https://github.com/sul-dlss/blacklight-hierarchy/actions?query=branch%3Amain)",
|
5
5
|
"main": "app/assets/javascripts/blacklight/hierarchy/hierarchy.js",
|
6
6
|
"files": [
|
7
7
|
"app/assets/javascripts/blacklight/hierarchy/*.js",
|
@@ -1,15 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
RSpec.describe Blacklight::HierarchyHelper do
|
4
|
-
describe '#render_hierarchy' do
|
5
|
-
it 'should remove the _suffix from the field name' do
|
6
|
-
expect(Deprecation).to receive(:warn)
|
7
|
-
field = OpenStruct.new(field: 'the_field_name_facet')
|
8
|
-
expect(helper).to receive(:facet_tree).with('the_field_name').and_return({})
|
9
|
-
helper.render_hierarchy(field)
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
4
|
describe '#facet_toggle_button' do
|
14
5
|
subject { helper.facet_toggle_button(field_name, described_by) }
|
15
6
|
let(:field_name) { 'exploded_tag_ssim' }
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: blacklight-hierarchy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 5.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael B. Klein
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-01-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: blacklight
|
@@ -155,7 +155,6 @@ files:
|
|
155
155
|
- ".rspec"
|
156
156
|
- ".rubocop.yml"
|
157
157
|
- ".rubocop_todo.yml"
|
158
|
-
- ".travis.yml"
|
159
158
|
- Gemfile
|
160
159
|
- LICENSE
|
161
160
|
- README.md
|
@@ -171,6 +170,7 @@ files:
|
|
171
170
|
- app/components/blacklight/hierarchy/selected_qfacet_value_component.html.erb
|
172
171
|
- app/components/blacklight/hierarchy/selected_qfacet_value_component.rb
|
173
172
|
- app/helpers/blacklight/hierarchy_helper.rb
|
173
|
+
- app/models/blacklight/hierarchy/facet_tree.rb
|
174
174
|
- app/models/hierarchical_facet_item.rb
|
175
175
|
- app/views/blacklight/hierarchy/_facet_hierarchy.html.erb
|
176
176
|
- blacklight-hierarchy.gemspec
|