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