##// END OF EJS Templates
clean code, show clock if tooltip is 'sticky'...
Matthias BUSSONNIER -
Show More
@@ -11,8 +11,8
11 11 //
12 12 // you can set the autocall time by setting `IPython.tooltip.time_before_tooltip` in ms
13 13 //
14 // you can configure the differents action of pressing tab several times in a row by
15 // setting/appending different fonction in the array
14 // you can configure the differents action of pressing tab several times in a row by
15 // setting/appending different fonction in the array
16 16 // IPython.tooltip.tabs_functions
17 17 //
18 18 // eg :
@@ -20,9 +20,10
20 20 //
21 21
22 22 var IPython = (function (IPython) {
23 "use strict";
23 24
24 25 var utils = IPython.utils;
25
26
26 27 // tooltip constructor
27 28 var Tooltip = function () {
28 29 var that = this;
@@ -56,6 +57,7 var IPython = (function (IPython) {
56 57 .addClass("ui-corner-all") //rounded corner
57 58 .attr('role',"button")
58 59 .attr('id','expanbutton')
60 .attr('title','Grow the tooltip vertically (press tab 2 times)')
59 61 .click(function(){that.expand()})
60 62 .append(
61 63 $('<span/>').text('Expand')
@@ -66,7 +68,8 var IPython = (function (IPython) {
66 68 // open in pager
67 69 var morelink=$('<a/>').attr('href',"#")
68 70 .attr('role',"button")
69 .addClass('ui-button');
71 .addClass('ui-button')
72 .attr('title','show the current docstring in pager (press tab 4 times)');
70 73 var morespan=$('<span/>').text('Open in Pager')
71 74 .addClass('ui-icon')
72 75 .addClass('ui-icon-arrowstop-l-n');
@@ -76,22 +79,40 var IPython = (function (IPython) {
76 79 });
77 80
78 81 // close the tooltip
79 var closelink=$('<a/>').attr('href',"#");
80 closelink.attr('role',"button");
81 closelink.addClass('ui-button');
82 var closespan=$('<span/>').text('Close');
83 closespan.addClass('ui-icon');
84 closespan.addClass('ui-icon-close');
82 var closelink=$('<a/>').attr('href',"#")
83 .attr('role',"button")
84 .addClass('ui-button');
85 var closespan=$('<span/>').text('Close')
86 .addClass('ui-icon')
87 .addClass('ui-icon-close');
85 88 closelink.append(closespan);
86 89 closelink.click(function(){
87 90 that.remove_and_cancel_tooltip(true);
88 91 });
89 92
93 this._clocklink=$('<a/>').attr('href',"#");
94 this._clocklink.attr('role',"button");
95 this._clocklink.addClass('ui-button');
96 this._clocklink.attr('title','Tootip is not dismissed while typing for 10 seconds');
97 var clockspan=$('<span/>').text('Close');
98 clockspan.addClass('ui-icon');
99 clockspan.addClass('ui-icon-clock');
100 this._clocklink.append(clockspan);
101 this._clocklink.click(function(){
102 that.cancel_stick();
103 });
104
105
106
107
90 108 //construct the tooltip
91 109 // add in the reverse order you want them to appear
92 110 this.buttons.append(closelink);
93 111 this.buttons.append(expandlink);
94 112 this.buttons.append(morelink);
113 this.buttons.append(this._clocklink);
114 this._clocklink.hide();
115
95 116
96 117 // we need a phony element to make the small arrow
97 118 // of the tooltip in css
@@ -101,7 +122,8 var IPython = (function (IPython) {
101 122 this.tooltip.append(this.arrow);
102 123 this.tooltip.append(this.text);
103 124
104 this.tabs_functions = [
125 // function that will be called if you press tab 1, 2, 3... times in a row
126 this.tabs_functions = [ function(cell,text){that._request_tooltip(cell,text)},
105 127 function(){that.expand()},
106 128 function(){that.stick()},
107 129 function(){
@@ -116,7 +138,6 var IPython = (function (IPython) {
116 138 this._old_cell = (cell)? cell : null ;
117 139 this._old_request = (text) ? text : null ;
118 140 this._consecutive_counter = 0;
119 this.cancel_stick();
120 141 }
121 142 };
122 143
@@ -135,7 +156,7 var IPython = (function (IPython) {
135 156 Tooltip.prototype.expand = function(){
136 157 this.text.removeClass('smalltooltip');
137 158 this.text.addClass('bigtooltip');
138 $('#expanbutton').addClass('hidden');
159 $('#expanbutton').hide('slow');
139 160 this._cmfocus();
140 161 }
141 162
@@ -144,7 +165,7 var IPython = (function (IPython) {
144 165 Tooltip.prototype.hide = function()
145 166 {
146 167 this.tooltip.addClass('hide');
147 $('#expanbutton').removeClass('hidden');
168 $('#expanbutton').show('slow');
148 169 this.text.removeClass('bigtooltip');
149 170 this.text.addClass('smalltooltip');
150 171 // keep scroll top to be sure to always see the first line
@@ -161,34 +182,32 var IPython = (function (IPython) {
161 182 this.hide();
162 183 }
163 184 this.cancel_pending();
164 this._old_cell = null ;
165 this._old_request = null ;
166 this._consecutive_counter = 0;
185 this.reset_tabs_function();
167 186 }
168 187
169 188 // cancel autocall done after '(' for example.
170 189 Tooltip.prototype.cancel_pending = function(){
171 if (this.tooltip_timeout != null){
172 clearTimeout(this.tooltip_timeout);
173 this.tooltip_timeout = null;
190 if (this._tooltip_timeout != null){
191 clearTimeout(this._tooltip_timeout);
192 this._tooltip_timeout = null;
174 193 }
175 194 }
176 195
177 196 // will trigger tooltip after timeout
178 Tooltip.prototype.pending = function(cell,text)
197 Tooltip.prototype.pending = function(cell)
179 198 {
180 199 var that = this;
181 this.tooltip_timeout = setTimeout(function(){that.request(cell)} , that.time_before_tooltip);
200 this._tooltip_timeout = setTimeout(function(){that.request(cell)} , that.time_before_tooltip);
182 201 }
183
184 Tooltip.prototype._request_tooltip = function(func)
202
203 Tooltip.prototype._request_tooltip = function(cell,func)
185 204 {
186 // use internally just to maek the request to the kernel
205 // use internally just to make the request to the kernel
187 206
188 207 // Feel free to shorten this logic if you are better
189 208 // than me in regEx
190 // basicaly you shoul be able to get xxx.xxx.xxx from
191 // something(range(10), kwarg=smth) ; xxx.xxx.xxx( firstarg, rand(234,23), kwarg1=2,
209 // basicaly you shoul be able to get xxx.xxx.xxx from
210 // something(range(10), kwarg=smth) ; xxx.xxx.xxx( firstarg, rand(234,23), kwarg1=2,
192 211 // remove everything between matchin bracket (need to iterate)
193 212 var matchBracket = /\([^\(\)]+\)/g;
194 213 var endBracket = /\([^\(]*$/g;
@@ -197,11 +216,12 var IPython = (function (IPython) {
197 216 func = func.replace(matchBracket,"");
198 217 while( oldfunc != func )
199 218 {
200 oldfunc = func;
201 func = func.replace(matchBracket,"");
219 oldfunc = func;
220 func = func.replace(matchBracket,"");
202 221 }
203 // remove everythin after last open bracket
222 // remove everything after last open bracket
204 223 func = func.replace(endBracket,"");
224
205 225 var re = /[a-z_][0-9a-z._]+$/gi; // casse insensitive
206 226 var callbacks = {'object_info_reply': $.proxy(this.show,this)}
207 227 var msg_id = IPython.notebook.kernel.object_info_request(re.exec(func), callbacks);
@@ -210,53 +230,50 var IPython = (function (IPython) {
210 230 // make an imediate completion request
211 231 Tooltip.prototype.request = function(cell)
212 232 {
213 // request(codecell)
233 // request(codecell)
214 234 // Deal with extracting the text from the cell and counting
215 235 // call in a row
216
236
217 237 this.cancel_pending();
218 238 var editor = cell.code_mirror;
219 239 var cursor = editor.getCursor();
220 240 var text = editor.getRange({line:cursor.line,ch:0},cursor).trim();
221
241
222 242 // need a permanent handel to codemirror for future auto recall
223 243 this.code_mirror = editor;
224 244
225
226 // now we treat the different kind of keypress
245 // now we treat the different number of keypress
227 246 // first if same cell, same text, increment counter by 1
228 247 if( this._old_cell == cell && this._old_request == text && this._hidden == false)
229 248 {
230 this._consecutive_counter = this._consecutive_counter +1;
249 this._consecutive_counter++;
231 250 } else {
232 251 // else reset
252 this.cancel_stick();
233 253 this.reset_tabs_function(cell,text);
234 254 }
235
255
236 256 // don't do anything if line beggin with '(' or is empty
237 257 if (text === "" || text === "(" ) {
238 258 return;
239 259 }
240
241 if (this._consecutive_counter == 0) {
242 // make a kernel request
243 this._request_tooltip(text);
244 } else if (this._consecutive_counter == this.tabs_functions.length) {
245 // then if we are at the end of list function, reset
246 // and call the last function after
247 this.tabs_functions[this._consecutive_counter-1]();
260
261 this.tabs_functions[this._consecutive_counter](cell,text);
262
263 // then if we are at the end of list function, reset
264 if (this._consecutive_counter == this.tabs_functions.length)
248 265 this.reset_tabs_function(cell,text);
249 } else {
250 // otherwise, call the nth function of the list
251 this.tabs_functions[this._consecutive_counter-1]();
252 }
266
253 267 return;
254 268 }
255 269
256 270 // cancel the option of having the tooltip to stick
257 271 Tooltip.prototype.cancel_stick = function()
258 272 {
273 console.log('cancel stick');
259 274 clearTimeout(this._stick_timeout);
275 this._stick_timeout = null;
276 this._clocklink.hide('slow');
260 277 this._sticky = false;
261 278 }
262 279
@@ -268,8 +285,10 var IPython = (function (IPython) {
268 285 {
269 286 var that = this;
270 287 this._sticky = true;
288 this._clocklink.show('slow');
271 289 this._stick_timeout = setTimeout( function(){
272 290 that._sticky = false;
291 that._clocklink.hide('slow');
273 292 }, 10*1000
274 293 );
275 294 }
@@ -279,15 +298,14 var IPython = (function (IPython) {
279 298 {
280 299 // move the bubble if it is not hidden
281 300 // otherwise fade it
282 var editor = this.code_mirror;
283 301 this.name = reply.name;
284 302
285 303 // do some math to have the tooltip arrow on more or less on left or right
286 304 // width of the editor
287 var w= $(this.code_mirror.getScrollerElement()).width();
305 var w = $(this.code_mirror.getScrollerElement()).width();
288 306 // ofset of the editor
289 var o= $(this.code_mirror.getScrollerElement()).offset();
290 var pos = editor.cursorCoords();
307 var o = $(this.code_mirror.getScrollerElement()).offset();
308 var pos = this.code_mirror.cursorCoords();
291 309 var xinit = pos.x;
292 310 var xinter = o.left + (xinit-o.left)/w*(w-450);
293 311 var posarrowleft = xinit - xinter;
@@ -307,11 +325,11 var IPython = (function (IPython) {
307 325 this._hidden = false;
308 326
309 327 // build docstring
310 defstring = reply.call_def;
328 var defstring = reply.call_def;
311 329 if (defstring == null) { defstring = reply.init_definition; }
312 330 if (defstring == null) { defstring = reply.definition; }
313 331
314 docstring = reply.call_docstring;
332 var docstring = reply.call_docstring;
315 333 if (docstring == null) { docstring = reply.init_docstring; }
316 334 if (docstring == null) { docstring = reply.docstring; }
317 335 if (docstring == null) { docstring = "<empty docstring>"; }
@@ -336,5 +354,7 var IPython = (function (IPython) {
336 354 }
337 355
338 356 IPython.Tooltip = Tooltip;
357
339 358 return IPython;
359
340 360 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now