Skip to content
This repository has been archived by the owner on Aug 6, 2022. It is now read-only.

Commit

Permalink
Added the performance mode also for the component
Browse files Browse the repository at this point in the history
  • Loading branch information
sconix committed Nov 23, 2018
1 parent 3784235 commit c7daaa8
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ Simply replace the element that would ordinarily be passed to `Swiper` with the

[index] // Can be used to set the active slide index.
[disabled] // Disables changing of slides (locks the Swiper).
[performance] // Toggle EventEmitters into performance mode (they run outside angular zone, and don't cause force change detection)

[useSwiperClass] // Use 'swiper' class (use provided default styles).

Expand Down Expand Up @@ -138,6 +137,7 @@ Swiper directive can be used in correctly structured div element with optional c

[index] // Can be used to set the active slide index.
[disabled] // Disables changing of slides (locks the Swiper).
[performance] // Emit all Swiper events outside the Angular zone.

(indexChange) // Event handler for the Swiper index change event.

Expand Down
2 changes: 1 addition & 1 deletion src/lib/swiper.component.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div #swiper class="s-wrapper" [class.swiper]="useSwiperClass" [class.swiper-container]="useSwiperClass" [swiper]="getConfig()" [index]="index" [disabled]="disabled">
<div #swiper class="s-wrapper" [class.swiper]="useSwiperClass" [class.swiper-container]="useSwiperClass" [swiper]="getConfig()" [index]="index" [disabled]="disabled" [performance]="performance">
<div #swiperSlides class="swiper-wrapper">
<ng-content></ng-content>
</div>
Expand Down
2 changes: 2 additions & 0 deletions src/lib/swiper.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export class SwiperComponent implements AfterViewInit, OnDestroy {

@Input() disabled: boolean = false;

@Input() performance: boolean = false;

@Input() config?: SwiperConfigInterface;

@Input() useSwiperClass: boolean = true;
Expand Down
27 changes: 12 additions & 15 deletions src/lib/swiper.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,9 @@ export class SwiperDirective implements AfterViewInit, OnDestroy, DoCheck, OnCha
@Output('slideChangeTransitionEnd' ) S_SLIDECHANGETRANSITIONEND = new EventEmitter<any>();
@Output('slideChangeTransitionStart' ) S_SLIDECHANGETRANSITIONSTART = new EventEmitter<any>();

constructor(
@Inject(PLATFORM_ID) private platformId: Object,
private zone: NgZone,
private elementRef: ElementRef,
private differs: KeyValueDiffers,
@Optional() @Inject(SWIPER_CONFIG) private defaults: SwiperConfigInterface
) {}
constructor(@Inject(PLATFORM_ID) private platformId: Object, private zone: NgZone,
private elementRef: ElementRef, private differs: KeyValueDiffers,
@Optional() @Inject(SWIPER_CONFIG) private defaults: SwiperConfigInterface) {}

ngAfterViewInit(): void {
if (!isPlatformBrowser(this.platformId)) {
Expand Down Expand Up @@ -160,6 +156,7 @@ export class SwiperDirective implements AfterViewInit, OnDestroy, DoCheck, OnCha
}

const output = `S_${swiperEvent.toUpperCase()}`;

const emitter = this[output as keyof SwiperDirective] as EventEmitter<any>;

if (emitter.observers.length) {
Expand All @@ -175,14 +172,6 @@ export class SwiperDirective implements AfterViewInit, OnDestroy, DoCheck, OnCha
}
}

emit(emitter: EventEmitter<any>, value: any) {
if (!this.performance) {
this.zone.run(() => emitter.emit(value));
} else {
emitter.emit(value);
}
}

ngOnDestroy(): void {
if (this.instance) {
this.zone.runOutsideAngular(() => {
Expand Down Expand Up @@ -229,6 +218,14 @@ export class SwiperDirective implements AfterViewInit, OnDestroy, DoCheck, OnCha
}
}

private emit(emitter: EventEmitter<any>, value: any): void {
if (this.performance) {
emitter.emit(value);
} else {
this.zone.run(() => emitter.emit(value));
}
}

public swiper(): any {
return this.instance;
}
Expand Down

0 comments on commit c7daaa8

Please sign in to comment.