mirror of
https://github.com/flynx/PortableMag.git
synced 2025-10-29 19:20:09 +00:00
refactored the post-scroll callback into a seporate generic entity...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
1873e6a147
commit
5dfb9866ea
@ -113,22 +113,31 @@ $(document).ready(function(){
|
|||||||
function(k){console.log(k)}))
|
function(k){console.log(k)}))
|
||||||
|
|
||||||
|
|
||||||
window.CLICK_THRESHOLD = 10
|
|
||||||
window.SNAP_TO_PAGES_IN_RIBBON = false
|
window.SNAP_TO_PAGES_IN_RIBBON = false
|
||||||
window.INITIAL_TIME = 200
|
window.INITIAL_TIME = 200
|
||||||
window.LONG_CLICK_THRESHOLD = 400
|
window.INNERTIA_SCALE = 1
|
||||||
|
|
||||||
// XXX make this a default setup in the lib...
|
// XXX make this a default setup in the lib...
|
||||||
window.MagazineScroller = makeScrollHandler($('.viewer'), {
|
window.MagazineScroller = makeScrollHandler($('.viewer'), {
|
||||||
hScroll: true,
|
hScroll: true,
|
||||||
vScroll: false,
|
vScroll: false,
|
||||||
|
|
||||||
|
/*
|
||||||
// XXX the callback signature is a tad messy, revise...
|
// XXX the callback signature is a tad messy, revise...
|
||||||
// XXX need ability to dampen innertia, add some kind of resistance...
|
// XXX need ability to dampen innertia, add some kind of resistance...
|
||||||
callback: function(evt, speed, distance, duration, touches, state){
|
callback: function(data){
|
||||||
console.log(duration)
|
var evt = data.orig_event
|
||||||
|
var scroller = data.scroller
|
||||||
|
var root = scroller.root
|
||||||
|
var speed = data.speed
|
||||||
|
var distance = data.distance
|
||||||
|
var duration = data.duration
|
||||||
|
var touches = data.touches
|
||||||
|
var state = data.state
|
||||||
|
|
||||||
// canceling a scroll...
|
// canceling a scroll...
|
||||||
if(state == 'canceling' && togglePageView('?') == 'on'){
|
if(state == 'canceling' && togglePageView('?') == 'on'){
|
||||||
|
//root.trigger('userScrollCancelled')
|
||||||
setCurrentPage()
|
setCurrentPage()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -142,9 +151,12 @@ $(document).ready(function(){
|
|||||||
// click/tap...
|
// click/tap...
|
||||||
if(Math.abs(distance) < CLICK_THRESHOLD){
|
if(Math.abs(distance) < CLICK_THRESHOLD){
|
||||||
if(duration > LONG_CLICK_THRESHOLD){
|
if(duration > LONG_CLICK_THRESHOLD){
|
||||||
return alert('long click...')
|
//root.trigger('userLongClick')
|
||||||
}
|
}
|
||||||
// click...
|
// click...
|
||||||
|
//root.trigger('userClick')
|
||||||
|
|
||||||
|
// get page target and select it if it's within a page...
|
||||||
var target = $(evt.target)
|
var target = $(evt.target)
|
||||||
target = getPageNumber(
|
target = getPageNumber(
|
||||||
target.hasClass('page') ? target
|
target.hasClass('page') ? target
|
||||||
@ -164,9 +176,13 @@ $(document).ready(function(){
|
|||||||
if(!isNavigationViewRelative()){
|
if(!isNavigationViewRelative()){
|
||||||
var cur = $('.current.page')
|
var cur = $('.current.page')
|
||||||
if(distance >= CLICK_THRESHOLD){
|
if(distance >= CLICK_THRESHOLD){
|
||||||
|
//root.trigger('userSwipeRight')
|
||||||
|
|
||||||
setTransitionDuration(mag, INITIAL_TIME)
|
setTransitionDuration(mag, INITIAL_TIME)
|
||||||
return nextPage(cur)
|
return nextPage(cur)
|
||||||
} else if(distance <= -CLICK_THRESHOLD){
|
} else if(distance <= -CLICK_THRESHOLD){
|
||||||
|
//root.trigger('userSwipeLeft')
|
||||||
|
|
||||||
setTransitionDuration(mag, INITIAL_TIME)
|
setTransitionDuration(mag, INITIAL_TIME)
|
||||||
return prevPage(cur)
|
return prevPage(cur)
|
||||||
}
|
}
|
||||||
@ -178,6 +194,7 @@ $(document).ready(function(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
// innertial scroll...
|
// innertial scroll...
|
||||||
|
// XXX make this generic...
|
||||||
var t = INITIAL_TIME * (1+Math.abs(speed))
|
var t = INITIAL_TIME * (1+Math.abs(speed))
|
||||||
// XXX this is only horisontal at this point...
|
// XXX this is only horisontal at this point...
|
||||||
var at = getElementShift(mag).left
|
var at = getElementShift(mag).left
|
||||||
@ -188,18 +205,18 @@ $(document).ready(function(){
|
|||||||
// filter out really small speeds...
|
// filter out really small speeds...
|
||||||
if(Math.abs(speed) > 0.5){
|
if(Math.abs(speed) > 0.5){
|
||||||
// check bounds...
|
// check bounds...
|
||||||
|
// NOTE: need to cut the distance and time if we are going the
|
||||||
|
// hit the bounds...
|
||||||
if(to > first){
|
if(to > first){
|
||||||
// trim the time proportionally...
|
// trim the time proportionally...
|
||||||
var _t = t
|
var _t = t
|
||||||
t = Math.abs(t * ((at-first)/(at-to)))
|
t = Math.abs(t * ((at-first)/(at-to)))
|
||||||
console.log(_t, t)
|
|
||||||
to = first
|
to = first
|
||||||
setTransitionEasing(mag, 'linear')
|
setTransitionEasing(mag, 'linear')
|
||||||
} else if(to < last){
|
} else if(to < last){
|
||||||
// trim the time proportionally...
|
// trim the time proportionally...
|
||||||
var _t = t
|
var _t = t
|
||||||
t = Math.abs(t * ((at-last)/(at-to)))
|
t = Math.abs(t * ((at-last)/(at-to)))
|
||||||
console.log(_t, t)
|
|
||||||
to = last
|
to = last
|
||||||
setTransitionEasing(mag, 'linear')
|
setTransitionEasing(mag, 'linear')
|
||||||
|
|
||||||
@ -218,6 +235,7 @@ $(document).ready(function(){
|
|||||||
setTransitionDuration(mag, INITIAL_TIME)
|
setTransitionDuration(mag, INITIAL_TIME)
|
||||||
}, t+10)
|
}, t+10)
|
||||||
|
|
||||||
|
// check scroll bounds...
|
||||||
// do not let the user scroll out of view...
|
// do not let the user scroll out of view...
|
||||||
} else {
|
} else {
|
||||||
if(at > first){
|
if(at > first){
|
||||||
@ -234,8 +252,127 @@ $(document).ready(function(){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
*/
|
||||||
}).start()
|
}).start()
|
||||||
|
|
||||||
|
$('.viewer')
|
||||||
|
.on('scrollCancelled', function(){
|
||||||
|
setCurrentPage()
|
||||||
|
})
|
||||||
|
.on('shortClick', function(evt, data){
|
||||||
|
// get page target and select it if it's within a page...
|
||||||
|
var target = $(data.orig_event.target)
|
||||||
|
target = getPageNumber(
|
||||||
|
target.hasClass('page') ? target
|
||||||
|
: target.parents('.page'))
|
||||||
|
if(target != -1){
|
||||||
|
togglePageView()
|
||||||
|
setCurrentPage(target)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.on('longClick', function(evt, data){
|
||||||
|
})
|
||||||
|
.on('swipeLeft', function(evt, data){
|
||||||
|
// ribbon mode...
|
||||||
|
if(isNavigationViewRelative()){
|
||||||
|
setTimeout(function(){
|
||||||
|
data.scroller.root.trigger('screenReleased', data)}, 2)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// full page view...
|
||||||
|
var cur = $('.current.page')
|
||||||
|
var mag = $('.magazine')
|
||||||
|
setTransitionDuration(mag, INITIAL_TIME)
|
||||||
|
|
||||||
|
prevPage(cur)
|
||||||
|
})
|
||||||
|
.on('swipeRight', function(evt, data){
|
||||||
|
// ribbon mode...
|
||||||
|
if(isNavigationViewRelative()){
|
||||||
|
setTimeout(function(){
|
||||||
|
data.scroller.root.trigger('screenReleased', data)}, 2)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// full page view...
|
||||||
|
var cur = $('.current.page')
|
||||||
|
var mag = $('.magazine')
|
||||||
|
setTransitionDuration(mag, INITIAL_TIME)
|
||||||
|
|
||||||
|
nextPage(cur)
|
||||||
|
})
|
||||||
|
|
||||||
|
// do snap and innertia...
|
||||||
|
.on('screenReleased', function(evt, data){
|
||||||
|
// this makes things snap...
|
||||||
|
if(SNAP_TO_PAGES_IN_RIBBON){
|
||||||
|
setCurrentPage()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var speed = data.speed
|
||||||
|
var pages = $('.page')
|
||||||
|
var mag = $('.magazine')
|
||||||
|
// innertial scroll...
|
||||||
|
// XXX make this generic...
|
||||||
|
var t = INITIAL_TIME * (1+Math.abs(speed))
|
||||||
|
// XXX this is only horisontal at this point...
|
||||||
|
var at = getElementShift(mag).left
|
||||||
|
var to = (at + (t*speed*INNERTIA_SCALE))
|
||||||
|
var first = getMagazineOffset(pages.first(), null, 'center')
|
||||||
|
var last = getMagazineOffset(pages.last(), null, 'center')
|
||||||
|
|
||||||
|
// filter out really small speeds...
|
||||||
|
if(Math.abs(speed) > 0.5){
|
||||||
|
// check bounds...
|
||||||
|
// NOTE: need to cut the distance and time if we are going the
|
||||||
|
// hit the bounds...
|
||||||
|
if(to > first){
|
||||||
|
// trim the time proportionally...
|
||||||
|
var _t = t
|
||||||
|
t = Math.abs(t * ((at-first)/(at-to)))
|
||||||
|
to = first
|
||||||
|
setTransitionEasing(mag, 'linear')
|
||||||
|
} else if(to < last){
|
||||||
|
// trim the time proportionally...
|
||||||
|
var _t = t
|
||||||
|
t = Math.abs(t * ((at-last)/(at-to)))
|
||||||
|
to = last
|
||||||
|
setTransitionEasing(mag, 'linear')
|
||||||
|
|
||||||
|
} else {
|
||||||
|
setTransitionEasing(mag, 'ease-out')
|
||||||
|
}
|
||||||
|
|
||||||
|
setTransitionDuration(mag, t)
|
||||||
|
setElementTransform(mag, to)
|
||||||
|
|
||||||
|
// restore defaults...
|
||||||
|
// XXX this is a bit dumb at this point...
|
||||||
|
// XXX run this as a transition end handler...
|
||||||
|
setTimeout(function(){
|
||||||
|
setTransitionEasing(mag, 'ease-out')
|
||||||
|
setTransitionDuration(mag, INITIAL_TIME)
|
||||||
|
}, t+10)
|
||||||
|
|
||||||
|
// check scroll bounds...
|
||||||
|
// do not let the user scroll out of view...
|
||||||
|
} else {
|
||||||
|
if(at > first){
|
||||||
|
setTransitionEasing(mag, 'ease-in')
|
||||||
|
setTransitionDuration(mag, INITIAL_TIME)
|
||||||
|
//setCurrentPage(0)
|
||||||
|
setElementTransform(mag, first)
|
||||||
|
|
||||||
|
} else if(at < last){
|
||||||
|
setTransitionEasing(mag, 'ease-in')
|
||||||
|
setTransitionDuration(mag, INITIAL_TIME)
|
||||||
|
//setCurrentPage(-1)
|
||||||
|
setElementTransform(mag, last)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
// XXX stub...
|
// XXX stub...
|
||||||
setTransitionEasing($('.magazine'), 'ease-out')
|
setTransitionEasing($('.magazine'), 'ease-out')
|
||||||
|
|
||||||
|
|||||||
58
lib/jli.js
58
lib/jli.js
@ -646,7 +646,14 @@ function makeScrollHandler(root, config){
|
|||||||
if(scroller.options.callback
|
if(scroller.options.callback
|
||||||
// XXX revise this....
|
// XXX revise this....
|
||||||
// call the callback...
|
// call the callback...
|
||||||
&& scroller.options.callback(evt, dx/dt, start_x-x, t-start_t, touches, scroller.state) === false
|
&& scroller.options.callback({
|
||||||
|
orig_event: evt,
|
||||||
|
scroller: scroller,
|
||||||
|
speed: dx/dt,
|
||||||
|
distance: start_x-x,
|
||||||
|
duration: t-start_t,
|
||||||
|
touches: touches
|
||||||
|
}) === false
|
||||||
|| touches == 0){
|
|| touches == 0){
|
||||||
// cleanup and stop...
|
// cleanup and stop...
|
||||||
touch = false
|
touch = false
|
||||||
@ -662,6 +669,7 @@ function makeScrollHandler(root, config){
|
|||||||
|
|
||||||
|
|
||||||
var scroller = {
|
var scroller = {
|
||||||
|
root: root,
|
||||||
options: {
|
options: {
|
||||||
// if one of these is false, it will restrict scrolling in
|
// if one of these is false, it will restrict scrolling in
|
||||||
// that direction. hScroll for horizontal and vScroll for
|
// that direction. hScroll for horizontal and vScroll for
|
||||||
@ -688,7 +696,7 @@ function makeScrollHandler(root, config){
|
|||||||
eventBounds: 5,
|
eventBounds: 5,
|
||||||
|
|
||||||
// NOTE: if this returns false explicitly, this will stop scrolling.
|
// NOTE: if this returns false explicitly, this will stop scrolling.
|
||||||
callback: null
|
callback: postScrollCallback
|
||||||
},
|
},
|
||||||
// NOTE: this is updated live but not used by the system in any way...
|
// NOTE: this is updated live but not used by the system in any way...
|
||||||
state: 'stopped',
|
state: 'stopped',
|
||||||
@ -740,6 +748,52 @@ function makeScrollHandler(root, config){
|
|||||||
return scroller
|
return scroller
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var CLICK_THRESHOLD = 10
|
||||||
|
var LONG_CLICK_THRESHOLD = 400
|
||||||
|
|
||||||
|
|
||||||
|
// this is the default callback...
|
||||||
|
// XXX add up/down swipes
|
||||||
|
// XXX add double clicks
|
||||||
|
// XXX add generic snap
|
||||||
|
// XXX add generic innertial scroll
|
||||||
|
function postScrollCallback(data){
|
||||||
|
var root = data.scroller.root
|
||||||
|
// cancel event...
|
||||||
|
if(data.scroller.state == 'canceling'){
|
||||||
|
return root.trigger('scrollCancelled', data)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ignore situations when the user is still touching...
|
||||||
|
// ...like when he/she lifted one finger of several...
|
||||||
|
if(data.touches > 0){
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// clicks and long clicks...
|
||||||
|
if(Math.abs(data.distance) < CLICK_THRESHOLD){
|
||||||
|
if(data.duration > LONG_CLICK_THRESHOLD){
|
||||||
|
return root.trigger('longClick', data)
|
||||||
|
}
|
||||||
|
return root.trigger('shortClick', data)
|
||||||
|
}
|
||||||
|
|
||||||
|
// left/right swipes...
|
||||||
|
// XXX add up/down support...
|
||||||
|
if(data.distance <= -CLICK_THRESHOLD){
|
||||||
|
return root.trigger('swipeLeft', data)
|
||||||
|
}
|
||||||
|
if(data.distance >= CLICK_THRESHOLD){
|
||||||
|
return root.trigger('swipeRight', data)
|
||||||
|
}
|
||||||
|
|
||||||
|
// XXX snap...
|
||||||
|
// XXX innertial scroll...
|
||||||
|
|
||||||
|
// XXX this may never get called directly....
|
||||||
|
return root.trigger('screenReleased', data)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/************************************************ jQuery extensions **/
|
/************************************************ jQuery extensions **/
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user