Skip to content

Commit

Permalink
feat(angular): index control
Browse files Browse the repository at this point in the history
  • Loading branch information
vltansky committed Dec 9, 2020
1 parent e66c241 commit 86670bd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
6 changes: 6 additions & 0 deletions playground-angular/src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
[breakpoints]="breakpoints"
[scrollbar]="scrollbar"
[loop]="true"
[(index)]="indexNumber"
[pagination]="{ type: 'fraction' }"
>
<ng-template swiperSlide>Best slide ever 1</ng-template>
Expand All @@ -39,4 +40,9 @@
<button (click)="toggleNavigation()">Toggle navigation</button>
<button (click)="toggleScrollbar()">Toggle scrollbar</button>
<button (click)="breakpointChange()">Breakpoint change</button>

<button (click)="indexNumber = 0">0</button>
<button (click)="indexNumber = 3">3</button>
<button (click)="indexNumber = 5">5</button>
<button (click)="indexNumber = 6">6</button>
</main>
1 change: 1 addition & 0 deletions playground-angular/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class AppComponent {
SwiperCore.use([Navigation, Pagination, Scrollbar, A11y, Virtual]);
}

indexNumber = 1;
slidesPerView: number = 4;
pagination: any = false;

Expand Down
35 changes: 34 additions & 1 deletion src/angular/src/swiper.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
EventEmitter,
HostBinding,
Input,
NgZone,
OnInit,
Output,
QueryList,
Expand Down Expand Up @@ -189,6 +190,13 @@ export class SwiperComponent implements OnInit {
return this._virtual;
}
private _virtual: SwiperOptions['virtual'];

@Input()
set index(index: number) {
if (index) {
this.setIndex(index);
}
}
// prettier-ignore
@Output('_beforeBreakpoint') s__beforeBreakpoint: EventEmitter<SwiperEvents['_beforeBreakpoint']> = new EventEmitter<any>();
// prettier-ignore
Expand Down Expand Up @@ -342,6 +350,8 @@ export class SwiperComponent implements OnInit {
// prettier-ignore
@Output('swiper') s_swiper: EventEmitter<any> = new EventEmitter<any>();

@Output() indexChange = new EventEmitter<number>();

@ViewChild('prevElRef', { static: false })
set prevElRef(el: ElementRef) {
this._setElement(el, this.navigation, 'navigation', 'prevEl');
Expand Down Expand Up @@ -389,7 +399,11 @@ export class SwiperComponent implements OnInit {
}

@HostBinding('class') containerClasses = 'swiper-container';
constructor(private elementRef: ElementRef, private _changeDetectorRef: ChangeDetectorRef) {}
constructor(
private zone: NgZone,
private elementRef: ElementRef,
private _changeDetectorRef: ChangeDetectorRef,
) {}

private _setElement(el: ElementRef, ref: any, update: string, key = 'el') {
if (!el && !ref) {
Expand Down Expand Up @@ -428,6 +442,11 @@ export class SwiperComponent implements OnInit {
};

Object.assign(swiperParams.on, {
slideChange() {
if (this.swiperRef) {
this.indexChange.emit(this.swiperRef.realIndex);
}
},
_containerClasses(swiper, classes) {
this.containerClasses = classes;
},
Expand Down Expand Up @@ -646,6 +665,20 @@ export class SwiperComponent implements OnInit {
}
}

setIndex(index: number, speed?: number, silent?: boolean): void {
if (!this.swiperRef) {
this.initialSlide = index;
return;
}
this.zone.runOutsideAngular(() => {
if (this.loop) {
this.swiperRef.slideToLoop(index, speed, !silent);
} else {
this.swiperRef.slideTo(index, speed, !silent);
}
});
}

ngOnDestroy() {
this.swiperRef.destroy();
}
Expand Down

0 comments on commit 86670bd

Please sign in to comment.