You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pagination.rb 7.7KB

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