aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/reading/component/index/BookDetail.scala
blob: 6d1c424a2bf805acbce6212c948d7640fd4e2895 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package reading.component.index

import rx._
import scalacss.Defaults._
import scalacss.ScalatagsCss._
import scalatags.JsDom.all._

import reading.component.index.style.{ BookDetail => BookStyle }
import reading.component.widget.AnimateMethod
import reading.models.{ Filter, Book, Program, Grade, Theme, GroupedTheme }
import reading.Route
import reading.utils.RxUtils._

object BookDetail {
  val componentId = s"books-detail"

  def apply(
    filters: Var[Set[Filter]],
    detail: Var[Option[Book]],
    search: Var[String],
    book: Book,
    parentId: String,
    onClose: => Unit
  )(
    implicit
    ctx: Ctx.Owner
  ): Frag = {
    val titleParts = if (book.parts > 1) s", ${book.parts} volumes" else ""
    val grades = book.programs.map(Grade.from(_)).distinct.sorted

    AnimateMethod.fadeIn(componentId, AnimateMethod.fadeOut(Books.componentId))

    div(
      BookStyle.render,
      BookStyle.detailParent,
      id := componentId,

      div(
        BookStyle.detail,

        img(
          BookStyle.cover,
          src := s"cover/${book.title}.jpg",
          alt := s"${book.title}, ${book.author}"
        ),

        div(
          BookStyle.presentation,
          div(BookStyle.title, s"${book.title}$titleParts"),
          div(BookStyle.author, book.author),
          p(BookStyle.summary, raw(book.summary)),

          dl(
            BookStyle.definitions,

            grades.map { grade =>
              val programs = book.programs.filter(p => Grade.from(p) == grade).sorted
              val pp = grade.prettyPrint
              val programFilters = programs.map { program =>
                (Filter(program), filters.map(addProgram(_, program)))
              }
              definition(filters, detail, search, pp, pp, programFilters, p => s"« $p »")
            },
            if (book.themes.nonEmpty) {
              val themeFilters = book.themes.sorted.map { theme =>
                (Filter(theme), filters.map(addTheme(_, theme)))
              }
              definition(filters, detail, search, "thème", "thèmes", themeFilters)
            },
            if (book.genres.nonEmpty) {
              val bookFilters = book.genres.sorted.map(Filter(_)).map(b => (b, filters.map(_ + b)))
              definition(filters, detail, search, "genre", "genres", bookFilters)
            },
            {
              val levelFilters = Seq(Filter(book.level)).map(b => (b, filters.map(_ + b)))
              definition(filters, detail, search, "niveau", "niveaux", levelFilters)
            }
          ),

          a(
            BookStyle.close,
            onclick := (() => onClose),
            href := Rx(Route.url(Route.Books(filters()))),
            "Fermer"
          )
        )
      )
    )
  }

  private def definition(
    filters: Var[Set[Filter]],
    detail: Var[Option[Book]],
    search: Var[String],
    term: String,
    pluralTerm: String,
    definitionFilters: Seq[(Filter, Rx[Set[Filter]])],
    format: String => String = _.capitalize
  )(
    implicit
    ctx: Ctx.Owner
  ): Seq[Frag] = {
    val sTerm = if (definitionFilters.length > 1) pluralTerm else term

    Seq(
      dt(BookStyle.definitionTerm, s"${sTerm.capitalize} :"),
      dd(
        BookStyle.definitionDescription,
        definitionFilters.map {
          case (filter, newFilters) =>
            a(
              BookStyle.definitionFilter,
              href := Rx(Route.url(Route.Books(newFilters()))),
              onclick := (() => FilterUtils.set(filters, detail, search, newFilters.now)),
              format(filter.name)
            )
        }
      )
    )
  }

  private def addProgram(filters: Set[Filter], program: Program): Set[Filter] = {
    val grade = Grade.from(program)
    val otherGrades = Grade.values.filter(_ != grade).map(Filter(_))
    val otherPrograms = Program.values.filter(Grade.from(_) != grade).map(Filter(_))
    filters -- otherGrades -- otherPrograms + Filter(grade) + Filter(program)
  }

  private def addTheme(filters: Set[Filter], theme: Theme): Set[Filter] = {
    val groupedTheme = GroupedTheme.from(theme)
    val otherGroupedThemes = GroupedTheme.values.filter(_ != groupedTheme).map(Filter(_))
    val otherThemes = Theme.values.filter(GroupedTheme.from(_) != groupedTheme).map(Filter(_))
    filters -- otherGroupedThemes -- otherThemes + Filter(groupedTheme) + Filter(theme)
  }
}