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

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
+
4
+ ## 7.1.17
5
+
6
+ * [DragSlider] Rework + add refresh function
7
+
3
8
  ## 7.1.16
4
9
 
5
10
  * 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;
@@ -45,13 +50,6 @@ export default class DragSlider {
45
50
  throw '[Drag Slider]: Missing at least one of viewportSelector, listSelector, itemSelector, dragClass';
46
51
  }
47
52
 
48
- this.#deltaMove = {
49
- "x": 0,
50
- "deltaX": 0,
51
- "deltaY": 0,
52
- "newX": 0
53
- };
54
-
55
53
  this.#$slider = $slider;
56
54
 
57
55
  this.#isDraggingActive = false;
@@ -84,8 +82,12 @@ export default class DragSlider {
84
82
  if ( !this.#$items || !this.#$list ) {
85
83
  return;
86
84
  }
87
- this.#viewportInfo = offset( this.#$viewport as HTMLElement );
88
- this.#siteOffset = parseInt( prop( (this.#$items[ 0 ] as HTMLElement), 'marginLeft' ), 10 );
85
+
86
+ const LAST_INDEX = this.#$items.length - 1;
87
+
88
+ this.#viewportInfo = offset( this.#$viewport as HTMLElement );
89
+ this.#siteOffsetLeft = parseInt( prop( (this.#$items[ 0 ] as HTMLElement), 'marginLeft' ), 10 );
90
+
89
91
  this.#listDelta = this.#viewportInfo.width - this.#$list.scrollWidth;
90
92
 
91
93
  const prevIsDraggingActive = this.#isDraggingActive;
@@ -108,23 +110,40 @@ export default class DragSlider {
108
110
  }
109
111
 
110
112
  this.#itemArray.length = 0;
113
+ const ABS_LIST_DELTA = Math.abs( this.#listDelta );
111
114
 
112
- const LAST_INDEX = this.#$items.length;
113
-
114
- this.#$items.forEach( ( $item: Node, index: number ) => {
115
- const $ITEM = $item as HTMLElement;
115
+ for (let index = 0; index < this.#$items.length; ++index) {
116
+ const $ITEM = this.#$items[ index ] as HTMLElement;
116
117
  const ITEM_OFFSET = offset( $ITEM, false, this.#$list );
117
118
 
119
+ if (ITEM_OFFSET.left - this.#siteOffsetLeft <= ABS_LIST_DELTA) {
120
+ this.#itemArray.push({
121
+ index,
122
+ "isFirst": index === 0,
123
+ "isLast": false,
124
+ "$item": $ITEM,
125
+ "info": ITEM_OFFSET
126
+ });
127
+
128
+ this.#itemMap.set( $ITEM, this.#itemArray[ index ] );
129
+ continue;
130
+ }
131
+
118
132
  this.#itemArray.push({
119
133
  index,
120
134
  "isFirst": index === 0,
121
- "isLast": index === LAST_INDEX - 1 || ITEM_OFFSET.left > Math.abs( this.#listDelta ),
135
+ "isLast": true,
122
136
  "$item": $ITEM,
123
- "info": ITEM_OFFSET
137
+ "info": {
138
+ ...ITEM_OFFSET,
139
+ "left": ABS_LIST_DELTA + this.#siteOffsetLeft,
140
+ "x": ABS_LIST_DELTA + this.#siteOffsetLeft
141
+ }
124
142
  });
125
-
126
143
  this.#itemMap.set( $ITEM, this.#itemArray[ index ] );
127
- } );
144
+
145
+ break;
146
+ }
128
147
 
129
148
  this.#firstItem = this.#itemMap.get( (this.#$items[ 0 ] as HTMLElement) ) as FLib.DragSlider.Item;
130
149
 
@@ -137,26 +156,26 @@ export default class DragSlider {
137
156
  }
138
157
 
139
158
 
140
- #snapToItemAnimation = ( snapItem: FLib.DragSlider.Item, snapToEnd?: FLib.DragSlider.Item ): gsap.core.Tween => {
141
- let finalX;
159
+ #snapToItemAnimation = ( snapItem: FLib.DragSlider.Item ): gsap.core.Tween | void => {
142
160
 
