72 lines
1.8 KiB
TypeScript
72 lines
1.8 KiB
TypeScript
import { Component, OnInit } from '@angular/core';
|
|
import { ElsAlbumService } from '../els-album.service';
|
|
|
|
import { Album } from '../model/album';
|
|
|
|
import { Utils } from '../utils';
|
|
|
|
enum query_edit_type {
|
|
exclude = 'must_not',
|
|
select = 'must'
|
|
}
|
|
|
|
@Component({
|
|
selector: 'app-albums',
|
|
templateUrl: './albums.component.html',
|
|
styleUrls: ['./albums.component.css']
|
|
})
|
|
export class AlbumsComponent implements OnInit {
|
|
numberToArray = Utils.numberToArray;
|
|
albums: Album[] = [];
|
|
filterQuery = Object.assign({}, ElsAlbumService.GET_ALBUMS_DEFAULT_QUERY);
|
|
queryEdited = false;
|
|
|
|
constructor(private elsService : ElsAlbumService) { }
|
|
|
|
ngOnInit(): void {
|
|
this.loadData();
|
|
}
|
|
|
|
private editQuery(field: string, value: Album, type: query_edit_type): void {
|
|
// TODO Move this method to a service
|
|
if (value[field] instanceof Array) {
|
|
value[field] = value[field][0]
|
|
}
|
|
|
|
// If firt edit, add needed fields in ELS Query
|
|
if (!this.filterQuery['query']) {
|
|
this.filterQuery['query']['bool'][type].push({ 'must': [] })
|
|
this.filterQuery['query']['bool'][type].push({ 'must_not': [] })
|
|
}
|
|
|
|
|
|
this.filterQuery['query']['bool'][type].push({
|
|
'match_phrase': {
|
|
[field]: value[field]
|
|
}
|
|
})
|
|
|
|
this.queryEdited = true;
|
|
}
|
|
|
|
exlude(field: string, value: Album): void {
|
|
this.editQuery(field, value, query_edit_type.exclude)
|
|
this.loadData()
|
|
}
|
|
|
|
select(field: string, value: Album): void {
|
|
this.editQuery(field, value, query_edit_type.select)
|
|
this.loadData()
|
|
}
|
|
|
|
resetQuery(): void {
|
|
this.filterQuery = Object.assign({}, ElsAlbumService.GET_ALBUMS_DEFAULT_QUERY);
|
|
this.loadData();
|
|
}
|
|
|
|
loadData(): void {
|
|
// console.log(JSON.stringify(this.filterQuery))
|
|
this.elsService.getAlbums(this.filterQuery).subscribe(data => this.albums = data);
|
|
}
|
|
}
|