more work

This commit is contained in:
albert
2010-10-19 19:34:31 -04:00
parent cb3d7e9e9b
commit acdce69f20
48 changed files with 2494 additions and 1585 deletions

View File

@@ -1,2 +0,0 @@
class PaginatorPresenter < Presenter
end

View File

@@ -0,0 +1,78 @@
module Paginators
class Base < Presenter
def sequential_pagination_html(template)
html = "<menu>"
prev_url = template.request.env["HTTP_REFERER"]
next_url = sequential_link(template)
html << %{<li><a href="#{prev_url}">&laquo; Previous</a></li>}
if post_set.posts.any?
html << %{<li><a href="#{next_url}">Next &raquo;</a></li>}
end
html << "</menu>"
html.html_safe
end
def numbered_pagination_html(template)
html = "<menu>"
window = 3
if total_pages <= (window * 2) + 5
1.upto(total_pages) do |page|
html << numbered_pagination_item(template, page, current_page)
end
elsif current_page <= window + 2
1.upto(current_page + window) do |page|
html << numbered_pagination_item(template, page, current_page)
end
html << numbered_pagination_item(template, "...", current_page)
html << numbered_pagination_item(template, total_pages, current_page)
elsif current_page >= total_pages - (window + 1)
html << numbered_pagination_item(template, 1, current_page)
html << numbered_pagination_item(template, "...", current_page)
(current_page - window).upto(total_pages) do |page|
html << numbered_pagination_item(template, page, current_page)
end
else
html << numbered_pagination_item(template, 1, current_page)
html << numbered_pagination_item(template, "...", current_page)
(current_page - window).upto(current_page + window) do |page|
html << numbered_pagination_item(template, page, current_page)
end
html << numbered_pagination_item(template, "...", current_page)
html << numbered_pagination_item(template, total_pages, current_page)
end
html << "</menu>"
html.html_safe
end
protected
def numbered_pagination_item(template, page, current_page)
html = "<li>"
if page == "..."
html << "..."
elsif page == current_page
html << page.to_s
else
html << link(template, page)
end
html << "</li>"
html.html_safe
end
def total_pages
raise NotImplementedError
end
def current_page
raise NotImplementedError
end
def sequential_link(template)
raise NotImplementedError
end
def paginated_link(template, page)
raise NotImplementedError
end
end
end

View File

@@ -0,0 +1,26 @@
module Paginators
class Post < Base
attr_accessor :post_set
def initialize(post_set)
@post_set = post_set
end
protected
def total_pages
(post_set.count.to_f / post_set.limit.to_f).ceil
end
def current_page
[1, post_set.page].max
end
def sequential_link(template)
template.posts_path(:tags => template.params[:tags], before_id => post_set.posts[-1].id, :page => nil)
end
def paginated_link(template, page)
template.link_to(page, template.posts_path(:tags => template.params[:tags], :page => page))
end
end
end

View File

@@ -1,18 +1,19 @@
require 'pp'
class PostSetPresenter < Presenter
attr_accessor :post_set
attr_accessor :post_set, :tag_set_presenter
def initialize(post_set)
@post_set = post_set
@tag_set_presenter = TagSetPresenter.new(RelatedTagCalculator.calculate_from_sample_to_array(@post_set.tags).map {|x| x[0]})
end
def posts
post_set.posts
end
def tag_list_html
""
def tag_list_html(template)
tag_set_presenter.tag_list_html(template)
end
def wiki_html(template)
@@ -39,72 +40,12 @@ class PostSetPresenter < Presenter
def pagination_html(template)
if post_set.use_sequential_paginator?
sequential_pagination_html(template)
Paginators::Post.new(post_set).sequential_pagination_html(template)
else
numbered_pagination_html(template)
Paginators::Post.new(post_set).numbered_pagination_html(template)
end
end
def sequential_pagination_html(template)
html = "<menu>"
prev_url = template.request.env["HTTP_REFERER"]
next_url = template.posts_path(:tags => template.params[:tags], before_id => post_set.posts[-1].id, :page => nil)
html << %{<li><a href="#{prev_url}">&laquo; Previous</a></li>}
if post_set.posts.any?
html << %{<li><a href="#{next_url}">Next &raquo;</a></li>}
end
html << "</menu>"
html.html_safe
end
def numbered_pagination_html(template)
total_pages = (post_set.count.to_f / post_set.limit.to_f).ceil
current_page = [1, post_set.page].max
html = "<menu>"
window = 3
if total_pages <= (window * 2) + 5
1.upto(total_pages) do |page|
html << numbered_pagination_item(template, page, current_page)
end
elsif current_page <= window + 2
1.upto(current_page + window) do |page|
html << numbered_pagination_item(template, page, current_page)
end
html << numbered_pagination_item(template, "...", current_page)
html << numbered_pagination_item(template, total_pages, current_page)
elsif current_page >= total_pages - (window + 1)
html << numbered_pagination_item(template, 1, current_page)
html << numbered_pagination_item(template, "...", current_page)
(current_page - window).upto(total_pages) do |page|
html << numbered_pagination_item(template, page, current_page)
end
else
html << numbered_pagination_item(template, 1, current_page)
html << numbered_pagination_item(template, "...", current_page)
(current_page - window).upto(current_page + window) do |page|
html << numbered_pagination_item(template, page, current_page)
end
html << numbered_pagination_item(template, "...", current_page)
html << numbered_pagination_item(template, total_pages, current_page)
end
html << "</menu>"
html.html_safe
end
def numbered_pagination_item(template, page, current_page)
html = "<li>"
if page == "..."
html << "..."
elsif page == current_page
html << page.to_s
else
html << template.link_to(page, template.__send__(:posts_path, :tags => template.params[:tags], :page => page))
end
html << "</li>"
html.html_safe
end
def post_previews_html
html = ""

View File

@@ -31,12 +31,12 @@ private
def build_list_item(tag, template, options)
html = ""
html << %{<li data-tag-type="#{category_for(tag)}">}
html << %{<li data-tag-type="#{category_for(tag)}" data-tag-name="#{u(tag)}">}
if options[:show_extra_links]
html << %{<a href="/wiki_pages/#{u(tag)}">?</a> }
if CurrentUser.user.is_privileged?
html << %{<a href="/wiki_pages?title=#{u(tag)}">?</a> }
html << %{<a href="#" class="search-inc-tag">+</a> }
html << %{<a href="#" class="search-exl-tag">-</a> }
html << %{<a href="#" class="search-exl-tag">&ndash;</a> }
end
humanized_tag = tag.tr("_", " ")