Show More
@@ -1,297 +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 | console.log('should open in pager'); | |
|
190 | 189 | this._old_cell = null ; |
|
191 | 190 | this._cancel_stick(); |
|
192 | 191 | this._old_request = null ; |
|
193 | 192 | this._consecutive_conter = 0; |
|
194 | 193 | this.showInPager(); |
|
195 | 194 | this._cmfocus(); |
|
196 | 195 | return; |
|
197 | 196 | } |
|
198 | 197 | else if( this._consecutive_conter == 4) |
|
199 | 198 | { |
|
200 | 199 | |
|
201 | 200 | } |
|
202 | 201 | |
|
203 | 202 | if (text === "" || text === "(" ) { |
|
204 | 203 | return; |
|
205 | 204 | // don't do anything if line beggin with '(' or is empty |
|
206 | 205 | } |
|
207 | 206 | IPython.notebook.request_tool_tip(cell, text); |
|
208 | 207 | } |
|
209 | 208 | |
|
210 | 209 | // cancel the option of having the tooltip to stick |
|
211 | 210 | Tooltip.prototype.cancel_stick = function() |
|
212 | 211 | { |
|
213 | 212 | clearTimeout(this._stick_timeout); |
|
214 | 213 | this._sticky = false; |
|
215 | 214 | } |
|
216 | 215 | |
|
217 | 216 | // put the tooltip in a sicky state for 10 seconds |
|
218 | 217 | // it won't be removed by remove_and_cancell() unless you called with |
|
219 | 218 | // the first parameter set to true. |
|
220 | 219 | // remove_and_cancell_tooltip(true) |
|
221 | 220 | Tooltip.prototype.stick = function() |
|
222 | 221 | { |
|
223 | console.log('tooltip will stick for at least 10 sec'); | |
|
224 | 222 | var that = this; |
|
225 | 223 | this._sticky = true; |
|
226 | 224 | this._stick_timeout = setTimeout( function(){ |
|
227 | 225 | that._sticky = false; |
|
228 | console.log('tooltip will not stick anymore'); | |
|
229 | 226 | }, 10*1000 |
|
230 | 227 | ); |
|
231 | 228 | } |
|
232 | 229 | |
|
233 | 230 | // should be called with the kernel reply to actually show the tooltip |
|
234 | 231 | Tooltip.prototype.show = function(reply, codecell) |
|
235 | 232 | { |
|
236 | 233 | // move the bubble if it is not hidden |
|
237 | 234 | // otherwise fade it |
|
238 | 235 | var editor = codecell.code_mirror; |
|
239 | 236 | this.name = reply.name; |
|
240 | 237 | this.code_mirror = editor; |
|
241 | 238 | |
|
242 | 239 | // do some math to have the tooltip arrow on more or less on left or right |
|
243 | 240 | // width of the editor |
|
244 | 241 | var w= $(this.code_mirror.getScrollerElement()).width(); |
|
245 | 242 | // ofset of the editor |
|
246 | 243 | var o= $(this.code_mirror.getScrollerElement()).offset(); |
|
247 | 244 | var pos = editor.cursorCoords(); |
|
248 | 245 | var xinit = pos.x; |
|
249 | 246 | var xinter = o.left + (xinit-o.left)/w*(w-450); |
|
250 | 247 | var posarrowleft = xinit - xinter; |
|
251 | 248 | |
|
252 | 249 | |
|
253 | 250 | if( this._hidden == false) |
|
254 | 251 | { |
|
255 | 252 | this.tooltip.animate({'left' : xinter-30+'px','top' :(pos.yBot+10)+'px'}); |
|
256 | 253 | } else |
|
257 | 254 | { |
|
258 | 255 | this.tooltip.css({'left' : xinter-30+'px'}); |
|
259 | 256 | this.tooltip.css({'top' :(pos.yBot+10)+'px'}); |
|
260 | 257 | } |
|
261 | 258 | this.arrow.animate({'left' : posarrowleft+'px'}); |
|
262 | 259 | this.tooltip.removeClass('hidden') |
|
263 | 260 | this.tooltip.removeClass('hide'); |
|
264 | 261 | this._hidden = false; |
|
265 | 262 | |
|
266 | 263 | // build docstring |
|
267 | 264 | defstring = reply.call_def; |
|
268 | 265 | if (defstring == null) { defstring = reply.init_definition; } |
|
269 | 266 | if (defstring == null) { defstring = reply.definition; } |
|
270 | 267 | |
|
271 | 268 | docstring = reply.call_docstring; |
|
272 | 269 | if (docstring == null) { docstring = reply.init_docstring; } |
|
273 | 270 | if (docstring == null) { docstring = reply.docstring; } |
|
274 | 271 | if (docstring == null) { docstring = "<empty docstring>"; } |
|
275 | 272 | |
|
276 | 273 | this.text.children().remove(); |
|
277 | 274 | |
|
278 | 275 | var pre=$('<pre/>').html(utils.fixConsole(docstring)); |
|
279 | 276 | if(defstring){ |
|
280 | 277 | var defstring_html = $('<pre/>').html(utils.fixConsole(defstring)); |
|
281 | 278 | this.text.append(defstring_html); |
|
282 | 279 | } |
|
283 | 280 | this.text.append(pre); |
|
284 | 281 | // keep scroll top to be sure to always see the first line |
|
285 | 282 | this.text.scrollTop(0); |
|
286 | 283 | } |
|
287 | 284 | |
|
288 | 285 | // convenient funciton to have the correct codemirror back into focus |
|
289 | 286 | Tooltip.prototype._cmfocus = function() |
|
290 | 287 | { |
|
291 | 288 | var cm = this.code_mirror; |
|
292 | 289 | setTimeout(function(){cm.focus();}, 50); |
|
293 | 290 | } |
|
294 | 291 | |
|
295 | 292 | IPython.Tooltip = Tooltip; |
|
296 | 293 | return IPython; |
|
297 | 294 | }(IPython)); |
General Comments 0
You need to be logged in to leave comments.
Login now