From 80cce049ff9dd330d8741cecb20baf18fff2ac4f Mon Sep 17 00:00:00 2001 From: Daniel Macak Date: Thu, 10 Aug 2023 22:24:21 +0200 Subject: [PATCH] change!: turn item width into visible item count This makes it declarative unlike before when the item count had to be calculated from the item and viewer width, which not only saves code but also resources. One observation of mine is that this relatively simple change required quite a few changes in the unit tests. It only shows that testing components in Angular is pain (calling ngOnChanges, calling tick in fakeAsync etc.) which makes a good argument for cy's component testing. --- apps/demo/src/app/app.component.html | 4 +- apps/demo/src/app/app.component.ts | 5 -- .../demo-multiple-items.component.html | 12 +--- .../demo-multiple-items.component.ts | 11 +--- .../demo-whole-config.component.html | 17 ++---- .../demo-whole-config.component.ts | 14 +---- .../components/gallery/gallery.component.html | 2 +- .../components/gallery/gallery.component.ts | 2 +- .../components/viewer/viewer.component.html | 4 +- .../viewer/viewer.component.simple.spec.ts | 27 ++------ .../viewer/viewer.component.spec.ts | 46 +++++++++++--- .../lib/components/viewer/viewer.component.ts | 61 ++++++++----------- 12 files changed, 82 insertions(+), 123 deletions(-) diff --git a/apps/demo/src/app/app.component.html b/apps/demo/src/app/app.component.html index bbc1cd4b..e043e638 100644 --- a/apps/demo/src/app/app.component.html +++ b/apps/demo/src/app/app.component.html @@ -67,10 +67,10 @@

Demos

