##// END OF EJS Templates
Replaced the 3 dots with an ellipsis sign....
Jean-Philippe Lang -
r14566:5f35caa6bfb8
parent child
Show More
@@ -1,266 +1,266
1 1 # encoding: utf-8
2 2 #
3 3 # Redmine - project management software
4 4 # Copyright (C) 2006-2015 Jean-Philippe Lang
5 5 #
6 6 # This program is free software; you can redistribute it and/or
7 7 # modify it under the terms of the GNU General Public License
8 8 # as published by the Free Software Foundation; either version 2
9 9 # of the License, or (at your option) any later version.
10 10 #
11 11 # This program is distributed in the hope that it will be useful,
12 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 14 # GNU General Public License for more details.
15 15 #
16 16 # You should have received a copy of the GNU General Public License
17 17 # along with this program; if not, write to the Free Software
18 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 19
20 20 module Redmine
21 21 module Pagination
22 22 class Paginator
23 23 attr_reader :item_count, :per_page, :page, :page_param
24 24
25 25 def initialize(*args)
26 26 if args.first.is_a?(ActionController::Base)
27 27 args.shift
28 28 ActiveSupport::Deprecation.warn "Paginator no longer takes a controller instance as the first argument. Remove it from #new arguments."
29 29 end
30 30 item_count, per_page, page, page_param = *args
31 31
32 32 @item_count = item_count
33 33 @per_page = per_page
34 34 page = (page || 1).to_i
35 35 if page < 1
36 36 page = 1
37 37 end
38 38 @page = page
39 39 @page_param = page_param || :page
40 40 end
41 41
42 42 def offset
43 43 (page - 1) * per_page
44 44 end
45 45
46 46 def first_page
47 47 if item_count > 0
48 48 1
49 49 end
50 50 end
51 51
52 52 def previous_page
53 53 if page > 1
54 54 page - 1
55 55 end
56 56 end
57 57
58 58 def next_page
59 59 if last_item < item_count
60 60 page + 1
61 61 end
62 62 end
63 63
64 64 def last_page
65 65 if item_count > 0
66 66 (item_count - 1) / per_page + 1
67 67 end
68 68 end
69 69
70 70 def multiple_pages?
71 71 per_page < item_count
72 72 end
73 73
74 74 def first_item
75 75 item_count == 0 ? 0 : (offset + 1)
76 76 end
77 77
78 78 def last_item
79 79 l = first_item + per_page - 1
80 80 l > item_count ? item_count : l
81 81 end
82 82
83 83 def linked_pages
84 84 pages = []
85 85 if item_count > 0
86 86 pages += [first_page, page, last_page]
87 87 pages += ((page-2)..(page+2)).to_a.select {|p| p > first_page && p < last_page}
88 88 end
89 89 pages = pages.compact.uniq.sort
90 90 if pages.size > 1
91 91 pages
92 92 else
93 93 []
94 94 end
95 95 end
96 96
97 97 def items_per_page
98 98 ActiveSupport::Deprecation.warn "Paginator#items_per_page will be removed. Use #per_page instead."
99 99 per_page
100 100 end
101 101
102 102 def current
103 103 ActiveSupport::Deprecation.warn "Paginator#current will be removed. Use .offset instead of .current.offset."
104 104 self
105 105 end
106 106 end
107 107
108 108 # Paginates the given scope or model. Returns a Paginator instance and
109 109 # the collection of objects for the current page.
110 110 #
111 111 # Options:
112 112 # :parameter name of the page parameter
113 113 #
114 114 # Examples:
115 115 # @user_pages, @users = paginate User.where(:status => 1)
116 116 #
117 117 def paginate(scope, options={})
118 118 options = options.dup
119 119 finder_options = options.extract!(
120 120 :conditions,
121 121 :order,
122 122 :joins,
123 123 :include,
124 124 :select
125 125 )
126 126 if scope.is_a?(Symbol) || finder_options.values.compact.any?
127 127 return deprecated_paginate(scope, finder_options, options)
128 128 end
129 129
130 130 paginator = paginator(scope.count, options)
131 131 collection = scope.limit(paginator.per_page).offset(paginator.offset).to_a
132 132
133 133 return paginator, collection
134 134 end
135 135
136 136 def deprecated_paginate(arg, finder_options, options={})
137 137 ActiveSupport::Deprecation.warn "#paginate with a Symbol and/or find options is depreceted and will be removed. Use a scope instead."
138 138 klass = arg.is_a?(Symbol) ? arg.to_s.classify.constantize : arg
139 139 scope = klass.scoped(finder_options)
140 140 paginate(scope, options)
141 141 end
142 142
143 143 def paginator(item_count, options={})
144 144 options.assert_valid_keys :parameter, :per_page
145 145
146 146 page_param = options[:parameter] || :page
147 147 page = (params[page_param] || 1).to_i
148 148 per_page = options[:per_page] || per_page_option
149 149 Paginator.new(item_count, per_page, page, page_param)
150 150 end
151 151
152 152 module Helper
153 153 include Redmine::I18n
154 154
155 155 # Renders the pagination links for the given paginator.
156 156 #
157 157 # Options:
158 158 # :per_page_links if set to false, the "Per page" links are not rendered
159 159 #
160 160 def pagination_links_full(*args)
161 161 pagination_links_each(*args) do |text, parameters, options|
162 162 if block_given?
163 163 yield text, parameters, options
164 164 else
165 165 link_to text, params.merge(parameters), options
166 166 end
167 167 end
168 168 end
169 169
170 170 # Yields the given block with the text and parameters
171 171 # for each pagination link and returns a string that represents the links
172 172 def pagination_links_each(paginator, count=nil, options={}, &block)
173 173 options.assert_valid_keys :per_page_links
174 174
175 175 per_page_links = options.delete(:per_page_links)
176 176 per_page_links = false if count.nil?
177 177 page_param = paginator.page_param
178 178
179 179 html = '<ul class="pages">'
180 180
181 181 if paginator.multiple_pages?
182 182 # \xc2\xab(utf-8) = &#171;
183 183 text = "\xc2\xab " + l(:label_previous)
184 184 if paginator.previous_page
185 185 html << content_tag('li',
186 186 yield(text, {page_param => paginator.previous_page},
187 187 :accesskey => accesskey(:previous)),
188 188 :class => 'previous page')
189 189 else
190 190 html << content_tag('li', content_tag('span', text), :class => 'previous')
191 191 end
192 192 end
193 193
194 194 previous = nil
195 195 paginator.linked_pages.each do |page|
196 196 if previous && previous != page - 1
197 html << content_tag('li', content_tag('span', '...'), :class => 'spacer')
197 html << content_tag('li', content_tag('span', '&hellip;'.html_safe), :class => 'spacer')
198 198 end
199 199 if page == paginator.page
200 200 html << content_tag('li', content_tag('span', page.to_s), :class => 'current')
201 201 else
202 202 html << content_tag('li',
203 203 yield(page.to_s, {page_param => page}),
204 204 :class => 'page')
205 205 end
206 206 previous = page
207 207 end
208 208
209 209 if paginator.multiple_pages?
210 210 # \xc2\xbb(utf-8) = &#187;
211 211 text = l(:label_next) + " \xc2\xbb"
212 212 if paginator.next_page
213 213 html << content_tag('li',
214 214 yield(text, {page_param => paginator.next_page},
215 215 :accesskey => accesskey(:next)),
216 216 :class => 'next page')
217 217 else
218 218 html << content_tag('li', content_tag('span', text), :class => 'next')
219 219 end
220 220 end
221 221 html << '</ul>'
222 222
223 223 info = ''.html_safe
224 224 info << content_tag('span', "(#{paginator.first_item}-#{paginator.last_item}/#{paginator.item_count})", :class => 'items') + ' '
225 225 if per_page_links != false && links = per_page_links(paginator, &block)
226 226 info << content_tag('span', links.to_s, :class => 'per-page')
227 227 end
228 228 html << content_tag('span', info)
229 229
230 230 html.html_safe
231 231 end
232 232
233 233 # Renders the "Per page" links.
234 234 def per_page_links(paginator, &block)
235 235 values = per_page_options(paginator.per_page, paginator.item_count)
236 236 if values.any?
237 237 links = values.collect do |n|
238 238 if n == paginator.per_page
239 239 content_tag('span', n.to_s)
240 240 else
241 241 yield(n, :per_page => n, paginator.page_param => nil)
242 242 end
243 243 end
244 244 l(:label_display_per_page, links.join(', ')).html_safe
245 245 end
246 246 end
247 247
248 248 def per_page_options(selected=nil, item_count=nil)
249 249 options = Setting.per_page_options_array
250 250 if item_count && options.any?
251 251 if item_count > options.first
252 252 max = options.detect {|value| value >= item_count} || item_count
253 253 else
254 254 max = item_count
255 255 end
256 256 options = options.select {|value| value <= max || value == selected}
257 257 end
258 258 if options.empty? || (options.size == 1 && options.first == selected)
259 259 []
260 260 else
261 261 options
262 262 end
263 263 end
264 264 end
265 265 end
266 266 end
General Comments 0
You need to be logged in to leave comments. Login now