add table view to library page
This commit is contained in:
1 parent
34766ba8e9
commit
7a21c6ebd8
18 files changed
+473
-36
No files matched your search
@@ -0,0 +1,448 @@
|
||||
import { AfterViewInit, Component, OnDestroy, OnInit } from '@angular/core';
|
||||
import { Entry } from '../../../shared/models/entry.model';
|
||||
import { LibraryService } from '../../../core/services/library.service';
|
||||
import { ActivatedRoute, Event, NavigationEnd, Router } from '@angular/router';
|
||||
import { LibraryQueryParams } from '../../../shared/models/LibraryQueryParams';
|
||||
|
||||
declare var $: any;
|
||||
@Component({
|
||||
selector: 'app-library-page',
|
||||
templateUrl: './library-page.component.html',
|
||||
styleUrls: ['./library-page.component.css'],
|
||||
})
|
||||
export class LibraryPageComponent implements OnInit, AfterViewInit {
|
||||
entries: Entry[] = [];
|
||||
entriesToShow: Entry[] = [];
|
||||
|
||||
// ? pagination
|
||||
entriesCount: number = 0;
|
||||
maxPage: number = 10;
|
||||
currentPage: number = 1;
|
||||
pageSize: number = 10;
|
||||
|
||||
viewMode: string = 'grid';
|
||||
|
||||
// ? filter
|
||||
years: number[] = [];
|
||||
genres: string[] = [
|
||||
'Action & Adventure',
|
||||
'Animation',
|
||||
'Comedy',
|
||||
'Crime',
|
||||
'Documentary',
|
||||
'Drama',
|
||||
'Family',
|
||||
'Kids',
|
||||
'Mystery',
|
||||
'News',
|
||||
'Reality',
|
||||
'Sci-Fi & Fantasy',
|
||||
'Soap',
|
||||
'Talk',
|
||||
'War & Politics',
|
||||
'Western',
|
||||
'Action',
|
||||
'Adventure',
|
||||
'Fantasy',
|
||||
'History',
|
||||
'Horror',
|
||||
'Music',
|
||||
'Romance',
|
||||
'Science Fiction',
|
||||
'TV Movie',
|
||||
'Thriller',
|
||||
'War',
|
||||
];
|
||||
|
||||
filterCriteria: {
|
||||
title?: string;
|
||||
type: number;
|
||||
years?: number[];
|
||||
genres?: string[];
|
||||
favorit: boolean;
|
||||
watchlist: boolean;
|
||||
hidden: boolean;
|
||||
matchAll: boolean;
|
||||
} = {
|
||||
title: '',
|
||||
type: 0,
|
||||
years: [],
|
||||
genres: [],
|
||||
favorit: false,
|
||||
watchlist: false,
|
||||
hidden: false,
|
||||
matchAll: true,
|
||||
};
|
||||
|
||||
constructor(
|
||||
private router: Router,
|
||||
private route: ActivatedRoute,
|
||||
private libraryService: LibraryService
|
||||
) {}
|
||||
|
||||
async ngOnInit() {
|
||||
this.getDefaultViewMode();
|
||||
|
||||
this.pageSize = await this.getDefaultPageSize();
|
||||
await this.getEntries(true);
|
||||
// this.router.events.subscribe((event) => {
|
||||
// if (event instanceof NavigationEnd && event.url === '/library') {
|
||||
// this.pageSize = this.getDefaultPageSize();
|
||||
// this.getEntries(true);
|
||||
// }
|
||||
// });
|
||||
await this.populateYears();
|
||||
}
|
||||
|
||||
// ? uses libraryService to get library all entries
|
||||
getEntries(forceFetch: boolean = false) {
|
||||
this.libraryService.getAllEntries(forceFetch).subscribe({
|
||||
next: (entries) => {
|
||||
this.entries = entries.filter((e) => !e.watchlist);
|
||||
this.getQueryParameters();
|
||||
this.filterEntries();
|
||||
},
|
||||
error: (error) => {
|
||||
console.log(`Couldn't get entries.`, error);
|
||||
// TODO show notification
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
getDefaultViewMode() {
|
||||
return 'grid';
|
||||
}
|
||||
|
||||
setViewMode(mode: string) {
|
||||
this.viewMode = mode;
|
||||
}
|
||||
|
||||
// ! FILTER
|
||||
getQueryParameters() {
|
||||
this.route.queryParams.subscribe((params) => {
|
||||
this.currentPage = params['page'] ? parseInt(params['page'], 10) : 1;
|
||||
this.filterCriteria.title = params['title'] || '';
|
||||
this.filterCriteria.favorit = params.hasOwnProperty('favorit')
|
||||
? params['favorit']
|
||||
: false;
|
||||
this.filterCriteria.years = params['years']
|
||||
? params['years'].split(',')
|
||||
: [];
|
||||
this.filterCriteria.genres = params['genres']
|
||||
? params['genres'].split(',')
|
||||
: [];
|
||||
this.filterCriteria.type = params.hasOwnProperty('type')
|
||||
? params['type']
|
||||
: 0;
|
||||
this.filterCriteria.hidden = params.hasOwnProperty('hidden')
|
||||
? params['hidden']
|
||||
: false;
|
||||
this.filterCriteria.matchAll = params.hasOwnProperty('matchAll')
|
||||
? params['matchAll']
|
||||
: true;
|
||||
});
|
||||
}
|
||||
|
||||
setQueryParameters() {
|
||||
let params = { page: this.currentPage } as LibraryQueryParams;
|
||||
|
||||
// ? title
|
||||
if (this.filterCriteria.title?.trim() != '') {
|
||||
params.title = this.filterCriteria.title?.trim();
|
||||
}
|
||||
|
||||
// ? years
|
||||
if (this.filterCriteria.years?.length) {
|
||||
params.years = this.filterCriteria.years.join(',');
|
||||
}
|
||||
// ? genres
|
||||
if (this.filterCriteria.genres?.length) {
|
||||
params.genres = this.filterCriteria.genres.join(',');
|
||||
}
|
||||
|
||||
// ? type
|
||||
if (this.filterCriteria.type != 0) {
|
||||
params.type = this.filterCriteria.type;
|
||||
}
|
||||
|
||||
// ? favorit
|
||||
if (this.filterCriteria.favorit != false) {
|
||||
params.favorit = this.filterCriteria.favorit;
|
||||
}
|
||||
|
||||
// ? hidden
|
||||
if (this.filterCriteria.hidden != false) {
|
||||
params.hidden = this.filterCriteria.hidden;
|
||||
}
|
||||
|
||||
// ? matchAll
|
||||
if (this.filterCriteria.matchAll != true) {
|
||||
params.matchAll = this.filterCriteria.matchAll;
|
||||
}
|
||||
|
||||
this.router.navigate([], {
|
||||
relativeTo: this.route,
|
||||
queryParams: params,
|
||||
queryParamsHandling: '',
|
||||
});
|
||||
}
|
||||
|
||||
ngAfterViewInit() {
|
||||
$('#year-select')
|
||||
.select2({
|
||||
placeholder: 'Year(s)...',
|
||||
allowClear: true,
|
||||
closeOnSelect: false,
|
||||
})
|
||||
.on('change', (event: { target: any }) => {
|
||||
this.onYearsChange(event);
|
||||
});
|
||||
|
||||
$('#genre-select')
|
||||
.select2({
|
||||
placeholder: 'Genre(s)...',
|
||||
allowClear: true,
|
||||
closeOnSelect: false,
|
||||
})
|
||||
.on('change', (event: { target: any }) => {
|
||||
this.onGenresChange(event);
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
* This function will generate a bunch of years starting from 1950 to the current year
|
||||
*/
|
||||
populateYears() {
|
||||
const currentYear = new Date().getFullYear();
|
||||
for (let year = currentYear; year >= 1950; year--) {
|
||||
this.years.push(year);
|
||||
}
|
||||
}
|
||||
|
||||
initializeSelect2() {
|
||||
setTimeout(() => {
|
||||
// Ensure that the DOM is fully loaded
|
||||
$('.select2').select2({
|
||||
placeholder: 'Select an option',
|
||||
allowClear: true,
|
||||
});
|
||||
}, 500);
|
||||
}
|
||||
|
||||
clearFilter() {
|
||||
this.filterCriteria = {
|
||||
title: '',
|
||||
type: 0,
|
||||
years: [],
|
||||
genres: [],
|
||||
favorit: false,
|
||||
watchlist: false,
|
||||
hidden: false,
|
||||
matchAll: true,
|
||||
};
|
||||
|
||||
this.setQueryParameters();
|
||||
this.filterEntries();
|
||||
}
|
||||
|
||||
async filterEntries() {
|
||||
var temp = [] as Entry[];
|
||||
|
||||
const formattedTitle =
|
||||
this.filterCriteria.title?.toLowerCase().trim() || '';
|
||||
temp = await this.entries.filter((entry) => {
|
||||
// * Type check
|
||||
let filterType = this.filterCriteria.type - 1 == 0 ? false : true;
|
||||
if (this.filterCriteria.type != 0 && filterType != entry.is_tvseries) {
|
||||
return false; // Skip entry if the type does not match, unless type is 'all' (0)
|
||||
}
|
||||
|
||||
// * Hidden check
|
||||
// ? skip hidden if show hidden is not checked
|
||||
if (!this.filterCriteria.hidden && entry.hidden) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const year = new Date(entry.release_date).getFullYear();
|
||||
// ! Match all criteria logic
|
||||
if (this.filterCriteria.matchAll) {
|
||||
return (
|
||||
(formattedTitle
|
||||
? entry.title.toLowerCase().includes(formattedTitle) ||
|
||||
entry.original_title.toLowerCase().includes(formattedTitle)
|
||||
: true) &&
|
||||
(this.filterCriteria.genres?.length
|
||||
? this.filterCriteria.genres.every((genre) =>
|
||||
entry.genres.includes(genre)
|
||||
)
|
||||
: true) &&
|
||||
(this.filterCriteria.years?.length
|
||||
? this.filterCriteria.years.includes(year)
|
||||
: true) &&
|
||||
(this.filterCriteria.favorit ? entry.favorit : true) &&
|
||||
(this.filterCriteria.watchlist ? entry.watchlist : true) &&
|
||||
(this.filterCriteria.hidden ? entry.hidden : true)
|
||||
);
|
||||
}
|
||||
|
||||
// ! Match at least one criteria logic
|
||||
return (
|
||||
(formattedTitle
|
||||
? entry.title.toLowerCase().includes(formattedTitle) ||
|
||||
entry.original_title.toLowerCase().includes(formattedTitle)
|
||||
: true) ||
|
||||
(this.filterCriteria.genres?.length &&
|
||||
this.filterCriteria.genres.some((genre) =>
|
||||
entry.genres.includes(genre)
|
||||
)) ||
|
||||
(this.filterCriteria.years?.length &&
|
||||
this.filterCriteria.years.includes(year)) ||
|
||||
(this.filterCriteria.favorit && entry.favorit) ||
|
||||
(this.filterCriteria.watchlist && entry.watchlist)
|
||||
);
|
||||
});
|
||||
|
||||
// ? sort filter
|
||||
temp = await this.sortEntries(temp);
|
||||
|
||||
this.entriesCount = temp.length;
|
||||
this.maxPage = Math.ceil(this.entriesCount / this.pageSize);
|
||||
if (this.currentPage > this.maxPage) this.currentPage = this.maxPage; // fix for when 1 entry in the last page is removed
|
||||
|
||||
const startIndex = (this.currentPage - 1) * this.pageSize;
|
||||
const endIndex = startIndex + this.pageSize;
|
||||
|
||||
this.entriesToShow = temp.slice(startIndex, endIndex);
|
||||
}
|
||||
// + FILTER END
|
||||
|
||||
// ! sorting
|
||||
sortEntries(entries: Entry[]) {
|
||||
return entries.sort(
|
||||
(a, b) =>
|
||||
new Date(b.date_last_seen).getTime() -
|
||||
new Date(a.date_last_seen).getTime()
|
||||
);
|
||||
}
|
||||
|
||||
// ! calculated fields
|
||||
moviesCount() {
|
||||
return this.entries.filter((e) => !e.is_tvseries).length;
|
||||
}
|
||||
|
||||
seriesCount() {
|
||||
return this.entries.filter((e) => e.is_tvseries).length;
|
||||
}
|
||||
|
||||
favoritCount() {
|
||||
return this.entries.filter((e) => e.favorit).length;
|
||||
}
|
||||
hiddenCount() {
|
||||
return this.entries.filter((e) => e.hidden).length;
|
||||
}
|
||||
|
||||
// ! events catcher
|
||||
entryRemoved(event: Event) {
|
||||
this.entries = this.entries.filter(
|
||||
(e) => e.id.toString() != event.toString()
|
||||
);
|
||||
this.pageSize = this.getDefaultPageSize();
|
||||
this.filterEntries();
|
||||
this.setQueryParameters(); // * update url query
|
||||
}
|
||||
|
||||
// * filter events
|
||||
onYearsChange(event: any) {
|
||||
this.currentPage = 1;
|
||||
|
||||
const selected = $(event.target).val();
|
||||
this.filterCriteria.years = Array.isArray(selected)
|
||||
? selected.map(Number)
|
||||
: [Number(selected)];
|
||||
|
||||
this.filterEntries();
|
||||
this.setQueryParameters(); // * update url query
|
||||
}
|
||||
|
||||
onGenresChange(event: any) {
|
||||
this.currentPage = 1;
|
||||
|
||||
const selected = $(event.target).val();
|
||||
this.filterCriteria.genres = Array.isArray(selected) ? selected : [];
|
||||
|
||||
this.filterEntries();
|
||||
this.setQueryParameters(); // * update url query
|
||||
}
|
||||
|
||||
onFilterChange() {
|
||||
this.currentPage = 1;
|
||||
this.filterEntries();
|
||||
this.setQueryParameters(); // * update url query
|
||||
}
|
||||
|
||||
onEntryTypeChange(entryType: number) {
|
||||
this.filterCriteria.type = entryType;
|
||||
|
||||
this.currentPage = 1;
|
||||
this.filterEntries();
|
||||
this.setQueryParameters(); // * update url query
|
||||
}
|
||||
|
||||
setFavorit() {
|
||||
this.filterCriteria.favorit = !this.filterCriteria.favorit;
|
||||
|
||||
this.currentPage = 1;
|
||||
this.filterEntries();
|
||||
this.setQueryParameters(); // * update url query
|
||||
}
|
||||
|
||||
setHidden() {
|
||||
this.filterCriteria.hidden = !this.filterCriteria.hidden;
|
||||
|
||||
this.currentPage = 1;
|
||||
this.filterEntries();
|
||||
this.setQueryParameters(); // * update url query
|
||||
}
|
||||
// + end filter event
|
||||
|
||||
// ! pagination
|
||||
setPageSize(event: any) {
|
||||
this.pageSize = event.target.value;
|
||||
this.filterEntries();
|
||||
}
|
||||
|
||||
getDefaultPageSize() {
|
||||
// TODO get default page size from settings service
|
||||
return 54;
|
||||
}
|
||||
|
||||
goToPage(page: number) {
|
||||
this.currentPage = page;
|
||||
this.filterEntries();
|
||||
this.setQueryParameters(); // * update url query
|
||||
}
|
||||
|
||||
get pageNumbers(): number[] {
|
||||
let pagesToShow = 5; // Total pages to show at any time
|
||||
let startPage: number, endPage: number;
|
||||
|
||||
if (this.maxPage <= pagesToShow) {
|
||||
startPage = 1;
|
||||
endPage = this.maxPage;
|
||||
} else {
|
||||
startPage = Math.max(this.currentPage - Math.floor(pagesToShow / 2), 1);
|
||||
endPage = startPage + pagesToShow - 1;
|
||||
|
||||
if (endPage >= this.maxPage) {
|
||||
endPage = this.maxPage;
|
||||
startPage = endPage - pagesToShow + 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Generate the page numbers array.
|
||||
return Array.from(
|
||||
{ length: endPage - startPage + 1 },
|
||||
(_, i) => startPage + i
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user