-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtouch-hover-to-link.coffee
46 lines (39 loc) · 1.37 KB
/
touch-hover-to-link.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# jQuery touch-hover-to-link plugin
# Original author: @mwitekdesign
# Further changes, comments: https://github.com/red-green-refactor/touch-hover-to-link
# Licensed under the MIT license
$ = jQuery
isTouchDevice = ->
return "ontouchstart" of (document.documentElement)
$.fn.extend
touchHoverToLink: (options) ->
_dataLinkSet = false
item = this
$item = $(item)
setupTouchLinkItem = () ->
$item.attr('data-href', $item.attr('href'))
$item.attr('href', "#touch_click_link")
bindTouchLinkClickEvent = () ->
$item.on 'touchstart', (event) ->
event.preventDefault()
if $(event.currentTarget).attr('data-linkSet') == "true"
window.location.href = $(event.currentTarget).attr('data-href')
_dataLinkSet = false
return
else
$item.attr('data-linkSet', "false")
$(event.currentTarget).attr('data-linkSet', "true")
_dataLinkSet = true
return
bindCloseHoverToBodyClick = () ->
$('body').on 'touchstart', (event) ->
unless $(event.target).hasClass(item.selector.substring(1))
if _dataLinkSet == true
$item.attr('data-linkSet', "false")
_dataLinkSet = false
return
if isTouchDevice()
setupTouchLinkItem()
bindTouchLinkClickEvent()
bindCloseHoverToBodyClick()
return