var IPython = (function (IPython) {
"use strict";
var utils = IPython.utils;
var Tooltip = function () {
var that = this;
this.time_before_tooltip = 1200;
this.tooltip = $('#tooltip');
this._hidden = true;
this._old_cell = null;
this._old_request = null;
this._consecutive_counter = 0;
this._sticky = false;
this._hide_if_no_docstring = false;
this.buttons = $('<div/>').addClass('tooltipbuttons');
this.text = $('<div/>').addClass('tooltiptext').addClass('smalltooltip');
var expandlink = $('<a/>').attr('href', "#").addClass("ui-corner-all")
.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'));
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);
});
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);
});
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();
});
this.buttons.append(closelink);
this.buttons.append(expandlink);
this.buttons.append(morelink);
this.buttons.append(this._clocklink);
this._clocklink.hide();
this.arrow = $('<div/>').addClass('pretooltiparrow');
this.tooltip.append(this.buttons);
this.tooltip.append(this.arrow);
this.tooltip.append(this.text);
this.tabs_functions = [function (cell, text) {
that._request_tooltip(cell, text);
}, function () {
that.expand();
}, function () {
that.stick();
}, function (cell) {
that.cancel_stick();
that.showInPager(cell);
}];
this.reset_tabs_function = function (cell, text) {
this._old_cell = (cell) ? cell : null;
this._old_request = (text) ? text : null;
this._consecutive_counter = 0;
};
};
Tooltip.prototype.is_visible = function () {
return !this._hidden;
};
Tooltip.prototype.showInPager = function (cell) {
var that = this;
var callbacks = {'shell' : {
'payload' : {
'page' : $.proxy(cell._open_with_pager, cell)
}
}
};
cell.kernel.execute(that.name + '?', callbacks, {'silent': false, 'store_history': true});
this.remove_and_cancel_tooltip();
};
Tooltip.prototype.expand = function () {
this.text.removeClass('smalltooltip');
this.text.addClass('bigtooltip');
$('#expanbutton').hide('slow');
};
Tooltip.prototype._hide = function () {
this._hidden = true;
this.tooltip.fadeOut('fast');
$('#expanbutton').show('slow');
this.text.removeClass('bigtooltip');
this.text.addClass('smalltooltip');
this.text.scrollTop(0);
this.code_mirror = null;
};
Tooltip.prototype.remove_and_cancel_tooltip = function (force) {
this.cancel_pending();
if (!this._hidden) {
if (force || !this._sticky) {
this.cancel_stick();
this._hide();
}
this.reset_tabs_function();
return true;
} else {
return false;
}
};
Tooltip.prototype.cancel_pending = function () {
if (this._tooltip_timeout !== null) {
clearTimeout(this._tooltip_timeout);
this._tooltip_timeout = null;
}
};
Tooltip.prototype.pending = function (cell, hide_if_no_docstring) {
var that = this;
this._tooltip_timeout = setTimeout(function () {
that.request(cell, hide_if_no_docstring);
}, that.time_before_tooltip);
};
Tooltip.last_token_re = /[a-z_][0-9a-z._]*$/gi;
Tooltip.prototype.extract_oir_token = function(line){
var matchBracket = /\([^\(\)]+\)/g;
var endBracket = /\([^\(]*$/g;
var oldline = line;
line = line.replace(matchBracket, "");
while (oldline != line) {
oldline = line;
line = line.replace(matchBracket, "");
}
line = line.replace(endBracket, "");
Tooltip.last_token_re.lastIndex = 0;
return Tooltip.last_token_re.exec(line);
};
Tooltip.prototype._request_tooltip = function (cell, line) {
var callbacks = $.proxy(this._show, this);
var oir_token = this.extract_oir_token(line);
var msg_id = cell.kernel.object_info(oir_token, callbacks);
};
Tooltip.prototype.request = function (cell, hide_if_no_docstring) {
this.cancel_pending();
var editor = cell.code_mirror;
var cursor = editor.getCursor();
var text = editor.getRange({
line: cursor.line,
ch: 0
}, cursor).trim();
this._hide_if_no_docstring = hide_if_no_docstring;
if(editor.somethingSelected()){
text = editor.getSelection();
}
this.code_mirror = editor;
if (this._old_cell == cell && this._old_request == text && this._hidden === false) {
this._consecutive_counter++;
} else {
this.cancel_stick();
this.reset_tabs_function (cell, text);
}
if (text === "" || text === "(") {
return;
}
this.tabs_functions[this._consecutive_counter](cell, text);
if (this._consecutive_counter == this.tabs_functions.length) {
this.reset_tabs_function (cell, text);
}
return;
};
Tooltip.prototype.cancel_stick = function () {
clearTimeout(this._stick_timeout);
this._stick_timeout = null;
this._clocklink.hide('slow');
this._sticky = false;
};
Tooltip.prototype.stick = function (time) {
time = (time !== undefined) ? time : 10;
var that = this;
this._sticky = true;
this._clocklink.show('slow');
this._stick_timeout = setTimeout(function () {
that._sticky = false;
that._clocklink.hide('slow');
}, time * 1000);
};
Tooltip.prototype._show = function (reply) {
var content = reply.content;
if (!content.found) {
return;
}
this.name = content.name;
var w = $(this.code_mirror.getScrollerElement()).width();
var o = $(this.code_mirror.getScrollerElement()).offset();
var anchor = this.code_mirror.cursorCoords(false);
var head = this.code_mirror.cursorCoords(true);
var xinit = (head.left+anchor.left)/2;
var xinter = o.left + (xinit - o.left) / w * (w - 450);
var posarrowleft = xinit - xinter;
if (this._hidden === false) {
this.tooltip.animate({
'left': xinter - 30 + 'px',
'top': (head.bottom + 10) + 'px'
});
} else {
this.tooltip.css({
'left': xinter - 30 + 'px'
});
this.tooltip.css({
'top': (head.bottom + 10) + 'px'
});
}
this.arrow.animate({
'left': posarrowleft + 'px'
});
var defstring = content.call_def;
if (!defstring) {
defstring = content.init_definition;
}
if (!defstring) {
defstring = content.definition;
}
var docstring = content.call_docstring;
if (!docstring) {
docstring = content.init_docstring;
}
if (!docstring) {
docstring = content.docstring;
}
if (!docstring) {
if (this._hide_if_no_docstring) {
return;
} else {
docstring = "<empty docstring>";
}
}
this._hidden = false;
this.tooltip.fadeIn('fast');
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);
}
this.text.append(pre);
this.text.scrollTop(0);
};
IPython.Tooltip = Tooltip;
return IPython;
}(IPython));