Skip to content

Commit

Permalink
pointerEvents: Add hold repeat events
Browse files Browse the repository at this point in the history
interact(target)
.pointerEvents({
  holdRepeatInterval: 500,
})
.on('hold', event => console.log(event.type, event.count));

Close #228
  • Loading branch information
taye committed Nov 30, 2016
1 parent 572b001 commit fe11a8e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ require('./legacyBrowsers');

// pointerEvents
require('./pointerEvents');
require('./pointerEvents/holdRepeat');
require('./pointerEvents/interactableTargets');

// inertia
Expand Down
38 changes: 38 additions & 0 deletions src/pointerEvents/holdRepeat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const pointerEvents = require('./index.js');
const Interaction = require('../Interaction');

pointerEvents.signals.on('new', function ({ pointerEvent }) {
pointerEvent.count = (pointerEvent.count || 0) + 1;
});

pointerEvents.signals.on('fired', function ({ interaction, pointerEvent, eventTarget, targets }) {
if (pointerEvent.type !== 'hold') { return; }

// get the repeat interval from the first eventable
const interval = targets[0].eventable.options.holdRepeatInterval;

// don't repeat if the interval is 0 or less
if (interval <= 0) { return; }

// set a timeout to fire the holdrepeat event
interaction.holdIntervalHandle = setTimeout(function () {
pointerEvents.collectEventTargets(interaction, pointerEvent, pointerEvent, eventTarget, 'hold');
}, interval);
});

function endHoldRepeat ({ interaction }) {
// set the interaction's holdStopTime property
// to stop further holdRepeat events
if (interaction.holdIntervalHandle) {
clearInterval(interaction.holdIntervalHandle);
interaction.holdIntervalHandle = null;
}
}

for (const signal of ['move', 'up', 'cancel', 'endall']) {
Interaction.signals.on(signal, endHoldRepeat);
}

// don't repeat by default
pointerEvents.defaults.holdRepeatInterval = 0;
pointerEvents.types.push('holdrepeat');
7 changes: 5 additions & 2 deletions src/pointerEvents/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,11 @@ function firePointers (interaction, pointer, event, eventTarget, targets, eventT
}

const signalArg = {
interaction,
pointerEvent,
pointer,
event,
eventTarget,
targets,
};

Expand All @@ -93,6 +95,8 @@ function firePointers (interaction, pointer, event, eventTarget, targets, eventT
pointerEvent.clientX -= originX;
pointerEvent.clientY -= originY;

pointerEvent.eventable = target.eventable;

target.eventable.fire(pointerEvent);

pointerEvent.pageX += originX;
Expand Down Expand Up @@ -229,8 +233,7 @@ Interaction.signals.on('down', function ({ interaction, pointer, event, eventTar
browser.isIE8? eventCopy : pointer,
eventCopy,
eventTarget,
'hold',
minDuration);
'hold');
}, minDuration);
});

Expand Down

0 comments on commit fe11a8e

Please sign in to comment.