Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

pagination.rb 7.7KB

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