From 22521c8f282991bcaa8dd772eb1c2e8700fd552d Mon Sep 17 00:00:00 2001
From: Geomitron <22552797+Geomitron@users.noreply.github.com>
Date: Fri, 22 Dec 2023 13:48:41 -0600
Subject: [PATCH] Table scrolling and sorting
---
.../components/browse/browse.component.html | 3 ++-
.../result-table/result-table.component.html | 24 +++++++++++++------
.../result-table/result-table.component.ts | 24 +++++++++++++++----
.../app/core/services/search.service.ts | 6 +++--
4 files changed, 42 insertions(+), 15 deletions(-)
diff --git a/src-angular/app/components/browse/browse.component.html b/src-angular/app/components/browse/browse.component.html
index 75c2899..abffae1 100644
--- a/src-angular/app/components/browse/browse.component.html
+++ b/src-angular/app/components/browse/browse.component.html
@@ -2,7 +2,8 @@
diff --git a/src-angular/app/components/browse/result-table/result-table.component.html b/src-angular/app/components/browse/result-table/result-table.component.html
index eeb6236..2c872bf 100644
--- a/src-angular/app/components/browse/result-table/result-table.component.html
+++ b/src-angular/app/components/browse/result-table/result-table.component.html
@@ -1,17 +1,27 @@
-
+
-
|
|
- Name |
- Artist |
- Album |
- Genre |
- Year |
+
+ Name
+ |
+
+ Artist
+
+ |
+
+ Album
+ |
+
+ Genre
+ |
+
+ Year
+ |
diff --git a/src-angular/app/components/browse/result-table/result-table.component.ts b/src-angular/app/components/browse/result-table/result-table.component.ts
index 863417c..442feee 100644
--- a/src-angular/app/components/browse/result-table/result-table.component.ts
+++ b/src-angular/app/components/browse/result-table/result-table.component.ts
@@ -1,6 +1,6 @@
import { Component, EventEmitter, HostBinding, OnInit, Output, QueryList, ViewChild, ViewChildren } from '@angular/core'
-import Comparators from 'comparators'
+import { sortBy } from 'lodash'
import { SettingsService } from 'src-angular/app/core/services/settings.service'
import { ChartData } from 'src-shared/interfaces/search.interface'
@@ -22,7 +22,7 @@ export class ResultTableComponent implements OnInit {
@ViewChildren('tableRow') tableRows: QueryList
activeSong: ChartData[] | null = null
- sortDirection: 'ascending' | 'descending' = 'descending'
+ sortDirection: 'ascending' | 'descending' = 'ascending'
sortColumn: 'name' | 'artist' | 'album' | 'genre' | 'year' | null = null
constructor(
@@ -53,7 +53,7 @@ export class ResultTableComponent implements OnInit {
if (this.songs.length === 0) { return }
if (this.sortColumn !== column) {
this.sortColumn = column
- this.sortDirection = 'descending'
+ this.sortDirection = 'ascending'
} else if (this.sortDirection === 'descending') {
this.sortDirection = 'ascending'
} else {
@@ -63,8 +63,11 @@ export class ResultTableComponent implements OnInit {
}
private updateSort() {
- if (this.sortColumn !== null) {
- this.songs.sort(Comparators.comparing(this.sortColumn, { reversed: this.sortDirection === 'ascending' }))
+ const col = this.sortColumn
+ if (col !== null) {
+ const groupedSongs = sortBy(this.searchService.groupedSongs, song => song[0][col])
+ if (this.sortDirection === 'descending') { groupedSongs.reverse() }
+ this.searchService.groupedSongs = groupedSongs
}
}
@@ -79,4 +82,15 @@ export class ResultTableComponent implements OnInit {
this.selectionService.deselectAll()
}
}
+
+ tableScrolled(event: Event): void {
+ if (event.target instanceof HTMLElement) {
+ if (event.target.scrollHeight - (event.target.scrollTop + event.target.clientHeight) < 100) {
+ // Scrolled near the bottom of the table
+ if (this.searchService.areMorePages && !this.searchService.searchLoading) {
+ this.searchService.search(this.searchService.searchControl.value || '*', true).subscribe()
+ }
+ }
+ }
+ }
}
diff --git a/src-angular/app/core/services/search.service.ts b/src-angular/app/core/services/search.service.ts
index e2b5c94..ce3fb8d 100644
--- a/src-angular/app/core/services/search.service.ts
+++ b/src-angular/app/core/services/search.service.ts
@@ -8,6 +8,8 @@ import { Difficulty, Instrument } from 'scan-chart'
import { environment } from 'src-angular/environments/environment'
import { AdvancedSearch, ChartData, SearchResult } from 'src-shared/interfaces/search.interface'
+const resultsPerPage = 25
+
@Injectable({
providedIn: 'root',
})
@@ -61,7 +63,7 @@ export class SearchService {
this.search().subscribe()
}
- get areMorePages() { return this.songsResponse.page && this.groupedSongs.length === this.songsResponse.page * 10 }
+ get areMorePages() { return this.songsResponse.page && this.groupedSongs.length === this.songsResponse.page * resultsPerPage }
/**
* General search, uses the `/search?q=` endpoint.
@@ -83,7 +85,7 @@ export class SearchService {
let retries = 10
return this.http.post(`${environment.apiUrl}/search`, {
search,
- per_page: 25,
+ per_page: resultsPerPage,
page: this.currentPage,
instrument: this.instrument.value,
difficulty: this.difficulty.value,