@creative-web-solution/front-library 7.1.28 → 7.1.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 7.1.29
4
+
5
+ * [DragSlider]: Fix callback issue
6
+
7
+
3
8
  ## 7.1.28
4
9
 
5
10
  * [YouTubePlayer]: Update types + use ready event to handle end of loading
@@ -1,33 +1,33 @@
1
- import { wait } from '../Helpers/Wait';
2
- import { debounce } from '../Helpers/Debounce';
3
- import { offset } from '../DOM/Offset';
4
- import { gesture, gestureOff } from '../Events/Gesture';
5
- import { prop } from '../DOM/Styles';
6
- import { aClass, rClass, tClass } from '../DOM/Class';
7
- import { on, off } from '../Events/EventsManager';
8
- import { isTouchDevice } from '../Tools/TouchDeviceSupport';
9
- import { extend } from '../Helpers/Extend';
1
+ import { wait } from "../Helpers/Wait";
2
+ import { debounce } from "../Helpers/Debounce";
3
+ import { offset } from "../DOM/Offset";
4
+ import { gesture, gestureOff } from "../Events/Gesture";
5
+ import { prop } from "../DOM/Styles";
6
+ import { aClass, rClass, tClass } from "../DOM/Class";
7
+ import { on, off } from "../Events/EventsManager";
8
+ import { isTouchDevice } from "../Tools/TouchDeviceSupport";
9
+ import { extend } from "../Helpers/Extend";
10
10
 
