##// END OF EJS Templates
clean code, show clock if tooltip is 'sticky'...
clean code, show clock if tooltip is 'sticky' - make code more concise - remove trailing space - add some animation - put some function/variable private with leading underscore

File last commit:

r7173:cf54aa1b
r7173:cf54aa1b
Show More
tooltip.js
360 lines | 12.3 KiB | application/javascript | JavascriptLexer
Matthias Bussonnier
tooltip to mac
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
//============================================================================
Matthias Bussonnier
fix scrolltop
r7151 //
Matthias BUSSONNIER
Clean code, retab and minor fix...
r7170 // you can set the autocall time by setting `IPython.tooltip.time_before_tooltip` in ms
Matthias BUSSONNIER
make tooltip tabs fonction configurable
r7172 //
Matthias BUSSONNIER
clean code, show clock if tooltip is 'sticky'...
r7173 // you can configure the differents action of pressing tab several times in a row by
// setting/appending different fonction in the array
Matthias BUSSONNIER
make tooltip tabs fonction configurable
r7172 // IPython.tooltip.tabs_functions
//
// eg :
// IPython.tooltip.tabs_functions[4] = function(){console.log('this is the action of the 4th tab pressing')}
//
Matthias BUSSONNIER
clean and comment tooltip file
r7162
Matthias Bussonnier
tooltip to mac
r7145 var IPython = (function (IPython) {
Matthias BUSSONNIER
clean code, show clock if tooltip is 'sticky'...
r7173 "use strict";
Matthias Bussonnier
tooltip to mac
r7145
var utils = IPython.utils;
Matthias BUSSONNIER
clean code, show clock if tooltip is 'sticky'...
r7173
Matthias BUSSONNIER
clean and comment tooltip file
r7162 // tooltip constructor
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 var Tooltip = function () {
Matthias BUSSONNIER
animation if already shown
r7153 var that = this;
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168 this.time_before_tooltip = 1200;
Matthias BUSSONNIER
clean and comment tooltip file
r7162
// handle to html
this.tooltip = $('#tooltip');
Matthias BUSSONNIER
animation if already shown
r7153 this._hidden = true;
Matthias BUSSONNIER
multiple tooltip action...
r7160 // variable for consecutive call
this._old_cell = null ;
this._old_request = null ;
Matthias BUSSONNIER
make tooltip tabs fonction configurable
r7172 this._consecutive_counter = 0;
Matthias BUSSONNIER
multiple tooltip action...
r7160
// 'sticky ?'
this._sticky = false;
Matthias BUSSONNIER
animation if already shown
r7153 // contain the button in the upper right corner
Matthias BUSSONNIER
change new tooltip appearence...
r7148 this.buttons = $('<div/>')
.addClass('tooltipbuttons');
Matthias BUSSONNIER
animation if already shown
r7153
Matthias BUSSONNIER
clean and comment tooltip file
r7162 // will contain the docstring
Matthias BUSSONNIER
improve new tooltip
r7147 this.text = $('<div/>')
Matthias BUSSONNIER
change new tooltip appearence...
r7148 .addClass('tooltiptext')
Matthias BUSSONNIER
improve new tooltip
r7147 .addClass('smalltooltip');
Matthias BUSSONNIER
clean and comment tooltip file
r7162 // build the buttons menu on the upper right
// expand the tooltip to see more
Matthias BUSSONNIER
improve new tooltip
r7147 var expandlink=$('<a/>').attr('href',"#")
.addClass("ui-corner-all") //rounded corner
.attr('role',"button")
.attr('id','expanbutton')
Matthias BUSSONNIER
clean code, show clock if tooltip is 'sticky'...
r7173 .attr('title','Grow the tooltip vertically (press tab 2 times)')
Matthias BUSSONNIER
multiple tooltip action...
r7160 .click(function(){that.expand()})
Matthias Bussonnier
play with tooltip growing css...
r7150 .append(
Matthias BUSSONNIER
move arow with tooltip positoin
r7155 $('<span/>').text('Expand')
.addClass('ui-icon')
.addClass('ui-icon-plus')
);
Matthias BUSSONNIER
improve new tooltip
r7147
Matthias BUSSONNIER
move arow with tooltip positoin
r7155 // open in pager
Matthias Bussonnier
play with tooltip growing css...
r7150 var morelink=$('<a/>').attr('href',"#")
.attr('role',"button")
Matthias BUSSONNIER
clean code, show clock if tooltip is 'sticky'...
r7173 .addClass('ui-button')
.attr('title','show the current docstring in pager (press tab 4 times)');
Matthias Bussonnier
play with tooltip growing css...
r7150 var morespan=$('<span/>').text('Open in Pager')
.addClass('ui-icon')
.addClass('ui-icon-arrowstop-l-n');
Matthias BUSSONNIER
improve new tooltip
r7147 morelink.append(morespan);
morelink.click(function(){
Matthias BUSSONNIER
multiple tooltip action...
r7160 that.showInPager();
Matthias BUSSONNIER
improve new tooltip
r7147 });
Matthias BUSSONNIER
move arow with tooltip positoin
r7155 // close the tooltip
Matthias BUSSONNIER
clean code, show clock if tooltip is 'sticky'...
r7173 var closelink=$('<a/>').attr('href',"#")
.attr('role',"button")
.addClass('ui-button');
var closespan=$('<span/>').text('Close')
.addClass('ui-icon')
.addClass('ui-icon-close');
Matthias BUSSONNIER
improve new tooltip
r7147 closelink.append(closespan);
closelink.click(function(){
Matthias BUSSONNIER
should fix click on close works when sticky
r7161 that.remove_and_cancel_tooltip(true);
Matthias BUSSONNIER
improve new tooltip
r7147 });
Matthias BUSSONNIER
clean and comment tooltip file
r7162
Matthias BUSSONNIER
clean code, show clock if tooltip is 'sticky'...
r7173 this._clocklink=$('<a/>').attr('href',"#");
this._clocklink.attr('role',"button");
this._clocklink.addClass('ui-button');
this._clocklink.attr('title','Tootip is not dismissed while typing for 10 seconds');
var clockspan=$('<span/>').text('Close');
clockspan.addClass('ui-icon');
clockspan.addClass('ui-icon-clock');
this._clocklink.append(clockspan);
this._clocklink.click(function(){
that.cancel_stick();
});
Matthias BUSSONNIER
move arow with tooltip positoin
r7155 //construct the tooltip
// add in the reverse order you want them to appear
Matthias BUSSONNIER
change new tooltip appearence...
r7148 this.buttons.append(closelink);
this.buttons.append(expandlink);
this.buttons.append(morelink);
Matthias BUSSONNIER
clean code, show clock if tooltip is 'sticky'...
r7173 this.buttons.append(this._clocklink);
this._clocklink.hide();
Matthias BUSSONNIER
move arow with tooltip positoin
r7155
// we need a phony element to make the small arrow
// of the tooltip in css
Matthias BUSSONNIER
clean and comment tooltip file
r7162 // we will move the arrow later
Matthias BUSSONNIER
move arow with tooltip positoin
r7155 this.arrow = $('<div/>').addClass('pretooltiparrow');
Matthias BUSSONNIER
change new tooltip appearence...
r7148 this.tooltip.append(this.buttons);
Matthias BUSSONNIER
move arow with tooltip positoin
r7155 this.tooltip.append(this.arrow);
Matthias BUSSONNIER
change new tooltip appearence...
r7148 this.tooltip.append(this.text);
Matthias BUSSONNIER
make tooltip tabs fonction configurable
r7172
Matthias BUSSONNIER
clean code, show clock if tooltip is 'sticky'...
r7173 // function that will be called if you press tab 1, 2, 3... times in a row
this.tabs_functions = [ function(cell,text){that._request_tooltip(cell,text)},
Matthias BUSSONNIER
make tooltip tabs fonction configurable
r7172 function(){that.expand()},
function(){that.stick()},
function(){
that.cancel_stick();
that.showInPager();
that._cmfocus();
}
];
// call after all the tabs function above have bee call to clean their effects
// if necessary
this.reset_tabs_function = function(cell,text){
this._old_cell = (cell)? cell : null ;
this._old_request = (text) ? text : null ;
this._consecutive_counter = 0;
}
Matthias Bussonnier
tooltip to mac
r7145 };
Matthias BUSSONNIER
multiple tooltip action...
r7160 Tooltip.prototype.showInPager = function()
{
Matthias BUSSONNIER
make tooltip tabs fonction configurable
r7172 // reexecute last call in pager by appending ? to show back in pager
var that = this;
var callbacks = {'execute_reply': $.proxy(that._handle_execute_reply,that)}
var msg_id = IPython.notebook.kernel.execute(this.name+"?", callbacks);
Brian Granger
Major refactoring of the Notebook, Kernel and CodeCell JavaScript....
r7168
Matthias BUSSONNIER
make tooltip tabs fonction configurable
r7172 this.remove_and_cancel_tooltip();
this._cmfocus();
Matthias BUSSONNIER
multiple tooltip action...
r7160 }
Matthias BUSSONNIER
clean and comment tooltip file
r7162 // grow the tooltip verticaly
Matthias BUSSONNIER
multiple tooltip action...
r7160 Tooltip.prototype.expand = function(){
this.text.removeClass('smalltooltip');
this.text.addClass('bigtooltip');
Matthias BUSSONNIER
clean code, show clock if tooltip is 'sticky'...
r7173 $('#expanbutton').hide('slow');
Matthias BUSSONNIER
multiple tooltip action...
r7160 this._cmfocus();
}
Matthias Bussonnier
fix scrolltop
r7151 // deal with all the logic of hiding the tooltip
// and reset it's status
Tooltip.prototype.hide = function()
{
Matthias BUSSONNIER
animation if already shown
r7153 this.tooltip.addClass('hide');
Matthias BUSSONNIER
clean code, show clock if tooltip is 'sticky'...
r7173 $('#expanbutton').show('slow');
Matthias BUSSONNIER
animation if already shown
r7153 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;
Matthias Bussonnier
fix scrolltop
r7151 }
Matthias Bussonnier
tooltip to mac
r7145
Matthias BUSSONNIER
multiple tooltip action...
r7160 Tooltip.prototype.remove_and_cancel_tooltip = function(force) {
Matthias Bussonnier
tooltip to mac
r7145 // 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.
Matthias BUSSONNIER
multiple tooltip action...
r7160 if(this._sticky == false || force == true)
{
this.hide();
}
this.cancel_pending();
Matthias BUSSONNIER
clean code, show clock if tooltip is 'sticky'...
r7173 this.reset_tabs_function();
Matthias BUSSONNIER
multiple tooltip action...
r7160 }
Matthias BUSSONNIER
clean and comment tooltip file
r7162 // cancel autocall done after '(' for example.
Matthias BUSSONNIER
multiple tooltip action...
r7160 Tooltip.prototype.cancel_pending = function(){
Matthias BUSSONNIER
clean code, show clock if tooltip is 'sticky'...
r7173 if (this._tooltip_timeout != null){
clearTimeout(this._tooltip_timeout);
this._tooltip_timeout = null;
Matthias Bussonnier
tooltip to mac
r7145 }
}
Matthias BUSSONNIER
clean and comment tooltip file
r7162
// will trigger tooltip after timeout
Matthias BUSSONNIER
clean code, show clock if tooltip is 'sticky'...
r7173 Tooltip.prototype.pending = function(cell)
Matthias BUSSONNIER
call tooltip after time
r7157 {
var that = this;
Matthias BUSSONNIER
clean code, show clock if tooltip is 'sticky'...
r7173 this._tooltip_timeout = setTimeout(function(){that.request(cell)} , that.time_before_tooltip);
Matthias BUSSONNIER
call tooltip after time
r7157 }
Matthias BUSSONNIER
clean code, show clock if tooltip is 'sticky'...
r7173
Tooltip.prototype._request_tooltip = function(cell,func)
Matthias BUSSONNIER
move some tooltip logic away from codecell.js
r7171 {
Matthias BUSSONNIER
clean code, show clock if tooltip is 'sticky'...
r7173 // use internally just to make the request to the kernel
Matthias BUSSONNIER
make tooltip tabs fonction configurable
r7172
Matthias BUSSONNIER
move some tooltip logic away from codecell.js
r7171 // Feel free to shorten this logic if you are better
// than me in regEx
Matthias BUSSONNIER
clean code, show clock if tooltip is 'sticky'...
r7173 // basicaly you shoul be able to get xxx.xxx.xxx from
// something(range(10), kwarg=smth) ; xxx.xxx.xxx( firstarg, rand(234,23), kwarg1=2,
Matthias BUSSONNIER
move some tooltip logic away from codecell.js
r7171 // remove everything between matchin bracket (need to iterate)
var matchBracket = /\([^\(\)]+\)/g;
var endBracket = /\([^\(]*$/g;
var oldfunc = func;
func = func.replace(matchBracket,"");
while( oldfunc != func )
{
Matthias BUSSONNIER
clean code, show clock if tooltip is 'sticky'...
r7173 oldfunc = func;
func = func.replace(matchBracket,"");
Matthias BUSSONNIER
move some tooltip logic away from codecell.js
r7171 }
Matthias BUSSONNIER
clean code, show clock if tooltip is 'sticky'...
r7173 // remove everything after last open bracket
Matthias BUSSONNIER
move some tooltip logic away from codecell.js
r7171 func = func.replace(endBracket,"");
Matthias BUSSONNIER
clean code, show clock if tooltip is 'sticky'...
r7173
Matthias BUSSONNIER
move some tooltip logic away from codecell.js
r7171 var re = /[a-z_][0-9a-z._]+$/gi; // casse insensitive
var callbacks = {'object_info_reply': $.proxy(this.show,this)}
var msg_id = IPython.notebook.kernel.object_info_request(re.exec(func), callbacks);
}
Matthias BUSSONNIER
clean and comment tooltip file
r7162
// make an imediate completion request
Matthias BUSSONNIER
multiple tooltip action...
r7160 Tooltip.prototype.request = function(cell)
Matthias BUSSONNIER
call tooltip by cell reference
r7156 {
Matthias BUSSONNIER
clean code, show clock if tooltip is 'sticky'...
r7173 // request(codecell)
Matthias BUSSONNIER
make tooltip tabs fonction configurable
r7172 // Deal with extracting the text from the cell and counting
// call in a row
Matthias BUSSONNIER
clean code, show clock if tooltip is 'sticky'...
r7173
Matthias BUSSONNIER
multiple tooltip action...
r7160 this.cancel_pending();
var editor = cell.code_mirror;
var cursor = editor.getCursor();
var text = editor.getRange({line:cursor.line,ch:0},cursor).trim();
Matthias BUSSONNIER
clean code, show clock if tooltip is 'sticky'...
r7173
Matthias BUSSONNIER
make tooltip tabs fonction configurable
r7172 // need a permanent handel to codemirror for future auto recall
this.code_mirror = editor;
Matthias BUSSONNIER
clean code, show clock if tooltip is 'sticky'...
r7173 // now we treat the different number of keypress
Matthias BUSSONNIER
make tooltip tabs fonction configurable
r7172 // first if same cell, same text, increment counter by 1
Matthias BUSSONNIER
multiple tooltip action...
r7160 if( this._old_cell == cell && this._old_request == text && this._hidden == false)
{
Matthias BUSSONNIER
clean code, show clock if tooltip is 'sticky'...
r7173 this._consecutive_counter++;
Matthias BUSSONNIER
multiple tooltip action...
r7160 } else {
Matthias BUSSONNIER
make tooltip tabs fonction configurable
r7172 // else reset
Matthias BUSSONNIER
clean code, show clock if tooltip is 'sticky'...
r7173 this.cancel_stick();
Matthias BUSSONNIER
make tooltip tabs fonction configurable
r7172 this.reset_tabs_function(cell,text);
Matthias BUSSONNIER
multiple tooltip action...
r7160 }
Matthias BUSSONNIER
clean code, show clock if tooltip is 'sticky'...
r7173
Matthias BUSSONNIER
make tooltip tabs fonction configurable
r7172 // don't do anything if line beggin with '(' or is empty
Matthias BUSSONNIER
multiple tooltip action...
r7160 if (text === "" || text === "(" ) {
return;
}
Matthias BUSSONNIER
clean code, show clock if tooltip is 'sticky'...
r7173
this.tabs_functions[this._consecutive_counter](cell,text);
// then if we are at the end of list function, reset
if (this._consecutive_counter == this.tabs_functions.length)
Matthias BUSSONNIER
make tooltip tabs fonction configurable
r7172 this.reset_tabs_function(cell,text);
Matthias BUSSONNIER
clean code, show clock if tooltip is 'sticky'...
r7173
Matthias BUSSONNIER
make tooltip tabs fonction configurable
r7172 return;
Matthias BUSSONNIER
multiple tooltip action...
r7160 }
Matthias BUSSONNIER
clean and comment tooltip file
r7162
// cancel the option of having the tooltip to stick
Matthias BUSSONNIER
multiple tooltip action...
r7160 Tooltip.prototype.cancel_stick = function()
{
Matthias BUSSONNIER
clean code, show clock if tooltip is 'sticky'...
r7173 console.log('cancel stick');
Matthias BUSSONNIER
multiple tooltip action...
r7160 clearTimeout(this._stick_timeout);
Matthias BUSSONNIER
clean code, show clock if tooltip is 'sticky'...
r7173 this._stick_timeout = null;
this._clocklink.hide('slow');
Matthias BUSSONNIER
multiple tooltip action...
r7160 this._sticky = false;
}
Matthias BUSSONNIER
clean and comment tooltip file
r7162 // put the tooltip in a sicky state for 10 seconds
// it won't be removed by remove_and_cancell() unless you called with
// the first parameter set to true.
// remove_and_cancell_tooltip(true)
Tooltip.prototype.stick = function()
Matthias BUSSONNIER
multiple tooltip action...
r7160 {
Matthias BUSSONNIER
clean and comment tooltip file
r7162 var that = this;
Matthias BUSSONNIER
multiple tooltip action...
r7160 this._sticky = true;
Matthias BUSSONNIER
clean code, show clock if tooltip is 'sticky'...
r7173 this._clocklink.show('slow');
Matthias BUSSONNIER
multiple tooltip action...
r7160 this._stick_timeout = setTimeout( function(){
that._sticky = false;
Matthias BUSSONNIER
clean code, show clock if tooltip is 'sticky'...
r7173 that._clocklink.hide('slow');
Matthias BUSSONNIER
multiple tooltip action...
r7160 }, 10*1000
);
Matthias BUSSONNIER
call tooltip by cell reference
r7156 }
Matthias BUSSONNIER
clean and comment tooltip file
r7162 // should be called with the kernel reply to actually show the tooltip
Matthias BUSSONNIER
move some tooltip logic away from codecell.js
r7171 Tooltip.prototype.show = function(reply)
Matthias Bussonnier
tooltip to mac
r7145 {
Matthias BUSSONNIER
animation if already shown
r7153 // move the bubble if it is not hidden
// otherwise fade it
Matthias BUSSONNIER
make tooltip tabs fonction configurable
r7172 this.name = reply.name;
Matthias BUSSONNIER
multiple tooltip action...
r7160
// do some math to have the tooltip arrow on more or less on left or right
// width of the editor
Matthias BUSSONNIER
clean code, show clock if tooltip is 'sticky'...
r7173 var w = $(this.code_mirror.getScrollerElement()).width();
Matthias BUSSONNIER
multiple tooltip action...
r7160 // ofset of the editor
Matthias BUSSONNIER
clean code, show clock if tooltip is 'sticky'...
r7173 var o = $(this.code_mirror.getScrollerElement()).offset();
var pos = this.code_mirror.cursorCoords();
Matthias BUSSONNIER
move arow with tooltip positoin
r7155 var xinit = pos.x;
Matthias BUSSONNIER
multiple tooltip action...
r7160 var xinter = o.left + (xinit-o.left)/w*(w-450);
Matthias BUSSONNIER
move arow with tooltip positoin
r7155 var posarrowleft = xinit - xinter;
Matthias BUSSONNIER
clean and comment tooltip file
r7162
Matthias BUSSONNIER
move arow with tooltip positoin
r7155
Matthias BUSSONNIER
animation if already shown
r7153 if( this._hidden == false)
{
Matthias BUSSONNIER
move arow with tooltip positoin
r7155 this.tooltip.animate({'left' : xinter-30+'px','top' :(pos.yBot+10)+'px'});
Matthias BUSSONNIER
clean and comment tooltip file
r7162 } else
Matthias BUSSONNIER
animation if already shown
r7153 {
Matthias BUSSONNIER
move arow with tooltip positoin
r7155 this.tooltip.css({'left' : xinter-30+'px'});
Matthias BUSSONNIER
animation if already shown
r7153 this.tooltip.css({'top' :(pos.yBot+10)+'px'});
}
Matthias BUSSONNIER
move arow with tooltip positoin
r7155 this.arrow.animate({'left' : posarrowleft+'px'});
Matthias BUSSONNIER
animation if already shown
r7153 this.tooltip.removeClass('hidden')
this.tooltip.removeClass('hide');
this._hidden = false;
Matthias BUSSONNIER
improve new tooltip
r7147
// build docstring
Matthias BUSSONNIER
clean code, show clock if tooltip is 'sticky'...
r7173 var defstring = reply.call_def;
Matthias BUSSONNIER
improve new tooltip
r7147 if (defstring == null) { defstring = reply.init_definition; }
if (defstring == null) { defstring = reply.definition; }
Matthias BUSSONNIER
clean code, show clock if tooltip is 'sticky'...
r7173 var docstring = reply.call_docstring;
Matthias BUSSONNIER
improve new tooltip
r7147 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);
}
Matthias Bussonnier
fix scrolltop
r7151 this.text.append(pre);
Matthias BUSSONNIER
almost all logic in tooltip.js, padding right button
r7158 // keep scroll top to be sure to always see the first line
this.text.scrollTop(0);
Matthias Bussonnier
tooltip to mac
r7145 }
Matthias BUSSONNIER
clean and comment tooltip file
r7162 // convenient funciton to have the correct codemirror back into focus
Matthias BUSSONNIER
call tooltip by cell reference
r7156 Tooltip.prototype._cmfocus = function()
{
var cm = this.code_mirror;
setTimeout(function(){cm.focus();}, 50);
}
Matthias Bussonnier
tooltip to mac
r7145 IPython.Tooltip = Tooltip;
Matthias BUSSONNIER
clean code, show clock if tooltip is 'sticky'...
r7173
Matthias Bussonnier
tooltip to mac
r7145 return IPython;
Matthias BUSSONNIER
clean code, show clock if tooltip is 'sticky'...
r7173
Matthias Bussonnier
tooltip to mac
r7145 }(IPython));