gian-ngx-tiny-slider
Version:
Angular component that gives you possibility to use tiny-slider library.
67 lines (53 loc) • 1.82 kB
text/typescript
import {Component, Input, OnDestroy, OnInit, ViewChild, ViewEncapsulation} from '@angular/core';
import {Subject} from 'rxjs';
import {first, takeWhile} from 'rxjs/operators';
import {GianNgxTinySliderService} from './gian-ngx-tiny-slider.service';
import {NgxTinySliderSettingsInterface} from './interfaces/ngx-tiny-slider-settings.interface';
export class GianNgxTinySliderComponent implements OnInit, OnDestroy {
config: NgxTinySliderSettingsInterface = {};
slideItemsContainerRef;
public sliderInstance;
private aliveObservable = true;
public domReady = new Subject();
private defaultConfig = this.ngxTinySliderService.getDefaultConfig();
constructor(private ngxTinySliderService: GianNgxTinySliderService) {
}
ngOnInit() {
if (this.config) {
this.extendConfig();
}
if (this.config.waitForDom) {
this.listenForDomReady();
} else {
this.initSlider();
}
}
ngOnDestroy() {
if (this.config.waitForDom) {
this.aliveObservable = false;
}
if (this.sliderInstance && this.sliderInstance.destroy) {
this.sliderInstance.destroy();
}
}
private listenForDomReady() {
this.domReady
.pipe(
first(),
takeWhile(() => this.aliveObservable)
)
.subscribe(() => this.initSlider());
}
private extendConfig() {
Object.keys(this.config).forEach(i => this.defaultConfig[i] = this.config[i]);
}
private initSlider() {
this.sliderInstance = this.ngxTinySliderService.initSlider(this.defaultConfig, this.slideItemsContainerRef);
}
}