morris.js/lib/morris.hover.coffee

42 lines
1.0 KiB
CoffeeScript
Raw Normal View History

2012-12-04 20:12:34 +01:00
class Morris.Hover
# Displays contextual information in a floating HTML div.
@defaults:
class: 'morris-hover morris-default-style'
2012-12-04 20:12:34 +01:00
constructor: (options = {}) ->
@options = $.extend {}, Morris.Hover.defaults, options
@el = $ "<div class='#{@options.class}'></div>"
@el.hide()
@options.parent.append(@el)
2012-12-04 20:12:34 +01:00
update: (html, x, y) ->
@html(html)
@show()
@moveTo(x, y)
html: (content) ->
@el.html(content)
moveTo: (x, y) ->
2012-12-11 19:25:18 +01:00
parentWidth = @options.parent.innerWidth()
parentHeight = @options.parent.innerHeight()
hoverWidth = @el.outerWidth()
hoverHeight = @el.outerHeight()
left = Math.min(Math.max(0, x - hoverWidth / 2), parentWidth - hoverWidth)
if y?
top = y - hoverHeight - 10
if top < 0
top = y + 10
if top + hoverHeight > parentHeight
top = parentHeight / 2 - hoverHeight / 2
else
top = parentHeight / 2 - hoverHeight / 2
@el.css(left: left + "px", top: top + "px")
show: ->
2012-12-04 20:12:34 +01:00
@el.show()
2012-12-04 20:12:34 +01:00
hide: ->
@el.hide()