blacklight 3.1.0 → 3.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +1 -1
- data/VERSION +1 -1
- data/app/controllers/catalog_controller.rb +6 -0
- data/app/helpers/blacklight/blacklight_helper_behavior.rb +416 -0
- data/app/helpers/blacklight/catalog_helper_behavior.rb +87 -0
- data/app/helpers/blacklight/facets_helper_behavior.rb +114 -0
- data/app/helpers/{hash_as_hidden_fields.rb → blacklight/hash_as_hidden_fields_helper_behavior.rb} +1 -1
- data/app/helpers/blacklight/html_head_helper_behavior.rb +103 -0
- data/app/helpers/blacklight/render_constraints_helper_behavior.rb +121 -0
- data/app/helpers/blacklight_helper.rb +1 -395
- data/app/helpers/catalog_helper.rb +1 -85
- data/app/helpers/facets_helper.rb +1 -112
- data/app/helpers/hash_as_hidden_fields_helper.rb +3 -0
- data/app/helpers/html_head_helper.rb +1 -101
- data/app/helpers/render_constraints_helper.rb +1 -119
- data/app/views/catalog/{_index_partials/_default.erb → _index_default.html.erb} +0 -0
- data/app/views/catalog/{_show_partials/_default.html.erb → _show_default.html.erb} +0 -0
- data/lib/blacklight/solr/document/marc_export.rb +2 -6
- data/test_support/spec/helpers/hash_as_hidden_fields_spec.rb +2 -2
- data/test_support/spec/lib/marc_export_spec.rb +1 -1
- data/test_support/spec/views/catalog/{_index_partials/_default.erb_spec.rb → _index_default.erb_spec.rb} +2 -2
- data/test_support/spec/views/catalog/{_show_partials/_default.html.erb_spec.rb → _show_default.erb_spec.rb} +2 -2
- metadata +31 -31
- data/app/helpers/bookmarks_helper.rb +0 -4
- data/app/helpers/feedback_helper.rb +0 -3
- data/app/helpers/saved_searches_helper.rb +0 -3
- data/app/helpers/search_history_helper.rb +0 -3
- data/test_support/spec/helpers/search_history_helper_spec.rb +0 -12
@@ -0,0 +1,114 @@
|
|
1
|
+
module Blacklight::FacetsHelperBehavior
|
2
|
+
|
3
|
+
#
|
4
|
+
# Blacklight.config based helpers ->
|
5
|
+
#
|
6
|
+
|
7
|
+
# used in the catalog/_facets partial
|
8
|
+
def facet_field_labels
|
9
|
+
Blacklight.config[:facet][:labels]
|
10
|
+
end
|
11
|
+
|
12
|
+
# used in the catalog/_facets partial
|
13
|
+
def facet_field_names
|
14
|
+
Blacklight.config[:facet][:field_names]
|
15
|
+
end
|
16
|
+
|
17
|
+
# used in the catalog/_facets partial and elsewhere
|
18
|
+
# Renders a single section for facet limit with a specified
|
19
|
+
# solr field used for faceting. Can be over-ridden for custom
|
20
|
+
# display on a per-facet basis.
|
21
|
+
def render_facet_limit(solr_field)
|
22
|
+
render( :partial => "catalog/facet_limit", :locals => {:solr_field =>solr_field })
|
23
|
+
end
|
24
|
+
|
25
|
+
#
|
26
|
+
# facet param helpers ->
|
27
|
+
#
|
28
|
+
|
29
|
+
# Standard display of a facet value in a list. Used in both _facets sidebar
|
30
|
+
# partial and catalog/facet expanded list. Will output facet value name as
|
31
|
+
# a link to add that to your restrictions, with count in parens.
|
32
|
+
# first arg item is a facet value item from rsolr-ext.
|
33
|
+
# options consist of:
|
34
|
+
# :suppress_link => true # do not make it a link, used for an already selected value for instance
|
35
|
+
def render_facet_value(facet_solr_field, item, options ={})
|
36
|
+
(link_to_unless(options[:suppress_link], item.value, add_facet_params_and_redirect(facet_solr_field, item.value), :class=>"facet_select label") + " " + render_facet_count(item.hits)).html_safe
|
37
|
+
end
|
38
|
+
|
39
|
+
# Standard display of a SELECTED facet value, no link, special span
|
40
|
+
# with class, and 'remove' button.
|
41
|
+
def render_selected_facet_value(facet_solr_field, item)
|
42
|
+
content_tag(:span, render_facet_value(facet_solr_field, item, :suppress_link => true), :class => "selected label") +
|
43
|
+
link_to("[remove]", remove_facet_params(facet_solr_field, item.value, params), :class=>"remove")
|
44
|
+
end
|
45
|
+
|
46
|
+
# Renders a count value for facet limits. Can be over-ridden locally
|
47
|
+
# to change style, for instance not use parens. And can be called
|
48
|
+
# by plugins to get consistent display.
|
49
|
+
def render_facet_count(num)
|
50
|
+
content_tag("span", "(" + format_num(num) + ")", :class => "count")
|
51
|
+
end
|
52
|
+
|
53
|
+
# adds the value and/or field to params[:f]
|
54
|
+
# Does NOT remove request keys and otherwise ensure that the hash
|
55
|
+
# is suitable for a redirect. See
|
56
|
+
# add_facet_params_and_redirect
|
57
|
+
def add_facet_params(field, value)
|
58
|
+
p = params.dup
|
59
|
+
p[:f] = (p[:f] || {}).dup # the command above is not deep in rails3, !@#$!@#$
|
60
|
+
p[:f][field] = (p[:f][field] || []).dup
|
61
|
+
p[:f][field].push(value)
|
62
|
+
p
|
63
|
+
end
|
64
|
+
|
65
|
+
# Used in catalog/facet action, facets.rb view, for a click
|
66
|
+
# on a facet value. Add on the facet params to existing
|
67
|
+
# search constraints. Remove any paginator-specific request
|
68
|
+
# params, or other request params that should be removed
|
69
|
+
# for a 'fresh' display.
|
70
|
+
# Change the action to 'index' to send them back to
|
71
|
+
# catalog/index with their new facet choice.
|
72
|
+
def add_facet_params_and_redirect(field, value)
|
73
|
+
new_params = add_facet_params(field, value)
|
74
|
+
|
75
|
+
# Delete page, if needed.
|
76
|
+
new_params.delete(:page)
|
77
|
+
|
78
|
+
# Delete any request params from facet-specific action, needed
|
79
|
+
# to redir to index action properly.
|
80
|
+
Blacklight::Solr::FacetPaginator.request_keys.values.each do |paginator_key|
|
81
|
+
new_params.delete(paginator_key)
|
82
|
+
end
|
83
|
+
new_params.delete(:id)
|
84
|
+
|
85
|
+
# Force action to be index.
|
86
|
+
new_params[:action] = "index"
|
87
|
+
new_params
|
88
|
+
end
|
89
|
+
# copies the current params (or whatever is passed in as the 3rd arg)
|
90
|
+
# removes the field value from params[:f]
|
91
|
+
# removes the field if there are no more values in params[:f][field]
|
92
|
+
# removes additional params (page, id, etc..)
|
93
|
+
def remove_facet_params(field, value, source_params=params)
|
94
|
+
p = source_params.dup
|
95
|
+
# need to dup the facet values too,
|
96
|
+
# if the values aren't dup'd, then the values
|
97
|
+
# from the session will get remove in the show view...
|
98
|
+
p[:f] = (p[:f] || {}).dup
|
99
|
+
p[:f][field] = (p[:f][field] || []).dup
|
100
|
+
p.delete :page
|
101
|
+
p.delete :id
|
102
|
+
p.delete :counter
|
103
|
+
p.delete :commit
|
104
|
+
p[:f][field] = p[:f][field] - [value]
|
105
|
+
p[:f].delete(field) if p[:f][field].size == 0
|
106
|
+
p
|
107
|
+
end
|
108
|
+
|
109
|
+
# true or false, depending on whether the field and value is in params[:f]
|
110
|
+
def facet_in_params?(field, value)
|
111
|
+
params[:f] and params[:f][field] and params[:f][field].include?(value)
|
112
|
+
end
|
113
|
+
|
114
|
+
end
|
data/app/helpers/{hash_as_hidden_fields.rb → blacklight/hash_as_hidden_fields_helper_behavior.rb}
RENAMED
@@ -9,7 +9,7 @@
|
|
9
9
|
#
|
10
10
|
# This is used to serialize a complete current query from current params
|
11
11
|
# to form fields used for sort and change per-page
|
12
|
-
module
|
12
|
+
module Blacklight::HashAsHiddenFieldsHelperBehavior
|
13
13
|
|
14
14
|
# Writes out zero or more <input type="hidden"> elements, completely
|
15
15
|
# representing a hash passed in using Rails-style request parameters
|
@@ -0,0 +1,103 @@
|
|
1
|
+
module Blacklight::HtmlHeadHelperBehavior
|
2
|
+
##
|
3
|
+
# This method should be included in any Blacklight layout, including
|
4
|
+
# custom ones. It will output results of #render_js_includes,
|
5
|
+
# #render_stylesheet_includes, all the content of
|
6
|
+
# current_controller#extra_head_content as well as any content passed
|
7
|
+
# in any content_for(:head) blocks.
|
8
|
+
#
|
9
|
+
# Uses controller methods #extra_head_content, #javascript_includes,
|
10
|
+
# and #stylesheet_links to find content. Tolerates it if those
|
11
|
+
# methods don't exist, silently skipping.
|
12
|
+
#
|
13
|
+
# By a layout outputting this in html HEAD, it provides an easy way for
|
14
|
+
# local config or extra plugins to add HEAD content.
|
15
|
+
#
|
16
|
+
# Add your own css or remove the defaults by simply editing
|
17
|
+
# controller.stylesheet_links, controller.javascript_includes,
|
18
|
+
# or controller.extra_head_content.
|
19
|
+
#
|
20
|
+
#
|
21
|
+
#
|
22
|
+
# in an initializer or other startup file (plugin init.rb?):
|
23
|
+
#
|
24
|
+
# == Apply to all actions in all controllers:
|
25
|
+
#
|
26
|
+
# ApplicationController.before_filter do |controller|
|
27
|
+
# # remove default jquery-ui theme.
|
28
|
+
# controller.stylesheet_links.each do |args|
|
29
|
+
# args.delete_if {|a| a =~ /^|\/jquery-ui-[\d.]+\.custom\.css$/ }
|
30
|
+
# end
|
31
|
+
#
|
32
|
+
# # add in a different jquery-ui theme, or any other css or what have you
|
33
|
+
# controller.stylesheet_links << 'my_css.css'
|
34
|
+
#
|
35
|
+
# controller.javascript_includes << "my_local_behaviors.js"
|
36
|
+
#
|
37
|
+
# controller.extra_head_content << '<link rel="something" href="something">'
|
38
|
+
# end
|
39
|
+
#
|
40
|
+
# == Apply to a particular action in a particular controller:
|
41
|
+
#
|
42
|
+
# CatalogController.before_filter :only => :show |controller|
|
43
|
+
# controller.extra_head_content << '<link rel="something" href="something">'
|
44
|
+
# end
|
45
|
+
#
|
46
|
+
# == Or in a view file that wants to add certain header content? no problem:
|
47
|
+
#
|
48
|
+
# <% stylesheet_links << "mystylesheet.css" %>
|
49
|
+
# <% javascript_includes << "my_js.js" %>
|
50
|
+
# <% extra_head_content << capture do %>
|
51
|
+
# <%= tag :link, { :href => some_method_for_something, :rel => "alternate" } %>
|
52
|
+
# <% end %>
|
53
|
+
#
|
54
|
+
# == Full power of javascript_include_tag and stylesheet_link_tag
|
55
|
+
# Note that the elements added to stylesheet_links and javascript_links
|
56
|
+
# are arguments to Rails javascript_include_tag and stylesheet_link_tag
|
57
|
+
# respectively, you can pass complex arguments. eg:
|
58
|
+
#
|
59
|
+
# stylesheet_links << ["stylesheet1.css", "stylesheet2.css", {:cache => "mykey"}]
|
60
|
+
# javascript_includes << ["myjavascript.js", {:plugin => :myplugin} ]
|
61
|
+
def render_head_content
|
62
|
+
render_stylesheet_includes +
|
63
|
+
render_js_includes +
|
64
|
+
render_extra_head_content +
|
65
|
+
content_for(:head)
|
66
|
+
end
|
67
|
+
|
68
|
+
##
|
69
|
+
# Assumes controller has a #stylesheet_link_tag method, array with
|
70
|
+
# each element being a set of arguments for stylesheet_link_tag
|
71
|
+
# See #render_head_content for instructions on local code or plugins
|
72
|
+
# adding stylesheets.
|
73
|
+
def render_stylesheet_includes
|
74
|
+
return "".html_safe unless respond_to?(:stylesheet_links)
|
75
|
+
|
76
|
+
stylesheet_links.uniq.collect do |args|
|
77
|
+
stylesheet_link_tag(*args)
|
78
|
+
end.join("\n").html_safe
|
79
|
+
end
|
80
|
+
|
81
|
+
|
82
|
+
##
|
83
|
+
# Assumes controller has a #js_includes method, array with each
|
84
|
+
# element being a set of arguments for javsascript_include_tag.
|
85
|
+
# See #render_head_content for instructions on local code or plugins
|
86
|
+
# adding js files.
|
87
|
+
def render_js_includes
|
88
|
+
return "".html_safe unless respond_to?(:javascript_includes)
|
89
|
+
|
90
|
+
javascript_includes.uniq.collect do |args|
|
91
|
+
javascript_include_tag(*args)
|
92
|
+
end.join("\n").html_safe
|
93
|
+
end
|
94
|
+
|
95
|
+
##
|
96
|
+
# Assumes controller has a #extra_head_content method
|
97
|
+
#
|
98
|
+
def render_extra_head_content
|
99
|
+
return "".html_safe unless respond_to?(:extra_head_content)
|
100
|
+
|
101
|
+
extra_head_content.join("\n").html_safe
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
# All methods in here are 'api' that may be over-ridden by plugins and local
|
3
|
+
# code, so method signatures and semantics should not be changed casually.
|
4
|
+
# implementations can be of course.
|
5
|
+
#
|
6
|
+
# Includes methods for rendering contraints graphically on the
|
7
|
+
# search results page (render_constraints(_*)), and also
|
8
|
+
# for rendering more textually on Search History page
|
9
|
+
# (render_search_to_s(_*))
|
10
|
+
module Blacklight::RenderConstraintsHelperBehavior
|
11
|
+
|
12
|
+
# Render actual constraints, not including header or footer
|
13
|
+
# info.
|
14
|
+
def render_constraints(localized_params = params)
|
15
|
+
(render_constraints_query(localized_params) + render_constraints_filters(localized_params)).html_safe
|
16
|
+
end
|
17
|
+
|
18
|
+
def render_constraints_query(localized_params = params)
|
19
|
+
# So simple don't need a view template, we can just do it here.
|
20
|
+
if (!localized_params[:q].blank?)
|
21
|
+
label =
|
22
|
+
if (localized_params[:search_field] == Blacklight.default_search_field[:key] or localized_params[:search_field].blank? )
|
23
|
+
nil
|
24
|
+
else
|
25
|
+
Blacklight.label_for_search_field(localized_params[:search_field])
|
26
|
+
end
|
27
|
+
|
28
|
+
render_constraint_element(label,
|
29
|
+
localized_params[:q],
|
30
|
+
:classes => ["query"],
|
31
|
+
:remove => catalog_index_path(localized_params.merge(:q=>nil, :action=>'index')))
|
32
|
+
else
|
33
|
+
"".html_safe
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def render_constraints_filters(localized_params = params)
|
38
|
+
return "".html_safe unless localized_params[:f]
|
39
|
+
content = ""
|
40
|
+
localized_params[:f].each_pair do |facet,values|
|
41
|
+
values.each do |val|
|
42
|
+
content << render_constraint_element( facet_field_labels[facet],
|
43
|
+
val,
|
44
|
+
:remove => catalog_index_path(remove_facet_params(facet, val, localized_params)),
|
45
|
+
:classes => ["filter", "filter-" + facet.parameterize]
|
46
|
+
) + "\n"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
return content.html_safe
|
51
|
+
end
|
52
|
+
|
53
|
+
# Render a label/value constraint on the screen. Can be called
|
54
|
+
# by plugins and such to get application-defined rendering.
|
55
|
+
#
|
56
|
+
# Can be over-ridden locally to render differently if desired,
|
57
|
+
# although in most cases you can just change CSS instead.
|
58
|
+
#
|
59
|
+
# Can pass in nil label if desired.
|
60
|
+
#
|
61
|
+
# options:
|
62
|
+
# [:remove]
|
63
|
+
# url to execute for a 'remove' action
|
64
|
+
# [:classes]
|
65
|
+
# can be an array of classes to add to container span for constraint.
|
66
|
+
# [:escape_label]
|
67
|
+
# default true, HTML escape.
|
68
|
+
# [:escape_value]
|
69
|
+
# default true, HTML escape.
|
70
|
+
def render_constraint_element(label, value, options = {})
|
71
|
+
render(:partial => "catalog/constraints_element", :locals => {:label => label, :value => value, :options => options})
|
72
|
+
end
|
73
|
+
|
74
|
+
|
75
|
+
# Simpler textual version of constraints, used on Search History page.
|
76
|
+
# Theoretically can may be DRY'd up with results page render_constraints,
|
77
|
+
# maybe even using the very same HTML with different CSS?
|
78
|
+
# But too tricky for now, too many changes to existing CSS. TODO.
|
79
|
+
def render_search_to_s(params)
|
80
|
+
render_search_to_s_q(params) +
|
81
|
+
render_search_to_s_filters(params)
|
82
|
+
end
|
83
|
+
|
84
|
+
def render_search_to_s_q(params)
|
85
|
+
return "".html_safe if params[:q].blank?
|
86
|
+
|
87
|
+
label = (params[:search_field] == Blacklight.default_search_field[:key]) ?
|
88
|
+
nil :
|
89
|
+
Blacklight.label_for_search_field(params[:search_field])
|
90
|
+
|
91
|
+
render_search_to_s_element(label , params[:q] )
|
92
|
+
end
|
93
|
+
def render_search_to_s_filters(params)
|
94
|
+
return "".html_safe unless params[:f]
|
95
|
+
|
96
|
+
params[:f].collect do |facet_field, value_list|
|
97
|
+
render_search_to_s_element(Blacklight.config[:facet][:labels][facet_field],
|
98
|
+
value_list.collect do |value|
|
99
|
+
render_filter_value(value)
|
100
|
+
end.join(content_tag(:span, 'and', :class =>'label')).html_safe
|
101
|
+
)
|
102
|
+
end.join(" \n ").html_safe
|
103
|
+
end
|
104
|
+
|
105
|
+
# value can be Array, in which case elements are joined with
|
106
|
+
# 'and'. Pass in option :escape_value => false to pass in pre-rendered
|
107
|
+
# html for value. key with escape_key if needed.
|
108
|
+
def render_search_to_s_element(key, value, options = {})
|
109
|
+
content_tag(:span, render_filter_name(key) + render_filter_value(value), :class => 'constraint')
|
110
|
+
end
|
111
|
+
|
112
|
+
def render_filter_name name
|
113
|
+
return "".html_safe if name.blank?
|
114
|
+
content_tag(:span, h(name) + ":", :class => 'filterName')
|
115
|
+
end
|
116
|
+
|
117
|
+
def render_filter_value value
|
118
|
+
content_tag(:span, h(value), :class => 'filterValue')
|
119
|
+
end
|
120
|
+
|
121
|
+
end
|
@@ -1,397 +1,3 @@
|
|
1
|
-
# -*- encoding : utf-8 -*-
|
2
|
-
# -*- coding: utf-8 -*-
|
3
|
-
#
|
4
|
-
# Methods added to this helper will be available to all templates in the hosting application
|
5
|
-
#
|
6
1
|
module BlacklightHelper
|
7
|
-
include
|
8
|
-
include RenderConstraintsHelper
|
9
|
-
include HtmlHeadHelper
|
10
|
-
include FacetsHelper
|
11
|
-
|
12
|
-
|
13
|
-
def application_name
|
14
|
-
'Blacklight'
|
15
|
-
end
|
16
|
-
|
17
|
-
|
18
|
-
# Create <link rel="alternate"> links from a documents dynamically
|
19
|
-
# provided export formats. Currently not used by standard BL layouts,
|
20
|
-
# but available for your custom layouts to provide link rel alternates.
|
21
|
-
#
|
22
|
-
# Returns empty string if no links available.
|
23
|
-
#
|
24
|
-
# :unique => true, will ensure only one link is output for every
|
25
|
-
# content type, as required eg in atom. Which one 'wins' is arbitrary.
|
26
|
-
# :exclude => array of format shortnames, formats to not include at all.
|
27
|
-
def render_link_rel_alternates(document=@document, options = {})
|
28
|
-
options = {:unique => false, :exclude => []}.merge(options)
|
29
|
-
|
30
|
-
return nil if document.nil?
|
31
|
-
|
32
|
-
seen = Set.new
|
33
|
-
|
34
|
-
html = ""
|
35
|
-
document.export_formats.each_pair do |format, spec|
|
36
|
-
unless( options[:exclude].include?(format) ||
|
37
|
-
(options[:unique] && seen.include?(spec[:content_type]))
|
38
|
-
)
|
39
|
-
html << tag(:link, {:rel=>"alternate", :title=>format, :type => spec[:content_type], :href=> catalog_url(document.id, format)}) << "\n"
|
40
|
-
|
41
|
-
seen.add(spec[:content_type]) if options[:unique]
|
42
|
-
end
|
43
|
-
end
|
44
|
-
return html.html_safe
|
45
|
-
end
|
46
|
-
|
47
|
-
def render_opensearch_response_metadata
|
48
|
-
render :partial => 'catalog/opensearch_response_metadata'
|
49
|
-
end
|
50
|
-
|
51
|
-
def render_body_class
|
52
|
-
extra_body_classes.join " "
|
53
|
-
end
|
54
|
-
|
55
|
-
# collection of items to be rendered in the @sidebar
|
56
|
-
def sidebar_items
|
57
|
-
@sidebar_items ||= []
|
58
|
-
end
|
59
|
-
|
60
|
-
def extra_body_classes
|
61
|
-
@extra_body_classes ||= ['blacklight-' + controller.controller_name, 'blacklight-' + [controller.controller_name, controller.action_name].join('-')]
|
62
|
-
end
|
63
|
-
|
64
|
-
|
65
|
-
def render_document_list_partial options={}
|
66
|
-
render :partial=>'catalog/document_list'
|
67
|
-
end
|
68
|
-
|
69
|
-
# Save function area for search results 'index' view, normally
|
70
|
-
# renders next to title. Includes just 'Folder' by default.
|
71
|
-
def render_index_doc_actions(document, options={})
|
72
|
-
content_tag("div", :class=>"documentFunctions") do
|
73
|
-
raw("#{render(:partial => 'bookmark_control', :locals => {:document=> document}.merge(options))}
|
74
|
-
#{render(:partial => 'folder_control', :locals => {:document=> document}.merge(options))}")
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
# Save function area for item detail 'show' view, normally
|
79
|
-
# renders next to title. By default includes 'Folder' and 'Bookmarks'
|
80
|
-
def render_show_doc_actions(document=@document, options={})
|
81
|
-
content_tag("div", :class=>"documentFunctions") do
|
82
|
-
raw("#{render(:partial => 'bookmark_control', :locals => {:document=> document}.merge(options))}
|
83
|
-
#{render(:partial => 'folder_control', :locals => {:document=> document}.merge(options))}")
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
# used in the catalog/_index_partials/_default view
|
88
|
-
def index_field_names
|
89
|
-
Blacklight.config[:index_fields][:field_names]
|
90
|
-
end
|
91
|
-
|
92
|
-
# used in the _index_partials/_default view
|
93
|
-
def index_field_labels
|
94
|
-
Blacklight.config[:index_fields][:labels]
|
95
|
-
end
|
96
|
-
|
97
|
-
def spell_check_max
|
98
|
-
Blacklight.config[:spell_max] || 0
|
99
|
-
end
|
100
|
-
|
101
|
-
def render_index_field_label args
|
102
|
-
field = args[:field]
|
103
|
-
html_escape index_field_labels[field]
|
104
|
-
end
|
105
|
-
|
106
|
-
def render_index_field_value args
|
107
|
-
value = args[:value]
|
108
|
-
value ||= args[:document].get(args[:field], :sep => nil) if args[:document] and args[:field]
|
109
|
-
render_field_value value
|
110
|
-
end
|
111
|
-
|
112
|
-
# Used in the show view for displaying the main solr document heading
|
113
|
-
def document_heading
|
114
|
-
@document[Blacklight.config[:show][:heading]] || @document.id
|
115
|
-
end
|
116
|
-
def render_document_heading
|
117
|
-
content_tag(:h1, document_heading)
|
118
|
-
end
|
119
|
-
|
120
|
-
# Used in the show view for setting the main html document title
|
121
|
-
def document_show_html_title
|
122
|
-
@document[Blacklight.config[:show][:html_title]]
|
123
|
-
end
|
124
|
-
|
125
|
-
# Used in citation view for displaying the title
|
126
|
-
def citation_title(document)
|
127
|
-
document[Blacklight.config[:show][:html_title]]
|
128
|
-
end
|
129
|
-
|
130
|
-
# Used in the document_list partial (search view) for building a select element
|
131
|
-
def sort_fields
|
132
|
-
Blacklight.config[:sort_fields]
|
133
|
-
end
|
134
|
-
|
135
|
-
# Used in the document list partial (search view) for creating a link to the document show action
|
136
|
-
def document_show_link_field
|
137
|
-
Blacklight.config[:index][:show_link].to_sym
|
138
|
-
end
|
139
|
-
|
140
|
-
# Used in the search form partial for building a select tag
|
141
|
-
def search_fields
|
142
|
-
Blacklight.search_field_options_for_select
|
143
|
-
end
|
144
|
-
|
145
|
-
# used in the catalog/_show/_default partial
|
146
|
-
def document_show_fields
|
147
|
-
Blacklight.config[:show_fields][:field_names]
|
148
|
-
end
|
149
|
-
|
150
|
-
# used in the catalog/_show/_default partial
|
151
|
-
def document_show_field_labels
|
152
|
-
Blacklight.config[:show_fields][:labels]
|
153
|
-
end
|
154
|
-
|
155
|
-
def render_document_show_field_label args
|
156
|
-
field = args[:field]
|
157
|
-
html_escape document_show_field_labels[field]
|
158
|
-
end
|
159
|
-
|
160
|
-
def render_document_show_field_value args
|
161
|
-
value = args[:value]
|
162
|
-
value ||= args[:document].get(args[:field], :sep => nil) if args[:document] and args[:field]
|
163
|
-
render_field_value value
|
164
|
-
end
|
165
|
-
|
166
|
-
def render_field_value value=nil
|
167
|
-
value = [value] unless value.is_a? Array
|
168
|
-
value = value.collect { |x| x.respond_to?(:force_encoding) ? x.force_encoding("UTF-8") : x}
|
169
|
-
return value.map { |v| html_escape v }.join(field_value_separator).html_safe
|
170
|
-
end
|
171
|
-
|
172
|
-
def field_value_separator
|
173
|
-
', '
|
174
|
-
end
|
175
|
-
|
176
|
-
# Return a normalized partial name that can be used to contruct view partial path
|
177
|
-
def document_partial_name(document)
|
178
|
-
# .to_s is necessary otherwise the default return value is not always a string
|
179
|
-
# using "_" as sep. to more closely follow the views file naming conventions
|
180
|
-
# parameterize uses "-" as the default sep. which throws errors
|
181
|
-
display_type = document[Blacklight.config[:show][:display_type]]
|
182
|
-
|
183
|
-
return 'default' unless display_type
|
184
|
-
display_type = display_type.join(" ") if display_type.respond_to?(:join)
|
185
|
-
|
186
|
-
"#{display_type.gsub("-"," ")}".parameterize("_").to_s
|
187
|
-
end
|
188
|
-
|
189
|
-
# given a doc and action_name, this method attempts to render a partial template
|
190
|
-
# based on the value of doc[:format]
|
191
|
-
# if this value is blank (nil/empty) the "default" is used
|
192
|
-
# if the partial is not found, the "default" partial is rendered instead
|
193
|
-
def render_document_partial(doc, action_name)
|
194
|
-
format = document_partial_name(doc)
|
195
|
-
begin
|
196
|
-
render :partial=>"catalog/_#{action_name}_partials/#{format}", :locals=>{:document=>doc}
|
197
|
-
rescue ActionView::MissingTemplate
|
198
|
-
render :partial=>"catalog/_#{action_name}_partials/default", :locals=>{:document=>doc}
|
199
|
-
end
|
200
|
-
end
|
201
|
-
|
202
|
-
# Search History and Saved Searches display
|
203
|
-
def link_to_previous_search(params)
|
204
|
-
link_to(raw(render_search_to_s(params)), catalog_index_path(params)).html_safe
|
205
|
-
end
|
206
|
-
|
207
|
-
#
|
208
|
-
# shortcut for built-in Rails helper, "number_with_delimiter"
|
209
|
-
#
|
210
|
-
def format_num(num); number_with_delimiter(num) end
|
211
|
-
|
212
|
-
#
|
213
|
-
# link based helpers ->
|
214
|
-
#
|
215
|
-
|
216
|
-
# create link to query (e.g. spelling suggestion)
|
217
|
-
def link_to_query(query)
|
218
|
-
p = params.dup
|
219
|
-
p.delete :page
|
220
|
-
p.delete :action
|
221
|
-
p[:q]=query
|
222
|
-
link_url = catalog_index_path(p)
|
223
|
-
link_to(query, link_url)
|
224
|
-
end
|
225
|
-
|
226
|
-
def render_document_index_label doc, opts
|
227
|
-
label = nil
|
228
|
-
label ||= doc.get(opts[:label]) if opts[:label].instance_of? Symbol
|
229
|
-
label ||= opts[:label].call(doc, opts) if opts[:label].instance_of? Proc
|
230
|
-
label ||= opts[:label] if opts[:label].is_a? String
|
231
|
-
label ||= doc.id
|
232
|
-
end
|
233
|
-
|
234
|
-
# link_to_document(doc, :label=>'VIEW', :counter => 3)
|
235
|
-
# Use the catalog_path RESTful route to create a link to the show page for a specific item.
|
236
|
-
# catalog_path accepts a HashWithIndifferentAccess object. The solr query params are stored in the session,
|
237
|
-
# so we only need the +counter+ param here. We also need to know if we are viewing to document as part of search results.
|
238
|
-
def link_to_document(doc, opts={:label=>Blacklight.config[:index][:show_link].to_sym, :counter => nil, :results_view => true})
|
239
|
-
label = render_document_index_label doc, opts
|
240
|
-
link_to_with_data(label, catalog_path(doc.id), {:method => :put, :class => label.parameterize, :data => opts}).html_safe
|
241
|
-
end
|
242
|
-
|
243
|
-
# link_back_to_catalog(:label=>'Back to Search')
|
244
|
-
# Create a link back to the index screen, keeping the user's facet, query and paging choices intact by using session.
|
245
|
-
def link_back_to_catalog(opts={:label=>'Back to Search'})
|
246
|
-
query_params = session[:search] ? session[:search].dup : {}
|
247
|
-
query_params.delete :counter
|
248
|
-
query_params.delete :total
|
249
|
-
link_url = catalog_index_path + "?" + query_params.to_query
|
250
|
-
link_to opts[:label], link_url
|
251
|
-
end
|
252
|
-
|
253
|
-
# Create form input type=hidden fields representing the entire search context,
|
254
|
-
# for inclusion in a form meant to change some aspect of it, like
|
255
|
-
# re-sort or change records per page. Can pass in params hash
|
256
|
-
# as :params => hash, otherwise defaults to #params. Can pass
|
257
|
-
# in certain top-level params keys to _omit_, defaults to :page
|
258
|
-
def search_as_hidden_fields(options={})
|
259
|
-
|
260
|
-
options = {:params => params, :omit_keys => [:page]}.merge(options)
|
261
|
-
my_params = options[:params].dup
|
262
|
-
options[:omit_keys].each {|omit_key| my_params.delete(omit_key)}
|
263
|
-
# removing action and controller from duplicate params so that we don't get hidden fields for them.
|
264
|
-
my_params.delete(:action)
|
265
|
-
my_params.delete(:controller)
|
266
|
-
# commit is just an artifact of submit button, we don't need it, and
|
267
|
-
# don't want it to pile up with another every time we press submit again!
|
268
|
-
my_params.delete(:commit)
|
269
|
-
# hash_as_hidden_fields in hash_as_hidden_fields.rb
|
270
|
-
return hash_as_hidden_fields(my_params)
|
271
|
-
end
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
def link_to_previous_document(previous_document)
|
276
|
-
return if previous_document == nil
|
277
|
-
link_to_document previous_document, :label=>'« Previous', :counter => session[:search][:counter].to_i - 1
|
278
|
-
end
|
279
|
-
|
280
|
-
def link_to_next_document(next_document)
|
281
|
-
return if next_document == nil
|
282
|
-
link_to_document next_document, :label=>'Next »', :counter => session[:search][:counter].to_i + 1
|
283
|
-
end
|
284
|
-
|
285
|
-
# Use case, you want to render an html partial from an XML (say, atom)
|
286
|
-
# template. Rails API kind of lets us down, we need to hack Rails internals
|
287
|
-
# a bit. code taken from:
|
288
|
-
# http://stackoverflow.com/questions/339130/how-do-i-render-a-partial-of-a-different-format-in-rails
|
289
|
-
def with_format(format, &block)
|
290
|
-
old_format = @template_format
|
291
|
-
@template_format = format
|
292
|
-
result = block.call
|
293
|
-
@template_format = old_format
|
294
|
-
return result
|
295
|
-
end
|
296
|
-
|
297
|
-
|
298
|
-
# This is an updated +link_to+ that allows you to pass a +data+ hash along with the +html_options+
|
299
|
-
# which are then written to the generated form for non-GET requests. The key is the form element name
|
300
|
-
# and the value is the value:
|
301
|
-
#
|
302
|
-
# link_to_with_data('Name', some_path(some_id), :method => :post, :html)
|
303
|
-
def link_to_with_data(*args, &block)
|
304
|
-
if block_given?
|
305
|
-
options = args.first || {}
|
306
|
-
html_options = args.second
|
307
|
-
concat(link_to(capture(&block), options, html_options))
|
308
|
-
else
|
309
|
-
name = args.first
|
310
|
-
options = args.second || {}
|
311
|
-
html_options = args.third
|
312
|
-
|
313
|
-
url = url_for(options)
|
314
|
-
|
315
|
-
if html_options
|
316
|
-
html_options = html_options.stringify_keys
|
317
|
-
href = html_options['href']
|
318
|
-
convert_options_to_javascript_with_data!(html_options, url)
|
319
|
-
tag_options = tag_options(html_options)
|
320
|
-
else
|
321
|
-
tag_options = nil
|
322
|
-
end
|
323
|
-
|
324
|
-
href_attr = "href=\"#{url}\"" unless href
|
325
|
-
"<a #{href_attr}#{tag_options}>#{h(name) || h(url)}</a>".html_safe
|
326
|
-
end
|
327
|
-
end
|
328
|
-
|
329
|
-
# This is derived from +convert_options_to_javascript+ from module +UrlHelper+ in +url_helper.rb+
|
330
|
-
def convert_options_to_javascript_with_data!(html_options, url = '')
|
331
|
-
confirm, popup = html_options.delete("confirm"), html_options.delete("popup")
|
332
|
-
|
333
|
-
method, href = html_options.delete("method"), html_options['href']
|
334
|
-
data = html_options.delete("data")
|
335
|
-
data = data.stringify_keys if data
|
336
|
-
|
337
|
-
html_options["onclick"] = case
|
338
|
-
when method
|
339
|
-
"#{method_javascript_function_with_data(method, url, href, data)}return false;"
|
340
|
-
else
|
341
|
-
html_options["onclick"]
|
342
|
-
end
|
343
|
-
end
|
344
|
-
|
345
|
-
# This is derived from +method_javascript_function+ from module +UrlHelper+ in +url_helper.rb+
|
346
|
-
def method_javascript_function_with_data(method, url = '', href = nil, data=nil)
|
347
|
-
action = (href && url.size > 0) ? "'#{url}'" : 'this.href'
|
348
|
-
submit_function =
|
349
|
-
"var f = document.createElement('form'); f.style.display = 'none'; " +
|
350
|
-
"this.parentNode.appendChild(f); f.method = 'POST'; f.action = #{action};"+
|
351
|
-
"if(event.metaKey || event.ctrlKey){f.target = '_blank';};" # if the command or control key is being held down while the link is clicked set the form's target to _blank
|
352
|
-
if data
|
353
|
-
data.each_pair do |key, value|
|
354
|
-
submit_function << "var d = document.createElement('input'); d.setAttribute('type', 'hidden'); "
|
355
|
-
submit_function << "d.setAttribute('name', '#{key}'); d.setAttribute('value', '#{escape_javascript(value.to_s)}'); f.appendChild(d);"
|
356
|
-
end
|
357
|
-
end
|
358
|
-
unless method == :post
|
359
|
-
submit_function << "var m = document.createElement('input'); m.setAttribute('type', 'hidden'); "
|
360
|
-
submit_function << "m.setAttribute('name', '_method'); m.setAttribute('value', '#{method}'); f.appendChild(m);"
|
361
|
-
end
|
362
|
-
|
363
|
-
if protect_against_forgery?
|
364
|
-
submit_function << "var s = document.createElement('input'); s.setAttribute('type', 'hidden'); "
|
365
|
-
submit_function << "s.setAttribute('name', '#{request_forgery_protection_token}'); s.setAttribute('value', '#{escape_javascript form_authenticity_token}'); f.appendChild(s);"
|
366
|
-
end
|
367
|
-
submit_function << "f.submit();"
|
368
|
-
end
|
369
|
-
|
370
|
-
# determines if the given document id is in the folder
|
371
|
-
def item_in_folder?(doc_id)
|
372
|
-
session[:folder_document_ids] && session[:folder_document_ids].include?(doc_id) ? true : false
|
373
|
-
end
|
374
|
-
|
375
|
-
# puts together a collection of documents into one refworks export string
|
376
|
-
def render_refworks_texts(documents)
|
377
|
-
val = ''
|
378
|
-
documents.each do |doc|
|
379
|
-
if doc.respond_to?(:to_marc)
|
380
|
-
val += doc.export_as_refworks_marc_txt + "\n"
|
381
|
-
end
|
382
|
-
end
|
383
|
-
val
|
384
|
-
end
|
385
|
-
|
386
|
-
# puts together a collection of documents into one endnote export string
|
387
|
-
def render_endnote_texts(documents)
|
388
|
-
val = ''
|
389
|
-
documents.each do |doc|
|
390
|
-
if doc.respond_to?(:to_marc)
|
391
|
-
val += doc.export_as_endnote + "\n"
|
392
|
-
end
|
393
|
-
end
|
394
|
-
val
|
395
|
-
end
|
396
|
-
|
2
|
+
include Blacklight::BlacklightHelperBehavior
|
397
3
|
end
|