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