diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main/scala/reading/Books.scala | 10 | ||||
-rw-r--r-- | src/main/scala/reading/component/index/Books.scala | 29 | ||||
-rw-r--r-- | src/main/scala/reading/component/index/Filters.scala | 2 | ||||
-rw-r--r-- | src/main/scala/reading/component/index/FiltersMenu.scala | 66 | ||||
-rw-r--r-- | src/main/scala/reading/component/index/style/Books.scala | 23 | ||||
-rw-r--r-- | src/main/scala/reading/models/Book.scala | 2 | ||||
-rw-r--r-- | src/main/scala/reading/models/Filter.scala | 17 | ||||
-rw-r--r-- | src/main/scala/reading/models/Genre.scala | 6 | ||||
-rw-r--r-- | src/main/scala/reading/models/Level.scala | 10 | ||||
-rw-r--r-- | src/main/scala/reading/models/Program.scala | 6 | ||||
-rw-r--r-- | src/main/scala/reading/models/Theme.scala | 6 |
11 files changed, 62 insertions, 115 deletions
diff --git a/src/main/scala/reading/Books.scala b/src/main/scala/reading/Books.scala index af23146..886f337 100644 --- a/src/main/scala/reading/Books.scala +++ b/src/main/scala/reading/Books.scala @@ -14,7 +14,7 @@ object Books { period = Siecle20, genres = Seq(Policier), themes = Seq(Peur), - program = Monstre, + programs = Seq(Monstre), pages = 250 ), @@ -24,7 +24,7 @@ object Books { period = Siecle19, genres = Seq(), themes = Seq(Peur), - program = Heros, + programs = Seq(Heros), pages = 170 ), @@ -34,7 +34,7 @@ object Books { period = Siecle20, genres = Seq(), themes = Seq(Peur), - program = IndividuEtSociete, + programs = Seq(IndividuEtSociete), pages = 380 ), @@ -44,7 +44,7 @@ object Books { period = Siecle19, genres = Seq(Policier), themes = Seq(Amitie), - program = RecitAventure, + programs = Seq(RecitAventure), pages = 130 ), @@ -54,7 +54,7 @@ object Books { period = Siecle19, genres = Seq(), themes = Seq(Famille), - program = Autrui, + programs = Seq(Autrui), pages = 850 ) ) diff --git a/src/main/scala/reading/component/index/Books.scala b/src/main/scala/reading/component/index/Books.scala index dacfe4e..421fcec 100644 --- a/src/main/scala/reading/component/index/Books.scala +++ b/src/main/scala/reading/component/index/Books.scala @@ -7,7 +7,7 @@ import scalacss.Defaults._ import scalacss.ScalatagsCss._ import reading.component.index.style.{Books => BooksStyle} -import reading.models.Book +import reading.models.{Book, Program} import reading.utils.RxTag object Books { @@ -22,26 +22,15 @@ object Books { div( BooksStyle.book, div(BooksStyle.title, book.title), - div(BooksStyle.author, book.author), + div(BooksStyle.author, s", ${book.author}"), div( - BooksStyle.period, - s"période: ${book.period}" - ), - div( - BooksStyle.genres, - s"genre: ${book.genres.mkString(", ")}" - ), - div( - BooksStyle.themes, - s"thème: ${book.themes.mkString(", ")}" - ), - div( - BooksStyle.program, - s"programme: ${book.program}" - ), - div( - BooksStyle.pages, - s"${book.pages} pages" + BooksStyle.description, + div(BooksStyle.item, s"classe : ${book.programs.map(Program.level).distinct.sorted.mkString(", ")}"), + div(BooksStyle.item, s"programme : ${book.programs.sorted.mkString(", ")}"), + div(BooksStyle.item, s"thème : ${book.themes.sorted.mkString(", ")}"), + div(BooksStyle.item, s"genre : ${book.genres.sorted.mkString(", ")}"), + div(BooksStyle.item, s"période : ${book.period}"), + div(BooksStyle.item, s"${book.pages} pages") ) ) } diff --git a/src/main/scala/reading/component/index/Filters.scala b/src/main/scala/reading/component/index/Filters.scala index 50b2f0e..a53e46d 100644 --- a/src/main/scala/reading/component/index/Filters.scala +++ b/src/main/scala/reading/component/index/Filters.scala @@ -26,7 +26,7 @@ object Filters { filters().sortBy(_.name).map { filter => div( FiltersStyle.filter, - RxAttr(onclick, Rx(() => filters() = filters().filter(!Filter.equals(_, filter)))), + RxAttr(onclick, Rx(() => filters() = Filter.remove(filters(), filter))), span(FiltersStyle.name, filter.name), Cross(15.px, Col.white) ) diff --git a/src/main/scala/reading/component/index/FiltersMenu.scala b/src/main/scala/reading/component/index/FiltersMenu.scala index 02f591b..51f8954 100644 --- a/src/main/scala/reading/component/index/FiltersMenu.scala +++ b/src/main/scala/reading/component/index/FiltersMenu.scala @@ -8,7 +8,7 @@ import scalacss.Defaults._ import scalacss.ScalatagsCss._ import reading.component.index.style.{FiltersMenu => FiltersMenuStyle} -import reading.models.{Book, Filter, Period, Genre, Theme, Program, Difficulty, FilterFactory} +import reading.models._ import reading.utils.{RxTag, RxAttr} object FiltersMenu { @@ -17,11 +17,16 @@ object FiltersMenu { div( FiltersMenuStyle.render, FiltersMenuStyle.groups, - group(books, filters, "Période", Period.values.map(Filter.apply(_))), - group(books, filters, "Genre", Genre.values.map(Filter.apply(_))), + filters().find(_.kind == LevelKind) match { + case None => + group(books, filters, "Classe", Level.values.map(Filter.apply(_))) + case Some(level) => + group(books, filters, "Programme", Program.values.map(Filter.apply(_))) + }, group(books, filters, "Theme", Theme.values.map(Filter.apply(_))), - multiGroup(books, filters, "Programme", Program.values, Program.level _), - group(books, filters, "Difficulté", Difficulty.filters) + group(books, filters, "Genre", Genre.values.map(Filter.apply(_))), + group(books, filters, "Niveau", Difficulty.filters), + group(books, filters, "Période", Period.values.map(Filter.apply(_))) ) } @@ -57,55 +62,4 @@ object FiltersMenu { ) ) } - - - - def multiGroup[A: FilterFactory, B: Ordering]( - books: Rx[Seq[Book]], - filters: Var[Seq[Filter]], - name: String, - groupFilters: Seq[A], - superGroup: A => B - )( - implicit context: Ctx.Data - ): Frag = - div( - FiltersMenuStyle.group, - div(FiltersMenuStyle.groupTitle, name), - groupFilters.groupBy(superGroup).toList.sortBy(_._1).map { case (superGroup, subGroupFilters) => - div( - superGroup.toString, - subGroup(books, filters, name, subGroupFilters.map(Filter(_))) - ) - } - ) - - def subGroup( - books: Rx[Seq[Book]], - filters: Var[Seq[Filter]], - name: String, - groupFilters: Seq[Filter] - )( - implicit context: Ctx.Data - ): Frag = { - val filtersWithCount = Rx { - groupFilters - .filter(filter => !Filter.contains(filters(), filter)) - .map(filter => (filter, Book.filter(books(), filter +: filters()).length)) - .filter(_._2 > 0) - } - - if(filtersWithCount().isEmpty) - span("") - else - div( - filtersWithCount().map { case (filter, count) => - button( - FiltersMenuStyle.filter, - RxAttr(onclick, Rx(() => filters() = filter +: filters())), - span(s"${filter.name} ($count)") - ) - } - ) - } } diff --git a/src/main/scala/reading/component/index/style/Books.scala b/src/main/scala/reading/component/index/style/Books.scala index 69e1c55..efc6af8 100644 --- a/src/main/scala/reading/component/index/style/Books.scala +++ b/src/main/scala/reading/component/index/style/Books.scala @@ -18,7 +18,6 @@ object Books extends StyleSheet.Inline { display.inlineBlock, fontWeight.bold, marginBottom(15.px), - marginRight(10.px), color(Col.congoBrown) ) @@ -26,27 +25,11 @@ object Books extends StyleSheet.Inline { display.inlineBlock ) - val period = style( - marginLeft(20.px), - marginBottom(10.px) - ) - - val genres = style( - marginLeft(20.px), - marginBottom(10.px) - ) - - val themes = style( - marginLeft(20.px), - marginBottom(10.px) + val description = style( + marginLeft(20.px) ) - val program = style( - marginLeft(20.px), + val item = style( marginBottom(10.px) ) - - val pages = style( - marginLeft(20.px) - ) } diff --git a/src/main/scala/reading/models/Book.scala b/src/main/scala/reading/models/Book.scala index ef9eb9b..344b4c6 100644 --- a/src/main/scala/reading/models/Book.scala +++ b/src/main/scala/reading/models/Book.scala @@ -6,7 +6,7 @@ case class Book ( period: Period, genres: Seq[Genre], themes: Seq[Theme], - program: Program, + programs: Seq[Program], pages: Int ) extends Ordered[Book] { def compare(that: Book) = { diff --git a/src/main/scala/reading/models/Filter.scala b/src/main/scala/reading/models/Filter.scala index a74a6f1..b8adcc1 100644 --- a/src/main/scala/reading/models/Filter.scala +++ b/src/main/scala/reading/models/Filter.scala @@ -12,6 +12,7 @@ case object ThemeKind extends FilterKind case object GenreKind extends FilterKind case object DifficultyKind extends FilterKind case object ProgramKind extends FilterKind +case object LevelKind extends FilterKind object Filter { def apply[T](in: T)(implicit filterFactory: FilterFactory[T]): Filter = @@ -22,6 +23,11 @@ object Filter { def equals(f1: Filter, f2: Filter): Boolean = f1.kind == f2.kind && f1.name == f2.name + + def remove(fs: Seq[Filter], rf: Filter): Seq[Filter] = + fs.filterNot { f => + equals(f, rf) || rf.kind == LevelKind && f.kind == ProgramKind + } } trait FilterFactory[T] { @@ -59,9 +65,18 @@ object FilterFactory { implicit object ProgramFilter extends FilterFactory[Program] { def create(program: Program): Filter = new Filter { - def filter(book: Book): Boolean = book.program == program + def filter(book: Book): Boolean = book.programs.contains(program) val kind: FilterKind = ProgramKind val name: String = program.toString() } } + + implicit object LevelFilter extends FilterFactory[Level] { + def create(level: Level): Filter = + new Filter { + def filter(book: Book): Boolean = book.programs.map(Program.level).contains(level) + val kind: FilterKind = LevelKind + val name: String = level.toString() + } + } } diff --git a/src/main/scala/reading/models/Genre.scala b/src/main/scala/reading/models/Genre.scala index bc5d219..1b4aabd 100644 --- a/src/main/scala/reading/models/Genre.scala +++ b/src/main/scala/reading/models/Genre.scala @@ -2,9 +2,13 @@ package reading.models import enumeratum._ -sealed trait Genre extends EnumEntry { +sealed trait Genre extends EnumEntry with Ordered[Genre] { import Genre._ + def compare(that: Genre): Int = { + values.indexOf(that) - values.indexOf(this) + } + override def toString(): String = this match { case JournalIntime => "journal intime" case RomanHistorique => "roman historique" diff --git a/src/main/scala/reading/models/Level.scala b/src/main/scala/reading/models/Level.scala index 1f7e153..329c720 100644 --- a/src/main/scala/reading/models/Level.scala +++ b/src/main/scala/reading/models/Level.scala @@ -5,14 +5,8 @@ import enumeratum._ sealed trait Level extends EnumEntry with Ordered[Level] { import Level._ - def compare(that: Level) = { - def toInt(level: Level): Int = level match { - case Sixieme => 6 - case Cinquieme => 5 - case Quatrieme => 4 - case Troisieme => 3 - } - toInt(that) - toInt(this) + def compare(that: Level): Int = { + values.indexOf(that) - values.indexOf(this) } override def toString(): String = this match { diff --git a/src/main/scala/reading/models/Program.scala b/src/main/scala/reading/models/Program.scala index 4603b03..641f352 100644 --- a/src/main/scala/reading/models/Program.scala +++ b/src/main/scala/reading/models/Program.scala @@ -2,9 +2,13 @@ package reading.models import enumeratum._ -sealed trait Program extends EnumEntry { +sealed trait Program extends EnumEntry with Ordered[Program] { import Program._ + def compare(that: Program): Int = { + values.indexOf(that) - values.indexOf(this) + } + override def toString(): String = this match { case Monstre => "Le monstre, aux limites de l'humain" case RecitAventure => "Récits d'aventures" diff --git a/src/main/scala/reading/models/Theme.scala b/src/main/scala/reading/models/Theme.scala index 35b84b8..d816f1c 100644 --- a/src/main/scala/reading/models/Theme.scala +++ b/src/main/scala/reading/models/Theme.scala @@ -2,9 +2,13 @@ package reading.models import enumeratum._ -sealed trait Theme extends EnumEntry { +sealed trait Theme extends EnumEntry with Ordered[Theme] { import Theme._ + def compare(that: Theme): Int = { + values.indexOf(that) - values.indexOf(this) + } + override def toString(): String = this match { case Amitie => "amitié" case Aventure => "aventure" |