import { Injectable, Inject } from '@angular/core'; import { merge, of } from 'rxjs'; import { tap, switchMap, first, filter } from 'rxjs/operators'; import { WINDOW } from './window-ref.service'; import { DOCUMENT } from './document-ref.service'; import * as i0 from "@angular/core"; import * as i1 from "./carousel.service"; class AutoplayService { carouselService; ngZone; /** * Subscrioption to merge Observables from CarouselService */ autoplaySubscription; /** * The autoplay timeout. */ _timeout = null; /** * Indicates whenever the autoplay is paused. */ _paused = false; /** * Shows whether the code (the plugin) changed the option 'AutoplayTimeout' for own needs */ _isArtificialAutoplayTimeout; /** * Shows whether the autoplay is paused for unlimited time by the developer. * Use to prevent autoplaying in case of firing `mouseleave` by adding layers to `` like `mat-menu` does */ _isAutoplayStopped = false; get isAutoplayStopped() { return this._isAutoplayStopped; } set isAutoplayStopped(value) { this._isAutoplayStopped = value; } winRef; docRef; constructor(carouselService, winRef, docRef, ngZone) { this.carouselService = carouselService; this.ngZone = ngZone; this.winRef = winRef; this.docRef = docRef; this.spyDataStreams(); } ngOnDestroy() { this.autoplaySubscription.unsubscribe(); } /** * Defines Observables which service must observe */ spyDataStreams() { const initializedCarousel$ = this.carouselService.getInitializedState().pipe(tap(() => { if (this.carouselService.settings.autoplay) { this.play(); } })); const changedSettings$ = this.carouselService.getChangedState().pipe(tap(data => { this._handleChangeObservable(data); })); const resized$ = this.carouselService.getResizedState().pipe(tap(() => { if (this.carouselService.settings.autoplay && !this._isAutoplayStopped) { this.play(); } else { this.stop(); } })); // original Autoplay Plugin has listeners on play.owl.core and stop.owl.core events. // They are triggered by Video Plugin const autoplayMerge$ = merge(initializedCarousel$, changedSettings$, resized$); this.autoplaySubscription = autoplayMerge$.subscribe(() => { }); } /** * Starts the autoplay. * @param timeout The interval before the next animation starts. * @param speed The animation speed for the animations. */ play(timeout, speed) { if (this._paused) { this._paused = false; this._setAutoPlayInterval(this.carouselService.settings.autoplayMouseleaveTimeout); } if (this.carouselService.is('rotating')) { return; } this.carouselService.enter('rotating'); this._setAutoPlayInterval(); } ; /** * Gets a new timeout * @param timeout - The interval before the next animation starts. * @param speed - The animation speed for the animations. * @return */ _getNextTimeout(timeout, speed) { if (this._timeout) { this.winRef.clearTimeout(this._timeout); } this._isArtificialAutoplayTimeout = timeout ? true : false; return this.ngZone.runOutsideAngular(() => { return this.winRef.setTimeout(() => { this.ngZone.run(() => { if (this._paused || this.carouselService.is('busy') || this.carouselService.is('interacting') || this.docRef.hidden) { return; } this.carouselService.next(speed || this.carouselService.settings.autoplaySpeed); }); }, timeout || this.carouselService.settings.autoplayTimeout); }); } ; /** * Sets autoplay in motion. */ _setAutoPlayInterval(timeout) { this._timeout = this._getNextTimeout(timeout); } ; /** * Stops the autoplay. */ stop() { if (!this.carouselService.is('rotating')) { return; } this._paused = true; this.winRef.clearTimeout(this._timeout); this.carouselService.leave('rotating'); } ; /** * Stops the autoplay. */ pause() { if (!this.carouselService.is('rotating')) { return; } this._paused = true; } ; /** * Manages by autoplaying according to data passed by _changedSettingsCarousel$ Obsarvable * @param data object with current position of carousel and type of change */ _handleChangeObservable(data) { if (data.property.name === 'settings') { if (this.carouselService.settings.autoplay) { this.play(); } else { this.stop(); } } else if (data.property.name === 'position') { //console.log('play?', e); if (this.carouselService.settings.autoplay) { this._setAutoPlayInterval(); } } } /** * Starts autoplaying of the carousel in the case when user leaves the carousel before it starts translateing (moving) */ _playAfterTranslated() { of('translated').pipe(switchMap(data => this.carouselService.getTranslatedState()), first(), filter(() => this._isArtificialAutoplayTimeout), tap(() => this._setAutoPlayInterval())).subscribe(() => { }); } /** * Starts pausing */ startPausing() { if (this.carouselService.settings.autoplayHoverPause && this.carouselService.is('rotating')) { this.pause(); } } /** * Starts playing after mouse leaves carousel */ startPlayingMouseLeave() { if (this.carouselService.settings.autoplayHoverPause && this.carouselService.is('rotating')) { this.play(); this._playAfterTranslated(); } } /** * Starts playing after touch ends */ startPlayingTouchEnd() { if (this.carouselService.settings.autoplayHoverPause && this.carouselService.is('rotating')) { this.play(); this._playAfterTranslated(); } } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.0.0", ngImport: i0, type: AutoplayService, deps: [{ token: i1.CarouselService }, { token: WINDOW }, { token: DOCUMENT }, { token: i0.NgZone }], target: i0.ɵɵFactoryTarget.Injectable }); static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.0.0", ngImport: i0, type: AutoplayService }); } export { AutoplayService }; i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.0.0", ngImport: i0, type: AutoplayService, decorators: [{ type: Injectable }], ctorParameters: function () { return [{ type: i1.CarouselService }, { type: undefined, decorators: [{ type: Inject, args: [WINDOW] }] }, { type: undefined, decorators: [{ type: Inject, args: [DOCUMENT] }] }, { type: i0.NgZone }]; } }); //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYXV0b3BsYXkuc2VydmljZS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uL2xpYnMvbmd4LW93bC1jYXJvdXNlbC1vL3NyYy9saWIvc2VydmljZXMvYXV0b3BsYXkuc2VydmljZS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEVBQUUsVUFBVSxFQUFFLE1BQU0sRUFBcUIsTUFBTSxlQUFlLENBQUM7QUFDdEUsT0FBTyxFQUE0QixLQUFLLEVBQUUsRUFBRSxFQUFFLE1BQU0sTUFBTSxDQUFDO0FBQzNELE9BQU8sRUFBRSxHQUFHLEVBQUUsU0FBUyxFQUFFLEtBQUssRUFBRSxNQUFNLEVBQUUsTUFBTSxnQkFBZ0IsQ0FBQztBQUcvRCxPQUFPLEVBQUUsTUFBTSxFQUFFLE1BQU0sc0JBQXNCLENBQUM7QUFDOUMsT0FBTyxFQUFFLFFBQVEsRUFBRSxNQUFNLHdCQUF3QixDQUFDOzs7QUFFbEQsTUFDYSxlQUFlO0lBcUNOO0lBR0E7SUF2Q3BCOztPQUVHO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