codecell.js
525 lines
| 17.5 KiB
| application/javascript
|
JavascriptLexer
Jonathan Frederic
|
r17198 | // Copyright (c) IPython Development Team. | ||
// Distributed under the terms of the Modified BSD License. | ||||
define([ | ||||
'base/js/namespace', | ||||
Jonathan Frederic
|
r17200 | 'jquery', | ||
Jonathan Frederic
|
r17198 | 'base/js/utils', | ||
'base/js/keyboard', | ||||
'notebook/js/cell', | ||||
'notebook/js/outputarea', | ||||
'notebook/js/completer', | ||||
Jonathan Frederic
|
r17200 | 'notebook/js/celltoolbar', | ||
Jonathan Frederic
|
r17215 | ], function(IPython, $, utils, keyboard, cell, outputarea, completer, celltoolbar) { | ||
Matthias BUSSONNIER
|
r7132 | "use strict"; | ||
Jonathan Frederic
|
r17203 | var Cell = cell.Cell; | ||
Matthias BUSSONNIER
|
r7132 | |||
Jonathan Frederic
|
r17198 | /* local util for codemirror */ | ||
var posEq = function(a, b) {return a.line == b.line && a.ch == b.ch;}; | ||||
/** | ||||
* | ||||
* 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; | ||||
Brian E. Granger
|
r4352 | |||
jon
|
r17210 | var CodeCell = function (kernel, options) { | ||
jon
|
r17211 | // 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 | ||||
Jonathan Frederic
|
r17215 | // tooltip: Tooltip instance | ||
MinRK
|
r13133 | this.kernel = kernel || null; | ||
jon
|
r17210 | this.notebook = options.notebook; | ||
MinRK
|
r7524 | this.collapsed = false; | ||
jon
|
r17210 | this.events = options.events; | ||
Jonathan Frederic
|
r17215 | this.tooltip = options.tooltip; | ||
jon
|
r17210 | this.config = options.config; | ||
Matthias BUSSONNIER
|
r9537 | |||
Matthias BUSSONNIER
|
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; | ||||
this.last_msg_id = null; | ||||
this.completer = null; | ||||
Matthias BUSSONNIER
|
r9537 | |||
var cm_overwrite_options = { | ||||
Brian E. Granger
|
r14021 | onKeyEvent: $.proxy(this.handle_keyevent,this) | ||
Matthias BUSSONNIER
|
r9537 | }; | ||
MinRK
|
r17764 | var config = utils.mergeopt(CodeCell, this.config, {cm_config: cm_overwrite_options}); | ||
jon
|
r17210 | Cell.apply(this,[{ | ||
config: config, | ||||
keyboard_manager: options.keyboard_manager, | ||||
events: this.events}]); | ||||
Matthias BUSSONNIER
|
r8202 | |||
Brian E. Granger
|
r13661 | // Attributes we want to override in this subclass. | ||
this.cell_type = "code"; | ||||
Matthias BUSSONNIER
|
r8202 | var that = this; | ||
this.element.focusout( | ||||
Matthias BUSSONNIER
|
r8281 | function() { that.auto_highlight(); } | ||
); | ||||
Brian E. Granger
|
r4352 | }; | ||
Matthias BUSSONNIER
|
r10165 | CodeCell.options_default = { | ||
cm_config : { | ||||
MinRK
|
r11488 | extraKeys: { | ||
"Tab" : "indentMore", | ||||
"Shift-Tab" : "indentLess", | ||||
"Backspace" : "delSpaceToPrevTabStop", | ||||
"Cmd-/" : "toggleComment", | ||||
"Ctrl-/" : "toggleComment" | ||||
}, | ||||
Brian E. Granger
|
r10418 | mode: 'ipython', | ||
Matthias BUSSONNIER
|
r9537 | theme: 'ipython', | ||
damianavila
|
r14986 | matchBrackets: true, | ||
MinRK
|
r15842 | // don't auto-close strings because of CodeMirror #2385 | ||
autoCloseBrackets: "()[]{}" | ||||
Matthias BUSSONNIER
|
r10165 | } | ||
Matthias BUSSONNIER
|
r9537 | }; | ||
Jonathan Frederic
|
r14604 | CodeCell.msg_cells = {}; | ||
Matthias BUSSONNIER
|
r9537 | |||
Jonathan Frederic
|
r17203 | CodeCell.prototype = new Cell(); | ||
Brian E. Granger
|
r4352 | |||
Matthias BUSSONNIER
|
r8768 | /** | ||
* @method auto_highlight | ||||
*/ | ||||
Matthias BUSSONNIER
|
r8202 | CodeCell.prototype.auto_highlight = function () { | ||
Jonathan Frederic
|
r17198 | this._auto_highlight(this.config.cell_magic_highlight); | ||
Matthias BUSSONNIER
|
r8202 | }; | ||
Matthias BUSSONNIER
|
r8768 | /** @method create_element */ | ||
Brian E. Granger
|
r4352 | CodeCell.prototype.create_element = function () { | ||
Jonathan Frederic
|
r17203 | Cell.prototype.create_element.apply(this, arguments); | ||
Matthias BUSSONNIER
|
r9055 | |||
Matthias BUSSONNIER
|
r17422 | var cell = $('<div></div>').addClass('cell code_cell'); | ||
Brian E. Granger
|
r4629 | cell.attr('tabindex','2'); | ||
Brian E. Granger
|
r9142 | |||
Matthias BUSSONNIER
|
r10215 | var input = $('<div></div>').addClass('input'); | ||
Brian E. Granger
|
r13776 | var prompt = $('<div/>').addClass('prompt input_prompt'); | ||
var inner_cell = $('<div/>').addClass('inner_cell'); | ||||
Jonathan Frederic
|
r17214 | this.celltoolbar = new celltoolbar.CellToolbar({ | ||
cell: this, | ||||
notebook: this.notebook}); | ||||
Brian E. Granger
|
r13776 | inner_cell.append(this.celltoolbar.element); | ||
Matthias BUSSONNIER
|
r9422 | var input_area = $('<div/>').addClass('input_area'); | ||
Jonathan Frederic
|
r17217 | this.code_mirror = new CodeMirror(input_area.get(0), this.cm_config); | ||
MinRK
|
r10065 | $(this.code_mirror.getInputField()).attr("spellcheck", "false"); | ||
Brian E. Granger
|
r13776 | inner_cell.append(input_area); | ||
input.append(prompt).append(inner_cell); | ||||
Jonathan Frederic
|
r14219 | |||
Jonathan Frederic
|
r14234 | var widget_area = $('<div/>') | ||
Jonathan Frederic
|
r14293 | .addClass('widget-area') | ||
Jonathan Frederic
|
r14234 | .hide(); | ||
this.widget_area = widget_area; | ||||
var widget_prompt = $('<div/>') | ||||
.addClass('prompt') | ||||
.appendTo(widget_area); | ||||
var widget_subarea = $('<div/>') | ||||
Jonathan Frederic
|
r14293 | .addClass('widget-subarea') | ||
Jonathan Frederic
|
r14234 | .appendTo(widget_area); | ||
this.widget_subarea = widget_subarea; | ||||
var widget_clear_buton = $('<button />') | ||||
.addClass('close') | ||||
.html('×') | ||||
.click(function() { | ||||
widget_area.slideUp('', function(){ widget_subarea.html(''); }); | ||||
}) | ||||
.appendTo(widget_prompt); | ||||
Jonathan Frederic
|
r14219 | |||
Brian Granger
|
r7177 | var output = $('<div></div>'); | ||
Jonathan Frederic
|
r14219 | cell.append(input).append(widget_area).append(output); | ||
Brian E. Granger
|
r4352 | this.element = cell; | ||
Jonathan Frederic
|
r17214 | this.output_area = new outputarea.OutputArea({ | ||
selector: output, | ||||
prompt_area: true, | ||||
events: this.events, | ||||
keyboard_manager: this.keyboard_manager}); | ||||
Jonathan Frederic
|
r17202 | this.completer = new completer.Completer(this, this.events); | ||
Brian E. Granger
|
r4352 | }; | ||
Brian E. Granger
|
r14015 | /** @method bind_events */ | ||
CodeCell.prototype.bind_events = function () { | ||||
Jonathan Frederic
|
r17203 | Cell.prototype.bind_events.apply(this); | ||
Brian E. Granger
|
r14015 | var that = this; | ||
this.element.focusout( | ||||
function() { that.auto_highlight(); } | ||||
); | ||||
}; | ||||
Brian E. Granger
|
r14021 | |||
Matthias BUSSONNIER
|
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 | ||||
*/ | ||||
Brian E. Granger
|
r4378 | CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) { | ||
Brian E. Granger
|
r14021 | |||
Matthias BUSSONNIER
|
r5398 | var that = this; | ||
Brian E. Granger
|
r14021 | // whatever key is pressed, first, cancel the tooltip request before | ||
// they are sent, and remove tooltip if any, except for tab again | ||||
Brian E. Granger
|
r14062 | var tooltip_closed = null; | ||
Brian E. Granger
|
r15619 | if (event.type === 'keydown' && event.which != keycodes.tab ) { | ||
Jonathan Frederic
|
r17198 | tooltip_closed = this.tooltip.remove_and_cancel_tooltip(); | ||
Brian E. Granger
|
r14021 | } | ||
Brian Granger
|
r6059 | |||
Brian E. Granger
|
r14021 | var cur = editor.getCursor(); | ||
Brian E. Granger
|
r15619 | if (event.keyCode === keycodes.enter){ | ||
Brian E. Granger
|
r14021 | this.auto_highlight(); | ||
} | ||||
Brian Granger
|
r6059 | |||
Jonathan Frederic
|
r17198 | if (event.which === keycodes.down && event.type === 'keypress' && this.tooltip.time_before_tooltip >= 0) { | ||
Brian E. Granger
|
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 | ||||
Jonathan Frederic
|
r17198 | this.tooltip.pending(that, true); | ||
Paul Ivanov
|
r15757 | } else if ( tooltip_closed && event.which === keycodes.esc && event.type === 'keydown') { | ||
Paul Ivanov
|
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 | ||||
Paul Ivanov
|
r15757 | // if it is in sticky mode. Thus, we have to check again if it is open | ||
// and close it with force=true. | ||||
Jonathan Frederic
|
r17198 | if (!this.tooltip._hidden) { | ||
this.tooltip.remove_and_cancel_tooltip(true); | ||||
Brian E. Granger
|
r14021 | } | ||
Paul Ivanov
|
r15757 | // If we closed the tooltip, don't let CM or the global handlers | ||
// handle this event. | ||||
event.stop(); | ||||
return true; | ||||
Brian E. Granger
|
r15619 | } else if (event.keyCode === keycodes.tab && event.type === 'keydown' && event.shiftKey) { | ||
Brian E. Granger
|
r14021 | if (editor.somethingSelected()){ | ||
var anchor = editor.getCursor("anchor"); | ||||
var head = editor.getCursor("head"); | ||||
if( anchor.line != head.line){ | ||||
return false; | ||||
Bussonnier Matthias
|
r8949 | } | ||
} | ||||
Jonathan Frederic
|
r17198 | this.tooltip.request(that); | ||
Bussonnier Matthias
|
r8949 | event.stop(); | ||
return true; | ||||
Brian E. Granger
|
r15619 | } else if (event.keyCode === keycodes.tab && event.type == 'keydown') { | ||
Brian E. Granger
|
r4555 | // Tab completion. | ||
Jonathan Frederic
|
r17198 | this.tooltip.remove_and_cancel_tooltip(); | ||
Matthias BUSSONNIER
|
r13572 | if (editor.somethingSelected()) { | ||
return false; | ||||
} | ||||
Matthias BUSSONNIER
|
r5398 | 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; | ||
} else { | ||||
Brian E. Granger
|
r14021 | event.stop(); | ||
this.completer.startCompletion(); | ||||
return true; | ||||
} | ||||
Paul Ivanov
|
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) | ||||
Jonathan Frederic
|
r17203 | return Cell.prototype.handle_codemirror_keyevent.apply(this, [editor, event]); | ||
Brian E. Granger
|
r4378 | }; | ||
Brian Granger
|
r7168 | // Kernel related calls. | ||
MinRK
|
r13133 | CodeCell.prototype.set_kernel = function (kernel) { | ||
this.kernel = kernel; | ||||
MinRK
|
r13210 | }; | ||
Brian Granger
|
r7197 | |||
Matthias BUSSONNIER
|
r8768 | /** | ||
* Execute current code cell to the kernel | ||||
* @method execute | ||||
*/ | ||||
Brian Granger
|
r7168 | CodeCell.prototype.execute = function () { | ||
MinRK
|
r13102 | this.output_area.clear_output(); | ||
Jonathan Frederic
|
r14248 | |||
// Clear widget area | ||||
Jonathan Frederic
|
r14235 | this.widget_subarea.html(''); | ||
this.widget_subarea.height(''); | ||||
this.widget_area.height(''); | ||||
Jonathan Frederic
|
r14248 | this.widget_area.hide(); | ||
Brian Granger
|
r7168 | this.set_input_prompt('*'); | ||
this.element.addClass("running"); | ||||
MinRK
|
r13225 | if (this.last_msg_id) { | ||
this.kernel.clear_callbacks_for_msg(this.last_msg_id); | ||||
} | ||||
var callbacks = this.get_callbacks(); | ||||
Jonathan Frederic
|
r14604 | var old_msg_id = this.last_msg_id; | ||
MinRK
|
r13225 | this.last_msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false, store_history: true}); | ||
Jonathan Frederic
|
r14604 | if (old_msg_id) { | ||
delete CodeCell.msg_cells[old_msg_id]; | ||||
} | ||||
CodeCell.msg_cells[this.last_msg_id] = this; | ||||
MinRK
|
r13225 | }; | ||
/** | ||||
* Construct the default callbacks for | ||||
* @method get_callbacks | ||||
*/ | ||||
CodeCell.prototype.get_callbacks = function () { | ||||
return { | ||||
MinRK
|
r13207 | shell : { | ||
reply : $.proxy(this._handle_execute_reply, this), | ||||
payload : { | ||||
set_next_input : $.proxy(this._handle_set_next_input, this), | ||||
MinRK
|
r13225 | page : $.proxy(this._open_with_pager, this) | ||
MinRK
|
r13207 | } | ||
}, | ||||
iopub : { | ||||
output : $.proxy(this.output_area.handle_output, this.output_area), | ||||
clear_output : $.proxy(this.output_area.handle_clear_output, this.output_area), | ||||
}, | ||||
input : $.proxy(this._handle_input_request, this) | ||||
MinRK
|
r13210 | }; | ||
MinRK
|
r13225 | }; | ||
CodeCell.prototype._open_with_pager = function (payload) { | ||||
Jonathan Frederic
|
r17198 | this.events.trigger('open_with_text.Pager', payload); | ||
Brian Granger
|
r7168 | }; | ||
Matthias BUSSONNIER
|
r8768 | /** | ||
* @method _handle_execute_reply | ||||
* @private | ||||
*/ | ||||
MinRK
|
r13207 | CodeCell.prototype._handle_execute_reply = function (msg) { | ||
this.set_input_prompt(msg.content.execution_count); | ||||
Brian Granger
|
r7168 | this.element.removeClass("running"); | ||
Jonathan Frederic
|
r17198 | this.events.trigger('set_dirty.Notebook', {value: true}); | ||
MinRK
|
r13210 | }; | ||
Brian E. Granger
|
r4389 | |||
MinRK
|
r10368 | /** | ||
* @method _handle_set_next_input | ||||
* @private | ||||
*/ | ||||
MinRK
|
r13207 | CodeCell.prototype._handle_set_next_input = function (payload) { | ||
MinRK
|
r13210 | var data = {'cell': this, 'text': payload.text}; | ||
Jonathan Frederic
|
r17198 | this.events.trigger('set_next_input.Notebook', data); | ||
MinRK
|
r13210 | }; | ||
Takeshi Kanmae
|
r12458 | |||
MinRK
|
r10368 | /** | ||
* @method _handle_input_request | ||||
* @private | ||||
*/ | ||||
MinRK
|
r13207 | CodeCell.prototype._handle_input_request = function (msg) { | ||
this.output_area.append_raw_input(msg); | ||||
MinRK
|
r13210 | }; | ||
MinRK
|
r10368 | |||
Brian Granger
|
r7223 | |||
Brian Granger
|
r7168 | // Basic cell manipulation. | ||
Brian E. Granger
|
r4352 | CodeCell.prototype.select = function () { | ||
Jonathan Frederic
|
r17203 | var cont = Cell.prototype.select.apply(this); | ||
Brian E. Granger
|
r14015 | if (cont) { | ||
Brian E. Granger
|
r14014 | this.code_mirror.refresh(); | ||
this.auto_highlight(); | ||||
Brian E. Granger
|
r14092 | } | ||
Brian E. Granger
|
r14015 | return cont; | ||
Brian E. Granger
|
r4352 | }; | ||
Brian E. Granger
|
r14014 | CodeCell.prototype.render = function () { | ||
Jonathan Frederic
|
r17203 | var cont = Cell.prototype.render.apply(this); | ||
Brian E. Granger
|
r14015 | // Always execute, even if we are already in the rendered state | ||
return cont; | ||||
Brian E. Granger
|
r14013 | }; | ||
Brian E. Granger
|
r14015 | |||
Brian E. Granger
|
r14014 | CodeCell.prototype.unrender = function () { | ||
Brian E. Granger
|
r14015 | // CodeCell is always rendered | ||
return false; | ||||
Brian E. Granger
|
r14013 | }; | ||
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 E. Granger
|
r14867 | CodeCell.prototype.collapse_output = function () { | ||
MinRK
|
r7524 | this.collapsed = true; | ||
this.output_area.collapse(); | ||||
Brian Granger
|
r7168 | }; | ||
Brian E. Granger
|
r14867 | CodeCell.prototype.expand_output = function () { | ||
MinRK
|
r7524 | this.collapsed = false; | ||
this.output_area.expand(); | ||||
Brian E. Granger
|
r14867 | this.output_area.unscroll_area(); | ||
Brian Granger
|
r7168 | }; | ||
Brian E. Granger
|
r14867 | CodeCell.prototype.scroll_output = function () { | ||
this.output_area.expand(); | ||||
this.output_area.scroll_if_long(); | ||||
}; | ||||
Brian Granger
|
r7168 | |||
CodeCell.prototype.toggle_output = function () { | ||||
MinRK
|
r7524 | this.collapsed = Boolean(1 - this.collapsed); | ||
this.output_area.toggle_output(); | ||||
Brian Granger
|
r7168 | }; | ||
MinRK
|
r7429 | CodeCell.prototype.toggle_output_scroll = function () { | ||
Brian E. Granger
|
r14867 | this.output_area.toggle_scroll(); | ||
MinRK
|
r7429 | }; | ||
Bussonnier Matthias
|
r8467 | CodeCell.input_prompt_classical = function (prompt_value, lines_number) { | ||
Matthias BUSSONNIER
|
r14714 | var ns; | ||
Paul Ivanov
|
r15820 | if (prompt_value === undefined) { | ||
Matthias BUSSONNIER
|
r14714 | ns = " "; | ||
} else { | ||||
ns = encodeURIComponent(prompt_value); | ||||
} | ||||
MinRK
|
r13210 | return 'In [' + ns + ']:'; | ||
Bussonnier Matthias
|
r8467 | }; | ||
Matthias BUSSONNIER
|
r8768 | |||
Bussonnier Matthias
|
r8467 | CodeCell.input_prompt_continuation = function (prompt_value, lines_number) { | ||
var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)]; | ||||
MinRK
|
r13210 | for(var i=1; i < lines_number; i++) { | ||
html.push(['...:']); | ||||
} | ||||
return html.join('<br/>'); | ||||
Bussonnier Matthias
|
r8467 | }; | ||
Matthias BUSSONNIER
|
r8768 | |||
Bussonnier Matthias
|
r8467 | CodeCell.input_prompt_function = CodeCell.input_prompt_classical; | ||
Brian Granger
|
r7168 | CodeCell.prototype.set_input_prompt = function (number) { | ||
MinRK
|
r13210 | var nline = 1; | ||
if (this.code_mirror !== undefined) { | ||||
Bussonnier Matthias
|
r8467 | nline = this.code_mirror.lineCount(); | ||
} | ||||
Paul Ivanov
|
r8603 | this.input_prompt_number = number; | ||
Bussonnier Matthias
|
r8467 | var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline); | ||
Jonathan Frederic
|
r15376 | // This HTML call is okay because the user contents are escaped. | ||
Bussonnier Matthias
|
r8467 | this.element.find('div.input_prompt').html(prompt_html); | ||
Brian Granger
|
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); | ||||
}; | ||||
MinRK
|
r13102 | CodeCell.prototype.clear_output = function (wait) { | ||
this.output_area.clear_output(wait); | ||||
Brian E. Granger
|
r14867 | this.set_input_prompt(); | ||
MinRK
|
r6423 | }; | ||
Brian Granger
|
r7168 | |||
// JSON serialization | ||||
Brian E. Granger
|
r4349 | |||
Brian E. Granger
|
r4352 | CodeCell.prototype.fromJSON = function (data) { | ||
Jonathan Frederic
|
r17203 | Cell.prototype.fromJSON.apply(this, arguments); | ||
Brian E. Granger
|
r4352 | if (data.cell_type === 'code') { | ||
Brian E. Granger
|
r4484 | if (data.input !== undefined) { | ||
Brian Granger
|
r5943 | this.set_text(data.input); | ||
Paul Ivanov
|
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(); | ||||
Matthias BUSSONNIER
|
r8202 | this.auto_highlight(); | ||
Brian E. Granger
|
r4484 | } | ||
if (data.prompt_number !== undefined) { | ||||
this.set_input_prompt(data.prompt_number); | ||||
} else { | ||||
this.set_input_prompt(); | ||||
MinRK
|
r13210 | } | ||
MinRK
|
r14856 | this.output_area.trusted = data.trusted || false; | ||
Brian Granger
|
r7177 | this.output_area.fromJSON(data.outputs); | ||
Brian E. Granger
|
r4533 | if (data.collapsed !== undefined) { | ||
if (data.collapsed) { | ||||
Brian E. Granger
|
r14867 | this.collapse_output(); | ||
Brian Granger
|
r6032 | } else { | ||
Brian E. Granger
|
r14867 | this.expand_output(); | ||
MinRK
|
r13210 | } | ||
} | ||||
} | ||||
Brian E. Granger
|
r4352 | }; | ||
CodeCell.prototype.toJSON = function () { | ||||
Jonathan Frederic
|
r17203 | var data = Cell.prototype.toJSON.apply(this); | ||
Brian Granger
|
r5943 | data.input = this.get_text(); | ||
Matthias BUSSONNIER
|
r13572 | // is finite protect against undefined and '*' value | ||
if (isFinite(this.input_prompt_number)) { | ||||
Stefan van der Walt
|
r5479 | data.prompt_number = this.input_prompt_number; | ||
MinRK
|
r13210 | } | ||
Brian Granger
|
r7177 | var outputs = this.output_area.toJSON(); | ||
Brian E. Granger
|
r4497 | data.outputs = outputs; | ||
Brian E. Granger
|
r4484 | data.language = 'python'; | ||
MinRK
|
r14856 | data.trusted = this.output_area.trusted; | ||
Brian E. Granger
|
r4533 | data.collapsed = this.collapsed; | ||
Brian E. Granger
|
r4484 | return data; | ||
Brian E. Granger
|
r4352 | }; | ||
Jonathan Frederic
|
r15793 | /** | ||
* handle cell level logic when a cell is unselected | ||||
* @method unselect | ||||
* @return is the action being taken | ||||
*/ | ||||
CodeCell.prototype.unselect = function () { | ||||
Jonathan Frederic
|
r17203 | var cont = Cell.prototype.unselect.apply(this); | ||
Jonathan Frederic
|
r15793 | if (cont) { | ||
Jonathan Frederic
|
r15818 | // When a code cell is usnelected, make sure that the corresponding | ||
Jonathan Frederic
|
r15952 | // tooltip and completer to that cell is closed. | ||
Jonathan Frederic
|
r17198 | this.tooltip.remove_and_cancel_tooltip(true); | ||
Jonathan Frederic
|
r15952 | if (this.completer !== null) { | ||
this.completer.close(); | ||||
} | ||||
Jonathan Frederic
|
r15793 | } | ||
return cont; | ||||
}; | ||||
Brian E. Granger
|
r4497 | |||
Jonathan Frederic
|
r17198 | // Backwards compatability. | ||
Brian E. Granger
|
r4352 | IPython.CodeCell = CodeCell; | ||
Jonathan Frederic
|
r17201 | return {'CodeCell': CodeCell}; | ||
Jonathan Frederic
|
r17198 | }); | ||