@creative-web-solution/front-library 7.1.16 → 7.1.18

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,13 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 7.1.18
4
+
5
+ * [DragSlider] Expose some properties + allow item index in goToItem function
6
+
7
+ ## 7.1.17
8
+
9
+ * [DragSlider] Rework + add refresh function
10
+
3
11
  ## 7.1.16
4
12
 
5
13
  * Fix doc
@@ -15,11 +15,16 @@ import { copy } from '../Helpers/Extend';
15
15
  export default class DragSlider {
16
16
  #isDraggingActive: boolean;
17
17
  #options: FLib.DragSlider.Options;
18
- #deltaMove: FLib.DragSlider.DeltaMove;
18
+ #deltaMove: FLib.DragSlider.DeltaMove = {
19
+ "x": 0,
20
+ "deltaX": 0,
21
+ "deltaY": 0,
22
+ "newX": 0
23
+ };
19
24
  #itemArray: FLib.DragSlider.Item[];
20
25
  #$slider: HTMLElement;
21
26
  #viewportInfo;
22
- #siteOffset;
27
+ #siteOffsetLeft = 0;
23
28
  #listDelta = 0;
24
29
  #$viewport: HTMLElement | undefined;
25
30
  #$items: NodeList | undefined;
@@ -34,10 +39,22 @@ export default class DragSlider {
34
39
  #debouncedOnResize;
35
40
 
36
41
 
42
+ get count(): number {
43
+ return this.#$items?.length ?? 0;
44
+ }
45
+
46
+ get currentSnapItem(): FLib.DragSlider.Item | undefined {
47
+ return this.#currentSnapItem;
48
+ }
49
+
37
50
  get isActive(): boolean {
38
51
  return this.#isDraggingActive;
39
52
  }
40
53
 
54
+ get items(): NodeList | undefined {
55
+ return this.#$items;
56
+ }
57
+
41
58
 
42
59
  constructor( $slider: HTMLElement, options: Partial<FLib.DragSlider.Options> ) {
43
60
 
@@ -45,13 +62,6 @@ export default class DragSlider {
45
62
  throw '[Drag Slider]: Missing at least one of viewportSelector, listSelector, itemSelector, dragClass';
46
63
  }
47
64
 
48
- this.#deltaMove = {
49
- "x": 0,
50
- "deltaX": 0,
51
- "deltaY": 0,
52
- "newX": 0
53
- };
54
-
55
65
  this.#$slider = $slider;
56
66
 
57
67
  this.#isDraggingActive = false;
@@ -84,8 +94,12 @@ export default class DragSlider {
84
94
  if ( !this.#$items || !this.#$list ) {
85
95
  return;
86
96
  }
87
- this.#viewportInfo = offset( this.#$viewport as HTMLElement );
88
- this.#siteOffset = parseInt( prop( (this.#$items[ 0 ] as HTMLElement), 'marginLeft' ), 10 );
97
+
98
+ const LAST_INDEX = this.#$items.length - 1;
99
+
100
+ this.#viewportInfo = offset( this.#$viewport as HTMLElement );
101
+ this.#siteOffsetLeft = parseInt( prop( (this.#$items[ 0 ] as HTMLElement), 'marginLeft' ), 10 );
102
+
89
103
  this.#listDelta = this.#viewportInfo.width - this.#$list.scrollWidth;
90
104
 
91
105
  const prevIsDraggingActive = this.#isDraggingActive;
@@ -108,23 +122,40 @@ export default class DragSlider {
108
122
  }
109
123
 
110
124
  this.#itemArray.length = 0;
125
+ const ABS_LIST_DELTA = Math.abs( this.#listDelta );
111
126
 
112
- const LAST_INDEX = this.#$items.length;
113
-
114
- this.#$items.forEach( ( $item: Node, index: number ) => {
115
- const $ITEM = $item as HTMLElement;
127
+ for (let index = 0; index < this.#$items.length; ++index) {
128
+ const $ITEM = this.#$items[ index ] as HTMLElement;
116
129
  const ITEM_OFFSET = offset( $ITEM, false, this.#$list );
117
130
 
131
+ if (ITEM_OFFSET.left - this.#siteOffsetLeft <= ABS_LIST_DELTA) {
132
+ this.#itemArray.push({
133
+ index,
134
+ "isFirst": index === 0,
135
+ "isLast": false,
136
+ "$item": $ITEM,
137
+ "info": ITEM_OFFSET
138
+ });
139
+
140
+ this.#itemMap.set( $ITEM, this.#itemArray[ index ] );
141
+ continue;
142
+ }
143
+
118
144
  this.#itemArray.push({
119
145
  index,
120
146
  "isFirst": index === 0,
121
- "isLast": index === LAST_INDEX - 1 || ITEM_OFFSET.left > Math.abs( this.#listDelta ),
147
+ "isLast": true,
122
148
  "$item": $ITEM,
123
- "info": ITEM_OFFSET
149
+ "info": {
150
+ ...ITEM_OFFSET,
151
+ "left": ABS_LIST_DELTA + this.#siteOffsetLeft,
152
+ "x": ABS_LIST_DELTA + this.#siteOffsetLeft
153
+ }
124
154
  });
125
-
126
155
  this.#itemMap.set( $ITEM, this.#itemArray[ index ] );
127
- } );
156
+
157
+ break;
158
+ }
128
159
 
