From 27926df522768e7a5910dda04dfc0f7c802bc3b3 Mon Sep 17 00:00:00 2001 From: albert Date: Sat, 9 Mar 2013 20:48:37 -0500 Subject: [PATCH] fixes #805 --- app/assets/javascripts/post_popular.js | 41 ++++++++++ app/presenters/post_set_presenters/popular.rb | 77 +++++++++++++++++++ .../explore/posts/_date_explore.html.erb | 19 ----- app/views/explore/posts/popular.html.erb | 2 +- 4 files changed, 119 insertions(+), 20 deletions(-) create mode 100644 app/assets/javascripts/post_popular.js delete mode 100644 app/views/explore/posts/_date_explore.html.erb diff --git a/app/assets/javascripts/post_popular.js b/app/assets/javascripts/post_popular.js new file mode 100644 index 000000000..ab4cf3371 --- /dev/null +++ b/app/assets/javascripts/post_popular.js @@ -0,0 +1,41 @@ +(function() { + Danbooru.PostPopular = {}; + + Danbooru.PostPopular.nav_prev = function() { + if ($("#popular-nav-links").length) { + var href = $("#popular-nav-links a[rel=prev]").attr("href"); + if (href) { + location.href = href; + } + } + } + + Danbooru.PostPopular.nav_next = function() { + if ($("#popular-nav-links").length) { + var href = $("#popular-nav-links a[rel=next]").attr("href"); + if (href) { + location.href = href; + } + } + } + + Danbooru.PostPopular.initialize_all = function() { + if ($("#c-explore-posts").length) { + if (Danbooru.meta("enable-js-navigation") === "true") { + $(document).bind("keydown.a", function(e) { + Danbooru.PostPopular.nav_prev(); + e.preventDefault(); + }); + + $(document).bind("keydown.d", function(e) { + Danbooru.PostPopular.nav_next(); + e.preventDefault(); + }); + } + } + } +})(); + +$(document).ready(function() { + Danbooru.PostPopular.initialize_all(); +}); \ No newline at end of file diff --git a/app/presenters/post_set_presenters/popular.rb b/app/presenters/post_set_presenters/popular.rb index 754b3fb3f..6c7f50628 100644 --- a/app/presenters/post_set_presenters/popular.rb +++ b/app/presenters/post_set_presenters/popular.rb @@ -31,6 +31,83 @@ module PostSetPresenters 1.month.since(date) end + def link_rel_for_scale?(template, scale) + (template.params[:scale].blank? && scale == "day") || template.params[:scale].to_s.include?(scale) + end + + def next_date_for_scale(scale) + case scale + when "Day" + next_day + + when "Week" + next_week + + when "Month" + next_month + + else + nil + end + end + + def prev_date_for_scale(scale) + case scale + when "Day" + prev_day + + when "Week" + prev_week + + when "Month" + prev_month + + else + nil + end + end + + def nav_links_for_scale(template, scale) + html = [] + html << '' + html << template.link_to( + "«prev".html_safe, + template.popular_explore_posts_path( + :date => prev_date_for_scale(scale), + :scale => scale.downcase + ), + :rel => (link_rel_for_scale?(template, scale.downcase) ? "prev" : nil) + ) + html << template.link_to( + scale, + template.popular_explore_posts_path( + :date => date, + :scale => scale.downcase + ), + :class => "desc" + ) + html << template.link_to( + "next»".html_safe, + template.popular_explore_posts_path( + :date => next_date_for_scale(scale), + :scale => scale.downcase + ), + :rel => (link_rel_for_scale?(template, scale.downcase) ? "next" : nil) + ) + html << '' + html.join("\n").html_safe + end + + def nav_links(template) + html = [] + html << '' + html.join("\n").html_safe + end + def range_text if min_date == max_date date.strftime("%B %d, %Y") diff --git a/app/views/explore/posts/_date_explore.html.erb b/app/views/explore/posts/_date_explore.html.erb deleted file mode 100644 index f07c44f8c..000000000 --- a/app/views/explore/posts/_date_explore.html.erb +++ /dev/null @@ -1,19 +0,0 @@ -

- - <%= link_to "«prev".html_safe, popular_explore_posts_path(:date => post_set.presenter.prev_day, :scale => "day") %> - <%= link_to "Day", popular_explore_posts_path(:date => post_set.presenter.date, :scale => "day"), :class => "desc" %> - <%= link_to "next»".html_safe, popular_explore_posts_path(:date => post_set.presenter.next_day, :scale => "day") %> - - | - - <%= link_to "«prev".html_safe, popular_explore_posts_path(:date => post_set.presenter.prev_week, :scale => "week") %> - <%= link_to "Week", popular_explore_posts_path(:date => post_set.presenter.date, :scale => "week"), :class => "desc" %> - <%= link_to "next»".html_safe, popular_explore_posts_path(:date => post_set.presenter.next_week, :scale => "week") %> - - | - - <%= link_to "«prev".html_safe, popular_explore_posts_path(:date => post_set.presenter.prev_month, :scale => "month") %> - <%= link_to "Month", popular_explore_posts_path(:date => post_set.presenter.date, :scale => "month"), :class => "desc" %> - <%= link_to "next»".html_safe, popular_explore_posts_path(:date => post_set.presenter.next_month, :scale => "month") %> - -

\ No newline at end of file diff --git a/app/views/explore/posts/popular.html.erb b/app/views/explore/posts/popular.html.erb index 1b8958839..06828296a 100644 --- a/app/views/explore/posts/popular.html.erb +++ b/app/views/explore/posts/popular.html.erb @@ -2,7 +2,7 @@

Popular: <%= @post_set.presenter.range_text %>

- <%= render "date_explore", :post_set => @post_set %> + <%= @post_set.presenter.nav_links(self) %> <%= render "blacklists" %>