Show More
tooltip.js
205 lines
| 6.9 KiB
| application/javascript
|
JavascriptLexer
|
r7145 | //---------------------------------------------------------------------------- | ||
// Copyright (C) 2008-2011 The IPython Development Team | ||||
// | ||||
// Distributed under the terms of the BSD License. The full license is in | ||||
// the file COPYING, distributed as part of this software. | ||||
//---------------------------------------------------------------------------- | ||||
//============================================================================ | ||||
// Tooltip | ||||
//============================================================================ | ||||
|
r7148 | // Todo : | ||
// use codemirror highlight example to | ||||
// highlight the introspection request and introspect on mouse hove ... | ||||
|
r7151 | // | ||
// | ||||
|
r7145 | var IPython = (function (IPython) { | ||
var utils = IPython.utils; | ||||
var Tooltip = function (notebook) { | ||||
this.tooltip = $('#tooltip'); | ||||
|
r7153 | var that = this; | ||
this._hidden = true; | ||||
// contain the button in the upper right corner | ||||
|
r7148 | this.buttons = $('<div/>') | ||
.addClass('tooltipbuttons'); | ||||
|
r7153 | |||
// will contain the docstring | ||||
|
r7147 | this.text = $('<div/>') | ||
|
r7148 | .addClass('tooltiptext') | ||
|
r7147 | .addClass('smalltooltip'); | ||
var tooltip = this.tooltip; | ||||
var text = this.text; | ||||
|
r7149 | // build the buttons menu on the upper right | ||
// expand the tooltip to see more | ||||
|
r7147 | var expandlink=$('<a/>').attr('href',"#") | ||
.addClass("ui-corner-all") //rounded corner | ||||
.attr('role',"button") | ||||
.attr('id','expanbutton') | ||||
.click(function(){ | ||||
text.removeClass('smalltooltip'); | ||||
text.addClass('bigtooltip'); | ||||
|
r7151 | $('#expanbutton').addClass('hidden'); | ||
|
r7156 | that._cmfocus(); | ||
|
r7150 | }) | ||
.append( | ||||
|
r7155 | $('<span/>').text('Expand') | ||
.addClass('ui-icon') | ||||
.addClass('ui-icon-plus') | ||||
); | ||||
|
r7147 | |||
|
r7155 | // open in pager | ||
|
r7150 | var morelink=$('<a/>').attr('href',"#") | ||
.attr('role',"button") | ||||
.addClass('ui-button'); | ||||
var morespan=$('<span/>').text('Open in Pager') | ||||
.addClass('ui-icon') | ||||
.addClass('ui-icon-arrowstop-l-n'); | ||||
|
r7147 | morelink.append(morespan); | ||
morelink.click(function(){ | ||||
|
r7156 | var msg_id = IPython.notebook.kernel.execute(that.name+"?"); | ||
|
r7147 | IPython.notebook.msg_cell_map[msg_id] = IPython.notebook.get_selected_cell().cell_id; | ||
that.remove_and_cancel_tooltip(); | ||||
|
r7156 | that._cmfocus(); | ||
|
r7147 | }); | ||
|
r7155 | // close the tooltip | ||
|
r7147 | var closelink=$('<a/>').attr('href',"#"); | ||
closelink.attr('role',"button"); | ||||
closelink.addClass('ui-button'); | ||||
var closespan=$('<span/>').text('Close'); | ||||
closespan.addClass('ui-icon'); | ||||
closespan.addClass('ui-icon-close'); | ||||
closelink.append(closespan); | ||||
closelink.click(function(){ | ||||
|
r7155 | that.hide(); | ||
|
r7147 | }); | ||
|
r7149 | |||
|
r7155 | //construct the tooltip | ||
// add in the reverse order you want them to appear | ||||
|
r7148 | this.buttons.append(closelink); | ||
this.buttons.append(expandlink); | ||||
this.buttons.append(morelink); | ||||
|
r7155 | |||
// we need a phony element to make the small arrow | ||||
// of the tooltip in css | ||||
// we could try to move the arrow later | ||||
this.arrow = $('<div/>').addClass('pretooltiparrow'); | ||||
|
r7148 | this.tooltip.append(this.buttons); | ||
|
r7155 | this.tooltip.append(this.arrow); | ||
|
r7148 | this.tooltip.append(this.text); | ||
|
r7145 | }; | ||
|
r7151 | // deal with all the logic of hiding the tooltip | ||
// and reset it's status | ||||
Tooltip.prototype.hide = function() | ||||
{ | ||||
|
r7153 | this.tooltip.addClass('hide'); | ||
$('#expanbutton').removeClass('hidden'); | ||||
this.text.removeClass('bigtooltip'); | ||||
this.text.addClass('smalltooltip'); | ||||
// keep scroll top to be sure to always see the first line | ||||
this.text.scrollTop(0); | ||||
this._hidden = true; | ||||
|
r7151 | } | ||
|
r7145 | |||
//TODO, try to diminish the number of parameters. | ||||
Tooltip.prototype.request_tooltip_after_time = function (pre_cursor,time){ | ||||
}; | ||||
Tooltip.prototype.remove_and_cancel_tooltip = function() { | ||||
// note that we don't handle closing directly inside the calltip | ||||
// as in the completer, because it is not focusable, so won't | ||||
// get the event. | ||||
|
r7157 | this.hide(); | ||
|
r7145 | if (this.tooltip_timeout != null){ | ||
clearTimeout(this.tooltip_timeout); | ||||
this.tooltip_timeout = null; | ||||
} | ||||
} | ||||
|
r7157 | |||
Tooltip.prototype.pending = function(cell,text) | ||||
{ | ||||
var that = this; | ||||
|
r7158 | this.tooltip_timeout = setTimeout(function(){that.request(cell, text)} , IPython.notebook.time_before_tooltip); | ||
|
r7157 | } | ||
|
r7156 | Tooltip.prototype.request = function(cell,text) | ||
{ | ||||
IPython.notebook.request_tool_tip(cell, text); | ||||
} | ||||
Tooltip.prototype.show = function(reply, codecell) | ||||
|
r7145 | { | ||
|
r7153 | // move the bubble if it is not hidden | ||
// otherwise fade it | ||||
|
r7156 | var editor = codecell.code_mirror; | ||
this.name = reply.name; | ||||
this.code_mirror = editor; | ||||
var pos = editor.cursorCoords(); | ||||
|
r7155 | var xinit = pos.x; | ||
var xinter = xinit/1000*600; | ||||
var posarrowleft = xinit - xinter; | ||||
|
r7153 | if( this._hidden == false) | ||
{ | ||||
|
r7155 | this.tooltip.animate({'left' : xinter-30+'px','top' :(pos.yBot+10)+'px'}); | ||
|
r7153 | } else | ||
{ | ||||
|
r7155 | this.tooltip.css({'left' : xinter-30+'px'}); | ||
|
r7153 | this.tooltip.css({'top' :(pos.yBot+10)+'px'}); | ||
} | ||||
|
r7155 | this.arrow.animate({'left' : posarrowleft+'px'}); | ||
|
r7153 | this.tooltip.removeClass('hidden') | ||
this.tooltip.removeClass('hide'); | ||||
this._hidden = false; | ||||
|
r7147 | |||
// build docstring | ||||
defstring = reply.call_def; | ||||
if (defstring == null) { defstring = reply.init_definition; } | ||||
if (defstring == null) { defstring = reply.definition; } | ||||
docstring = reply.call_docstring; | ||||
if (docstring == null) { docstring = reply.init_docstring; } | ||||
if (docstring == null) { docstring = reply.docstring; } | ||||
if (docstring == null) { docstring = "<empty docstring>"; } | ||||
this.text.children().remove(); | ||||
var pre=$('<pre/>').html(utils.fixConsole(docstring)); | ||||
if(defstring){ | ||||
var defstring_html = $('<pre/>').html(utils.fixConsole(defstring)); | ||||
this.text.append(defstring_html); | ||||
} | ||||
|
r7151 | this.text.append(pre); | ||
|
r7158 | // keep scroll top to be sure to always see the first line | ||
this.text.scrollTop(0); | ||||
|
r7147 | |||
|
r7158 | } | ||
|
r7145 | |||
Tooltip.prototype.showInPager = function(){ | ||||
var msg_id = IPython.notebook.kernel.execute(name+"?"); | ||||
IPython.notebook.msg_cell_map[msg_id] = IPython.notebook.get_selected_cell().cell_id; | ||||
that.remove_and_cancel_tooltip(); | ||||
setTimeout(function(){that.code_mirror.focus();}, 50); | ||||
} | ||||
|
r7156 | Tooltip.prototype._cmfocus = function() | ||
{ | ||||
var cm = this.code_mirror; | ||||
setTimeout(function(){cm.focus();}, 50); | ||||
} | ||||
|
r7145 | |||
IPython.Tooltip = Tooltip; | ||||
return IPython; | ||||
}(IPython)); | ||||