Skip to content

Instantly share code, notes, and snippets.

@methyl
Created January 23, 2014 13:39
Show Gist options
  • Save methyl/8578569 to your computer and use it in GitHub Desktop.
Save methyl/8578569 to your computer and use it in GitHub Desktop.
class SquerbWeb.views.Tooltip extends Backbone.View
template: HandlebarsTemplates['new/tooltip']
className: 'tooltip'
constructor: (options) ->
@$parent = options.parent
@html = options.html
super(options)
render: =>
@$el.html(@template())
@$('[data-content]').html(@html)
@$el.appendTo('body')
@reposition()
@
# private
delegateEvents: ->
super()
@$parent.on 'mouseenter', @show
@$parent.on 'mouseleave', @hide
undelegateEvents: ->
@$parent.off 'mouseenter', @show
@$parent.off 'mouseleave', @hide
show: (e) =>
@$target = $(e.currentTarget)
@html = @$target.attr('data-tooltip') if @$target.attr('data-tooltip')?
@render()
@$el.addClass('visible')
hide: =>
@$el.removeClass('visible').detach()
reposition: ->
@$el.css
top: @$target.offset().top - @$el.outerHeight()
left: @$target.offset().left + (@$target.outerWidth() - @$el.outerWidth()) / 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment