UNPKG

@baleada/listenable-gestures

Version:

A collection of gesture recognizers that are compatible with Baleada Logic's Listenable class.

74 lines (63 loc) 1.97 kB
import { emit, toEmitted, storeStartMetadata, storeMoveMetadata, isDefined } from '../util'; /* * pan is defined as a single touch that: * - starts at given point * - travels a distance greater than 0px (or a minimum distance of your choice) * - does not cancel or end */ var defaultOptions = { minDistance: 0 // TODO: research appropriate/accessible minDistance }; export default function pan() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var onStart = options.onStart, onMove = options.onMove, onCancel = options.onCancel, onEnd = options.onEnd, minDistance = isDefined(options.minDistance) ? options.minDistance : defaultOptions.minDistance; function touchstart(handlerApi) { var event = handlerApi.event, setMetadata = handlerApi.setMetadata; setMetadata({ path: 'touchTotal', value: event.touches.length }); storeStartMetadata(event, handlerApi, 'touch'); emit(onStart, toEmitted(handlerApi)); } function touchmove(handlerApi) { var event = handlerApi.event, getMetadata = handlerApi.getMetadata, denied = handlerApi.denied; if (getMetadata().touchTotal === 1) { storeMoveMetadata(event, handlerApi, 'touch'); recognize(handlerApi); } else { denied(); } emit(onMove, toEmitted(handlerApi)); } function recognize(_ref) { var getMetadata = _ref.getMetadata, recognized = _ref.recognized; if (getMetadata().distance.fromStart >= minDistance) { recognized(); } } function touchcancel(handlerApi) { var denied = handlerApi.denied; denied(); emit(onCancel, toEmitted(handlerApi)); } function touchend(handlerApi) { var denied = handlerApi.denied; denied(); emit(onEnd, toEmitted(handlerApi)); } return { touchstart: touchstart, touchmove: touchmove, touchcancel: touchcancel, touchend: touchend }; }