Added infinite scrolling to AMVs

This commit is contained in:
Eduard Urbach 2018-04-16 15:48:54 +02:00
parent 39ef07307c
commit ca020a9faa
6 changed files with 95 additions and 44 deletions

View File

@ -1,44 +0,0 @@
package amvs
import (
"sort"
"github.com/aerogo/aero"
"github.com/animenotifier/arn"
"github.com/animenotifier/notify.moe/components"
"github.com/animenotifier/notify.moe/utils"
)
// Latest AMVs.
func Latest(ctx *aero.Context) string {
user := utils.GetUser(ctx)
amvs := arn.FilterAMVs(func(amv *arn.AMV) bool {
return !amv.IsDraft
})
sort.Slice(amvs, func(i, j int) bool {
return amvs[i].Created > amvs[j].Created
})
return ctx.HTML(components.AMVs(amvs, -1, "", user))
}
// Best AMVs.
func Best(ctx *aero.Context) string {
user := utils.GetUser(ctx)
amvs := arn.FilterAMVs(func(amv *arn.AMV) bool {
return !amv.IsDraft
})
sort.Slice(amvs, func(i, j int) bool {
if len(amvs[i].Likes) == len(amvs[j].Likes) {
return amvs[i].Title.String() < amvs[j].Title.String()
}
return len(amvs[i].Likes) > len(amvs[j].Likes)
})
return ctx.HTML(components.AMVs(amvs, -1, "", user))
}

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

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

9
pages/amvs/fetch.go Normal file
View File

@ -0,0 +1,9 @@
package amvs
import "github.com/animenotifier/arn"
func fetchAll() []*arn.AMV {
return arn.FilterAMVs(func(amv *arn.AMV) bool {
return !amv.IsDraft
})
}

18
pages/amvs/latest.go Normal file
View File

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

44
pages/amvs/render.go Normal file
View File

@ -0,0 +1,44 @@
package amvs
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 (
amvsFirstLoad = 12
amvsPerScroll = 9
)
// render renders the AMVs page with the given AMVs.
func render(ctx *aero.Context, allAMVs []*arn.AMV) string {
user := utils.GetUser(ctx)
index, _ := ctx.GetInt("index")
tag := ctx.Get("tag")
// Slice the part that we need
amvs := allAMVs[index:]
maxLength := amvsFirstLoad
if index > 0 {
maxLength = amvsPerScroll
}
if len(amvs) > maxLength {
amvs = amvs[:maxLength]
}
// Next index
nextIndex := infinitescroll.NextIndex(ctx, len(allAMVs), maxLength, index)
// In case we're scrolling, send AMVs only (without the page frame)
if index > 0 {
return ctx.HTML(components.AMVsScrollable(amvs, user))
}
// Otherwise, send the full page
return ctx.HTML(components.AMVs(amvs, nextIndex, tag, user))
}

View File

@ -143,7 +143,9 @@ func Configure(app *aero.Application) {
// AMVs // AMVs
l.Page("/amvs", amvs.Latest) l.Page("/amvs", amvs.Latest)
l.Page("/amvs/from/:index", amvs.Latest)
l.Page("/amvs/best", amvs.Best) l.Page("/amvs/best", amvs.Best)
l.Page("/amvs/best/from/:index", amvs.Best)
l.Page("/amv/:id", amv.Get) l.Page("/amv/:id", amv.Get)
l.Page("/amv/:id/edit", amv.Edit) l.Page("/amv/:id/edit", amv.Edit)
l.Page("/amv/:id/history", amv.History) l.Page("/amv/:id/history", amv.History)