Skip to content

Instantly share code, notes, and snippets.

@davibe
Created September 28, 2011 17:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davibe/1248656 to your computer and use it in GitHub Desktop.
Save davibe/1248656 to your computer and use it in GitHub Desktop.
Custom fast click handling I'm using in JQM projects (since JQM team decided to disable custom vclick event)
this.FastClick = {}
ns = this.FastClick
ns.bind = (element, cb) ->
element = $ element
if not $.support.touch
# desktops don't need it
element.bind 'click', cb
return
element.bind 'touchstart', ns.touchStart
element.bind 'touchend', ns.touchEnd
element.bind 'touchmove', ns.touchMove
element.bind 'touchcancel', ns.touchCancel
element.bind 'fastClick', cb
refCount = element.data 'fastClickRefCount'
if not refCount
refCount = 1
else
refCount = refCount + 1
element.data 'fastClickRefCount', refCount
return
ns.unbind = (element, cb) ->
element = $ element
if not ns.supportsTouch
element.unbind 'click', cb
return
element.unbind 'fastClick', cb
refCount = element.data 'fastClickRefCount'
if refCount and refCount > 0
refCount = refCount - 1
element.data 'fastClickRefCount', refCount
else
element.removeData 'fastClickRefCount'
element.unbind 'touchstart', ns.touchStart
element.unbind 'touchend', ns.touchEnd
element.unbind 'touchmove', ns.touchMove
element.unbind 'touchcancel', ns.touchCancel
return
ns.touchStart = (event) ->
console.log event
element = $ event.target
element.data 'fastClickMoved', no
return
ns.touchMove = (event) ->
console.log event
element = $ event.target
#TODO: Should check if it moved enough
element.data 'fastClickMoved', yes
return
ns.touchEnd = (event) ->
element = $ event.target
if not element.data 'fastClickMoved'
element.removeData 'fastClickMoved'
element.trigger 'fastClick'
return
ns.touchCancel = (event) ->
element = $ event.target
element.removeData 'fastClickMoved'
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment