1
0
Fork 0
mirror of https://github.com/kaminari/kaminari.git synced 2022-11-09 13:44:37 -05:00

refs #30 add real first/last page links, and use them by default instead of outer window

* :window, :outer_window, :left, :right will be interpreted similarly to how they were, except the supplied N would be the number of pages you actually get. 0 means disable for the outer windows
* outer windows will be disabled by default in favor of first/last links
* include a "disabled" class when appropriate, and always include the label text (but no link tag when current page). Users that want these tags to disappear completely when inapplicable can style them to display: none. (Similarly for "first" and "last".)
* changed the default labels - double angle quotes for "first" and "last", single angle quotes for "previous" and "next". (Consider a standard set of video or audio transport controls: << < stop > >>)
This commit is contained in:
Akira Matsuda 2011-02-25 12:04:59 +09:00
parent 4beef97cea
commit 393943a50e
21 changed files with 105 additions and 109 deletions

View file

@ -0,0 +1,11 @@
<%# Link to the "First" page
- available local variables
url: url to the first page
current_page: the page number of currently displayed page
num_pages: total number of pages
per_page: number of items to fetch per page
remote: data-remote
-%>
<span class="first<%= ' disabled' if current_page == 1 %>">
<%= link_to_unless current_page == 1, raw(t 'views.pagination.first'), url, :class => 'prev', :rel => 'prev', :remote => remote %>
</span>

View file

@ -0,0 +1,9 @@
-# Link to the "First" page
- available local variables
url: url to the first page
current_page: the page number of currently displayed page
num_pages: total number of pages
per_page: number of items to fetch per page
remote: data-remote
%span{:class => "first#{' disabled' if current_page == 1}"}
= link_to_unless current_page == 1, raw(t 'views.pagination.first'), url, :class => 'prev', :rel => 'prev', :remote => remote

View file

@ -0,0 +1,11 @@
<%# Link to the "Last" page
- available local variables
url: url to the last page
current_page: the page number of currently displayed page
num_pages: total number of pages
per_page: number of items to fetch per page
remote: data-remote
-%>
<span class="last<%= ' disabled' if current_page == num_pages %>">
<%= link_to_unless current_page == num_pages, raw(t 'views.pagination.last'), url, :class => 'last', :remote => remote %>
</span>

View file

@ -0,0 +1,9 @@
-# Link to the "Last" page
- available local variables
url: url to the last page
current_page: the page number of currently displayed page
num_pages: total number of pages
per_page: number of items to fetch per page
remote: data-remote
%span{:class => "last#{' disabled' if current_page == num_pages}"}
= link_to_unless current_page == num_pages, raw(t 'views.pagination.last'), url, :class => 'last', :remote => remote

View file

@ -1,4 +1,4 @@
<%# "Next" with link <%# Link to the "Next" page
- available local variables - available local variables
url: url to the next page url: url to the next page
current_page: the page number of currently displayed page current_page: the page number of currently displayed page
@ -6,6 +6,6 @@
per_page: number of items to fetch per page per_page: number of items to fetch per page
remote: data-remote remote: data-remote
-%> -%>
<span class="next"> <span class="next<%= ' disabled' if current_page == num_pages %>">
<%= link_to raw(t 'views.pagination.next'), url, :class => 'next', :rel => 'next', :remote => remote %> <%= link_to_unless current_page == num_pages, raw(t 'views.pagination.next'), url, :class => 'next', :rel => 'next', :remote => remote %>
</span> </span>

View file

@ -1,4 +1,4 @@
-# "Next" with link -# Link to the "Next" page
- available local variables - available local variables
url: url to the next page url: url to the next page
current_page: the page number of currently displayed page current_page: the page number of currently displayed page
@ -6,4 +6,4 @@
per_page: number of items to fetch per page per_page: number of items to fetch per page
remote: data-remote remote: data-remote
%span.next %span.next
= link_to raw(t 'views.pagination.next'), url, :class => 'next', :rel => 'next', :remote => remote = link_to_unless current_page == num_pages, raw(t 'views.pagination.next'), url, :class => 'next', :rel => 'next', :remote => remote

