-
Notifications
You must be signed in to change notification settings - Fork 786
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pointerEvents: Add hold repeat events
interact(target) .pointerEvents({ holdRepeatInterval: 500, }) .on('hold', event => console.log(event.type, event.count)); Close #228
- Loading branch information
Showing
3 changed files
with
44 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters