1
1
import { ExportNs } from '../../../esl-utils/environment/export-ns' ;
2
- import { attr , bind , listen , ready } from '../../../esl-utils/decorators' ;
2
+ import { bind , listen , ready } from '../../../esl-utils/decorators' ;
3
3
4
4
import { ESLCarouselPlugin } from '../esl-carousel.plugin' ;
5
5
import { ESLCarouselSlideEvent } from '../../core/esl-carousel.events' ;
6
6
7
+ export interface ESLCarouselAutoplayConfig {
8
+ /** Timeout to send next command to the host carousel */
9
+ timeout : number ;
10
+ /** Navigation command to send to the host carousel. Default: 'slide:next' */
11
+ command : string ;
12
+ }
13
+
7
14
/**
8
15
* {@link ESLCarousel } autoplay (auto-advance) plugin mixin
9
16
* Automatically switch slides by timeout
10
17
*
11
18
* @author Alexey Stsefanovich (ala'n)
12
19
*/
13
20
@ExportNs ( 'Carousel.Autoplay' )
14
- export class ESLCarouselAutoplayMixin extends ESLCarouselPlugin {
21
+ export class ESLCarouselAutoplayMixin extends ESLCarouselPlugin < ESLCarouselAutoplayConfig > {
15
22
public static override is = 'esl-carousel-autoplay' ;
16
-
17
- /** Timeout to send next command to the host carousel */
18
- @attr ( { defaultValue : '5000' , name : ESLCarouselAutoplayMixin . is } )
19
- public timeout : number ;
20
-
21
- /** Navigation command to send to the host carousel. Default: 'slide:next' */
22
- @attr ( { defaultValue : 'slide:next' , name : ESLCarouselAutoplayMixin . is + '-command' } )
23
- public command : string ;
23
+ public static override DEFAULT_CONFIG_KEY = 'timeout' ;
24
24
25
25
private _timeout : number | null = null ;
26
26
27
+ public get active ( ) : boolean {
28
+ return ! ! this . _timeout ;
29
+ }
30
+
27
31
@ready
28
32
protected override connectedCallback ( ) : void {
29
33
if ( super . connectedCallback ( ) ) {
@@ -36,33 +40,34 @@ export class ESLCarouselAutoplayMixin extends ESLCarouselPlugin {
36
40
this . stop ( ) ;
37
41
}
38
42
39
- protected override attributeChangedCallback ( name : string , oldValue : string | null , newValue : string | null ) : void {
43
+ protected override onConfigChange ( ) : void {
40
44
this . start ( ) ;
41
45
}
42
46
43
47
/** Activates the timer to send commands */
44
48
public start ( ) : void {
45
49
this . stop ( ) ;
46
- this . _timeout = window . setTimeout ( this . _onInterval , this . timeout ) ;
50
+ this . _timeout = window . setTimeout ( this . _onInterval , this . config . timeout ) ;
47
51
}
48
52
49
53
/** Deactivates the timer to send commands */
50
54
public stop ( ) : void {
51
- if ( typeof this . _timeout === 'number' ) {
52
- window . clearTimeout ( this . _timeout ) ;
53
- }
55
+ this . _timeout && window . clearTimeout ( this . _timeout ) ;
56
+ this . _timeout = null ;
54
57
}
55
58
56
59
/** Handles next timer interval */
57
60
@bind
58
61
protected _onInterval ( ) : void {
59
- this . $host ?. goTo ( this . command ) ;
60
- this . _timeout = window . setTimeout ( this . _onInterval , this . timeout ) ;
62
+ this . $host ?. goTo ( this . config . command ) ;
63
+ this . _timeout = window . setTimeout ( this . _onInterval , this . config . timeout ) ;
61
64
}
62
65
63
66
/** Handles auxiliary events to pause/resume timer */
64
67
@listen ( `mouseout mouseover focusin focusout ${ ESLCarouselSlideEvent . AFTER } ` )
65
68
protected _onInteract ( e : Event ) : void {
69
+ // Slide change can only delay the timer, but not start it
70
+ if ( e . type === ESLCarouselSlideEvent . AFTER && ! this . active ) return ;
66
71
if ( [ 'mouseover' , 'focusin' ] . includes ( e . type ) ) {
67
72
this . stop ( ) ;
68
73
} else {
0 commit comments