Skip to content

Commit ad2255b

Browse files
committed
feat(lightbox directive) closes #200
1 parent 2a2c169 commit ad2255b

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { Directive, ElementRef, OnInit, Input, OnDestroy, Renderer2 } from '@angular/core';
2+
import { fromEvent, SubscriptionLike, Subscription } from 'rxjs';
3+
import { tap } from 'rxjs/operators';
4+
import { Lightbox } from './lightbox.service';
5+
6+
@Directive({
7+
selector: '[lightbox]'
8+
})
9+
export class LightboxDirective implements OnInit, OnDestroy {
10+
11+
clickEvent: SubscriptionLike = Subscription.EMPTY;
12+
13+
@Input('lightbox') index = 0;
14+
@Input('gallery') id = 'root';
15+
16+
constructor(private _lightbox: Lightbox, private _el: ElementRef, private _renderer: Renderer2) {
17+
}
18+
19+
ngOnInit() {
20+
this._renderer.setStyle(this._el.nativeElement, 'cursor', 'pointer');
21+
this.clickEvent = fromEvent(this._el.nativeElement, 'click').pipe(
22+
tap(() => this._lightbox.open(this.index, this.id))
23+
).subscribe();
24+
}
25+
26+
ngOnDestroy() {
27+
this.clickEvent.unsubscribe();
28+
}
29+
}

projects/lightbox/src/lib/lightbox.module.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Gallery, GalleryModule } from '@ngx-gallery/core';
44

55
import { Lightbox } from './lightbox.service';
66
import { LightboxComponent } from './lightbox.component';
7+
import { LightboxDirective } from './lightbox.directive';
78
import { LightboxConfig } from './lightbox.model';
89
import { LIGHTBOX_CONFIG } from './lightbox.token';
910

@@ -17,7 +18,11 @@ export function lightboxFactory(config: LightboxConfig, gallery: Gallery, overla
1718
GalleryModule
1819
],
1920
declarations: [
20-
LightboxComponent
21+
LightboxComponent,
22+
LightboxDirective
23+
],
24+
exports: [
25+
LightboxDirective
2126
],
2227
entryComponents: [
2328
LightboxComponent

0 commit comments

Comments
 (0)