mirror of
https://github.com/atom/atom.git
synced 2026-02-17 01:51:54 -05:00
Add TooltipManager
Signed-off-by: Max Brunsfeld <maxbrunsfeld@gmail.com>
This commit is contained in:
committed by
Max Brunsfeld
parent
6bdbabecbd
commit
175d7811b4
45
src/tooltip-manager.coffee
Normal file
45
src/tooltip-manager.coffee
Normal 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: $})
|
||||
Reference in New Issue
Block a user