Tweak oob method name
[gitorious:just_paginate.git] / lib / just_paginate.rb
1 # -*- coding: utf-8 -*-
2 module JustPaginate
3
4   VERSION = "0.0.12"
5
6   # TODO make sure negative numbers, non-integers etc are just converted to page 1.
7   def self.page_value(page)
8     if page.nil?
9       1
10     else
11       page.to_i
12     end
13   end
14
15   def self.paginate(curr_page, per_page, total_entry_count,  &selection_strategy)
16     raise "Pagination just supplies index range, expects a selection strategy" if selection_strategy.nil?
17
18     entries = yield(index_range(curr_page, per_page, total_entry_count)) || []
19     return entries, total_page_number(total_entry_count, per_page)
20   end
21
22   def self.total_page_number(total_entry_count, per_page)
23     (total_entry_count.to_f / per_page).ceil
24   end
25
26   def self.index_range(curr_page, per_page, total_entry_count)
27     start_index = ((curr_page-1)*per_page)
28     end_index = (start_index+per_page)-1
29
30     if(start_index>(total_entry_count-1))
31       start_index = total_entry_count-per_page
32       end_index = total_entry_count-1
33     end
34
35     if end_index>total_entry_count
36       end_index = total_entry_count
37     end
38
39     Range.new(start_index, end_index)
40   end
41
42   def self.page_out_of_bounds?(curr_page, per_page, total_entry_count)
43     if curr_page < 1
44       true
45     else
46       start_index = ((curr_page-1)*per_page)
47       end_index = (start_index+per_page)-1
48       start_index > total_entry_count
49     end
50   end
51
52   def self.page_navigation(curr_page, total_page_count, &page_link_constructor)
53     links = page_links(curr_page.to_i, total_page_count, &page_link_constructor)
54     return "<div class='pagination'>#{links}</div>"
55   end
56
57   def self.page_links(curr_page, total_page_count, &page_link_constructor)
58     page_labels(curr_page, total_page_count).map do |label|
59       page_element = ""
60
61       if label == "..."
62         page_element = "<span class='gap'>#{label}</span>"
63       elsif label == "<"
64         page_url = yield(curr_page-1)
65         page_element = "<a rel='prev' href='#{page_url}'>#{label}</a>"
66       elsif label == ">"
67         page_url = yield(curr_page+1)
68         page_element = "<a rel='next' href='#{page_url}'>#{label}</a>"
69       else
70         page_url = yield(label)
71         if label.to_i == curr_page
72           page_element = "<span class='current'>#{label}</span>"
73         else
74           page_element = "<a href='#{page_url}'>#{label}</a>"
75         end
76       end
77
78     end.join(" ")
79   end
80
81   def self.page_labels(current, total)
82     max_visible = 10
83     labels = []
84
85     if total <= max_visible
86       1.upto(total).each {|n| labels.push(n.to_s)}
87     else
88       if current > max_visible
89         labels = ["<", "1", "..."]
90       else
91         current = 1
92       end
93
94       if (current > (total-max_visible))
95         current = (total-max_visible)+1
96       end
97
98       current.upto(current+(max_visible-1)).each {|n| labels.push(n.to_s)}
99
100       if (current <= (total-max_visible))
101         labels.concat ["...", "#{total}", ">"]
102       end
103     end
104
105     return labels
106   end
107
108 end