View file

@ -1,8 +0,0 @@
<%# "Next" without link
- available local variables
current_page: the page number of currently displayed page
num_pages: total number of pages
per_page: number of items to fetch per page
remote: data-remote
-%>
<span class="next"></span>

View file

@ -1,7 +0,0 @@
-# "Next" without link
- available local variables
current_page: the page number of currently displayed page
num_pages: total number of pages
per_page: number of items to fetch per page
remote: data-remote
%span.next

View file

@ -8,14 +8,16 @@
-%> -%>
<%= paginator.render do -%> <%= paginator.render do -%>
<nav class="pagination"> <nav class="pagination">
<%= current_page > 1 ? prev_link_tag : prev_span_tag %> <%= first_page_tag %>
<%= prev_page_tag %>
<% each_page do |page| -%> <% each_page do |page| -%>
<% if page.left_outer? || page.right_outer? || page.inside_window? -%> <% if page.left_outer? || page.right_outer? || page.inside_window? -%>
<%= page_tag page %> <%= page_tag page %>
<% elsif !page.was_truncated? -%> <% elsif !page.was_truncated? -%>
<%= truncated_span_tag %> <%= gap_tag %>
<% end -%> <% end -%>
<% end -%> <% end -%>
<%= num_pages > current_page ? next_link_tag : next_span_tag %> <%= next_page_tag %>
<%= last_page_tag %>
</nav> </nav>
<% end -%> <% end -%>

View file

@ -7,10 +7,12 @@
paginator: the paginator that renders the pagination tags inside paginator: the paginator that renders the pagination tags inside
= paginator.render do = paginator.render do
%nav.pagination %nav.pagination
= current_page > 1 ? prev_link_tag : prev_span_tag = first_page_tag
= prev_page_tag
- each_page do |page| - each_page do |page|
- if page.left_outer? || page.right_outer? || page.inside_window? - if page.left_outer? || page.right_outer? || page.inside_window?
= page_tag page = page_tag page
- elsif !page.was_truncated? - elsif !page.was_truncated?
= truncated_span_tag = gap_tag
= num_pages > current_page ? next_link_tag : next_span_tag = next_page_tag
= last_page_tag

View file

@ -1,4 +1,4 @@
<%# "Previous" with link <%# Link to the "Previous" page
- available local variables - available local variables
url: url to the previous page url: url to the previous page
current_page: the page number of currently displayed page current_page: the page number of currently displayed page
@ -6,6 +6,6 @@
per_page: number of items to fetch per page per_page: number of items to fetch per page
remote: data-remote remote: data-remote
-%> -%>
<span class="prev"> <span class="prev<%= ' disabled' if current_page == 1 %>">
<%= link_to raw(t 'views.pagination.previous'), url, :class => 'prev', :rel => 'prev', :remote => remote %> <%= link_to_unless current_page == 1, raw(t 'views.pagination.previous'), url, :class => 'prev', :rel => 'prev', :remote => remote %>
</span> </span>

View file

@ -1,4 +1,4 @@
-# "Previous" with link -# Link to the "Previous" page
- available local variables - available local variables
url: url to the previous page url: url to the previous page
current_page: the page number of currently displayed page current_page: the page number of currently displayed page
@ -6,4 +6,4 @@
per_page: number of items to fetch per page per_page: number of items to fetch per page
remote: data-remote remote: data-remote
%span.prev %span.prev
= link_to raw(t 'views.pagination.previous'), url, :class => 'prev', :rel => 'prev', :remote => remote = link_to_unless current_page == 1, raw(t 'views.pagination.previous'), url, :class => 'prev', :rel => 'prev', :remote => remote

View file

