codecell.js
306 lines
| 10.4 KiB
| application/javascript
|
JavascriptLexer
Brian E. Granger
|
r4609 | //---------------------------------------------------------------------------- | ||
// Copyright (C) 2008-2011 The IPython Development Team | ||||
// | ||||
// Distributed under the terms of the BSD License. The full license is in | ||||
// the file COPYING, distributed as part of this software. | ||||
//---------------------------------------------------------------------------- | ||||
Brian E. Granger
|
r4349 | |||
//============================================================================ | ||||
// CodeCell | ||||
//============================================================================ | ||||
Brian E. Granger
|
r4352 | var IPython = (function (IPython) { | ||
Matthias BUSSONNIER
|
r7132 | "use strict"; | ||
Brian E. Granger
|
r4352 | var utils = IPython.utils; | ||
Matthias BUSSONNIER
|
r7136 | var key = IPython.utils.keycodes; | ||
Brian E. Granger
|
r4352 | |||
Brian Granger
|
r7168 | var CodeCell = function (kernel) { | ||
Brian Granger
|
r7197 | // The kernel doesn't have to be set at creation time, in that case | ||
// it will be null and set_kernel has to be called later. | ||||
this.kernel = kernel || null; | ||||
Brian E. Granger
|
r4352 | this.code_mirror = null; | ||
MinRK
|
r5814 | this.input_prompt_number = null; | ||
Brian Granger
|
r7168 | this.tooltip_on_tab = true; | ||
Brian E. Granger
|
r4352 | IPython.Cell.apply(this, arguments); | ||
}; | ||||
CodeCell.prototype = new IPython.Cell(); | ||||
CodeCell.prototype.create_element = function () { | ||||
Brian E. Granger
|
r4361 | var cell = $('<div></div>').addClass('cell border-box-sizing code_cell vbox'); | ||
Brian E. Granger
|
r4629 | cell.attr('tabindex','2'); | ||
Brian E. Granger
|
r4360 | var input = $('<div></div>').addClass('input hbox'); | ||
Brian E. Granger
|
r4379 | input.append($('<div/>').addClass('prompt input_prompt')); | ||
Brian E. Granger
|
r4360 | var input_area = $('<div/>').addClass('input_area box-flex1'); | ||
Brian E. Granger
|
r4352 | this.code_mirror = CodeMirror(input_area.get(0), { | ||
indentUnit : 4, | ||||
Brian E. Granger
|
r4504 | mode: 'python', | ||
theme: 'ipython', | ||||
MinRK
|
r5200 | readOnly: this.read_only, | ||
Matthias BUSSONNIER
|
r7132 | onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this) | ||
Brian E. Granger
|
r4352 | }); | ||
input.append(input_area); | ||||
Brian Granger
|
r7177 | var output = $('<div></div>'); | ||
Brian E. Granger
|
r4352 | cell.append(input).append(output); | ||
this.element = cell; | ||||
Matthias BUSSONNIER
|
r7205 | this.output_area = new IPython.OutputArea(output, true); | ||
Matthias BUSSONNIER
|
r7132 | |||
Matthias BUSSONNIER
|
r7166 | // construct a completer only if class exist | ||
// otherwise no print view | ||||
Brian Granger
|
r7197 | if (IPython.Completer !== undefined) | ||
Matthias BUSSONNIER
|
r7166 | { | ||
Matthias BUSSONNIER
|
r7171 | this.completer = new IPython.Completer(this); | ||
Matthias BUSSONNIER
|
r7166 | } | ||
Brian E. Granger
|
r4352 | }; | ||
Brian E. Granger
|
r4378 | CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) { | ||
Fernando Perez
|
r5016 | // This method gets called in CodeMirror's onKeyDown/onKeyPress | ||
// handlers and is used to provide custom key handling. Its return | ||||
// value is used to determine if CodeMirror should ignore the event: | ||||
// true = ignore, false = don't ignore. | ||||
MinRK
|
r5656 | |||
if (this.read_only){ | ||||
return false; | ||||
} | ||||
fawce
|
r5990 | |||
Matthias BUSSONNIER
|
r5398 | var that = this; | ||
Matthias BUSSONNIER
|
r5397 | // whatever key is pressed, first, cancel the tooltip request before | ||
Matthias BUSSONNIER
|
r7160 | // they are sent, and remove tooltip if any, except for tab again | ||
Matthias BUSSONNIER
|
r7193 | if (event.type === 'keydown' && event.which != key.TAB ) { | ||
Matthias Bussonnier
|
r7151 | IPython.tooltip.remove_and_cancel_tooltip(); | ||
Brian Granger
|
r6059 | }; | ||
Brian Granger
|
r7168 | var cur = editor.getCursor(); | ||
Brian Granger
|
r6059 | |||
Matthias BUSSONNIER
|
r7193 | if (event.keyCode === key.ENTER && (event.shiftKey || event.ctrlKey)) { | ||
Brian E. Granger
|
r4378 | // Always ignore shift-enter in CodeMirror as we handle it. | ||
return true; | ||||
Brian Granger
|
r7168 | } else if (event.which === 40 && event.type === 'keypress' && IPython.tooltip.time_before_tooltip >= 0) { | ||
Matthias BUSSONNIER
|
r7160 | // triger on keypress (!) otherwise inconsistent event.which depending on plateform | ||
Matthias BUSSONNIER
|
r5403 | // browser and keyboard layout ! | ||
Matthias BUSSONNIER
|
r5399 | // Pressing '(' , request tooltip, don't forget to reappend it | ||
Matthias BUSSONNIER
|
r7160 | IPython.tooltip.pending(that); | ||
Brian Granger
|
r7199 | } else if (event.which === key.UPARROW && event.type === 'keydown') { | ||
Brian Granger
|
r5944 | // If we are not at the top, let CM handle the up arrow and | ||
// prevent the global keydown handler from handling it. | ||||
if (!that.at_top()) { | ||||
event.stop(); | ||||
return false; | ||||
} else { | ||||
return true; | ||||
}; | ||||
Matthias BUSSONNIER
|
r7206 | } else if (event.which === key.ESC) { | ||
IPython.tooltip.remove_and_cancel_tooltip(true); | ||||
return true; | ||||
Brian Granger
|
r7199 | } else if (event.which === key.DOWNARROW && event.type === 'keydown') { | ||
Brian Granger
|
r5944 | // If we are not at the bottom, let CM handle the down arrow and | ||
// prevent the global keydown handler from handling it. | ||||
if (!that.at_bottom()) { | ||||
event.stop(); | ||||
return false; | ||||
} else { | ||||
return true; | ||||
}; | ||||
Matthias BUSSONNIER
|
r7193 | } else if (event.keyCode === key.TAB && event.type == 'keydown') { | ||
Brian E. Granger
|
r4555 | // Tab completion. | ||
Matthias BUSSONNIER
|
r5398 | //Do not trim here because of tooltip | ||
var pre_cursor = editor.getRange({line:cur.line,ch:0},cur); | ||||
if (pre_cursor.trim() === "") { | ||||
Fernando Perez
|
r5016 | // Don't autocomplete if the part of the line before the cursor | ||
// is empty. In this case, let CodeMirror handle indentation. | ||||
Brian E. Granger
|
r4393 | return false; | ||
Brian Granger
|
r7168 | } else if ((pre_cursor.substr(-1) === "("|| pre_cursor.substr(-1) === " ") && that.tooltip_on_tab ) { | ||
Matthias BUSSONNIER
|
r7160 | IPython.tooltip.request(that); | ||
Brian Granger
|
r6052 | // Prevent the event from bubbling up. | ||
event.stop(); | ||||
// Prevent CodeMirror from handling the tab. | ||||
return true; | ||||
Brian E. Granger
|
r4393 | } else { | ||
event.stop(); | ||||
Matthias BUSSONNIER
|
r7132 | this.completer.startCompletion(); | ||
Brian E. Granger
|
r4393 | return true; | ||
Brian Granger
|
r6059 | }; | ||
Matthias BUSSONNIER
|
r7193 | } else if (event.keyCode === key.BACKSPACE && event.type == 'keydown') { | ||
Brian E. Granger
|
r4512 | // If backspace and the line ends with 4 spaces, remove them. | ||
var line = editor.getLine(cur.line); | ||||
var ending = line.slice(-4); | ||||
if (ending === ' ') { | ||||
editor.replaceRange('', | ||||
{line: cur.line, ch: cur.ch-4}, | ||||
{line: cur.line, ch: cur.ch} | ||||
); | ||||
event.stop(); | ||||
return true; | ||||
} else { | ||||
return false; | ||||
Brian Granger
|
r6059 | }; | ||
} else { | ||||
Fernando Perez
|
r5016 | // keypress/keyup also trigger on TAB press, and we don't want to | ||
// use those to disable tab completion. | ||||
Brian E. Granger
|
r4378 | return false; | ||
}; | ||||
Stefan van der Walt
|
r5479 | return false; | ||
Brian E. Granger
|
r4378 | }; | ||
Brian Granger
|
r7197 | |||
Brian Granger
|
r7168 | // Kernel related calls. | ||
Brian Granger
|
r7197 | CodeCell.prototype.set_kernel = function (kernel) { | ||
this.kernel = kernel; | ||||
} | ||||
Brian Granger
|
r7168 | CodeCell.prototype.execute = function () { | ||
Brian Granger
|
r7177 | this.output_area.clear_output(true, true, true); | ||
Brian Granger
|
r7168 | this.set_input_prompt('*'); | ||
this.element.addClass("running"); | ||||
var callbacks = { | ||||
'execute_reply': $.proxy(this._handle_execute_reply, this), | ||||
Brian Granger
|
r7177 | 'output': $.proxy(this.output_area.handle_output, this.output_area), | ||
'clear_output': $.proxy(this.output_area.handle_clear_output, this.output_area), | ||||
Brian Granger
|
r7223 | 'set_next_input': $.proxy(this._handle_set_next_input, this) | ||
Brian Granger
|
r7168 | }; | ||
Brian Granger
|
r7176 | var msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false}); | ||
Brian Granger
|
r7168 | }; | ||
Matthias BUSSONNIER
|
r5507 | |||
Brian Granger
|
r7168 | CodeCell.prototype._handle_execute_reply = function (content) { | ||
this.set_input_prompt(content.execution_count); | ||||
this.element.removeClass("running"); | ||||
Brian Granger
|
r7228 | $([IPython.events]).trigger('set_dirty.Notebook', {'value': true}); | ||
Matthias BUSSONNIER
|
r7141 | } | ||
Brian E. Granger
|
r4389 | |||
Brian Granger
|
r7223 | CodeCell.prototype._handle_set_next_input = function (text) { | ||
var data = {'cell': this, 'text': text} | ||||
$([IPython.events]).trigger('set_next_input.Notebook', data); | ||||
} | ||||
Brian Granger
|
r7168 | // Basic cell manipulation. | ||
Brian E. Granger
|
r4352 | CodeCell.prototype.select = function () { | ||
IPython.Cell.prototype.select.apply(this); | ||||
Brian Granger
|
r5942 | this.code_mirror.refresh(); | ||
Brian E. Granger
|
r4352 | this.code_mirror.focus(); | ||
Brian Granger
|
r5971 | // We used to need an additional refresh() after the focus, but | ||
// it appears that this has been fixed in CM. This bug would show | ||||
// up on FF when a newly loaded markdown cell was edited. | ||||
Brian E. Granger
|
r4352 | }; | ||
Brian E. Granger
|
r4675 | CodeCell.prototype.select_all = function () { | ||
var start = {line: 0, ch: 0}; | ||||
var nlines = this.code_mirror.lineCount(); | ||||
var last_line = this.code_mirror.getLine(nlines-1); | ||||
var end = {line: nlines-1, ch: last_line.length}; | ||||
this.code_mirror.setSelection(start, end); | ||||
}; | ||||
Brian Granger
|
r7168 | CodeCell.prototype.collapse = function () { | ||
Matthias BUSSONNIER
|
r7205 | this.output_area.collapse(); | ||
Brian Granger
|
r7168 | }; | ||
CodeCell.prototype.expand = function () { | ||||
Matthias BUSSONNIER
|
r7205 | this.output_area.expand(); | ||
Brian Granger
|
r7168 | }; | ||
CodeCell.prototype.toggle_output = function () { | ||||
Matthias BUSSONNIER
|
r7205 | this.output_area.toggle_output(); | ||
Brian Granger
|
r7168 | }; | ||
CodeCell.prototype.set_input_prompt = function (number) { | ||||
this.input_prompt_number = number; | ||||
var ns = number || " "; | ||||
this.element.find('div.input_prompt').html('In [' + ns + ']:'); | ||||
}; | ||||
CodeCell.prototype.clear_input = function () { | ||||
this.code_mirror.setValue(''); | ||||
}; | ||||
CodeCell.prototype.get_text = function () { | ||||
return this.code_mirror.getValue(); | ||||
}; | ||||
CodeCell.prototype.set_text = function (code) { | ||||
return this.code_mirror.setValue(code); | ||||
}; | ||||
CodeCell.prototype.at_top = function () { | ||||
var cursor = this.code_mirror.getCursor(); | ||||
if (cursor.line === 0) { | ||||
return true; | ||||
} else { | ||||
return false; | ||||
} | ||||
}; | ||||
CodeCell.prototype.at_bottom = function () { | ||||
var cursor = this.code_mirror.getCursor(); | ||||
if (cursor.line === (this.code_mirror.lineCount()-1)) { | ||||
return true; | ||||
} else { | ||||
return false; | ||||
} | ||||
}; | ||||
MinRK
|
r5085 | CodeCell.prototype.clear_output = function (stdout, stderr, other) { | ||
Brian Granger
|
r7177 | this.output_area.clear_output(stdout, stderr, other); | ||
MinRK
|
r6423 | }; | ||
Brian Granger
|
r7168 | |||
// JSON serialization | ||||
Brian E. Granger
|
r4349 | |||
Brian E. Granger
|
r4352 | CodeCell.prototype.fromJSON = function (data) { | ||
if (data.cell_type === 'code') { | ||||
Brian E. Granger
|
r4484 | if (data.input !== undefined) { | ||
Brian Granger
|
r5943 | this.set_text(data.input); | ||
Brian E. Granger
|
r4484 | } | ||
if (data.prompt_number !== undefined) { | ||||
this.set_input_prompt(data.prompt_number); | ||||
} else { | ||||
this.set_input_prompt(); | ||||
}; | ||||
Brian Granger
|
r7177 | this.output_area.fromJSON(data.outputs); | ||
Brian E. Granger
|
r4533 | if (data.collapsed !== undefined) { | ||
if (data.collapsed) { | ||||
this.collapse(); | ||||
Brian Granger
|
r6032 | } else { | ||
this.expand(); | ||||
Brian E. Granger
|
r4533 | }; | ||
}; | ||||
Brian E. Granger
|
r4352 | }; | ||
}; | ||||
CodeCell.prototype.toJSON = function () { | ||||
Brian E. Granger
|
r4497 | var data = {}; | ||
Brian Granger
|
r5943 | data.input = this.get_text(); | ||
Brian E. Granger
|
r4484 | data.cell_type = 'code'; | ||
MinRK
|
r5814 | if (this.input_prompt_number) { | ||
Stefan van der Walt
|
r5479 | data.prompt_number = this.input_prompt_number; | ||
Brian E. Granger
|
r4352 | }; | ||
Brian Granger
|
r7177 | var outputs = this.output_area.toJSON(); | ||
Brian E. Granger
|
r4497 | data.outputs = outputs; | ||
Brian E. Granger
|
r4484 | data.language = 'python'; | ||
Brian E. Granger
|
r4533 | data.collapsed = this.collapsed; | ||
Brian E. Granger
|
r4484 | return data; | ||
Brian E. Granger
|
r4352 | }; | ||
Brian E. Granger
|
r4497 | |||
Brian E. Granger
|
r4352 | IPython.CodeCell = CodeCell; | ||
return IPython; | ||||
}(IPython)); | ||||