From ca020a9faadead757e152fa5552f9d7b8f4f2123 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Mon, 16 Apr 2018 15:48:54 +0200 Subject: [PATCH] Added infinite scrolling to AMVs --- pages/amvs/amvs.go | 44 -------------------------------------------- pages/amvs/best.go | 22 ++++++++++++++++++++++ pages/amvs/fetch.go | 9 +++++++++ pages/amvs/latest.go | 18 ++++++++++++++++++ pages/amvs/render.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ pages/index.go | 2 ++ 6 files changed, 95 insertions(+), 44 deletions(-) delete mode 100644 pages/amvs/amvs.go create mode 100644 pages/amvs/best.go create mode 100644 pages/amvs/fetch.go create mode 100644 pages/amvs/latest.go create mode 100644 pages/amvs/render.go diff --git a/pages/amvs/amvs.go b/pages/amvs/amvs.go deleted file mode 100644 index 94b69b57..00000000 --- a/pages/amvs/amvs.go +++ /dev/null @@ -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)) -} diff --git a/pages/amvs/best.go b/pages/amvs/best.go new file mode 100644 index 00000000..b6c1b0be --- /dev/null +++ b/pages/amvs/best.go @@ -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) +} diff --git a/pages/amvs/fetch.go b/pages/amvs/fetch.go new file mode 100644 index 00000000..15971de8 --- /dev/null +++ b/pages/amvs/fetch.go @@ -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 + }) +} diff --git a/pages/amvs/latest.go b/pages/amvs/latest.go new file mode 100644 index 00000000..b3db602e --- /dev/null +++ b/pages/amvs/latest.go @@ -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) +} diff --git a/pages/amvs/render.go b/pages/amvs/render.go new file mode 100644 index 00000000..ecc97b50 --- /dev/null +++ b/pages/amvs/render.go @@ -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)) +} diff --git a/pages/index.go b/pages/index.go index cf10f90f..2dd1b6e6 100644 --- a/pages/index.go +++ b/pages/index.go @@ -143,7 +143,9 @@ func Configure(app *aero.Application) { // AMVs l.Page("/amvs", amvs.Latest) + l.Page("/amvs/from/:index", amvs.Latest) l.Page("/amvs/best", amvs.Best) + l.Page("/amvs/best/from/:index", amvs.Best) l.Page("/amv/:id", amv.Get) l.Page("/amv/:id/edit", amv.Edit) l.Page("/amv/:id/history", amv.History)