Skip to content
This repository was archived by the owner on Aug 24, 2020. It is now read-only.

Commit 83b4a45

Browse files
To typescript file
1 parent 7aa6b05 commit 83b4a45

File tree

5 files changed

+165
-158
lines changed

5 files changed

+165
-158
lines changed

src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ export * from './angular2-swing/swing-card-component';
99

1010
export * from './angular2-swing/swing';
1111

12+
export * from './swing/card';
13+
export * from './swing/direction';
14+
export * from './swing/stack';
15+
export * from './swing/utilities';
16+
1217
@NgModule({
1318
imports: [
1419
CommonModule

src/swing/card.js renamed to src/swing/card.ts

Lines changed: 156 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {
99
isTouchDevice
1010
} from './utilities';
1111

12+
declare var global: any;
13+
1214
/**
1315
* @param {number} fromX
1416
* @param {number} fromY
@@ -30,6 +32,150 @@ const computeDirection = (fromX, fromY, allowedDirections) => {
3032
return direction;
3133
};
3234

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+
33179
/**
34180
* @param {Stack} stack
35181
* @param {HTMLElement} targetElement
@@ -60,7 +206,7 @@ const Card = (stack, targetElement, prepend) => {
60206

61207
const construct = () => {
62208
card = {};
63-
config = Card.makeConfig(stack.getConfig());
209+
config = makeConfig(stack.getConfig());
64210
eventEmitter = Sister();
65211
springSystem = stack.getSpringSystem();
66212
springThrowIn = springSystem.createSpring(250, 10);
@@ -98,13 +244,13 @@ const Card = (stack, targetElement, prepend) => {
98244
});
99245

100246
if (prepend) {
101-
Card.prependToParent(targetElement);
247+
prependToParent(targetElement);
102248
} else {
103-
Card.appendToParent(targetElement);
249+
appendToParent(targetElement);
104250
}
105251

106252
eventEmitter.on('panstart', () => {
107-
Card.appendToParent(targetElement);
253+
appendToParent(targetElement);
108254

109255
eventEmitter.trigger('dragstart', {
110256
target: targetElement
@@ -304,7 +450,7 @@ const Card = (stack, targetElement, prepend) => {
304450
};
305451

306452
/**
307-
* @param {Card.THROW_IN|Card.THROW_OUT} where
453+
* @param {THROW_IN|THROW_OUT} where
308454
* @param {number} fromX
309455
* @param {number} fromY
310456
* @param {Direction} [direction]
@@ -317,14 +463,14 @@ const Card = (stack, targetElement, prepend) => {
317463
// If direction argument is not set, compute it from coordinates.
318464
lastThrow.direction = direction || computeDirection(fromX, fromY, config.allowedDirections);
319465

320-
if (where === Card.THROW_IN) {
466+
if (where === THROW_IN) {
321467
springThrowIn.setCurrentValue(0).setAtRest().setEndValue(1);
322468

323469
eventEmitter.trigger('throwin', {
324470
target: targetElement,
325471
throwDirection: lastThrow.direction
326472
});
327-
} else if (where === Card.THROW_OUT) {
473+
} else if (where === THROW_OUT) {
328474
springThrowOut.setCurrentValue(0).setAtRest().setVelocity(100).setEndValue(1);
329475

330476
eventEmitter.trigger('throwout', {
@@ -360,7 +506,7 @@ const Card = (stack, targetElement, prepend) => {
360506
* @returns {undefined}
361507
*/
362508
card.throwIn = (coordinateX, coordinateY, direction) => {
363-
throwWhere(Card.THROW_IN, coordinateX, coordinateY, direction);
509+
throwWhere(THROW_IN, coordinateX, coordinateY, direction);
364510
};
365511

366512
/**
@@ -372,7 +518,7 @@ const Card = (stack, targetElement, prepend) => {
372518
* @returns {undefined}
373519
*/
374520
card.throwOut = (coordinateX, coordinateY, direction) => {
375-
throwWhere(Card.THROW_OUT, coordinateX, coordinateY, direction);
521+
throwWhere(THROW_OUT, coordinateX, coordinateY, direction);
376522
};
377523

378524
/**
@@ -392,148 +538,4 @@ const Card = (stack, targetElement, prepend) => {
392538
return card;
393539
};
394540

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;

src/swing/direction.js renamed to src/swing/direction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ const Direction = {
66
UP: Symbol('UP')
77
};
88

9-
export default Direction;
9+
export default Direction;

0 commit comments

Comments
 (0)