-
-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathMove.ts
More file actions
302 lines (271 loc) · 8.74 KB
/
Move.ts
File metadata and controls
302 lines (271 loc) · 8.74 KB
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
import {
EVENT_MOUNTED,
EVENT_MOVE,
EVENT_MOVED,
EVENT_REFRESH,
EVENT_RESIZED,
EVENT_SHIFTED,
EVENT_UPDATED,
} from '../../constants/events';
import { IDLE, MOVING } from '../../constants/states';
import { FADE, LOOP, SLIDE } from '../../constants/types';
import { EventInterface } from '../../constructors';
import { Splide } from '../../core/Splide/Splide';
import { AnyFunction, BaseComponent, Components, Options, TransitionComponent } from '../../types';
import { abs, ceil, clamp, isUndefined, rect, style } from '../../utils';
/**
* The interface for the Move component.
*
* @since 3.0.0
*/
export interface MoveComponent extends BaseComponent {
move( dest: number, index: number, prev: number, callback?: AnyFunction ): void;
jump( index: number ): void;
translate( position: number, preventLoop?: boolean ): void;
shift( position: number, backwards: boolean ): number;
cancel(): void;
toIndex( position: number ): number;
toPosition( index: number, trimming?: boolean ): number;
getPosition(): number;
getLimit( max: boolean ): number;
exceededLimit( max?: boolean | undefined, position?: number ): boolean;
/** @internal */
reposition(): void;
}
/**
* The component for moving the slider.
*
* @since 3.0.0
*
* @param Splide - A Splide instance.
* @param Components - A collection of components.
* @param options - Options.
*
* @return A Move component object.
*/
export function Move( Splide: Splide, Components: Components, options: Options ): MoveComponent {
const { on, emit } = EventInterface( Splide );
const { set } = Splide.state;
const { slideSize, getPadding, totalSize, listSize, sliderSize } = Components.Layout;
const { resolve, orient } = Components.Direction;
const { list, track } = Components.Elements;
/**
* Holds the Transition component.
*/
let Transition: TransitionComponent;
/**
* Called when the component is mounted.
*/
function mount(): void {
Transition = Components.Transition;
on( [ EVENT_MOUNTED, EVENT_RESIZED, EVENT_UPDATED, EVENT_REFRESH ], reposition );
}
/**
* Repositions the slider.
* - Do not call `cancel()` here because LazyLoad may emit resize while transitioning.
* - iOS Safari emits window resize event while the user swipes the slider because of the bottom bar.
*/
function reposition(): void {
if ( ! Components.Controller.isBusy() ) {
Components.Scroll.cancel();
jump( Splide.index );
Components.Slides.update();
}
}
/**
* Moves the slider to the dest index with the Transition component.
*
* @param dest - A destination index to go to, including clones'.
* @param index - A slide index.
* @param prev - A previous index.
* @param callback - Optional. A callback function invoked after transition ends.
*/
function move( dest: number, index: number, prev: number, callback?: AnyFunction ): void {
if ( dest !== index && canShift( dest > prev ) ) {
cancel();
translate( shift( getPosition(), dest > prev ), true );
}
set( MOVING );
emit( EVENT_MOVE, index, prev, dest );
Transition.start( index, () => {
set( IDLE );
emit( EVENT_MOVED, index, prev, dest );
callback && callback();
} );
}
/**
* Jumps to the slide at the specified index.
*
* @param index - An index to jump to.
*/
function jump( index: number ): void {
translate( toPosition( index, true ) );
}
/**
* Moves the slider to the provided position.
*
* @param position - The position to move to.
* @param preventLoop - Optional. If `true`, sets the provided position as is.
*/
function translate( position: number, preventLoop?: boolean ): void {
if ( ! Splide.is( FADE ) ) {
const destination = preventLoop ? position : loop( position );
style( list, 'transform', `translate${ resolve( 'X' ) }(${ Math.round( destination ) }px)` );
position !== destination && emit( EVENT_SHIFTED );
}
}
/**
* Loops the provided position if it exceeds bounds (limit indices).
*
* @param position - A position to loop.
*/
function loop( position: number ): number {
if ( Splide.is( LOOP ) ) {
const index = toIndex( position );
const exceededMax = index > Components.Controller.getEnd();
const exceededMin = index < 0;
if ( exceededMin || exceededMax ) {
position = shift( position, exceededMax );
}
}
return position;
}
/**
* Adds or subtracts the slider width to the provided position.
*
* @param position - A position to shift.
* @param backwards - Determines whether to shift the slider backwards or forwards.
*
* @return The shifted position.
*/
function shift( position: number, backwards: boolean ): number {
const excess = position - getLimit( backwards );
const size = sliderSize();
position -= orient( size * ( ceil( abs( excess ) / size ) || 1 ) ) * ( backwards ? 1 : -1 );
return position;
}
/**
* Cancels transition.
*/
function cancel(): void {
translate( getPosition(), true );
Transition.cancel();
}
/**
* Returns the closest index to the position.
*
* @param position - A position to convert.
*
* @return The closest index to the position.
*/
function toIndex( position: number ): number {
const Slides = Components.Slides.get();
let index = 0;
let minDistance = Infinity;
for ( let i = 0; i < Slides.length; i++ ) {
const slideIndex = Slides[ i ].index;
const distance = abs( toPosition( slideIndex, true ) - position );
if ( distance <= minDistance ) {
minDistance = distance;
index = slideIndex;
} else {
break;
}
}
return index;
}
/**
* Converts the slide index to the position.
*
* @param index - An index to convert.
* @param trimming - Optional. Whether to trim edge spaces or not.
*
* @return The position corresponding with the index.
*/
function toPosition( index: number, trimming?: boolean ): number {
const position = orient( totalSize( index - 1 ) - offset( index ) );
return trimming ? trim( position ) : position;
}
/**
* Returns the current position.
*
* @return The position of the list element.
*/
function getPosition(): number {
const left = resolve( 'left' );
return rect( list )[ left ] - rect( track )[ left ] + orient( getPadding( false ) );
}
/**
* Trims spaces on the edge of the slider.
*
* @param position - A position to trim.
*
* @return A trimmed position.
*/
function trim( position: number ): number {
if ( options.trimSpace && Splide.is( SLIDE ) ) {
position = clamp( position, 0, orient( sliderSize( true ) - listSize() ) );
}
return position;
}
/**
* Returns the offset amount.
*
* @param index - An index.
*/
function offset( index: number ): number {
const { focus } = options;
return focus === 'center' ? ( listSize() - slideSize( index, true ) ) / 2 : +focus * slideSize( index ) || 0;
}
/**
* Returns the limit number that the slider can move to.
*
* @param max - Determines whether to return the maximum or minimum limit.
*
* @return The border number.
*/
function getLimit( max: boolean ): number {
return toPosition( max ? Components.Controller.getEnd() : 0, !! options.trimSpace );
}
/**
* Checks if there is enough width to shift the slider.
*
* @param backwards - `true` for checking backwards, or `false` for doing forwards.
*
* @return `true` if the slider can be shifted for the specified direction, or otherwise `false`.
*/
function canShift( backwards: boolean ): boolean {
const shifted = orient( shift( getPosition(), backwards ) );
return backwards
? shifted >= 0
: shifted <= list[ resolve( 'scrollWidth' ) ] - rect( track )[ resolve( 'width' ) ];
}
/**
* Checks if the provided position exceeds the minimum or maximum limit or not.
*
* @param max - Optional. `true` for testing max, `false` for min, and `undefined` for both.
* @param position - Optional. A position to test. If omitted, tests the current position.
*
* @return `true` if the position exceeds the limit, or otherwise `false`.
*/
function exceededLimit( max?: boolean | undefined, position?: number ): boolean {
position = isUndefined( position ) ? getPosition() : position;
const exceededMin = max !== true && orient( position ) < orient( getLimit( false ) );
const exceededMax = max !== false && orient( position ) > orient( getLimit( true ) );
return exceededMin || exceededMax;
}
return {
mount,
move,
jump,
translate,
shift,
cancel,
toIndex,
toPosition,
getPosition,
getLimit,
exceededLimit,
reposition,
};
}