123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- ( function( window, factory ) {
-
-
- if ( typeof define == 'function' && define.amd ) {
-
- define( [
- 'ev-emitter/ev-emitter'
- ], function( EvEmitter ) {
- return factory( window, EvEmitter );
- });
- } else if ( typeof module == 'object' && module.exports ) {
-
- module.exports = factory(
- window,
- require('ev-emitter')
- );
- } else {
-
- window.Unipointer = factory(
- window,
- window.EvEmitter
- );
- }
- }( window, function factory( window, EvEmitter ) {
- 'use strict';
- function noop() {}
- function Unipointer() {}
- var proto = Unipointer.prototype = Object.create( EvEmitter.prototype );
- proto.bindStartEvent = function( elem ) {
- this._bindStartEvent( elem, true );
- };
- proto.unbindStartEvent = function( elem ) {
- this._bindStartEvent( elem, false );
- };
- proto._bindStartEvent = function( elem, isAdd ) {
-
- isAdd = isAdd === undefined ? true : isAdd;
- var bindMethod = isAdd ? 'addEventListener' : 'removeEventListener';
-
- var startEvent = 'mousedown';
- if ( window.PointerEvent ) {
-
- startEvent = 'pointerdown';
- } else if ( 'ontouchstart' in window ) {
-
- startEvent = 'touchstart';
- }
- elem[ bindMethod ]( startEvent, this );
- };
- proto.handleEvent = function( event ) {
- var method = 'on' + event.type;
- if ( this[ method ] ) {
- this[ method ]( event );
- }
- };
- proto.getTouch = function( touches ) {
- for ( var i=0; i < touches.length; i++ ) {
- var touch = touches[i];
- if ( touch.identifier == this.pointerIdentifier ) {
- return touch;
- }
- }
- };
- proto.onmousedown = function( event ) {
-
- var button = event.button;
- if ( button && ( button !== 0 && button !== 1 ) ) {
- return;
- }
- this._pointerDown( event, event );
- };
- proto.ontouchstart = function( event ) {
- this._pointerDown( event, event.changedTouches[0] );
- };
- proto.onpointerdown = function( event ) {
- this._pointerDown( event, event );
- };
- proto._pointerDown = function( event, pointer ) {
-
-
- if ( event.button || this.isPointerDown ) {
- return;
- }
- this.isPointerDown = true;
-
- this.pointerIdentifier = pointer.pointerId !== undefined ?
-
- pointer.pointerId : pointer.identifier;
- this.pointerDown( event, pointer );
- };
- proto.pointerDown = function( event, pointer ) {
- this._bindPostStartEvents( event );
- this.emitEvent( 'pointerDown', [ event, pointer ] );
- };
- var postStartEvents = {
- mousedown: [ 'mousemove', 'mouseup' ],
- touchstart: [ 'touchmove', 'touchend', 'touchcancel' ],
- pointerdown: [ 'pointermove', 'pointerup', 'pointercancel' ],
- };
- proto._bindPostStartEvents = function( event ) {
- if ( !event ) {
- return;
- }
-
- var events = postStartEvents[ event.type ];
-
- events.forEach( function( eventName ) {
- window.addEventListener( eventName, this );
- }, this );
-
- this._boundPointerEvents = events;
- };
- proto._unbindPostStartEvents = function() {
-
- if ( !this._boundPointerEvents ) {
- return;
- }
- this._boundPointerEvents.forEach( function( eventName ) {
- window.removeEventListener( eventName, this );
- }, this );
- delete this._boundPointerEvents;
- };
- proto.onmousemove = function( event ) {
- this._pointerMove( event, event );
- };
- proto.onpointermove = function( event ) {
- if ( event.pointerId == this.pointerIdentifier ) {
- this._pointerMove( event, event );
- }
- };
- proto.ontouchmove = function( event ) {
- var touch = this.getTouch( event.changedTouches );
- if ( touch ) {
- this._pointerMove( event, touch );
- }
- };
- proto._pointerMove = function( event, pointer ) {
- this.pointerMove( event, pointer );
- };
- proto.pointerMove = function( event, pointer ) {
- this.emitEvent( 'pointerMove', [ event, pointer ] );
- };
- proto.onmouseup = function( event ) {
- this._pointerUp( event, event );
- };
- proto.onpointerup = function( event ) {
- if ( event.pointerId == this.pointerIdentifier ) {
- this._pointerUp( event, event );
- }
- };
- proto.ontouchend = function( event ) {
- var touch = this.getTouch( event.changedTouches );
- if ( touch ) {
- this._pointerUp( event, touch );
- }
- };
- proto._pointerUp = function( event, pointer ) {
- this._pointerDone();
- this.pointerUp( event, pointer );
- };
- proto.pointerUp = function( event, pointer ) {
- this.emitEvent( 'pointerUp', [ event, pointer ] );
- };
- proto._pointerDone = function() {
- this._pointerReset();
- this._unbindPostStartEvents();
- this.pointerDone();
- };
- proto._pointerReset = function() {
-
- this.isPointerDown = false;
- delete this.pointerIdentifier;
- };
- proto.pointerDone = noop;
- proto.onpointercancel = function( event ) {
- if ( event.pointerId == this.pointerIdentifier ) {
- this._pointerCancel( event, event );
- }
- };
- proto.ontouchcancel = function( event ) {
- var touch = this.getTouch( event.changedTouches );
- if ( touch ) {
- this._pointerCancel( event, touch );
- }
- };
- proto._pointerCancel = function( event, pointer ) {
- this._pointerDone();
- this.pointerCancel( event, pointer );
- };
- proto.pointerCancel = function( event, pointer ) {
- this.emitEvent( 'pointerCancel', [ event, pointer ] );
- };
- Unipointer.getPointerPoint = function( pointer ) {
- return {
- x: pointer.pageX,
- y: pointer.pageY
- };
- };
- return Unipointer;
- }));
|