Module: CategoryFormat

Includes:
GeneralUtils
Included in:
DocsHelper
Defined in:
app/helpers/category_format.rb

Instance Method Summary collapse

Methods included from GeneralUtils

#genLink, #getHR, #get_date_index, #get_search_param, #paramMatch?

Instance Method Details

#combinedHTML(top_results, overflow_results, categories_chosen, category_name, field, total_count) ⇒ Object

Generates HTML for category


50
51
52
53
54
55
# File 'app/helpers/category_format.rb', line 50

def combinedHTML(top_results, overflow_results, categories_chosen, category_name, field, total_count)
  outhtml = genPartialHTML(top_results, false, categories_chosen, category_name, field, total_count)
  outhtml += genPartialHTML(overflow_results, true, categories_chosen, category_name, field, total_count) if overflow_results
  outhtml += "</ul></li></ul><br />"
  return outhtml
end

#facetFormat(facets) ⇒ Object

Format all facets on sidebar


6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/helpers/category_format.rb', line 6

def facetFormat(facets)
  outhtml = ""
  
  # Go through all facets
  sortFields(@all_facets.keys, @all_field_info).each do |field|
    # sortFields(@facet_fields).each do |field|
    total_count = facets[field]["terms"].count.to_s
    outhtml += genFacet(facets, "", field, total_count) if facets[field]["terms"].count > 0
  end

  return outhtml
end

#genFacet(categories, outhtml, field, total_count) ⇒ Object

Gen html for list of links for each facet


20
21
22
23
24
25
26
# File 'app/helpers/category_format.rb', line 20

def genFacet(categories, outhtml, field, total_count)
  category_name = field.to_s+"_facet" 
  categories_chosen = params[category_name] 

  top_results, overflow_results = splitResults(categories, field)
  return combinedHTML(top_results, overflow_results, categories_chosen, category_name, field, total_count)
end

#genPartialHTML(items, is_overflow, categories_chosen, category_name, field, total_count) ⇒ Object

Generates the html for single category


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/helpers/category_format.rb', line 58

def genPartialHTML(items, is_overflow, categories_chosen, category_name, field, total_count)
  field_spec = getFieldDetails(field.to_s, @all_field_info) # Get field display details

  if is_overflow
    list_html = '<li><label class="tree-toggler nav-header plus"></label>
                      <ul class="nav nav-list tree collapse">'
  else
    list_html = '<ul class="nav nav-list">
                   <li>
                      <label class="tree-toggler nav-header just-plus" title="' + total_count + ' Filters">
                        ' + image_tag(field_spec["Icon"]+"-24.png") + field_spec["Human Readable Name"] + '
                      </label>
                      <ul class="nav nav-list tree collapse">'
  end

  # Generate link text for each item
  items.each do |i|
    list_html += termLink(i, categories_chosen, category_name).to_s
  end

  list_html += "</li></ul>" if is_overflow
  
  return list_html
end

#sortResults(categories, field) ⇒ Object

Sorts facets by number of results


45
46
47
# File 'app/helpers/category_format.rb', line 45

def sortResults(categories, field)
  return categories[field]["terms"].sort {|a,b| b["count"] <=> a["count"]}
end

#splitResults(categories, field) ⇒ Object

Splits the results into overflow/not overflow


29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/helpers/category_format.rb', line 29

def splitResults(categories, field)
  # Overflow calculation settings                             
  totalnum = categories[field]["terms"].count
  numshow = totalnum > 5 ? 5+totalnum*0.01 : totalnum
  
  # Divides list of terms
  sorted_results = sortResults(categories, field)
  top_results = sorted_results[0..numshow]
  overflow_results = sorted_results[numshow+1..sorted_results.length-1]

  # Don't allow crazy long facet lists
  processed_overflow = overflow_results[0..500] if overflow_results
  return top_results, processed_overflow
end