Files
metube/ui/src/app/app.component.ts

159 lines
5.0 KiB
TypeScript
Raw Normal View History

2019-12-03 22:32:07 +02:00
import { Component, ViewChild, ElementRef, AfterViewInit } from '@angular/core';
import { faTrashAlt, faCheckCircle, faTimesCircle } from '@fortawesome/free-regular-svg-icons';
2022-01-04 22:04:53 +00:00
import { faRedoAlt, faSun, faMoon, faExternalLinkAlt } from '@fortawesome/free-solid-svg-icons';
2021-09-13 20:25:32 +03:00
import { CookieService } from 'ngx-cookie-service';
2019-11-29 19:31:34 +02:00
import { DownloadsService, Status } from './downloads.service';
2019-12-03 22:32:07 +02:00
import { MasterCheckboxComponent } from './master-checkbox.component';
import { Formats, Format, Quality } from './formats';
2019-11-29 19:31:34 +02:00
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.sass']
})
2019-12-03 22:32:07 +02:00
export class AppComponent implements AfterViewInit {
2019-11-29 19:31:34 +02:00
addUrl: string;
formats: Format[] = Formats;
2021-10-28 11:19:17 +01:00
qualities: Quality[];
quality: string;
2021-09-13 20:25:32 +03:00
format: string;
2019-11-29 19:31:34 +02:00
addInProgress = false;
2022-08-29 19:02:00 -04:00
showFolderDropdown = false;
2021-12-16 21:52:56 +00:00
darkMode: boolean;
2021-07-29 11:12:40 +03:00
@ViewChild('queueMasterCheckbox') queueMasterCheckbox: MasterCheckboxComponent;
@ViewChild('queueDelSelected') queueDelSelected: ElementRef;
@ViewChild('doneMasterCheckbox') doneMasterCheckbox: MasterCheckboxComponent;
@ViewChild('doneDelSelected') doneDelSelected: ElementRef;
@ViewChild('doneClearCompleted') doneClearCompleted: ElementRef;
@ViewChild('doneClearFailed') doneClearFailed: ElementRef;
2019-12-03 22:32:07 +02:00
2019-11-29 19:31:34 +02:00
faTrashAlt = faTrashAlt;
2019-12-03 22:32:07 +02:00
faCheckCircle = faCheckCircle;
faTimesCircle = faTimesCircle;
2021-07-29 11:12:40 +03:00
faRedoAlt = faRedoAlt;
2021-12-17 19:30:20 +02:00
faSun = faSun;
2022-01-04 22:04:53 +00:00
faMoon = faMoon;
faExternalLinkAlt = faExternalLinkAlt;
2019-11-29 19:31:34 +02:00
2021-09-13 20:25:32 +03:00
constructor(public downloads: DownloadsService, private cookieService: CookieService) {
this.format = cookieService.get('metube_format') || 'any';
2021-10-28 11:19:17 +01:00
// Needs to be set or qualities won't automatically be set
this.setQualities()
this.quality = cookieService.get('metube_quality') || 'best';
2021-12-16 21:52:56 +00:00
this.setupTheme(cookieService)
2019-12-03 22:32:07 +02:00
}
ngAfterViewInit() {
this.downloads.queueChanged.subscribe(() => {
this.queueMasterCheckbox.selectionChanged();
});
this.downloads.doneChanged.subscribe(() => {
this.doneMasterCheckbox.selectionChanged();
let completed: number = 0, failed: number = 0;
this.downloads.done.forEach(dl => {
if (dl.status === 'finished')
completed++;
else if (dl.status === 'error')
failed++;
});
this.doneClearCompleted.nativeElement.disabled = completed === 0;
this.doneClearFailed.nativeElement.disabled = failed === 0;
});
2019-11-29 19:31:34 +02:00
}
// workaround to allow fetching of Map values in the order they were inserted
// https://github.com/angular/angular/issues/31420
asIsOrder(a, b) {
return 1;
}
2021-09-13 20:25:32 +03:00
qualityChanged() {
this.cookieService.set('metube_quality', this.quality, { expires: 3650 });
2021-09-13 20:25:32 +03:00
}
2021-12-16 21:52:56 +00:00
setupTheme(cookieService) {
if (cookieService.check('metube_dark')) {
this.darkMode = cookieService.get('metube_dark') === "true"
} else {
this.darkMode = window.matchMedia("prefers-color-scheme: dark").matches
}
this.setTheme()
}
themeChanged() {
this.darkMode = !this.darkMode
this.cookieService.set('metube_dark', this.darkMode.toString(), { expires: 3650 });
this.setTheme()
}
setTheme() {
const doc = document.querySelector('html')
const filter = this.darkMode ? "invert(1) hue-rotate(180deg)" : ""
doc.style.filter = filter
}
2021-09-13 20:25:32 +03:00
formatChanged() {
this.cookieService.set('metube_format', this.format, { expires: 3650 });
2021-10-28 11:19:17 +01:00
// Updates to use qualities available
this.setQualities()
2021-09-13 20:25:32 +03:00
}
2019-12-03 22:32:07 +02:00
queueSelectionChanged(checked: number) {
this.queueDelSelected.nativeElement.disabled = checked == 0;
2019-11-29 19:31:34 +02:00
}
2019-12-03 22:32:07 +02:00
doneSelectionChanged(checked: number) {
this.doneDelSelected.nativeElement.disabled = checked == 0;
2019-11-29 19:31:34 +02:00
}
2021-10-28 11:19:17 +01:00
setQualities() {
// qualities for specific format
this.qualities = this.formats.find(el => el.id == this.format).qualities
const exists = this.qualities.find(el => el.id === this.quality)
this.quality = exists ? this.quality : 'best'
2021-10-28 11:19:17 +01:00
}
2022-08-29 19:02:00 -04:00
clickFolderDropdown() {
this.showFolderDropdown = !this.showFolderDropdown;
}
2021-09-13 20:25:32 +03:00
addDownload(url?: string, quality?: string, format?: string) {
2021-07-29 11:12:40 +03:00
url = url ?? this.addUrl
quality = quality ?? this.quality
format = format ?? this.format
2021-07-29 11:12:40 +03:00
2019-11-29 19:31:34 +02:00
this.addInProgress = true;
2021-09-13 20:25:32 +03:00
this.downloads.add(url, quality, format).subscribe((status: Status) => {
2019-11-29 19:31:34 +02:00
if (status.status === 'error') {
alert(`Error adding URL: ${status.msg}`);
} else {
this.addUrl = '';
}
this.addInProgress = false;
});
}
2022-01-04 22:05:33 +00:00
retryDownload(key: string, url: string, quality: string, format: string) {
this.addDownload(url, quality, format);
this.downloads.delById('done', [key]).subscribe();
2021-07-29 11:12:40 +03:00
}
2019-12-03 22:32:07 +02:00
delDownload(where: string, id: string) {
this.downloads.delById(where, [id]).subscribe();
}
delSelectedDownloads(where: string) {
this.downloads.delByFilter(where, dl => dl.checked).subscribe();
}
clearCompletedDownloads() {
this.downloads.delByFilter('done', dl => dl.status === 'finished').subscribe();
2019-11-29 19:31:34 +02:00
}
2019-12-03 22:32:07 +02:00
clearFailedDownloads() {
this.downloads.delByFilter('done', dl => dl.status === 'error').subscribe();
2019-11-29 19:31:34 +02:00
}
}