codecell.js
679 lines
| 22.7 KiB
| application/javascript
|
JavascriptLexer
|
r17198 | // Copyright (c) IPython Development Team. | ||
// Distributed under the terms of the Modified BSD License. | ||||
|
r18280 | /** | ||
* | ||||
* | ||||
* @module codecell | ||||
* @namespace codecell | ||||
* @class CodeCell | ||||
*/ | ||||
|
r17198 | |||
define([ | ||||
'base/js/namespace', | ||||
|
r17200 | 'jquery', | ||
|
r17198 | 'base/js/utils', | ||
'base/js/keyboard', | ||||
|
r19529 | 'services/config', | ||
|
r17198 | 'notebook/js/cell', | ||
'notebook/js/outputarea', | ||||
'notebook/js/completer', | ||||
|
r17200 | 'notebook/js/celltoolbar', | ||
|
r18280 | 'codemirror/lib/codemirror', | ||
'codemirror/mode/python/python', | ||||
'notebook/js/codemirror-ipython' | ||||
|
r19529 | ], function(IPython, | ||
$, | ||||
utils, | ||||
keyboard, | ||||
configmod, | ||||
cell, | ||||
outputarea, | ||||
completer, | ||||
celltoolbar, | ||||
CodeMirror, | ||||
cmpython, | ||||
cmip | ||||
) { | ||||
|
r7132 | "use strict"; | ||
|
r18953 | |||
|
r17203 | var Cell = cell.Cell; | ||
|
r7132 | |||
|
r17198 | /* local util for codemirror */ | ||
|
r19739 | var posEq = function(a, b) {return a.line === b.line && a.ch === b.ch;}; | ||
|
r17198 | |||
/** | ||||
* | ||||
* function to delete until previous non blanking space character | ||||
* or first multiple of 4 tabstop. | ||||
* @private | ||||
*/ | ||||
CodeMirror.commands.delSpaceToPrevTabStop = function(cm){ | ||||
var from = cm.getCursor(true), to = cm.getCursor(false), sel = !posEq(from, to); | ||||
if (!posEq(from, to)) { cm.replaceRange("", from, to); return; } | ||||
var cur = cm.getCursor(), line = cm.getLine(cur.line); | ||||
var tabsize = cm.getOption('tabSize'); | ||||
var chToPrevTabStop = cur.ch-(Math.ceil(cur.ch/tabsize)-1)*tabsize; | ||||
from = {ch:cur.ch-chToPrevTabStop,line:cur.line}; | ||||
var select = cm.getRange(from,cur); | ||||
if( select.match(/^\ +$/) !== null){ | ||||
cm.replaceRange("",from,cur); | ||||
} else { | ||||
cm.deleteH(-1,"char"); | ||||
} | ||||
}; | ||||
var keycodes = keyboard.keycodes; | ||||
|
r4352 | |||
|
r17210 | var CodeCell = function (kernel, options) { | ||
|
r19176 | /** | ||
* Constructor | ||||
* | ||||
* A Cell conceived to write code. | ||||
* | ||||
* Parameters: | ||||
* kernel: Kernel instance | ||||
* 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. | ||||
* options: dictionary | ||||
* Dictionary of keyword arguments. | ||||
* events: $(Events) instance | ||||
* config: dictionary | ||||
* keyboard_manager: KeyboardManager instance | ||||
* notebook: Notebook instance | ||||
* tooltip: Tooltip instance | ||||
*/ | ||||
|
r13133 | this.kernel = kernel || null; | ||
|
r17210 | this.notebook = options.notebook; | ||
|
r7524 | this.collapsed = false; | ||
|
r17210 | this.events = options.events; | ||
|
r17215 | this.tooltip = options.tooltip; | ||
|
r17210 | this.config = options.config; | ||
|
r19529 | this.class_config = new configmod.ConfigWithDefaults(this.config, | ||
CodeCell.config_defaults, 'CodeCell'); | ||||
|
r9537 | |||
|
r13574 | // create all attributed in constructor function | ||
// even if null for V8 VM optimisation | ||||
this.input_prompt_number = null; | ||||
this.celltoolbar = null; | ||||
this.output_area = null; | ||||
|
r18955 | // Keep a stack of the 'active' output areas (where active means the | ||
// output area that recieves output). When a user activates an output | ||||
// area, it gets pushed to the stack. Then, when the output area is | ||||
// deactivated, it's popped from the stack. When the stack is empty, | ||||
// the cell's output area is used. | ||||
|
r18957 | this.active_output_areas = []; | ||
|
r18958 | var that = this; | ||
Object.defineProperty(this, 'active_output_area', { | ||||
get: function() { | ||||
if (that.active_output_areas && that.active_output_areas.length > 0) { | ||||
return that.active_output_areas[that.active_output_areas.length-1]; | ||||
} else { | ||||
return that.output_area; | ||||
} | ||||
}, | ||||
}); | ||||
|
r13574 | this.last_msg_id = null; | ||
this.completer = null; | ||||
|
r19350 | this.widget_views = []; | ||
|
r19572 | this._widgets_live = true; | ||
|
r9537 | |||
|
r17210 | Cell.apply(this,[{ | ||
|
r19529 | config: $.extend({}, CodeCell.options_default), | ||
|
r17210 | keyboard_manager: options.keyboard_manager, | ||
events: this.events}]); | ||||
|
r8202 | |||
|
r13661 | // Attributes we want to override in this subclass. | ||
this.cell_type = "code"; | ||||
|
r8202 | this.element.focusout( | ||
|
r8281 | function() { that.auto_highlight(); } | ||
); | ||||
|
r4352 | }; | ||
|
r10165 | CodeCell.options_default = { | ||
cm_config : { | ||||
|
r11488 | extraKeys: { | ||
"Tab" : "indentMore", | ||||
"Shift-Tab" : "indentLess", | ||||
"Backspace" : "delSpaceToPrevTabStop", | ||||
"Cmd-/" : "toggleComment", | ||||
"Ctrl-/" : "toggleComment" | ||||
}, | ||||
|
r10418 | mode: 'ipython', | ||
|
r9537 | theme: 'ipython', | ||
|
r18280 | matchBrackets: true | ||
|
r10165 | } | ||
|
r9537 | }; | ||
|
r19529 | CodeCell.config_defaults = { | ||
cell_magic_highlight : { | ||||
'magic_javascript' :{'reg':[/^%%javascript/]}, | ||||
'magic_perl' :{'reg':[/^%%perl/]}, | ||||
'magic_ruby' :{'reg':[/^%%ruby/]}, | ||||
'magic_python' :{'reg':[/^%%python3?/]}, | ||||
'magic_shell' :{'reg':[/^%%bash/]}, | ||||
'magic_r' :{'reg':[/^%%R/]}, | ||||
'magic_text/x-cython' :{'reg':[/^%%cython/]}, | ||||
}, | ||||
}; | ||||
|
r14604 | CodeCell.msg_cells = {}; | ||
|
r9537 | |||
|
r18377 | CodeCell.prototype = Object.create(Cell.prototype); | ||
|
r4352 | |||
|
r8768 | /** | ||
|
r18953 | * @method push_output_area | ||
*/ | ||||
CodeCell.prototype.push_output_area = function (output_area) { | ||||
|
r18957 | this.active_output_areas.push(output_area); | ||
|
r18953 | }; | ||
/** | ||||
* @method pop_output_area | ||||
*/ | ||||
|
r18956 | CodeCell.prototype.pop_output_area = function (output_area) { | ||
|
r18957 | var index = this.active_output_areas.lastIndexOf(output_area); | ||
|
r18956 | if (index > -1) { | ||
|
r18957 | this.active_output_areas.splice(index, 1); | ||
|
r18956 | } | ||
|
r18953 | }; | ||
/** | ||||
|
r8768 | * @method auto_highlight | ||
*/ | ||||
|
r8202 | CodeCell.prototype.auto_highlight = function () { | ||
|
r19529 | this._auto_highlight(this.class_config.get_sync('cell_magic_highlight')); | ||
|
r8202 | }; | ||
|
r8768 | /** @method create_element */ | ||
|
r4352 | CodeCell.prototype.create_element = function () { | ||
|
r17203 | Cell.prototype.create_element.apply(this, arguments); | ||
|
r9055 | |||
|
r17422 | var cell = $('<div></div>').addClass('cell code_cell'); | ||
|
r4629 | cell.attr('tabindex','2'); | ||
|
r9142 | |||
|
r10215 | var input = $('<div></div>').addClass('input'); | ||
|
r13776 | var prompt = $('<div/>').addClass('prompt input_prompt'); | ||
var inner_cell = $('<div/>').addClass('inner_cell'); | ||||
|
r17214 | this.celltoolbar = new celltoolbar.CellToolbar({ | ||
cell: this, | ||||
notebook: this.notebook}); | ||||
|
r13776 | inner_cell.append(this.celltoolbar.element); | ||
|
r9422 | var input_area = $('<div/>').addClass('input_area'); | ||
|
r17217 | this.code_mirror = new CodeMirror(input_area.get(0), this.cm_config); | ||
|
r18282 | this.code_mirror.on('keydown', $.proxy(this.handle_keyevent,this)) | ||
|
r10065 | $(this.code_mirror.getInputField()).attr("spellcheck", "false"); | ||
|
r13776 | inner_cell.append(input_area); | ||
input.append(prompt).append(inner_cell); | ||||
|
r14219 | |||
|
r14234 | var widget_area = $('<div/>') | ||
|
r14293 | .addClass('widget-area') | ||
|
r14234 | .hide(); | ||
this.widget_area = widget_area; | ||||
var widget_prompt = $('<div/>') | ||||
.addClass('prompt') | ||||
.appendTo(widget_area); | ||||
var widget_subarea = $('<div/>') | ||||
|
r14293 | .addClass('widget-subarea') | ||
|
r14234 | .appendTo(widget_area); | ||
this.widget_subarea = widget_subarea; | ||||
|
r19350 | var that = this; | ||
|
r14234 | var widget_clear_buton = $('<button />') | ||
.addClass('close') | ||||
.html('×') | ||||
.click(function() { | ||||
|
r19350 | widget_area.slideUp('', function(){ | ||
for (var i = 0; i < that.widget_views.length; i++) { | ||||
|
r19572 | var view = that.widget_views[i]; | ||
view.remove(); | ||||
// Remove widget live events. | ||||
view.off('comm:live', that._widget_live); | ||||
view.off('comm:dead', that._widget_dead); | ||||
|
r19350 | } | ||
that.widget_views = []; | ||||
widget_subarea.html(''); | ||||
}); | ||||
}) | ||||
|
r14234 | .appendTo(widget_prompt); | ||
|
r14219 | |||
|
r7177 | var output = $('<div></div>'); | ||
|
r14219 | cell.append(input).append(widget_area).append(output); | ||
|
r4352 | this.element = cell; | ||
|
r17214 | this.output_area = new outputarea.OutputArea({ | ||
selector: output, | ||||
prompt_area: true, | ||||
events: this.events, | ||||
keyboard_manager: this.keyboard_manager}); | ||||
|
r17202 | this.completer = new completer.Completer(this, this.events); | ||
|
r4352 | }; | ||
|
r19350 | /** | ||
* Display a widget view in the cell. | ||||
*/ | ||||
CodeCell.prototype.display_widget_view = function(view_promise) { | ||||
|
r19351 | |||
|
r19350 | // Display a dummy element | ||
var dummy = $('<div/>'); | ||||
this.widget_subarea.append(dummy); | ||||
// Display the view. | ||||
var that = this; | ||||
return view_promise.then(function(view) { | ||||
|
r19351 | that.widget_area.show(); | ||
|
r19350 | dummy.replaceWith(view.$el); | ||
|
r19351 | that.widget_views.push(view); | ||
|
r19572 | |||
// Check the live state of the view's model. | ||||
if (view.model.comm_live) { | ||||
that._widget_live(view); | ||||
} else { | ||||
that._widget_dead(view); | ||||
} | ||||
// Listen to comm live events for the view. | ||||
view.on('comm:live', that._widget_live, that); | ||||
view.on('comm:dead', that._widget_dead, that); | ||||
|
r19350 | return view; | ||
}); | ||||
}; | ||||
|
r19572 | /** | ||
* Handles when a widget loses it's comm connection. | ||||
* @param {WidgetView} view | ||||
*/ | ||||
CodeCell.prototype._widget_dead = function(view) { | ||||
if (this._widgets_live) { | ||||
this._widgets_live = false; | ||||
this.widget_area.addClass('connection-problems'); | ||||
} | ||||
}; | ||||
/** | ||||
* Handles when a widget is connected to a live comm. | ||||
* @param {WidgetView} view | ||||
*/ | ||||
CodeCell.prototype._widget_live = function(view) { | ||||
if (!this._widgets_live) { | ||||
// Check that the other widgets are live too. O(N) operation. | ||||
// Abort the function at the first dead widget found. | ||||
for (var i = 0; i < this.widget_views.length; i++) { | ||||
if (!this.widget_views[i].model.comm_live) return; | ||||
} | ||||
this._widgets_live = true; | ||||
this.widget_area.removeClass('connection-problems'); | ||||
} | ||||
}; | ||||
|
r14015 | /** @method bind_events */ | ||
CodeCell.prototype.bind_events = function () { | ||||
|
r17203 | Cell.prototype.bind_events.apply(this); | ||
|
r14015 | var that = this; | ||
this.element.focusout( | ||||
function() { that.auto_highlight(); } | ||||
); | ||||
}; | ||||
|
r14021 | |||
|
r8768 | /** | ||
* 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. | ||||
* @method handle_codemirror_keyevent | ||||
*/ | ||||
|
r19098 | |||
|
r4378 | CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) { | ||
|
r14021 | |||
|
r5398 | var that = this; | ||
|
r14021 | // whatever key is pressed, first, cancel the tooltip request before | ||
// they are sent, and remove tooltip if any, except for tab again | ||||
|
r14062 | var tooltip_closed = null; | ||
|
r19739 | if (event.type === 'keydown' && event.which !== keycodes.tab ) { | ||
|
r17198 | tooltip_closed = this.tooltip.remove_and_cancel_tooltip(); | ||
|
r14021 | } | ||
|
r6059 | |||
|
r14021 | var cur = editor.getCursor(); | ||
|
r15619 | if (event.keyCode === keycodes.enter){ | ||
|
r14021 | this.auto_highlight(); | ||
} | ||||
|
r6059 | |||
|
r17198 | if (event.which === keycodes.down && event.type === 'keypress' && this.tooltip.time_before_tooltip >= 0) { | ||
|
r14021 | // triger on keypress (!) otherwise inconsistent event.which depending on plateform | ||
// browser and keyboard layout ! | ||||
// Pressing '(' , request tooltip, don't forget to reappend it | ||||
// The second argument says to hide the tooltip if the docstring | ||||
// is actually empty | ||||
|
r17198 | this.tooltip.pending(that, true); | ||
|
r15757 | } else if ( tooltip_closed && event.which === keycodes.esc && event.type === 'keydown') { | ||
|
r15820 | // If tooltip is active, cancel it. The call to | ||
// remove_and_cancel_tooltip above doesn't pass, force=true. | ||||
// Because of this it won't actually close the tooltip | ||||
|
r15757 | // if it is in sticky mode. Thus, we have to check again if it is open | ||
// and close it with force=true. | ||||
|
r17198 | if (!this.tooltip._hidden) { | ||
this.tooltip.remove_and_cancel_tooltip(true); | ||||
|
r14021 | } | ||
|
r15757 | // If we closed the tooltip, don't let CM or the global handlers | ||
// handle this event. | ||||
|
r18280 | event.codemirrorIgnore = true; | ||
event.preventDefault(); | ||||
|
r15757 | return true; | ||
|
r15619 | } else if (event.keyCode === keycodes.tab && event.type === 'keydown' && event.shiftKey) { | ||
|
r18284 | if (editor.somethingSelected() || editor.getSelections().length !== 1){ | ||
|
r14021 | var anchor = editor.getCursor("anchor"); | ||
var head = editor.getCursor("head"); | ||||
|
r19739 | if( anchor.line !== head.line){ | ||
|
r14021 | return false; | ||
|
r8949 | } | ||
} | ||||
|
r17198 | this.tooltip.request(that); | ||
|
r18280 | event.codemirrorIgnore = true; | ||
event.preventDefault(); | ||||
|
r8949 | return true; | ||
|
r19739 | } else if (event.keyCode === keycodes.tab && event.type === 'keydown') { | ||
|
r4555 | // Tab completion. | ||
|
r17198 | this.tooltip.remove_and_cancel_tooltip(); | ||
|
r18284 | |||
// completion does not work on multicursor, it might be possible though in some cases | ||||
if (editor.somethingSelected() || editor.getSelections().length > 1) { | ||||
|
r13572 | return false; | ||
} | ||||
|
r5398 | var pre_cursor = editor.getRange({line:cur.line,ch:0},cur); | ||
if (pre_cursor.trim() === "") { | ||||
|
r5016 | // Don't autocomplete if the part of the line before the cursor | ||
// is empty. In this case, let CodeMirror handle indentation. | ||||
|
r4393 | return false; | ||
} else { | ||||
|
r18280 | event.codemirrorIgnore = true; | ||
event.preventDefault(); | ||||
|
r14021 | this.completer.startCompletion(); | ||
return true; | ||||
} | ||||
|
r15757 | } | ||
// keyboard event wasn't one of those unique to code cells, let's see | ||||
// if it's one of the generic ones (i.e. check edit mode shortcuts) | ||||
|
r17203 | return Cell.prototype.handle_codemirror_keyevent.apply(this, [editor, event]); | ||
|
r4378 | }; | ||
|
r7168 | // Kernel related calls. | ||
|
r13133 | CodeCell.prototype.set_kernel = function (kernel) { | ||
this.kernel = kernel; | ||||
|
r13210 | }; | ||
|
r7197 | |||
|
r8768 | /** | ||
* Execute current code cell to the kernel | ||||
* @method execute | ||||
*/ | ||||
|
r7168 | CodeCell.prototype.execute = function () { | ||
|
r18931 | if (!this.kernel || !this.kernel.is_connected()) { | ||
console.log("Can't execute, kernel is not connected."); | ||||
return; | ||||
} | ||||
|
r18953 | |||
|
r19769 | this.active_output_area.clear_output(false, true); | ||
|
r14248 | |||
// Clear widget area | ||||
|
r19350 | for (var i = 0; i < this.widget_views.length; i++) { | ||
|
r19572 | var view = this.widget_views[i]; | ||
view.remove(); | ||||
// Remove widget live events. | ||||
view.off('comm:live', this._widget_live); | ||||
view.off('comm:dead', this._widget_dead); | ||||
|
r19350 | } | ||
this.widget_views = []; | ||||
|
r14235 | this.widget_subarea.html(''); | ||
this.widget_subarea.height(''); | ||||
this.widget_area.height(''); | ||||
|
r14248 | this.widget_area.hide(); | ||
|
r7168 | this.set_input_prompt('*'); | ||
this.element.addClass("running"); | ||||
|
r13225 | if (this.last_msg_id) { | ||
this.kernel.clear_callbacks_for_msg(this.last_msg_id); | ||||
} | ||||
var callbacks = this.get_callbacks(); | ||||
|
r14604 | var old_msg_id = this.last_msg_id; | ||
|
r13225 | this.last_msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false, store_history: true}); | ||
|
r14604 | if (old_msg_id) { | ||
delete CodeCell.msg_cells[old_msg_id]; | ||||
} | ||||
CodeCell.msg_cells[this.last_msg_id] = this; | ||||
|
r18270 | this.render(); | ||
|
r19041 | this.events.trigger('execute.CodeCell', {cell: this}); | ||
|
r13225 | }; | ||
/** | ||||
* Construct the default callbacks for | ||||
* @method get_callbacks | ||||
*/ | ||||
CodeCell.prototype.get_callbacks = function () { | ||||
|
r18953 | var that = this; | ||
|
r13225 | return { | ||
|
r13207 | shell : { | ||
reply : $.proxy(this._handle_execute_reply, this), | ||||
payload : { | ||||
set_next_input : $.proxy(this._handle_set_next_input, this), | ||||
|
r13225 | page : $.proxy(this._open_with_pager, this) | ||
|
r13207 | } | ||
}, | ||||
iopub : { | ||||
|
r18953 | output : function() { | ||
|
r18959 | that.active_output_area.handle_output.apply(that.active_output_area, arguments); | ||
|
r18953 | }, | ||
clear_output : function() { | ||||
|
r18959 | that.active_output_area.handle_clear_output.apply(that.active_output_area, arguments); | ||
|
r18953 | }, | ||
|
r13207 | }, | ||
input : $.proxy(this._handle_input_request, this) | ||||
|
r13210 | }; | ||
|
r13225 | }; | ||
CodeCell.prototype._open_with_pager = function (payload) { | ||||
|
r17198 | this.events.trigger('open_with_text.Pager', payload); | ||
|
r7168 | }; | ||
|
r8768 | /** | ||
* @method _handle_execute_reply | ||||
* @private | ||||
*/ | ||||
|
r13207 | CodeCell.prototype._handle_execute_reply = function (msg) { | ||
this.set_input_prompt(msg.content.execution_count); | ||||
|
r7168 | this.element.removeClass("running"); | ||
|
r17198 | this.events.trigger('set_dirty.Notebook', {value: true}); | ||
|
r13210 | }; | ||
|
r4389 | |||
|
r10368 | /** | ||
* @method _handle_set_next_input | ||||
* @private | ||||
*/ | ||||
|
r13207 | CodeCell.prototype._handle_set_next_input = function (payload) { | ||
|
r19250 | var data = {'cell': this, 'text': payload.text, replace: payload.replace}; | ||
|
r17198 | this.events.trigger('set_next_input.Notebook', data); | ||
|
r13210 | }; | ||
|
r12458 | |||
|
r10368 | /** | ||
* @method _handle_input_request | ||||
* @private | ||||
*/ | ||||
|
r13207 | CodeCell.prototype._handle_input_request = function (msg) { | ||
|
r18958 | this.active_output_area.append_raw_input(msg); | ||
|
r13210 | }; | ||
|
r10368 | |||
|
r7223 | |||
|
r7168 | // Basic cell manipulation. | ||
|
r4352 | CodeCell.prototype.select = function () { | ||
|
r17203 | var cont = Cell.prototype.select.apply(this); | ||
|
r14015 | if (cont) { | ||
|
r14014 | this.code_mirror.refresh(); | ||
this.auto_highlight(); | ||||
|
r14092 | } | ||
|
r14015 | return cont; | ||
|
r4352 | }; | ||
|
r14014 | CodeCell.prototype.render = function () { | ||
|
r17203 | var cont = Cell.prototype.render.apply(this); | ||
|
r14015 | // Always execute, even if we are already in the rendered state | ||
return cont; | ||||
|
r14013 | }; | ||
|
r14015 | |||
|
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); | ||||
}; | ||||
|
r14867 | CodeCell.prototype.collapse_output = function () { | ||
|
r7524 | this.output_area.collapse(); | ||
|
r7168 | }; | ||
|
r14867 | CodeCell.prototype.expand_output = function () { | ||
|
r7524 | this.output_area.expand(); | ||
|
r14867 | this.output_area.unscroll_area(); | ||
|
r7168 | }; | ||
|
r14867 | CodeCell.prototype.scroll_output = function () { | ||
this.output_area.expand(); | ||||
this.output_area.scroll_if_long(); | ||||
}; | ||||
|
r7168 | |||
CodeCell.prototype.toggle_output = function () { | ||||
|
r7524 | this.output_area.toggle_output(); | ||
|
r7168 | }; | ||
|
r7429 | CodeCell.prototype.toggle_output_scroll = function () { | ||
|
r14867 | this.output_area.toggle_scroll(); | ||
|
r7429 | }; | ||
|
r8467 | CodeCell.input_prompt_classical = function (prompt_value, lines_number) { | ||
|
r14714 | var ns; | ||
|
r18326 | if (prompt_value === undefined || prompt_value === null) { | ||
|
r14714 | ns = " "; | ||
} else { | ||||
ns = encodeURIComponent(prompt_value); | ||||
} | ||||
|
r13210 | return 'In [' + ns + ']:'; | ||
|
r8467 | }; | ||
|
r8768 | |||
|
r8467 | CodeCell.input_prompt_continuation = function (prompt_value, lines_number) { | ||
var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)]; | ||||
|
r13210 | for(var i=1; i < lines_number; i++) { | ||
html.push(['...:']); | ||||
} | ||||
return html.join('<br/>'); | ||||
|
r8467 | }; | ||
|
r8768 | |||
|
r8467 | CodeCell.input_prompt_function = CodeCell.input_prompt_classical; | ||
|
r7168 | CodeCell.prototype.set_input_prompt = function (number) { | ||
|
r13210 | var nline = 1; | ||
if (this.code_mirror !== undefined) { | ||||
|
r8467 | nline = this.code_mirror.lineCount(); | ||
} | ||||
|
r8603 | this.input_prompt_number = number; | ||
|
r8467 | var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline); | ||
|
r15376 | // This HTML call is okay because the user contents are escaped. | ||
|
r8467 | this.element.find('div.input_prompt').html(prompt_html); | ||
|
r7168 | }; | ||
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); | ||||
}; | ||||
|
r13102 | CodeCell.prototype.clear_output = function (wait) { | ||
|
r18958 | this.active_output_area.clear_output(wait); | ||
|
r14867 | this.set_input_prompt(); | ||
|
r6423 | }; | ||
|
r7168 | |||
// JSON serialization | ||||
|
r4349 | |||
|
r4352 | CodeCell.prototype.fromJSON = function (data) { | ||
|
r17203 | Cell.prototype.fromJSON.apply(this, arguments); | ||
|
r4352 | if (data.cell_type === 'code') { | ||
|
r18584 | if (data.source !== undefined) { | ||
this.set_text(data.source); | ||||
|
r7587 | // make this value the starting point, so that we can only undo | ||
// to this state, instead of a blank cell | ||||
this.code_mirror.clearHistory(); | ||||
|
r8202 | this.auto_highlight(); | ||
|
r4484 | } | ||
|
r18587 | this.set_input_prompt(data.execution_count); | ||
|
r18255 | this.output_area.trusted = data.metadata.trusted || false; | ||
|
r7177 | this.output_area.fromJSON(data.outputs); | ||
|
r18584 | if (data.metadata.collapsed !== undefined) { | ||
if (data.metadata.collapsed) { | ||||
|
r14867 | this.collapse_output(); | ||
|
r6032 | } else { | ||
|
r14867 | this.expand_output(); | ||
|
r13210 | } | ||
} | ||||
} | ||||
|
r4352 | }; | ||
CodeCell.prototype.toJSON = function () { | ||||
|
r17203 | var data = Cell.prototype.toJSON.apply(this); | ||
|
r18584 | data.source = this.get_text(); | ||
|
r13572 | // is finite protect against undefined and '*' value | ||
if (isFinite(this.input_prompt_number)) { | ||||
|
r18587 | data.execution_count = this.input_prompt_number; | ||
|
r18584 | } else { | ||
|
r18587 | data.execution_count = null; | ||
|
r13210 | } | ||
|
r7177 | var outputs = this.output_area.toJSON(); | ||
|
r4497 | data.outputs = outputs; | ||
|
r18255 | data.metadata.trusted = this.output_area.trusted; | ||
|
r18584 | data.metadata.collapsed = this.output_area.collapsed; | ||
|
r4484 | return data; | ||
|
r4352 | }; | ||
|
r15793 | /** | ||
* handle cell level logic when a cell is unselected | ||||
* @method unselect | ||||
* @return is the action being taken | ||||
*/ | ||||
CodeCell.prototype.unselect = function () { | ||||
|
r17203 | var cont = Cell.prototype.unselect.apply(this); | ||
|
r15793 | if (cont) { | ||
|
r15818 | // When a code cell is usnelected, make sure that the corresponding | ||
|
r15952 | // tooltip and completer to that cell is closed. | ||
|
r17198 | this.tooltip.remove_and_cancel_tooltip(true); | ||
|
r15952 | if (this.completer !== null) { | ||
this.completer.close(); | ||||
} | ||||
|
r15793 | } | ||
return cont; | ||||
}; | ||||
|
r4497 | |||
|
r17198 | // Backwards compatability. | ||
|
r4352 | IPython.CodeCell = CodeCell; | ||
|
r17201 | return {'CodeCell': CodeCell}; | ||
|
r17198 | }); | ||