annotate .svn/pristine/13/138f43d777efffd6ecf4f9d5f88f24c526eb7287.svn-base @ 1327:287f201c2802 redmine-2.2-integration

Add italic
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Wed, 19 Jun 2013 20:56:22 +0100
parents cbb26bc654de
children
rev   line source
Chris@909 1 module ActionView
Chris@909 2 module Helpers
Chris@909 3 # Provides methods for linking to ActionController::Pagination objects using a simple generator API. You can optionally
Chris@909 4 # also build your links manually using ActionView::Helpers::AssetHelper#link_to like so:
Chris@909 5 #
Chris@909 6 # <%= link_to "Previous page", { :page => paginator.current.previous } if paginator.current.previous %>
Chris@909 7 # <%= link_to "Next page", { :page => paginator.current.next } if paginator.current.next %>
Chris@909 8 module PaginationHelper
Chris@909 9 unless const_defined?(:DEFAULT_OPTIONS)
Chris@909 10 DEFAULT_OPTIONS = {
Chris@909 11 :name => :page,
Chris@909 12 :window_size => 2,
Chris@909 13 :always_show_anchors => true,
Chris@909 14 :link_to_current_page => false,
Chris@909 15 :params => {}
Chris@909 16 }
Chris@909 17 end
Chris@909 18
Chris@909 19 # Creates a basic HTML link bar for the given +paginator+. Links will be created
Chris@909 20 # for the next and/or previous page and for a number of other pages around the current
Chris@909 21 # pages position. The +html_options+ hash is passed to +link_to+ when the links are created.
Chris@909 22 #
Chris@909 23 # ==== Options
Chris@909 24 # <tt>:name</tt>:: the routing name for this paginator
Chris@909 25 # (defaults to +page+)
Chris@909 26 # <tt>:prefix</tt>:: prefix for pagination links
Chris@909 27 # (i.e. Older Pages: 1 2 3 4)
Chris@909 28 # <tt>:suffix</tt>:: suffix for pagination links
Chris@909 29 # (i.e. 1 2 3 4 <- Older Pages)
Chris@909 30 # <tt>:window_size</tt>:: the number of pages to show around
Chris@909 31 # the current page (defaults to <tt>2</tt>)
Chris@909 32 # <tt>:always_show_anchors</tt>:: whether or not the first and last
Chris@909 33 # pages should always be shown
Chris@909 34 # (defaults to +true+)
Chris@909 35 # <tt>:link_to_current_page</tt>:: whether or not the current page
Chris@909 36 # should be linked to (defaults to
Chris@909 37 # +false+)
Chris@909 38 # <tt>:params</tt>:: any additional routing parameters
Chris@909 39 # for page URLs
Chris@909 40 #
Chris@909 41 # ==== Examples
Chris@909 42 # # We'll assume we have a paginator setup in @person_pages...
Chris@909 43 #
Chris@909 44 # pagination_links(@person_pages)
Chris@909 45 # # => 1 <a href="/?page=2/">2</a> <a href="/?page=3/">3</a> ... <a href="/?page=10/">10</a>
Chris@909 46 #
Chris@909 47 # pagination_links(@person_pages, :link_to_current_page => true)
Chris@909 48 # # => <a href="/?page=1/">1</a> <a href="/?page=2/">2</a> <a href="/?page=3/">3</a> ... <a href="/?page=10/">10</a>
Chris@909 49 #
Chris@909 50 # pagination_links(@person_pages, :always_show_anchors => false)
Chris@909 51 # # => 1 <a href="/?page=2/">2</a> <a href="/?page=3/">3</a>
Chris@909 52 #
Chris@909 53 # pagination_links(@person_pages, :window_size => 1)
Chris@909 54 # # => 1 <a href="/?page=2/">2</a> ... <a href="/?page=10/">10</a>
Chris@909 55 #
Chris@909 56 # pagination_links(@person_pages, :params => { :viewer => "flash" })
Chris@909 57 # # => 1 <a href="/?page=2&amp;viewer=flash/">2</a> <a href="/?page=3&amp;viewer=flash/">3</a> ...
Chris@909 58 # # <a href="/?page=10&amp;viewer=flash/">10</a>
Chris@909 59 def pagination_links(paginator, options={}, html_options={})
Chris@909 60 name = options[:name] || DEFAULT_OPTIONS[:name]
Chris@909 61 params = (options[:params] || DEFAULT_OPTIONS[:params]).clone
Chris@909 62
Chris@909 63 prefix = options[:prefix] || ''
Chris@909 64 suffix = options[:suffix] || ''
Chris@909 65
Chris@909 66 pagination_links_each(paginator, options, prefix, suffix) do |n|
Chris@909 67 params[name] = n
Chris@909 68 link_to(n.to_s, params, html_options)
Chris@909 69 end
Chris@909 70 end
Chris@909 71
Chris@909 72 # Iterate through the pages of a given +paginator+, invoking a
Chris@909 73 # block for each page number that needs to be rendered as a link.
Chris@909 74 #
Chris@909 75 # ==== Options
Chris@909 76 # <tt>:window_size</tt>:: the number of pages to show around
Chris@909 77 # the current page (defaults to +2+)
Chris@909 78 # <tt>:always_show_anchors</tt>:: whether or not the first and last
Chris@909 79 # pages should always be shown
Chris@909 80 # (defaults to +true+)
Chris@909 81 # <tt>:link_to_current_page</tt>:: whether or not the current page
Chris@909 82 # should be linked to (defaults to
Chris@909 83 # +false+)
Chris@909 84 #
Chris@909 85 # ==== Example
Chris@909 86 # # Turn paginated links into an Ajax call
Chris@909 87 # pagination_links_each(paginator, page_options) do |link|
Chris@909 88 # options = { :url => {:action => 'list'}, :update => 'results' }
Chris@909 89 # html_options = { :href => url_for(:action => 'list') }
Chris@909 90 #
Chris@909 91 # link_to_remote(link.to_s, options, html_options)
Chris@909 92 # end
Chris@909 93 def pagination_links_each(paginator, options, prefix = nil, suffix = nil)
Chris@909 94 options = DEFAULT_OPTIONS.merge(options)
Chris@909 95 link_to_current_page = options[:link_to_current_page]
Chris@909 96 always_show_anchors = options[:always_show_anchors]
Chris@909 97
Chris@909 98 current_page = paginator.current_page
Chris@909 99 window_pages = current_page.window(options[:window_size]).pages
Chris@909 100 return if window_pages.length <= 1 unless link_to_current_page
Chris@909 101
Chris@909 102 first, last = paginator.first, paginator.last
Chris@909 103
Chris@909 104 html = ''
Chris@909 105
Chris@909 106 html << prefix if prefix
Chris@909 107
Chris@909 108 if always_show_anchors and not (wp_first = window_pages[0]).first?
Chris@909 109 html << yield(first.number)
Chris@909 110 html << ' ... ' if wp_first.number - first.number > 1
Chris@909 111 html << ' '
Chris@909 112 end
Chris@909 113
Chris@909 114 window_pages.each do |page|
Chris@909 115 if current_page == page && !link_to_current_page
Chris@909 116 html << page.number.to_s
Chris@909 117 else
Chris@909 118 html << yield(page.number)
Chris@909 119 end
Chris@909 120 html << ' '
Chris@909 121 end
Chris@909 122
Chris@909 123 if always_show_anchors and not (wp_last = window_pages[-1]).last?
Chris@909 124 html << ' ... ' if last.number - wp_last.number > 1
Chris@909 125 html << yield(last.number)
Chris@909 126 end
Chris@909 127
Chris@909 128 html << suffix if suffix
Chris@909 129
Chris@909 130 html
Chris@909 131 end
Chris@909 132
Chris@909 133 end # PaginationHelper
Chris@909 134 end # Helpers
Chris@909 135 end # ActionView