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