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