11
11
  const defaultOptions = {
12
12
  swipeTresholdMin: 40,
13
13
  swipeTresholdSize: 0.5,
14
- lockedClass: 'is-locked',
15
- _animReset: function($list) {
14
+ lockedClass: "is-locked",
15
+ _animReset: function ($list) {
16
16
  // gsap.set( $list, {
17
17
  // "x": 0,
18
18
  // "y": 0,
19
19
  // "z": 0
20
20
  // } );
21
21
  },
22
- _animClear: function($list) {
22
+ _animClear: function ($list) {
23
23
  // gsap.set( $list, {
24
24
  // "clearProps": "all"
25
25
  // } );
26
26
  },
27
- _animKill: function($list) {
27
+ _animKill: function ($list) {
28
28
  // gsap.killTweensOf( $list );
29
29
  },
30
- _animMoveItem: function($list, x, onUpdate) {
30
+ _animMoveItem: function ($list, x, onUpdate) {
31
31
  // return gsap.to( $list, {
32
32
  // "duration": 0.3,
33
33
  // "x": x,
@@ -38,14 +38,14 @@ const defaultOptions = {
38
38
  // }
39
39
  // } );
40
40
  },
41
- _setCoordinates: function($list, x) {
41
+ _setCoordinates: function ($list, x) {
42
42
  // gsap.set( $list, {
43
43
  // "x": x,
44
44
  // "y": 0,
45
45
  // "z": 0
46
46
  // } );
47
- }
48
- }
47
+ },
48
+ };
49
49
 
50
50
  const MINIMUM_MOVEMENT_TO_START_DRAG = 3; // px
51
51
 
@@ -53,34 +53,35 @@ const MINIMUM_MOVEMENT_TO_START_DRAG = 3; // px
53
53
  * DragSlider
54
54
  */
55
55
  export default class DragSlider {
56
- #isDraggingActive: boolean;
57
- #options: FLib.DragSlider.Options;
58
- #deltaMove: FLib.DragSlider.DeltaMove = {
59
- "x": 0,
60
- "deltaX": 0,
61
- "deltaY": 0,
62
- "newX": 0
56
+ #isDraggingActive: boolean;
57
+ #options: FLib.DragSlider.Options;
58
+ #deltaMove: FLib.DragSlider.DeltaMove = {
59
+ x: 0,
60
+ deltaX: 0,
61
+ deltaY: 0,
62
+ newX: 0,
63
63
  };
64
- #itemArray: FLib.DragSlider.Item[];
65
- #$slider: HTMLElement;
64
+ #itemArray: FLib.DragSlider.Item[];
65
+ #$slider: HTMLElement;
66
66
  #viewportInfo;
67
- #siteOffsetLeft = 0;
68
- #siteOffsetRight = 0;
69
- #listDelta = 0;
70
- #$viewport: HTMLElement | undefined;
71
- #$items: NodeList | undefined;
72
- #$list: HTMLElement | undefined;
73
- #isDragging = false;
74
- #itemMap = new Map<HTMLElement, FLib.DragSlider.Item>();
75
- #firstItem: FLib.DragSlider.Item | undefined;
76
- #currentSnapItem: FLib.DragSlider.Item | undefined;
77
- #hasAlreadyBeenDragged = false;
78
- #startDragCoords: FLib.Events.Gesture.Coords | undefined;
79
- #isInitialized = false;
80
- #visibleItems: FLib.DragSlider.Item[] = [];
81
- #hiddenItems: FLib.DragSlider.Item[] = [];
67
+ #siteOffsetLeft = 0;
68
+ #siteOffsetRight = 0;
69
+ #listDelta = 0;
70
+ #$viewport: HTMLElement | undefined;
71
+ #$items: NodeList | undefined;
72
+ #$list: HTMLElement | undefined;
73
+ #isDragging = false;
74
+ #itemMap = new Map<HTMLElement, FLib.DragSlider.Item>();
75
+ #firstItem: FLib.DragSlider.Item | undefined;
76
+ #currentSnapItem: FLib.DragSlider.Item | undefined;
77
+ #hasAlreadyBeenDragged = false;
78
+ #startDragCoords: FLib.Events.Gesture.Coords | undefined;
79
+ #isInitialized = false;
80
+ #visibleItems: FLib.DragSlider.Item[] = [];
81
+ #hiddenItems: FLib.DragSlider.Item[] = [];
82
82
  #debouncedOnResize;
83
-
83
+ #minXPos: number = 0;
84
+ #maxXPos: number = 0;
84
85
 
85
86
  get count(): number {
86
87
  return this.#$items?.length ?? 0;
@@ -106,83 +107,110 @@ export default class DragSlider {
106
107
  return this.#hiddenItems;
107
108
  }
108
109
 
110
+ get firstItem(): FLib.DragSlider.Item | undefined {
111
+ return this.#itemArray[0];
112
+ }
109
113
 
110
- constructor( $slider: HTMLElement, userOptions: Partial<FLib.DragSlider.Options> ) {
114
+ get lastItem(): FLib.DragSlider.Item | undefined {
115
+ return this.#itemArray[this.#itemArray.length - 1];
116
+ }
111
117
 
112
- if ( !userOptions.viewportSelector || !userOptions.listSelector || !userOptions.itemSelector || !userOptions.dragClass ) {
113
- throw '[Drag Slider]: Missing at least one of viewportSelector, listSelector, itemSelector, dragClass';
118
+ constructor(
119
+ $slider: HTMLElement,
120
+ userOptions: Partial<FLib.DragSlider.Options>
121
+ ) {
122
+ if (
123
+ !userOptions.viewportSelector ||
124
+ !userOptions.listSelector ||
125
+ !userOptions.itemSelector ||
126
+ !userOptions.dragClass
127
+ ) {
128
+ throw "[Drag Slider]: Missing at least one of viewportSelector, listSelector, itemSelector, dragClass";
114
129
  }
115
130
 
116
131
  this.#$slider = $slider;
117
132
 
118
133
  this.#isDraggingActive = false;
119
134
 
120
- this.#options = extend( defaultOptions, userOptions );
135
+ this.#options = extend(defaultOptions, userOptions);
121
136
 
122
137
  this.#itemArray = [];
123
138
 
124
- wait( 'idle' ).then( this.init );
139
+ wait("idle").then(this.init);
125
140
  }
126
141
 
127
-
128
142
  #cancelLinkClick = (): void => {
129
- aClass( this.#$slider, this.#options.dragClass );
130
- }
131
-
143
+ aClass(this.#$slider, this.#options.dragClass);
144
+ };
132
145
 
133
146
  #activeLinkClick = (): void => {
134
- wait().then( () => {
135
- rClass( this.#$slider, this.#options.dragClass );
136
- } );
137
- }
138
-
147
+ wait().then(() => {
148
+ rClass(this.#$slider, this.#options.dragClass);
149
+ });
150
+ };
139
151
 
140
152
  #onResize = (): void => {
141
- if ( !this.#$items || !this.#$list ) {
153
+ if (!this.#$items || !this.#$list) {
142
154
  return;
143
155
  }
144
156
 
145
- this.#viewportInfo = offset( this.#$viewport as HTMLElement );
146
- this.#siteOffsetLeft = parseInt( prop( (this.#$items[ 0 ] as HTMLElement), 'marginLeft' ), 10 );
147
- this.#siteOffsetRight = parseInt( prop( (this.#$items[ this.#$items.length - 1 ] as HTMLElement), 'marginRight' ), 10 );
157
+ this.#viewportInfo = offset(this.#$viewport as HTMLElement);
158
+ this.#siteOffsetLeft = parseInt(
159
+ prop(this.#$items[0] as HTMLElement, "marginLeft"),
160
+ 10
161
+ );
162
+ this.#siteOffsetRight = parseInt(
163
+ prop(
164
+ this.#$items[this.#$items.length - 1] as HTMLElement,
165
+ "marginRight"
166
+ ),
167
+ 10
168
+ );
169
+
170
+ this.#listDelta = this.#getMaxMoveSize();
148
171
 
149
- this.#listDelta = this.#viewportInfo.width - this.#$list.scrollWidth - this.#siteOffsetLeft - this.#siteOffsetRight;
172
+ this.#minXPos = 0;
173
+ this.#maxXPos = this.#listDelta;
150
174
 
151
175
  const prevIsDraggingActive = this.#isDraggingActive;
152
- this.#isDraggingActive = this.#listDelta < 0;
176
+ this.#isDraggingActive = this.#listDelta < 0;
153
177
 
154
- if ( !this.#isDraggingActive ) {
178
+ if (!this.#isDraggingActive) {
155
179
  this.#isDragging = false;
156
- this.#options._animKill( this.#$list );
157
- this.#options._animReset( this.#$list );
180
+ this.#options._animKill(this.#$list);
181
+ this.#options._animReset(this.#$list);
158
182
  }
159
183
 
160
- tClass( this.#$slider, this.#options.lockedClass, !this.#isDraggingActive );
184
+ tClass(
185
+ this.#$slider,
186
+ this.#options.lockedClass,
187
+ !this.#isDraggingActive
188
+ );
161
189
 
162
- if ( prevIsDraggingActive !== this.#isDraggingActive ) {
163
- this.#options.onChangeState?.( this.#isDraggingActive );
190
+ if (prevIsDraggingActive !== this.#isDraggingActive) {
191
+ this.#options.onChangeState?.(this.#isDraggingActive);
164
192
  }
165
193
 
166
194
  this.#itemArray.length = 0;
167
- const ABS_LIST_DELTA = Math.abs( this.#listDelta );
195
+ const ABS_LIST_DELTA = Math.abs(this.#listDelta);
168
196
 
169
197
  let flag = false;
170
198
 
171
199
  for (let index = 0; index < this.#$items.length; ++index) {
172
- const $ITEM = this.#$items[ index ] as HTMLElement;
173
- const ITEM_OFFSET = offset( $ITEM, false, this.#$list );
200
+ const $ITEM = this.#$items[index] as HTMLElement;
201
+ const ITEM_OFFSET = offset($ITEM, false, this.#$list);
174
202
  const DATA = {
175
203
  index,
176
- "isFirst": index === 0,
177
- "isLast": false,
178
- "$item": $ITEM,
179
- "info": ITEM_OFFSET
204
+ isFirst: index === 0,
205
+ isLast: false,
206
+ $item: $ITEM,
207
+ info: ITEM_OFFSET,
180
208
  };
181
209
 
182
210
  if (ITEM_OFFSET.left - this.#siteOffsetLeft <= ABS_LIST_DELTA) {
183
211
  this.#itemArray.push(DATA);
184
212
 
185
- this.#itemMap.set( $ITEM, DATA );
213
+ this.#itemMap.set($ITEM, DATA);
186
214
  continue;
187
215
  }
188
216
 
@@ -201,84 +229,84 @@ export default class DragSlider {
201
229
  }
202
230
  flag = true;
203
231
 
204
- this.#itemMap.set( $ITEM, DATA );
232
+ this.#itemMap.set($ITEM, DATA);
205
233
  }
206
234
 
207
- this.#firstItem = this.#itemMap.get( (this.#$items[ 0 ] as HTMLElement) ) as FLib.DragSlider.Item;
235
+ this.#firstItem = this.#itemMap.get(
236
+ this.#$items[0] as HTMLElement
237
+ ) as FLib.DragSlider.Item;
208
238
 
209
- if ( !this.#currentSnapItem ) {
239
+ if (!this.#currentSnapItem) {
210
240
  this.#currentSnapItem = this.#firstItem;
211
- }
212
- else {
213
- this.#currentSnapItem = this.#itemArray[ this.#currentSnapItem.index ];
241
+ } else {
242
+ this.#currentSnapItem =
243
+ this.#itemArray[this.#currentSnapItem.index];
214
244
  }
215
245
 
216
246
  this.#updateAccessibilityFeature();
217
- }
218
-
247
+ };
219
248
 
220
- #snapToItemAnimation = ( snapItem: FLib.DragSlider.Item ): Promise<any> => {
249
+ #snapToItemAnimation = (snapItem: FLib.DragSlider.Item): Promise<any> => {
221
250
  if (!snapItem) {
222
251
  return Promise.resolve();
223
252
  }
224
253
 
225
- let finalX;
254
+ const finalX = Math.max(
255
+ Math.min(
256
+ this.#minXPos,
257
+ -1 * snapItem.info.left + this.#siteOffsetLeft
258
+ ),
259
+ this.#maxXPos
260
+ );
226
261
 
227
- finalX = -1 * snapItem.info.left;
262
+ this.#options.onSnap?.(this.#getCallbackOptions(finalX, snapItem));
228
263
 
229
- finalX = Math.max( Math.min( 0, finalX ), this.#listDelta );
230
-
231
- // If close to the end, then snap to it
232
- if ( Math.abs( finalX - this.#listDelta ) <= 3 ) {
233
- finalX = this.#listDelta;
234
- }
235
-
236
- // If close to the start, then snap to it
237
- if ( Math.abs( finalX ) <= 3 ) {
238
- finalX = 0;
239
- }
264
+ this.#currentSnapItem = snapItem;
240
265
 
241
- const IS_SNAP_TO_END = finalX === this.#listDelta;
242
- const IS_SNAP_TO_START = finalX === 0;
266
+ return this.#options
267
+ ._animMoveItem(this.#$list, finalX, (newX) => {
268
+ this.#deltaMove.x = newX;
243
269
 
244
- this.#options.onSnap?.( {
245
- "item": snapItem,
246
- "xPos": this.#deltaMove.x,
247
- "moveMaxSize": this.#listDelta,
248
- "isAtStart": IS_SNAP_TO_START,
249
- "isAtEnd": IS_SNAP_TO_END,
250
- "visibleItems": this.#visibleItems,
251
- "hiddenItems": this.#hiddenItems
252
- } );
270
+ this.#options.onSnapUpdate?.(
271
+ this.#getCallbackOptions(newX, snapItem)
272
+ );
273
+ })
274
+ .then(() => {
275
+ return this.#updateAccessibilityFeature();
276
+ })
277
+ .then(() => {
278
+ this.#deltaMove.x = finalX;
279
+ this.#options.onSnapEnd?.(
280
+ this.#getCallbackOptions(this.#deltaMove.x)
281
+ );
282
+ });
283
+ };
253
284
 
254
- this.#currentSnapItem = snapItem;
285
+ #getCallbackOptions(xPos: number, snapItem?: FLib.DragSlider.Item) {
286
+ const IS_SNAP_TO_END = xPos === this.#listDelta;
287
+ const IS_SNAP_TO_START = xPos === 0;
255
288
 
256
- finalX += this.#siteOffsetLeft;
257
-
258
- return this.#options._animMoveItem( this.#$list, finalX, (newX) => {
259
- this.#deltaMove.x = newX;
260
-
261
- this.#options.onSnapUpdate?.( {
262
- "item": snapItem,
263
- "xPos": this.#deltaMove.x,
264
- "moveMaxSize": this.#listDelta,
265
- "isAtStart": IS_SNAP_TO_START,
266
- "isAtEnd": IS_SNAP_TO_END,
267
- "visibleItems": this.#visibleItems,
268
- "hiddenItems": this.#hiddenItems
269
- } );
270
- } ).then(() => this.#updateAccessibilityFeature());
289
+ return {
290
+ item: snapItem ?? this.#currentSnapItem,
291
+ xPos: this.#deltaMove.x,
292
+ moveMaxSize: this.#listDelta,
293
+ isAtStart: IS_SNAP_TO_START,
294
+ isAtEnd: IS_SNAP_TO_END,
295
+ visibleItems: this.#visibleItems,
296
+ hiddenItems: this.#hiddenItems,
297
+ };
271
298
  }
272
299
 
273
-
274
- #getFirstPreviousItem = ( xPos: number ): { snapItem: FLib.DragSlider.Item, snapToEnd: boolean } => {
300
+ #getFirstPreviousItem = (
301
+ xPos: number
302
+ ): { snapItem: FLib.DragSlider.Item; snapToEnd: boolean } => {
275
303
  let snapItem;
276
304
 
277
- const absXPos = Math.abs( xPos );
305
+ const absXPos = Math.abs(xPos);
278
306
 
279
- for ( const item of this.#itemArray ) {
280
- if ( item.info.left <= absXPos ) {
281
- snapItem = item;
307
+ for (const item of this.#itemArray) {
308
+ if (item.info.left <= absXPos) {
309
+ snapItem = item;
282
310
  continue;
283
311
  }
284
312
  break;
@@ -286,168 +314,173 @@ export default class DragSlider {
286
314
 
287
315
  return {
288
316
  snapItem,
289
- "snapToEnd": false
317
+ snapToEnd: false,
290
318
  };
291
- }
292
-
319
+ };
293
320
 
294
- #getFirstNextItem = ( xPos: number ): { snapItem: FLib.DragSlider.Item, snapToEnd: boolean } => {
321
+ #getFirstNextItem = (
322
+ xPos: number
323
+ ): { snapItem: FLib.DragSlider.Item; snapToEnd: boolean } => {
295
324
  let snapItem;
296
325
 
297
- const absXPos = Math.abs( xPos );
326
+ const absXPos = Math.abs(xPos);
298
327
 
299
- for ( const item of this.#itemArray ) {
300
- if ( item.info.left < absXPos ) {
328
+ for (const item of this.#itemArray) {
329
+ if (item.info.left < absXPos) {
301
330
  continue;
302
331
  }
303
332
 
304
- snapItem = item;
333
+ snapItem = item;
305
334
  break;
306
335
  }
307
336
 
308
337
  return {
309
- "snapItem": snapItem ? snapItem : this.#itemArray[ this.#itemArray.length - 1 ],
310
- "snapToEnd": !snapItem
338
+ snapItem: snapItem
339
+ ? snapItem
340
+ : this.#itemArray[this.#itemArray.length - 1],
341
+ snapToEnd: !snapItem,
311
342
  };
312
- }
313
-
343
+ };
314
344
 
315
- #getClosestItem = ( xPos: number ): { snapItem: FLib.DragSlider.Item, snapToEnd: boolean } => {
345
+ #getClosestItem = (
346
+ xPos: number
347
+ ): { snapItem: FLib.DragSlider.Item; snapToEnd: boolean } => {
316
348
  let lastDelta, snapItem;
317
349
 
318
- const absXPos = Math.abs( xPos );
350
+ const absXPos = Math.abs(xPos);
319
351
 
320
- for ( const item of this.#itemArray ) {
321
- const IS_LAST_DELTA = typeof lastDelta !== 'undefined';
322
- const newDelta = Math.abs( absXPos - item.info.left + this.#siteOffsetLeft );
352
+ for (const item of this.#itemArray) {
353
+ const IS_LAST_DELTA = typeof lastDelta !== "undefined";
354
+ const newDelta = Math.abs(
355
+ absXPos - item.info.left + this.#siteOffsetLeft
356
+ );
323
357
 
324
- if ( !IS_LAST_DELTA || newDelta <= lastDelta ) {
358
+ if (!IS_LAST_DELTA || newDelta <= lastDelta) {
325
359
  lastDelta = newDelta;
326
- snapItem = item;
360
+ snapItem = item;
327
361
  }
328
362
  }
329
363
 
330
364
  return {
331
365
  snapItem,
332
- snapToEnd: false
366
+ snapToEnd: false,
333
367
  };
334
- }
335
-
368
+ };
336
369
 
337
370
  #snapToItem = (): void => {
338
371
  let snapItem;
339
372
 
340
- const ABS_DELTA_X = Math.abs( this.#deltaMove.deltaX );
373
+ const ABS_DELTA_X = Math.abs(this.#deltaMove.deltaX);
341
374
 
342
375
  // If move a bit on right or left, juste move by one item only
343
- if ( ABS_DELTA_X >= this.#options.swipeTresholdMin && ABS_DELTA_X < Math.min( (this.#firstItem as FLib.DragSlider.Item ).info.width * this.#options.swipeTresholdSize, this.#options.swipeTresholdMin * 3 ) ) {
344
- if ( this.#deltaMove.deltaX < 0 ) {
345
- snapItem = this.#getFirstNextItem( this.#deltaMove.x );
376
+ if (
377
+ ABS_DELTA_X >= this.#options.swipeTresholdMin &&
378
+ ABS_DELTA_X <
379
+ Math.min(
380
+ (this.#firstItem as FLib.DragSlider.Item).info.width *
381
+ this.#options.swipeTresholdSize,
382
+ this.#options.swipeTresholdMin * 3
383
+ )
384
+ ) {
385
+ if (this.#deltaMove.deltaX < 0) {
386
+ snapItem = this.#getFirstNextItem(this.#deltaMove.x);
387
+ } else {
388
+ snapItem = this.#getFirstPreviousItem(this.#deltaMove.x);
346
389
  }
347
- else {
348
- snapItem = this.#getFirstPreviousItem( this.#deltaMove.x );
349
- }
350
- }
351
- else {
352
- snapItem = this.#getClosestItem( this.#deltaMove.x );
390
+ } else {
391
+ snapItem = this.#getClosestItem(this.#deltaMove.x);
353
392
  }
354
393
 
355
- if ( !snapItem ) {
394
+ if (!snapItem) {
356
395
  return;
357
396
  }
358
397
 
359
- this.#snapToItemAnimation( snapItem.snapItem ).then(() => {
360
- this.#options.onSnapEnd?.({
361
- "item": this.#currentSnapItem,
362
- "xPos": this.#deltaMove.x,
363
- "moveMaxSize": this.#listDelta,
364
- "isAtStart": this.#deltaMove.x === 0,
365
- "isAtEnd": this.#deltaMove.x === this.#listDelta,
366
- "visibleItems": this.#visibleItems,
367
- "hiddenItems": this.#hiddenItems
368
- })
369
- });
370
- }
398
+ this.#snapToItemAnimation(snapItem.snapItem);
399
+ };
371
400
 
401
+ #getMaxMoveSize(): number {
402
+ return (
403
+ this.#viewportInfo.width -
404
+ this.#$list!.scrollWidth -
405
+ this.#siteOffsetLeft
406
+ );
407
+ }
372
408
 
373
- #onStartDrag = ( e: Event, $target: HTMLElement, coords: FLib.Events.Gesture.Coords ): void => {
374
- if ( !this.#hasAlreadyBeenDragged ) {
409
+ #onStartDrag = (
410
+ e: Event,
411
+ $target: HTMLElement,
412
+ coords: FLib.Events.Gesture.Coords
413
+ ): void => {
414
+ if (!this.#hasAlreadyBeenDragged) {
375
415
  this.#onResize();
376
416
  this.#hasAlreadyBeenDragged = true;
377
417
  }
378
418
 
379
- if ( !this.#isDraggingActive || !this.#$list ) {
419
+ if (!this.#isDraggingActive || !this.#$list) {
380
420
  return;
381
421
  }
382
422
 
383
423
  this.#isDragging = true;
384
424
 
385
- this.#options._animKill( this.#$list );
425
+ this.#options._animKill(this.#$list);
386
426
 
387
427
  this.#startDragCoords = coords;
388
- this.#listDelta = this.#viewportInfo.width - this.#$list.scrollWidth - this.#siteOffsetLeft - this.#siteOffsetRight;
389
- this.#deltaMove.newX = this.#deltaMove.x;
390
-
391
- gesture( document.body, 'dragSlider', {
392
- "move": this.#onMove,
393
- "end": this.#onStopDrag,
394
- "preventMove": e => {
395
- if ( !e.cancelable ) {
428
+ this.#listDelta = this.#getMaxMoveSize();
429
+ this.#deltaMove.newX = this.#deltaMove.x;
430
+
431
+ gesture(document.body, "dragSlider", {
432
+ move: this.#onMove,
433
+ end: this.#onStopDrag,
434
+ preventMove: (e) => {
435
+ if (!e.cancelable) {
396
436
  return false;
397
437
  }
398
438
 
399
- return !isTouchDevice ||
400
- isTouchDevice && Math.abs( this.#deltaMove.deltaY ) < Math.abs( this.#deltaMove.deltaX );
401
- }
402
- } );
403
-
404
- this.#options.onStartDrag?.( {
405
- "item": this.#currentSnapItem,
406
- "xPos": this.#deltaMove.x,
407
- "moveMaxSize": this.#listDelta,
408
- "isAtStart": this.#deltaMove.x === 0,
409
- "isAtEnd": this.#deltaMove.x === this.#listDelta,
410
- "visibleItems": this.#visibleItems,
411
- "hiddenItems": this.#hiddenItems
412
- } );
413
- }
439
+ return (
440
+ !isTouchDevice ||
441
+ (isTouchDevice &&
442
+ Math.abs(this.#deltaMove.deltaY) <
443
+ Math.abs(this.#deltaMove.deltaX))
444
+ );
445
+ },
446
+ });
414
447
 
448
+ this.#options.onStartDrag?.(
449
+ this.#getCallbackOptions(this.#deltaMove.x)
450
+ );
451
+ };
415
452
 
416
- #onMove = ( e: Event, $target: HTMLElement, coords: FLib.Events.Gesture.Coords ): void => {
453
+ #onMove = (
454
+ e: Event,
455
+ $target: HTMLElement,
456
+ coords: FLib.Events.Gesture.Coords
457
+ ): void => {
417
458
  this.#cancelLinkClick();
418
459
 
419
- this.#deltaMove.deltaX = coords.pageX - ( this.#startDragCoords as FLib.Events.Gesture.Coords ).pageX;
420
- this.#deltaMove.deltaY = coords.pageY - ( this.#startDragCoords as FLib.Events.Gesture.Coords ).pageY;
460
+ this.#deltaMove.deltaX =
461
+ coords.pageX -
462
+ (this.#startDragCoords as FLib.Events.Gesture.Coords).pageX;
463
+ this.#deltaMove.deltaY =
464
+ coords.pageY -
465
+ (this.#startDragCoords as FLib.Events.Gesture.Coords).pageY;
421
466
 
422
467
  if (Math.abs(this.#deltaMove.deltaX) < MINIMUM_MOVEMENT_TO_START_DRAG) {
423
468
  return;
424
469
  }
425
470
 
426
- this.#deltaMove.newX = this.#deltaMove.deltaX + this.#deltaMove.x;
427
-
428
- if ( this.#deltaMove.newX > 0 ) {
429
- this.#deltaMove.newX = 0;
430
- }
431
- else if ( this.#deltaMove.newX < this.#listDelta + this.#siteOffsetRight ) {
432
- this.#deltaMove.newX = this.#listDelta + this.#siteOffsetRight;
433
- }
471
+ this.#deltaMove.newX = this.#deltaMove.deltaX + this.#deltaMove.x;
472
+ this.#deltaMove.newX = Math.max(
473
+ Math.min(this.#minXPos, this.#deltaMove.newX),
474
+ this.#maxXPos
475
+ );
434
476
 
435
477
  this.#options._setCoordinates(this.#$list, this.#deltaMove.newX);
436
478
 
437
- this.#options.onDrag?.( {
438
- "item": this.#currentSnapItem,
439
- "xPos": this.#deltaMove.newX,
440
- "moveMaxSize": this.#listDelta,
441
- "isAtStart": this.#deltaMove.newX === 0,
442
- "isAtEnd": this.#deltaMove.newX === this.#listDelta,
443
- "visibleItems": this.#visibleItems,
444
- "hiddenItems": this.#hiddenItems
445
- } );
446
- }
447
-
479
+ this.#options.onDrag?.(this.#getCallbackOptions(this.#deltaMove.newX));
480
+ };
448
481
 
449
482
  #onStopDrag = (): void => {
450
- gestureOff( document.body, 'dragSlider' );
483
+ gestureOff(document.body, "dragSlider");
451
484
  this.#isDragging = false;
452
485
 
453
486
  this.#activeLinkClick();
@@ -460,138 +493,109 @@ export default class DragSlider {
460
493
 
461
494
  this.#snapToItem();
462
495
 
463
- this.#options.onStopDrag?.({
464
- "item": this.#currentSnapItem,
465
- "xPos": this.#deltaMove.x,
466
- "moveMaxSize": this.#listDelta,
467
- "isAtStart": this.#deltaMove.x === 0,
468
- "isAtEnd": this.#deltaMove.x === this.#listDelta,
469
- "visibleItems": this.#visibleItems,
470
- "hiddenItems": this.#hiddenItems
471
- } );
472
- }
473
-
496
+ this.#options.onStopDrag?.(this.#getCallbackOptions(this.#deltaMove.x));
497
+ };
474
498
 
475
499
  #onMouseenter = (): void => {
476
- if ( this.#isDragging || !this.#isDraggingActive ) {
500
+ if (this.#isDragging || !this.#isDraggingActive) {
477
501
  return;
478
502
  }
479
503
 
480
- this.#options.onMouseEnter?.( {
481
- "item": this.#currentSnapItem,
482
- "xPos": this.#deltaMove.x,
483
- "moveMaxSize": this.#listDelta,
484
- "isAtStart": this.#deltaMove.x === 0,
485
- "isAtEnd": this.#deltaMove.x === this.#listDelta,
486
- "visibleItems": this.#visibleItems,
487
- "hiddenItems": this.#hiddenItems
488
- } );
489
- }
490
-
504
+ this.#options.onMouseEnter?.(
505
+ this.#getCallbackOptions(this.#deltaMove.x)
506
+ );
507
+ };
491
508
 
