##// END OF EJS Templates
Updating tooltip.js to work with CM3.
Brian E. Granger -
Show More
@@ -1,361 +1,361 b''
1 //----------------------------------------------------------------------------
1 //----------------------------------------------------------------------------
2 // Copyright (C) 2008-2011 The IPython Development Team
2 // Copyright (C) 2008-2011 The IPython Development Team
3 //
3 //
4 // Distributed under the terms of the BSD License. The full license is in
4 // Distributed under the terms of the BSD License. The full license is in
5 // the file COPYING, distributed as part of this software.
5 // the file COPYING, distributed as part of this software.
6 //----------------------------------------------------------------------------
6 //----------------------------------------------------------------------------
7 //============================================================================
7 //============================================================================
8 // Tooltip
8 // Tooltip
9 //============================================================================
9 //============================================================================
10 //
10 //
11 // you can set the autocall time by setting `IPython.tooltip.time_before_tooltip` in ms
11 // you can set the autocall time by setting `IPython.tooltip.time_before_tooltip` in ms
12 //
12 //
13 // you can configure the differents action of pressing tab several times in a row by
13 // you can configure the differents action of pressing tab several times in a row by
14 // setting/appending different fonction in the array
14 // setting/appending different fonction in the array
15 // IPython.tooltip.tabs_functions
15 // IPython.tooltip.tabs_functions
16 //
16 //
17 // eg :
17 // eg :
18 // IPython.tooltip.tabs_functions[4] = function (){console.log('this is the action of the 4th tab pressing')}
18 // IPython.tooltip.tabs_functions[4] = function (){console.log('this is the action of the 4th tab pressing')}
19 //
19 //
20 var IPython = (function (IPython) {
20 var IPython = (function (IPython) {
21 "use strict";
21 "use strict";
22
22
23 var utils = IPython.utils;
23 var utils = IPython.utils;
24
24
25 // tooltip constructor
25 // tooltip constructor
26 var Tooltip = function () {
26 var Tooltip = function () {
27 var that = this;
27 var that = this;
28 this.time_before_tooltip = 1200;
28 this.time_before_tooltip = 1200;
29
29
30 // handle to html
30 // handle to html
31 this.tooltip = $('#tooltip');
31 this.tooltip = $('#tooltip');
32 this._hidden = true;
32 this._hidden = true;
33
33
34 // variable for consecutive call
34 // variable for consecutive call
35 this._old_cell = null;
35 this._old_cell = null;
36 this._old_request = null;
36 this._old_request = null;
37 this._consecutive_counter = 0;
37 this._consecutive_counter = 0;
38
38
39 // 'sticky ?'
39 // 'sticky ?'
40 this._sticky = false;
40 this._sticky = false;
41
41
42 // contain the button in the upper right corner
42 // contain the button in the upper right corner
43 this.buttons = $('<div/>').addClass('tooltipbuttons');
43 this.buttons = $('<div/>').addClass('tooltipbuttons');
44
44
45 // will contain the docstring
45 // will contain the docstring
46 this.text = $('<div/>').addClass('tooltiptext').addClass('smalltooltip');
46 this.text = $('<div/>').addClass('tooltiptext').addClass('smalltooltip');
47
47
48 // build the buttons menu on the upper right
48 // build the buttons menu on the upper right
49 // expand the tooltip to see more
49 // expand the tooltip to see more
50 var expandlink = $('<a/>').attr('href', "#").addClass("ui-corner-all") //rounded corner
50 var expandlink = $('<a/>').attr('href', "#").addClass("ui-corner-all") //rounded corner
51 .attr('role', "button").attr('id', 'expanbutton').attr('title', 'Grow the tooltip vertically (press tab 2 times)').click(function () {
51 .attr('role', "button").attr('id', 'expanbutton').attr('title', 'Grow the tooltip vertically (press tab 2 times)').click(function () {
52 that.expand()
52 that.expand()
53 }).append(
53 }).append(
54 $('<span/>').text('Expand').addClass('ui-icon').addClass('ui-icon-plus'));
54 $('<span/>').text('Expand').addClass('ui-icon').addClass('ui-icon-plus'));
55
55
56 // open in pager
56 // open in pager
57 var morelink = $('<a/>').attr('href', "#").attr('role', "button").addClass('ui-button').attr('title', 'show the current docstring in pager (press tab 4 times)');
57 var morelink = $('<a/>').attr('href', "#").attr('role', "button").addClass('ui-button').attr('title', 'show the current docstring in pager (press tab 4 times)');
58 var morespan = $('<span/>').text('Open in Pager').addClass('ui-icon').addClass('ui-icon-arrowstop-l-n');
58 var morespan = $('<span/>').text('Open in Pager').addClass('ui-icon').addClass('ui-icon-arrowstop-l-n');
59 morelink.append(morespan);
59 morelink.append(morespan);
60 morelink.click(function () {
60 morelink.click(function () {
61 that.showInPager();
61 that.showInPager();
62 });
62 });
63
63
64 // close the tooltip
64 // close the tooltip
65 var closelink = $('<a/>').attr('href', "#").attr('role', "button").addClass('ui-button');
65 var closelink = $('<a/>').attr('href', "#").attr('role', "button").addClass('ui-button');
66 var closespan = $('<span/>').text('Close').addClass('ui-icon').addClass('ui-icon-close');
66 var closespan = $('<span/>').text('Close').addClass('ui-icon').addClass('ui-icon-close');
67 closelink.append(closespan);
67 closelink.append(closespan);
68 closelink.click(function () {
68 closelink.click(function () {
69 that.remove_and_cancel_tooltip(true);
69 that.remove_and_cancel_tooltip(true);
70 });
70 });
71
71
72 this._clocklink = $('<a/>').attr('href', "#");
72 this._clocklink = $('<a/>').attr('href', "#");
73 this._clocklink.attr('role', "button");
73 this._clocklink.attr('role', "button");
74 this._clocklink.addClass('ui-button');
74 this._clocklink.addClass('ui-button');
75 this._clocklink.attr('title', 'Tootip is not dismissed while typing for 10 seconds');
75 this._clocklink.attr('title', 'Tootip is not dismissed while typing for 10 seconds');
76 var clockspan = $('<span/>').text('Close');
76 var clockspan = $('<span/>').text('Close');
77 clockspan.addClass('ui-icon');
77 clockspan.addClass('ui-icon');
78 clockspan.addClass('ui-icon-clock');
78 clockspan.addClass('ui-icon-clock');
79 this._clocklink.append(clockspan);
79 this._clocklink.append(clockspan);
80 this._clocklink.click(function () {
80 this._clocklink.click(function () {
81 that.cancel_stick();
81 that.cancel_stick();
82 });
82 });
83
83
84
84
85
85
86
86
87 //construct the tooltip
87 //construct the tooltip
88 // add in the reverse order you want them to appear
88 // add in the reverse order you want them to appear
89 this.buttons.append(closelink);
89 this.buttons.append(closelink);
90 this.buttons.append(expandlink);
90 this.buttons.append(expandlink);
91 this.buttons.append(morelink);
91 this.buttons.append(morelink);
92 this.buttons.append(this._clocklink);
92 this.buttons.append(this._clocklink);
93 this._clocklink.hide();
93 this._clocklink.hide();
94
94
95
95
96 // we need a phony element to make the small arrow
96 // we need a phony element to make the small arrow
97 // of the tooltip in css
97 // of the tooltip in css
98 // we will move the arrow later
98 // we will move the arrow later
99 this.arrow = $('<div/>').addClass('pretooltiparrow');
99 this.arrow = $('<div/>').addClass('pretooltiparrow');
100 this.tooltip.append(this.buttons);
100 this.tooltip.append(this.buttons);
101 this.tooltip.append(this.arrow);
101 this.tooltip.append(this.arrow);
102 this.tooltip.append(this.text);
102 this.tooltip.append(this.text);
103
103
104 // function that will be called if you press tab 1, 2, 3... times in a row
104 // function that will be called if you press tab 1, 2, 3... times in a row
105 this.tabs_functions = [function (cell, text) {
105 this.tabs_functions = [function (cell, text) {
106 that._request_tooltip(cell, text);
106 that._request_tooltip(cell, text);
107 }, function () {
107 }, function () {
108 that.expand();
108 that.expand();
109 }, function () {
109 }, function () {
110 that.stick();
110 that.stick();
111 }, function (cell) {
111 }, function (cell) {
112 that.cancel_stick();
112 that.cancel_stick();
113 that.showInPager(cell);
113 that.showInPager(cell);
114 }];
114 }];
115 // call after all the tabs function above have bee call to clean their effects
115 // call after all the tabs function above have bee call to clean their effects
116 // if necessary
116 // if necessary
117 this.reset_tabs_function = function (cell, text) {
117 this.reset_tabs_function = function (cell, text) {
118 this._old_cell = (cell) ? cell : null;
118 this._old_cell = (cell) ? cell : null;
119 this._old_request = (text) ? text : null;
119 this._old_request = (text) ? text : null;
120 this._consecutive_counter = 0;
120 this._consecutive_counter = 0;
121 }
121 }
122 };
122 };
123
123
124 Tooltip.prototype.showInPager = function (cell) {
124 Tooltip.prototype.showInPager = function (cell) {
125 // reexecute last call in pager by appending ? to show back in pager
125 // reexecute last call in pager by appending ? to show back in pager
126 var that = this;
126 var that = this;
127 var empty = function () {};
127 var empty = function () {};
128 cell.kernel.execute(
128 cell.kernel.execute(
129 that.name + '?', {
129 that.name + '?', {
130 'execute_reply': empty,
130 'execute_reply': empty,
131 'output': empty,
131 'output': empty,
132 'clear_output': empty,
132 'clear_output': empty,
133 'cell': cell
133 'cell': cell
134 }, {
134 }, {
135 'silent': false
135 'silent': false
136 });
136 });
137 this.remove_and_cancel_tooltip();
137 this.remove_and_cancel_tooltip();
138 }
138 }
139
139
140 // grow the tooltip verticaly
140 // grow the tooltip verticaly
141 Tooltip.prototype.expand = function () {
141 Tooltip.prototype.expand = function () {
142 this.text.removeClass('smalltooltip');
142 this.text.removeClass('smalltooltip');
143 this.text.addClass('bigtooltip');
143 this.text.addClass('bigtooltip');
144 $('#expanbutton').hide('slow');
144 $('#expanbutton').hide('slow');
145 }
145 }
146
146
147 // deal with all the logic of hiding the tooltip
147 // deal with all the logic of hiding the tooltip
148 // and reset it's status
148 // and reset it's status
149 Tooltip.prototype._hide = function () {
149 Tooltip.prototype._hide = function () {
150 this.tooltip.fadeOut('fast');
150 this.tooltip.fadeOut('fast');
151 $('#expanbutton').show('slow');
151 $('#expanbutton').show('slow');
152 this.text.removeClass('bigtooltip');
152 this.text.removeClass('bigtooltip');
153 this.text.addClass('smalltooltip');
153 this.text.addClass('smalltooltip');
154 // keep scroll top to be sure to always see the first line
154 // keep scroll top to be sure to always see the first line
155 this.text.scrollTop(0);
155 this.text.scrollTop(0);
156 this._hidden = true;
156 this._hidden = true;
157 this.code_mirror = null;
157 this.code_mirror = null;
158 }
158 }
159
159
160 Tooltip.prototype.remove_and_cancel_tooltip = function (force) {
160 Tooltip.prototype.remove_and_cancel_tooltip = function (force) {
161 // note that we don't handle closing directly inside the calltip
161 // note that we don't handle closing directly inside the calltip
162 // as in the completer, because it is not focusable, so won't
162 // as in the completer, because it is not focusable, so won't
163 // get the event.
163 // get the event.
164 if (this._sticky == false || force == true) {
164 if (this._sticky == false || force == true) {
165 this.cancel_stick();
165 this.cancel_stick();
166 this._hide();
166 this._hide();
167 }
167 }
168 this.cancel_pending();
168 this.cancel_pending();
169 this.reset_tabs_function();
169 this.reset_tabs_function();
170 }
170 }
171
171
172 // cancel autocall done after '(' for example.
172 // cancel autocall done after '(' for example.
173 Tooltip.prototype.cancel_pending = function () {
173 Tooltip.prototype.cancel_pending = function () {
174 if (this._tooltip_timeout != null) {
174 if (this._tooltip_timeout != null) {
175 clearTimeout(this._tooltip_timeout);
175 clearTimeout(this._tooltip_timeout);
176 this._tooltip_timeout = null;
176 this._tooltip_timeout = null;
177 }
177 }
178 }
178 }
179
179
180 // will trigger tooltip after timeout
180 // will trigger tooltip after timeout
181 Tooltip.prototype.pending = function (cell) {
181 Tooltip.prototype.pending = function (cell) {
182 var that = this;
182 var that = this;
183 this._tooltip_timeout = setTimeout(function () {
183 this._tooltip_timeout = setTimeout(function () {
184 that.request(cell)
184 that.request(cell)
185 }, that.time_before_tooltip);
185 }, that.time_before_tooltip);
186 }
186 }
187
187
188 Tooltip.prototype._request_tooltip = function (cell, func) {
188 Tooltip.prototype._request_tooltip = function (cell, func) {
189 // use internally just to make the request to the kernel
189 // use internally just to make the request to the kernel
190 // Feel free to shorten this logic if you are better
190 // Feel free to shorten this logic if you are better
191 // than me in regEx
191 // than me in regEx
192 // basicaly you shoul be able to get xxx.xxx.xxx from
192 // basicaly you shoul be able to get xxx.xxx.xxx from
193 // something(range(10), kwarg=smth) ; xxx.xxx.xxx( firstarg, rand(234,23), kwarg1=2,
193 // something(range(10), kwarg=smth) ; xxx.xxx.xxx( firstarg, rand(234,23), kwarg1=2,
194 // remove everything between matchin bracket (need to iterate)
194 // remove everything between matchin bracket (need to iterate)
195 var matchBracket = /\([^\(\)]+\)/g;
195 var matchBracket = /\([^\(\)]+\)/g;
196 var endBracket = /\([^\(]*$/g;
196 var endBracket = /\([^\(]*$/g;
197 var oldfunc = func;
197 var oldfunc = func;
198
198
199 func = func.replace(matchBracket, "");
199 func = func.replace(matchBracket, "");
200 while (oldfunc != func) {
200 while (oldfunc != func) {
201 oldfunc = func;
201 oldfunc = func;
202 func = func.replace(matchBracket, "");
202 func = func.replace(matchBracket, "");
203 }
203 }
204 // remove everything after last open bracket
204 // remove everything after last open bracket
205 func = func.replace(endBracket, "");
205 func = func.replace(endBracket, "");
206
206
207 var re = /[a-z_][0-9a-z._]+$/gi; // casse insensitive
207 var re = /[a-z_][0-9a-z._]+$/gi; // casse insensitive
208 var callbacks = {
208 var callbacks = {
209 'object_info_reply': $.proxy(this._show, this)
209 'object_info_reply': $.proxy(this._show, this)
210 }
210 }
211 var msg_id = cell.kernel.object_info_request(re.exec(func), callbacks);
211 var msg_id = cell.kernel.object_info_request(re.exec(func), callbacks);
212 }
212 }
213
213
214 // make an imediate completion request
214 // make an imediate completion request
215 Tooltip.prototype.request = function (cell) {
215 Tooltip.prototype.request = function (cell) {
216 // request(codecell)
216 // request(codecell)
217 // Deal with extracting the text from the cell and counting
217 // Deal with extracting the text from the cell and counting
218 // call in a row
218 // call in a row
219 this.cancel_pending();
219 this.cancel_pending();
220 var editor = cell.code_mirror;
220 var editor = cell.code_mirror;
221 var cursor = editor.getCursor();
221 var cursor = editor.getCursor();
222 var text = editor.getRange({
222 var text = editor.getRange({
223 line: cursor.line,
223 line: cursor.line,
224 ch: 0
224 ch: 0
225 }, cursor).trim();
225 }, cursor).trim();
226
226
227 if(editor.somethingSelected()){
227 if(editor.somethingSelected()){
228 text = editor.getSelection();
228 text = editor.getSelection();
229 }
229 }
230
230
231 // need a permanent handel to code_mirror for future auto recall
231 // need a permanent handel to code_mirror for future auto recall
232 this.code_mirror = editor;
232 this.code_mirror = editor;
233
233
234 // now we treat the different number of keypress
234 // now we treat the different number of keypress
235 // first if same cell, same text, increment counter by 1
235 // first if same cell, same text, increment counter by 1
236 if (this._old_cell == cell && this._old_request == text && this._hidden == false) {
236 if (this._old_cell == cell && this._old_request == text && this._hidden == false) {
237 this._consecutive_counter++;
237 this._consecutive_counter++;
238 } else {
238 } else {
239 // else reset
239 // else reset
240 this.cancel_stick();
240 this.cancel_stick();
241 this.reset_tabs_function (cell, text);
241 this.reset_tabs_function (cell, text);
242 }
242 }
243
243
244 // don't do anything if line beggin with '(' or is empty
244 // don't do anything if line beggin with '(' or is empty
245 if (text === "" || text === "(") {
245 if (text === "" || text === "(") {
246 return;
246 return;
247 }
247 }
248
248
249 this.tabs_functions[this._consecutive_counter](cell, text);
249 this.tabs_functions[this._consecutive_counter](cell, text);
250
250
251 // then if we are at the end of list function, reset
251 // then if we are at the end of list function, reset
252 if (this._consecutive_counter == this.tabs_functions.length) this.reset_tabs_function (cell, text);
252 if (this._consecutive_counter == this.tabs_functions.length) this.reset_tabs_function (cell, text);
253
253
254 return;
254 return;
255 }
255 }
256
256
257 // cancel the option of having the tooltip to stick
257 // cancel the option of having the tooltip to stick
258 Tooltip.prototype.cancel_stick = function () {
258 Tooltip.prototype.cancel_stick = function () {
259 clearTimeout(this._stick_timeout);
259 clearTimeout(this._stick_timeout);
260 this._stick_timeout = null;
260 this._stick_timeout = null;
261 this._clocklink.hide('slow');
261 this._clocklink.hide('slow');
262 this._sticky = false;
262 this._sticky = false;
263 }
263 }
264
264
265 // put the tooltip in a sicky state for 10 seconds
265 // put the tooltip in a sicky state for 10 seconds
266 // it won't be removed by remove_and_cancell() unless you called with
266 // it won't be removed by remove_and_cancell() unless you called with
267 // the first parameter set to true.
267 // the first parameter set to true.
268 // remove_and_cancell_tooltip(true)
268 // remove_and_cancell_tooltip(true)
269 Tooltip.prototype.stick = function (time) {
269 Tooltip.prototype.stick = function (time) {
270 time = (time != undefined) ? time : 10;
270 time = (time != undefined) ? time : 10;
271 var that = this;
271 var that = this;
272 this._sticky = true;
272 this._sticky = true;
273 this._clocklink.show('slow');
273 this._clocklink.show('slow');
274 this._stick_timeout = setTimeout(function () {
274 this._stick_timeout = setTimeout(function () {
275 that._sticky = false;
275 that._sticky = false;
276 that._clocklink.hide('slow');
276 that._clocklink.hide('slow');
277 }, time * 1000);
277 }, time * 1000);
278 }
278 }
279
279
280 // should be called with the kernel reply to actually show the tooltip
280 // should be called with the kernel reply to actually show the tooltip
281 Tooltip.prototype._show = function (reply) {
281 Tooltip.prototype._show = function (reply) {
282 // move the bubble if it is not hidden
282 // move the bubble if it is not hidden
283 // otherwise fade it
283 // otherwise fade it
284 this.name = reply.name;
284 this.name = reply.name;
285
285
286 // do some math to have the tooltip arrow on more or less on left or right
286 // do some math to have the tooltip arrow on more or less on left or right
287 // width of the editor
287 // width of the editor
288 var w = $(this.code_mirror.getScrollerElement()).width();
288 var w = $(this.code_mirror.getScrollerElement()).width();
289 // ofset of the editor
289 // ofset of the editor
290 var o = $(this.code_mirror.getScrollerElement()).offset();
290 var o = $(this.code_mirror.getScrollerElement()).offset();
291
291
292 // whatever anchor/head order but arrow at mid x selection
292 // whatever anchor/head order but arrow at mid x selection
293 var anchor = this.code_mirror.cursorCoords(false);
293 var anchor = this.code_mirror.cursorCoords(false);
294 var head = this.code_mirror.cursorCoords(true);
294 var head = this.code_mirror.cursorCoords(true);
295 var pos = {};
295 var pos = {};
296 pos.y = head.y
296 pos.y = head.top
297 pos.yBot = head.yBot
297 pos.yBot = head.bottom
298 pos.x = (head.x+anchor.x)/2;
298 pos.x = (head.left+anchor.left)/2;
299
299
300 var xinit = pos.x;
300 var xinit = pos.x;
301 var xinter = o.left + (xinit - o.left) / w * (w - 450);
301 var xinter = o.left + (xinit - o.left) / w * (w - 450);
302 var posarrowleft = xinit - xinter;
302 var posarrowleft = xinit - xinter;
303
303
304
304
305 if (this._hidden == false) {
305 if (this._hidden == false) {
306 this.tooltip.animate({
306 this.tooltip.animate({
307 'left': xinter - 30 + 'px',
307 'left': xinter - 30 + 'px',
308 'top': (pos.yBot + 10) + 'px'
308 'top': (pos.yBot + 10) + 'px'
309 });
309 });
310 } else {
310 } else {
311 this.tooltip.css({
311 this.tooltip.css({
312 'left': xinter - 30 + 'px'
312 'left': xinter - 30 + 'px'
313 });
313 });
314 this.tooltip.css({
314 this.tooltip.css({
315 'top': (pos.yBot + 10) + 'px'
315 'top': (pos.yBot + 10) + 'px'
316 });
316 });
317 }
317 }
318 this.arrow.animate({
318 this.arrow.animate({
319 'left': posarrowleft + 'px'
319 'left': posarrowleft + 'px'
320 });
320 });
321 this.tooltip.fadeIn('fast');
321 this.tooltip.fadeIn('fast');
322 this._hidden = false;
322 this._hidden = false;
323
323
324 // build docstring
324 // build docstring
325 var defstring = reply.call_def;
325 var defstring = reply.call_def;
326 if (defstring == null) {
326 if (defstring == null) {
327 defstring = reply.init_definition;
327 defstring = reply.init_definition;
328 }
328 }
329 if (defstring == null) {
329 if (defstring == null) {
330 defstring = reply.definition;
330 defstring = reply.definition;
331 }
331 }
332
332
333 var docstring = reply.call_docstring;
333 var docstring = reply.call_docstring;
334 if (docstring == null) {
334 if (docstring == null) {
335 docstring = reply.init_docstring;
335 docstring = reply.init_docstring;
336 }
336 }
337 if (docstring == null) {
337 if (docstring == null) {
338 docstring = reply.docstring;
338 docstring = reply.docstring;
339 }
339 }
340 if (docstring == null) {
340 if (docstring == null) {
341 docstring = "<empty docstring>";
341 docstring = "<empty docstring>";
342 }
342 }
343
343
344 this.text.children().remove();
344 this.text.children().remove();
345
345
346 var pre = $('<pre/>').html(utils.fixConsole(docstring));
346 var pre = $('<pre/>').html(utils.fixConsole(docstring));
347 if (defstring) {
347 if (defstring) {
348 var defstring_html = $('<pre/>').html(utils.fixConsole(defstring));
348 var defstring_html = $('<pre/>').html(utils.fixConsole(defstring));
349 this.text.append(defstring_html);
349 this.text.append(defstring_html);
350 }
350 }
351 this.text.append(pre);
351 this.text.append(pre);
352 // keep scroll top to be sure to always see the first line
352 // keep scroll top to be sure to always see the first line
353 this.text.scrollTop(0);
353 this.text.scrollTop(0);
354 }
354 }
355
355
356
356
357 IPython.Tooltip = Tooltip;
357 IPython.Tooltip = Tooltip;
358
358
359 return IPython;
359 return IPython;
360
360
361 }(IPython));
361 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now