import * as SpriteSpin from '../core' ; ( ( ) => { const NAME = 'drag' ; interface DragState { startAt ? : number ; endAt ? : number ; frame ? : number ; lane ? : number ; wasPlaying ? : boolean ; minTime : number ; maxTime : number ; } function getState ( data ) { return SpriteSpin . getPluginState ( data , NAME ) as DragState ; } function getAxis ( data ) { if ( typeof data . orientation === 'number' ) { return data . orientation * Math . PI / 180 ; } if ( data . orientation === 'horizontal' ) { return 0 ; } return Math . PI / 2 ; } function onInit ( e , data ) { const state = getState ( data ) ; const d = [ 200 , 1500 ] ; const t = data . touchScrollTimer || d ; state . minTime = t [ 0 ] || d [ 0 ] ; state . maxTime = t [ 1 ] || d [ 1 ] ; } function dragStart ( e , data ) { const state = getState ( data ) ; if ( data . loading || SpriteSpin . is ( data , 'dragging' ) || data [ 'zoomPinFrame' ] && ! data . stage . is ( ':visible' ) ) { return ; } const now = new Date ( ) . getTime ( ) ; if ( state . endAt && ( now - state . endAt > state . maxTime ) ) { state . startAt = null ; state . endAt = null ; } if ( state . startAt && ( now - state . startAt > state . minTime ) ) { e . preventDefault ( ) ; } state . startAt = now ; state . wasPlaying = ! ! SpriteSpin . getPlaybackState ( data ) . handler ; state . frame = data . frame || 0 ; state . lane = data . lane || 0 ; SpriteSpin . flag ( data , 'dragging' , true ) ; SpriteSpin . updateInput ( e , data ) ; } function dragEnd ( e , data ) { if ( SpriteSpin . is ( data , 'dragging' ) ) { getState ( data ) . endAt = new Date ( ) . getTime ( ) ; SpriteSpin . flag ( data , 'dragging' , false ) ; SpriteSpin . resetInput ( data ) ; if ( data . retainAnimate && getState ( data ) . wasPlaying ) { SpriteSpin . startAnimation ( data ) ; } } } function drag ( e , data ) { const state = getState ( data ) ; const input = SpriteSpin . getInputState ( data ) ; if ( ! SpriteSpin . is ( data , 'dragging' ) ) { return ; } SpriteSpin . updateInput ( e , data ) ; const rad = getAxis ( data ) ; const sn = Math . sin ( rad ) ; const cs = Math . cos ( rad ) ; const x = ( ( input . nddX * cs - input . nddY * sn ) * data . sense ) || 0 ; const y = ( ( input . nddX * sn + input . nddY * cs ) * ( data . senseLane || data . sense ) ) || 0 ; state . frame += data . frames * x ; state . lane += data . lanes * y ; const oldFrame = data . frame ; const oldLane = data . lane ; SpriteSpin . updateFrame ( data , Math . floor ( state . frame ) , Math . floor ( state . lane ) ) ; SpriteSpin . stopAnimation ( data ) ; } function mousemove ( e , data ) { dragStart ( e , data ) ; drag ( e , data ) ; } SpriteSpin . registerPlugin ( 'drag' , { name : 'drag' , onInit : onInit , mousedown : dragStart , mousemove : drag , mouseup : dragEnd , documentmousemove : drag , documentmouseup : dragEnd , touchstart : dragStart , touchmove : drag , touchend : dragEnd , touchcancel : dragEnd } ) ; SpriteSpin . registerPlugin ( 'move' , { name : 'move' , onInit : onInit , mousemove : mousemove , mouseleave : dragEnd , touchstart : dragStart , touchmove : drag , touchend : dragEnd , touchcancel : dragEnd } ) ; } ) ( ) ;