492
509
  #onMouseleave = (): void => {
493
- if ( this.#isDragging || !this.#isDraggingActive ) {
510
+ if (this.#isDragging || !this.#isDraggingActive) {
494
511
  return;
495
512
  }
496
513
 
497
- this.#options.onMouseLeave?.( {
498
- "item": this.#currentSnapItem,
499
- "xPos": this.#deltaMove.x,
500
- "moveMaxSize": this.#listDelta,
501
- "isAtStart": this.#deltaMove.x === 0,
502
- "isAtEnd": this.#deltaMove.x === this.#listDelta,
503
- "visibleItems": this.#visibleItems,
504
- "hiddenItems": this.#hiddenItems
505
- } );
506
- }
507
-
514
+ this.#options.onMouseLeave?.(
515
+ this.#getCallbackOptions(this.#deltaMove.x)
516
+ );
517
+ };
508
518
 
509
- #cancelDrag = ( e: Event ): void => {
519
+ #cancelDrag = (e: Event): void => {
510
520
  e.preventDefault();
511
- }
512
-
521
+ };
513
522
 
514
523
  next(): Promise<any> {
515
524
  const CURRENT_ITEM = this.#currentSnapItem as FLib.DragSlider.Item;
516
525
 
517
- if ( !this.#isDraggingActive || !this.#itemArray[ CURRENT_ITEM.index + 1 ] ) {
526
+ if (
527
+ !this.#isDraggingActive ||
528
+ !this.#itemArray[CURRENT_ITEM.index + 1]
529
+ ) {
518
530
  return Promise.resolve();
519
531
  }
520
532
 
521
- return this.#snapToItemAnimation( this.#itemArray[ CURRENT_ITEM.index + 1 ] );
533
+ return this.#snapToItemAnimation(
534
+ this.#itemArray[CURRENT_ITEM.index + 1]
535
+ );
522
536
  }
