Commit 5466936544d39f042cd8a292e8445dd4563e8144
1 parent
5fce9d46
Exists in
staging
and in
42 other branches
Add facet helpers
Showing
1 changed file
with
68 additions
and
0 deletions
Show diff stats
app/helpers/search_helper.rb
... | ... | @@ -69,6 +69,74 @@ module SearchHelper |
69 | 69 | @asset_class = asset_class(asset) |
70 | 70 | render(:partial => 'facets_unselect_menu') |
71 | 71 | end |
72 | + | |
73 | + def facet_javascript(input_id, facet, array) | |
74 | + hintText = _('Type in an option') | |
75 | + text_field_tag('facet['+input_id+']', '', :id => input_id) + | |
76 | + javascript_tag("jQuery.TokenList(jQuery('##{input_id}'), #{array.to_json}, | |
77 | + {searchDelay: 0, permanentDropdown: true, theme: 'facet', dontAdd: true, preventDuplicates: true, | |
78 | + #{jquery_token_input_messages_json(hintText)}});") | |
79 | + end | |
80 | + | |
81 | + def facet_link_html(facet, params, value, label, count) | |
82 | + params = params.dup | |
83 | + has_extra = label.kind_of?(Array) | |
84 | + link_label = has_extra ? label[0] : label | |
85 | + id = facet[:solr_field].to_s | |
86 | + params[:facet] ||= {} | |
87 | + params[:facet][id] ||= {} | |
88 | + | |
89 | + selected = facet[:label_id].nil? ? params[:facet][id] == value : params[:facet][id][facet[:label_id]].to_a.include?(value) | |
90 | + | |
91 | + if count > 0 | |
92 | + url = params.merge(:facet => params[:facet].merge( | |
93 | + id => facet[:label_id].nil? ? value : params[:facet][id].merge( facet[:label_id] => params[:facet][id][facet[:label_id]].to_a.push(value) ) | |
94 | + )) | |
95 | + else | |
96 | + url = params.merge(:facet => { | |
97 | + id => facet[:label_id].nil? ? value : { facet[:label_id] => value } | |
98 | + }) | |
99 | + end | |
100 | + | |
101 | + content_tag 'div', link_to(link_label, url, :class => 'facet-result-link-label') + | |
102 | + content_tag('span', (has_extra ? label[1] : ''), :class => 'facet-result-extra-label') + | |
103 | + (count > 0 ? content_tag('span', " (#{count})", :class => 'facet-result-count') : ''), | |
104 | + :class => 'facet-menu-item' + (selected ? ' facet-result-link-selected' : '') | |
105 | + end | |
106 | + | |
107 | + def facet_selecteds_html_for(environment, klass, params) | |
108 | + def name_with_extra(klass, facet, value) | |
109 | + name = klass.facet_result_name(facet, value) | |
110 | + name = name[0] + name[1] if name.kind_of?(Array) | |
111 | + name | |
112 | + end | |
113 | + | |
114 | + ret = [] | |
115 | + params = params.dup | |
116 | + params[:facet].each do |id, value| | |
117 | + facet = klass.facet_by_id(id.to_sym) | |
118 | + if value.kind_of?(Hash) | |
119 | + label_hash = facet[:label].call(environment) | |
120 | + value.each do |label_id, value| | |
121 | + facet[:label_id] = label_id | |
122 | + facet[:label] = label_hash[label_id] | |
123 | + value.to_a.each do |value| | |
124 | + ret << [facet[:label], name_with_extra(klass, facet, value), | |
125 | + params.merge(:facet => params[:facet].merge(id => params[:facet][id].merge(label_id => params[:facet][id][label_id].to_a.reject{ |v| v == value })))] | |
126 | + end | |
127 | + end | |
128 | + else | |
129 | + ret << [facet[:label], name_with_extra(klass, facet, value), | |
130 | + params.merge(:facet => params[:facet].reject{ |k,v| k == id })] | |
131 | + end | |
132 | + end | |
133 | + | |
134 | + ret.map do |label, name, url| | |
135 | + content_tag('div', content_tag('span', label, :class => 'facet-selected-label') + | |
136 | + content_tag('span', name, :class => 'facet-selected-name') + | |
137 | + link_to('', url, :class => 'facet-selected-remove'), :class => 'facet-selected') | |
138 | + end.join | |
139 | + end | |
72 | 140 | |
73 | 141 | def asset_class(asset) |
74 | 142 | asset.to_s.singularize.camelize.constantize | ... | ... |