morris.hover.coffee 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. class Morris.Hover
  2. # Displays contextual information in a floating HTML div.
  3. @defaults:
  4. class: 'morris-hover morris-default-style'
  5. constructor: (options = {}) ->
  6. @options = $.extend {}, Morris.Hover.defaults, options
  7. @el = $ "<div class='#{@options.class}'></div>"
  8. @el.hide()
  9. @options.parent.append(@el)
  10. update: (html, x, y) ->
  11. if not html
  12. @hide()
  13. else
  14. @html(html)
  15. @show()
  16. @moveTo(x, y)
  17. html: (content) ->
  18. @el.html(content)
  19. moveTo: (x, y) ->
  20. parentWidth = @options.parent.innerWidth()
  21. parentHeight = @options.parent.innerHeight()
  22. hoverWidth = @el.outerWidth()
  23. hoverHeight = @el.outerHeight()
  24. left = Math.min(Math.max(0, x - hoverWidth / 2), parentWidth - hoverWidth)
  25. if y?
  26. top = y - hoverHeight - 10
  27. if top < 0
  28. top = y + 10
  29. if top + hoverHeight > parentHeight
  30. top = parentHeight / 2 - hoverHeight / 2
  31. else
  32. top = parentHeight / 2 - hoverHeight / 2
  33. @el.css(left: left + "px", top: parseInt(top) + "px")
  34. show: ->
  35. @el.show()
  36. hide: ->
  37. @el.hide()