UNPKG

4.62 kBJavaScriptView Raw
1const extend = require('./utils/extend');
2const getOriginXY = require('./utils/getOriginXY');
3const defaults = require('./defaultOptions');
4const signals = require('./utils/Signals').new();
5
6class InteractEvent {
7 /** */
8 constructor (interaction, event, action, phase, element, related, preEnd = false) {
9 const target = interaction.target;
10 const deltaSource = (target && target.options || defaults).deltaSource;
11 const origin = getOriginXY(target, element, action);
12 const starting = phase === 'start';
13 const ending = phase === 'end';
14 const coords = starting? interaction.startCoords : interaction.curCoords;
15 const prevEvent = interaction.prevEvent;
16
17 element = element || interaction.element;
18
19 const page = extend({}, coords.page);
20 const client = extend({}, coords.client);
21
22 page.x -= origin.x;
23 page.y -= origin.y;
24
25 client.x -= origin.x;
26 client.y -= origin.y;
27
28 this.ctrlKey = event.ctrlKey;
29 this.altKey = event.altKey;
30 this.shiftKey = event.shiftKey;
31 this.metaKey = event.metaKey;
32 this.button = event.button;
33 this.buttons = event.buttons;
34 this.target = element;
35 this.currentTarget = element;
36 this.relatedTarget = related || null;
37 this.preEnd = preEnd;
38 this.type = action + (phase || '');
39 this.interaction = interaction;
40 this.interactable = target;
41
42 this.t0 = starting ? interaction.downTimes[interaction.downTimes.length - 1]
43 : prevEvent.t0;
44
45 const signalArg = {
46 interaction,
47 event,
48 action,
49 phase,
50 element,
51 related,
52 page,
53 client,
54 coords,
55 starting,
56 ending,
57 deltaSource,
58 iEvent: this,
59 };
60
61 signals.fire('set-xy', signalArg);
62
63 if (ending) {
64 // use previous coords when ending
65 this.pageX = prevEvent.pageX;
66 this.pageY = prevEvent.pageY;
67 this.clientX = prevEvent.clientX;
68 this.clientY = prevEvent.clientY;
69 }
70 else {
71 this.pageX = page.x;
72 this.pageY = page.y;
73 this.clientX = client.x;
74 this.clientY = client.y;
75 }
76
77 this.x0 = interaction.startCoords.page.x - origin.x;
78 this.y0 = interaction.startCoords.page.y - origin.y;
79 this.clientX0 = interaction.startCoords.client.x - origin.x;
80 this.clientY0 = interaction.startCoords.client.y - origin.y;
81
82 signals.fire('set-delta', signalArg);
83
84 this.timeStamp = coords.timeStamp;
85 this.dt = interaction.pointerDelta.timeStamp;
86 this.duration = this.timeStamp - this.t0;
87
88 // speed and velocity in pixels per second
89 this.speed = interaction.pointerDelta[deltaSource].speed;
90 this.velocityX = interaction.pointerDelta[deltaSource].vx;
91 this.velocityY = interaction.pointerDelta[deltaSource].vy;
92
93 this.swipe = (ending || phase === 'inertiastart')? this.getSwipe() : null;
94
95 signals.fire('new', signalArg);
96 }
97
98 getSwipe () {
99 const interaction = this.interaction;
100
101 if (interaction.prevEvent.speed < 600
102 || this.timeStamp - interaction.prevEvent.timeStamp > 150) {
103 return null;
104 }
105
106 let angle = 180 * Math.atan2(interaction.prevEvent.velocityY, interaction.prevEvent.velocityX) / Math.PI;
107 const overlap = 22.5;
108
109 if (angle < 0) {
110 angle += 360;
111 }
112
113 const left = 135 - overlap <= angle && angle < 225 + overlap;
114 const up = 225 - overlap <= angle && angle < 315 + overlap;
115
116 const right = !left && (315 - overlap <= angle || angle < 45 + overlap);
117 const down = !up && 45 - overlap <= angle && angle < 135 + overlap;
118
119 return {
120 up,
121 down,
122 left,
123 right,
124 angle,
125 speed: interaction.prevEvent.speed,
126 velocity: {
127 x: interaction.prevEvent.velocityX,
128 y: interaction.prevEvent.velocityY,
129 },
130 };
131 }
132
133 preventDefault () {}
134
135 /** */
136 stopImmediatePropagation () {
137 this.immediatePropagationStopped = this.propagationStopped = true;
138 }
139
140 /** */
141 stopPropagation () {
142 this.propagationStopped = true;
143 }
144}
145
146signals.on('set-delta', function ({ iEvent, interaction, starting, deltaSource }) {
147 const prevEvent = starting? iEvent : interaction.prevEvent;
148
149 if (deltaSource === 'client') {
150 iEvent.dx = iEvent.clientX - prevEvent.clientX;
151 iEvent.dy = iEvent.clientY - prevEvent.clientY;
152 }
153 else {
154 iEvent.dx = iEvent.pageX - prevEvent.pageX;
155 iEvent.dy = iEvent.pageY - prevEvent.pageY;
156 }
157});
158
159InteractEvent.signals = signals;
160
161module.exports = InteractEvent;