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