Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
MurhafSousli committed Nov 1, 2024
1 parent 7cc727f commit 58f48b1
Show file tree
Hide file tree
Showing 40 changed files with 396 additions and 461 deletions.
35 changes: 20 additions & 15 deletions projects/ng-gallery-demo/src/app/pages/lab/lab.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ <h1 fxLayout>
<gallery #gallery id="lab"
[items]="photos$ | async"
[centralized]="config.centralized"
[autoHeight]="config.autoHeight"
[itemAutosize]="config.itemAutosize"
[loadingAttr]="config.loadingAttr"
[resizeDebounceTime]="config.resizeDebounceTime"
Expand Down Expand Up @@ -47,17 +46,18 @@ <h1 fxLayout>
<gallery-counter [align]="counterConfig.align"/>
}

<gallery-thumbs [autosize]="thumbConfig.autosize"
[imageSize]="thumbConfig.imageSize"
[centralized]="thumbConfig.centralized"
[thumbWidth]="thumbConfig.thumbWidth"
[thumbHeight]="thumbConfig.thumbHeight"
[disabled]="thumbConfig.disabled"
[disableScroll]="thumbConfig.disableScroll"
[disableMouseScroll]="thumbConfig.disableMouseScroll"
[position]="thumbConfig.position"
[detach]="thumbConfig.detach"/>

@if (thumbConfig.thumbs) {
<gallery-thumbs [autosize]="thumbConfig.autosize"
[imageSize]="thumbConfig.imageSize"
[centralized]="thumbConfig.centralized"
[thumbWidth]="thumbConfig.thumbWidth"
[thumbHeight]="thumbConfig.thumbHeight"
[disabled]="thumbConfig.disabled"
[disableScroll]="thumbConfig.disableScroll"
[disableMouseScroll]="thumbConfig.disableMouseScroll"
[position]="thumbConfig.position"
[detach]="thumbConfig.detach"/>
}
</gallery>

<div class="buttons-container">
Expand Down Expand Up @@ -244,7 +244,8 @@ <h1 fxLayout>
<mat-checkbox [(ngModel)]="config.centralized">Centralize Slider</mat-checkbox>
</div>
<div>
<mat-checkbox [(ngModel)]="thumbConfig.centralized">Centralize Thumbnails</mat-checkbox>
<mat-checkbox [(ngModel)]="thumbConfig.centralized" [disabled]="!thumbConfig.thumbs">Centralize Thumbnails
</mat-checkbox>
</div>
<div>
<mat-checkbox [(ngModel)]="thumbConfig.detach" [disabled]="!thumbConfig.thumbs">Detach thumbnails
Expand All @@ -263,15 +264,19 @@ <h1 fxLayout>
</div>

<div>
<mat-checkbox [(ngModel)]="thumbConfig.disableScroll">Disable Thumb Scroll</mat-checkbox>
<mat-checkbox [(ngModel)]="thumbConfig.disableScroll" [disabled]="!thumbConfig.thumbs">Disable Thumb
Scroll
</mat-checkbox>
</div>

<div>
<mat-checkbox [(ngModel)]="config.disableMouseScroll">Disable Mouse Scroll</mat-checkbox>
</div>

