-
-
Notifications
You must be signed in to change notification settings - Fork 436
/
Copy pathAutoplay.ts
181 lines (162 loc) · 4.65 KB
/
Autoplay.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { ARIA_CONTROLS, ARIA_LABEL } from '../../constants/attributes';
import { CLASS_ACTIVE } from '../../constants/classes';
import {
EVENT_AUTOPLAY_PAUSE,
EVENT_AUTOPLAY_PLAY,
EVENT_AUTOPLAY_PLAYING,
EVENT_MOVE,
EVENT_MOVED,
EVENT_REFRESH,
EVENT_SCROLL,
} from '../../constants/events';
import { EventInterface, RequestInterval } from '../../constructors';
import { Splide } from '../../core/Splide/Splide';
import { BaseComponent, Components, Options } from '../../types';
import { apply, getAttribute, setAttribute, style, toggleClass } from '../../utils';
import { INTERVAL_DATA_ATTRIBUTE } from './constants';
/**
* The interface for the Autoplay component.
*
* @since 3.0.0
*/
export interface AutoplayComponent extends BaseComponent {
play(): void;
pause(): void;
isPaused(): boolean;
}
/**
* The component for autoplay, handling a progress bar and a toggle button.
*
* @since 3.0.0
*
* @param Splide - A Splide instance.
* @param Components - A collection of components.
* @param options - Options.
*
* @return An Autoplay component object.
*/
export function Autoplay( Splide: Splide, Components: Components, options: Options ): AutoplayComponent {
const { on, bind, emit } = EventInterface( Splide );
const interval = RequestInterval( options.interval, Splide.go.bind( Splide, '>' ), onAnimationFrame );
const { isPaused } = interval;
const { Elements, Elements: { root, toggle } } = Components;
const { autoplay } = options;
/**
* Indicates whether the slider is hovered or not.
*/
let hovered: boolean;
/**
* Indicates whether one of slider elements has focus or not.
*/
let focused: boolean;
/**
* Indicates whether the autoplay is stopped or not.
* If stopped, autoplay won't start automatically unless `play()` is explicitly called.
*/
let stopped = autoplay === 'pause';
/**
* Called when the component is mounted.
*/
function mount(): void {
if ( autoplay ) {
listen();
toggle && setAttribute( toggle, ARIA_CONTROLS, Elements.track.id );
stopped || play();
update();
}
}
/**
* Listens to some events.
*/
function listen(): void {
if ( options.pauseOnHover ) {
bind( root, 'mouseenter mouseleave', e => {
hovered = e.type === 'mouseenter';
autoToggle();
} );
}
if ( options.pauseOnFocus ) {
bind( root, 'focusin focusout', e => {
focused = e.type === 'focusin';
autoToggle();
} );
}
if ( toggle ) {
bind( toggle, 'click', () => {
stopped ? play() : pause( true );
} );
}
on( [ EVENT_MOVE, EVENT_SCROLL, EVENT_REFRESH ], interval.rewind );
on( EVENT_MOVE, onMove );
on( EVENT_MOVED, apply( play, false ) );
}
/**
* Starts autoplay and clears all flags.
* @param isRestart - Determines whether to restart autoplay or not.
*/
function play(isRestart = true): void {
if ( ( isRestart && isPaused() && Components.Slides.isEnough() ) || ( ! isRestart && ! stopped && Components.Slides.isEnough() ) ) {
interval.start( ! options.resetProgress );
focused = hovered = stopped = false;
update();
emit( EVENT_AUTOPLAY_PLAY );
}
}
/**
* Pauses autoplay.
*
* @param stop - If `true`, autoplay keeps paused until `play()` is explicitly called.
*/
function pause( stop = true ): void {
stopped = !! stop;
update();
if ( ! isPaused() ) {
interval.pause();
emit( EVENT_AUTOPLAY_PAUSE );
}
}
/**
* Toggles play/pause according to current flags.
* If autoplay is manually paused, this will do nothing.
*/
function autoToggle(): void {
if ( ! stopped ) {
hovered || focused ? pause( false ) : play();
}
}
/**
* Updates the toggle button status.
*/
function update(): void {
if ( toggle ) {
toggleClass( toggle, CLASS_ACTIVE, ! stopped );
setAttribute( toggle, ARIA_LABEL, options.i18n[ stopped ? 'play' : 'pause' ] );
}
}
/**
* Called on every animation frame while autoplay is active.
*
* @param rate - The progress rate between 0 and 1.
*/
function onAnimationFrame( rate: number ): void {
const { bar } = Elements;
bar && style( bar, 'width', `${ rate * 100 }%` );
emit( EVENT_AUTOPLAY_PLAYING, rate );
}
/**
* Updates or restores the interval duration.
*
* @param index - An index to move to.
*/
function onMove( index: number ): void {
const Slide = Components.Slides.getAt( index );
interval.set( Slide && +getAttribute( Slide.slide, INTERVAL_DATA_ATTRIBUTE ) || options.interval );
}
return {
mount,
destroy: interval.cancel,
play,
pause,
isPaused,
};
}