web-remote/src/app/components/slides/slide-list/slide-list.component.ts

71 lines
1.9 KiB
TypeScript
Raw Normal View History

2021-09-06 07:11:19 +00:00
import { Component, EventEmitter, OnDestroy, OnInit, Output } from '@angular/core';
import { Subscription } from 'rxjs';
import { Slide } from '../../../responses';
import { OpenLPService } from '../../../openlp.service';
import { HotKeysService } from '../../../hotkeys.service';
2021-09-06 07:11:19 +00:00
@Component({
selector: 'openlp-slide-list',
templateUrl: './slide-list.component.html',
2023-03-01 03:19:16 +00:00
styleUrls: ['./slide-list.component.scss', '../../no-items.scss'],
2021-09-06 07:11:19 +00:00
})
export class SlideListComponent implements OnInit, OnDestroy {
slides: Slide[] = null;
@Output() slideSelected = new EventEmitter<SlideListItem>();
_subscription: Subscription;
2023-03-01 03:19:16 +00:00
loading = false;
previousServiceItemId: string;
isServiceItemChanged: boolean;
2021-09-06 07:11:19 +00:00
constructor(private openlpService: OpenLPService, private hotKeysService: HotKeysService) {
this._subscription = openlpService.stateChanged$.subscribe(_ =>
this.fetchSlides()
);
2021-09-06 07:11:19 +00:00
}
ngOnInit() {
this.fetchSlides();
}
ngOnDestroy() {
this._subscription.unsubscribe();
}
onSlideSelected(slide: Slide, index: number) {
this.slideSelected.emit({slide, index});
}
fetchSlides() {
2023-03-01 03:19:16 +00:00
this.loading = true;
this.openlpService.getServiceItem().subscribe({
next: (serviceItem) => {
this.loading = false;
if (serviceItem instanceof Array) {
this.slides = serviceItem;
}
else {
this.slides = serviceItem.slides;
if (this.previousServiceItemId !== serviceItem.id) {
this.isServiceItemChanged = true;
this.previousServiceItemId = serviceItem.id;
}
}
},
complete: () => {
if (this.isServiceItemChanged) {
setTimeout(() => this.hotKeysService.scrollToCurrentItem('slide', window.scrollY === 0 ? 'center' : 'end'), 25);
this.isServiceItemChanged = false;
}
2021-09-06 07:11:19 +00:00
}
});
}
}
export interface SlideListItem {
slide: Slide;
index: number;
}