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