9
9
isTouchDevice
10
10
} from './utilities' ;
11
11
12
+ declare var global : any ;
13
+
12
14
/**
13
15
* @param {number } fromX
14
16
* @param {number } fromY
@@ -30,6 +32,150 @@ const computeDirection = (fromX, fromY, allowedDirections) => {
30
32
return direction ;
31
33
} ;
32
34
35
+ /**
36
+ * Prepend element to the parentNode.
37
+ *
38
+ * This makes the element last among the siblings.
39
+ *
40
+ * Invoked when card is added to the stack (when prepend is true).
41
+ *
42
+ * @param {HTMLElement } element The target element.
43
+ * @return {undefined }
44
+ */
45
+ const prependToParent = ( element ) => {
46
+ const parentNode = element . parentNode ;
47
+
48
+ parentNode . removeChild ( element ) ;
49
+ parentNode . insertBefore ( element , parentNode . firstChild ) ;
50
+ } ;
51
+
52
+ /**
53
+ * Append element to the parentNode.
54
+ *
55
+ * This makes the element first among the siblings. The reason for using
56
+ * this as opposed to zIndex is to allow CSS selector :nth-child.
57
+ *
58
+ * Invoked in the event of mousedown.
59
+ * Invoked when card is added to the stack.
60
+ *
61
+ * @param {HTMLElement } element The target element.
62
+ * @returns {undefined }
63
+ */
64
+ const appendToParent = ( element ) => {
65
+ const parentNode = element . parentNode ;
66
+ const siblings = elementChildren ( parentNode ) ;
67
+ const targetIndex = siblings . indexOf ( element ) ;
68
+
69
+ if ( targetIndex + 1 !== siblings . length ) {
70
+ parentNode . removeChild ( element ) ;
71
+ parentNode . appendChild ( element ) ;
72
+ }
73
+ } ;
74
+
75
+ /**
76
+ * Uses CSS transform to translate element position and rotation.
77
+ *
78
+ * Invoked in the event of `dragmove` and every time the physics solver is triggered.
79
+ *
80
+ * @param {HTMLElement } element
81
+ * @param {number } coordinateX Horizontal offset from the startDrag.
82
+ * @param {number } coordinateY Vertical offset from the startDrag.
83
+ * @param {number } rotation
84
+ * @returns {undefined }
85
+ */
86
+ const transform = ( element , coordinateX , coordinateY , rotation ) => {
87
+ element . style [ vendorPrefix ( 'transform' ) ] = 'translate3d(0, 0, 0) translate(' + coordinateX + 'px, ' + coordinateY + 'px) rotate(' + rotation + 'deg)' ;
88
+ } ;
89
+
90
+ /**
91
+ * Returns a value between 0 and 1 indicating the completeness of the throw out condition.
92
+ *
93
+ * Ration of the absolute distance from the original card position and element width.
94
+ *
95
+ * @param {number } xOffset Distance from the dragStart.
96
+ * @param {number } yOffset Distance from the dragStart.
97
+ * @param {HTMLElement } element Element.
98
+ * @returns {number }
99
+ */
100
+ const throwOutConfidence = ( xOffset , yOffset , element ) => {
101
+ const xConfidence = Math . min ( Math . abs ( xOffset ) / element . offsetWidth , 1 ) ;
102
+ const yConfidence = Math . min ( Math . abs ( yOffset ) / element . offsetHeight , 1 ) ;
103
+
104
+ return Math . max ( xConfidence , yConfidence ) ;
105
+ } ;
106
+
107
+ /**
108
+ * Determines if element is being thrown out of the stack.
109
+ *
110
+ * Element is considered to be thrown out when throwOutConfidence is equal to 1.
111
+ *
112
+ * @param {number } xOffset Distance from the dragStart.
113
+ * @param {number } yOffset Distance from the dragStart.
114
+ * @param {HTMLElement } element Element.
115
+ * @param {number } throwOutConfidence config.throwOutConfidence
116
+ * @returns {boolean }
117
+ */
118
+ const isThrowOut = ( xOffset , yOffset , element , throwOutConfidence ) => {
119
+ return throwOutConfidence === 1 ;
120
+ } ;
121
+
122
+ /**
123
+ * Calculates a distances at which the card is thrown out of the stack.
124
+ *
125
+ * @param {number } min
126
+ * @param {number } max
127
+ * @returns {number }
128
+ */
129
+ const throwOutDistance = ( min , max ) => {
130
+ return _ . random ( min , max ) ;
131
+ } ;
132
+
133
+ /**
134
+ * Calculates rotation based on the element x and y offset, element width and maxRotation variables.
135
+ *
136
+ * @param {number } coordinateX Horizontal offset from the startDrag.
137
+ * @param {number } coordinateY Vertical offset from the startDrag.
138
+ * @param {HTMLElement } element Element.
139
+ * @param {number } maxRotation
140
+ * @returns {number } Rotation angle expressed in degrees.
141
+ */
142
+ const rotation = ( coordinateX , coordinateY , element , maxRotation ) => {
143
+ const horizontalOffset = Math . min ( Math . max ( coordinateX / element . offsetWidth , - 1 ) , 1 ) ;
144
+ const verticalOffset = ( coordinateY > 0 ? 1 : - 1 ) * Math . min ( Math . abs ( coordinateY ) / 100 , 1 ) ;
145
+ const calculatedRotation = horizontalOffset * verticalOffset * maxRotation ;
146
+
147
+ return calculatedRotation ;
148
+ } ;
149
+
150
+ const THROW_IN = 'in' ;
151
+ const THROW_OUT = 'out' ;
152
+
153
+ /**
154
+ * Creates a configuration object.
155
+ *
156
+ * @param {Object } config
157
+ * @returns {Object }
158
+ */
159
+ const makeConfig = ( config = { } ) => {
160
+ const defaultConfig = {
161
+ allowedDirections : [
162
+ Direction . RIGHT ,
163
+ Direction . LEFT ,
164
+ Direction . UP
165
+ ] ,
166
+ isThrowOut : isThrowOut ,
167
+ maxRotation : 20 ,
168
+ maxThrowOutDistance : 500 ,
169
+ minThrowOutDistance : 400 ,
170
+ rotation : rotation ,
171
+ throwOutConfidence : throwOutConfidence ,
172
+ throwOutDistance : throwOutDistance ,
173
+ transform : transform
174
+ } ;
175
+
176
+ return _ . assign ( { } , defaultConfig , config ) ;
177
+ } ;
178
+
33
179
/**
34
180
* @param {Stack } stack
35
181
* @param {HTMLElement } targetElement
@@ -60,7 +206,7 @@ const Card = (stack, targetElement, prepend) => {
60
206
61
207
const construct = ( ) => {
62
208
card = { } ;
63
- config = Card . makeConfig ( stack . getConfig ( ) ) ;
209
+ config = makeConfig ( stack . getConfig ( ) ) ;
64
210
eventEmitter = Sister ( ) ;
65
211
springSystem = stack . getSpringSystem ( ) ;
66
212
springThrowIn = springSystem . createSpring ( 250 , 10 ) ;
@@ -98,13 +244,13 @@ const Card = (stack, targetElement, prepend) => {
98
244
} ) ;
99
245
100
246
if ( prepend ) {
101
- Card . prependToParent ( targetElement ) ;
247
+ prependToParent ( targetElement ) ;
102
248
} else {
103
- Card . appendToParent ( targetElement ) ;
249
+ appendToParent ( targetElement ) ;
104
250
}
105
251
106
252
eventEmitter . on ( 'panstart' , ( ) => {
107
- Card . appendToParent ( targetElement ) ;
253
+ appendToParent ( targetElement ) ;
108
254
109
255
eventEmitter . trigger ( 'dragstart' , {
110
256
target : targetElement
@@ -304,7 +450,7 @@ const Card = (stack, targetElement, prepend) => {
304
450
} ;
305
451
306
452
/**
307
- * @param {Card. THROW_IN|Card. THROW_OUT } where
453
+ * @param {THROW_IN|THROW_OUT } where
308
454
* @param {number } fromX
309
455
* @param {number } fromY
310
456
* @param {Direction } [direction]
@@ -317,14 +463,14 @@ const Card = (stack, targetElement, prepend) => {
317
463
// If direction argument is not set, compute it from coordinates.
318
464
lastThrow . direction = direction || computeDirection ( fromX , fromY , config . allowedDirections ) ;
319
465
320
- if ( where === Card . THROW_IN ) {
466
+ if ( where === THROW_IN ) {
321
467
springThrowIn . setCurrentValue ( 0 ) . setAtRest ( ) . setEndValue ( 1 ) ;
322
468
323
469
eventEmitter . trigger ( 'throwin' , {
324
470
target : targetElement ,
325
471
throwDirection : lastThrow . direction
326
472
} ) ;
327
- } else if ( where === Card . THROW_OUT ) {
473
+ } else if ( where === THROW_OUT ) {
328
474
springThrowOut . setCurrentValue ( 0 ) . setAtRest ( ) . setVelocity ( 100 ) . setEndValue ( 1 ) ;
329
475
330
476
eventEmitter . trigger ( 'throwout' , {
@@ -360,7 +506,7 @@ const Card = (stack, targetElement, prepend) => {
360
506
* @returns {undefined }
361
507
*/
362
508
card . throwIn = ( coordinateX , coordinateY , direction ) => {
363
- throwWhere ( Card . THROW_IN , coordinateX , coordinateY , direction ) ;
509
+ throwWhere ( THROW_IN , coordinateX , coordinateY , direction ) ;
364
510
} ;
365
511
366
512
/**
@@ -372,7 +518,7 @@ const Card = (stack, targetElement, prepend) => {
372
518
* @returns {undefined }
373
519
*/
374
520
card . throwOut = ( coordinateX , coordinateY , direction ) => {
375
- throwWhere ( Card . THROW_OUT , coordinateX , coordinateY , direction ) ;
521
+ throwWhere ( THROW_OUT , coordinateX , coordinateY , direction ) ;
376
522
} ;
377
523
378
524
/**
@@ -392,148 +538,4 @@ const Card = (stack, targetElement, prepend) => {
392
538
return card ;
393
539
} ;
394
540
395
- /**
396
- * Creates a configuration object.
397
- *
398
- * @param {Object } config
399
- * @returns {Object }
400
- */
401
- Card . makeConfig = ( config = { } ) => {
402
- const defaultConfig = {
403
- allowedDirections : [
404
- Direction . RIGHT ,
405
- Direction . LEFT ,
406
- Direction . UP
407
- ] ,
408
- isThrowOut : Card . isThrowOut ,
409
- maxRotation : 20 ,
410
- maxThrowOutDistance : 500 ,
411
- minThrowOutDistance : 400 ,
412
- rotation : Card . rotation ,
413
- throwOutConfidence : Card . throwOutConfidence ,
414
- throwOutDistance : Card . throwOutDistance ,
415
- transform : Card . transform
416
- } ;
417
-
418
- return _ . assign ( { } , defaultConfig , config ) ;
419
- } ;
420
-
421
- /**
422
- * Uses CSS transform to translate element position and rotation.
423
- *
424
- * Invoked in the event of `dragmove` and every time the physics solver is triggered.
425
- *
426
- * @param {HTMLElement } element
427
- * @param {number } coordinateX Horizontal offset from the startDrag.
428
- * @param {number } coordinateY Vertical offset from the startDrag.
429
- * @param {number } rotation
430
- * @returns {undefined }
431
- */
432
- Card . transform = ( element , coordinateX , coordinateY , rotation ) => {
433
- element . style [ vendorPrefix ( 'transform' ) ] = 'translate3d(0, 0, 0) translate(' + coordinateX + 'px, ' + coordinateY + 'px) rotate(' + rotation + 'deg)' ;
434
- } ;
435
-
436
- /**
437
- * Append element to the parentNode.
438
- *
439
- * This makes the element first among the siblings. The reason for using
440
- * this as opposed to zIndex is to allow CSS selector :nth-child.
441
- *
442
- * Invoked in the event of mousedown.
443
- * Invoked when card is added to the stack.
444
- *
445
- * @param {HTMLElement } element The target element.
446
- * @returns {undefined }
447
- */
448
- Card . appendToParent = ( element ) => {
449
- const parentNode = element . parentNode ;
450
- const siblings = elementChildren ( parentNode ) ;
451
- const targetIndex = siblings . indexOf ( element ) ;
452
-
453
- if ( targetIndex + 1 !== siblings . length ) {
454
- parentNode . removeChild ( element ) ;
455
- parentNode . appendChild ( element ) ;
456
- }
457
- } ;
458
-
459
- /**
460
- * Prepend element to the parentNode.
461
- *
462
- * This makes the element last among the siblings.
463
- *
464
- * Invoked when card is added to the stack (when prepend is true).
465
- *
466
- * @param {HTMLElement } element The target element.
467
- * @return {undefined }
468
- */
469
- Card . prependToParent = ( element ) => {
470
- const parentNode = element . parentNode ;
471
-
472
- parentNode . removeChild ( element ) ;
473
- parentNode . insertBefore ( element , parentNode . firstChild ) ;
474
- } ;
475
-
476
- /**
477
- * Returns a value between 0 and 1 indicating the completeness of the throw out condition.
478
- *
479
- * Ration of the absolute distance from the original card position and element width.
480
- *
481
- * @param {number } xOffset Distance from the dragStart.
482
- * @param {number } yOffset Distance from the dragStart.
483
- * @param {HTMLElement } element Element.
484
- * @returns {number }
485
- */
486
- Card . throwOutConfidence = ( xOffset , yOffset , element ) => {
487
- const xConfidence = Math . min ( Math . abs ( xOffset ) / element . offsetWidth , 1 ) ;
488
- const yConfidence = Math . min ( Math . abs ( yOffset ) / element . offsetHeight , 1 ) ;
489
-
490
- return Math . max ( xConfidence , yConfidence ) ;
491
- } ;
492
-
493
- /**
494
- * Determines if element is being thrown out of the stack.
495
- *
496
- * Element is considered to be thrown out when throwOutConfidence is equal to 1.
497
- *
498
- * @param {number } xOffset Distance from the dragStart.
499
- * @param {number } yOffset Distance from the dragStart.
500
- * @param {HTMLElement } element Element.
501
- * @param {number } throwOutConfidence config.throwOutConfidence
502
- * @returns {boolean }
503
- */
504
- Card . isThrowOut = ( xOffset , yOffset , element , throwOutConfidence ) => {
505
- return throwOutConfidence === 1 ;
506
- } ;
507
-
508
- /**
509
- * Calculates a distances at which the card is thrown out of the stack.
510
- *
511
- * @param {number } min
512
- * @param {number } max
513
- * @returns {number }
514
- */
515
- Card . throwOutDistance = ( min , max ) => {
516
- return _ . random ( min , max ) ;
517
- } ;
518
-
519
- /**
520
- * Calculates rotation based on the element x and y offset, element width and maxRotation variables.
521
- *
522
- * @param {number } coordinateX Horizontal offset from the startDrag.
523
- * @param {number } coordinateY Vertical offset from the startDrag.
524
- * @param {HTMLElement } element Element.
525
- * @param {number } maxRotation
526
- * @returns {number } Rotation angle expressed in degrees.
527
- */
528
- Card . rotation = ( coordinateX , coordinateY , element , maxRotation ) => {
529
- const horizontalOffset = Math . min ( Math . max ( coordinateX / element . offsetWidth , - 1 ) , 1 ) ;
530
- const verticalOffset = ( coordinateY > 0 ? 1 : - 1 ) * Math . min ( Math . abs ( coordinateY ) / 100 , 1 ) ;
531
- const rotation = horizontalOffset * verticalOffset * maxRotation ;
532
-
533
- return rotation ;
534
- } ;
535
-
536
- Card . THROW_IN = 'in' ;
537
- Card . THROW_OUT = 'out' ;
538
-
539
- export default Card ;
541
+ export default Card ;
0 commit comments