2010-05-30 23:57:33 +00:00
|
|
|
module Locomotive
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
module Liquid
|
|
|
|
|
|
|
|
module Tags
|
|
|
|
|
|
|
|
# Paginate a collection
|
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
#
|
|
|
|
# {% paginate contents.projects by 5 %}
|
|
|
|
# {% for project in paginate.collection %}
|
|
|
|
# {{ project.name }}
|
|
|
|
# {% endfor %}
|
|
|
|
# {% endpaginate %}
|
|
|
|
#
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
class Paginate < ::Liquid::Block
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
Syntax = /(#{::Liquid::Expression}+)\s+by\s+([0-9]+)/
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-08-20 00:31:01 +00:00
|
|
|
def initialize(tag_name, markup, tokens, context)
|
2010-05-30 23:57:33 +00:00
|
|
|
if markup =~ Syntax
|
|
|
|
@collection_name = $1
|
|
|
|
@per_page = $2.to_i
|
|
|
|
else
|
2010-07-06 00:05:47 +00:00
|
|
|
raise ::Liquid::SyntaxError.new("Syntax Error in 'paginate' - Valid syntax: paginate <collection> by <number>")
|
2010-05-30 23:57:33 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
super
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
def render(context)
|
|
|
|
context.stack do
|
|
|
|
collection = context[@collection_name]
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-06 00:05:47 +00:00
|
|
|
raise ::Liquid::ArgumentError.new("Cannot paginate array '#{@collection_name}'. Not found.") if collection.nil?
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2011-11-05 07:42:04 +00:00
|
|
|
if collection.is_a? Array
|
|
|
|
pagination = Kaminari.paginate_array(collection).page(context['current_page']).per(@per_page).to_liquid.stringify_keys
|
|
|
|
else
|
|
|
|
pagination = collection.send(:paginate, {
|
|
|
|
:page => context['current_page'],
|
2012-01-26 01:33:39 +00:00
|
|
|
:per_page => @per_page
|
|
|
|
}).to_liquid.stringify_keys
|
2011-11-05 07:42:04 +00:00
|
|
|
end
|
2010-05-30 23:57:33 +00:00
|
|
|
page_count, current_page = pagination['total_pages'], pagination['current_page']
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2011-10-24 15:37:39 +00:00
|
|
|
path = context['path']
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
pagination['previous'] = link(I18n.t('pagination.previous'), current_page - 1, path) if pagination['previous_page']
|
|
|
|
pagination['next'] = link(I18n.t('pagination.next'), current_page + 1, path) if pagination['next_page']
|
|
|
|
pagination['parts'] = []
|
|
|
|
|
|
|
|
hellip_break = false
|
|
|
|
|
|
|
|
if page_count > 1
|
|
|
|
1.upto(page_count) do |page|
|
|
|
|
if current_page == page
|
|
|
|
pagination['parts'] << no_link(page)
|
|
|
|
elsif page == 1
|
|
|
|
pagination['parts'] << link(page, page, path)
|
|
|
|
elsif page == page_count - 1
|
|
|
|
pagination['parts'] << link(page, page, path)
|
|
|
|
elsif page <= current_page - window_size or page >= current_page + window_size
|
|
|
|
next if hellip_break
|
|
|
|
pagination['parts'] << no_link('…')
|
|
|
|
hellip_break = true
|
|
|
|
next
|
|
|
|
else
|
|
|
|
pagination['parts'] << link(page, page, path)
|
|
|
|
end
|
|
|
|
|
|
|
|
hellip_break = false
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
end
|
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
context['paginate'] = pagination
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
render_all(@nodelist, context)
|
|
|
|
end
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
def window_size
|
|
|
|
3
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
def no_link(title)
|
|
|
|
{ 'title' => title, 'is_link' => false, 'hellip_break' => title == '…' }
|
|
|
|
end
|
|
|
|
|
|
|
|
def link(title, page, path)
|
|
|
|
{ 'title' => title, 'url' => path + "?page=#{page}", 'is_link' => true}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
::Liquid::Template.register_tag('paginate', Paginate)
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
|
|
|
end
|