-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
AutoScroll.ts
342 lines (298 loc) · 7.71 KB
/
AutoScroll.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import {
BaseComponent,
CLASS_ACTIVE,
Components,
EVENT_DRAG, EVENT_DRAGGED,
EVENT_MOVE,
EVENT_MOVED,
EVENT_SCROLL,
EVENT_SCROLLED,
EVENT_UPDATED,
EventInterface,
FADE,
Options,
RequestInterval,
Throttle,
RequestIntervalInterface,
SLIDE,
Splide,
} from '@splidejs/splide';
import { assign, clamp, isObject, isUndefined, setAttribute, toggleClass } from '@splidejs/splide/src/js/utils';
import { DEFAULTS } from '../../constants/defaults';
import { AutoScrollOptions } from '../../types/options';
import { I18N } from "../../constants/i18n";
/**
* Lets the compiler know this component.
*/
declare module '@splidejs/splide' {
interface Options {
autoScroll?: AutoScrollOptions | boolean;
}
interface Components {
AutoScroll?: AutoScrollComponent;
}
}
/**
* The interface for the AutoScroll component.
*
* @since 0.1.0
*/
export interface AutoScrollComponent extends BaseComponent {
play(): void;
pause(): void;
isPaused(): boolean;
}
/**
* The extension for continuously scrolling the slider.
*
* @since 0.1.0
*
* @param Splide - A Splide instance.
* @param Components - A collection of components.
* @param options - Options.
*
* @return An AutoScroll component object.
*/
export function AutoScroll( Splide: Splide, Components: Components, options: Options ): AutoScrollComponent {
const { on, off, bind, unbind } = EventInterface( Splide );
const { translate, getPosition, toIndex, getLimit } = Components.Move;
const { setIndex, getIndex } = Components.Controller;
const { orient } = Components.Direction;
const { toggle } = Components.Elements;
const { Live } = Components;
const { root } = Splide;
/**
* The throttled function to update arrows.
*/
const throttledUpdateArrows = Throttle( Components.Arrows.update, 500 );
/**
* Keeps the latest options.
*/
let autoScrollOptions: AutoScrollOptions = {};
/**
* The RequestInterval object.
*/
let interval: RequestIntervalInterface;
/**
* Turns into `true` when the auto scroll is manually paused.
*/
let stopped: boolean;
/**
* Indicates whether the mouse cursor is on the slider or not.
*/
let hovered: boolean;
/**
* Indicates whether the slider contains active element or not.
*/
let focused: boolean;
/**
* Indicates whether the slider is currently busy or not.
*/
let busy: boolean;
/**
* Keeps the current position to restore.
*/
let currPosition: number;
/**
* Sets up the component.
*/
function setup(): void {
const { autoScroll } = options;
autoScrollOptions = assign( {}, DEFAULTS, isObject( autoScroll ) ? autoScroll : {} );
}
/**
* Called when the component is mounted.
*/
function mount(): void {
if ( ! Splide.is( FADE ) ) {
if ( ! interval && options.autoScroll !== false ) {
interval = RequestInterval( 0, move );
listen();
autoStart();
}
}
}
/**
* Destroys the component.
*/
function destroy(): void {
if ( interval ) {
interval.cancel();
interval = null;
currPosition = undefined;
off( [ EVENT_MOVE, EVENT_DRAG, EVENT_SCROLL, EVENT_MOVED, EVENT_SCROLLED ] );
unbind( root, 'mouseenter mouseleave focusin focusout' );
unbind( toggle, 'click' );
}
}
/**
* Listens to some events.
*/
function listen(): void {
if ( autoScrollOptions.pauseOnHover ) {
bind( root, 'mouseenter mouseleave', e => {
hovered = e.type === 'mouseenter';
autoToggle();
} );
}
if ( autoScrollOptions.pauseOnFocus ) {
bind( root, 'focusin focusout', e => {
focused = e.type === 'focusin';
autoToggle();
} );
}
if ( autoScrollOptions.useToggleButton ) {
bind( toggle, 'click', () => {
stopped ? play() : pause();
} );
}
on( EVENT_UPDATED, update );
on( [ EVENT_MOVE, EVENT_DRAG, EVENT_SCROLL ], () => {
busy = true;
pause( false );
} );
on( [ EVENT_MOVED, EVENT_DRAGGED, EVENT_SCROLLED ], () => {
busy = false;
autoToggle();
} );
}
/**
* Called when the slider is updated.
* Attempts to keep continuous scrolling with the current position
* since the update event makes the slider jump to the current index.
*/
function update(): void {
const { autoScroll } = options;
if ( autoScroll !== false ) {
autoScrollOptions = assign( {}, autoScrollOptions, isObject( autoScroll ) ? autoScroll : {} );
mount();
} else {
destroy();
}
if ( interval && ! isUndefined( currPosition ) ) {
translate( currPosition );
}
}
/**
* Starts scrolling the slider on the proper timing.
*/
function autoStart(): void {
if ( autoScrollOptions.autoStart ) {
if ( document.readyState === 'complete' ) {
play();
} else {
bind( window, 'load', play );
}
}
}
/**
* Starts auto scroll.
*/
function play(): void {
if ( isPaused() ) {
interval.start( true );
Live.disable( true );
focused = hovered = stopped = false;
updateButton();
}
}
/**
* Pauses auto scroll.
*
* @param stop - Optional. If `true`, auto scroll will never restart without calling `play()`.
*/
function pause( stop = true ): void {
if ( ! stopped ) {
stopped = stop;
updateButton();
if ( ! isPaused() ) {
interval.pause();
Live.disable( false );
}
}
}
/**
* Automatically plays or pauses scrolling.
*/
function autoToggle(): void {
if ( ! stopped ) {
hovered || focused || busy ? pause( false ) : play();
}
}
/**
* Called on every animation frame while the auto scroll is active.
*/
function move(): void {
const position = getPosition();
const destination = computeDestination( position );
if ( position !== destination ) {
translate( destination );
updateIndex( ( currPosition = getPosition() ) );
} else {
pause( false );
if ( autoScrollOptions.rewind ) {
Splide.go( autoScrollOptions.speed > 0 ? 0 : Components.Controller.getEnd() );
}
}
throttledUpdateArrows();
}
/**
* Returns the position to move.
*
* @param position - The current position.
*
* @return A computed destination.
*/
function computeDestination( position: number ): number {
const speed = autoScrollOptions.speed || 1;
position += orient( speed );
if ( Splide.is( SLIDE ) ) {
position = clamp( position, getLimit( false ), getLimit( true ) );
}
return position;
}
/**
* Updates index and notify them to other components.
* - Slides: Activity and visibility
* - Pagination: Updates the active item
* - LazyLoad: Load close images if necessary
*
* @param position - A current position.
*/
function updateIndex( position: number ): void {
const { length } = Splide;
const index = ( toIndex( position ) + length ) % length;
if ( index !== getIndex() ) {
setIndex( index );
Components.Slides.update();
Components.Pagination.update();
options.lazyLoad === 'nearby' && Components.LazyLoad.check();
}
}
/**
* Updates the toggle button status.
*/
function updateButton(): void {
if ( toggle ) {
const key = stopped ? 'startScroll' : 'pauseScroll';
toggleClass( toggle, CLASS_ACTIVE, ! stopped );
setAttribute( toggle, 'aria-label', options.i18n[ key ] || I18N[ key ] );
}
}
/**
* Checks if the auto scroll is paused or not.
*
* @return `true` if it's paused, or otherwise `false`.
*/
function isPaused(): boolean {
return ! interval || interval.isPaused();
}
return {
setup,
mount,
destroy,
play,
pause,
isPaused,
};
}