stackblitz="https://stackblitz.com/edit/ngx-gallery-demo-infinite-loop?file=src%2Fapp%2Fapp.component.html" > diff --git a/apps/demo/src/app/app.component.ts b/apps/demo/src/app/app.component.ts index 8a7bfd8c..6dc41777 100644 --- a/apps/demo/src/app/app.component.ts +++ b/apps/demo/src/app/app.component.ts @@ -67,11 +67,6 @@ export class AppComponent { }, ]; - smallerImages = this.images.map(i => ({ - ...i, - src: i.src.replace('-lg', '-md'), - })); - extendedImages: GalleryItem[] = [ ...this.images, { diff --git a/apps/demo/src/app/components/demo-multiple-items/demo-multiple-items.component.html b/apps/demo/src/app/components/demo-multiple-items/demo-multiple-items.component.html index 22ce95c5..21c24648 100644 --- a/apps/demo/src/app/components/demo-multiple-items/demo-multiple-items.component.html +++ b/apps/demo/src/app/components/demo-multiple-items/demo-multiple-items.component.html @@ -1,16 +1,6 @@ - - Item width - - - diff --git a/apps/demo/src/app/components/demo-multiple-items/demo-multiple-items.component.ts b/apps/demo/src/app/components/demo-multiple-items/demo-multiple-items.component.ts index f9ad5d6d..39c7ea08 100644 --- a/apps/demo/src/app/components/demo-multiple-items/demo-multiple-items.component.ts +++ b/apps/demo/src/app/components/demo-multiple-items/demo-multiple-items.component.ts @@ -3,8 +3,6 @@ import { ChangeDetectorRef, Component, Input, - OnChanges, - SimpleChanges, } from '@angular/core'; import { GalleryItem } from '@daelmaak/ngx-gallery'; @@ -14,21 +12,14 @@ import { GalleryItem } from '@daelmaak/ngx-gallery'; styleUrls: ['./demo-multiple-items.component.scss'], changeDetection: ChangeDetectionStrategy.OnPush, }) -export class DemoMultipleItemsComponent implements OnChanges { +export class DemoMultipleItemsComponent { @Input() items: GalleryItem[]; @Input() mobile: boolean; - itemWidth: string; showGallery = true; constructor(private cd: ChangeDetectorRef) {} - ngOnChanges({ mobile }: SimpleChanges) { - if (mobile && mobile.firstChange) { - this.itemWidth = this.mobile ? 'calc(100% / 1.5)' : 'calc(100% / 2.5)'; - } - } - reloadGallery() { this.showGallery = false; this.cd.detectChanges(); diff --git a/apps/demo/src/app/components/demo-whole-config/demo-whole-config.component.html b/apps/demo/src/app/components/demo-whole-config/demo-whole-config.component.html index 4cd1f1ab..19aae67e 100644 --- a/apps/demo/src/app/components/demo-whole-config/demo-whole-config.component.html +++ b/apps/demo/src/app/components/demo-whole-config/demo-whole-config.component.html @@ -94,11 +94,11 @@

Gallery viewer properties

- + @@ -203,7 +203,7 @@

Auxiliary

[touchGestures]="galleryConfig.touchGestures" [counter]="galleryConfig.counter" [counterOrientation]="galleryConfig.counterOrientation" - [itemWidth]="galleryConfig.itemWidth" + [visibleItems]="galleryConfig.visibleItems" [objectFit]="galleryConfig.objectFit" [loading]="galleryConfig.loading" [loop]="galleryConfig.loop" @@ -215,7 +215,6 @@

Auxiliary

[thumbsArrows]="galleryConfig.thumbsArrows" [thumbsArrowSlideByLength]="galleryConfig.thumbsArrowSlideByLength" [thumbsScrollBehavior]="galleryConfig.thumbsScrollBehavior" - (imageClick)="onImageClick($event)" (descriptionClick)="galleryConfig.descriptions = false" > @@ -234,12 +233,7 @@

Auxiliary

- +
Custom item loading ...
@@ -250,7 +244,6 @@

Auxiliary

class="custom-media" [src]="seen ? item.src : ''" controls - (loadedmetadata)="onImageLoad()" >
diff --git a/apps/demo/src/app/components/demo-whole-config/demo-whole-config.component.ts b/apps/demo/src/app/components/demo-whole-config/demo-whole-config.component.ts index 152d3284..2e960bdd 100644 --- a/apps/demo/src/app/components/demo-whole-config/demo-whole-config.component.ts +++ b/apps/demo/src/app/components/demo-whole-config/demo-whole-config.component.ts @@ -11,7 +11,6 @@ import { delay, switchMap } from 'rxjs/operators'; import { GalleryComponent, GalleryItem, - GalleryItemEvent, Loading, ObjectFit, Orientation, @@ -26,7 +25,7 @@ interface GalleryConfig { touchGestures: boolean; counter: boolean; counterOrientation: VerticalOrientation; - itemWidth: string; + visibleItems: number; objectFit: ObjectFit; loading: Loading; loop: boolean; @@ -62,7 +61,7 @@ export class DemoWholeConfigComponent implements OnInit { touchGestures: true, counter: true, counterOrientation: 'bottom', - itemWidth: '', + visibleItems: 1, objectFit: 'cover', loading: 'lazy', loop: false, @@ -92,14 +91,7 @@ export class DemoWholeConfigComponent implements OnInit { window.addEventListener('pagehide', this.storeGalleryConfig); } - async onImageClick(event: GalleryItemEvent) {} - - onImageLoad() {} - - onConfigChange( - prop: keyof GalleryConfig, - value: GalleryConfig[keyof GalleryConfig] - ) { + onConfigChange(prop: keyof GalleryConfig, value: unknown) { if (value === '') { return; } diff --git a/libs/gallery/src/lib/components/gallery/gallery.component.html b/libs/gallery/src/lib/components/gallery/gallery.component.html index 722e2a11..e41064e0 100644 --- a/libs/gallery/src/lib/components/gallery/gallery.component.html +++ b/libs/gallery/src/lib/components/gallery/gallery.component.html @@ -30,7 +30,7 @@ [touchGestures]="touchGestures" [counter]="counter" [counterOrientation]="counterOrientation" - [itemWidth]="itemWidth" + [visibleItems]="visibleItems" [objectFit]="objectFit" [loading]="loading" [itemTemplate]="itemTemplate" diff --git a/libs/gallery/src/lib/components/gallery/gallery.component.ts b/libs/gallery/src/lib/components/gallery/gallery.component.ts index 623aa032..aa3262a6 100644 --- a/libs/gallery/src/lib/components/gallery/gallery.component.ts +++ b/libs/gallery/src/lib/components/gallery/gallery.component.ts @@ -48,7 +48,7 @@ export class GalleryComponent { @Input() touchGestures = true; @Input() counter = true; @Input() counterOrientation: VerticalOrientation = 'bottom'; - @Input() itemWidth: string; + @Input() visibleItems = 1; @Input() loading: Loading = 'lazy'; @Input() loop = false; @Input() objectFit: ObjectFit = 'cover'; diff --git a/libs/gallery/src/lib/components/viewer/viewer.component.html b/libs/gallery/src/lib/components/viewer/viewer.component.html index 309d8f9d..193e81b0 100644 --- a/libs/gallery/src/lib/components/viewer/viewer.component.html +++ b/libs/gallery/src/lib/components/viewer/viewer.component.html @@ -8,10 +8,10 @@