@creative-web-solution/front-library 7.1.15 → 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 +10 -0
- package/Modules/DragSlider.ts +74 -65
- package/README.md +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/Modules/DragSlider.ts
CHANGED
|
@@ -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
|
-
#
|
|
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
|
-
|
|
88
|
-
|
|
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
|
-
|
|
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":
|
|
135
|
+
"isLast": true,
|
|
122
136
|
"$item": $ITEM,
|
|
123
|
-
"info":
|
|
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,21 +156,26 @@ export default class DragSlider {
|
|
|
137
156
|
}
|
|
138
157
|
|
|
139
158
|
|
|
140
|
-
#snapToItemAnimation =
|
|
159
|
+
#snapToItemAnimation = ( snapItem: FLib.DragSlider.Item ): gsap.core.Tween | void => {
|
|
160
|
+
|
|
161
|
+
if (!snapItem) {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
|
|
141
165
|
let finalX;
|
|
142
166
|
|
|
143
|
-
|
|
167
|
+
finalX = -1 * snapItem.info.left + this.#siteOffsetLeft;
|
|
168
|
+
|
|
169
|
+
finalX = Math.max( Math.min( 0, finalX ), this.#listDelta );
|
|
170
|
+
|
|
171
|
+
// If close to the end, then snap to it
|
|
172
|
+
if ( Math.abs( finalX - this.#listDelta ) <= 3 ) {
|
|
144
173
|
finalX = this.#listDelta;
|
|
145
174
|
}
|
|
146
|
-
else {
|
|
147
|
-
finalX = -1 * snapItem.info.left + this.#siteOffset;
|
|
148
175
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
if ( Math.abs( finalX - this.#listDelta ) <= 3 ) {
|
|
153
|
-
finalX = this.#listDelta;
|
|
154
|
-
}
|
|
176
|
+
// If close to the start, then snap to it
|
|
177
|
+
if ( Math.abs( finalX ) <= 3 ) {
|
|
178
|
+
finalX = 0;
|
|
155
179
|
}
|
|
156
180
|
|
|
157
181
|
const IS_SNAP_TO_END = finalX === this.#listDelta;
|
|
@@ -211,7 +235,7 @@ export default class DragSlider {
|
|
|
211
235
|
|
|
212
236
|
|
|
213
237
|
#getFirstNextItem = ( xPos: number ): { snapItem: FLib.DragSlider.Item, snapToEnd: boolean } => {
|
|
214
|
-
let lastDelta, snapItem
|
|
238
|
+
let lastDelta, snapItem;
|
|
215
239
|
|
|
216
240
|
const absXPos = Math.abs( xPos );
|
|
217
241
|
|
|
@@ -225,18 +249,9 @@ export default class DragSlider {
|
|
|
225
249
|
break;
|
|
226
250
|
}
|
|
227
251
|
|
|
228
|
-
const lastItem = this.#itemArray[ this.#itemArray.length - 1 ];
|
|
229
|
-
|
|
230
|
-
if ( !snapItem.isLast &&
|
|
231
|
-
Math.abs( absXPos + this.#viewportInfo.width - ( lastItem.info.left + lastItem.info.width ) ) < lastDelta
|
|
232
|
-
) {
|
|
233
|
-
snapItem = lastItem;
|
|
234
|
-
snapToEnd = true;
|
|
235
|
-
}
|
|
236
|
-
|
|
237
252
|
return {
|
|
238
|
-
snapItem,
|
|
239
|
-
snapToEnd
|
|
253
|
+
"snapItem": snapItem ? snapItem : this.#itemArray[ this.#itemArray.length - 1 ],
|
|
254
|
+
"snapToEnd": !snapItem
|
|
240
255
|
};
|
|
241
256
|
}
|
|
242
257
|
|
|
@@ -248,26 +263,12 @@ export default class DragSlider {
|
|
|
248
263
|
|
|
249
264
|
for ( const item of this.#itemArray ) {
|
|
250
265
|
const IS_LAST_DELTA = typeof lastDelta !== 'undefined';
|
|
251
|
-
let newDelta = Math.abs( absXPos - item.info.left );
|
|
266
|
+
let newDelta = Math.abs( absXPos - item.info.left + this.#siteOffsetLeft );
|
|
252
267
|
|
|
253
|
-
if ( !IS_LAST_DELTA || newDelta
|
|
268
|
+
if ( !IS_LAST_DELTA || newDelta <= lastDelta ) {
|
|
254
269
|
lastDelta = newDelta;
|
|
255
270
|
snapItem = item;
|
|
256
271
|
}
|
|
257
|
-
|
|
258
|
-
if ( !IS_LAST_DELTA ) {
|
|
259
|
-
continue;
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
if ( !snapItem.isLast && item.index === this.#itemMap.size - 1 ) {
|
|
263
|
-
newDelta = Math.abs( Math.abs( this.#deltaMove.x ) + this.#viewportInfo.width - ( item.info.left + item.info.width ) );
|
|
264
|
-
|
|
265
|
-
if ( newDelta < lastDelta ) {
|
|
266
|
-
lastDelta = newDelta;
|
|
267
|
-
snapItem = item;
|
|
268
|
-
snapToEnd = true;
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
272
|
}
|
|
272
273
|
|
|
273
274
|
return {
|
|
@@ -282,6 +283,7 @@ export default class DragSlider {
|
|
|
282
283
|
|
|
283
284
|
const ABS_DELTA_X = Math.abs( this.#deltaMove.deltaX );
|
|
284
285
|
|
|
286
|
+
// If move a bit on right or left, juste move by one item only
|
|
285
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 ) ) {
|
|
286
288
|
if ( this.#deltaMove.deltaX < 0 ) {
|
|
287
289
|
snapItem = this.#getFirstNextItem( this.#deltaMove.x );
|
|
@@ -298,7 +300,7 @@ export default class DragSlider {
|
|
|
298
300
|
return;
|
|
299
301
|
}
|
|
300
302
|
|
|
301
|
-
this.#snapToItemAnimation( snapItem.snapItem
|
|
303
|
+
this.#snapToItemAnimation( snapItem.snapItem );
|
|
302
304
|
}
|
|
303
305
|
|
|
304
306
|
|
|
@@ -430,20 +432,24 @@ export default class DragSlider {
|
|
|
430
432
|
|
|
431
433
|
|
|
432
434
|
next(): gsap.core.Tween | void {
|
|
433
|
-
|
|
435
|
+
const CURRENT_ITEM = this.#currentSnapItem as FLib.DragSlider.Item;
|
|
436
|
+
|
|
437
|
+
if ( !this.#isDraggingActive || !this.#itemArray[ CURRENT_ITEM.index + 1 ] ) {
|
|
434
438
|
return;
|
|
435
439
|
}
|
|
436
440
|
|
|
437
|
-
return this.#snapToItemAnimation( this.#itemArray[
|
|
441
|
+
return this.#snapToItemAnimation( this.#itemArray[ CURRENT_ITEM.index + 1 ] );
|
|
438
442
|
}
|
|
439
443
|
|
|
440
444
|
|
|
441
445
|
previous(): gsap.core.Tween | void {
|
|
442
|
-
|
|
446
|
+
const CURRENT_ITEM = this.#currentSnapItem as FLib.DragSlider.Item;
|
|
447
|
+
|
|
448
|
+
if ( !this.#isDraggingActive || CURRENT_ITEM.isFirst ) {
|
|
443
449
|
return;
|
|
444
450
|
}
|
|
445
451
|
|
|
446
|
-
return this.#snapToItemAnimation( this.#itemArray[
|
|
452
|
+
return this.#snapToItemAnimation( this.#itemArray[ CURRENT_ITEM.index - 1 ] );
|
|
447
453
|
}
|
|
448
454
|
|
|
449
455
|
|
|
@@ -469,7 +475,7 @@ export default class DragSlider {
|
|
|
469
475
|
|
|
470
476
|
return gsap.to( this.#$list as HTMLElement, {
|
|
471
477
|
"duration": 0.3,
|
|
472
|
-
"x": -1 * ITEM.info.left + this.#
|
|
478
|
+
"x": -1 * ITEM.info.left + this.#siteOffsetLeft,
|
|
473
479
|
"y": 0,
|
|
474
480
|
"z": 0,
|
|
475
481
|
"onUpdateParams": [ this ],
|
|
@@ -488,6 +494,9 @@ export default class DragSlider {
|
|
|
488
494
|
} );
|
|
489
495
|
}
|
|
490
496
|
|
|
497
|
+
refresh = () => {
|
|
498
|
+
this.#onResize();
|
|
499
|
+
}
|
|
491
500
|
|
|
492
501
|
init = (): this => {
|
|
493
502
|
if ( this.#isInitialized ) {
|
|
@@ -522,6 +531,8 @@ export default class DragSlider {
|
|
|
522
531
|
return this;
|
|
523
532
|
}
|
|
524
533
|
|
|
534
|
+
aClass( this.#$slider, 'is-active' );
|
|
535
|
+
|
|
525
536
|
this.#onResize();
|
|
526
537
|
|
|
527
538
|
on( window, {
|
|
@@ -557,8 +568,6 @@ export default class DragSlider {
|
|
|
557
568
|
"isAtEnd": this.#deltaMove.x === this.#listDelta
|
|
558
569
|
} );
|
|
559
570
|
|
|
560
|
-
aClass( this.#$slider, 'is-active' );
|
|
561
|
-
|
|
562
571
|
return this;
|
|
563
572
|
}
|
|
564
573
|
|
package/README.md
CHANGED
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.
|
|
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": [],
|