123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- function setTimeout(instance, cb, time) {
- if (time > 0) {
- var s = getDate().getTime()
- var fn = function () {
- if (getDate().getTime() - s > time) {
- cb && cb()
- } else
- instance.requestAnimationFrame(fn)
- }
- fn()
- }
- else
- cb && cb()
- }
- function decideSwiperDirection(startTouches, currentTouches, vertical) {
-
- var toleranceShake = 150
-
- var toleranceTranslate = 10
-
- if (!vertical) {
-
- if (Math.abs(currentTouches.y - startTouches.y) <= toleranceShake) {
-
- if (Math.abs(currentTouches.x - startTouches.x) > toleranceTranslate) {
- if (currentTouches.x - startTouches.x > 0) {
- return 'right'
- } else if (currentTouches.x - startTouches.x < 0) {
- return 'left'
- }
- }
- }
- } else {
-
- if (Math.abs(currentTouches.x - startTouches.x) <= toleranceShake) {
-
- if (Math.abs(currentTouches.y - startTouches.y) > toleranceTranslate) {
- if (currentTouches.y - startTouches.y > 0) {
- return 'down'
- } else if (currentTouches.y - startTouches.y < 0) {
- return 'up'
- }
- }
- }
- }
- return ''
- }
- var itemDataObserver = function(newVal, oldVal, ownerInstance, instance) {
- if (!newVal || newVal === 'undefined') return
- var state = ownerInstance.getState()
- state.itemData = newVal
- }
- var currentIndexObserver = function(newVal, oldVal, ownerInstance, instance) {
- if ((!newVal && newVal != 0) || newVal === 'undefined') return
- var state = ownerInstance.getState()
- state.currentIndex = newVal
- }
- var containerDataObserver = function(newVal, oldVal, ownerInstance, instance) {
- if (!newVal || newVal === 'undefined') return
- var state = ownerInstance.getState()
- state.containerData = newVal
- }
- var touchStart = function(event, ownerInstance) {
- console.log('touchStart');
- var instance = event.instance
- var dataset = instance.getDataset()
- var state = ownerInstance.getState()
- var itemData = state.itemData
- var containerData = state.containerData
-
-
- if (!state.currentIndex || state.currentIndex === 'undefined') {
- state.currentIndex = 0
- }
-
- if (!containerData || containerData.circular === 'undefined') {
- containerData.circular = false
- }
- state.containerData = containerData
-
-
-
- if (!containerData.animationFinish) {
- ownerInstance.callMethod('changeParentSwiperContainerStyleStatus',{
- status: 'reload'
- })
- }
-
- if (itemData.index != state.currentIndex) return
-
- var touches = event.changedTouches[0]
- if (!touches) return
-
-
- state.touchStartTime = getDate().getTime()
-
-
- state.touchRelactive = {
- x: touches.pageX,
- y: touches.pageY
- }
-
- state.touchId = touches.identifier
-
-
- state.touching = true
- ownerInstance.callMethod('updateTouchingStatus', {
- status: true
- })
- }
- var touchMove = function(event, ownerInstance) {
- console.log('touchMove');
- var instance = event.instance
- var dataset = instance.getDataset()
- var state = ownerInstance.getState()
- var itemData = state.itemData
- var containerData = state.containerData
-
-
- if (itemData.index != state.currentIndex) return
-
-
- if (!state.touching) return
-
- var touches = event.changedTouches[0]
- if (!touches) return
-
- if (state.touchId != touches.identifier) return
-
- var currentTouchRelactive = {
- x: touches.pageX,
- y: touches.pageY
- }
-
-
- if (containerData.vertical) {
- var touchDistance = currentTouchRelactive.y - state.touchRelactive.y
- var itemHeight = itemData.itemHeight
- var distanceRate = touchDistance / itemHeight
-
-
-
- if (!containerData.circular &&
- ((state.currentIndex === 0 && touchDistance > 0) || (state.currentIndex === containerData.swiperItemLength - 1 && touchDistance < 0))
- ) {
- return
- }
-
-
- if((Math.abs(touchDistance) > (itemData.itemTop + itemData.itemHeight))) {
- ownerInstance.callMethod('updateParentSwiperContainerStyle', {
- value: distanceRate < 0 ? -1 : 1
- })
- return
- }
- } else {
- var touchDistance = currentTouchRelactive.x - state.touchRelactive.x
- var itemWidth = itemData.itemWidth
- var distanceRate = touchDistance / itemWidth
-
-
-
- if (!containerData.circular &&
- ((state.currentIndex === 0 && touchDistance > 0) || (state.currentIndex === containerData.swiperItemLength - 1 && touchDistance < 0))
- ) {
- return
- }
-
-
- if((Math.abs(touchDistance) > (itemData.itemLeft + itemData.itemWidth))) {
- ownerInstance.callMethod('updateParentSwiperContainerStyle', {
- value: distanceRate < 0 ? -1 : 1
- })
- return
- }
- }
-
- ownerInstance.callMethod('updateParentSwiperContainerStyle', {
- value: distanceRate
- })
- }
- var touchEnd = function(event, ownerInstance) {
- console.log('touchEnd');
- var instance = event.instance
- var dataset = instance.getDataset()
- var state = ownerInstance.getState()
- var itemData = state.itemData
- var containerData = state.containerData
-
-
- if (itemData.index != state.currentIndex) return
-
-
- if (!state.touching) return
-
- var touches = event.changedTouches[0]
- if (!touches) return
-
- if (state.touchId != touches.identifier) return
-
-
- var currentTime = getDate().getTime()
- var currentTouchRelactive = {
- x: touches.pageX,
- y: touches.pageY
- }
-
- if (containerData.vertical) {
-
- var direction = decideSwiperDirection(state.touchRelactive, currentTouchRelactive, true)
-
- if (containerData.circular ||
- !((state.currentIndex === 0 && direction === 'down') || (state.currentIndex === containerData.swiperItemLength - 1 && direction === 'up'))
- ) {
-
-
- if ((currentTime - state.touchStartTime) > 200 && Math.abs(currentTouchRelactive.y - state.touchRelactive.y) < itemData.itemHeight / 2) {
- ownerInstance.callMethod('changeParentSwiperContainerStyleStatus',{
- status: 'reset'
- })
- } else {
-
-
- ownerInstance.callMethod('updateParentSwiperContainerStyleWithDirection', {
- direction: direction
- })
- }
- }
- } else {
-
- var direction = decideSwiperDirection(state.touchRelactive, currentTouchRelactive, false)
-
- if (containerData.circular ||
- !((state.currentIndex === 0 && direction === 'right') || (state.currentIndex === containerData.swiperItemLength - 1 && direction === 'left'))
- ) {
-
-
- if ((currentTime - state.touchStartTime) > 200 && Math.abs(currentTouchRelactive.x - state.touchRelactive.x) < itemData.itemWidth / 2) {
- ownerInstance.callMethod('changeParentSwiperContainerStyleStatus',{
- status: 'reset'
- })
- } else {
-
-
- ownerInstance.callMethod('updateParentSwiperContainerStyleWithDirection', {
- direction: direction
- })
- }
- }
- }
-
-
- state.touchId = null
- state.touchRelactive = null
- state.touchStartTime = 0
-
-
-
- state.touching = true
- ownerInstance.callMethod('updateTouchingStatus', {
- status: false
- })
- }
- module.exports = {
- itemDataObserver: itemDataObserver,
- currentIndexObserver: currentIndexObserver,
- containerDataObserver: containerDataObserver,
- touchStart: touchStart,
- touchMove: touchMove,
- touchEnd: touchEnd
- }
|