app/helpers/blacklight/hierarchy_helper.rb in blacklight-hierarchy-0.1.0 vs app/helpers/blacklight/hierarchy_helper.rb in blacklight-hierarchy-0.1.1

- old
+ new

@@ -1,66 +1,62 @@ module Blacklight::HierarchyHelper - # Putting bare HTML strings in a helper sucks. But in this case, with a + # Putting bare HTML strings in a helper sucks. But in this case, with a # lot of recursive tree-walking going on, it's an order of magnitude faster # than either render(:partial) or content_tag def render_facet_hierarchy_item(field_name, data, key) item = data[:_] subset = data.reject { |k,v| ! k.is_a?(String) } li_class = subset.empty? ? 'h-leaf' : 'h-node' li = ul = '' - + if item.nil? li = key elsif facet_in_params?(field_name, item.qvalue) li = render_selected_qfacet_value(field_name, item) else li = render_qfacet_value(field_name, item) end - + unless subset.empty? - subul = subset.keys.sort.collect do |subkey| - render_facet_hierarchy_item(field_name, subset[subkey], subkey) + subul = subset.keys.sort.collect do |subkey| + render_facet_hierarchy_item(field_name, subset[subkey], subkey) end.join('') ul = "<ul>#{subul}</ul>".html_safe end - + %{<li class="#{li_class}">#{li.html_safe}#{ul.html_safe}</li>}.html_safe end - # TODO: remove baked in notion of underscores being part of the blacklight facet field names, - # and of _facet being the suffix of the Solr field name seems to be baked in. - # + # TODO: remove baked in notion of _facet being the suffix of the Solr field name + # # @param [Blacklight::Configuration::FacetField] as defined in controller with config.add_facet_field (and with :partial => 'blacklight/hierarchy/facet_hierarchy') # @return [String] html for the facet tree - def render_hierarchy(bl_facet_field) + def render_hierarchy(bl_facet_field, delim='_') field_name = bl_facet_field.field - prefix = field_name.gsub("_#{field_name.split(/_/).last}", '') - tree = facet_tree(prefix)[field_name] - if tree - result = tree.keys.sort.collect do |key| - render_facet_hierarchy_item(field_name, tree[key], key) - end.join("\n").html_safe - else - "" - end + prefix = field_name.gsub("#{delim}#{field_name.split(/#{delim}/).last}", '') + facet_tree_for_prefix = facet_tree(prefix) + tree = facet_tree_for_prefix ? facet_tree_for_prefix[field_name] : nil + return '' unless tree + tree.keys.sort.collect do |key| + render_facet_hierarchy_item(field_name, tree[key], key) + end.join("\n").html_safe end def render_qfacet_value(facet_solr_field, item, options ={}) (link_to_unless(options[:suppress_link], item.value, add_facet_params(facet_solr_field, item.qvalue), :class=>"facet_select") + " " + render_facet_count(item.hits)).html_safe end - # Standard display of a SELECTED facet value, no link, special span - # with class, and 'remove' button. + # Standard display of a SELECTED facet value, no link, special span with class, and 'remove' button. def render_selected_qfacet_value(facet_solr_field, item) content_tag(:span, render_qfacet_value(facet_solr_field, item, :suppress_link => true), :class => "selected") + " " + link_to(content_tag(:span, '', :class => "glyphicon glyphicon-remove") + content_tag(:span, '[remove]', :class => 'sr-only'), remove_facet_params(facet_solr_field, item.qvalue, params), :class=>"remove") end HierarchicalFacetItem = Struct.new :qvalue, :value, :hits - + # @param [String] hkey - a key to access the rest of the hierarchy tree, as defined in controller config.facet_display[:hierarchy] declaration. # e.g. if you had this in controller: # config.facet_display = { # :hierarchy => { # 'wf' => [['wps','wsp','swp'], ':'], @@ -68,46 +64,44 @@ # } # } # then possible hkey values would be 'wf' and 'callnum' def facet_tree(hkey) @facet_tree ||= {} - if blacklight_config.facet_display[:hierarchy] && blacklight_config.facet_display[:hierarchy][hkey] - value_delim = blacklight_config.facet_display[:hierarchy][hkey].last - split_regex = Regexp.new("\s*#{Regexp.escape(value_delim)}\s*") - if @facet_tree[hkey].nil? - @facet_tree[hkey] = {} - blacklight_config.facet_display[:hierarchy][hkey].first.each { |key| - # TODO: remove baked in notion of underscores being part of the blacklight facet field names - facet_field = [hkey,key].compact.join('_') - @facet_tree[hkey][facet_field] ||= {} - data = @response.facet_by_field_name(facet_field) - next if data.nil? + return @facet_tree[hkey] unless @facet_tree[hkey].nil? + return @facet_tree[hkey] unless blacklight_config.facet_display[:hierarchy] && blacklight_config.facet_display[:hierarchy][hkey] + @facet_tree[hkey] = {} + facet_config= blacklight_config.facet_display[:hierarchy][hkey] + split_regex = Regexp.new("\s*#{Regexp.escape(facet_config.length >= 2 ? facet_config[1] : ':')}\s*") + facet_config.first.each { |key| + # TODO: remove baked in notion of underscores being part of the blacklight facet field names + facet_field = [hkey,key].compact.join('_') + @facet_tree[hkey][facet_field] ||= {} + data = @response.facet_by_field_name(facet_field) + next if data.nil? - data.items.each { |facet_item| - path = facet_item.value.split(split_regex) - loc = @facet_tree[hkey][facet_field] - while path.length > 0 - loc = loc[path.shift] ||= {} - end - loc[:_] = HierarchicalFacetItem.new(facet_item.value, facet_item.value.split(split_regex).last, facet_item.hits) - } - } - end - end + data.items.each { |facet_item| + path = facet_item.value.split(split_regex) + loc = @facet_tree[hkey][facet_field] + while path.length > 0 + loc = loc[path.shift] ||= {} + end + loc[:_] = HierarchicalFacetItem.new(facet_item.value, facet_item.value.split(split_regex).last, facet_item.hits) + } + } @facet_tree[hkey] end -# -------------------------------------------------------------------------------------------------------------------------------- +# -------------------------------------------------------------------------------------------------------------------------------- # below are methods pertaining to the "rotate" notion where you may want to look at the same tree data organized another way -# -------------------------------------------------------------------------------------------------------------------------------- - +# -------------------------------------------------------------------------------------------------------------------------------- + # FIXME: remove baked in underscore separator in field name def is_hierarchical?(field_name) (prefix,order) = field_name.split(/_/, 2) list = blacklight_config.facet_display[:hierarchy][prefix] and list.include?(order) end - + def facet_order(prefix) param_name = "#{prefix}_facet_order".to_sym params[param_name] || blacklight_config.facet_display[:hierarchy][prefix].first end @@ -123,10 +117,10 @@ field_name != "#{prefix}_#{facet_order(prefix)}" else false end end - + # FIXME: remove baked in colon separator def rotate_facet_value(val, from, to) components = Hash[from.split(//).zip(val.split(/:/))] new_values = components.values_at(*(to.split(//))) while new_values.last.nil?