annotate lib/redmine/pagination.rb @ 1524:82fac3dcf466 redmine-2.5-integration

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