##// END OF EJS Templates
fix cancel_stick typo
Matthias BUSSONNIER -
Show More
@@ -1,294 +1,294 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 //============================================================================
9 9 // Tooltip
10 10 //============================================================================
11 11 //
12 12 // you can set the autocall time by setting `IPython.notebook.time_before_tooltip` in ms
13 13
14 14 var IPython = (function (IPython) {
15 15
16 16 var utils = IPython.utils;
17 17
18 18 // tooltip constructor
19 19 var Tooltip = function (notebook) {
20 20 var that = this;
21 21
22 22 // handle to html
23 23 this.tooltip = $('#tooltip');
24 24 var tooltip = this.tooltip;
25 25 this._hidden = true;
26 26
27 27 // variable for consecutive call
28 28 this._old_cell = null ;
29 29 this._old_request = null ;
30 30 this._consecutive_conter = 0;
31 31
32 32 // 'sticky ?'
33 33 this._sticky = false;
34 34
35 35 // contain the button in the upper right corner
36 36 this.buttons = $('<div/>')
37 37 .addClass('tooltipbuttons');
38 38
39 39 // will contain the docstring
40 40 this.text = $('<div/>')
41 41 .addClass('tooltiptext')
42 42 .addClass('smalltooltip');
43 43
44 44 // build the buttons menu on the upper right
45 45
46 46 // expand the tooltip to see more
47 47 var expandlink=$('<a/>').attr('href',"#")
48 48 .addClass("ui-corner-all") //rounded corner
49 49 .attr('role',"button")
50 50 .attr('id','expanbutton')
51 51 .click(function(){that.expand()})
52 52 .append(
53 53 $('<span/>').text('Expand')
54 54 .addClass('ui-icon')
55 55 .addClass('ui-icon-plus')
56 56 );
57 57
58 58 // open in pager
59 59 var morelink=$('<a/>').attr('href',"#")
60 60 .attr('role',"button")
61 61 .addClass('ui-button');
62 62 var morespan=$('<span/>').text('Open in Pager')
63 63 .addClass('ui-icon')
64 64 .addClass('ui-icon-arrowstop-l-n');
65 65 morelink.append(morespan);
66 66 morelink.click(function(){
67 67 that.showInPager();
68 68 });
69 69
70 70 // close the tooltip
71 71 var closelink=$('<a/>').attr('href',"#");
72 72 closelink.attr('role',"button");
73 73 closelink.addClass('ui-button');
74 74 var closespan=$('<span/>').text('Close');
75 75 closespan.addClass('ui-icon');
76 76 closespan.addClass('ui-icon-close');
77 77 closelink.append(closespan);
78 78 closelink.click(function(){
79 79 that.remove_and_cancel_tooltip(true);
80 80 });
81 81
82 82 //construct the tooltip
83 83 // add in the reverse order you want them to appear
84 84 this.buttons.append(closelink);
85 85 this.buttons.append(expandlink);
86 86 this.buttons.append(morelink);
87 87
88 88 // we need a phony element to make the small arrow
89 89 // of the tooltip in css
90 90 // we will move the arrow later
91 91 this.arrow = $('<div/>').addClass('pretooltiparrow');
92 92 this.tooltip.append(this.buttons);
93 93 this.tooltip.append(this.arrow);
94 94 this.tooltip.append(this.text);
95 95 };
96 96
97 97 // will resend the request on behalf on the cell which invoked the tooltip
98 98 // to show in it in pager. This is done so to be sure of having the same
99 99 // result as invoking `something?`
100 100 Tooltip.prototype.showInPager = function()
101 101 {
102 102 var msg_id = IPython.notebook.kernel.execute(that.name+"?");
103 103 IPython.notebook.msg_cell_map[msg_id] = IPython.notebook.get_selected_cell().cell_id;
104 104 this.remove_and_cancel_tooltip();
105 105 this._cmfocus();
106 106 }
107 107
108 108 // grow the tooltip verticaly
109 109 Tooltip.prototype.expand = function(){
110 110 this.text.removeClass('smalltooltip');
111 111 this.text.addClass('bigtooltip');
112 112 $('#expanbutton').addClass('hidden');
113 113 this._cmfocus();
114 114 }
115 115
116 116 // deal with all the logic of hiding the tooltip
117 117 // and reset it's status
118 118 Tooltip.prototype.hide = function()
119 119 {
120 120 this.tooltip.addClass('hide');
121 121 $('#expanbutton').removeClass('hidden');
122 122 this.text.removeClass('bigtooltip');
123 123 this.text.addClass('smalltooltip');
124 124 // keep scroll top to be sure to always see the first line
125 125 this.text.scrollTop(0);
126 126 this._hidden = true;
127 127 }
128 128
129 129 Tooltip.prototype.remove_and_cancel_tooltip = function(force) {
130 130 // note that we don't handle closing directly inside the calltip
131 131 // as in the completer, because it is not focusable, so won't
132 132 // get the event.
133 133 if(this._sticky == false || force == true)
134 134 {
135 135 this.hide();
136 136 }
137 137 this.cancel_pending();
138 138 this._old_cell = null ;
139 139 this._old_request = null ;
140 140 this._consecutive_conter = 0;
141 141 }
142 142
143 143 // cancel autocall done after '(' for example.
144 144 Tooltip.prototype.cancel_pending = function(){
145 145 if (this.tooltip_timeout != null){
146 146 clearTimeout(this.tooltip_timeout);
147 147 this.tooltip_timeout = null;
148 148 }
149 149 }
150 150
151 151 // will trigger tooltip after timeout
152 152 Tooltip.prototype.pending = function(cell,text)
153 153 {
154 154 var that = this;
155 155 this.tooltip_timeout = setTimeout(function(){that.request(cell)} , IPython.notebook.time_before_tooltip);
156 156 }
157 157
158 158 // make an imediate completion request
159 159 Tooltip.prototype.request = function(cell)
160 160 {
161 161 this.cancel_pending();
162 162 var editor = cell.code_mirror;
163 163 this.code_mirror = editor;
164 164 var cursor = editor.getCursor();
165 165 var text = editor.getRange({line:cursor.line,ch:0},cursor).trim();
166 166
167 167 if( this._old_cell == cell && this._old_request == text && this._hidden == false)
168 168 {
169 169 this._consecutive_conter = this._consecutive_conter +1;
170 170 } else {
171 171 this._old_cell = cell ;
172 172 this._old_request = text ;
173 173 this._consecutive_conter =0;
174 174 this.cancel_stick();
175 175 }
176 176
177 177 if( this._consecutive_conter == 1 )
178 178 {
179 179 this.expand()
180 180 return;
181 181 }
182 182 else if( this._consecutive_conter == 2)
183 183 {
184 184 this.stick();
185 185 return;
186 186 }
187 187 else if( this._consecutive_conter == 3)
188 188 {
189 189 this._old_cell = null ;
190 this._cancel_stick();
190 this.cancel_stick();
191 191 this._old_request = null ;
192 192 this._consecutive_conter = 0;
193 193 this.showInPager();
194 194 this._cmfocus();
195 195 return;
196 196 }
197 197 else if( this._consecutive_conter == 4)
198 198 {
199 199
200 200 }
201 201
202 202 if (text === "" || text === "(" ) {
203 203 return;
204 204 // don't do anything if line beggin with '(' or is empty
205 205 }
206 206 IPython.notebook.request_tool_tip(cell, text);
207 207 }
208 208
209 209 // cancel the option of having the tooltip to stick
210 210 Tooltip.prototype.cancel_stick = function()
211 211 {
212 212 clearTimeout(this._stick_timeout);
213 213 this._sticky = false;
214 214 }
215 215
216 216 // put the tooltip in a sicky state for 10 seconds
217 217 // it won't be removed by remove_and_cancell() unless you called with
218 218 // the first parameter set to true.
219 219 // remove_and_cancell_tooltip(true)
220 220 Tooltip.prototype.stick = function()
221 221 {
222 222 var that = this;
223 223 this._sticky = true;
224 224 this._stick_timeout = setTimeout( function(){
225 225 that._sticky = false;
226 226 }, 10*1000
227 227 );
228 228 }
229 229
230 230 // should be called with the kernel reply to actually show the tooltip
231 231 Tooltip.prototype.show = function(reply, codecell)
232 232 {
233 233 // move the bubble if it is not hidden
234 234 // otherwise fade it
235 235 var editor = codecell.code_mirror;
236 236 this.name = reply.name;
237 237 this.code_mirror = editor;
238 238
239 239 // do some math to have the tooltip arrow on more or less on left or right
240 240 // width of the editor
241 241 var w= $(this.code_mirror.getScrollerElement()).width();
242 242 // ofset of the editor
243 243 var o= $(this.code_mirror.getScrollerElement()).offset();
244 244 var pos = editor.cursorCoords();
245 245 var xinit = pos.x;
246 246 var xinter = o.left + (xinit-o.left)/w*(w-450);
247 247 var posarrowleft = xinit - xinter;
248 248
249 249
250 250 if( this._hidden == false)
251 251 {
252 252 this.tooltip.animate({'left' : xinter-30+'px','top' :(pos.yBot+10)+'px'});
253 253 } else
254 254 {
255 255 this.tooltip.css({'left' : xinter-30+'px'});
256 256 this.tooltip.css({'top' :(pos.yBot+10)+'px'});
257 257 }
258 258 this.arrow.animate({'left' : posarrowleft+'px'});
259 259 this.tooltip.removeClass('hidden')
260 260 this.tooltip.removeClass('hide');
261 261 this._hidden = false;
262 262
263 263 // build docstring
264 264 defstring = reply.call_def;
265 265 if (defstring == null) { defstring = reply.init_definition; }
266 266 if (defstring == null) { defstring = reply.definition; }
267 267
268 268 docstring = reply.call_docstring;
269 269 if (docstring == null) { docstring = reply.init_docstring; }
270 270 if (docstring == null) { docstring = reply.docstring; }
271 271 if (docstring == null) { docstring = "<empty docstring>"; }
272 272
273 273 this.text.children().remove();
274 274
275 275 var pre=$('<pre/>').html(utils.fixConsole(docstring));
276 276 if(defstring){
277 277 var defstring_html = $('<pre/>').html(utils.fixConsole(defstring));
278 278 this.text.append(defstring_html);
279 279 }
280 280 this.text.append(pre);
281 281 // keep scroll top to be sure to always see the first line
282 282 this.text.scrollTop(0);
283 283 }
284 284
285 285 // convenient funciton to have the correct codemirror back into focus
286 286 Tooltip.prototype._cmfocus = function()
287 287 {
288 288 var cm = this.code_mirror;
289 289 setTimeout(function(){cm.focus();}, 50);
290 290 }
291 291
292 292 IPython.Tooltip = Tooltip;
293 293 return IPython;
294 294 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now