523
537
 
524
-
525
538
  previous(): Promise<any> {
526
539
  const CURRENT_ITEM = this.#currentSnapItem as FLib.DragSlider.Item;
527
540
 
528
- if ( !this.#isDraggingActive || CURRENT_ITEM.isFirst ) {
541
+ if (!this.#isDraggingActive || CURRENT_ITEM.isFirst) {
529
542
  return Promise.resolve();
530
543
  }
531
544
 
532
- return this.#snapToItemAnimation( this.#itemArray[ CURRENT_ITEM.index - 1 ] );
545
+ return this.#snapToItemAnimation(
546
+ this.#itemArray[CURRENT_ITEM.index - 1]
547
+ );
533
548
  }
534
549
 
535
-
536
- goToItem( blockOrIndex: HTMLElement | number ): Promise<any> {
537
-
538
- if ( !this.#isDraggingActive ) {
550
+ goToItem(blockOrIndex: HTMLElement | number): Promise<any> {
551
+ if (!this.#isDraggingActive) {
539
552
  return Promise.resolve();
540
553
  }
541
554
 
542
555
  let $block;
543
556
 
544
- if (typeof blockOrIndex === 'number') {
557
+ if (typeof blockOrIndex === "number") {
545
558
  $block = this.#$items?.[blockOrIndex] as HTMLElement;
546
- }
547
- else {
559
+ } else {
548
560
  $block = blockOrIndex;
549
561
  }
550
562
 
551
- if ( !$block ) {
563
+ if (!$block) {
552
564
  return Promise.resolve();
553
565
  }
554
566
 
555
- const ITEM = this.#itemMap.get( $block );
567
+ const ITEM = this.#itemMap.get($block);
556
568
 
557
- if ( !ITEM ) {
569
+ if (!ITEM) {
558
570
  return Promise.resolve();
559
571
  }
560
572
 
561
573
  this.#currentSnapItem = ITEM;
562
574
 
563
- this.#options.onSnap?.( {
564
- "item": ITEM,
565
- "xPos": this.#deltaMove.x,
566
- "moveMaxSize": this.#listDelta,
567
- "isAtStart": this.#deltaMove.x === 0,
568
- "isAtEnd": this.#deltaMove.x === this.#listDelta,
569
- "visibleItems": this.#visibleItems,
570
- "hiddenItems": this.#hiddenItems
571
- } );
572
-
573
- return this.#options._animMoveItem(this.#$list, -1 * ITEM.info.left + this.#siteOffsetLeft, (x) => {
574
- this.#deltaMove.x = x;
575
-
576
- this.#options.onSnapUpdate?.( {
577
- "item": ITEM,
578
- "xPos": this.#deltaMove.x,
579
- "moveMaxSize": this.#listDelta,
580
- "isAtStart": this.#deltaMove.x === 0,
581
- "isAtEnd": this.#deltaMove.x === this.#listDelta,
582
- "visibleItems": this.#visibleItems,
583
- "hiddenItems": this.#hiddenItems
584
- } );
585
- }).then(() => this.#updateAccessibilityFeature());
575
+ const xPos = -1 * ITEM.info.left + this.#siteOffsetLeft;
576
+
577
+ this.#options.onSnap?.(this.#getCallbackOptions(xPos, ITEM));
578
+
579
+ return this.#options
580
+ ._animMoveItem(this.#$list, xPos, (x) => {
581
+ this.#deltaMove.x = x;
582
+
583
+ this.#options.onSnapUpdate?.(
584
+ this.#getCallbackOptions(this.#deltaMove.x, ITEM)
585
+ );
586
+ })
587
+ .then(() => this.#updateAccessibilityFeature());
586
588
  }
