Added infinite scrolling to companies

This commit is contained in:
Eduard Urbach 2018-03-13 23:06:16 +01:00
parent 3e2594ab9f
commit 40e2c43a8d
12 changed files with 64 additions and 63 deletions

View File

@ -11,7 +11,7 @@ import (
"github.com/animenotifier/notify.moe/utils"
)
// All renders the companies page.
// All renders an index of all companies.
func All(ctx *aero.Context) string {
user := utils.GetUser(ctx)
@ -44,5 +44,5 @@ func All(ctx *aero.Context) string {
groups[currentGroupIndex] = append(groups[currentGroupIndex], company)
}
return ctx.HTML(components.Companies(groups, user))
return ctx.HTML(components.CompaniesIndex(groups, user))
}

View File

@ -1,11 +1,11 @@
component Companies(groups [][]*arn.Company, user *arn.User)
component CompaniesIndex(groups [][]*arn.Company, user *arn.User)
CompaniesTabs(user)
h1.page-title All companies
.company-groups
.company-index-groups
each group in groups
.company-group.mountable
.company-index-group.mountable
h3= strings.ToUpper(group[0].Name.English[:1])
ul
@ -15,8 +15,8 @@ component Companies(groups [][]*arn.Company, user *arn.User)
component CompaniesTabs(user *arn.User)
.tabs
Tab("All", "font", "/companies")
Tab("Popular", "globe", "/companies/popular")
Tab("Popular", "globe", "/companies")
Tab("All", "font", "/companies/all")
.corner-buttons
if user != nil && (user.Role == "editor" || user.Role == "admin")

View File

@ -0,0 +1,5 @@
.company-index-groups
horizontal-wrap
.company-index-group
padding 1rem

View File

@ -1,5 +0,0 @@
.company-groups
horizontal-wrap
.company-group
padding 1rem

10
pages/companies/fetch.go Normal file
View File

@ -0,0 +1,10 @@
package companies
import "github.com/animenotifier/arn"
// fetchAll returns all companies
func fetchAll() []*arn.Company {
return arn.FilterCompanies(func(company *arn.Company) bool {
return !company.IsDraft
})
}

View File

@ -1,57 +1,41 @@
package companies
import (
"sort"
"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 maxPopularCompanies = 100
const maxPopularCompanies = 50
// Popular renders the companies sorted by popularity.
// Popular renders the best companies.
func Popular(ctx *aero.Context) string {
user := utils.GetUser(ctx)
companies := []*arn.Company{}
index, _ := ctx.GetInt("index")
// ID to popularity
popularity := map[string]int{}
// Fetch all eligible companies
allCompanies := fetchAll()
for anime := range arn.StreamAnime() {
for _, studio := range anime.Studios() {
popularity[studio.ID] += anime.Popularity.Watching + anime.Popularity.Completed
}
}
// Sort the companies by popularity
arn.SortCompaniesPopularFirst(allCompanies)
for companyID := range popularity {
company, err := arn.GetCompany(companyID)
if err != nil {
continue
}
companies = append(companies, company)
}
sort.Slice(companies, func(i, j int) bool {
a := companies[i]
b := companies[j]
aPopularity := popularity[a.ID]
bPopularity := popularity[b.ID]
if aPopularity == bPopularity {
return a.Name.English < b.Name.English
}
return aPopularity > bPopularity
})
// Slice the part that we need
companies := allCompanies[index:]
if len(companies) > maxPopularCompanies {
companies = companies[:maxPopularCompanies]
}
return ctx.HTML(components.PopularCompanies(companies, popularity, user))
// Next index
nextIndex := infinitescroll.NextIndex(ctx, len(allCompanies), maxPopularCompanies, index)
// In case we're scrolling, send companies only (without the page frame)
if index > 0 {
return ctx.HTML(components.PopularCompaniesScrollable(companies, user))
}
// Otherwise, send the full page
return ctx.HTML(components.PopularCompanies(companies, nextIndex, user))
}

View File

@ -1,11 +1,17 @@
component PopularCompanies(companies []*arn.Company, popularity map[string]int, user *arn.User)
component PopularCompanies(companies []*arn.Company, nextIndex int, user *arn.User)
CompaniesTabs(user)
h1.page-title Popular companies
.companies
ol
each company in companies
li.mountable
a.ajax(href=company.Link())= company.Name.English
span= " (" + strconv.Itoa(popularity[company.ID]) + ")"
ol#load-more-target.companies
PopularCompaniesScrollable(companies, user)
if nextIndex != -1
.buttons
LoadMore(nextIndex)
component PopularCompaniesScrollable(companies []*arn.Company, user *arn.User)
each company in companies
li.company.mountable
a.ajax(href=company.Link())= company.Name.English
//- span= " (" + strconv.Itoa(popularity[company.ID]) + ")"

View File

@ -11,7 +11,7 @@ import (
const maxDescriptionLength = 170
// Get company.
// Get renders a company page.
func Get(ctx *aero.Context) string {
user := utils.GetUser(ctx)
id := ctx.Get("id")

View File

@ -131,8 +131,9 @@ func Configure(app *aero.Application) {
l.Page("/company/:id", company.Get)
l.Page("/company/:id/edit", company.Edit)
l.Page("/company/:id/history", company.History)
l.Page("/companies", companies.All)
l.Page("/companies/popular", companies.Popular)
l.Page("/companies", companies.Popular)
l.Page("/companies/from/:index", companies.Popular)
l.Page("/companies/all", companies.All)
// Settings
l.Page("/settings", settings.Get(components.SettingsPersonal))

View File

@ -16,7 +16,7 @@ func Best(ctx *aero.Context) string {
// Fetch all eligible quotes
allQuotes := fetchAll()
// Sort the quotes by date
// Sort the quotes by number of likes
arn.SortQuotesPopularFirst(allQuotes)
// Slice the part that we need

View File

@ -1,4 +1,4 @@
component Quotes(quotes []*arn.Quote, loadMoreIndex int, user *arn.User)
component Quotes(quotes []*arn.Quote, nextIndex int, user *arn.User)
h1.page-title Quotes
QuotesTabs
@ -17,9 +17,9 @@ component Quotes(quotes []*arn.Quote, loadMoreIndex int, user *arn.User)
#load-more-target.quotes
QuotesScrollable(quotes, user)
if loadMoreIndex != -1
if nextIndex != -1
.buttons
LoadMore(loadMoreIndex)
LoadMore(nextIndex)
component QuotesScrollable(quotes []*arn.Quote, user *arn.User)
each quote in quotes

View File

@ -16,7 +16,7 @@ func Best(ctx *aero.Context) string {
// Fetch all eligible tracks
allTracks := fetchAll()
// Sort the tracks by date
// Sort the tracks by number of likes
arn.SortSoundTracksPopularFirst(allTracks)
// Slice the part that we need