@ -1,8 +0,0 @@
<%# "Previous" without link
- available local variables
current_page: the page number of currently displayed page
num_pages: total number of pages
per_page: number of items to fetch per page
remote: data-remote
-%>
<span class="prev"></span>

View file

@ -1,7 +0,0 @@
-# "Previous" without link
- available local variables
current_page: the page number of currently displayed page
num_pages: total number of pages
per_page: number of items to fetch per page
remote: data-remote
%span.prev

View file

@ -3,6 +3,8 @@
en: en:
views: views:
pagination: pagination:
previous: "&laquo; Prev" first: "&laquo; First"
next: "Next &raquo;" last: "Last &raquo;"
previous: "&lsaquo; Prev"
next: "Next &rsaquo;"
truncate: "..." truncate: "..."

View file

@ -9,10 +9,10 @@ module Kaminari
# <%= paginate @articles %> # <%= paginate @articles %>
# #
# ==== Options # ==== Options
# * <tt>:window</tt> - The "inner window" size (2 by default). # * <tt>:window</tt> - The "inner window" size (4 by default).
# * <tt>:outer_window</tt> - The "outer window" size (1 by default). # * <tt>:outer_window</tt> - The "outer window" size (0 by default).
# * <tt>:left</tt> - The "left outer window" size (1 by default). # * <tt>:left</tt> - The "left outer window" size (0 by default).
# * <tt>:right</tt> - The "right outer window" size (1 by default). # * <tt>:right</tt> - The "right outer window" size (0 by default).
# * <tt>:params</tt> - url_for parameters for the links (:controller, :action, etc.) # * <tt>:params</tt> - url_for parameters for the links (:controller, :action, etc.)
# * <tt>:param_name</tt> - parameter name for page number in the links (:page by default) # * <tt>:param_name</tt> - parameter name for page number in the links (:page by default)
# * <tt>:remote</tt> - Ajax? (false by default) # * <tt>:remote</tt> - Ajax? (false by default)

View file

@ -39,8 +39,8 @@ module Kaminari
@window_options = {}.tap do |h| @window_options = {}.tap do |h|
h[:window] = options.delete(:window) || options.delete(:inner_window) || 4 h[:window] = options.delete(:window) || options.delete(:inner_window) || 4
outer_window = options.delete(:outer_window) outer_window = options.delete(:outer_window)
h[:left] = options.delete(:left) || outer_window || 1 h[:left] = options.delete(:left) || outer_window || 0
h[:right] = options.delete(:right) || outer_window || 1 h[:right] = options.delete(:right) || outer_window || 0
end end
@template = TemplateWrapper.new(template, options) @template = TemplateWrapper.new(template, options)
end end

View file