587
589
 
588
590
  #updateAccessibilityFeature = (): void => {
589
591
  this.#visibleItems.length = 0;
590
592
  this.#hiddenItems.length = 0;
591
593
 
592
- this.#itemMap.forEach(item => {
594
+ this.#itemMap.forEach((item) => {
593
595
  const hideElement1 = item.info.left + this.#deltaMove.x < 0;
594
- const hideElement2 = item.info.left + item.info.width + this.#deltaMove.x > this.#viewportInfo.width;
596
+ const hideElement2 =
597
+ item.info.left + item.info.width + this.#deltaMove.x >
598
+ this.#viewportInfo.width;
595
599
 
596
600
  if (hideElement1 || hideElement2) {
597
601
  item.$item.setAttribute("tabindex", "-1");
@@ -606,132 +610,128 @@ export default class DragSlider {
606
610
  item.$item.setAttribute("aria-hidden", "false");
607
611
  this.#visibleItems.push(item);
608
612
  });
609
- }
613
+ };
610
614
 
611
615
  refresh = (): this => {
612
616
  this.#onResize();
613
617
 
614
618
  return this;
615
- }
619
+ };
616
620
 
617
621
  init = (): this => {
618
- if ( this.#isInitialized ) {
622
+ if (this.#isInitialized) {
619
623
  return this;
620
624
  }
621
625
 
622
626
  this.#isInitialized = true;
623
627
 
624
- if ( !this.#$viewport ) {
625
- const $VP = this.#$slider.querySelector( this.#options.viewportSelector );
626
- if ( !$VP ) {
627
- throw `"${ this.#options.viewportSelector }" not found`;
628
+ if (!this.#$viewport) {
629
+ const $VP = this.#$slider.querySelector(
630
+ this.#options.viewportSelector
631
+ );
632
+ if (!$VP) {
633
+ throw `"${this.#options.viewportSelector}" not found`;
628
634
  }
629
- this.#$viewport = $VP as HTMLElement;
635
+ this.#$viewport = $VP as HTMLElement;
630
636
 
631
- const $LST = this.#$viewport.querySelector( this.#options.listSelector );
632
- if ( !$LST ) {
633
- throw `"${ this.#options.listSelector }" not found`;
637
+ const $LST = this.#$viewport.querySelector(
638
+ this.#options.listSelector
639
+ );
640
+ if (!$LST) {
641
+ throw `"${this.#options.listSelector}" not found`;
634
642
  }
635
- this.#$list = $LST as HTMLElement;
643
+ this.#$list = $LST as HTMLElement;
636
644
 
637
- const $ITEMS = this.#$list.querySelectorAll( this.#options.itemSelector );
638
- if ( !$ITEMS ) {
639
- throw `"${ this.#options.itemSelector }" not found`;
645
+ const $ITEMS = this.#$list.querySelectorAll(
646
+ this.#options.itemSelector
647
+ );
648
+ if (!$ITEMS) {
649
+ throw `"${this.#options.itemSelector}" not found`;
640
650
  }
641
- this.#$items = $ITEMS as NodeList;
651
+ this.#$items = $ITEMS as NodeList;
642
652
 
643
- this.#debouncedOnResize = debounce( this.#onResize );
653
+ this.#debouncedOnResize = debounce(this.#onResize);
644
654
  }
645
655
 
646
- if ( !this.#$items?.length ) {
656
+ if (!this.#$items?.length) {
647
657
  return this;
648
658
  }
649
659
 
650
- aClass( this.#$slider, 'is-active' );
660
+ aClass(this.#$slider, "is-active");
651
661
 
652
662
  this.#onResize();
653
663
 
654
- on( window, {
655
- "eventsName": "resize",
656
- "callback": this.#debouncedOnResize
657
- } );
664
+ on(window, {
665
+ eventsName: "resize",
666
+ callback: this.#debouncedOnResize,
667
+ });
658
668
 
659
- gesture( this.#$viewport, 'dragSlider', {
660
- "start": this.#onStartDrag
661
- } );
669
+ gesture(this.#$viewport, "dragSlider", {
670
+ start: this.#onStartDrag,
671
+ });
662
672
 
663
673
  // Avoid image or content drag on Firefox
664
- on( this.#$items, {
665
- "eventsName": "dragstart",
666
- "callback": this.#cancelDrag
667
- } );
668
-
669
- on( this.#$viewport, {
670
- "eventsName": "mouseenter",
671
- "callback": this.#onMouseenter
672
- } );
673
-
674
- on( this.#$viewport, {
675
- "eventsName": "mouseleave",
676
- "callback": this.#onMouseleave
677
- } );
678
-
679
- this.#options.onInit?.( {
680
- "item": this.#currentSnapItem,
681
- "xPos": this.#deltaMove.x,
682
- "moveMaxSize": this.#listDelta,
683
- "isAtStart": this.#deltaMove.x === 0,
684
- "isAtEnd": this.#deltaMove.x === this.#listDelta,
685
- "visibleItems": this.#visibleItems,
686
- "hiddenItems": this.#hiddenItems
687
- } );
674
+ on(this.#$items, {
675
+ eventsName: "dragstart",
676
+ callback: this.#cancelDrag,
677
+ });
688
678
 
