##// END OF EJS Templates
set `spellcheck=false` in CodeCell inputarea...
MinRK -
Show More
@@ -1,427 +1,428 b''
1 //----------------------------------------------------------------------------
1 //----------------------------------------------------------------------------
2 // Copyright (C) 2008-2011 The IPython Development Team
2 // Copyright (C) 2008-2011 The IPython Development Team
3 //
3 //
4 // Distributed under the terms of the BSD License. The full license is in
4 // Distributed under the terms of the BSD License. The full license is in
5 // the file COPYING, distributed as part of this software.
5 // the file COPYING, distributed as part of this software.
6 //----------------------------------------------------------------------------
6 //----------------------------------------------------------------------------
7
7
8 //============================================================================
8 //============================================================================
9 // CodeCell
9 // CodeCell
10 //============================================================================
10 //============================================================================
11 /**
11 /**
12 * An extendable module that provide base functionnality to create cell for notebook.
12 * An extendable module that provide base functionnality to create cell for notebook.
13 * @module IPython
13 * @module IPython
14 * @namespace IPython
14 * @namespace IPython
15 * @submodule CodeCell
15 * @submodule CodeCell
16 */
16 */
17
17
18
18
19 /* local util for codemirror */
19 /* local util for codemirror */
20 var posEq = function(a, b) {return a.line == b.line && a.ch == b.ch;}
20 var posEq = function(a, b) {return a.line == b.line && a.ch == b.ch;}
21
21
22 /**
22 /**
23 *
23 *
24 * function to delete until previous non blanking space character
24 * function to delete until previous non blanking space character
25 * or first multiple of 4 tabstop.
25 * or first multiple of 4 tabstop.
26 * @private
26 * @private
27 */
27 */
28 CodeMirror.commands.delSpaceToPrevTabStop = function(cm){
28 CodeMirror.commands.delSpaceToPrevTabStop = function(cm){
29 var from = cm.getCursor(true), to = cm.getCursor(false), sel = !posEq(from, to);
29 var from = cm.getCursor(true), to = cm.getCursor(false), sel = !posEq(from, to);
30 if (!posEq(from, to)) {cm.replaceRange("", from, to); return}
30 if (!posEq(from, to)) {cm.replaceRange("", from, to); return}
31 var cur = cm.getCursor(), line = cm.getLine(cur.line);
31 var cur = cm.getCursor(), line = cm.getLine(cur.line);
32 var tabsize = cm.getOption('tabSize');
32 var tabsize = cm.getOption('tabSize');
33 var chToPrevTabStop = cur.ch-(Math.ceil(cur.ch/tabsize)-1)*tabsize;
33 var chToPrevTabStop = cur.ch-(Math.ceil(cur.ch/tabsize)-1)*tabsize;
34 var from = {ch:cur.ch-chToPrevTabStop,line:cur.line}
34 var from = {ch:cur.ch-chToPrevTabStop,line:cur.line}
35 var select = cm.getRange(from,cur)
35 var select = cm.getRange(from,cur)
36 if( select.match(/^\ +$/) != null){
36 if( select.match(/^\ +$/) != null){
37 cm.replaceRange("",from,cur)
37 cm.replaceRange("",from,cur)
38 } else {
38 } else {
39 cm.deleteH(-1,"char")
39 cm.deleteH(-1,"char")
40 }
40 }
41 };
41 };
42
42
43
43
44 var IPython = (function (IPython) {
44 var IPython = (function (IPython) {
45 "use strict";
45 "use strict";
46
46
47 var utils = IPython.utils;
47 var utils = IPython.utils;
48 var key = IPython.utils.keycodes;
48 var key = IPython.utils.keycodes;
49 CodeMirror.modeURL = "/static/codemirror/mode/%N/%N.js";
49 CodeMirror.modeURL = "/static/codemirror/mode/%N/%N.js";
50
50
51 /**
51 /**
52 * A Cell conceived to write code.
52 * A Cell conceived to write code.
53 *
53 *
54 * The kernel doesn't have to be set at creation time, in that case
54 * The kernel doesn't have to be set at creation time, in that case
55 * it will be null and set_kernel has to be called later.
55 * it will be null and set_kernel has to be called later.
56 * @class CodeCell
56 * @class CodeCell
57 * @extends IPython.Cell
57 * @extends IPython.Cell
58 *
58 *
59 * @constructor
59 * @constructor
60 * @param {Object|null} kernel
60 * @param {Object|null} kernel
61 * @param {object|undefined} [options]
61 * @param {object|undefined} [options]
62 * @param [options.cm_config] {object} config to pass to CodeMirror
62 * @param [options.cm_config] {object} config to pass to CodeMirror
63 */
63 */
64 var CodeCell = function (kernel, options) {
64 var CodeCell = function (kernel, options) {
65 var options = options || {}
65 var options = options || {}
66 this.kernel = kernel || null;
66 this.kernel = kernel || null;
67 this.code_mirror = null;
67 this.code_mirror = null;
68 this.input_prompt_number = null;
68 this.input_prompt_number = null;
69 this.collapsed = false;
69 this.collapsed = false;
70 this.default_mode = 'python';
70 this.default_mode = 'python';
71
71
72
72
73 var cm_overwrite_options = {
73 var cm_overwrite_options = {
74 extraKeys: {"Tab": "indentMore","Shift-Tab" : "indentLess",'Backspace':"delSpaceToPrevTabStop"},
74 extraKeys: {"Tab": "indentMore","Shift-Tab" : "indentLess",'Backspace':"delSpaceToPrevTabStop"},
75 onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this)
75 onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this)
76 };
76 };
77
77
78 var arg_cm_options = options.cm_options || {};
78 var arg_cm_options = options.cm_options || {};
79 var cm_config = $.extend({},CodeCell.cm_default, arg_cm_options, cm_overwrite_options);
79 var cm_config = $.extend({},CodeCell.cm_default, arg_cm_options, cm_overwrite_options);
80
80
81 var options = {};
81 var options = {};
82 options.cm_config = cm_config;
82 options.cm_config = cm_config;
83
83
84 IPython.Cell.apply(this,[options]);
84 IPython.Cell.apply(this,[options]);
85
85
86 var that = this;
86 var that = this;
87 this.element.focusout(
87 this.element.focusout(
88 function() { that.auto_highlight(); }
88 function() { that.auto_highlight(); }
89 );
89 );
90 };
90 };
91
91
92 CodeCell.cm_default = {
92 CodeCell.cm_default = {
93 mode: 'python',
93 mode: 'python',
94 theme: 'ipython',
94 theme: 'ipython',
95 matchBrackets: true
95 matchBrackets: true
96 };
96 };
97
97
98
98
99 CodeCell.prototype = new IPython.Cell();
99 CodeCell.prototype = new IPython.Cell();
100
100
101 /**
101 /**
102 * @method auto_highlight
102 * @method auto_highlight
103 */
103 */
104 CodeCell.prototype.auto_highlight = function () {
104 CodeCell.prototype.auto_highlight = function () {
105 this._auto_highlight(IPython.config.cell_magic_highlight)
105 this._auto_highlight(IPython.config.cell_magic_highlight)
106 };
106 };
107
107
108 /** @method create_element */
108 /** @method create_element */
109 CodeCell.prototype.create_element = function () {
109 CodeCell.prototype.create_element = function () {
110 IPython.Cell.prototype.create_element.apply(this, arguments);
110 IPython.Cell.prototype.create_element.apply(this, arguments);
111
111
112 var cell = $('<div></div>').addClass('cell border-box-sizing code_cell vbox');
112 var cell = $('<div></div>').addClass('cell border-box-sizing code_cell vbox');
113 cell.attr('tabindex','2');
113 cell.attr('tabindex','2');
114
114
115 this.celltoolbar = new IPython.CellToolbar(this);
115 this.celltoolbar = new IPython.CellToolbar(this);
116
116
117 var input = $('<div></div>').addClass('input hbox');
117 var input = $('<div></div>').addClass('input hbox');
118 var vbox = $('<div/>').addClass('vbox box-flex1')
118 var vbox = $('<div/>').addClass('vbox box-flex1')
119 input.append($('<div/>').addClass('prompt input_prompt'));
119 input.append($('<div/>').addClass('prompt input_prompt'));
120 vbox.append(this.celltoolbar.element);
120 vbox.append(this.celltoolbar.element);
121 var input_area = $('<div/>').addClass('input_area');
121 var input_area = $('<div/>').addClass('input_area');
122 this.code_mirror = CodeMirror(input_area.get(0), this.cm_config);
122 this.code_mirror = CodeMirror(input_area.get(0), this.cm_config);
123 $(this.code_mirror.getInputField()).attr("spellcheck", "false");
123 vbox.append(input_area);
124 vbox.append(input_area);
124 input.append(vbox);
125 input.append(vbox);
125 var output = $('<div></div>');
126 var output = $('<div></div>');
126 cell.append(input).append(output);
127 cell.append(input).append(output);
127 this.element = cell;
128 this.element = cell;
128 this.output_area = new IPython.OutputArea(output, true);
129 this.output_area = new IPython.OutputArea(output, true);
129
130
130 // construct a completer only if class exist
131 // construct a completer only if class exist
131 // otherwise no print view
132 // otherwise no print view
132 if (IPython.Completer !== undefined)
133 if (IPython.Completer !== undefined)
133 {
134 {
134 this.completer = new IPython.Completer(this);
135 this.completer = new IPython.Completer(this);
135 }
136 }
136 };
137 };
137
138
138 /**
139 /**
139 * This method gets called in CodeMirror's onKeyDown/onKeyPress
140 * This method gets called in CodeMirror's onKeyDown/onKeyPress
140 * handlers and is used to provide custom key handling. Its return
141 * handlers and is used to provide custom key handling. Its return
141 * value is used to determine if CodeMirror should ignore the event:
142 * value is used to determine if CodeMirror should ignore the event:
142 * true = ignore, false = don't ignore.
143 * true = ignore, false = don't ignore.
143 * @method handle_codemirror_keyevent
144 * @method handle_codemirror_keyevent
144 */
145 */
145 CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) {
146 CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) {
146
147
147 if (this.read_only){
148 if (this.read_only){
148 return false;
149 return false;
149 }
150 }
150
151
151 var that = this;
152 var that = this;
152 // whatever key is pressed, first, cancel the tooltip request before
153 // whatever key is pressed, first, cancel the tooltip request before
153 // they are sent, and remove tooltip if any, except for tab again
154 // they are sent, and remove tooltip if any, except for tab again
154 if (event.type === 'keydown' && event.which != key.TAB ) {
155 if (event.type === 'keydown' && event.which != key.TAB ) {
155 IPython.tooltip.remove_and_cancel_tooltip();
156 IPython.tooltip.remove_and_cancel_tooltip();
156 };
157 };
157
158
158 var cur = editor.getCursor();
159 var cur = editor.getCursor();
159 if (event.keyCode === key.ENTER){
160 if (event.keyCode === key.ENTER){
160 this.auto_highlight();
161 this.auto_highlight();
161 }
162 }
162
163
163 if (event.keyCode === key.ENTER && (event.shiftKey || event.ctrlKey)) {
164 if (event.keyCode === key.ENTER && (event.shiftKey || event.ctrlKey)) {
164 // Always ignore shift-enter in CodeMirror as we handle it.
165 // Always ignore shift-enter in CodeMirror as we handle it.
165 return true;
166 return true;
166 } else if (event.which === 40 && event.type === 'keypress' && IPython.tooltip.time_before_tooltip >= 0) {
167 } else if (event.which === 40 && event.type === 'keypress' && IPython.tooltip.time_before_tooltip >= 0) {
167 // triger on keypress (!) otherwise inconsistent event.which depending on plateform
168 // triger on keypress (!) otherwise inconsistent event.which depending on plateform
168 // browser and keyboard layout !
169 // browser and keyboard layout !
169 // Pressing '(' , request tooltip, don't forget to reappend it
170 // Pressing '(' , request tooltip, don't forget to reappend it
170 IPython.tooltip.pending(that);
171 IPython.tooltip.pending(that);
171 } else if (event.which === key.UPARROW && event.type === 'keydown') {
172 } else if (event.which === key.UPARROW && event.type === 'keydown') {
172 // If we are not at the top, let CM handle the up arrow and
173 // If we are not at the top, let CM handle the up arrow and
173 // prevent the global keydown handler from handling it.
174 // prevent the global keydown handler from handling it.
174 if (!that.at_top()) {
175 if (!that.at_top()) {
175 event.stop();
176 event.stop();
176 return false;
177 return false;
177 } else {
178 } else {
178 return true;
179 return true;
179 };
180 };
180 } else if (event.which === key.ESC) {
181 } else if (event.which === key.ESC) {
181 IPython.tooltip.remove_and_cancel_tooltip(true);
182 IPython.tooltip.remove_and_cancel_tooltip(true);
182 return true;
183 return true;
183 } else if (event.which === key.DOWNARROW && event.type === 'keydown') {
184 } else if (event.which === key.DOWNARROW && event.type === 'keydown') {
184 // If we are not at the bottom, let CM handle the down arrow and
185 // If we are not at the bottom, let CM handle the down arrow and
185 // prevent the global keydown handler from handling it.
186 // prevent the global keydown handler from handling it.
186 if (!that.at_bottom()) {
187 if (!that.at_bottom()) {
187 event.stop();
188 event.stop();
188 return false;
189 return false;
189 } else {
190 } else {
190 return true;
191 return true;
191 };
192 };
192 } else if (event.keyCode === key.TAB && event.type == 'keydown' && event.shiftKey) {
193 } else if (event.keyCode === key.TAB && event.type == 'keydown' && event.shiftKey) {
193 if (editor.somethingSelected()){
194 if (editor.somethingSelected()){
194 var anchor = editor.getCursor("anchor");
195 var anchor = editor.getCursor("anchor");
195 var head = editor.getCursor("head");
196 var head = editor.getCursor("head");
196 if( anchor.line != head.line){
197 if( anchor.line != head.line){
197 return false;
198 return false;
198 }
199 }
199 }
200 }
200 IPython.tooltip.request(that);
201 IPython.tooltip.request(that);
201 event.stop();
202 event.stop();
202 return true;
203 return true;
203 } else if (event.keyCode === key.TAB && event.type == 'keydown') {
204 } else if (event.keyCode === key.TAB && event.type == 'keydown') {
204 // Tab completion.
205 // Tab completion.
205 //Do not trim here because of tooltip
206 //Do not trim here because of tooltip
206 if (editor.somethingSelected()){return false}
207 if (editor.somethingSelected()){return false}
207 var pre_cursor = editor.getRange({line:cur.line,ch:0},cur);
208 var pre_cursor = editor.getRange({line:cur.line,ch:0},cur);
208 if (pre_cursor.trim() === "") {
209 if (pre_cursor.trim() === "") {
209 // Don't autocomplete if the part of the line before the cursor
210 // Don't autocomplete if the part of the line before the cursor
210 // is empty. In this case, let CodeMirror handle indentation.
211 // is empty. In this case, let CodeMirror handle indentation.
211 return false;
212 return false;
212 } else if ((pre_cursor.substr(-1) === "("|| pre_cursor.substr(-1) === " ") && IPython.config.tooltip_on_tab ) {
213 } else if ((pre_cursor.substr(-1) === "("|| pre_cursor.substr(-1) === " ") && IPython.config.tooltip_on_tab ) {
213 IPython.tooltip.request(that);
214 IPython.tooltip.request(that);
214 // Prevent the event from bubbling up.
215 // Prevent the event from bubbling up.
215 event.stop();
216 event.stop();
216 // Prevent CodeMirror from handling the tab.
217 // Prevent CodeMirror from handling the tab.
217 return true;
218 return true;
218 } else {
219 } else {
219 event.stop();
220 event.stop();
220 this.completer.startCompletion();
221 this.completer.startCompletion();
221 return true;
222 return true;
222 };
223 };
223 } else {
224 } else {
224 // keypress/keyup also trigger on TAB press, and we don't want to
225 // keypress/keyup also trigger on TAB press, and we don't want to
225 // use those to disable tab completion.
226 // use those to disable tab completion.
226 return false;
227 return false;
227 };
228 };
228 return false;
229 return false;
229 };
230 };
230
231
231
232
232 // Kernel related calls.
233 // Kernel related calls.
233
234
234 CodeCell.prototype.set_kernel = function (kernel) {
235 CodeCell.prototype.set_kernel = function (kernel) {
235 this.kernel = kernel;
236 this.kernel = kernel;
236 }
237 }
237
238
238 /**
239 /**
239 * Execute current code cell to the kernel
240 * Execute current code cell to the kernel
240 * @method execute
241 * @method execute
241 */
242 */
242 CodeCell.prototype.execute = function () {
243 CodeCell.prototype.execute = function () {
243 this.output_area.clear_output(true, true, true);
244 this.output_area.clear_output(true, true, true);
244 this.set_input_prompt('*');
245 this.set_input_prompt('*');
245 this.element.addClass("running");
246 this.element.addClass("running");
246 var callbacks = {
247 var callbacks = {
247 'execute_reply': $.proxy(this._handle_execute_reply, this),
248 'execute_reply': $.proxy(this._handle_execute_reply, this),
248 'output': $.proxy(this.output_area.handle_output, this.output_area),
249 'output': $.proxy(this.output_area.handle_output, this.output_area),
249 'clear_output': $.proxy(this.output_area.handle_clear_output, this.output_area),
250 'clear_output': $.proxy(this.output_area.handle_clear_output, this.output_area),
250 'set_next_input': $.proxy(this._handle_set_next_input, this)
251 'set_next_input': $.proxy(this._handle_set_next_input, this)
251 };
252 };
252 var msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false});
253 var msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false});
253 };
254 };
254
255
255 /**
256 /**
256 * @method _handle_execute_reply
257 * @method _handle_execute_reply
257 * @private
258 * @private
258 */
259 */
259 CodeCell.prototype._handle_execute_reply = function (content) {
260 CodeCell.prototype._handle_execute_reply = function (content) {
260 this.set_input_prompt(content.execution_count);
261 this.set_input_prompt(content.execution_count);
261 this.element.removeClass("running");
262 this.element.removeClass("running");
262 $([IPython.events]).trigger('set_dirty.Notebook', {'value': true});
263 $([IPython.events]).trigger('set_dirty.Notebook', {'value': true});
263 }
264 }
264
265
265 CodeCell.prototype._handle_set_next_input = function (text) {
266 CodeCell.prototype._handle_set_next_input = function (text) {
266 var data = {'cell': this, 'text': text}
267 var data = {'cell': this, 'text': text}
267 $([IPython.events]).trigger('set_next_input.Notebook', data);
268 $([IPython.events]).trigger('set_next_input.Notebook', data);
268 }
269 }
269
270
270 // Basic cell manipulation.
271 // Basic cell manipulation.
271
272
272 CodeCell.prototype.select = function () {
273 CodeCell.prototype.select = function () {
273 IPython.Cell.prototype.select.apply(this);
274 IPython.Cell.prototype.select.apply(this);
274 this.code_mirror.refresh();
275 this.code_mirror.refresh();
275 this.code_mirror.focus();
276 this.code_mirror.focus();
276 this.auto_highlight();
277 this.auto_highlight();
277 // We used to need an additional refresh() after the focus, but
278 // We used to need an additional refresh() after the focus, but
278 // it appears that this has been fixed in CM. This bug would show
279 // it appears that this has been fixed in CM. This bug would show
279 // up on FF when a newly loaded markdown cell was edited.
280 // up on FF when a newly loaded markdown cell was edited.
280 };
281 };
281
282
282
283
283 CodeCell.prototype.select_all = function () {
284 CodeCell.prototype.select_all = function () {
284 var start = {line: 0, ch: 0};
285 var start = {line: 0, ch: 0};
285 var nlines = this.code_mirror.lineCount();
286 var nlines = this.code_mirror.lineCount();
286 var last_line = this.code_mirror.getLine(nlines-1);
287 var last_line = this.code_mirror.getLine(nlines-1);
287 var end = {line: nlines-1, ch: last_line.length};
288 var end = {line: nlines-1, ch: last_line.length};
288 this.code_mirror.setSelection(start, end);
289 this.code_mirror.setSelection(start, end);
289 };
290 };
290
291
291
292
292 CodeCell.prototype.collapse = function () {
293 CodeCell.prototype.collapse = function () {
293 this.collapsed = true;
294 this.collapsed = true;
294 this.output_area.collapse();
295 this.output_area.collapse();
295 };
296 };
296
297
297
298
298 CodeCell.prototype.expand = function () {
299 CodeCell.prototype.expand = function () {
299 this.collapsed = false;
300 this.collapsed = false;
300 this.output_area.expand();
301 this.output_area.expand();
301 };
302 };
302
303
303
304
304 CodeCell.prototype.toggle_output = function () {
305 CodeCell.prototype.toggle_output = function () {
305 this.collapsed = Boolean(1 - this.collapsed);
306 this.collapsed = Boolean(1 - this.collapsed);
306 this.output_area.toggle_output();
307 this.output_area.toggle_output();
307 };
308 };
308
309
309
310
310 CodeCell.prototype.toggle_output_scroll = function () {
311 CodeCell.prototype.toggle_output_scroll = function () {
311 this.output_area.toggle_scroll();
312 this.output_area.toggle_scroll();
312 };
313 };
313
314
314
315
315 CodeCell.input_prompt_classical = function (prompt_value, lines_number) {
316 CodeCell.input_prompt_classical = function (prompt_value, lines_number) {
316 var ns = prompt_value || "&nbsp;";
317 var ns = prompt_value || "&nbsp;";
317 return 'In&nbsp;[' + ns + ']:'
318 return 'In&nbsp;[' + ns + ']:'
318 };
319 };
319
320
320 CodeCell.input_prompt_continuation = function (prompt_value, lines_number) {
321 CodeCell.input_prompt_continuation = function (prompt_value, lines_number) {
321 var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)];
322 var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)];
322 for(var i=1; i < lines_number; i++){html.push(['...:'])};
323 for(var i=1; i < lines_number; i++){html.push(['...:'])};
323 return html.join('</br>')
324 return html.join('</br>')
324 };
325 };
325
326
326 CodeCell.input_prompt_function = CodeCell.input_prompt_classical;
327 CodeCell.input_prompt_function = CodeCell.input_prompt_classical;
327
328
328
329
329 CodeCell.prototype.set_input_prompt = function (number) {
330 CodeCell.prototype.set_input_prompt = function (number) {
330 var nline = 1
331 var nline = 1
331 if( this.code_mirror != undefined) {
332 if( this.code_mirror != undefined) {
332 nline = this.code_mirror.lineCount();
333 nline = this.code_mirror.lineCount();
333 }
334 }
334 this.input_prompt_number = number;
335 this.input_prompt_number = number;
335 var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline);
336 var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline);
336 this.element.find('div.input_prompt').html(prompt_html);
337 this.element.find('div.input_prompt').html(prompt_html);
337 };
338 };
338
339
339
340
340 CodeCell.prototype.clear_input = function () {
341 CodeCell.prototype.clear_input = function () {
341 this.code_mirror.setValue('');
342 this.code_mirror.setValue('');
342 };
343 };
343
344
344
345
345 CodeCell.prototype.get_text = function () {
346 CodeCell.prototype.get_text = function () {
346 return this.code_mirror.getValue();
347 return this.code_mirror.getValue();
347 };
348 };
348
349
349
350
350 CodeCell.prototype.set_text = function (code) {
351 CodeCell.prototype.set_text = function (code) {
351 return this.code_mirror.setValue(code);
352 return this.code_mirror.setValue(code);
352 };
353 };
353
354
354
355
355 CodeCell.prototype.at_top = function () {
356 CodeCell.prototype.at_top = function () {
356 var cursor = this.code_mirror.getCursor();
357 var cursor = this.code_mirror.getCursor();
357 if (cursor.line === 0 && cursor.ch === 0) {
358 if (cursor.line === 0 && cursor.ch === 0) {
358 return true;
359 return true;
359 } else {
360 } else {
360 return false;
361 return false;
361 }
362 }
362 };
363 };
363
364
364
365
365 CodeCell.prototype.at_bottom = function () {
366 CodeCell.prototype.at_bottom = function () {
366 var cursor = this.code_mirror.getCursor();
367 var cursor = this.code_mirror.getCursor();
367 if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) {
368 if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) {
368 return true;
369 return true;
369 } else {
370 } else {
370 return false;
371 return false;
371 }
372 }
372 };
373 };
373
374
374
375
375 CodeCell.prototype.clear_output = function (stdout, stderr, other) {
376 CodeCell.prototype.clear_output = function (stdout, stderr, other) {
376 this.output_area.clear_output(stdout, stderr, other);
377 this.output_area.clear_output(stdout, stderr, other);
377 };
378 };
378
379
379
380
380 // JSON serialization
381 // JSON serialization
381
382
382 CodeCell.prototype.fromJSON = function (data) {
383 CodeCell.prototype.fromJSON = function (data) {
383 IPython.Cell.prototype.fromJSON.apply(this, arguments);
384 IPython.Cell.prototype.fromJSON.apply(this, arguments);
384 if (data.cell_type === 'code') {
385 if (data.cell_type === 'code') {
385 if (data.input !== undefined) {
386 if (data.input !== undefined) {
386 this.set_text(data.input);
387 this.set_text(data.input);
387 // make this value the starting point, so that we can only undo
388 // make this value the starting point, so that we can only undo
388 // to this state, instead of a blank cell
389 // to this state, instead of a blank cell
389 this.code_mirror.clearHistory();
390 this.code_mirror.clearHistory();
390 this.auto_highlight();
391 this.auto_highlight();
391 }
392 }
392 if (data.prompt_number !== undefined) {
393 if (data.prompt_number !== undefined) {
393 this.set_input_prompt(data.prompt_number);
394 this.set_input_prompt(data.prompt_number);
394 } else {
395 } else {
395 this.set_input_prompt();
396 this.set_input_prompt();
396 };
397 };
397 this.output_area.fromJSON(data.outputs);
398 this.output_area.fromJSON(data.outputs);
398 if (data.collapsed !== undefined) {
399 if (data.collapsed !== undefined) {
399 if (data.collapsed) {
400 if (data.collapsed) {
400 this.collapse();
401 this.collapse();
401 } else {
402 } else {
402 this.expand();
403 this.expand();
403 };
404 };
404 };
405 };
405 };
406 };
406 };
407 };
407
408
408
409
409 CodeCell.prototype.toJSON = function () {
410 CodeCell.prototype.toJSON = function () {
410 var data = IPython.Cell.prototype.toJSON.apply(this);
411 var data = IPython.Cell.prototype.toJSON.apply(this);
411 data.input = this.get_text();
412 data.input = this.get_text();
412 data.cell_type = 'code';
413 data.cell_type = 'code';
413 if (this.input_prompt_number) {
414 if (this.input_prompt_number) {
414 data.prompt_number = this.input_prompt_number;
415 data.prompt_number = this.input_prompt_number;
415 };
416 };
416 var outputs = this.output_area.toJSON();
417 var outputs = this.output_area.toJSON();
417 data.outputs = outputs;
418 data.outputs = outputs;
418 data.language = 'python';
419 data.language = 'python';
419 data.collapsed = this.collapsed;
420 data.collapsed = this.collapsed;
420 return data;
421 return data;
421 };
422 };
422
423
423
424
424 IPython.CodeCell = CodeCell;
425 IPython.CodeCell = CodeCell;
425
426
426 return IPython;
427 return IPython;
427 }(IPython));
428 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now