##// END OF EJS Templates
Fixing cell_type in CodeCell constructor....
Brian E. Granger -
Show More
@@ -1,463 +1,465 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 from = {ch:cur.ch-chToPrevTabStop,line:cur.line};
34 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
49
50 /**
50 /**
51 * A Cell conceived to write code.
51 * A Cell conceived to write code.
52 *
52 *
53 * The kernel doesn't have to be set at creation time, in that case
53 * The kernel doesn't have to be set at creation time, in that case
54 * it will be null and set_kernel has to be called later.
54 * it will be null and set_kernel has to be called later.
55 * @class CodeCell
55 * @class CodeCell
56 * @extends IPython.Cell
56 * @extends IPython.Cell
57 *
57 *
58 * @constructor
58 * @constructor
59 * @param {Object|null} kernel
59 * @param {Object|null} kernel
60 * @param {object|undefined} [options]
60 * @param {object|undefined} [options]
61 * @param [options.cm_config] {object} config to pass to CodeMirror
61 * @param [options.cm_config] {object} config to pass to CodeMirror
62 */
62 */
63 var CodeCell = function (kernel, options) {
63 var CodeCell = function (kernel, options) {
64 this.kernel = kernel || null;
64 this.kernel = kernel || null;
65 this.collapsed = false;
65 this.collapsed = false;
66 this.cell_type = "code";
67
66
68 // create all attributed in constructor function
67 // create all attributed in constructor function
69 // even if null for V8 VM optimisation
68 // even if null for V8 VM optimisation
70 this.input_prompt_number = null;
69 this.input_prompt_number = null;
71 this.celltoolbar = null;
70 this.celltoolbar = null;
72 this.output_area = null;
71 this.output_area = null;
73 this.last_msg_id = null;
72 this.last_msg_id = null;
74 this.completer = null;
73 this.completer = null;
75
74
76
75
77 var cm_overwrite_options = {
76 var cm_overwrite_options = {
78 onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this)
77 onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this)
79 };
78 };
80
79
81 options = this.mergeopt(CodeCell, options, {cm_config:cm_overwrite_options});
80 options = this.mergeopt(CodeCell, options, {cm_config:cm_overwrite_options});
82
81
83 IPython.Cell.apply(this,[options]);
82 IPython.Cell.apply(this,[options]);
84
83
84 // Attributes we want to override in this subclass.
85 this.cell_type = "code";
86
85 var that = this;
87 var that = this;
86 this.element.focusout(
88 this.element.focusout(
87 function() { that.auto_highlight(); }
89 function() { that.auto_highlight(); }
88 );
90 );
89 };
91 };
90
92
91 CodeCell.options_default = {
93 CodeCell.options_default = {
92 cm_config : {
94 cm_config : {
93 extraKeys: {
95 extraKeys: {
94 "Tab" : "indentMore",
96 "Tab" : "indentMore",
95 "Shift-Tab" : "indentLess",
97 "Shift-Tab" : "indentLess",
96 "Backspace" : "delSpaceToPrevTabStop",
98 "Backspace" : "delSpaceToPrevTabStop",
97 "Cmd-/" : "toggleComment",
99 "Cmd-/" : "toggleComment",
98 "Ctrl-/" : "toggleComment"
100 "Ctrl-/" : "toggleComment"
99 },
101 },
100 mode: 'ipython',
102 mode: 'ipython',
101 theme: 'ipython',
103 theme: 'ipython',
102 matchBrackets: true
104 matchBrackets: true
103 }
105 }
104 };
106 };
105
107
106
108
107 CodeCell.prototype = new IPython.Cell();
109 CodeCell.prototype = new IPython.Cell();
108
110
109 /**
111 /**
110 * @method auto_highlight
112 * @method auto_highlight
111 */
113 */
112 CodeCell.prototype.auto_highlight = function () {
114 CodeCell.prototype.auto_highlight = function () {
113 this._auto_highlight(IPython.config.cell_magic_highlight);
115 this._auto_highlight(IPython.config.cell_magic_highlight);
114 };
116 };
115
117
116 /** @method create_element */
118 /** @method create_element */
117 CodeCell.prototype.create_element = function () {
119 CodeCell.prototype.create_element = function () {
118 IPython.Cell.prototype.create_element.apply(this, arguments);
120 IPython.Cell.prototype.create_element.apply(this, arguments);
119
121
120 var cell = $('<div></div>').addClass('cell border-box-sizing code_cell');
122 var cell = $('<div></div>').addClass('cell border-box-sizing code_cell');
121 cell.attr('tabindex','2');
123 cell.attr('tabindex','2');
122
124
123 this.celltoolbar = new IPython.CellToolbar(this);
125 this.celltoolbar = new IPython.CellToolbar(this);
124
126
125 var input = $('<div></div>').addClass('input');
127 var input = $('<div></div>').addClass('input');
126 var vbox = $('<div/>').addClass('vbox box-flex1');
128 var vbox = $('<div/>').addClass('vbox box-flex1');
127 input.append($('<div/>').addClass('prompt input_prompt'));
129 input.append($('<div/>').addClass('prompt input_prompt'));
128 vbox.append(this.celltoolbar.element);
130 vbox.append(this.celltoolbar.element);
129 var input_area = $('<div/>').addClass('input_area');
131 var input_area = $('<div/>').addClass('input_area');
130 this.code_mirror = CodeMirror(input_area.get(0), this.cm_config);
132 this.code_mirror = CodeMirror(input_area.get(0), this.cm_config);
131 $(this.code_mirror.getInputField()).attr("spellcheck", "false");
133 $(this.code_mirror.getInputField()).attr("spellcheck", "false");
132 vbox.append(input_area);
134 vbox.append(input_area);
133 input.append(vbox);
135 input.append(vbox);
134 var output = $('<div></div>');
136 var output = $('<div></div>');
135 cell.append(input).append(output);
137 cell.append(input).append(output);
136 this.element = cell;
138 this.element = cell;
137 this.output_area = new IPython.OutputArea(output, true);
139 this.output_area = new IPython.OutputArea(output, true);
138 this.completer = new IPython.Completer(this);
140 this.completer = new IPython.Completer(this);
139 };
141 };
140
142
141 /**
143 /**
142 * This method gets called in CodeMirror's onKeyDown/onKeyPress
144 * This method gets called in CodeMirror's onKeyDown/onKeyPress
143 * handlers and is used to provide custom key handling. Its return
145 * handlers and is used to provide custom key handling. Its return
144 * value is used to determine if CodeMirror should ignore the event:
146 * value is used to determine if CodeMirror should ignore the event:
145 * true = ignore, false = don't ignore.
147 * true = ignore, false = don't ignore.
146 * @method handle_codemirror_keyevent
148 * @method handle_codemirror_keyevent
147 */
149 */
148 CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) {
150 CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) {
149
151
150 var that = this;
152 var that = this;
151 // whatever key is pressed, first, cancel the tooltip request before
153 // whatever key is pressed, first, cancel the tooltip request before
152 // 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
153 if (event.type === 'keydown' && event.which != key.TAB ) {
155 if (event.type === 'keydown' && event.which != key.TAB ) {
154 IPython.tooltip.remove_and_cancel_tooltip();
156 IPython.tooltip.remove_and_cancel_tooltip();
155 }
157 }
156
158
157 var cur = editor.getCursor();
159 var cur = editor.getCursor();
158 if (event.keyCode === key.ENTER){
160 if (event.keyCode === key.ENTER){
159 this.auto_highlight();
161 this.auto_highlight();
160 }
162 }
161
163
162 if (event.keyCode === key.ENTER && (event.shiftKey || event.ctrlKey)) {
164 if (event.keyCode === key.ENTER && (event.shiftKey || event.ctrlKey)) {
163 // Always ignore shift-enter in CodeMirror as we handle it.
165 // Always ignore shift-enter in CodeMirror as we handle it.
164 return true;
166 return true;
165 } 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) {
166 // triger on keypress (!) otherwise inconsistent event.which depending on plateform
168 // triger on keypress (!) otherwise inconsistent event.which depending on plateform
167 // browser and keyboard layout !
169 // browser and keyboard layout !
168 // Pressing '(' , request tooltip, don't forget to reappend it
170 // Pressing '(' , request tooltip, don't forget to reappend it
169 // The second argument says to hide the tooltip if the docstring
171 // The second argument says to hide the tooltip if the docstring
170 // is actually empty
172 // is actually empty
171 IPython.tooltip.pending(that, true);
173 IPython.tooltip.pending(that, true);
172 } else if (event.which === key.UPARROW && event.type === 'keydown') {
174 } else if (event.which === key.UPARROW && event.type === 'keydown') {
173 // If we are not at the top, let CM handle the up arrow and
175 // If we are not at the top, let CM handle the up arrow and
174 // prevent the global keydown handler from handling it.
176 // prevent the global keydown handler from handling it.
175 if (!that.at_top()) {
177 if (!that.at_top()) {
176 event.stop();
178 event.stop();
177 return false;
179 return false;
178 } else {
180 } else {
179 return true;
181 return true;
180 }
182 }
181 } else if (event.which === key.ESC) {
183 } else if (event.which === key.ESC) {
182 return IPython.tooltip.remove_and_cancel_tooltip(true);
184 return IPython.tooltip.remove_and_cancel_tooltip(true);
183 } else if (event.which === key.DOWNARROW && event.type === 'keydown') {
185 } else if (event.which === key.DOWNARROW && event.type === 'keydown') {
184 // If we are not at the bottom, let CM handle the down arrow and
186 // If we are not at the bottom, let CM handle the down arrow and
185 // prevent the global keydown handler from handling it.
187 // prevent the global keydown handler from handling it.
186 if (!that.at_bottom()) {
188 if (!that.at_bottom()) {
187 event.stop();
189 event.stop();
188 return false;
190 return false;
189 } else {
191 } else {
190 return true;
192 return true;
191 }
193 }
192 } else if (event.keyCode === key.TAB && event.type == 'keydown' && event.shiftKey) {
194 } else if (event.keyCode === key.TAB && event.type == 'keydown' && event.shiftKey) {
193 if (editor.somethingSelected()){
195 if (editor.somethingSelected()){
194 var anchor = editor.getCursor("anchor");
196 var anchor = editor.getCursor("anchor");
195 var head = editor.getCursor("head");
197 var head = editor.getCursor("head");
196 if( anchor.line != head.line){
198 if( anchor.line != head.line){
197 return false;
199 return false;
198 }
200 }
199 }
201 }
200 IPython.tooltip.request(that);
202 IPython.tooltip.request(that);
201 event.stop();
203 event.stop();
202 return true;
204 return true;
203 } else if (event.keyCode === key.TAB && event.type == 'keydown') {
205 } else if (event.keyCode === key.TAB && event.type == 'keydown') {
204 // Tab completion.
206 // Tab completion.
205 IPython.tooltip.remove_and_cancel_tooltip();
207 IPython.tooltip.remove_and_cancel_tooltip();
206 if (editor.somethingSelected()) {
208 if (editor.somethingSelected()) {
207 return false;
209 return false;
208 }
210 }
209 var pre_cursor = editor.getRange({line:cur.line,ch:0},cur);
211 var pre_cursor = editor.getRange({line:cur.line,ch:0},cur);
210 if (pre_cursor.trim() === "") {
212 if (pre_cursor.trim() === "") {
211 // Don't autocomplete if the part of the line before the cursor
213 // Don't autocomplete if the part of the line before the cursor
212 // is empty. In this case, let CodeMirror handle indentation.
214 // is empty. In this case, let CodeMirror handle indentation.
213 return false;
215 return false;
214 } else {
216 } else {
215 event.stop();
217 event.stop();
216 this.completer.startCompletion();
218 this.completer.startCompletion();
217 return true;
219 return true;
218 }
220 }
219 } else {
221 } else {
220 // keypress/keyup also trigger on TAB press, and we don't want to
222 // keypress/keyup also trigger on TAB press, and we don't want to
221 // use those to disable tab completion.
223 // use those to disable tab completion.
222 return false;
224 return false;
223 }
225 }
224 return false;
226 return false;
225 };
227 };
226
228
227
229
228 // Kernel related calls.
230 // Kernel related calls.
229
231
230 CodeCell.prototype.set_kernel = function (kernel) {
232 CodeCell.prototype.set_kernel = function (kernel) {
231 this.kernel = kernel;
233 this.kernel = kernel;
232 };
234 };
233
235
234 /**
236 /**
235 * Execute current code cell to the kernel
237 * Execute current code cell to the kernel
236 * @method execute
238 * @method execute
237 */
239 */
238 CodeCell.prototype.execute = function () {
240 CodeCell.prototype.execute = function () {
239 this.output_area.clear_output();
241 this.output_area.clear_output();
240 this.set_input_prompt('*');
242 this.set_input_prompt('*');
241 this.element.addClass("running");
243 this.element.addClass("running");
242 if (this.last_msg_id) {
244 if (this.last_msg_id) {
243 this.kernel.clear_callbacks_for_msg(this.last_msg_id);
245 this.kernel.clear_callbacks_for_msg(this.last_msg_id);
244 }
246 }
245 var callbacks = this.get_callbacks();
247 var callbacks = this.get_callbacks();
246
248
247 this.last_msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false, store_history: true});
249 this.last_msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false, store_history: true});
248 };
250 };
249
251
250 /**
252 /**
251 * Construct the default callbacks for
253 * Construct the default callbacks for
252 * @method get_callbacks
254 * @method get_callbacks
253 */
255 */
254 CodeCell.prototype.get_callbacks = function () {
256 CodeCell.prototype.get_callbacks = function () {
255 return {
257 return {
256 shell : {
258 shell : {
257 reply : $.proxy(this._handle_execute_reply, this),
259 reply : $.proxy(this._handle_execute_reply, this),
258 payload : {
260 payload : {
259 set_next_input : $.proxy(this._handle_set_next_input, this),
261 set_next_input : $.proxy(this._handle_set_next_input, this),
260 page : $.proxy(this._open_with_pager, this)
262 page : $.proxy(this._open_with_pager, this)
261 }
263 }
262 },
264 },
263 iopub : {
265 iopub : {
264 output : $.proxy(this.output_area.handle_output, this.output_area),
266 output : $.proxy(this.output_area.handle_output, this.output_area),
265 clear_output : $.proxy(this.output_area.handle_clear_output, this.output_area),
267 clear_output : $.proxy(this.output_area.handle_clear_output, this.output_area),
266 },
268 },
267 input : $.proxy(this._handle_input_request, this)
269 input : $.proxy(this._handle_input_request, this)
268 };
270 };
269 };
271 };
270
272
271 CodeCell.prototype._open_with_pager = function (payload) {
273 CodeCell.prototype._open_with_pager = function (payload) {
272 $([IPython.events]).trigger('open_with_text.Pager', payload);
274 $([IPython.events]).trigger('open_with_text.Pager', payload);
273 };
275 };
274
276
275 /**
277 /**
276 * @method _handle_execute_reply
278 * @method _handle_execute_reply
277 * @private
279 * @private
278 */
280 */
279 CodeCell.prototype._handle_execute_reply = function (msg) {
281 CodeCell.prototype._handle_execute_reply = function (msg) {
280 this.set_input_prompt(msg.content.execution_count);
282 this.set_input_prompt(msg.content.execution_count);
281 this.element.removeClass("running");
283 this.element.removeClass("running");
282 $([IPython.events]).trigger('set_dirty.Notebook', {value: true});
284 $([IPython.events]).trigger('set_dirty.Notebook', {value: true});
283 };
285 };
284
286
285 /**
287 /**
286 * @method _handle_set_next_input
288 * @method _handle_set_next_input
287 * @private
289 * @private
288 */
290 */
289 CodeCell.prototype._handle_set_next_input = function (payload) {
291 CodeCell.prototype._handle_set_next_input = function (payload) {
290 var data = {'cell': this, 'text': payload.text};
292 var data = {'cell': this, 'text': payload.text};
291 $([IPython.events]).trigger('set_next_input.Notebook', data);
293 $([IPython.events]).trigger('set_next_input.Notebook', data);
292 };
294 };
293
295
294 /**
296 /**
295 * @method _handle_input_request
297 * @method _handle_input_request
296 * @private
298 * @private
297 */
299 */
298 CodeCell.prototype._handle_input_request = function (msg) {
300 CodeCell.prototype._handle_input_request = function (msg) {
299 this.output_area.append_raw_input(msg);
301 this.output_area.append_raw_input(msg);
300 };
302 };
301
303
302
304
303 // Basic cell manipulation.
305 // Basic cell manipulation.
304
306
305 CodeCell.prototype.select = function () {
307 CodeCell.prototype.select = function () {
306 IPython.Cell.prototype.select.apply(this);
308 IPython.Cell.prototype.select.apply(this);
307 this.code_mirror.refresh();
309 this.code_mirror.refresh();
308 this.code_mirror.focus();
310 this.code_mirror.focus();
309 this.auto_highlight();
311 this.auto_highlight();
310 // We used to need an additional refresh() after the focus, but
312 // We used to need an additional refresh() after the focus, but
311 // it appears that this has been fixed in CM. This bug would show
313 // it appears that this has been fixed in CM. This bug would show
312 // up on FF when a newly loaded markdown cell was edited.
314 // up on FF when a newly loaded markdown cell was edited.
313 };
315 };
314
316
315
317
316 CodeCell.prototype.select_all = function () {
318 CodeCell.prototype.select_all = function () {
317 var start = {line: 0, ch: 0};
319 var start = {line: 0, ch: 0};
318 var nlines = this.code_mirror.lineCount();
320 var nlines = this.code_mirror.lineCount();
319 var last_line = this.code_mirror.getLine(nlines-1);
321 var last_line = this.code_mirror.getLine(nlines-1);
320 var end = {line: nlines-1, ch: last_line.length};
322 var end = {line: nlines-1, ch: last_line.length};
321 this.code_mirror.setSelection(start, end);
323 this.code_mirror.setSelection(start, end);
322 };
324 };
323
325
324
326
325 CodeCell.prototype.collapse = function () {
327 CodeCell.prototype.collapse = function () {
326 this.collapsed = true;
328 this.collapsed = true;
327 this.output_area.collapse();
329 this.output_area.collapse();
328 };
330 };
329
331
330
332
331 CodeCell.prototype.expand = function () {
333 CodeCell.prototype.expand = function () {
332 this.collapsed = false;
334 this.collapsed = false;
333 this.output_area.expand();
335 this.output_area.expand();
334 };
336 };
335
337
336
338
337 CodeCell.prototype.toggle_output = function () {
339 CodeCell.prototype.toggle_output = function () {
338 this.collapsed = Boolean(1 - this.collapsed);
340 this.collapsed = Boolean(1 - this.collapsed);
339 this.output_area.toggle_output();
341 this.output_area.toggle_output();
340 };
342 };
341
343
342
344
343 CodeCell.prototype.toggle_output_scroll = function () {
345 CodeCell.prototype.toggle_output_scroll = function () {
344 this.output_area.toggle_scroll();
346 this.output_area.toggle_scroll();
345 };
347 };
346
348
347
349
348 CodeCell.input_prompt_classical = function (prompt_value, lines_number) {
350 CodeCell.input_prompt_classical = function (prompt_value, lines_number) {
349 var ns = prompt_value || "&nbsp;";
351 var ns = prompt_value || "&nbsp;";
350 return 'In&nbsp;[' + ns + ']:';
352 return 'In&nbsp;[' + ns + ']:';
351 };
353 };
352
354
353 CodeCell.input_prompt_continuation = function (prompt_value, lines_number) {
355 CodeCell.input_prompt_continuation = function (prompt_value, lines_number) {
354 var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)];
356 var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)];
355 for(var i=1; i < lines_number; i++) {
357 for(var i=1; i < lines_number; i++) {
356 html.push(['...:']);
358 html.push(['...:']);
357 }
359 }
358 return html.join('<br/>');
360 return html.join('<br/>');
359 };
361 };
360
362
361 CodeCell.input_prompt_function = CodeCell.input_prompt_classical;
363 CodeCell.input_prompt_function = CodeCell.input_prompt_classical;
362
364
363
365
364 CodeCell.prototype.set_input_prompt = function (number) {
366 CodeCell.prototype.set_input_prompt = function (number) {
365 var nline = 1;
367 var nline = 1;
366 if (this.code_mirror !== undefined) {
368 if (this.code_mirror !== undefined) {
367 nline = this.code_mirror.lineCount();
369 nline = this.code_mirror.lineCount();
368 }
370 }
369 this.input_prompt_number = number;
371 this.input_prompt_number = number;
370 var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline);
372 var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline);
371 this.element.find('div.input_prompt').html(prompt_html);
373 this.element.find('div.input_prompt').html(prompt_html);
372 };
374 };
373
375
374
376
375 CodeCell.prototype.clear_input = function () {
377 CodeCell.prototype.clear_input = function () {
376 this.code_mirror.setValue('');
378 this.code_mirror.setValue('');
377 };
379 };
378
380
379
381
380 CodeCell.prototype.get_text = function () {
382 CodeCell.prototype.get_text = function () {
381 return this.code_mirror.getValue();
383 return this.code_mirror.getValue();
382 };
384 };
383
385
384
386
385 CodeCell.prototype.set_text = function (code) {
387 CodeCell.prototype.set_text = function (code) {
386 return this.code_mirror.setValue(code);
388 return this.code_mirror.setValue(code);
387 };
389 };
388
390
389
391
390 CodeCell.prototype.at_top = function () {
392 CodeCell.prototype.at_top = function () {
391 var cursor = this.code_mirror.getCursor();
393 var cursor = this.code_mirror.getCursor();
392 if (cursor.line === 0 && cursor.ch === 0) {
394 if (cursor.line === 0 && cursor.ch === 0) {
393 return true;
395 return true;
394 } else {
396 } else {
395 return false;
397 return false;
396 }
398 }
397 };
399 };
398
400
399
401
400 CodeCell.prototype.at_bottom = function () {
402 CodeCell.prototype.at_bottom = function () {
401 var cursor = this.code_mirror.getCursor();
403 var cursor = this.code_mirror.getCursor();
402 if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) {
404 if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) {
403 return true;
405 return true;
404 } else {
406 } else {
405 return false;
407 return false;
406 }
408 }
407 };
409 };
408
410
409
411
410 CodeCell.prototype.clear_output = function (wait) {
412 CodeCell.prototype.clear_output = function (wait) {
411 this.output_area.clear_output(wait);
413 this.output_area.clear_output(wait);
412 };
414 };
413
415
414
416
415 // JSON serialization
417 // JSON serialization
416
418
417 CodeCell.prototype.fromJSON = function (data) {
419 CodeCell.prototype.fromJSON = function (data) {
418 IPython.Cell.prototype.fromJSON.apply(this, arguments);
420 IPython.Cell.prototype.fromJSON.apply(this, arguments);
419 if (data.cell_type === 'code') {
421 if (data.cell_type === 'code') {
420 if (data.input !== undefined) {
422 if (data.input !== undefined) {
421 this.set_text(data.input);
423 this.set_text(data.input);
422 // make this value the starting point, so that we can only undo
424 // make this value the starting point, so that we can only undo
423 // to this state, instead of a blank cell
425 // to this state, instead of a blank cell
424 this.code_mirror.clearHistory();
426 this.code_mirror.clearHistory();
425 this.auto_highlight();
427 this.auto_highlight();
426 }
428 }
427 if (data.prompt_number !== undefined) {
429 if (data.prompt_number !== undefined) {
428 this.set_input_prompt(data.prompt_number);
430 this.set_input_prompt(data.prompt_number);
429 } else {
431 } else {
430 this.set_input_prompt();
432 this.set_input_prompt();
431 }
433 }
432 this.output_area.fromJSON(data.outputs);
434 this.output_area.fromJSON(data.outputs);
433 if (data.collapsed !== undefined) {
435 if (data.collapsed !== undefined) {
434 if (data.collapsed) {
436 if (data.collapsed) {
435 this.collapse();
437 this.collapse();
436 } else {
438 } else {
437 this.expand();
439 this.expand();
438 }
440 }
439 }
441 }
440 }
442 }
441 };
443 };
442
444
443
445
444 CodeCell.prototype.toJSON = function () {
446 CodeCell.prototype.toJSON = function () {
445 var data = IPython.Cell.prototype.toJSON.apply(this);
447 var data = IPython.Cell.prototype.toJSON.apply(this);
446 data.input = this.get_text();
448 data.input = this.get_text();
447 data.cell_type = 'code';
449 data.cell_type = 'code';
448 // is finite protect against undefined and '*' value
450 // is finite protect against undefined and '*' value
449 if (isFinite(this.input_prompt_number)) {
451 if (isFinite(this.input_prompt_number)) {
450 data.prompt_number = this.input_prompt_number;
452 data.prompt_number = this.input_prompt_number;
451 }
453 }
452 var outputs = this.output_area.toJSON();
454 var outputs = this.output_area.toJSON();
453 data.outputs = outputs;
455 data.outputs = outputs;
454 data.language = 'python';
456 data.language = 'python';
455 data.collapsed = this.collapsed;
457 data.collapsed = this.collapsed;
456 return data;
458 return data;
457 };
459 };
458
460
459
461
460 IPython.CodeCell = CodeCell;
462 IPython.CodeCell = CodeCell;
461
463
462 return IPython;
464 return IPython;
463 }(IPython));
465 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now