tooltip.js
317 lines
| 10.9 KiB
| application/javascript
|
JavascriptLexer
MinRK
|
r16580 | // Copyright (c) IPython Development Team. | ||
// Distributed under the terms of the Modified BSD License. | ||||
Jonathan Frederic
|
r17198 | define([ | ||
'base/js/namespace', | ||||
Jonathan Frederic
|
r17200 | 'jquery', | ||
Jonathan Frederic
|
r17198 | 'base/js/utils', | ||
], function(IPython, $, utils) { | ||||
Matthias BUSSONNIER
|
r7173 | "use strict"; | ||
Matthias Bussonnier
|
r7145 | |||
Matthias BUSSONNIER
|
r7162 | // tooltip constructor | ||
Jonathan Frederic
|
r17198 | var Tooltip = function (events) { | ||
var that = this; | ||||
this.events = events; | ||||
this.time_before_tooltip = 1200; | ||||
Jessica B. Hamrick
|
r11715 | |||
Jonathan Frederic
|
r17198 | // handle to html | ||
this.tooltip = $('#tooltip'); | ||||
this._hidden = true; | ||||
Matthias BUSSONNIER
|
r7189 | |||
Jonathan Frederic
|
r17198 | // variable for consecutive call | ||
this._old_cell = null; | ||||
this._old_request = null; | ||||
this._consecutive_counter = 0; | ||||
Matthias BUSSONNIER
|
r7189 | |||
Jonathan Frederic
|
r17198 | // 'sticky ?' | ||
this._sticky = false; | ||||
Matthias BUSSONNIER
|
r7189 | |||
Jonathan Frederic
|
r17198 | // display tooltip if the docstring is empty? | ||
this._hide_if_no_docstring = false; | ||||
// contain the button in the upper right corner | ||||
this.buttons = $('<div/>').addClass('tooltipbuttons'); | ||||
// will contain the docstring | ||||
this.text = $('<div/>').addClass('tooltiptext').addClass('smalltooltip'); | ||||
// build the buttons menu on the upper right | ||||
// expand the tooltip to see more | ||||
var expandlink = $('<a/>').attr('href', "#").addClass("ui-corner-all") //rounded corner | ||||
.attr('role', "button").attr('id', 'expanbutton').attr('title', 'Grow the tooltip vertically (press shift-tab twice)').click(function () { | ||||
that.expand(); | ||||
}).append( | ||||
$('<span/>').text('Expand').addClass('ui-icon').addClass('ui-icon-plus')); | ||||
// open in pager | ||||
var morelink = $('<a/>').attr('href', "#").attr('role', "button").addClass('ui-button').attr('title', 'show the current docstring in pager (press shift-tab 4 times)'); | ||||
var morespan = $('<span/>').text('Open in Pager').addClass('ui-icon').addClass('ui-icon-arrowstop-l-n'); | ||||
morelink.append(morespan); | ||||
morelink.click(function () { | ||||
that.showInPager(that._old_cell); | ||||
}); | ||||
Matthias BUSSONNIER
|
r7147 | |||
Jonathan Frederic
|
r17198 | // close the tooltip | ||
var closelink = $('<a/>').attr('href', "#").attr('role', "button").addClass('ui-button'); | ||||
var closespan = $('<span/>').text('Close').addClass('ui-icon').addClass('ui-icon-close'); | ||||
closelink.append(closespan); | ||||
closelink.click(function () { | ||||
that.remove_and_cancel_tooltip(true); | ||||
}); | ||||
Matthias BUSSONNIER
|
r7162 | |||
Jonathan Frederic
|
r17198 | 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
|
r7173 | |||
Jonathan Frederic
|
r17198 | //construct the tooltip | ||
// add in the reverse order you want them to appear | ||||
this.buttons.append(closelink); | ||||
this.buttons.append(expandlink); | ||||
this.buttons.append(morelink); | ||||
this.buttons.append(this._clocklink); | ||||
this._clocklink.hide(); | ||||
// we need a phony element to make the small arrow | ||||
// of the tooltip in css | ||||
// we will move the arrow later | ||||
this.arrow = $('<div/>').addClass('pretooltiparrow'); | ||||
this.tooltip.append(this.buttons); | ||||
this.tooltip.append(this.arrow); | ||||
this.tooltip.append(this.text); | ||||
// function that will be called if you press tab 1, 2, 3... times in a row | ||||
this.tabs_functions = [function (cell, text, cursor) { | ||||
that._request_tooltip(cell, text, cursor); | ||||
}, function () { | ||||
that.expand(); | ||||
}, function () { | ||||
that.stick(); | ||||
}, function (cell) { | ||||
that.cancel_stick(); | ||||
that.showInPager(cell); | ||||
}]; | ||||
// 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
|
r7189 | }; | ||
Jonathan Frederic
|
r17198 | }; | ||
Matthias BUSSONNIER
|
r7189 | |||
Jonathan Frederic
|
r15493 | Tooltip.prototype.is_visible = function () { | ||
return !this._hidden; | ||||
}; | ||||
Matthias BUSSONNIER
|
r7191 | Tooltip.prototype.showInPager = function (cell) { | ||
Jonathan Frederic
|
r19176 | /** | ||
* reexecute last call in pager by appending ? to show back in pager | ||||
*/ | ||||
Matthias Bussonnier
|
r18284 | this.events.trigger('open_with_text.Pager', this._reply.content); | ||
Matthias BUSSONNIER
|
r7172 | this.remove_and_cancel_tooltip(); | ||
Jonathan Frederic
|
r15493 | }; | ||
Matthias BUSSONNIER
|
r7160 | |||
Matthias BUSSONNIER
|
r7162 | // grow the tooltip verticaly | ||
Matthias BUSSONNIER
|
r7191 | Tooltip.prototype.expand = function () { | ||
Matthias BUSSONNIER
|
r7160 | this.text.removeClass('smalltooltip'); | ||
this.text.addClass('bigtooltip'); | ||||
Matthias BUSSONNIER
|
r7173 | $('#expanbutton').hide('slow'); | ||
Jonathan Frederic
|
r15493 | }; | ||
Matthias BUSSONNIER
|
r7160 | |||
Matthias Bussonnier
|
r7151 | // deal with all the logic of hiding the tooltip | ||
// and reset it's status | ||||
Matthias BUSSONNIER
|
r7202 | Tooltip.prototype._hide = function () { | ||
Jonathan Frederic
|
r15493 | this._hidden = true; | ||
Matthias BUSSONNIER
|
r7202 | this.tooltip.fadeOut('fast'); | ||
Matthias BUSSONNIER
|
r7189 | $('#expanbutton').show('slow'); | ||
this.text.removeClass('bigtooltip'); | ||||
this.text.addClass('smalltooltip'); | ||||
// keep scroll top to be sure to always see the first line | ||||
this.text.scrollTop(0); | ||||
Matthias BUSSONNIER
|
r7204 | this.code_mirror = null; | ||
Jonathan Frederic
|
r15493 | }; | ||
Matthias Bussonnier
|
r7145 | |||
Takeshi Kanmae
|
r12459 | // return true on successfully removing a visible tooltip; otherwise return | ||
// false. | ||||
Matthias BUSSONNIER
|
r7191 | Tooltip.prototype.remove_and_cancel_tooltip = function (force) { | ||
Jonathan Frederic
|
r19176 | /** | ||
* 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. | ||||
*/ | ||||
MinRK
|
r12955 | this.cancel_pending(); | ||
Takeshi Kanmae
|
r12533 | if (!this._hidden) { | ||
if (force || !this._sticky) { | ||||
Takeshi Kanmae
|
r12459 | this.cancel_stick(); | ||
this._hide(); | ||||
} | ||||
this.reset_tabs_function(); | ||||
return true; | ||||
} else { | ||||
return false; | ||||
Matthias BUSSONNIER
|
r7160 | } | ||
Jonathan Frederic
|
r15493 | }; | ||
Matthias BUSSONNIER
|
r7160 | |||
Matthias BUSSONNIER
|
r7162 | // cancel autocall done after '(' for example. | ||
Matthias BUSSONNIER
|
r7191 | Tooltip.prototype.cancel_pending = function () { | ||
Jonathan Frederic
|
r15493 | if (this._tooltip_timeout !== null) { | ||
Matthias BUSSONNIER
|
r7173 | clearTimeout(this._tooltip_timeout); | ||
this._tooltip_timeout = null; | ||||
Matthias Bussonnier
|
r7145 | } | ||
Jonathan Frederic
|
r15493 | }; | ||
Matthias BUSSONNIER
|
r7162 | |||
// will trigger tooltip after timeout | ||||
Jessica B. Hamrick
|
r11715 | Tooltip.prototype.pending = function (cell, hide_if_no_docstring) { | ||
Matthias BUSSONNIER
|
r7157 | var that = this; | ||
Matthias BUSSONNIER
|
r7191 | this._tooltip_timeout = setTimeout(function () { | ||
Jonathan Frederic
|
r15493 | that.request(cell, hide_if_no_docstring); | ||
Matthias BUSSONNIER
|
r7189 | }, that.time_before_tooltip); | ||
Jonathan Frederic
|
r15493 | }; | ||
Matthias BUSSONNIER
|
r7173 | |||
Matthias BUSSONNIER
|
r12819 | // easy access for julia monkey patching. | ||
MinRK
|
r14716 | Tooltip.last_token_re = /[a-z_][0-9a-z._]*$/gi; | ||
Matthias BUSSONNIER
|
r12819 | |||
MinRK
|
r16580 | Tooltip.prototype._request_tooltip = function (cell, text, cursor_pos) { | ||
Matthias BUSSONNIER
|
r13327 | var callbacks = $.proxy(this._show, this); | ||
MinRK
|
r16587 | var msg_id = cell.kernel.inspect(text, cursor_pos, callbacks); | ||
MinRK
|
r13207 | }; | ||
Matthias BUSSONNIER
|
r7162 | |||
Matthias Bussonnier
|
r18284 | // make an immediate completion request | ||
Jessica B. Hamrick
|
r11715 | Tooltip.prototype.request = function (cell, hide_if_no_docstring) { | ||
Jonathan Frederic
|
r19176 | /** | ||
* request(codecell) | ||||
* Deal with extracting the text from the cell and counting | ||||
* call in a row | ||||
*/ | ||||
Matthias BUSSONNIER
|
r7160 | this.cancel_pending(); | ||
var editor = cell.code_mirror; | ||||
var cursor = editor.getCursor(); | ||||
MinRK
|
r16588 | var cursor_pos = utils.to_absolute_cursor_pos(editor, cursor); | ||
MinRK
|
r16580 | var text = cell.get_text(); | ||
Matthias BUSSONNIER
|
r7173 | |||
Jessica B. Hamrick
|
r11715 | this._hide_if_no_docstring = hide_if_no_docstring; | ||
Bussonnier Matthias
|
r8949 | if(editor.somethingSelected()){ | ||
Matthias Bussonnier
|
r18284 | // get only the most recent selection. | ||
Bussonnier Matthias
|
r8949 | text = editor.getSelection(); | ||
} | ||||
Matthias Bussonnier
|
r18284 | // need a permanent handle to code_mirror for future auto recall | ||
Matthias BUSSONNIER
|
r7172 | this.code_mirror = editor; | ||
Matthias BUSSONNIER
|
r7173 | // now we treat the different number of keypress | ||
Matthias BUSSONNIER
|
r7172 | // first if same cell, same text, increment counter by 1 | ||
Jonathan Frederic
|
r15493 | if (this._old_cell == cell && this._old_request == text && this._hidden === false) { | ||
Matthias BUSSONNIER
|
r7173 | this._consecutive_counter++; | ||
Matthias BUSSONNIER
|
r7160 | } else { | ||
Matthias BUSSONNIER
|
r7172 | // else reset | ||
Matthias BUSSONNIER
|
r7173 | this.cancel_stick(); | ||
Matthias BUSSONNIER
|
r7191 | this.reset_tabs_function (cell, text); | ||
Matthias BUSSONNIER
|
r7160 | } | ||
Matthias BUSSONNIER
|
r7173 | |||
MinRK
|
r16580 | this.tabs_functions[this._consecutive_counter](cell, text, cursor_pos); | ||
Matthias BUSSONNIER
|
r7173 | |||
// then if we are at the end of list function, reset | ||||
Steve Fox
|
r13793 | if (this._consecutive_counter == this.tabs_functions.length) { | ||
MinRK
|
r16580 | this.reset_tabs_function (cell, text, cursor); | ||
} | ||||
Matthias BUSSONNIER
|
r7173 | |||
Matthias BUSSONNIER
|
r7172 | return; | ||
Jonathan Frederic
|
r15493 | }; | ||
Matthias BUSSONNIER
|
r7162 | |||
// cancel the option of having the tooltip to stick | ||||
Matthias BUSSONNIER
|
r7191 | Tooltip.prototype.cancel_stick = function () { | ||
Matthias BUSSONNIER
|
r7189 | clearTimeout(this._stick_timeout); | ||
this._stick_timeout = null; | ||||
this._clocklink.hide('slow'); | ||||
this._sticky = false; | ||||
Jonathan Frederic
|
r15493 | }; | ||
Matthias BUSSONNIER
|
r7160 | |||
Matthias BUSSONNIER
|
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) | ||||
Matthias BUSSONNIER
|
r7191 | Tooltip.prototype.stick = function (time) { | ||
Jonathan Frederic
|
r15493 | time = (time !== undefined) ? time : 10; | ||
Matthias BUSSONNIER
|
r7162 | var that = this; | ||
Matthias BUSSONNIER
|
r7160 | this._sticky = true; | ||
Matthias BUSSONNIER
|
r7173 | this._clocklink.show('slow'); | ||
Matthias BUSSONNIER
|
r7191 | this._stick_timeout = setTimeout(function () { | ||
Matthias BUSSONNIER
|
r7160 | that._sticky = false; | ||
Matthias BUSSONNIER
|
r7173 | that._clocklink.hide('slow'); | ||
Matthias BUSSONNIER
|
r7189 | }, time * 1000); | ||
Jonathan Frederic
|
r15493 | }; | ||
Matthias BUSSONNIER
|
r7156 | |||
Matthias BUSSONNIER
|
r7162 | // should be called with the kernel reply to actually show the tooltip | ||
Matthias BUSSONNIER
|
r7202 | Tooltip.prototype._show = function (reply) { | ||
Jonathan Frederic
|
r19176 | /** | ||
* move the bubble if it is not hidden | ||||
* otherwise fade it | ||||
*/ | ||||
MinRK
|
r16580 | this._reply = reply; | ||
MinRK
|
r13207 | var content = reply.content; | ||
MinRK
|
r14787 | if (!content.found) { | ||
// object not found, nothing to show | ||||
return; | ||||
} | ||||
MinRK
|
r13207 | this.name = content.name; | ||
Matthias BUSSONNIER
|
r7160 | |||
// do some math to have the tooltip arrow on more or less on left or right | ||||
// width of the editor | ||||
Matthias BUSSONNIER
|
r7173 | var w = $(this.code_mirror.getScrollerElement()).width(); | ||
Matthias BUSSONNIER
|
r7160 | // ofset of the editor | ||
Matthias BUSSONNIER
|
r7173 | var o = $(this.code_mirror.getScrollerElement()).offset(); | ||
Bussonnier Matthias
|
r8949 | |||
// whatever anchor/head order but arrow at mid x selection | ||||
var anchor = this.code_mirror.cursorCoords(false); | ||||
var head = this.code_mirror.cursorCoords(true); | ||||
Brian E. Granger
|
r10425 | var xinit = (head.left+anchor.left)/2; | ||
Matthias BUSSONNIER
|
r7189 | var xinter = o.left + (xinit - o.left) / w * (w - 450); | ||
Matthias BUSSONNIER
|
r7155 | var posarrowleft = xinit - xinter; | ||
Matthias BUSSONNIER
|
r7162 | |||
Jonathan Frederic
|
r15493 | if (this._hidden === false) { | ||
Matthias BUSSONNIER
|
r7189 | this.tooltip.animate({ | ||
'left': xinter - 30 + 'px', | ||||
Brian E. Granger
|
r10425 | 'top': (head.bottom + 10) + 'px' | ||
Matthias BUSSONNIER
|
r7189 | }); | ||
} else { | ||||
this.tooltip.css({ | ||||
'left': xinter - 30 + 'px' | ||||
}); | ||||
this.tooltip.css({ | ||||
Brian E. Granger
|
r10425 | 'top': (head.bottom + 10) + 'px' | ||
Matthias BUSSONNIER
|
r7189 | }); | ||
Matthias BUSSONNIER
|
r7153 | } | ||
Matthias BUSSONNIER
|
r7189 | this.arrow.animate({ | ||
'left': posarrowleft + 'px' | ||||
}); | ||||
MinRK
|
r16580 | |||
Jessica B. Hamrick
|
r11715 | this._hidden = false; | ||
Jonathan Frederic
|
r15493 | this.tooltip.fadeIn('fast'); | ||
Matthias BUSSONNIER
|
r7147 | this.text.children().remove(); | ||
MinRK
|
r16580 | |||
// This should support rich data types, but only text/plain for now | ||||
Jonathan Frederic
|
r15376 | // Any HTML within the docstring is escaped by the fixConsole() method. | ||
MinRK
|
r16580 | var pre = $('<pre/>').html(utils.fixConsole(content.data['text/plain'])); | ||
Matthias Bussonnier
|
r7151 | this.text.append(pre); | ||
Matthias BUSSONNIER
|
r7158 | // keep scroll top to be sure to always see the first line | ||
this.text.scrollTop(0); | ||||
Jonathan Frederic
|
r15493 | }; | ||
Matthias BUSSONNIER
|
r7156 | |||
Matthias Bussonnier
|
r18284 | // Backwards compatibility. | ||
Matthias Bussonnier
|
r7145 | IPython.Tooltip = Tooltip; | ||
Matthias BUSSONNIER
|
r7173 | |||
Jonathan Frederic
|
r17201 | return {'Tooltip': Tooltip}; | ||
Jonathan Frederic
|
r17198 | }); | ||