| 1 | /* |
|---|
| 2 | * To change this template, choose Tools case class Templates |
|---|
| 3 | * and open the template in the editor. |
|---|
| 4 | */ |
|---|
| 5 | |
|---|
| 6 | package org.aiotrade.blog.frontend |
|---|
| 7 | |
|---|
| 8 | import org.aiotrade.blog.frontend.{Feeds => F} |
|---|
| 9 | import org.aiotrade.blog.model.{Entry => B} |
|---|
| 10 | import org.aiotrade.blog.model.Entry.Item |
|---|
| 11 | import org.aiotrade.blog.model.Entry.Model |
|---|
| 12 | import org.aiotrade.blog.{Constants => C} |
|---|
| 13 | |
|---|
| 14 | object Views { |
|---|
| 15 | trait View extends Viewable[View] { |
|---|
| 16 | def url = url_(this) + page_suffix(page_number) |
|---|
| 17 | |
|---|
| 18 | def page_number = this match { |
|---|
| 19 | case ByYMDPermatitle(_, _, _, _) => None |
|---|
| 20 | case ByPermatitle(_) => None |
|---|
| 21 | case All(page_n) => page_n |
|---|
| 22 | case ByYear(_, page_n) => page_n |
|---|
| 23 | case ByMonth(_, _, page_n) => page_n |
|---|
| 24 | case ByDay(_, _, _, page_n) => page_n |
|---|
| 25 | case ByTags(_, page_n) => page_n |
|---|
| 26 | case ByTag(_, page_n) => page_n |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | def page_size = this match { |
|---|
| 30 | case All(_) => C.default_page_size |
|---|
| 31 | case ByYear(_, _) => C.default_page_size |
|---|
| 32 | case ByMonth(_, _, _) => C.default_page_size |
|---|
| 33 | case ByDay(_, _, _, _) => C.default_page_size |
|---|
| 34 | case ByTags(_, _) => C.default_page_size |
|---|
| 35 | case ByTag(_, _) => C.default_page_size |
|---|
| 36 | case ByYMDPermatitle(_, _, _, _) => C.default_page_size |
|---|
| 37 | case ByPermatitle(_) => C.default_page_size |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | def kind = this match { |
|---|
| 41 | case ByYMDPermatitle(_, _, _, _) => Single |
|---|
| 42 | case ByPermatitle(_) => Single |
|---|
| 43 | case _ => Multiple |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | def lens(md: Model) = this match { |
|---|
| 47 | case All(_) => md.all_posts filter (_.kind == B.Post) |
|---|
| 48 | case ByYear(y, _) => B.year_filter(y) (md.all_posts) |
|---|
| 49 | case ByMonth(y, m, _) => B.month_filter(y, m) (md.all_posts) |
|---|
| 50 | case ByDay(y, m, d, _) => B.day_filter(y, m, d) (md.all_posts) |
|---|
| 51 | case ByTag(t, _) => B.tag_filter(t) (md.all_posts) |
|---|
| 52 | case ByTags(t, _) => B.tags_filter(t) (md.all_posts) |
|---|
| 53 | case ByYMDPermatitle(_, _, _, t) => md.maybe_post_by_permatitle(t) toList |
|---|
| 54 | case ByPermatitle(t) => md.maybe_post_by_permatitle(t) toList |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | def no_posts_message = this match { |
|---|
| 58 | case All(_) => "No posts were found at all... Something's fishy." |
|---|
| 59 | case ByYear(y, _) => "No posts from year " + y + " were found." |
|---|
| 60 | case ByMonth(y, m, _) => "No posts from " + y + pad_(m) + " were found." |
|---|
| 61 | case ByDay(y, m, d, _) => "No posts were made on " + y + pad_(m) + pad_(d) + "." |
|---|
| 62 | case ByTags(ts, _) => "No posts are in the intersection of the tags {" + ts.mkString(",") + "}." |
|---|
| 63 | case ByTag(t, _) => "No posts are tagged " + t + "." |
|---|
| 64 | case ByYMDPermatitle(y, m, d, t) => "No posts with permatitle " + t + " were made on" + y + pad_(m) + pad_(d) + |
|---|
| 65 | "; try the same URL without the " + |
|---|
| 66 | "/" + y + "/" + pad_(m) + "/" + pad_ (d) + " fragment." |
|---|
| 67 | case ByPermatitle(t) => "No post with permatitle " + t + " was found." |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | def title = this match { |
|---|
| 71 | case All(_) => "All Posts" |
|---|
| 72 | case ByYear(y, _) => y.toString |
|---|
| 73 | case ByMonth(y, m, _) => y + "/" + m |
|---|
| 74 | case ByDay(y, m, d, _) => y + "/" + m + "/" + d |
|---|
| 75 | case ByTag(t, _) => "Posts tagged \"" + t + "\"" |
|---|
| 76 | case ByTags(ts, _) => "Posts tagged " + ts |
|---|
| 77 | case ByYMDPermatitle(_, _, _, t) => t |
|---|
| 78 | case ByPermatitle(t) => t |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | def first_page = this match { |
|---|
| 82 | case All(_) => All(Some(1)) |
|---|
| 83 | case ByYear(y, _) => ByYear(y, Some(1)) |
|---|
| 84 | case ByMonth(y, m, _) => ByMonth(y, m, Some(1)) |
|---|
| 85 | case ByDay(y, m, d, _) => ByDay(y, m, d, Some(1)) |
|---|
| 86 | case ByTag(t, _) => ByTag(t, Some(1)) |
|---|
| 87 | case ByTags(t, _) => ByTags(t, Some(1)) |
|---|
| 88 | case _ => this |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | def next_page = this match { |
|---|
| 92 | case All(None) => All(Some(2)) |
|---|
| 93 | case All(Some(a)) => All(Some(a + 1)) |
|---|
| 94 | case ByYear(y, None) => ByYear(y, Some(2)) |
|---|
| 95 | case ByYear(y, Some(a)) => ByYear(y, Some(a + 1)) |
|---|
| 96 | case ByMonth(y, m, None) => ByMonth(y, m, Some(2)) |
|---|
| 97 | case ByMonth(y, m, Some(a)) => ByMonth(y, m, Some(a + 1)) |
|---|
| 98 | case ByDay(y, m, d, None) => ByDay(y, m, d, Some(2)) |
|---|
| 99 | case ByDay(y, m, d, Some(a)) => ByDay(y, m, d, Some(a + 1)) |
|---|
| 100 | case ByTag(t, None) => ByTag(t, Some(2)) |
|---|
| 101 | case ByTag(t, Some(a)) => ByTag(t, Some(a + 1)) |
|---|
| 102 | case ByTags(ts, None) => ByTags(ts, Some(2)) |
|---|
| 103 | case ByTags(ts, Some(a)) => ByTags(ts, Some(a + 1)) |
|---|
| 104 | case _ => this |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | def discoverable_feeds = this match { |
|---|
| 108 | case All(_) => List(F.articles_feed, F.all_comments_feed) |
|---|
| 109 | case ByYear(_, _) => List(F.articles_feed, F.all_comments_feed) |
|---|
| 110 | case ByMonth(_, _, _) => List(F.articles_feed, F.all_comments_feed) |
|---|
| 111 | case ByDay(_, _, _, _) => List(F.articles_feed, F.all_comments_feed) |
|---|
| 112 | case ByYMDPermatitle(_, _, _, t) => List(F.articles_feed, F.all_comments_feed, F.comments_feed(t)) |
|---|
| 113 | case ByPermatitle(t) => List(F.articles_feed, F.all_comments_feed, F.comments_feed(t)) |
|---|
| 114 | case ByTag(t, _) => List(F.articles_feed, F.all_comments_feed, F.tags_feed(List(t))) |
|---|
| 115 | case ByTags(ts, _) => List(F.articles_feed, F.all_comments_feed, F.tags_feed(ts)) |
|---|
| 116 | } |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | case class All(page_n: Option[Int]) extends View |
|---|
| 120 | case class ByYear(year: Int, page_n: Option[Int]) extends View |
|---|
| 121 | case class ByMonth(year: Int, month: Int, page_n: Option[Int]) extends View |
|---|
| 122 | case class ByDay(year: Int, month: Int, day: Int, page_n: Option[Int]) extends View |
|---|
| 123 | case class ByTags(tags: List[String], page_n: Option[Int]) extends View |
|---|
| 124 | case class ByTag(tag: String, page_n: Option[Int]) extends View |
|---|
| 125 | case class ByYMDPermatitle(year: Int, month: Int, day: Int, permalink: String) extends View |
|---|
| 126 | case class ByPermatitle (permalink: String) extends View |
|---|
| 127 | |
|---|
| 128 | trait ViewKind |
|---|
| 129 | case object Single extends ViewKind |
|---|
| 130 | case object Multiple extends ViewKind |
|---|
| 131 | |
|---|
| 132 | trait Viewable[V] { |
|---|
| 133 | def url: String |
|---|
| 134 | def kind: ViewKind |
|---|
| 135 | def lens(m: Model): List[Item] |
|---|
| 136 | def title: String |
|---|
| 137 | def discoverable_feeds: List[F.DiscoverableFeed] |
|---|
| 138 | def page_number: Option[Int] |
|---|
| 139 | def page_size: Int |
|---|
| 140 | def no_posts_message: String |
|---|
| 141 | def next_page: V |
|---|
| 142 | def first_page: V |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | def url_(v: View): String = |
|---|
| 146 | v match { |
|---|
| 147 | case All(_) => "/a" |
|---|
| 148 | case ByYear(y, _) => "/a/" + y |
|---|
| 149 | case ByTag(t, _) => "/t/" + t |
|---|
| 150 | case ByTags(ts, _) => "/t/" + ts.mkString(",") |
|---|
| 151 | case ByMonth(y, m, _) => "/a/" + y + "/" + pad_(m) |
|---|
| 152 | case ByDay(y, m, d, _) => "/a/" + y + "/" + pad_(m) + "/" + pad_(d) |
|---|
| 153 | case ByPermatitle(t) => "/p/" + t |
|---|
| 154 | case ByYMDPermatitle(_, _, _, t) => "/p/" + t |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | def page_suffix(n_? : Option[Int]): String = |
|---|
| 159 | n_? match { |
|---|
| 160 | case None => "" |
|---|
| 161 | case Some(n) => "/page/" + n |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | def pad_(n: Int): String = |
|---|
| 165 | if (n < 10) "0" + n else "" + n |
|---|
| 166 | } |
|---|