Skip to content

Commit 4847fc6

Browse files
authored
Merge pull request DSpace#2156 from alexandrevryghem/fix-enabling-video-mediaviewer-without-image-mediaviewer-main
Made it possible to enable video viewer without also enabling image viewer
2 parents cefe1bf + 93ac195 commit 4847fc6

11 files changed

Lines changed: 121 additions & 110 deletions

src/app/item-page/media-viewer/media-viewer-image/media-viewer-image.component.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, Input, OnInit } from '@angular/core';
1+
import { Component, Input, OnChanges, OnInit } from '@angular/core';
22
import { NgxGalleryImage, NgxGalleryOptions } from '@kolkov/ngx-gallery';
33
import { MediaViewerItem } from '../../../core/shared/media-viewer-item.model';
44
import { NgxGalleryAnimation } from '@kolkov/ngx-gallery';
@@ -13,15 +13,16 @@ import { AuthService } from '../../../core/auth/auth.service';
1313
templateUrl: './media-viewer-image.component.html',
1414
styleUrls: ['./media-viewer-image.component.scss'],
1515
})
16-
export class MediaViewerImageComponent implements OnInit {
16+
export class MediaViewerImageComponent implements OnChanges, OnInit {
1717
@Input() images: MediaViewerItem[];
1818
@Input() preview?: boolean;
1919
@Input() image?: string;
2020

2121
thumbnailPlaceholder = './assets/images/replacement_image.svg';
2222

23-
galleryOptions: NgxGalleryOptions[];
24-
galleryImages: NgxGalleryImage[];
23+
galleryOptions: NgxGalleryOptions[] = [];
24+
25+
galleryImages: NgxGalleryImage[] = [];
2526

2627
/**
2728
* Whether or not the current user is authenticated
@@ -33,11 +34,7 @@ export class MediaViewerImageComponent implements OnInit {
3334
) {
3435
}
3536

36-
/**
37-
* Thi method sets up the gallery settings and data
38-
*/
39-
ngOnInit(): void {
40-
this.isAuthenticated$ = this.authService.isAuthenticated();
37+
ngOnChanges(): void {
4138
this.galleryOptions = [
4239
{
4340
preview: this.preview !== undefined ? this.preview : true,
@@ -53,7 +50,6 @@ export class MediaViewerImageComponent implements OnInit {
5350
previewFullscreen: true,
5451
},
5552
];
56-
5753
if (this.image) {
5854
this.galleryImages = [
5955
{
@@ -67,6 +63,11 @@ export class MediaViewerImageComponent implements OnInit {
6763
}
6864
}
6965

66+
ngOnInit(): void {
67+
this.isAuthenticated$ = this.authService.isAuthenticated();
68+
this.ngOnChanges();
69+
}
70+
7071
/**
7172
* This method convert an array of MediaViewerItem into NgxGalleryImage array
7273
* @param medias input NgxGalleryImage array

src/app/item-page/media-viewer/media-viewer-video/media-viewer-video.component.html

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
<video
22
crossorigin="anonymous"
3-
#media
4-
[src]="filteredMedias[currentIndex].bitstream._links.content.href"
3+
[src]="medias[currentIndex].bitstream._links.content.href"
54
id="singleVideo"
65
[poster]="
7-
filteredMedias[currentIndex].thumbnail ||
8-
replacements[filteredMedias[currentIndex].format]
6+
medias[currentIndex].thumbnail ||
7+
replacements[medias[currentIndex].format]
98
"
109
preload="none"
1110
controls
1211
>
13-
<ng-container *ngIf="getMediaCap(filteredMedias[currentIndex].bitstream.name) as capInfos">
12+
<ng-container *ngIf="getMediaCap(medias[currentIndex].bitstream.name, captions) as capInfos">
1413
<ng-container *ngFor="let capInfo of capInfos">
1514
<track [src]="capInfo.src" [label]="capInfo.langLabel" [srclang]="capInfo.srclang" />
1615
</ng-container>
1716
</ng-container>
1817

1918
</video>
20-
<div class="buttons" *ngIf="filteredMedias?.length > 1">
19+
<div class="buttons" *ngIf="medias?.length > 1">
2120
<button
2221
class="btn btn-primary previous"
2322
[disabled]="currentIndex === 0"
@@ -28,7 +27,7 @@
2827

2928
<button
3029
class="btn btn-primary next"
31-
[disabled]="currentIndex === filteredMedias.length - 1"
30+
[disabled]="currentIndex === medias.length - 1"
3231
(click)="nextMedia()"
3332
>
3433
{{ "media-viewer.next" | translate }}
@@ -44,7 +43,7 @@
4443
<div ngbDropdownMenu aria-labelledby="dropdownBasic1">
4544
<button
4645
ngbDropdownItem
47-
*ngFor="let item of filteredMedias; index as indexOfelement"
46+
*ngFor="let item of medias; index as indexOfelement"
4847
class="list-element"
4948
(click)="selectedMedia(indexOfelement)"
5049
>

src/app/item-page/media-viewer/media-viewer-video/media-viewer-video.component.spec.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ describe('MediaViewerVideoComponent', () => {
8383
fixture = TestBed.createComponent(MediaViewerVideoComponent);
8484
component = fixture.componentInstance;
8585
component.medias = mockMediaViewerItem;
86-
component.filteredMedias = mockMediaViewerItem;
8786
fixture.detectChanges();
8887
});
8988

@@ -94,7 +93,6 @@ describe('MediaViewerVideoComponent', () => {
9493
describe('should show controller buttons when the having mode then one video', () => {
9594
beforeEach(() => {
9695
component.medias = mockMediaViewerItems;
97-
component.filteredMedias = mockMediaViewerItems;
9896
fixture.detectChanges();
9997
});
10098

src/app/item-page/media-viewer/media-viewer-video/media-viewer-video.component.ts

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { Component, Input, OnInit } from '@angular/core';
1+
import { Component, Input } from '@angular/core';
22
import { MediaViewerItem } from '../../../core/shared/media-viewer-item.model';
33
import { languageHelper } from './language-helper';
4-
import { CaptionInfo} from './caption-info';
4+
import { CaptionInfo } from './caption-info';
5+
import { Bitstream } from 'src/app/core/shared/bitstream.model';
56

67
/**
78
* This component renders a video viewer and playlist for the media viewer
@@ -11,24 +12,20 @@ import { CaptionInfo} from './caption-info';
1112
templateUrl: './media-viewer-video.component.html',
1213
styleUrls: ['./media-viewer-video.component.scss'],
1314
})
14-
export class MediaViewerVideoComponent implements OnInit {
15+
export class MediaViewerVideoComponent {
1516
@Input() medias: MediaViewerItem[];
1617

17-
filteredMedias: MediaViewerItem[];
18+
@Input() captions: Bitstream[] = [];
19+
20+
isCollapsed = false;
1821

19-
isCollapsed: boolean;
2022
currentIndex = 0;
2123

2224
replacements = {
2325
video: './assets/images/replacement_video.svg',
2426
audio: './assets/images/replacement_audio.svg',
2527
};
2628

27-
ngOnInit() {
28-
this.isCollapsed = false;
29-
this.filteredMedias = this.medias.filter((media) => media.format === 'audio' || media.format === 'video');
30-
}
31-
3229
/**
3330
* This method check if there is caption file for the media
3431
* The caption file name is the media name plus "-" following two letter
@@ -39,44 +36,39 @@ export class MediaViewerVideoComponent implements OnInit {
3936
* Two letter language code reference
4037
* https://www.w3schools.com/tags/ref_language_codes.asp
4138
*/
42-
getMediaCap(name: string): CaptionInfo[] {
43-
let filteredCapMedias: MediaViewerItem[];
44-
let capInfos: CaptionInfo[] = [];
45-
filteredCapMedias = this.medias
46-
.filter((media) => media.mimetype === 'text/vtt')
47-
.filter((media) => media.bitstream.name.substring(0, (media.bitstream.name.length - 7) ).toLowerCase() === name.toLowerCase());
39+
getMediaCap(name: string, captions: Bitstream[]): CaptionInfo[] {
40+
const capInfos: CaptionInfo[] = [];
41+
const filteredCapMedias: Bitstream[] = captions
42+
.filter((media: Bitstream) => media.name.substring(0, (media.name.length - 7)).toLowerCase() === name.toLowerCase());
4843

49-
if (filteredCapMedias) {
50-
filteredCapMedias
51-
.forEach((media, index) => {
52-
let srclang: string = media.bitstream.name.slice(-6, -4).toLowerCase();
53-
capInfos.push(new CaptionInfo(
54-
media.bitstream._links.content.href,
55-
srclang,
56-
languageHelper[srclang]
57-
));
58-
});
44+
for (const media of filteredCapMedias) {
45+
let srclang: string = media.name.slice(-6, -4).toLowerCase();
46+
capInfos.push(new CaptionInfo(
47+
media._links.content.href,
48+
srclang,
49+
languageHelper[srclang],
50+
));
5951
}
6052
return capInfos;
6153
}
6254

6355
/**
64-
* This method sets the reviced index into currentIndex
56+
* This method sets the received index into currentIndex
6557
* @param index Selected index
6658
*/
6759
selectedMedia(index: number) {
6860
this.currentIndex = index;
6961
}
7062

7163
/**
72-
* This method increade the number of the currentIndex
64+
* This method increases the number of the currentIndex
7365
*/
7466
nextMedia() {
7567
this.currentIndex++;
7668
}
7769

7870
/**
79-
* This method decrese the number of the currentIndex
71+
* This method decreases the number of the currentIndex
8072
*/
8173
prevMedia() {
8274
this.currentIndex--;

src/app/item-page/media-viewer/media-viewer-video/themed-media-viewer-video.component.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Component, Input } from '@angular/core';
22
import { ThemedComponent } from '../../../shared/theme-support/themed.component';
33
import { MediaViewerItem } from '../../../core/shared/media-viewer-item.model';
44
import { MediaViewerVideoComponent } from './media-viewer-video.component';
5+
import { Bitstream } from '../../../core/shared/bitstream.model';
56

67
/**
78
* Themed wrapper for {@link MediaViewerVideoComponent}.
@@ -15,8 +16,11 @@ export class ThemedMediaViewerVideoComponent extends ThemedComponent<MediaViewer
1516

1617
@Input() medias: MediaViewerItem[];
1718

19+
@Input() captions: Bitstream[];
20+
1821
protected inAndOutputNames: (keyof MediaViewerVideoComponent & keyof this)[] = [
1922
'medias',
23+
'captions',
2024
];
2125

2226
protected getComponentName(): string {

src/app/item-page/media-viewer/media-viewer.component.html

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,23 @@
55
[showMessage]="false"
66
></ds-themed-loading>
77
<div class="media-viewer" *ngIf="!isLoading">
8-
<ng-container *ngIf="mediaList.length > 0">
9-
<ng-container *ngIf="videoOptions">
10-
<ng-container
11-
*ngIf="
12-
mediaList[0]?.format === 'video' || mediaList[0]?.format === 'audio'
13-
"
14-
>
15-
<ds-themed-media-viewer-video [medias]="mediaList"></ds-themed-media-viewer-video>
8+
<ng-container *ngIf="mediaList.length > 0; else showThumbnail">
9+
<ng-container *ngVar="mediaOptions.video && ['audio', 'video'].includes(mediaList[0]?.format) as showVideo">
10+
<ng-container *ngVar="mediaOptions.image && mediaList[0]?.format === 'image' as showImage">
11+
<ds-themed-media-viewer-video *ngIf="showVideo" [medias]="mediaList" [captions]="captions$ | async"></ds-themed-media-viewer-video>
12+
<ds-themed-media-viewer-image *ngIf="showImage" [images]="mediaList"></ds-themed-media-viewer-image>
13+
<ng-container *ngIf="showImage || showVideo; else showThumbnail"></ng-container>
1614
</ng-container>
1715
</ng-container>
18-
<ng-container *ngIf="mediaList[0]?.format === 'image'">
19-
<ds-themed-media-viewer-image [images]="mediaList"></ds-themed-media-viewer-image>
20-
</ng-container>
21-
</ng-container>
22-
<ng-container
23-
*ngIf="
24-
((mediaList[0]?.format !== 'image') &&
25-
(!videoOptions || mediaList[0]?.format !== 'video') &&
26-
(!videoOptions || mediaList[0]?.format !== 'audio')) ||
27-
mediaList.length === 0
28-
"
29-
>
30-
<ds-themed-media-viewer-image
31-
[image]="mediaList[0]?.thumbnail || thumbnailPlaceholder"
32-
[preview]="false"
33-
></ds-themed-media-viewer-image>
3416
</ng-container>
3517
</div>
18+
<ng-template #showThumbnail>
19+
<ds-themed-media-viewer-image *ngIf="mediaOptions.image && mediaOptions.video"
20+
[image]="(thumbnailsRD$ | async)?.payload?.page[0]?._links.content.href || thumbnailPlaceholder"
21+
[preview]="false"
22+
></ds-themed-media-viewer-image>
23+
<ds-thumbnail *ngIf="!(mediaOptions.image && mediaOptions.video)"
24+
[thumbnail]="(thumbnailsRD$ | async)?.payload?.page[0]">
25+
</ds-thumbnail>
26+
</ng-template>
3627
</ng-container>

src/app/item-page/media-viewer/media-viewer.component.spec.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ describe('MediaViewerComponent', () => {
6161
);
6262

6363
beforeEach(waitForAsync(() => {
64-
TestBed.configureTestingModule({
64+
return TestBed.configureTestingModule({
6565
imports: [
6666
TranslateModule.forRoot({
6767
loader: {
@@ -94,7 +94,10 @@ describe('MediaViewerComponent', () => {
9494
describe('when the bitstreams are loading', () => {
9595
beforeEach(() => {
9696
comp.mediaList$.next([mockMediaViewerItem]);
97-
comp.videoOptions = true;
97+
comp.mediaOptions = {
98+
image: true,
99+
video: true,
100+
};
98101
comp.isLoading = true;
99102
fixture.detectChanges();
100103
});
@@ -118,7 +121,10 @@ describe('MediaViewerComponent', () => {
118121
describe('when the bitstreams loading is failed', () => {
119122
beforeEach(() => {
120123
comp.mediaList$.next([]);
121-
comp.videoOptions = true;
124+
comp.mediaOptions = {
125+
image: true,
126+
video: true,
127+
};
122128
comp.isLoading = false;
123129
fixture.detectChanges();
124130
});

0 commit comments

Comments
 (0)