From c9a55c8217f3a8526e975aae2c08d1360395e4e8 Mon Sep 17 00:00:00 2001 From: "Maxence G. de Montauzan" Date: Fri, 5 May 2017 02:52:43 +0200 Subject: [PATCH] Really same artist/album + stats --- dashboard/src/app/artist.component.html | 21 +++++++++ dashboard/src/app/artist.component.ts | 57 ++++++++++++++++++++++--- dashboard/src/app/els.service.ts | 21 +++++++++ 3 files changed, 94 insertions(+), 5 deletions(-) diff --git a/dashboard/src/app/artist.component.html b/dashboard/src/app/artist.component.html index 35c7b80..ee5b8ed 100644 --- a/dashboard/src/app/artist.component.html +++ b/dashboard/src/app/artist.component.html @@ -5,6 +5,27 @@ Returned song: {{songs.length}}
+
+
+
+
+
+
+ +
+
+
+

+

{{albumNames.length}}

+
+

Total time
+
+
+
+
+
+
+ diff --git a/dashboard/src/app/artist.component.ts b/dashboard/src/app/artist.component.ts index 0d3ba73..205fee6 100644 --- a/dashboard/src/app/artist.component.ts +++ b/dashboard/src/app/artist.component.ts @@ -7,8 +7,8 @@ import { Song } from './object/song'; @Component({ selector: 'artist-component', - templateUrl: './artist.component.html' -// styleUrls: [ './album.component.css' ] + templateUrl: './artist.component.html', + styleUrls: [ './album.component.css', './dashboard.component.css' ] }) export class ArtistComponent implements OnInit { @@ -19,17 +19,64 @@ export class ArtistComponent implements OnInit { ) { } artistName = ""; - songs: Array = []; + moreDataAvailable: boolean = false; + atBottom: boolean = false; + albumNames: string[]; + ngOnInit(): void { this.route.params .subscribe((params: Params) => this.artistName = params['name']); - this.elsService.getArtistSongs(this.artistName).subscribe( + this.loadSongs(); + + this.elsService.getArtistAlbums(this.artistName).subscribe(data => this.albumNames = data ); + } + + // TODO Duplicate code! + loadSongs(): void { + this.elsService.getArtistSongs(this.artistName, this.songs.length).subscribe( data => { - this.songs = data; + console.log(this.moreDataAvailable); + this.moreDataAvailable = data.length == ElsService.DEFAULT_SIZE; + + // Erase song array with result for first load, then add elements one by one + // instead use concat => concat will sort table at each load, very consuming! and not user friendly + if (this.songs.length == 0) { + this.songs = data; + } else { + data.forEach(song => { + this.songs.push(song); + }); + } } ); } + + scrollTop(): void { + window.scrollTo(0,0); + } + + /** + * Handle scroll: + * - load data if at bottom of screen (if needed) + * - hide/show "go to top" button + * + * @param event scroll event + */ + onScroll(event: any) { + if (this.moreDataAvailable && + (window.innerHeight + window.scrollY) >= document.body.offsetHeight) { + this.loadSongs(); + } + + if (window.scrollY > window.innerHeight) { + this.atBottom = true; + } + + if (window.scrollY == 0) { + this.atBottom = false; + } + } } \ No newline at end of file diff --git a/dashboard/src/app/els.service.ts b/dashboard/src/app/els.service.ts index ca684a1..e5b529d 100644 --- a/dashboard/src/app/els.service.ts +++ b/dashboard/src/app/els.service.ts @@ -162,6 +162,27 @@ export class ElsService { }); } + getArtistAlbums(artistName: string): Observable { + return this.http + .post(this.elsUrl + "artist/_search", + JSON.stringify({"fields": "Album","query":{"match_phrase":{"Artist":artistName}},"size": ElsService.DEFAULT_SIZE}), + {headers: this.headers}) + .map(res => res.json().hits.hits) + .map((hits: Array) => { + // Theorically, my script prevent to found two documents with this query. + // But Prevention is better than cure as Shakespeare said + if (hits.length < 1) { + console.info('No artist "' + artistName + '" found.'); + return undefined; + } + if (hits.length > 1) { + console.error('More than one artist "' + artistName + '" found, return the first.'); + console.error('This is not normal!') + } + return hits[0].fields.Album; + }); + } + private handleError(error: any): Promise { console.error('An error occurred', error); // for demo purposes only return Promise.reject(error.message || error);