Rewritten infinite scrolling
This commit is contained in:
@ -1,70 +1,39 @@
|
||||
package soundtracks
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
// Best renders the soundtracks page.
|
||||
// Best renders the best soundtracks.
|
||||
func Best(ctx *aero.Context) string {
|
||||
user := utils.GetUser(ctx)
|
||||
index, _ := ctx.GetInt("index")
|
||||
|
||||
tracks := arn.FilterSoundTracks(func(track *arn.SoundTrack) bool {
|
||||
return !track.IsDraft && len(track.Media) > 0
|
||||
})
|
||||
|
||||
arn.SortSoundTracksPopularFirst(tracks)
|
||||
|
||||
// Limit the number of displayed tracks
|
||||
loadMoreIndex := 0
|
||||
|
||||
if len(tracks) > maxTracks {
|
||||
tracks = tracks[:maxTracks]
|
||||
loadMoreIndex = maxTracks
|
||||
}
|
||||
|
||||
return ctx.HTML(components.SoundTracks(tracks, loadMoreIndex, "", user))
|
||||
}
|
||||
|
||||
// BestFrom renders the soundtracks from the given index.
|
||||
func BestFrom(ctx *aero.Context) string {
|
||||
user := utils.GetUser(ctx)
|
||||
index, err := ctx.GetInt("index")
|
||||
|
||||
if err != nil {
|
||||
return ctx.Error(http.StatusBadRequest, "Invalid start index", err)
|
||||
}
|
||||
|
||||
allTracks := arn.FilterSoundTracks(func(track *arn.SoundTrack) bool {
|
||||
return !track.IsDraft && len(track.Media) > 0
|
||||
})
|
||||
|
||||
if index < 0 || index >= len(allTracks) {
|
||||
return ctx.Error(http.StatusBadRequest, "Invalid start index (maximum is "+strconv.Itoa(len(allTracks))+")", nil)
|
||||
}
|
||||
// Fetch all eligible tracks
|
||||
allTracks := fetchAll()
|
||||
|
||||
// Sort the tracks by date
|
||||
arn.SortSoundTracksPopularFirst(allTracks)
|
||||
|
||||
// Slice the part that we need
|
||||
tracks := allTracks[index:]
|
||||
|
||||
if len(tracks) > maxTracks {
|
||||
tracks = tracks[:maxTracks]
|
||||
}
|
||||
|
||||
nextIndex := index + maxTracks
|
||||
// Next index
|
||||
nextIndex := infinitescroll.NextIndex(ctx, len(allTracks), maxTracks, index)
|
||||
|
||||
if nextIndex >= len(allTracks) {
|
||||
// End of data - no more scrolling
|
||||
ctx.Response().Header().Set("X-LoadMore-Index", "-1")
|
||||
} else {
|
||||
// Send the index for the next request
|
||||
ctx.Response().Header().Set("X-LoadMore-Index", strconv.Itoa(nextIndex))
|
||||
// In case we're scrolling, send soundtracks only (without the page frame)
|
||||
if index > 0 {
|
||||
return ctx.HTML(components.SoundTracksScrollable(tracks, user))
|
||||
}
|
||||
|
||||
return ctx.HTML(components.SoundTracksScrollable(tracks, user))
|
||||
// Otherwise, send the full page
|
||||
return ctx.HTML(components.SoundTracks(tracks, nextIndex, "", user))
|
||||
}
|
||||
|
12
pages/soundtracks/fetch.go
Normal file
12
pages/soundtracks/fetch.go
Normal file
@ -0,0 +1,12 @@
|
||||
package soundtracks
|
||||
|
||||
import (
|
||||
"github.com/animenotifier/arn"
|
||||
)
|
||||
|
||||
// fetchAll returns all soundtracks
|
||||
func fetchAll() []*arn.SoundTrack {
|
||||
return arn.FilterSoundTracks(func(track *arn.SoundTrack) bool {
|
||||
return !track.IsDraft && len(track.Media) > 0
|
||||
})
|
||||
}
|
@ -1,72 +1,41 @@
|
||||
package soundtracks
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"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 maxTracks = 12
|
||||
|
||||
// Latest renders the soundtracks page.
|
||||
// Latest renders the latest soundtracks.
|
||||
func Latest(ctx *aero.Context) string {
|
||||
user := utils.GetUser(ctx)
|
||||
index, _ := ctx.GetInt("index")
|
||||
|
||||
tracks := arn.FilterSoundTracks(func(track *arn.SoundTrack) bool {
|
||||
return !track.IsDraft && len(track.Media) > 0
|
||||
})
|
||||
|
||||
arn.SortSoundTracksLatestFirst(tracks)
|
||||
|
||||
// Limit the number of displayed tracks
|
||||
loadMoreIndex := 0
|
||||
|
||||
if len(tracks) > maxTracks {
|
||||
tracks = tracks[:maxTracks]
|
||||
loadMoreIndex = maxTracks
|
||||
}
|
||||
|
||||
return ctx.HTML(components.SoundTracks(tracks, loadMoreIndex, "", user))
|
||||
}
|
||||
|
||||
// LatestFrom renders the soundtracks from the given index.
|
||||
func LatestFrom(ctx *aero.Context) string {
|
||||
user := utils.GetUser(ctx)
|
||||
index, err := ctx.GetInt("index")
|
||||
|
||||
if err != nil {
|
||||
return ctx.Error(http.StatusBadRequest, "Invalid start index", err)
|
||||
}
|
||||
|
||||
allTracks := arn.FilterSoundTracks(func(track *arn.SoundTrack) bool {
|
||||
return !track.IsDraft && len(track.Media) > 0
|
||||
})
|
||||
|
||||
if index < 0 || index >= len(allTracks) {
|
||||
return ctx.Error(http.StatusBadRequest, "Invalid start index (maximum is "+strconv.Itoa(len(allTracks))+")", nil)
|
||||
}
|
||||
// Fetch all eligible tracks
|
||||
allTracks := fetchAll()
|
||||
|
||||
// Sort the tracks by date
|
||||
arn.SortSoundTracksLatestFirst(allTracks)
|
||||
|
||||
// Slice the part that we need
|
||||
tracks := allTracks[index:]
|
||||
|
||||
if len(tracks) > maxTracks {
|
||||
tracks = tracks[:maxTracks]
|
||||
}
|
||||
|
||||
nextIndex := index + maxTracks
|
||||
// Next index
|
||||
nextIndex := infinitescroll.NextIndex(ctx, len(allTracks), maxTracks, index)
|
||||
|
||||
if nextIndex >= len(allTracks) {
|
||||
// End of data - no more scrolling
|
||||
ctx.Response().Header().Set("X-LoadMore-Index", "-1")
|
||||
} else {
|
||||
// Send the index for the next request
|
||||
ctx.Response().Header().Set("X-LoadMore-Index", strconv.Itoa(nextIndex))
|
||||
// In case we're scrolling, send soundtracks only (without the page frame)
|
||||
if index > 0 {
|
||||
return ctx.HTML(components.SoundTracksScrollable(tracks, user))
|
||||
}
|
||||
|
||||
return ctx.HTML(components.SoundTracksScrollable(tracks, user))
|
||||
// Otherwise, send the full page
|
||||
return ctx.HTML(components.SoundTracks(tracks, nextIndex, "", user))
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
component SoundTracks(tracks []*arn.SoundTrack, loadMoreIndex int, tag string, user *arn.User)
|
||||
component SoundTracks(tracks []*arn.SoundTrack, nextIndex int, tag string, user *arn.User)
|
||||
h1.page-title Soundtracks
|
||||
|
||||
SoundTracksTabs(tag)
|
||||
@ -17,9 +17,9 @@ component SoundTracks(tracks []*arn.SoundTrack, loadMoreIndex int, tag string, u
|
||||
#load-more-target.soundtracks
|
||||
SoundTracksScrollable(tracks, user)
|
||||
|
||||
if loadMoreIndex != 0
|
||||
if nextIndex != -1
|
||||
.buttons
|
||||
LoadMore(loadMoreIndex)
|
||||
LoadMore(nextIndex)
|
||||
|
||||
component SoundTracksScrollable(tracks []*arn.SoundTrack, user *arn.User)
|
||||
each track in tracks
|
||||
|
Reference in New Issue
Block a user