Added characters page

This commit is contained in:
2018-04-22 17:43:20 +02:00
parent e8c66f28ab
commit 8a8b363e03
10 changed files with 203 additions and 9 deletions

22
pages/characters/best.go Normal file
View File

@ -0,0 +1,22 @@
package characters
import (
"sort"
"github.com/aerogo/aero"
)
// Best characters.
func Best(ctx *aero.Context) string {
characters := fetchAll()
sort.Slice(characters, func(i, j int) bool {
if len(characters[i].Likes) == len(characters[j].Likes) {
return characters[i].Name.Canonical < characters[j].Name.Canonical
}
return len(characters[i].Likes) > len(characters[j].Likes)
})
return render(ctx, characters)
}

View File

@ -14,19 +14,19 @@ component Characters(characters []*arn.Character, nextIndex int, tag string, use
Icon("pencil")
span Edit draft
//- #load-more-target.characters.characters-page
//- CharactersScrollable(characters, user)
#load-more-target.characters.characters-page
CharactersScrollable(characters, user)
//- if nextIndex != -1
//- .buttons
//- LoadMore(nextIndex)
if nextIndex != -1
.buttons
LoadMore(nextIndex)
component CharactersScrollable(characters []*arn.Character, user *arn.User)
each character in characters
Character(character, user)
component CharactersTabs(tag string)
//- .tab-groups
//- .tabs
//- Tab("Latest", "video-camera", "/characters")
//- Tab("Best", "heart", "/characters/best")
.tab-groups
.tabs
Tab("Latest", "child", "/characters")
Tab("Best", "heart", "/characters/best")

View File

@ -0,0 +1,9 @@
package characters
import "github.com/animenotifier/arn"
func fetchAll() []*arn.Character {
return arn.FilterCharacters(func(character *arn.Character) bool {
return !character.IsDraft
})
}

View File

@ -0,0 +1,18 @@
package characters
import (
"sort"
"github.com/aerogo/aero"
)
// Latest characters.
func Latest(ctx *aero.Context) string {
characters := fetchAll()
sort.Slice(characters, func(i, j int) bool {
return characters[i].Created > characters[j].Created
})
return render(ctx, characters)
}

View File

@ -0,0 +1,44 @@
package characters
import (
"github.com/aerogo/aero"
"github.com/animenotifier/arn"
"github.com/animenotifier/notify.moe/components"
"github.com/animenotifier/notify.moe/utils"
"github.com/animenotifier/notify.moe/utils/infinitescroll"
)
const (
charactersFirstLoad = 104
charactersPerScroll = 39
)
// render renders the characters page with the given characters.
func render(ctx *aero.Context, allCharacters []*arn.Character) string {
user := utils.GetUser(ctx)
index, _ := ctx.GetInt("index")
tag := ctx.Get("tag")
// Slice the part that we need
characters := allCharacters[index:]
maxLength := charactersFirstLoad
if index > 0 {
maxLength = charactersPerScroll
}
if len(characters) > maxLength {
characters = characters[:maxLength]
}
// Next index
nextIndex := infinitescroll.NextIndex(ctx, len(allCharacters), maxLength, index)
// In case we're scrolling, send Characters only (without the page frame)
if index > 0 {
return ctx.HTML(components.CharactersScrollable(characters, user))
}
// Otherwise, send the full page
return ctx.HTML(components.Characters(characters, nextIndex, tag, user))
}

View File

@ -19,6 +19,7 @@ import (
"github.com/animenotifier/notify.moe/pages/apiview/apidocs"
"github.com/animenotifier/notify.moe/pages/calendar"
"github.com/animenotifier/notify.moe/pages/character"
"github.com/animenotifier/notify.moe/pages/characters"
"github.com/animenotifier/notify.moe/pages/charge"
"github.com/animenotifier/notify.moe/pages/companies"
"github.com/animenotifier/notify.moe/pages/company"
@ -140,6 +141,12 @@ func Configure(app *aero.Application) {
l.Page("/anime/:id/edit/history", editanime.History)
// Characters
l.Page("/characters", characters.Latest)
l.Page("/characters/from/:index", characters.Latest)
l.Page("/characters/best", characters.Best)
l.Page("/characters/best/from/:index", characters.Best)
// Character
l.Page("/character/:id", character.Get)
l.Page("/character/:id/edit", character.Edit)
l.Page("/character/:id/edit/images", character.EditImages)
@ -150,6 +157,8 @@ func Configure(app *aero.Application) {
l.Page("/amvs/from/:index", amvs.Latest)
l.Page("/amvs/best", amvs.Best)
l.Page("/amvs/best/from/:index", amvs.Best)
// AMV
l.Page("/amv/:id", amv.Get)
l.Page("/amv/:id/edit", amv.Edit)
l.Page("/amv/:id/history", amv.History)