143
- if ( snapToEnd ) {
144
- finalX = this.#listDelta;
161
+ if (!snapItem) {
162
+ return;
145
163
  }
146
- else {
147
- finalX = -1 * snapItem.info.left + this.#siteOffset;
148
164
 
149
- finalX = Math.max( Math.min( 0, finalX ), this.#listDelta );
165
+ let finalX;
166
+
167
+ finalX = -1 * snapItem.info.left + this.#siteOffsetLeft;
150
168
 
151
- // If close to the end, then snap to it
152
- if ( Math.abs( finalX - this.#listDelta ) <= 3 ) {
153
- finalX = this.#listDelta;
154
- }
169
+ finalX = Math.max( Math.min( 0, finalX ), this.#listDelta );
155
170
 
156
- // If close to the start, then snap to it
157
- if ( Math.abs( finalX ) <= 3 ) {
158
- finalX = 0;
159
- }
171
+ // If close to the end, then snap to it
172
+ if ( Math.abs( finalX - this.#listDelta ) <= 3 ) {
173
+ finalX = this.#listDelta;
174
+ }
175
+
176
+ // If close to the start, then snap to it
177
+ if ( Math.abs( finalX ) <= 3 ) {
178
+ finalX = 0;
160
179
  }
161
180
 
162
181
  const IS_SNAP_TO_END = finalX === this.#listDelta;
@@ -216,7 +235,7 @@ export default class DragSlider {
216
235
 
217
236
 
218
237
  #getFirstNextItem = ( xPos: number ): { snapItem: FLib.DragSlider.Item, snapToEnd: boolean } => {
219
- let lastDelta, snapItem, snapToEnd;
238
+ let lastDelta, snapItem;
220
239
 
221
240
  const absXPos = Math.abs( xPos );
222
241
 
@@ -230,18 +249,9 @@ export default class DragSlider {
230
249
  break;
231
250
  }
232
251
 
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
252
  return {
243
- snapItem,
244
- snapToEnd
253
+ "snapItem": snapItem ? snapItem : this.#itemArray[ this.#itemArray.length - 1 ],
254
+ "snapToEnd": !snapItem
245
255
  };
246
256
  }
247
257
 
@@ -253,26 +263,12 @@ export default class DragSlider {
253
263
 
254
264
  for ( const item of this.#itemArray ) {
255
265
  const IS_LAST_DELTA = typeof lastDelta !== 'undefined';
256
- let newDelta = Math.abs( absXPos - item.info.left );
266
+ let newDelta = Math.abs( absXPos - item.info.left + this.#siteOffsetLeft );
257
267
 
258
- if ( !IS_LAST_DELTA || newDelta < lastDelta ) {
268
+ if ( !IS_LAST_DELTA || newDelta <= lastDelta ) {
259
269
  lastDelta = newDelta;
260
270
  snapItem = item;
261
271
  }
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
272
  }
277
273
 
278
274
  return {
@@ -287,6 +283,7 @@ export default class DragSlider {
287
283
 
288
284
  const ABS_DELTA_X = Math.abs( this.#deltaMove.deltaX );
289
285
 
286
+ // If move a bit on right or left, juste move by one item only
290
287
  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
288
  if ( this.#deltaMove.deltaX < 0 ) {
292
289
  snapItem = this.#getFirstNextItem( this.#deltaMove.x );
@@ -303,7 +300,7 @@ export default class DragSlider {
303
300
  return;
304
301
  }
305
302
 
306
- this.#snapToItemAnimation( snapItem.snapItem, snapItem.snapToEnd );
303
+ this.#snapToItemAnimation( snapItem.snapItem );
307
304
  }
308
305
 
309
306
 
@@ -435,20 +432,24 @@ export default class DragSlider {
435
432
 
436
433
 
437
434
  next(): gsap.core.Tween | void {
438
- if ( !this.#isDraggingActive || ( this.#currentSnapItem as FLib.DragSlider.Item ).isLast ) {
435
+ const CURRENT_ITEM = this.#currentSnapItem as FLib.DragSlider.Item;
436
+
437
+ if ( !this.#isDraggingActive || !this.#itemArray[ CURRENT_ITEM.index + 1 ] ) {
439
438
  return;
440
439
  }
441
440
 
442
- return this.#snapToItemAnimation( this.#itemArray[ ( this.#currentSnapItem as FLib.DragSlider.Item ).index + 1 ] );
441
+ return this.#snapToItemAnimation( this.#itemArray[ CURRENT_ITEM.index + 1 ] );
443
442
  }
444
443
 
445
444
 
446
445
  previous(): gsap.core.Tween | void {
447
- if ( !this.#isDraggingActive || ( this.#currentSnapItem as FLib.DragSlider.Item ).isFirst ) {
446
+ const CURRENT_ITEM = this.#currentSnapItem as FLib.DragSlider.Item;
447
+
448
+ if ( !this.#isDraggingActive || CURRENT_ITEM.isFirst ) {
448
449
  return;
449
450
  }
450
451
 
451
- return this.#snapToItemAnimation( this.#itemArray[ ( this.#currentSnapItem as FLib.DragSlider.Item ).index - 1 ] );
452
+ return this.#snapToItemAnimation( this.#itemArray[ CURRENT_ITEM.index - 1 ] );
452
453
  }
453
454
 
454
455
 
@@ -474,7 +475,7 @@ export default class DragSlider {
474
475
 
475
476
  return gsap.to( this.#$list as HTMLElement, {
476
477
  "duration": 0.3,
477
- "x": -1 * ITEM.info.left + this.#siteOffset,
478
+ "x": -1 * ITEM.info.left + this.#siteOffsetLeft,
478
479
  "y": 0,
479
480
  "z": 0,
480
481
  "onUpdateParams": [ this ],
@@ -493,6 +494,9 @@ export default class DragSlider {
493
494
  } );
494
495
  }
495
496
 
497
+ refresh = () => {
498
+ this.#onResize();
499
+ }
496
500
 
497
501
  init = (): this => {
498
502
  if ( this.#isInitialized ) {
@@ -527,6 +531,8 @@ export default class DragSlider {
527
531
  return this;
528
532
  }
529
533
 
534
+ aClass( this.#$slider, 'is-active' );
535
+
530
536
  this.#onResize();
531
537
 
532
538
  on( window, {
@@ -562,8 +568,6 @@ export default class DragSlider {
562
568
  "isAtEnd": this.#deltaMove.x === this.#listDelta
563
569
  } );
564
570
 
565
- aClass( this.#$slider, 'is-active' );
566
-
567
571
  return this;
568
572
  }
569
573
 
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Front Library
2
2
 
3
- @version: 7.1.16
3
+ @version: 7.1.17
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.17",
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": [],