@ -94,7 +94,7 @@ module Kaminari
@last = Page.new @template, :page => page @last = Page.new @template, :page => page
end end
%w[prev_link prev_span next_link next_span truncated_span].each do |tag| %w[first_page prev_page next_page last_page gap].each do |tag|
eval <<-DEF eval <<-DEF
def #{tag}_tag def #{tag}_tag
@last = #{tag.classify}.new @template @last = #{tag.classify}.new @template
@ -136,12 +136,12 @@ module Kaminari
# within the left outer window or not # within the left outer window or not
def left_outer? def left_outer?
@page <= @options[:left] + 1 @page <= @options[:left]
end end
# within the right outer window or not # within the right outer window or not
def right_outer? def right_outer?
@options[:num_pages] - @page <= @options[:right] @options[:num_pages] - @page < @options[:right]
end end
# inside the inner window or not # inside the inner window or not
@ -151,7 +151,7 @@ module Kaminari
# The last rendered tag was "truncated" or not # The last rendered tag was "truncated" or not
def was_truncated? def was_truncated?
@last.is_a? TruncatedSpan @last.is_a? Gap
end end
def to_i def to_i
@ -171,6 +171,7 @@ module Kaminari
# Tag that contains a link # Tag that contains a link
module Link module Link
include Renderable include Renderable
# target page number
def page def page
raise 'Override page with the actual page value to be a Page.' raise 'Override page with the actual page value to be a Page.'
end end
@ -195,54 +196,41 @@ module Kaminari
end end
end end
# Tag that doesn't contain a link # Link with page number that appears at the leftmost
module NonLink class FirstPage < Tag
include Renderable include Link
def page #:nodoc:
1
end
end
# Link with page number that appears at the rightmost
class LastPage < Tag
include Link
def page #:nodoc:
@options[:num_pages]
end
end end
# The "previous" page of the current page # The "previous" page of the current page
module Prev class PrevPage < Tag
include Renderable
end
# "Previous" without link
class PrevSpan < Tag
include NonLink
include Prev
end
# "Previous" with link
class PrevLink < Tag
include Link include Link
include Prev
def page #:nodoc: def page #:nodoc:
@options[:current_page] - 1 @options[:current_page] - 1
end end
end end
# The "next" page of the current page # The "next" page of the current page
module Next class NextPage < Tag
include Renderable
end
# "Next" without link
class NextSpan < Tag
include NonLink
include Next
end
# "Next" with link
class NextLink < Tag
include Link include Link
include Next
def page #:nodoc: def page #:nodoc:
@options[:current_page] + 1 @options[:current_page] + 1
end end
end end
# Non-link tag that stands for skipped pages... # Non-link tag that stands for skipped pages...
class TruncatedSpan < Tag class Gap < Tag
include NonLink include Renderable
end end
end end
end end

View file

@ -7,29 +7,21 @@ describe 'Kaminari::Helpers' do
subject { Paginator } subject { Paginator }
its(:ancestor_renderables) { should == [Paginator] } its(:ancestor_renderables) { should == [Paginator] }
end end
describe 'PrevLink' do describe 'PrevPage' do
subject { PrevLink } subject { PrevPage }
its(:ancestor_renderables) { should == [PrevLink, Prev, Link] } its(:ancestor_renderables) { should == [PrevPage, Link] }
end
describe 'PrevSpan' do
subject { PrevSpan }
its(:ancestor_renderables) { should == [PrevSpan, Prev, NonLink] }
end end
describe 'Page' do describe 'Page' do
subject { Page } subject { Page }
its(:ancestor_renderables) { should == [Page, Link] } its(:ancestor_renderables) { should == [Page, Link] }
end end
describe 'TruncatedSpan' do describe 'Gap' do
subject { TruncatedSpan } subject { Gap }
its(:ancestor_renderables) { should == [TruncatedSpan, NonLink] } its(:ancestor_renderables) { should == [Gap] }
end end
describe 'NextLink' do describe 'NextPage' do
subject { NextLink } subject { NextPage }
its(:ancestor_renderables) { should == [NextLink, Next, Link] } its(:ancestor_renderables) { should == [NextPage, Link] }
end
describe 'NextSpan' do
subject { NextSpan }
its(:ancestor_renderables) { should == [NextSpan, Next, NonLink] }
end end
end end
@ -132,11 +124,11 @@ describe 'Kaminari::Helpers' do
before do before do
stub(@template = Object.new).options { {} } stub(@template = Object.new).options { {} }
end end
context 'last.is_a? TruncatedSpan' do context 'last.is_a? Gap' do
subject { Paginator::PageProxy.new({}, 10, TruncatedSpan.new(@template)) } subject { Paginator::PageProxy.new({}, 10, Gap.new(@template)) }
its(:was_truncated?) { should be_true } its(:was_truncated?) { should be_true }
end end
context 'last.is not a TruncatedSpan' do context 'last.is not a Gap' do
subject { Paginator::PageProxy.new({}, 10, Page.new(@template)) } subject { Paginator::PageProxy.new({}, 10, Page.new(@template)) }
its(:was_truncated?) { should_not be_true } its(:was_truncated?) { should_not be_true }
end end