129
160
  this.#firstItem = this.#itemMap.get( (this.#$items[ 0 ] as HTMLElement) ) as FLib.DragSlider.Item;
130
161
 
@@ -137,26 +168,26 @@ export default class DragSlider {
137
168
  }
138
169
 
139
170
 
140
- #snapToItemAnimation = ( snapItem: FLib.DragSlider.Item, snapToEnd?: FLib.DragSlider.Item ): gsap.core.Tween => {
141
- let finalX;
171
+ #snapToItemAnimation = ( snapItem: FLib.DragSlider.Item ): gsap.core.Tween | void => {
142
172
 
143
- if ( snapToEnd ) {
144
- finalX = this.#listDelta;
173
+ if (!snapItem) {
174
+ return;
145
175
  }
146
- else {
147
- finalX = -1 * snapItem.info.left + this.#siteOffset;
148
176
 
149
- finalX = Math.max( Math.min( 0, finalX ), this.#listDelta );
177
+ let finalX;
150
178
 
151
- // If close to the end, then snap to it
152
- if ( Math.abs( finalX - this.#listDelta ) <= 3 ) {
153
- finalX = this.#listDelta;
154
- }
179
+ finalX = -1 * snapItem.info.left + this.#siteOffsetLeft;
155
180
 
156
- // If close to the start, then snap to it
157
- if ( Math.abs( finalX ) <= 3 ) {
158
- finalX = 0;
159
- }
181
+ finalX = Math.max( Math.min( 0, finalX ), this.#listDelta );
182
+
183
+ // If close to the end, then snap to it
184
+ if ( Math.abs( finalX - this.#listDelta ) <= 3 ) {
185
+ finalX = this.#listDelta;
186
+ }
187
+
188
+ // If close to the start, then snap to it
189
+ if ( Math.abs( finalX ) <= 3 ) {
190
+ finalX = 0;
160
191
  }
161
192
 
162
193
  const IS_SNAP_TO_END = finalX === this.#listDelta;
@@ -216,7 +247,7 @@ export default class DragSlider {
216
247
 
217
248
 
218
249
  #getFirstNextItem = ( xPos: number ): { snapItem: FLib.DragSlider.Item, snapToEnd: boolean } => {
219
- let lastDelta, snapItem, snapToEnd;
250
+ let lastDelta, snapItem;
220
251
 
221
252
  const absXPos = Math.abs( xPos );
222
253
 
@@ -230,54 +261,31 @@ export default class DragSlider {
230
261
  break;
231
262
  }
232
263
 
233
- const lastItem = this.#itemArray[ this.#itemArray.length - 1 ];
234
-
235
- if ( !snapItem.isLast &&
236
- Math.abs( absXPos + this.#viewportInfo.width - ( lastItem.info.left + lastItem.info.width ) ) < lastDelta
237
- ) {
238
- snapItem = lastItem;
239
- snapToEnd = true;
240
- }
241
-
242
264
  return {
243
- snapItem,
244
- snapToEnd
265
+ "snapItem": snapItem ? snapItem : this.#itemArray[ this.#itemArray.length - 1 ],
266
+ "snapToEnd": !snapItem
245
267
  };
246
268
  }
247
269
 
248
270
 
249
271
  #getClosestItem = ( xPos: number ): { snapItem: FLib.DragSlider.Item, snapToEnd: boolean } => {
250
- let lastDelta, snapItem, snapToEnd = false;
272
+ let lastDelta, snapItem;
251
273
 
252
274
  const absXPos = Math.abs( xPos );
253
275
 
254
276
  for ( const item of this.#itemArray ) {
255
277
  const IS_LAST_DELTA = typeof lastDelta !== 'undefined';
256
- let newDelta = Math.abs( absXPos - item.info.left );
278
+ const newDelta = Math.abs( absXPos - item.info.left + this.#siteOffsetLeft );
257
279
 
258
- if ( !IS_LAST_DELTA || newDelta < lastDelta ) {
280
+ if ( !IS_LAST_DELTA || newDelta <= lastDelta ) {
259
281
  lastDelta = newDelta;
260
282
  snapItem = item;
261
283
  }
262
-
263
- if ( !IS_LAST_DELTA ) {
264
- continue;
265
- }
266
-
267
- if ( !snapItem.isLast && item.index === this.#itemMap.size - 1 ) {
268
- newDelta = Math.abs( Math.abs( this.#deltaMove.x ) + this.#viewportInfo.width - ( item.info.left + item.info.width ) );
269
-
270
- if ( newDelta < lastDelta ) {
271
- lastDelta = newDelta;
272
- snapItem = item;
273
- snapToEnd = true;
274
- }
275
- }
276
284
  }
277
285
 
278
286
  return {
279
287
  snapItem,
280
- snapToEnd
288
+ snapToEnd: false
281
289
  };
282
290
  }
283
291
 
@@ -287,6 +295,7 @@ export default class DragSlider {
287
295
 
288
296
  const ABS_DELTA_X = Math.abs( this.#deltaMove.deltaX );
289
297
 
298
+ // If move a bit on right or left, juste move by one item only
290
299
  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 ) ) {
291
300
  if ( this.#deltaMove.deltaX < 0 ) {
292
301
  snapItem = this.#getFirstNextItem( this.#deltaMove.x );
@@ -303,7 +312,7 @@ export default class DragSlider {
303
312
  return;
304
313
  }
305
314
 
306
- this.#snapToItemAnimation( snapItem.snapItem, snapItem.snapToEnd );
315
+ this.#snapToItemAnimation( snapItem.snapItem );
307
316
  }
308
317
 
309
318
 
@@ -435,29 +444,46 @@ export default class DragSlider {
435
444
 
436
445
 
437
446
  next(): gsap.core.Tween | void {
438
- if ( !this.#isDraggingActive || ( this.#currentSnapItem as FLib.DragSlider.Item ).isLast ) {
447
+ const CURRENT_ITEM = this.#currentSnapItem as FLib.DragSlider.Item;
448
+
449
+ if ( !this.#isDraggingActive || !this.#itemArray[ CURRENT_ITEM.index + 1 ] ) {
439
450
  return;
440
451
  }
441
452
 
442
- return this.#snapToItemAnimation( this.#itemArray[ ( this.#currentSnapItem as FLib.DragSlider.Item ).index + 1 ] );
453
+ return this.#snapToItemAnimation( this.#itemArray[ CURRENT_ITEM.index + 1 ] );
443
454
  }
444
455
 
445
456
 
446
457
  previous(): gsap.core.Tween | void {
447
- if ( !this.#isDraggingActive || ( this.#currentSnapItem as FLib.DragSlider.Item ).isFirst ) {
458
+ const CURRENT_ITEM = this.#currentSnapItem as FLib.DragSlider.Item;
459
+
460
+ if ( !this.#isDraggingActive || CURRENT_ITEM.isFirst ) {
448
461
  return;
449
462
  }
450
463
 
451
- return this.#snapToItemAnimation( this.#itemArray[ ( this.#currentSnapItem as FLib.DragSlider.Item ).index - 1 ] );
464
+ return this.#snapToItemAnimation( this.#itemArray[ CURRENT_ITEM.index - 1 ] );
452
465
  }
453
466
 
454
467
 
455
- goToItem( $block: HTMLElement ): gsap.core.Tween | void {
468
+ goToItem( blockOrIndex: HTMLElement | number ): gsap.core.Tween | void {
456
469
 
457
470
  if ( !this.#isDraggingActive ) {
458
471
  return;
459
472
  }
460
473
 
474
+ let $block;
475
+
476
+ if (typeof blockOrIndex === 'number') {
477
+ $block = this.#$items?.[blockOrIndex] as HTMLElement;
478
+ }
479
+ else {
480
+ $block = blockOrIndex;
481
+ }
482
+
483
+ if ( !$block ) {
484
+ return;
485
+ }
486
+
461
487
  const ITEM = this.#itemMap.get( $block );
462
488
 
463
489
  if ( !ITEM ) {
@@ -474,7 +500,7 @@ export default class DragSlider {
474
500
 
475
501
  return gsap.to( this.#$list as HTMLElement, {
476
502
  "duration": 0.3,
477
- "x": -1 * ITEM.info.left + this.#siteOffset,
503
+ "x": -1 * ITEM.info.left + this.#siteOffsetLeft,
478
504
  "y": 0,
479
505
  "z": 0,
480
506
  "onUpdateParams": [ this ],
@@ -493,6 +519,11 @@ export default class DragSlider {
493
519
  } );
494
520
  }
495
521
 
522
+ refresh = (): this => {
523
+ this.#onResize();
524
+
525
+ return this;
526
+ }
496
527
 
497
528
  init = (): this => {
498
529
  if ( this.#isInitialized ) {
@@ -527,6 +558,8 @@ export default class DragSlider {
527
558
  return this;
528
559
  }
529
560
 
561
+ aClass( this.#$slider, 'is-active' );
562
+
530
563
  this.#onResize();
531
564
 
532
565
  on( window, {
@@ -562,8 +595,6 @@ export default class DragSlider {
562
595
  "isAtEnd": this.#deltaMove.x === this.#listDelta
563
596
  } );
564
597
 
565
- aClass( this.#$slider, 'is-active' );
566
-
567
598
  return this;
568
599
  }
569
600
 
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Front Library
2
2
 
3
- @version: 7.1.16
3
+ @version: 7.1.18
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.16",
5
+ "version": "7.1.18",
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": [],