689
- return this;
690
- }
679
+ on(this.#$viewport, {
680
+ eventsName: "mouseenter",
681
+ callback: this.#onMouseenter,
682
+ });
683
+
684
+ on(this.#$viewport, {
685
+ eventsName: "mouseleave",
686
+ callback: this.#onMouseleave,
687
+ });
688
+
689
+ this.#options.onInit?.(this.#getCallbackOptions(this.#deltaMove.x));
691
690
 
691
+ return this;
692
+ };
692
693
 
693
694
  destroy(): this {
694
- this.#isInitialized = false;
695
+ this.#isInitialized = false;
695
696
  this.#hasAlreadyBeenDragged = false;
696
697
 
697
- if ( !this.#$items?.length ) {
698
+ if (!this.#$items?.length) {
698
699
  return this;
699
700
  }
700
701
 
701
- off( window, {
702
- "eventsName": "resize",
703
- "callback": this.#debouncedOnResize
704
- } );
702
+ off(window, {
703
+ eventsName: "resize",
704
+ callback: this.#debouncedOnResize,
705
+ });
705
706
 
706
- this.#$viewport && gestureOff( this.#$viewport, 'dragSlider' );
707
+ this.#$viewport && gestureOff(this.#$viewport, "dragSlider");
707
708
 
708
- gestureOff( document.body, 'dragSlider' );
709
+ gestureOff(document.body, "dragSlider");
709
710
 
710
- off( this.#$items, {
711
- "eventsName": "dragstart",
712
- "callback": this.#cancelDrag
713
- } );
711
+ off(this.#$items, {
712
+ eventsName: "dragstart",
713
+ callback: this.#cancelDrag,
714
+ });
714
715
 