<div>
<mat-checkbox [(ngModel)]="thumbConfig.disableMouseScroll">Disable Thumb Mouse Scroll</mat-checkbox>
<mat-checkbox [(ngModel)]="thumbConfig.disableMouseScroll" [disabled]="!thumbConfig.thumbs">Disable Thumb
Mouse Scroll
</mat-checkbox>
</div>
</div>
</div>
Expand Down
7 changes: 4 additions & 3 deletions projects/ng-gallery-demo/src/app/pages/lab/lab.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
GalleryBulletsComponent,
GalleryCounterComponent,
GalleryItemDef,
ImgRecognizer
ImgRecognizer, AutoHeight
} from 'ng-gallery';
import { MatInputModule } from '@angular/material/input';
import { MatCheckboxModule } from '@angular/material/checkbox';
Expand All @@ -39,6 +39,7 @@ import { FooterComponent } from '../../shared/footer/footer.component';
changeDetection: ChangeDetectionStrategy.OnPush,
standalone: true,
imports: [
AutoHeight,
FlexLayoutModule,
MatIconModule,
NgIf,
Expand Down Expand Up @@ -113,7 +114,7 @@ export class LabComponent implements OnInit {
indexChange$ = new BehaviorSubject<any>({ active: false });

constructor(pixabay: Pixabay, private _title: Title) {
this.photos$ = pixabay.getHDImages('jet fighter');
this.photos$ = pixabay.getHDImages('tropical');
}

ngOnInit() {
Expand All @@ -130,7 +131,7 @@ export class LabComponent implements OnInit {
autoplayInterval: 3000,
loadingStrategy: LoadingStrategy.Preload,
orientation: Orientation.Horizontal,
autoHeight: false,
autoHeight: true,
itemAutosize: false,
scrollBehavior: 'smooth',
loadingAttr: 'lazy',
Expand Down
100 changes: 100 additions & 0 deletions projects/ng-gallery/src/lib/auto-height/auto-height.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import {
Directive,
effect,
inject,
signal,
untracked,
WritableSignal,
EffectCleanupRegisterFn
} from '@angular/core';
import {
Observable,
Subscription,
of,
filter,
fromEvent,
switchMap,
tap,
take,
debounceTime,
animationFrameScheduler
} from 'rxjs';
import { ImgManager } from '../utils/img-manager';
import { GalleryRef } from '../services/gallery-ref';
import { GalleryComponent } from '../components/gallery.component';
import { ResizeSensor } from '../services/resize-sensor';

/**
* Auto height feature prerequisites:
* - autosize should be set to 'false'
* - if thumbnails exist, it should not be aligned to the right or left
*/

@Directive({
standalone: true,
selector: 'gallery[autoHeight]',
host: {
'[attr.autoHeight]': 'true',
'[class.g-resizing]': 'isResizing()',
'[style.--slider-auto-height.px]': 'height()',
}
})
export class AutoHeight {

private readonly gallery: GalleryComponent = inject(GalleryComponent);

private readonly manager: ImgManager = inject(ImgManager);

readonly isResizing: WritableSignal<boolean> = signal(false);

readonly height: WritableSignal<number> = signal(0);

constructor() {
let sub$: Subscription;

let afterHeightChanged$: Observable<any>;

effect((onCleanup: EffectCleanupRegisterFn) => {
const resizeSensor: ResizeSensor = this.gallery.slider().resizeSensor();
// Check if height has transition for the auto-height feature
const transitionDuration: string = getComputedStyle(resizeSensor.nativeElement).transitionDuration;
if (!parseFloat(transitionDuration)) {
afterHeightChanged$ = of({});
} else {
console.log(transitionDuration)
afterHeightChanged$ = fromEvent(resizeSensor.nativeElement, 'transitionend');
}
// if (!this.galleryRef.config().autoHeight) return;
// const currIndex = this.galleryRef.currIndex();
untracked(() => {
sub$ = this.manager.getActiveItem().pipe(
// Wait for item image to be rendered
debounceTime(0, animationFrameScheduler),
// Skip if img height is equal the slider height
filter((img: HTMLImageElement) => {
console.log('🦕', resizeSensor.nativeElement.clientHeight, img.height)
return img.height !== resizeSensor.nativeElement.clientHeight
}),
switchMap((img: HTMLImageElement) => {
console.log('👽 Resize started! --slider-height', resizeSensor.nativeElement.clientHeight, img.height)
resizeSensor.disabled.set(true);
this.isResizing.set(true);

resizeSensor.nativeElement.style.setProperty('--slider-height', `${img.height}px`)

return afterHeightChanged$.pipe(
debounceTime(0, animationFrameScheduler),
tap(() => {
resizeSensor.disabled.set(false);
this.isResizing.set(false);
}),
take(1)
);
})
).subscribe();

onCleanup(() => sub$?.unsubscribe());
});
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
GalleryItemData,
ItemState,
VideoItemData,
} from './templates/items.model';
} from '../templates/items.model';
import { SliderItem } from './items/items';

@Component({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, inject, output, OutputEmitterRef, ChangeDetectionStrategy } from '@angular/core';
import { Component, inject, output, OutputEmitterRef, ChangeDetectionStrategy, viewChild } from '@angular/core';
import { GalleryError } from '../models/gallery.model';
import { GalleryRef } from '../services/gallery-ref';
import { SmoothScroll } from '../smooth-scroll';
Expand All @@ -8,7 +8,6 @@ import { GalleryItemComponent } from './gallery-item.component';
import { ScrollSnapType } from '../services/scroll-snap-type';
import { ResizeSensor } from '../services/resize-sensor';
import { SliderComponent } from './slider/slider';
import { AutoHeight } from '../observers/auto-height';

@Component({
standalone: true,
Expand All @@ -17,7 +16,6 @@ import { AutoHeight } from '../observers/auto-height';
<g-slider [orientation]="galleryRef.config().orientation"
[autosize]="galleryRef.config().itemAutosize"
[centralized]="galleryRef.config().centralized"
resizeSensor
smoothScroll
intersectionSensor
Expand Down Expand Up @@ -55,14 +53,16 @@ import { AutoHeight } from '../observers/auto-height';
HammerSliding,
ScrollSnapType,
GalleryItemComponent,
SliderComponent,
AutoHeight
SliderComponent
]
})
export class GallerySliderComponent {

readonly galleryRef: GalleryRef = inject(GalleryRef);

// TODO: Allow auto-height to access resize sensor
resizeSensor = viewChild(ResizeSensor);

/** Stream that emits when thumb is clicked */
itemClick: OutputEmitterRef<number> = output<number>();

Expand Down
19 changes: 12 additions & 7 deletions projects/ng-gallery/src/lib/components/gallery-slider.scss
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
height: var(--slider-height, 100%);
width: var(--slider-width, 100%);

//&.g-resizing {
// // Changes the height of the slider to match the active item height
// height: var(--slider-auto-height, 100%);
//}

overflow: var(--slider-overflow);
scroll-snap-type: var(--slider-scroll-snap-type);
scroll-snap-stop: always;
Expand Down Expand Up @@ -48,13 +53,13 @@
display: none;
}

&.g-resizing {
::ng-deep {
gallery-item {
visibility: hidden;
}
}
}
//&.g-resizing {
// ::ng-deep {
// gallery-item {
// visibility: hidden;
// }
// }
//}

&.g-sliding, &.g-scrolling {
// Disable mouse click on gallery items/thumbnails when the slider is being dragged using the mouse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import {
} from '@angular/core';
import { NgTemplateOutlet } from '@angular/common';
import { GalleryItemContext } from '../directives/gallery-item-def.directive';
import { GalleryImageComponent } from './templates/gallery-image.component';
import { ImageItemData } from './templates/items.model';
import { GalleryImageComponent } from '../templates/gallery-image.component';
import { ImageItemData } from '../templates/items.model';
import { GalleryItemType } from '../models/constants';
import { GalleryRef } from '../services/gallery-ref';
import { SliderItem } from './items/items';
Expand Down
16 changes: 9 additions & 7 deletions projects/ng-gallery/src/lib/components/gallery.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import {
TemplateRef,
OutputEmitterRef,
ChangeDetectionStrategy,
InputSignalWithTransform
InputSignalWithTransform, viewChild
} from '@angular/core';
import { NgTemplateOutlet } from '@angular/common';
import { Directionality } from '@angular/cdk/bidi';
import { GalleryRef } from '../services/gallery-ref';
import { GalleryError, GalleryItem } from '../models/gallery.model';
import { IframeItemData, ImageItemData, VideoItemData, VimeoItemData, YoutubeItemData } from './templates/items.model';
import { IframeItemData, ImageItemData, VideoItemData, VimeoItemData, YoutubeItemData } from '../templates/items.model';
import { GALLERY_CONFIG, GalleryConfig } from '../models/config.model';
import { BezierEasingOptions } from '../smooth-scroll';
import { GalleryImageDef } from '../directives/gallery-image-def.directive';
Expand All @@ -41,7 +41,7 @@ import { GallerySliderComponent } from './gallery-slider.component';
'[attr.dir]': 'dir.value',
'[attr.debug]': 'debug()',
'[attr.imageSize]': 'imageSize()',
'[attr.autoHeight]': 'autoHeight()',
// '[attr.autoHeight]': 'autoHeight()',
'[attr.orientation]': 'orientation()',
'[attr.itemAutosize]': 'itemAutosize()',
'[attr.scrollDisabled]': 'disableScroll()'
Expand Down Expand Up @@ -71,6 +71,8 @@ import { GallerySliderComponent } from './gallery-slider.component';
})
export class GalleryComponent {

slider = viewChild(GallerySliderComponent);

/**
* The gallery reference instance
*/
Expand Down Expand Up @@ -128,9 +130,9 @@ export class GalleryComponent {
/**
* Automatically adjusts the gallery's height to fit the content
*/
autoHeight: InputSignalWithTransform<boolean, string | boolean> = input<boolean, string | boolean>(this._config.autoHeight, {
transform: booleanAttribute
});
// autoHeight: InputSignalWithTransform<boolean, string | boolean> = input<boolean, string | boolean>(this._config.autoHeight, {
// transform: booleanAttribute
// });

/**
* Automatically cycle through items at time interval
Expand Down Expand Up @@ -283,7 +285,7 @@ export class GalleryComponent {
disableScroll: this.disableScroll(),
disableMouseScroll: this.disableMouseScroll(),
itemAutosize: this.itemAutosize(),
autoHeight: this.autoHeight()
// autoHeight: this.autoHeight()
};
});

Expand Down
12 changes: 11 additions & 1 deletion projects/ng-gallery/src/lib/components/gallery.scss
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

gap: var(--g-gutter-size);
width: 100%;
width: 776px;
//width: 776px;
height: 500px;
min-height: 100%;
max-height: 100%;
Expand All @@ -33,6 +33,16 @@
--g-item-height: 100%;
--g-item-max-height: var(--slider-height);

&.g-resizing {
// Changes the height of the slider to match the active item height
//--slider-height: var(--slider-auto-height) !important;
//::ng-deep {
// .g-slider {
// height: var(--slider-auto-height) !important;
// }
//}
}

&[gallerize] {
--g-item-cursor: pointer;
}
Expand Down
Loading

0 comments on commit 58f48b1

Please sign in to comment.