##// END OF EJS Templates
Have remove_and_cancel_tooltip() return a boolean
Takeshi Kanmae -
Show More
@@ -1,446 +1,441
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 // CodeCell
9 // CodeCell
10 //============================================================================
10 //============================================================================
11 /**
11 /**
12 * An extendable module that provide base functionnality to create cell for notebook.
12 * An extendable module that provide base functionnality to create cell for notebook.
13 * @module IPython
13 * @module IPython
14 * @namespace IPython
14 * @namespace IPython
15 * @submodule CodeCell
15 * @submodule CodeCell
16 */
16 */
17
17
18
18
19 /* local util for codemirror */
19 /* local util for codemirror */
20 var posEq = function(a, b) {return a.line == b.line && a.ch == b.ch;}
20 var posEq = function(a, b) {return a.line == b.line && a.ch == b.ch;}
21
21
22 /**
22 /**
23 *
23 *
24 * function to delete until previous non blanking space character
24 * function to delete until previous non blanking space character
25 * or first multiple of 4 tabstop.
25 * or first multiple of 4 tabstop.
26 * @private
26 * @private
27 */
27 */
28 CodeMirror.commands.delSpaceToPrevTabStop = function(cm){
28 CodeMirror.commands.delSpaceToPrevTabStop = function(cm){
29 var from = cm.getCursor(true), to = cm.getCursor(false), sel = !posEq(from, to);
29 var from = cm.getCursor(true), to = cm.getCursor(false), sel = !posEq(from, to);
30 if (!posEq(from, to)) {cm.replaceRange("", from, to); return}
30 if (!posEq(from, to)) {cm.replaceRange("", from, to); return}
31 var cur = cm.getCursor(), line = cm.getLine(cur.line);
31 var cur = cm.getCursor(), line = cm.getLine(cur.line);
32 var tabsize = cm.getOption('tabSize');
32 var tabsize = cm.getOption('tabSize');
33 var chToPrevTabStop = cur.ch-(Math.ceil(cur.ch/tabsize)-1)*tabsize;
33 var chToPrevTabStop = cur.ch-(Math.ceil(cur.ch/tabsize)-1)*tabsize;
34 var from = {ch:cur.ch-chToPrevTabStop,line:cur.line}
34 var from = {ch:cur.ch-chToPrevTabStop,line:cur.line}
35 var select = cm.getRange(from,cur)
35 var select = cm.getRange(from,cur)
36 if( select.match(/^\ +$/) != null){
36 if( select.match(/^\ +$/) != null){
37 cm.replaceRange("",from,cur)
37 cm.replaceRange("",from,cur)
38 } else {
38 } else {
39 cm.deleteH(-1,"char")
39 cm.deleteH(-1,"char")
40 }
40 }
41 };
41 };
42
42
43
43
44 var IPython = (function (IPython) {
44 var IPython = (function (IPython) {
45 "use strict";
45 "use strict";
46
46
47 var utils = IPython.utils;
47 var utils = IPython.utils;
48 var key = IPython.utils.keycodes;
48 var key = IPython.utils.keycodes;
49
49
50 /**
50 /**
51 * A Cell conceived to write code.
51 * A Cell conceived to write code.
52 *
52 *
53 * The kernel doesn't have to be set at creation time, in that case
53 * The kernel doesn't have to be set at creation time, in that case
54 * it will be null and set_kernel has to be called later.
54 * it will be null and set_kernel has to be called later.
55 * @class CodeCell
55 * @class CodeCell
56 * @extends IPython.Cell
56 * @extends IPython.Cell
57 *
57 *
58 * @constructor
58 * @constructor
59 * @param {Object|null} kernel
59 * @param {Object|null} kernel
60 * @param {object|undefined} [options]
60 * @param {object|undefined} [options]
61 * @param [options.cm_config] {object} config to pass to CodeMirror
61 * @param [options.cm_config] {object} config to pass to CodeMirror
62 */
62 */
63 var CodeCell = function (kernel, options) {
63 var CodeCell = function (kernel, options) {
64 this.kernel = kernel || null;
64 this.kernel = kernel || null;
65 this.code_mirror = null;
65 this.code_mirror = null;
66 this.input_prompt_number = null;
66 this.input_prompt_number = null;
67 this.collapsed = false;
67 this.collapsed = false;
68 this.cell_type = "code";
68 this.cell_type = "code";
69
69
70
70
71 var cm_overwrite_options = {
71 var cm_overwrite_options = {
72 onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this)
72 onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this)
73 };
73 };
74
74
75 options = this.mergeopt(CodeCell, options, {cm_config:cm_overwrite_options});
75 options = this.mergeopt(CodeCell, options, {cm_config:cm_overwrite_options});
76
76
77 IPython.Cell.apply(this,[options]);
77 IPython.Cell.apply(this,[options]);
78
78
79 var that = this;
79 var that = this;
80 this.element.focusout(
80 this.element.focusout(
81 function() { that.auto_highlight(); }
81 function() { that.auto_highlight(); }
82 );
82 );
83 };
83 };
84
84
85 CodeCell.options_default = {
85 CodeCell.options_default = {
86 cm_config : {
86 cm_config : {
87 extraKeys: {
87 extraKeys: {
88 "Tab" : "indentMore",
88 "Tab" : "indentMore",
89 "Shift-Tab" : "indentLess",
89 "Shift-Tab" : "indentLess",
90 "Backspace" : "delSpaceToPrevTabStop",
90 "Backspace" : "delSpaceToPrevTabStop",
91 "Cmd-/" : "toggleComment",
91 "Cmd-/" : "toggleComment",
92 "Ctrl-/" : "toggleComment"
92 "Ctrl-/" : "toggleComment"
93 },
93 },
94 mode: 'ipython',
94 mode: 'ipython',
95 theme: 'ipython',
95 theme: 'ipython',
96 matchBrackets: true
96 matchBrackets: true
97 }
97 }
98 };
98 };
99
99
100
100
101 CodeCell.prototype = new IPython.Cell();
101 CodeCell.prototype = new IPython.Cell();
102
102
103 /**
103 /**
104 * @method auto_highlight
104 * @method auto_highlight
105 */
105 */
106 CodeCell.prototype.auto_highlight = function () {
106 CodeCell.prototype.auto_highlight = function () {
107 this._auto_highlight(IPython.config.cell_magic_highlight)
107 this._auto_highlight(IPython.config.cell_magic_highlight)
108 };
108 };
109
109
110 /** @method create_element */
110 /** @method create_element */
111 CodeCell.prototype.create_element = function () {
111 CodeCell.prototype.create_element = function () {
112 IPython.Cell.prototype.create_element.apply(this, arguments);
112 IPython.Cell.prototype.create_element.apply(this, arguments);
113
113
114 var cell = $('<div></div>').addClass('cell border-box-sizing code_cell');
114 var cell = $('<div></div>').addClass('cell border-box-sizing code_cell');
115 cell.attr('tabindex','2');
115 cell.attr('tabindex','2');
116
116
117 this.celltoolbar = new IPython.CellToolbar(this);
117 this.celltoolbar = new IPython.CellToolbar(this);
118
118
119 var input = $('<div></div>').addClass('input');
119 var input = $('<div></div>').addClass('input');
120 var vbox = $('<div/>').addClass('vbox box-flex1')
120 var vbox = $('<div/>').addClass('vbox box-flex1')
121 input.append($('<div/>').addClass('prompt input_prompt'));
121 input.append($('<div/>').addClass('prompt input_prompt'));
122 vbox.append(this.celltoolbar.element);
122 vbox.append(this.celltoolbar.element);
123 var input_area = $('<div/>').addClass('input_area');
123 var input_area = $('<div/>').addClass('input_area');
124 this.code_mirror = CodeMirror(input_area.get(0), this.cm_config);
124 this.code_mirror = CodeMirror(input_area.get(0), this.cm_config);
125 $(this.code_mirror.getInputField()).attr("spellcheck", "false");
125 $(this.code_mirror.getInputField()).attr("spellcheck", "false");
126 vbox.append(input_area);
126 vbox.append(input_area);
127 input.append(vbox);
127 input.append(vbox);
128 var output = $('<div></div>');
128 var output = $('<div></div>');
129 cell.append(input).append(output);
129 cell.append(input).append(output);
130 this.element = cell;
130 this.element = cell;
131 this.output_area = new IPython.OutputArea(output, true);
131 this.output_area = new IPython.OutputArea(output, true);
132
132
133 // construct a completer only if class exist
133 // construct a completer only if class exist
134 // otherwise no print view
134 // otherwise no print view
135 if (IPython.Completer !== undefined)
135 if (IPython.Completer !== undefined)
136 {
136 {
137 this.completer = new IPython.Completer(this);
137 this.completer = new IPython.Completer(this);
138 }
138 }
139 };
139 };
140
140
141 /**
141 /**
142 * This method gets called in CodeMirror's onKeyDown/onKeyPress
142 * This method gets called in CodeMirror's onKeyDown/onKeyPress
143 * handlers and is used to provide custom key handling. Its return
143 * handlers and is used to provide custom key handling. Its return
144 * value is used to determine if CodeMirror should ignore the event:
144 * value is used to determine if CodeMirror should ignore the event:
145 * true = ignore, false = don't ignore.
145 * true = ignore, false = don't ignore.
146 * @method handle_codemirror_keyevent
146 * @method handle_codemirror_keyevent
147 */
147 */
148 CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) {
148 CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) {
149
149
150 var that = this;
150 var that = this;
151 // whatever key is pressed, first, cancel the tooltip request before
151 // whatever key is pressed, first, cancel the tooltip request before
152 // they are sent, and remove tooltip if any, except for tab again
152 // they are sent, and remove tooltip if any, except for tab again
153 if (event.type === 'keydown' && event.which != key.TAB ) {
153 if (event.type === 'keydown' && event.which != key.TAB ) {
154 IPython.tooltip.remove_and_cancel_tooltip();
154 IPython.tooltip.remove_and_cancel_tooltip();
155 };
155 };
156
156
157 var cur = editor.getCursor();
157 var cur = editor.getCursor();
158 if (event.keyCode === key.ENTER){
158 if (event.keyCode === key.ENTER){
159 this.auto_highlight();
159 this.auto_highlight();
160 }
160 }
161
161
162 if (event.keyCode === key.ENTER && (event.shiftKey || event.ctrlKey)) {
162 if (event.keyCode === key.ENTER && (event.shiftKey || event.ctrlKey)) {
163 // Always ignore shift-enter in CodeMirror as we handle it.
163 // Always ignore shift-enter in CodeMirror as we handle it.
164 return true;
164 return true;
165 } else if (event.which === 40 && event.type === 'keypress' && IPython.tooltip.time_before_tooltip >= 0) {
165 } else if (event.which === 40 && event.type === 'keypress' && IPython.tooltip.time_before_tooltip >= 0) {
166 // triger on keypress (!) otherwise inconsistent event.which depending on plateform
166 // triger on keypress (!) otherwise inconsistent event.which depending on plateform
167 // browser and keyboard layout !
167 // browser and keyboard layout !
168 // Pressing '(' , request tooltip, don't forget to reappend it
168 // Pressing '(' , request tooltip, don't forget to reappend it
169 // The second argument says to hide the tooltip if the docstring
169 // The second argument says to hide the tooltip if the docstring
170 // is actually empty
170 // is actually empty
171 IPython.tooltip.pending(that, true);
171 IPython.tooltip.pending(that, true);
172 } else if (event.which === key.UPARROW && event.type === 'keydown') {
172 } else if (event.which === key.UPARROW && event.type === 'keydown') {
173 // If we are not at the top, let CM handle the up arrow and
173 // If we are not at the top, let CM handle the up arrow and
174 // prevent the global keydown handler from handling it.
174 // prevent the global keydown handler from handling it.
175 if (!that.at_top()) {
175 if (!that.at_top()) {
176 event.stop();
176 event.stop();
177 return false;
177 return false;
178 } else {
178 } else {
179 return true;
179 return true;
180 };
180 };
181 } else if (event.which === key.ESC) {
181 } else if (event.which === key.ESC) {
182 if (!IPython.tooltip._hidden) {
182 return IPython.tooltip.remove_and_cancel_tooltip(true);
183 IPython.tooltip.remove_and_cancel_tooltip(true);
184 return true;
185 } else {
186 return false;
187 }
188 } else if (event.which === key.DOWNARROW && event.type === 'keydown') {
183 } else if (event.which === key.DOWNARROW && event.type === 'keydown') {
189 // If we are not at the bottom, let CM handle the down arrow and
184 // If we are not at the bottom, let CM handle the down arrow and
190 // prevent the global keydown handler from handling it.
185 // prevent the global keydown handler from handling it.
191 if (!that.at_bottom()) {
186 if (!that.at_bottom()) {
192 event.stop();
187 event.stop();
193 return false;
188 return false;
194 } else {
189 } else {
195 return true;
190 return true;
196 };
191 };
197 } else if (event.keyCode === key.TAB && event.type == 'keydown' && event.shiftKey) {
192 } else if (event.keyCode === key.TAB && event.type == 'keydown' && event.shiftKey) {
198 if (editor.somethingSelected()){
193 if (editor.somethingSelected()){
199 var anchor = editor.getCursor("anchor");
194 var anchor = editor.getCursor("anchor");
200 var head = editor.getCursor("head");
195 var head = editor.getCursor("head");
201 if( anchor.line != head.line){
196 if( anchor.line != head.line){
202 return false;
197 return false;
203 }
198 }
204 }
199 }
205 IPython.tooltip.request(that);
200 IPython.tooltip.request(that);
206 event.stop();
201 event.stop();
207 return true;
202 return true;
208 } else if (event.keyCode === key.TAB && event.type == 'keydown') {
203 } else if (event.keyCode === key.TAB && event.type == 'keydown') {
209 // Tab completion.
204 // Tab completion.
210 //Do not trim here because of tooltip
205 //Do not trim here because of tooltip
211 if (editor.somethingSelected()){return false}
206 if (editor.somethingSelected()){return false}
212 var pre_cursor = editor.getRange({line:cur.line,ch:0},cur);
207 var pre_cursor = editor.getRange({line:cur.line,ch:0},cur);
213 if (pre_cursor.trim() === "") {
208 if (pre_cursor.trim() === "") {
214 // Don't autocomplete if the part of the line before the cursor
209 // Don't autocomplete if the part of the line before the cursor
215 // is empty. In this case, let CodeMirror handle indentation.
210 // is empty. In this case, let CodeMirror handle indentation.
216 return false;
211 return false;
217 } else if ((pre_cursor.substr(-1) === "("|| pre_cursor.substr(-1) === " ") && IPython.config.tooltip_on_tab ) {
212 } else if ((pre_cursor.substr(-1) === "("|| pre_cursor.substr(-1) === " ") && IPython.config.tooltip_on_tab ) {
218 IPython.tooltip.request(that);
213 IPython.tooltip.request(that);
219 // Prevent the event from bubbling up.
214 // Prevent the event from bubbling up.
220 event.stop();
215 event.stop();
221 // Prevent CodeMirror from handling the tab.
216 // Prevent CodeMirror from handling the tab.
222 return true;
217 return true;
223 } else {
218 } else {
224 event.stop();
219 event.stop();
225 this.completer.startCompletion();
220 this.completer.startCompletion();
226 return true;
221 return true;
227 };
222 };
228 } else {
223 } else {
229 // keypress/keyup also trigger on TAB press, and we don't want to
224 // keypress/keyup also trigger on TAB press, and we don't want to
230 // use those to disable tab completion.
225 // use those to disable tab completion.
231 return false;
226 return false;
232 };
227 };
233 return false;
228 return false;
234 };
229 };
235
230
236
231
237 // Kernel related calls.
232 // Kernel related calls.
238
233
239 CodeCell.prototype.set_kernel = function (kernel) {
234 CodeCell.prototype.set_kernel = function (kernel) {
240 this.kernel = kernel;
235 this.kernel = kernel;
241 }
236 }
242
237
243 /**
238 /**
244 * Execute current code cell to the kernel
239 * Execute current code cell to the kernel
245 * @method execute
240 * @method execute
246 */
241 */
247 CodeCell.prototype.execute = function () {
242 CodeCell.prototype.execute = function () {
248 this.output_area.clear_output(true, true, true);
243 this.output_area.clear_output(true, true, true);
249 this.set_input_prompt('*');
244 this.set_input_prompt('*');
250 this.element.addClass("running");
245 this.element.addClass("running");
251 var callbacks = {
246 var callbacks = {
252 'execute_reply': $.proxy(this._handle_execute_reply, this),
247 'execute_reply': $.proxy(this._handle_execute_reply, this),
253 'output': $.proxy(this.output_area.handle_output, this.output_area),
248 'output': $.proxy(this.output_area.handle_output, this.output_area),
254 'clear_output': $.proxy(this.output_area.handle_clear_output, this.output_area),
249 'clear_output': $.proxy(this.output_area.handle_clear_output, this.output_area),
255 'set_next_input': $.proxy(this._handle_set_next_input, this),
250 'set_next_input': $.proxy(this._handle_set_next_input, this),
256 'input_request': $.proxy(this._handle_input_request, this)
251 'input_request': $.proxy(this._handle_input_request, this)
257 };
252 };
258 var msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false, store_history: true});
253 var msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false, store_history: true});
259 };
254 };
260
255
261 /**
256 /**
262 * @method _handle_execute_reply
257 * @method _handle_execute_reply
263 * @private
258 * @private
264 */
259 */
265 CodeCell.prototype._handle_execute_reply = function (content) {
260 CodeCell.prototype._handle_execute_reply = function (content) {
266 this.set_input_prompt(content.execution_count);
261 this.set_input_prompt(content.execution_count);
267 this.element.removeClass("running");
262 this.element.removeClass("running");
268 $([IPython.events]).trigger('set_dirty.Notebook', {value: true});
263 $([IPython.events]).trigger('set_dirty.Notebook', {value: true});
269 }
264 }
270
265
271 /**
266 /**
272 * @method _handle_set_next_input
267 * @method _handle_set_next_input
273 * @private
268 * @private
274 */
269 */
275 CodeCell.prototype._handle_set_next_input = function (text) {
270 CodeCell.prototype._handle_set_next_input = function (text) {
276 var data = {'cell': this, 'text': text}
271 var data = {'cell': this, 'text': text}
277 $([IPython.events]).trigger('set_next_input.Notebook', data);
272 $([IPython.events]).trigger('set_next_input.Notebook', data);
278 }
273 }
279
274
280 /**
275 /**
281 * @method _handle_input_request
276 * @method _handle_input_request
282 * @private
277 * @private
283 */
278 */
284 CodeCell.prototype._handle_input_request = function (content) {
279 CodeCell.prototype._handle_input_request = function (content) {
285 this.output_area.append_raw_input(content);
280 this.output_area.append_raw_input(content);
286 }
281 }
287
282
288
283
289 // Basic cell manipulation.
284 // Basic cell manipulation.
290
285
291 CodeCell.prototype.select = function () {
286 CodeCell.prototype.select = function () {
292 IPython.Cell.prototype.select.apply(this);
287 IPython.Cell.prototype.select.apply(this);
293 this.code_mirror.refresh();
288 this.code_mirror.refresh();
294 this.code_mirror.focus();
289 this.code_mirror.focus();
295 this.auto_highlight();
290 this.auto_highlight();
296 // We used to need an additional refresh() after the focus, but
291 // We used to need an additional refresh() after the focus, but
297 // it appears that this has been fixed in CM. This bug would show
292 // it appears that this has been fixed in CM. This bug would show
298 // up on FF when a newly loaded markdown cell was edited.
293 // up on FF when a newly loaded markdown cell was edited.
299 };
294 };
300
295
301
296
302 CodeCell.prototype.select_all = function () {
297 CodeCell.prototype.select_all = function () {
303 var start = {line: 0, ch: 0};
298 var start = {line: 0, ch: 0};
304 var nlines = this.code_mirror.lineCount();
299 var nlines = this.code_mirror.lineCount();
305 var last_line = this.code_mirror.getLine(nlines-1);
300 var last_line = this.code_mirror.getLine(nlines-1);
306 var end = {line: nlines-1, ch: last_line.length};
301 var end = {line: nlines-1, ch: last_line.length};
307 this.code_mirror.setSelection(start, end);
302 this.code_mirror.setSelection(start, end);
308 };
303 };
309
304
310
305
311 CodeCell.prototype.collapse = function () {
306 CodeCell.prototype.collapse = function () {
312 this.collapsed = true;
307 this.collapsed = true;
313 this.output_area.collapse();
308 this.output_area.collapse();
314 };
309 };
315
310
316
311
317 CodeCell.prototype.expand = function () {
312 CodeCell.prototype.expand = function () {
318 this.collapsed = false;
313 this.collapsed = false;
319 this.output_area.expand();
314 this.output_area.expand();
320 };
315 };
321
316
322
317
323 CodeCell.prototype.toggle_output = function () {
318 CodeCell.prototype.toggle_output = function () {
324 this.collapsed = Boolean(1 - this.collapsed);
319 this.collapsed = Boolean(1 - this.collapsed);
325 this.output_area.toggle_output();
320 this.output_area.toggle_output();
326 };
321 };
327
322
328
323
329 CodeCell.prototype.toggle_output_scroll = function () {
324 CodeCell.prototype.toggle_output_scroll = function () {
330 this.output_area.toggle_scroll();
325 this.output_area.toggle_scroll();
331 };
326 };
332
327
333
328
334 CodeCell.input_prompt_classical = function (prompt_value, lines_number) {
329 CodeCell.input_prompt_classical = function (prompt_value, lines_number) {
335 var ns = prompt_value || "&nbsp;";
330 var ns = prompt_value || "&nbsp;";
336 return 'In&nbsp;[' + ns + ']:'
331 return 'In&nbsp;[' + ns + ']:'
337 };
332 };
338
333
339 CodeCell.input_prompt_continuation = function (prompt_value, lines_number) {
334 CodeCell.input_prompt_continuation = function (prompt_value, lines_number) {
340 var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)];
335 var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)];
341 for(var i=1; i < lines_number; i++){html.push(['...:'])};
336 for(var i=1; i < lines_number; i++){html.push(['...:'])};
342 return html.join('</br>')
337 return html.join('</br>')
343 };
338 };
344
339
345 CodeCell.input_prompt_function = CodeCell.input_prompt_classical;
340 CodeCell.input_prompt_function = CodeCell.input_prompt_classical;
346
341
347
342
348 CodeCell.prototype.set_input_prompt = function (number) {
343 CodeCell.prototype.set_input_prompt = function (number) {
349 var nline = 1
344 var nline = 1
350 if( this.code_mirror != undefined) {
345 if( this.code_mirror != undefined) {
351 nline = this.code_mirror.lineCount();
346 nline = this.code_mirror.lineCount();
352 }
347 }
353 this.input_prompt_number = number;
348 this.input_prompt_number = number;
354 var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline);
349 var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline);
355 this.element.find('div.input_prompt').html(prompt_html);
350 this.element.find('div.input_prompt').html(prompt_html);
356 };
351 };
357
352
358
353
359 CodeCell.prototype.clear_input = function () {
354 CodeCell.prototype.clear_input = function () {
360 this.code_mirror.setValue('');
355 this.code_mirror.setValue('');
361 };
356 };
362
357
363
358
364 CodeCell.prototype.get_text = function () {
359 CodeCell.prototype.get_text = function () {
365 return this.code_mirror.getValue();
360 return this.code_mirror.getValue();
366 };
361 };
367
362
368
363
369 CodeCell.prototype.set_text = function (code) {
364 CodeCell.prototype.set_text = function (code) {
370 return this.code_mirror.setValue(code);
365 return this.code_mirror.setValue(code);
371 };
366 };
372
367
373
368
374 CodeCell.prototype.at_top = function () {
369 CodeCell.prototype.at_top = function () {
375 var cursor = this.code_mirror.getCursor();
370 var cursor = this.code_mirror.getCursor();
376 if (cursor.line === 0 && cursor.ch === 0) {
371 if (cursor.line === 0 && cursor.ch === 0) {
377 return true;
372 return true;
378 } else {
373 } else {
379 return false;
374 return false;
380 }
375 }
381 };
376 };
382
377
383
378
384 CodeCell.prototype.at_bottom = function () {
379 CodeCell.prototype.at_bottom = function () {
385 var cursor = this.code_mirror.getCursor();
380 var cursor = this.code_mirror.getCursor();
386 if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) {
381 if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) {
387 return true;
382 return true;
388 } else {
383 } else {
389 return false;
384 return false;
390 }
385 }
391 };
386 };
392
387
393
388
394 CodeCell.prototype.clear_output = function (stdout, stderr, other) {
389 CodeCell.prototype.clear_output = function (stdout, stderr, other) {
395 this.output_area.clear_output(stdout, stderr, other);
390 this.output_area.clear_output(stdout, stderr, other);
396 };
391 };
397
392
398
393
399 // JSON serialization
394 // JSON serialization
400
395
401 CodeCell.prototype.fromJSON = function (data) {
396 CodeCell.prototype.fromJSON = function (data) {
402 IPython.Cell.prototype.fromJSON.apply(this, arguments);
397 IPython.Cell.prototype.fromJSON.apply(this, arguments);
403 if (data.cell_type === 'code') {
398 if (data.cell_type === 'code') {
404 if (data.input !== undefined) {
399 if (data.input !== undefined) {
405 this.set_text(data.input);
400 this.set_text(data.input);
406 // make this value the starting point, so that we can only undo
401 // make this value the starting point, so that we can only undo
407 // to this state, instead of a blank cell
402 // to this state, instead of a blank cell
408 this.code_mirror.clearHistory();
403 this.code_mirror.clearHistory();
409 this.auto_highlight();
404 this.auto_highlight();
410 }
405 }
411 if (data.prompt_number !== undefined) {
406 if (data.prompt_number !== undefined) {
412 this.set_input_prompt(data.prompt_number);
407 this.set_input_prompt(data.prompt_number);
413 } else {
408 } else {
414 this.set_input_prompt();
409 this.set_input_prompt();
415 };
410 };
416 this.output_area.fromJSON(data.outputs);
411 this.output_area.fromJSON(data.outputs);
417 if (data.collapsed !== undefined) {
412 if (data.collapsed !== undefined) {
418 if (data.collapsed) {
413 if (data.collapsed) {
419 this.collapse();
414 this.collapse();
420 } else {
415 } else {
421 this.expand();
416 this.expand();
422 };
417 };
423 };
418 };
424 };
419 };
425 };
420 };
426
421
427
422
428 CodeCell.prototype.toJSON = function () {
423 CodeCell.prototype.toJSON = function () {
429 var data = IPython.Cell.prototype.toJSON.apply(this);
424 var data = IPython.Cell.prototype.toJSON.apply(this);
430 data.input = this.get_text();
425 data.input = this.get_text();
431 data.cell_type = 'code';
426 data.cell_type = 'code';
432 if (this.input_prompt_number) {
427 if (this.input_prompt_number) {
433 data.prompt_number = this.input_prompt_number;
428 data.prompt_number = this.input_prompt_number;
434 };
429 };
435 var outputs = this.output_area.toJSON();
430 var outputs = this.output_area.toJSON();
436 data.outputs = outputs;
431 data.outputs = outputs;
437 data.language = 'python';
432 data.language = 'python';
438 data.collapsed = this.collapsed;
433 data.collapsed = this.collapsed;
439 return data;
434 return data;
440 };
435 };
441
436
442
437
443 IPython.CodeCell = CodeCell;
438 IPython.CodeCell = CodeCell;
444
439
445 return IPython;
440 return IPython;
446 }(IPython));
441 }(IPython));
@@ -1,367 +1,374
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 // Tooltip
8 // Tooltip
9 //============================================================================
9 //============================================================================
10 //
10 //
11 // you can set the autocall time by setting `IPython.tooltip.time_before_tooltip` in ms
11 // you can set the autocall time by setting `IPython.tooltip.time_before_tooltip` in ms
12 //
12 //
13 // you can configure the differents action of pressing tab several times in a row by
13 // you can configure the differents action of pressing tab several times in a row by
14 // setting/appending different fonction in the array
14 // setting/appending different fonction in the array
15 // IPython.tooltip.tabs_functions
15 // IPython.tooltip.tabs_functions
16 //
16 //
17 // eg :
17 // eg :
18 // IPython.tooltip.tabs_functions[4] = function (){console.log('this is the action of the 4th tab pressing')}
18 // IPython.tooltip.tabs_functions[4] = function (){console.log('this is the action of the 4th tab pressing')}
19 //
19 //
20 var IPython = (function (IPython) {
20 var IPython = (function (IPython) {
21 "use strict";
21 "use strict";
22
22
23 var utils = IPython.utils;
23 var utils = IPython.utils;
24
24
25 // tooltip constructor
25 // tooltip constructor
26 var Tooltip = function () {
26 var Tooltip = function () {
27 var that = this;
27 var that = this;
28 this.time_before_tooltip = 1200;
28 this.time_before_tooltip = 1200;
29
29
30 // handle to html
30 // handle to html
31 this.tooltip = $('#tooltip');
31 this.tooltip = $('#tooltip');
32 this._hidden = true;
32 this._hidden = true;
33
33
34 // variable for consecutive call
34 // variable for consecutive call
35 this._old_cell = null;
35 this._old_cell = null;
36 this._old_request = null;
36 this._old_request = null;
37 this._consecutive_counter = 0;
37 this._consecutive_counter = 0;
38
38
39 // 'sticky ?'
39 // 'sticky ?'
40 this._sticky = false;
40 this._sticky = false;
41
41
42 // display tooltip if the docstring is empty?
42 // display tooltip if the docstring is empty?
43 this._hide_if_no_docstring = false;
43 this._hide_if_no_docstring = false;
44
44
45 // contain the button in the upper right corner
45 // contain the button in the upper right corner
46 this.buttons = $('<div/>').addClass('tooltipbuttons');
46 this.buttons = $('<div/>').addClass('tooltipbuttons');
47
47
48 // will contain the docstring
48 // will contain the docstring
49 this.text = $('<div/>').addClass('tooltiptext').addClass('smalltooltip');
49 this.text = $('<div/>').addClass('tooltiptext').addClass('smalltooltip');
50
50
51 // build the buttons menu on the upper right
51 // build the buttons menu on the upper right
52 // expand the tooltip to see more
52 // expand the tooltip to see more
53 var expandlink = $('<a/>').attr('href', "#").addClass("ui-corner-all") //rounded corner
53 var expandlink = $('<a/>').attr('href', "#").addClass("ui-corner-all") //rounded corner
54 .attr('role', "button").attr('id', 'expanbutton').attr('title', 'Grow the tooltip vertically (press tab 2 times)').click(function () {
54 .attr('role', "button").attr('id', 'expanbutton').attr('title', 'Grow the tooltip vertically (press tab 2 times)').click(function () {
55 that.expand()
55 that.expand()
56 }).append(
56 }).append(
57 $('<span/>').text('Expand').addClass('ui-icon').addClass('ui-icon-plus'));
57 $('<span/>').text('Expand').addClass('ui-icon').addClass('ui-icon-plus'));
58
58
59 // open in pager
59 // open in pager
60 var morelink = $('<a/>').attr('href', "#").attr('role', "button").addClass('ui-button').attr('title', 'show the current docstring in pager (press tab 4 times)');
60 var morelink = $('<a/>').attr('href', "#").attr('role', "button").addClass('ui-button').attr('title', 'show the current docstring in pager (press tab 4 times)');
61 var morespan = $('<span/>').text('Open in Pager').addClass('ui-icon').addClass('ui-icon-arrowstop-l-n');
61 var morespan = $('<span/>').text('Open in Pager').addClass('ui-icon').addClass('ui-icon-arrowstop-l-n');
62 morelink.append(morespan);
62 morelink.append(morespan);
63 morelink.click(function () {
63 morelink.click(function () {
64 that.showInPager(that._old_cell);
64 that.showInPager(that._old_cell);
65 });
65 });
66
66
67 // close the tooltip
67 // close the tooltip
68 var closelink = $('<a/>').attr('href', "#").attr('role', "button").addClass('ui-button');
68 var closelink = $('<a/>').attr('href', "#").attr('role', "button").addClass('ui-button');
69 var closespan = $('<span/>').text('Close').addClass('ui-icon').addClass('ui-icon-close');
69 var closespan = $('<span/>').text('Close').addClass('ui-icon').addClass('ui-icon-close');
70 closelink.append(closespan);
70 closelink.append(closespan);
71 closelink.click(function () {
71 closelink.click(function () {
72 that.remove_and_cancel_tooltip(true);
72 that.remove_and_cancel_tooltip(true);
73 });
73 });
74
74
75 this._clocklink = $('<a/>').attr('href', "#");
75 this._clocklink = $('<a/>').attr('href', "#");
76 this._clocklink.attr('role', "button");
76 this._clocklink.attr('role', "button");
77 this._clocklink.addClass('ui-button');
77 this._clocklink.addClass('ui-button');
78 this._clocklink.attr('title', 'Tootip is not dismissed while typing for 10 seconds');
78 this._clocklink.attr('title', 'Tootip is not dismissed while typing for 10 seconds');
79 var clockspan = $('<span/>').text('Close');
79 var clockspan = $('<span/>').text('Close');
80 clockspan.addClass('ui-icon');
80 clockspan.addClass('ui-icon');
81 clockspan.addClass('ui-icon-clock');
81 clockspan.addClass('ui-icon-clock');
82 this._clocklink.append(clockspan);
82 this._clocklink.append(clockspan);
83 this._clocklink.click(function () {
83 this._clocklink.click(function () {
84 that.cancel_stick();
84 that.cancel_stick();
85 });
85 });
86
86
87
87
88
88
89
89
90 //construct the tooltip
90 //construct the tooltip
91 // add in the reverse order you want them to appear
91 // add in the reverse order you want them to appear
92 this.buttons.append(closelink);
92 this.buttons.append(closelink);
93 this.buttons.append(expandlink);
93 this.buttons.append(expandlink);
94 this.buttons.append(morelink);
94 this.buttons.append(morelink);
95 this.buttons.append(this._clocklink);
95 this.buttons.append(this._clocklink);
96 this._clocklink.hide();
96 this._clocklink.hide();
97
97
98
98
99 // we need a phony element to make the small arrow
99 // we need a phony element to make the small arrow
100 // of the tooltip in css
100 // of the tooltip in css
101 // we will move the arrow later
101 // we will move the arrow later
102 this.arrow = $('<div/>').addClass('pretooltiparrow');
102 this.arrow = $('<div/>').addClass('pretooltiparrow');
103 this.tooltip.append(this.buttons);
103 this.tooltip.append(this.buttons);
104 this.tooltip.append(this.arrow);
104 this.tooltip.append(this.arrow);
105 this.tooltip.append(this.text);
105 this.tooltip.append(this.text);
106
106
107 // function that will be called if you press tab 1, 2, 3... times in a row
107 // function that will be called if you press tab 1, 2, 3... times in a row
108 this.tabs_functions = [function (cell, text) {
108 this.tabs_functions = [function (cell, text) {
109 that._request_tooltip(cell, text);
109 that._request_tooltip(cell, text);
110 }, function () {
110 }, function () {
111 that.expand();
111 that.expand();
112 }, function () {
112 }, function () {
113 that.stick();
113 that.stick();
114 }, function (cell) {
114 }, function (cell) {
115 that.cancel_stick();
115 that.cancel_stick();
116 that.showInPager(cell);
116 that.showInPager(cell);
117 }];
117 }];
118 // call after all the tabs function above have bee call to clean their effects
118 // call after all the tabs function above have bee call to clean their effects
119 // if necessary
119 // if necessary
120 this.reset_tabs_function = function (cell, text) {
120 this.reset_tabs_function = function (cell, text) {
121 this._old_cell = (cell) ? cell : null;
121 this._old_cell = (cell) ? cell : null;
122 this._old_request = (text) ? text : null;
122 this._old_request = (text) ? text : null;
123 this._consecutive_counter = 0;
123 this._consecutive_counter = 0;
124 }
124 }
125 };
125 };
126
126
127 Tooltip.prototype.showInPager = function (cell) {
127 Tooltip.prototype.showInPager = function (cell) {
128 // reexecute last call in pager by appending ? to show back in pager
128 // reexecute last call in pager by appending ? to show back in pager
129 var that = this;
129 var that = this;
130 var empty = function () {};
130 var empty = function () {};
131 cell.kernel.execute(
131 cell.kernel.execute(
132 that.name + '?', {
132 that.name + '?', {
133 'execute_reply': empty,
133 'execute_reply': empty,
134 'output': empty,
134 'output': empty,
135 'clear_output': empty,
135 'clear_output': empty,
136 'cell': cell
136 'cell': cell
137 }, {
137 }, {
138 'silent': false,
138 'silent': false,
139 'store_history': true
139 'store_history': true
140 });
140 });
141 this.remove_and_cancel_tooltip();
141 this.remove_and_cancel_tooltip();
142 }
142 }
143
143
144 // grow the tooltip verticaly
144 // grow the tooltip verticaly
145 Tooltip.prototype.expand = function () {
145 Tooltip.prototype.expand = function () {
146 this.text.removeClass('smalltooltip');
146 this.text.removeClass('smalltooltip');
147 this.text.addClass('bigtooltip');
147 this.text.addClass('bigtooltip');
148 $('#expanbutton').hide('slow');
148 $('#expanbutton').hide('slow');
149 }
149 }
150
150
151 // deal with all the logic of hiding the tooltip
151 // deal with all the logic of hiding the tooltip
152 // and reset it's status
152 // and reset it's status
153 Tooltip.prototype._hide = function () {
153 Tooltip.prototype._hide = function () {
154 this.tooltip.fadeOut('fast');
154 this.tooltip.fadeOut('fast');
155 $('#expanbutton').show('slow');
155 $('#expanbutton').show('slow');
156 this.text.removeClass('bigtooltip');
156 this.text.removeClass('bigtooltip');
157 this.text.addClass('smalltooltip');
157 this.text.addClass('smalltooltip');
158 // keep scroll top to be sure to always see the first line
158 // keep scroll top to be sure to always see the first line
159 this.text.scrollTop(0);
159 this.text.scrollTop(0);
160 this._hidden = true;
160 this._hidden = true;
161 this.code_mirror = null;
161 this.code_mirror = null;
162 }
162 }
163
163
164 // return true on successfully removing a visible tooltip; otherwise return
165 // false.
164 Tooltip.prototype.remove_and_cancel_tooltip = function (force) {
166 Tooltip.prototype.remove_and_cancel_tooltip = function (force) {
165 // note that we don't handle closing directly inside the calltip
167 // note that we don't handle closing directly inside the calltip
166 // as in the completer, because it is not focusable, so won't
168 // as in the completer, because it is not focusable, so won't
167 // get the event.
169 // get the event.
168 if (this._sticky == false || force == true) {
170 if (this._hidden === false) {
169 this.cancel_stick();
171 if (this._sticky == false || force == true) {
170 this._hide();
172 this.cancel_stick();
173 this._hide();
174 }
175 this.cancel_pending();
176 this.reset_tabs_function();
177 return true;
178 } else {
179 return false;
171 }
180 }
172 this.cancel_pending();
173 this.reset_tabs_function();
174 }
181 }
175
182
176 // cancel autocall done after '(' for example.
183 // cancel autocall done after '(' for example.
177 Tooltip.prototype.cancel_pending = function () {
184 Tooltip.prototype.cancel_pending = function () {
178 if (this._tooltip_timeout != null) {
185 if (this._tooltip_timeout != null) {
179 clearTimeout(this._tooltip_timeout);
186 clearTimeout(this._tooltip_timeout);
180 this._tooltip_timeout = null;
187 this._tooltip_timeout = null;
181 }
188 }
182 }
189 }
183
190
184 // will trigger tooltip after timeout
191 // will trigger tooltip after timeout
185 Tooltip.prototype.pending = function (cell, hide_if_no_docstring) {
192 Tooltip.prototype.pending = function (cell, hide_if_no_docstring) {
186 var that = this;
193 var that = this;
187 this._tooltip_timeout = setTimeout(function () {
194 this._tooltip_timeout = setTimeout(function () {
188 that.request(cell, hide_if_no_docstring)
195 that.request(cell, hide_if_no_docstring)
189 }, that.time_before_tooltip);
196 }, that.time_before_tooltip);
190 }
197 }
191
198
192 Tooltip.prototype._request_tooltip = function (cell, func) {
199 Tooltip.prototype._request_tooltip = function (cell, func) {
193 // use internally just to make the request to the kernel
200 // use internally just to make the request to the kernel
194 // Feel free to shorten this logic if you are better
201 // Feel free to shorten this logic if you are better
195 // than me in regEx
202 // than me in regEx
196 // basicaly you shoul be able to get xxx.xxx.xxx from
203 // basicaly you shoul be able to get xxx.xxx.xxx from
197 // something(range(10), kwarg=smth) ; xxx.xxx.xxx( firstarg, rand(234,23), kwarg1=2,
204 // something(range(10), kwarg=smth) ; xxx.xxx.xxx( firstarg, rand(234,23), kwarg1=2,
198 // remove everything between matchin bracket (need to iterate)
205 // remove everything between matchin bracket (need to iterate)
199 var matchBracket = /\([^\(\)]+\)/g;
206 var matchBracket = /\([^\(\)]+\)/g;
200 var endBracket = /\([^\(]*$/g;
207 var endBracket = /\([^\(]*$/g;
201 var oldfunc = func;
208 var oldfunc = func;
202
209
203 func = func.replace(matchBracket, "");
210 func = func.replace(matchBracket, "");
204 while (oldfunc != func) {
211 while (oldfunc != func) {
205 oldfunc = func;
212 oldfunc = func;
206 func = func.replace(matchBracket, "");
213 func = func.replace(matchBracket, "");
207 }
214 }
208 // remove everything after last open bracket
215 // remove everything after last open bracket
209 func = func.replace(endBracket, "");
216 func = func.replace(endBracket, "");
210
217
211 var re = /[a-z_][0-9a-z._]+$/gi; // casse insensitive
218 var re = /[a-z_][0-9a-z._]+$/gi; // casse insensitive
212 var callbacks = {
219 var callbacks = {
213 'object_info_reply': $.proxy(this._show, this)
220 'object_info_reply': $.proxy(this._show, this)
214 }
221 }
215 var msg_id = cell.kernel.object_info_request(re.exec(func), callbacks);
222 var msg_id = cell.kernel.object_info_request(re.exec(func), callbacks);
216 }
223 }
217
224
218 // make an imediate completion request
225 // make an imediate completion request
219 Tooltip.prototype.request = function (cell, hide_if_no_docstring) {
226 Tooltip.prototype.request = function (cell, hide_if_no_docstring) {
220 // request(codecell)
227 // request(codecell)
221 // Deal with extracting the text from the cell and counting
228 // Deal with extracting the text from the cell and counting
222 // call in a row
229 // call in a row
223 this.cancel_pending();
230 this.cancel_pending();
224 var editor = cell.code_mirror;
231 var editor = cell.code_mirror;
225 var cursor = editor.getCursor();
232 var cursor = editor.getCursor();
226 var text = editor.getRange({
233 var text = editor.getRange({
227 line: cursor.line,
234 line: cursor.line,
228 ch: 0
235 ch: 0
229 }, cursor).trim();
236 }, cursor).trim();
230
237
231 this._hide_if_no_docstring = hide_if_no_docstring;
238 this._hide_if_no_docstring = hide_if_no_docstring;
232
239
233 if(editor.somethingSelected()){
240 if(editor.somethingSelected()){
234 text = editor.getSelection();
241 text = editor.getSelection();
235 }
242 }
236
243
237 // need a permanent handel to code_mirror for future auto recall
244 // need a permanent handel to code_mirror for future auto recall
238 this.code_mirror = editor;
245 this.code_mirror = editor;
239
246
240 // now we treat the different number of keypress
247 // now we treat the different number of keypress
241 // first if same cell, same text, increment counter by 1
248 // first if same cell, same text, increment counter by 1
242 if (this._old_cell == cell && this._old_request == text && this._hidden == false) {
249 if (this._old_cell == cell && this._old_request == text && this._hidden == false) {
243 this._consecutive_counter++;
250 this._consecutive_counter++;
244 } else {
251 } else {
245 // else reset
252 // else reset
246 this.cancel_stick();
253 this.cancel_stick();
247 this.reset_tabs_function (cell, text);
254 this.reset_tabs_function (cell, text);
248 }
255 }
249
256
250 // don't do anything if line beggin with '(' or is empty
257 // don't do anything if line beggin with '(' or is empty
251 if (text === "" || text === "(") {
258 if (text === "" || text === "(") {
252 return;
259 return;
253 }
260 }
254
261
255 this.tabs_functions[this._consecutive_counter](cell, text);
262 this.tabs_functions[this._consecutive_counter](cell, text);
256
263
257 // then if we are at the end of list function, reset
264 // then if we are at the end of list function, reset
258 if (this._consecutive_counter == this.tabs_functions.length) this.reset_tabs_function (cell, text);
265 if (this._consecutive_counter == this.tabs_functions.length) this.reset_tabs_function (cell, text);
259
266
260 return;
267 return;
261 }
268 }
262
269
263 // cancel the option of having the tooltip to stick
270 // cancel the option of having the tooltip to stick
264 Tooltip.prototype.cancel_stick = function () {
271 Tooltip.prototype.cancel_stick = function () {
265 clearTimeout(this._stick_timeout);
272 clearTimeout(this._stick_timeout);
266 this._stick_timeout = null;
273 this._stick_timeout = null;
267 this._clocklink.hide('slow');
274 this._clocklink.hide('slow');
268 this._sticky = false;
275 this._sticky = false;
269 }
276 }
270
277
271 // put the tooltip in a sicky state for 10 seconds
278 // put the tooltip in a sicky state for 10 seconds
272 // it won't be removed by remove_and_cancell() unless you called with
279 // it won't be removed by remove_and_cancell() unless you called with
273 // the first parameter set to true.
280 // the first parameter set to true.
274 // remove_and_cancell_tooltip(true)
281 // remove_and_cancell_tooltip(true)
275 Tooltip.prototype.stick = function (time) {
282 Tooltip.prototype.stick = function (time) {
276 time = (time != undefined) ? time : 10;
283 time = (time != undefined) ? time : 10;
277 var that = this;
284 var that = this;
278 this._sticky = true;
285 this._sticky = true;
279 this._clocklink.show('slow');
286 this._clocklink.show('slow');
280 this._stick_timeout = setTimeout(function () {
287 this._stick_timeout = setTimeout(function () {
281 that._sticky = false;
288 that._sticky = false;
282 that._clocklink.hide('slow');
289 that._clocklink.hide('slow');
283 }, time * 1000);
290 }, time * 1000);
284 }
291 }
285
292
286 // should be called with the kernel reply to actually show the tooltip
293 // should be called with the kernel reply to actually show the tooltip
287 Tooltip.prototype._show = function (reply) {
294 Tooltip.prototype._show = function (reply) {
288 // move the bubble if it is not hidden
295 // move the bubble if it is not hidden
289 // otherwise fade it
296 // otherwise fade it
290 this.name = reply.name;
297 this.name = reply.name;
291
298
292 // do some math to have the tooltip arrow on more or less on left or right
299 // do some math to have the tooltip arrow on more or less on left or right
293 // width of the editor
300 // width of the editor
294 var w = $(this.code_mirror.getScrollerElement()).width();
301 var w = $(this.code_mirror.getScrollerElement()).width();
295 // ofset of the editor
302 // ofset of the editor
296 var o = $(this.code_mirror.getScrollerElement()).offset();
303 var o = $(this.code_mirror.getScrollerElement()).offset();
297
304
298 // whatever anchor/head order but arrow at mid x selection
305 // whatever anchor/head order but arrow at mid x selection
299 var anchor = this.code_mirror.cursorCoords(false);
306 var anchor = this.code_mirror.cursorCoords(false);
300 var head = this.code_mirror.cursorCoords(true);
307 var head = this.code_mirror.cursorCoords(true);
301 var xinit = (head.left+anchor.left)/2;
308 var xinit = (head.left+anchor.left)/2;
302 var xinter = o.left + (xinit - o.left) / w * (w - 450);
309 var xinter = o.left + (xinit - o.left) / w * (w - 450);
303 var posarrowleft = xinit - xinter;
310 var posarrowleft = xinit - xinter;
304
311
305 if (this._hidden == false) {
312 if (this._hidden == false) {
306 this.tooltip.animate({
313 this.tooltip.animate({
307 'left': xinter - 30 + 'px',
314 'left': xinter - 30 + 'px',
308 'top': (head.bottom + 10) + 'px'
315 'top': (head.bottom + 10) + 'px'
309 });
316 });
310 } else {
317 } else {
311 this.tooltip.css({
318 this.tooltip.css({
312 'left': xinter - 30 + 'px'
319 'left': xinter - 30 + 'px'
313 });
320 });
314 this.tooltip.css({
321 this.tooltip.css({
315 'top': (head.bottom + 10) + 'px'
322 'top': (head.bottom + 10) + 'px'
316 });
323 });
317 }
324 }
318 this.arrow.animate({
325 this.arrow.animate({
319 'left': posarrowleft + 'px'
326 'left': posarrowleft + 'px'
320 });
327 });
321
328
322 // build docstring
329 // build docstring
323 var defstring = reply.call_def;
330 var defstring = reply.call_def;
324 if (defstring == null) {
331 if (defstring == null) {
325 defstring = reply.init_definition;
332 defstring = reply.init_definition;
326 }
333 }
327 if (defstring == null) {
334 if (defstring == null) {
328 defstring = reply.definition;
335 defstring = reply.definition;
329 }
336 }
330
337
331 var docstring = reply.call_docstring;
338 var docstring = reply.call_docstring;
332 if (docstring == null) {
339 if (docstring == null) {
333 docstring = reply.init_docstring;
340 docstring = reply.init_docstring;
334 }
341 }
335 if (docstring == null) {
342 if (docstring == null) {
336 docstring = reply.docstring;
343 docstring = reply.docstring;
337 }
344 }
338
345
339 if (docstring == null) {
346 if (docstring == null) {
340 // For reals this time, no docstring
347 // For reals this time, no docstring
341 if (this._hide_if_no_docstring) {
348 if (this._hide_if_no_docstring) {
342 return;
349 return;
343 } else {
350 } else {
344 docstring = "<empty docstring>";
351 docstring = "<empty docstring>";
345 }
352 }
346 }
353 }
347
354
348 this.tooltip.fadeIn('fast');
355 this.tooltip.fadeIn('fast');
349 this._hidden = false;
356 this._hidden = false;
350 this.text.children().remove();
357 this.text.children().remove();
351
358
352 var pre = $('<pre/>').html(utils.fixConsole(docstring));
359 var pre = $('<pre/>').html(utils.fixConsole(docstring));
353 if (defstring) {
360 if (defstring) {
354 var defstring_html = $('<pre/>').html(utils.fixConsole(defstring));
361 var defstring_html = $('<pre/>').html(utils.fixConsole(defstring));
355 this.text.append(defstring_html);
362 this.text.append(defstring_html);
356 }
363 }
357 this.text.append(pre);
364 this.text.append(pre);
358 // keep scroll top to be sure to always see the first line
365 // keep scroll top to be sure to always see the first line
359 this.text.scrollTop(0);
366 this.text.scrollTop(0);
360 }
367 }
361
368
362
369
363 IPython.Tooltip = Tooltip;
370 IPython.Tooltip = Tooltip;
364
371
365 return IPython;
372 return IPython;
366
373
367 }(IPython));
374 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now