715
- off( this.#$viewport, {
716
- "eventsName": "mouseenter",
717
- "callback": this.#onMouseenter
718
- } );
716
+ off(this.#$viewport, {
717
+ eventsName: "mouseenter",
718
+ callback: this.#onMouseenter,
719
+ });
719
720
 
720
- off( this.#$viewport, {
721
- "eventsName": "mouseleave",
722
- "callback": this.#onMouseleave
723
- } );
721
+ off(this.#$viewport, {
722
+ eventsName: "mouseleave",
723
+ callback: this.#onMouseleave,
724
+ });
724
725
 
725
- if ( this.#$list ) {
726
- this.#options._animKill( this.#$list );
727
- this.#options._animClear( this.#$list );
726
+ if (this.#$list) {
727
+ this.#options._animKill(this.#$list);
728
+ this.#options._animClear(this.#$list);
728
729
  }
729
730
 
730
- this.#$viewport && rClass( this.#$viewport, this.#options.dragClass );
731
+ this.#$viewport && rClass(this.#$viewport, this.#options.dragClass);
731
732
 
732
- rClass( this.#$slider, 'is-active' );
733
+ rClass(this.#$slider, "is-active");
733
734
 
734
735
  return this;
735
736
  }
736
-
737
737
  }
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Front Library
2
2
 
3
- @version: 7.1.28
3
+ @version: 7.1.29
4
4
 
5
5
 
6
6
  ## Use
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@creative-web-solution/front-library",
3
3
  "title": "Frontend library",
4
4
  "description": "Frontend functions and modules",
5
- "version": "7.1.28",
5
+ "version": "7.1.29",
6
6
  "homepage": "https://github.com/creative-web-solution/front-library",
7
7
  "author": "Creative Web Solution <contact@cws-studio.com> (https://www.cws-studio.com)",
8
8
  "keywords": [],