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