##// END OF EJS Templates
Removed a space after spacer (#21258)....
Jean-Philippe Lang -
r14516:b2db1f5f193d
parent child
Show More
@@ -1,252 +1,252
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 first_item
70 def first_item
71 item_count == 0 ? 0 : (offset + 1)
71 item_count == 0 ? 0 : (offset + 1)
72 end
72 end
73
73
74 def last_item
74 def last_item
75 l = first_item + per_page - 1
75 l = first_item + per_page - 1
76 l > item_count ? item_count : l
76 l > item_count ? item_count : l
77 end
77 end
78
78
79 def linked_pages
79 def linked_pages
80 pages = []
80 pages = []
81 if item_count > 0
81 if item_count > 0
82 pages += [first_page, page, last_page]
82 pages += [first_page, page, last_page]
83 pages += ((page-2)..(page+2)).to_a.select {|p| p > first_page && p < last_page}
83 pages += ((page-2)..(page+2)).to_a.select {|p| p > first_page && p < last_page}
84 end
84 end
85 pages = pages.compact.uniq.sort
85 pages = pages.compact.uniq.sort
86 if pages.size > 1
86 if pages.size > 1
87 pages
87 pages
88 else
88 else
89 []
89 []
90 end
90 end
91 end
91 end
92
92
93 def items_per_page
93 def items_per_page
94 ActiveSupport::Deprecation.warn "Paginator#items_per_page will be removed. Use #per_page instead."
94 ActiveSupport::Deprecation.warn "Paginator#items_per_page will be removed. Use #per_page instead."
95 per_page
95 per_page
96 end
96 end
97
97
98 def current
98 def current
99 ActiveSupport::Deprecation.warn "Paginator#current will be removed. Use .offset instead of .current.offset."
99 ActiveSupport::Deprecation.warn "Paginator#current will be removed. Use .offset instead of .current.offset."
100 self
100 self
101 end
101 end
102 end
102 end
103
103
104 # Paginates the given scope or model. Returns a Paginator instance and
104 # Paginates the given scope or model. Returns a Paginator instance and
105 # the collection of objects for the current page.
105 # the collection of objects for the current page.
106 #
106 #
107 # Options:
107 # Options:
108 # :parameter name of the page parameter
108 # :parameter name of the page parameter
109 #
109 #
110 # Examples:
110 # Examples:
111 # @user_pages, @users = paginate User.where(:status => 1)
111 # @user_pages, @users = paginate User.where(:status => 1)
112 #
112 #
113 def paginate(scope, options={})
113 def paginate(scope, options={})
114 options = options.dup
114 options = options.dup
115 finder_options = options.extract!(
115 finder_options = options.extract!(
116 :conditions,
116 :conditions,
117 :order,
117 :order,
118 :joins,
118 :joins,
119 :include,
119 :include,
120 :select
120 :select
121 )
121 )
122 if scope.is_a?(Symbol) || finder_options.values.compact.any?
122 if scope.is_a?(Symbol) || finder_options.values.compact.any?
123 return deprecated_paginate(scope, finder_options, options)
123 return deprecated_paginate(scope, finder_options, options)
124 end
124 end
125
125
126 paginator = paginator(scope.count, options)
126 paginator = paginator(scope.count, options)
127 collection = scope.limit(paginator.per_page).offset(paginator.offset).to_a
127 collection = scope.limit(paginator.per_page).offset(paginator.offset).to_a
128
128
129 return paginator, collection
129 return paginator, collection
130 end
130 end
131
131
132 def deprecated_paginate(arg, finder_options, options={})
132 def deprecated_paginate(arg, finder_options, options={})
133 ActiveSupport::Deprecation.warn "#paginate with a Symbol and/or find options is depreceted and will be removed. Use a scope instead."
133 ActiveSupport::Deprecation.warn "#paginate with a Symbol and/or find options is depreceted and will be removed. Use a scope instead."
134 klass = arg.is_a?(Symbol) ? arg.to_s.classify.constantize : arg
134 klass = arg.is_a?(Symbol) ? arg.to_s.classify.constantize : arg
135 scope = klass.scoped(finder_options)
135 scope = klass.scoped(finder_options)
136 paginate(scope, options)
136 paginate(scope, options)
137 end
137 end
138
138
139 def paginator(item_count, options={})
139 def paginator(item_count, options={})
140 options.assert_valid_keys :parameter, :per_page
140 options.assert_valid_keys :parameter, :per_page
141
141
142 page_param = options[:parameter] || :page
142 page_param = options[:parameter] || :page
143 page = (params[page_param] || 1).to_i
143 page = (params[page_param] || 1).to_i
144 per_page = options[:per_page] || per_page_option
144 per_page = options[:per_page] || per_page_option
145 Paginator.new(item_count, per_page, page, page_param)
145 Paginator.new(item_count, per_page, page, page_param)
146 end
146 end
147
147
148 module Helper
148 module Helper
149 include Redmine::I18n
149 include Redmine::I18n
150
150
151 # Renders the pagination links for the given paginator.
151 # Renders the pagination links for the given paginator.
152 #
152 #
153 # Options:
153 # Options:
154 # :per_page_links if set to false, the "Per page" links are not rendered
154 # :per_page_links if set to false, the "Per page" links are not rendered
155 #
155 #
156 def pagination_links_full(*args)
156 def pagination_links_full(*args)
157 pagination_links_each(*args) do |text, parameters, options|
157 pagination_links_each(*args) do |text, parameters, options|
158 if block_given?
158 if block_given?
159 yield text, parameters, options
159 yield text, parameters, options
160 else
160 else
161 link_to text, params.merge(parameters), options
161 link_to text, params.merge(parameters), options
162 end
162 end
163 end
163 end
164 end
164 end
165
165
166 # Yields the given block with the text and parameters
166 # Yields the given block with the text and parameters
167 # for each pagination link and returns a string that represents the links
167 # for each pagination link and returns a string that represents the links
168 def pagination_links_each(paginator, count=nil, options={}, &block)
168 def pagination_links_each(paginator, count=nil, options={}, &block)
169 options.assert_valid_keys :per_page_links
169 options.assert_valid_keys :per_page_links
170
170
171 per_page_links = options.delete(:per_page_links)
171 per_page_links = options.delete(:per_page_links)
172 per_page_links = false if count.nil?
172 per_page_links = false if count.nil?
173 page_param = paginator.page_param
173 page_param = paginator.page_param
174
174
175 html = '<ul class="pages">'
175 html = '<ul class="pages">'
176 if paginator.previous_page
176 if paginator.previous_page
177 # \xc2\xab(utf-8) = &#171;
177 # \xc2\xab(utf-8) = &#171;
178 text = "\xc2\xab " + l(:label_previous)
178 text = "\xc2\xab " + l(:label_previous)
179 html << content_tag('li',
179 html << content_tag('li',
180 yield(text, {page_param => paginator.previous_page},
180 yield(text, {page_param => paginator.previous_page},
181 :accesskey => accesskey(:previous)),
181 :accesskey => accesskey(:previous)),
182 :class => 'previous page')
182 :class => 'previous page')
183 end
183 end
184
184
185 previous = nil
185 previous = nil
186 paginator.linked_pages.each do |page|
186 paginator.linked_pages.each do |page|
187 if previous && previous != page - 1
187 if previous && previous != page - 1
188 html << content_tag('li', content_tag('span', '...'), :class => 'spacer') + ' '
188 html << content_tag('li', content_tag('span', '...'), :class => 'spacer')
189 end
189 end
190 if page == paginator.page
190 if page == paginator.page
191 html << content_tag('li', content_tag('span', page.to_s), :class => 'current')
191 html << content_tag('li', content_tag('span', page.to_s), :class => 'current')
192 else
192 else
193 html << content_tag('li',
193 html << content_tag('li',
194 yield(page.to_s, {page_param => page}),
194 yield(page.to_s, {page_param => page}),
195 :class => 'page')
195 :class => 'page')
196 end
196 end
197 previous = page
197 previous = page
198 end
198 end
199
199
200 if paginator.next_page
200 if paginator.next_page
201 # \xc2\xbb(utf-8) = &#187;
201 # \xc2\xbb(utf-8) = &#187;
202 text = l(:label_next) + " \xc2\xbb"
202 text = l(:label_next) + " \xc2\xbb"
203 html << content_tag('li',
203 html << content_tag('li',
204 yield(text, {page_param => paginator.next_page},
204 yield(text, {page_param => paginator.next_page},
205 :accesskey => accesskey(:next)),
205 :accesskey => accesskey(:next)),
206 :class => 'next page')
206 :class => 'next page')
207 end
207 end
208 html << '</ul>'
208 html << '</ul>'
209
209
210 html << content_tag('span', "(#{paginator.first_item}-#{paginator.last_item}/#{paginator.item_count})", :class => 'items') + ' '
210 html << content_tag('span', "(#{paginator.first_item}-#{paginator.last_item}/#{paginator.item_count})", :class => 'items') + ' '
211
211
212 if per_page_links != false && links = per_page_links(paginator, &block)
212 if per_page_links != false && links = per_page_links(paginator, &block)
213 html << content_tag('span', links.to_s, :class => 'per-page')
213 html << content_tag('span', links.to_s, :class => 'per-page')
214 end
214 end
215
215
216 html.html_safe
216 html.html_safe
217 end
217 end
218
218
219 # Renders the "Per page" links.
219 # Renders the "Per page" links.
220 def per_page_links(paginator, &block)
220 def per_page_links(paginator, &block)
221 values = per_page_options(paginator.per_page, paginator.item_count)
221 values = per_page_options(paginator.per_page, paginator.item_count)
222 if values.any?
222 if values.any?
223 links = values.collect do |n|
223 links = values.collect do |n|
224 if n == paginator.per_page
224 if n == paginator.per_page
225 content_tag('span', n.to_s)
225 content_tag('span', n.to_s)
226 else
226 else
227 yield(n, :per_page => n, paginator.page_param => nil)
227 yield(n, :per_page => n, paginator.page_param => nil)
228 end
228 end
229 end
229 end
230 l(:label_display_per_page, links.join(', ')).html_safe
230 l(:label_display_per_page, links.join(', ')).html_safe
231 end
231 end
232 end
232 end
233
233
234 def per_page_options(selected=nil, item_count=nil)
234 def per_page_options(selected=nil, item_count=nil)
235 options = Setting.per_page_options_array
235 options = Setting.per_page_options_array
236 if item_count && options.any?
236 if item_count && options.any?
237 if item_count > options.first
237 if item_count > options.first
238 max = options.detect {|value| value >= item_count} || item_count
238 max = options.detect {|value| value >= item_count} || item_count
239 else
239 else
240 max = item_count
240 max = item_count
241 end
241 end
242 options = options.select {|value| value <= max || value == selected}
242 options = options.select {|value| value <= max || value == selected}
243 end
243 end
244 if options.empty? || (options.size == 1 && options.first == selected)
244 if options.empty? || (options.size == 1 && options.first == selected)
245 []
245 []
246 else
246 else
247 options
247 options
248 end
248 end
249 end
249 end
250 end
250 end
251 end
251 end
252 end
252 end
General Comments 0
You need to be logged in to leave comments. Login now