Skip to content

Commit d620905

Browse files
authored
Merge pull request #14 from romellem/feature/adds-more-lifecycle-events
Feature - adds more lifecycle events
2 parents 9fdb331 + 1c4bf8a commit d620905

2 files changed

Lines changed: 70 additions & 5 deletions

File tree

README.md

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import Implosion from 'implosion';
2222

2323
let myImplosion = new Implosion({
2424
source: myNode,
25-
update(x, y) {
25+
onUpdate(x, y, previousX, previousY) {
2626
// whatever you want to do with the values
2727
}
2828
});
@@ -50,10 +50,28 @@ Implosion will register itself as an AMD module if it's available.
5050
<td>Element reference or query string for the target on which to listen for movement.</td>
5151
</tr>
5252
<tr>
53-
<th scope="row" align="left"><code>update</code> (required)</th>
53+
<th scope="row" align="left"><code>onStart</code></th>
5454
<td><code>function(x, y)</code></td>
5555
<td>-</td>
56-
<td>This function will be called with the updated <var>x</var> and <var>y</var> values.</td>
56+
<td>This function will be called when starting to drag the element</td>
57+
</tr>
58+
<tr>
59+
<th scope="row" align="left"><code>onUpdate</code> (required)</th>
60+
<td><code>function(x, y)</code></td>
61+
<td>-</td>
62+
<td>This function will be called with the updated <var>x</var> and <var>y</var> values. This configuration was renamed from `update`, and `update` has been deprecated, to be removed in the next major version.</td>
63+
</tr>
64+
<tr>
65+
<th scope="row" align="left"><code>onStartDecelerating</code></th>
66+
<td><code>function(x, y)</code></td>
67+
<td>-</td>
68+
<td>This function will be called when the deceleration begun (and drag has ended)</td>
69+
</tr>
70+
<tr>
71+
<th scope="row" align="left"><code>onEndDecelerating</code></th>
72+
<td><code>function(x, y)</code></td>
73+
<td>-</td>
74+
<td>This function will be called when the deceleration has ended</td>
5775
</tr>
5876
<tr>
5977
<th scope="row" align="left"><code>multiplier</code></th>
@@ -70,7 +88,7 @@ Implosion will register itself as an AMD module if it's available.
7088
<tr>
7189
<th scope="row" align="left"><code>initialValues</code></th>
7290
<td><code>[Number, Number]</code></td>
73-
<td><code>[0, 0]</code></td>
91+
<td><code>[0, 0]</code></td>
7492
<td>Array of initial <var>x</var> and <var>y</var> values.</td>
7593
</tr>
7694
<tr>

src/implosion.js

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ window.addEventListener('touchmove', function() {}, passiveSupported ? { passive
3939
export default class Implosion {
4040
constructor({
4141
source: sourceEl = document,
42-
update: updateCallback,
42+
update: updateCallbackDeprecated,
43+
onUpdate: updateCallback,
44+
onStart: startCallback,
45+
onStartDecelerating: startDeceleratingCallback,
46+
onEndDecelerating: endDeceleratingCallback,
4347
multiplier = 1,
4448
friction = 0.92,
4549
initialValues,
@@ -78,6 +82,14 @@ export default class Implosion {
7882
throw new Error('IMPETUS: source not found.');
7983
}
8084

85+
/**
86+
* Using the `update` configuration is deprecated, us the `onUpdate` key instead
87+
* @deprecated
88+
*/
89+
if (!updateCallback && updateCallbackDeprecated) {
90+
updateCallback = updateCallbackDeprecated;
91+
}
92+
8193
if (!updateCallback) {
8294
throw new Error('IMPETUS: update function not defined.');
8395
}
@@ -219,6 +231,36 @@ export default class Implosion {
219231
prevTargetY = targetY;
220232
}
221233

234+
/**
235+
* Executes the start function
236+
*/
237+
function callStartCallback() {
238+
if (!startCallback) {
239+
return;
240+
}
241+
startCallback.call(sourceEl, targetX, targetY, prevTargetX, prevTargetY);
242+
}
243+
244+
/**
245+
* Executes the start decelerating function
246+
*/
247+
function callStartDeceleratingCallback() {
248+
if (!startDeceleratingCallback) {
249+
return;
250+
}
251+
startDeceleratingCallback.call(sourceEl, targetX, targetY, prevTargetX, prevTargetY);
252+
}
253+
254+
/**
255+
* Executes the end decelerating function
256+
*/
257+
function callEndDeceleratingCallback() {
258+
if (!endDeceleratingCallback) {
259+
return;
260+
}
261+
endDeceleratingCallback.call(sourceEl, targetX, targetY, prevTargetX, prevTargetY);
262+
}
263+
222264
/**
223265
* Creates a custom normalized event object from touch and mouse events
224266
* @param {Event} ev
@@ -249,6 +291,7 @@ export default class Implosion {
249291
function onDown(ev) {
250292
let event = normalizeEvent(ev);
251293
if (!pointerActive && !paused) {
294+
callStartCallback();
252295
pointerActive = true;
253296
decelerating = false;
254297
pointerId = event.id;
@@ -419,9 +462,12 @@ export default class Implosion {
419462

420463
let diff = checkBounds();
421464

465+
callStartDeceleratingCallback();
422466
if (Math.abs(decVelX) > 1 || Math.abs(decVelY) > 1 || !diff.inBounds) {
423467
decelerating = true;
424468
requestAnimFrame(stepDecelAnim);
469+
} else {
470+
callEndDeceleratingCallback();
425471
}
426472
}
427473

@@ -485,6 +531,7 @@ export default class Implosion {
485531
requestAnimFrame(stepDecelAnim);
486532
} else {
487533
decelerating = false;
534+
callEndDeceleratingCallback();
488535
}
489536
}
490537
}

0 commit comments

Comments
 (0)