Add TooltipManager

Signed-off-by: Max Brunsfeld <maxbrunsfeld@gmail.com>
This commit is contained in:
Nathan Sobo
2014-11-19 18:57:08 -07:00
committed by Max Brunsfeld
parent 6bdbabecbd
commit 175d7811b4
2 changed files with 99 additions and 0 deletions

View File

@@ -0,0 +1,45 @@
_ = require 'underscore-plus'
{$} = require './space-pen-extensions'
module.exports =
class TooltipManager
defaults:
delay:
show: 1000
hide: 100
container: 'body'
html: true
placement: 'auto top'
viewportPadding: 2
add: (target, options) ->
requireBootstrapTooltip()
{keyBindingCommand} = options
if keyBindingCommand?
keyBindingTarget = target unless typeof target is 'string'
bindings = atom.keymaps.findKeyBindings(command: keyBindingCommand, target: keyBindingTarget)
if options.title?
options.title += " " + getKeystroke(bindings)
else
options.title = getKeystroke(bindings)
if typeof target is 'string'
options.selector = target
target = document.body
$(target).tooltip(_.defaults(options, @defaults))
humanizeKeystrokes = (keystroke) ->
keystrokes = keystroke.split(' ')
keystrokes = (_.humanizeKeystroke(stroke) for stroke in keystrokes)
keystrokes.join(' ')
getKeystroke = (bindings) ->
if bindings?.length
"<span class=\"keystroke\">#{humanizeKeystrokes(bindings[0].keystrokes)}</span>"
else
requireBootstrapTooltip = _.once ->
atom.requireWithGlobals('bootstrap/js/tooltip', {jQuery: $})