From 40e2c43a8d8a7555bdaf1e066cad6bbfb5cc67df Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Tue, 13 Mar 2018 23:06:16 +0100 Subject: [PATCH] Added infinite scrolling to companies --- pages/companies/{companies.go => all.go} | 4 +- pages/companies/{companies.pixy => all.pixy} | 10 ++-- pages/companies/all.scarlet | 5 ++ pages/companies/companies.scarlet | 5 -- pages/companies/fetch.go | 10 ++++ pages/companies/popular.go | 56 +++++++------------- pages/companies/popular.pixy | 20 ++++--- pages/company/company.go | 2 +- pages/index.go | 5 +- pages/quotes/best.go | 2 +- pages/quotes/quotes.pixy | 6 +-- pages/soundtracks/best.go | 2 +- 12 files changed, 64 insertions(+), 63 deletions(-) rename pages/companies/{companies.go => all.go} (90%) rename pages/companies/{companies.pixy => all.pixy} (76%) create mode 100644 pages/companies/all.scarlet delete mode 100644 pages/companies/companies.scarlet create mode 100644 pages/companies/fetch.go diff --git a/pages/companies/companies.go b/pages/companies/all.go similarity index 90% rename from pages/companies/companies.go rename to pages/companies/all.go index a5105703..ce5ee1cc 100644 --- a/pages/companies/companies.go +++ b/pages/companies/all.go @@ -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)) } diff --git a/pages/companies/companies.pixy b/pages/companies/all.pixy similarity index 76% rename from pages/companies/companies.pixy rename to pages/companies/all.pixy index c8986204..b554b4c3 100644 --- a/pages/companies/companies.pixy +++ b/pages/companies/all.pixy @@ -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") diff --git a/pages/companies/all.scarlet b/pages/companies/all.scarlet new file mode 100644 index 00000000..fe39ca60 --- /dev/null +++ b/pages/companies/all.scarlet @@ -0,0 +1,5 @@ +.company-index-groups + horizontal-wrap + +.company-index-group + padding 1rem \ No newline at end of file diff --git a/pages/companies/companies.scarlet b/pages/companies/companies.scarlet deleted file mode 100644 index a5fee370..00000000 --- a/pages/companies/companies.scarlet +++ /dev/null @@ -1,5 +0,0 @@ -.company-groups - horizontal-wrap - -.company-group - padding 1rem \ No newline at end of file diff --git a/pages/companies/fetch.go b/pages/companies/fetch.go new file mode 100644 index 00000000..642c2051 --- /dev/null +++ b/pages/companies/fetch.go @@ -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 + }) +} diff --git a/pages/companies/popular.go b/pages/companies/popular.go index a989e42e..34dba397 100644 --- a/pages/companies/popular.go +++ b/pages/companies/popular.go @@ -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)) } diff --git a/pages/companies/popular.pixy b/pages/companies/popular.pixy index c2a0d3a9..7ecc1ac1 100644 --- a/pages/companies/popular.pixy +++ b/pages/companies/popular.pixy @@ -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]) + ")" \ No newline at end of file + 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]) + ")" \ No newline at end of file diff --git a/pages/company/company.go b/pages/company/company.go index 76246e40..3a4c4894 100644 --- a/pages/company/company.go +++ b/pages/company/company.go @@ -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") diff --git a/pages/index.go b/pages/index.go index 3fd7b32e..f0667a35 100644 --- a/pages/index.go +++ b/pages/index.go @@ -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)) diff --git a/pages/quotes/best.go b/pages/quotes/best.go index cc6f92bf..3ac8d453 100644 --- a/pages/quotes/best.go +++ b/pages/quotes/best.go @@ -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 diff --git a/pages/quotes/quotes.pixy b/pages/quotes/quotes.pixy index 054672e7..4c017c23 100644 --- a/pages/quotes/quotes.pixy +++ b/pages/quotes/quotes.pixy @@ -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 diff --git a/pages/soundtracks/best.go b/pages/soundtracks/best.go index e0ee9b74..e2b4316d 100644 --- a/pages/soundtracks/best.go +++ b/pages/soundtracks/best.go @@ -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