-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathswing.ts
178 lines (154 loc) · 4.76 KB
/
swing.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// here the ambient definitions for the swing
// module are specified. Normally they should be at DefinitelyTyped
// or better with the repository
declare var require: any;
const Swing = require('swing');
export interface ThrowEvent {
/**
* The element being dragged.
*/
target: HTMLElement;
/**
* The direction in which the element is being dragged: Card.DIRECTION_LEFT
* or Card.DIRECTION_RIGHT
*/
throwDirection: Direction;
}
export interface DragEvent {
/**
* The element being dragged.
*/
target: HTMLElement;
/**
* Only available when the event is dragmove
*/
throwOutConfidence?: number;
/**
* Only available when the event is dragmove
*/
throwDirection?: Direction;
/**
* Only available when the event is dragmove
*/
offset?: number;
}
export type ThrowEventName = 'throwin' | 'throwinend' |
'throwout' | 'throwoutend' | 'throwoutleft' | 'throwoutup' | 'throwoutdown' | 'throwoutright';
export type DragEventName = 'dragstart' | 'dragmove' | 'dragend';
export interface Card {
/**
* Unbinds all Hammer.Manager events.
* Removes the listeners from the physics simulation.
*
* @return {undefined}
*/
destroy(): void;
/**
* Throws a card into the stack from an arbitrary position.
*
* @param {Number} fromX
* @param {Number} fromY
* @return {undefined}
*/
throwIn(x: number, y: number): void;
/**
* Throws a card out of the stack in the direction away from the original offset.
*
* @param {Number} fromX
* @param {Number} fromY
* @return {undefined}
*/
throwOut(x: number, y: number): void;
on(eventName: ThrowEventName, callabck: (event: ThrowEvent) => void): void;
on(eventName: DragEventName, callabck: (event: DragEvent) => void): void;
}
export interface Stack {
/**
* Creates an instance of Card and associates it with an element.
*
* @param {HTMLElement} element
* @param {boolean} prepend
* @return {Card}
*/
createCard(elment: HTMLElement, prepend?: boolean): void;
/**
* Returns an instance of Card associated with an element.
*
* @param {HTMLElement} element
* @return {Card|null}
*/
getCard(element: HTMLElement): Card;
/**
*
*@param {Card} card
*/
destroyCard(card: Card): void;
on(eventName: ThrowEventName, callabck: (event: ThrowEvent) => void): void;
on(eventName: DragEventName, callabck: (event: DragEvent) => void): void;
}
export interface StackConfig {
minThrowOutDistance?: number;
maxThrowOutDistance?: number;
maxRotation?: number;
allowedDirections?: Array<any>;
/**
* Determines if element is being thrown out of the stack.
*
* Element is considered to be thrown out when throwOutConfidence is equal to 1.
*
* @param {Number} offset Distance from the dragStart.
* @param {HTMLElement} element Element.
* @param {Number} throwOutConfidence config.throwOutConfidence
* @return {Boolean}
*/
isThrowOut?: (offset: number, element: HTMLElement, throwOutConfidence: number) => boolean;
/**
* Returns a value between 0 and 1 indicating the completeness of the throw out condition.
*
* Ration of the absolute distance from the original card position and element width.
*
* @param {Number} offsetX Distance from the dragStart.
* @param {Number} offsetY Distance from the dragStart.
* @param {HTMLElement} element Element.
* @return {Number}
*/
throwOutConfidence?: (offsetX: number, offsetY: number, element: HTMLElement) => number;
/**
* Calculates a distances at which the card is thrown out of the stack.
*
* @param {Number} min
* @param {Number} max
* @return {Number}
*/
throwOutDistance?: (min: number, max: number) => number;
/**
* Calculates rotation based on the element x and y offset, element width and
* maxRotation variables.
*
* @param {Number} x Horizontal offset from the startDrag.
* @param {Number} y Vertical offset from the startDrag.
* @param {HTMLElement} element Element.
* @param {Number} maxRotation
* @return {Number} Rotation angle expressed in degrees.
*/
rotation?: (x: number, y: number, element: HTMLElement, maxRotation: number) => number;
/**
* Uses CSS transform to translate element position and rotation.
*
* Invoked in the event of `dragmove` and every time the physics solver is triggered.
*
* @param {HTMLElement} element
* @param {Number} x Horizontal offset from the startDrag.
* @param {Number} y Vertical offset from the startDrag.
* @param {Number} r
* @return {undefined}
*/
transform?: (element: HTMLElement, x: number, y: number, r: number) => void;
}
export enum Direction {
DOWN = Swing.Direction.DOWN,
INVALID = Swing.Direction.INVALID,
LEFT = Swing.Direction.LEFT,
RIGHT = Swing.Direction.RIGHT,
UP = Swing.Direction.UP
}