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

108 lines
3.3 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';
2021-07-29 11:12:40 +03:00
import { faRedoAlt } from '@fortawesome/free-solid-svg-icons';
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';
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;
2019-12-13 22:43:58 +02:00
qualities: Array<Object> = [
{id: "best", text: "Best"},
2021-01-12 20:12:21 +02:00
{id: "1440p", text: "1440p"},
2019-12-13 22:43:58 +02:00
{id: "1080p", text: "1080p"},
{id: "720p", text: "720p"},
2021-03-04 11:10:39 +02:00
{id: "480p", text: "480p"},
{id: "audio", text: "Audio only"}
2019-12-13 22:43:58 +02:00
];
quality: string = "best";
2019-11-29 19:31:34 +02:00
addInProgress = false;
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;
2019-11-29 19:31:34 +02:00
2019-11-29 22:44:49 +02:00
constructor(public downloads: DownloadsService) {
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;
}
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-07-29 11:12:40 +03:00
addDownload(url?: string, quality?: string) {
url = url ?? this.addUrl
quality = quality ?? this.quality
2019-11-29 19:31:34 +02:00
this.addInProgress = true;
2021-07-29 11:12:40 +03:00
this.downloads.add(url, quality).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;
});
}
2021-07-29 11:12:40 +03:00
retryDownload(key: string, quality:string){
this.addDownload(key, quality);
}
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
}
}