This repository has been archived by the owner on Jan 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
tooltips.coffee
179 lines (128 loc) · 4.41 KB
/
tooltips.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# Defaults
Tooltip =
text: false
css: {top: 0, left: 0}
direction: 'tooltip--top'
classes: ''
dep = new Tracker.Dependency()
offset = [10, 10]
DIRECTION_MAP =
'n': 'tooltip--top'
's': 'tooltip--bottom'
'e': 'tooltip--right'
'w': 'tooltip--left'
# Tooltip functions
getTooltip = ->
dep.depend()
return Tooltip
setTooltip = (what, where) ->
Tooltip.css = where if where
Tooltip.text = what
dep.changed()
setPosition = (position, direction) ->
Tooltip.css = position
Tooltip.direction = DIRECTION_MAP[direction] if direction
dep.changed()
setClasses = (classes) ->
Tooltip.classes = classes or ''
hideTooltip = ->
setTooltip false
toggleTooltip = ->
if getTooltip().text then hideTooltip() else showTooltip null, $(this)
positionTooltip = ($el) ->
direction = $el.attr('data-tooltip-direction') or 'n'
$tooltip = $(".tooltip")
position = $el.offset()
offLeft = $el.attr('data-tooltip-left')
offTop = $el.attr('data-tooltip-top')
if _.isUndefined(offLeft)
offLeft = 0
else
hasOffsetLeft = true
if _.isUndefined(offTop)
offTop = 0
else
hasOffsetTop = true
position.top = switch direction
when 'w', 'e' then (center vertically $tooltip, $el) + offTop
when 'n' then position.top - $tooltip.outerHeight() - (if hasOffsetTop then offTop else offset[1])
when 's' then position.top + $el.outerHeight() + (if hasOffsetTop then offTop else offset[1])
position.left = switch direction
when 'n', 's' then (center horizontally $tooltip, $el) + offLeft
when 'w' then position.left - $tooltip.outerWidth() - (if hasOffsetLeft then offLeft else offset[0])
when 'e' then position.left + $el.outerWidth() + (if hasOffsetLeft then offLeft else offset[0])
setPosition(position, direction)
showTooltip = (evt, $el) ->
$el = $el or $(this)
viewport = $el.attr 'data-tooltip-disable'
if viewport and _.isString(viewport)
mq = window.matchMedia(viewport)
return false if mq.matches
content = if selector = $el.attr 'data-tooltip-element'
$target = $(selector)
$target.length and $target.html()
else
$el.attr('data-tooltip')
setTooltip(content)
setPosition(top: 0, left: 0)
setClasses($el.attr('data-tooltip-classes'))
Tracker.afterFlush -> positionTooltip($el)
# Positioning
center = (args) ->
middle = args[0] + args[1] / 2
middle - Math.round(args[2] / 2)
horizontally = ($el, $reference) ->
[$reference.offset().left, $reference.outerWidth(), $el.outerWidth()]
vertically = ($el, $reference) ->
[$reference.offset().top, $reference.outerHeight(), $el.outerHeight()]
# Exports
Tooltips =
disable: false
set: setTooltip
get: getTooltip
hide: hideTooltip
setPosition: setPosition
# Enable/disable for viewports
Template.tooltips.onCreated ->
@disabled = new ReactiveVar(Tooltips.disable)
if Tooltips.disable and _.isString(Tooltips.disable)
mq = window.matchMedia(Tooltips.disable)
@disabled.set(mq.matches)
mq.addListener (changed) =>
@disabled.set(changed.matches)
# Template helpers
Template.tooltips.helpers
display: ->
tip = getTooltip()
if Template.instance().disabled.get() is true
return 'hide'
if tip.text then 'show' else 'hide'
position: ->
css = getTooltip().css
return "position: absolute; top: #{css.top}px; left: #{css.left}px;"
content: ->
getTooltip().text
direction: ->
getTooltip().direction
classes: ->
getTooltip().classes
# Init
Template.tooltip.onRendered ->
this.lastNode._uihooks =
insertElement: (node, next) ->
next.parentNode.insertBefore(node, next)
moveElement: (node, next) ->
Tooltips.hide()
next.parentNode.insertBefore(node, next)
removeElement: (node) ->
Tooltips.hide()
node.parentNode.removeChild(node)
Meteor.startup ->
$(document).on 'mouseover', '[data-tooltip]:not([data-tooltip-trigger]), [data-tooltip-element]:not([data-tooltip-trigger]), [data-tooltip-trigger="hover"]', showTooltip
$(document).on 'mouseout', '[data-tooltip]:not([data-tooltip-trigger]), [data-tooltip-element]:not([data-tooltip-trigger]), [data-tooltip-trigger="hover"]', hideTooltip
$(document).on 'click', '[data-tooltip-trigger="click"]', toggleTooltip
$(document).on 'focus', '[data-tooltip-trigger="focus"]', showTooltip
$(document).on 'blur', '[data-tooltip-trigger="focus"]', hideTooltip
$(document).on 'tooltips:show', '[data-tooltip-trigger="manual"]', showTooltip
$(document).on 'tooltips:hide', '[data-tooltip-trigger="manual"]', hideTooltip
$(document).on 'tooltips:toggle', '[data-tooltip-trigger="manual"]', toggleTooltip