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