##// END OF EJS Templates
prompt '*' strore fix + tab remove tooltip...
Matthias BUSSONNIER -
Show More
@@ -1,460 +1,463 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 IPython.tooltip.remove_and_cancel_tooltip();
206 if (editor.somethingSelected()) { return false; }
206 if (editor.somethingSelected()) {
207 return false;
208 }
207 var pre_cursor = editor.getRange({line:cur.line,ch:0},cur);
209 var pre_cursor = editor.getRange({line:cur.line,ch:0},cur);
208 if (pre_cursor.trim() === "") {
210 if (pre_cursor.trim() === "") {
209 // Don't autocomplete if the part of the line before the cursor
211 // Don't autocomplete if the part of the line before the cursor
210 // is empty. In this case, let CodeMirror handle indentation.
212 // is empty. In this case, let CodeMirror handle indentation.
211 return false;
213 return false;
212 } else {
214 } else {
213 event.stop();
215 event.stop();
214 this.completer.startCompletion();
216 this.completer.startCompletion();
215 return true;
217 return true;
216 }
218 }
217 } else {
219 } else {
218 // keypress/keyup also trigger on TAB press, and we don't want to
220 // keypress/keyup also trigger on TAB press, and we don't want to
219 // use those to disable tab completion.
221 // use those to disable tab completion.
220 return false;
222 return false;
221 }
223 }
222 return false;
224 return false;
223 };
225 };
224
226
225
227
226 // Kernel related calls.
228 // Kernel related calls.
227
229
228 CodeCell.prototype.set_kernel = function (kernel) {
230 CodeCell.prototype.set_kernel = function (kernel) {
229 this.kernel = kernel;
231 this.kernel = kernel;
230 };
232 };
231
233
232 /**
234 /**
233 * Execute current code cell to the kernel
235 * Execute current code cell to the kernel
234 * @method execute
236 * @method execute
235 */
237 */
236 CodeCell.prototype.execute = function () {
238 CodeCell.prototype.execute = function () {
237 this.output_area.clear_output();
239 this.output_area.clear_output();
238 this.set_input_prompt('*');
240 this.set_input_prompt('*');
239 this.element.addClass("running");
241 this.element.addClass("running");
240 if (this.last_msg_id) {
242 if (this.last_msg_id) {
241 this.kernel.clear_callbacks_for_msg(this.last_msg_id);
243 this.kernel.clear_callbacks_for_msg(this.last_msg_id);
242 }
244 }
243 var callbacks = this.get_callbacks();
245 var callbacks = this.get_callbacks();
244
246
245 this.last_msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false, store_history: true});
247 this.last_msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false, store_history: true});
246 };
248 };
247
249
248 /**
250 /**
249 * Construct the default callbacks for
251 * Construct the default callbacks for
250 * @method get_callbacks
252 * @method get_callbacks
251 */
253 */
252 CodeCell.prototype.get_callbacks = function () {
254 CodeCell.prototype.get_callbacks = function () {
253 return {
255 return {
254 shell : {
256 shell : {
255 reply : $.proxy(this._handle_execute_reply, this),
257 reply : $.proxy(this._handle_execute_reply, this),
256 payload : {
258 payload : {
257 set_next_input : $.proxy(this._handle_set_next_input, this),
259 set_next_input : $.proxy(this._handle_set_next_input, this),
258 page : $.proxy(this._open_with_pager, this)
260 page : $.proxy(this._open_with_pager, this)
259 }
261 }
260 },
262 },
261 iopub : {
263 iopub : {
262 output : $.proxy(this.output_area.handle_output, this.output_area),
264 output : $.proxy(this.output_area.handle_output, this.output_area),
263 clear_output : $.proxy(this.output_area.handle_clear_output, this.output_area),
265 clear_output : $.proxy(this.output_area.handle_clear_output, this.output_area),
264 },
266 },
265 input : $.proxy(this._handle_input_request, this)
267 input : $.proxy(this._handle_input_request, this)
266 };
268 };
267 };
269 };
268
270
269 CodeCell.prototype._open_with_pager = function (payload) {
271 CodeCell.prototype._open_with_pager = function (payload) {
270 $([IPython.events]).trigger('open_with_text.Pager', payload);
272 $([IPython.events]).trigger('open_with_text.Pager', payload);
271 };
273 };
272
274
273 /**
275 /**
274 * @method _handle_execute_reply
276 * @method _handle_execute_reply
275 * @private
277 * @private
276 */
278 */
277 CodeCell.prototype._handle_execute_reply = function (msg) {
279 CodeCell.prototype._handle_execute_reply = function (msg) {
278 this.set_input_prompt(msg.content.execution_count);
280 this.set_input_prompt(msg.content.execution_count);
279 this.element.removeClass("running");
281 this.element.removeClass("running");
280 $([IPython.events]).trigger('set_dirty.Notebook', {value: true});
282 $([IPython.events]).trigger('set_dirty.Notebook', {value: true});
281 };
283 };
282
284
283 /**
285 /**
284 * @method _handle_set_next_input
286 * @method _handle_set_next_input
285 * @private
287 * @private
286 */
288 */
287 CodeCell.prototype._handle_set_next_input = function (payload) {
289 CodeCell.prototype._handle_set_next_input = function (payload) {
288 var data = {'cell': this, 'text': payload.text};
290 var data = {'cell': this, 'text': payload.text};
289 $([IPython.events]).trigger('set_next_input.Notebook', data);
291 $([IPython.events]).trigger('set_next_input.Notebook', data);
290 };
292 };
291
293
292 /**
294 /**
293 * @method _handle_input_request
295 * @method _handle_input_request
294 * @private
296 * @private
295 */
297 */
296 CodeCell.prototype._handle_input_request = function (msg) {
298 CodeCell.prototype._handle_input_request = function (msg) {
297 this.output_area.append_raw_input(msg);
299 this.output_area.append_raw_input(msg);
298 };
300 };
299
301
300
302
301 // Basic cell manipulation.
303 // Basic cell manipulation.
302
304
303 CodeCell.prototype.select = function () {
305 CodeCell.prototype.select = function () {
304 IPython.Cell.prototype.select.apply(this);
306 IPython.Cell.prototype.select.apply(this);
305 this.code_mirror.refresh();
307 this.code_mirror.refresh();
306 this.code_mirror.focus();
308 this.code_mirror.focus();
307 this.auto_highlight();
309 this.auto_highlight();
308 // We used to need an additional refresh() after the focus, but
310 // We used to need an additional refresh() after the focus, but
309 // it appears that this has been fixed in CM. This bug would show
311 // it appears that this has been fixed in CM. This bug would show
310 // up on FF when a newly loaded markdown cell was edited.
312 // up on FF when a newly loaded markdown cell was edited.
311 };
313 };
312
314
313
315
314 CodeCell.prototype.select_all = function () {
316 CodeCell.prototype.select_all = function () {
315 var start = {line: 0, ch: 0};
317 var start = {line: 0, ch: 0};
316 var nlines = this.code_mirror.lineCount();
318 var nlines = this.code_mirror.lineCount();
317 var last_line = this.code_mirror.getLine(nlines-1);
319 var last_line = this.code_mirror.getLine(nlines-1);
318 var end = {line: nlines-1, ch: last_line.length};
320 var end = {line: nlines-1, ch: last_line.length};
319 this.code_mirror.setSelection(start, end);
321 this.code_mirror.setSelection(start, end);
320 };
322 };
321
323
322
324
323 CodeCell.prototype.collapse = function () {
325 CodeCell.prototype.collapse = function () {
324 this.collapsed = true;
326 this.collapsed = true;
325 this.output_area.collapse();
327 this.output_area.collapse();
326 };
328 };
327
329
328
330
329 CodeCell.prototype.expand = function () {
331 CodeCell.prototype.expand = function () {
330 this.collapsed = false;
332 this.collapsed = false;
331 this.output_area.expand();
333 this.output_area.expand();
332 };
334 };
333
335
334
336
335 CodeCell.prototype.toggle_output = function () {
337 CodeCell.prototype.toggle_output = function () {
336 this.collapsed = Boolean(1 - this.collapsed);
338 this.collapsed = Boolean(1 - this.collapsed);
337 this.output_area.toggle_output();
339 this.output_area.toggle_output();
338 };
340 };
339
341
340
342
341 CodeCell.prototype.toggle_output_scroll = function () {
343 CodeCell.prototype.toggle_output_scroll = function () {
342 this.output_area.toggle_scroll();
344 this.output_area.toggle_scroll();
343 };
345 };
344
346
345
347
346 CodeCell.input_prompt_classical = function (prompt_value, lines_number) {
348 CodeCell.input_prompt_classical = function (prompt_value, lines_number) {
347 var ns = prompt_value || "&nbsp;";
349 var ns = prompt_value || "&nbsp;";
348 return 'In&nbsp;[' + ns + ']:';
350 return 'In&nbsp;[' + ns + ']:';
349 };
351 };
350
352
351 CodeCell.input_prompt_continuation = function (prompt_value, lines_number) {
353 CodeCell.input_prompt_continuation = function (prompt_value, lines_number) {
352 var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)];
354 var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)];
353 for(var i=1; i < lines_number; i++) {
355 for(var i=1; i < lines_number; i++) {
354 html.push(['...:']);
356 html.push(['...:']);
355 }
357 }
356 return html.join('<br/>');
358 return html.join('<br/>');
357 };
359 };
358
360
359 CodeCell.input_prompt_function = CodeCell.input_prompt_classical;
361 CodeCell.input_prompt_function = CodeCell.input_prompt_classical;
360
362
361
363
362 CodeCell.prototype.set_input_prompt = function (number) {
364 CodeCell.prototype.set_input_prompt = function (number) {
363 var nline = 1;
365 var nline = 1;
364 if (this.code_mirror !== undefined) {
366 if (this.code_mirror !== undefined) {
365 nline = this.code_mirror.lineCount();
367 nline = this.code_mirror.lineCount();
366 }
368 }
367 this.input_prompt_number = number;
369 this.input_prompt_number = number;
368 var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline);
370 var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline);
369 this.element.find('div.input_prompt').html(prompt_html);
371 this.element.find('div.input_prompt').html(prompt_html);
370 };
372 };
371
373
372
374
373 CodeCell.prototype.clear_input = function () {
375 CodeCell.prototype.clear_input = function () {
374 this.code_mirror.setValue('');
376 this.code_mirror.setValue('');
375 };
377 };
376
378
377
379
378 CodeCell.prototype.get_text = function () {
380 CodeCell.prototype.get_text = function () {
379 return this.code_mirror.getValue();
381 return this.code_mirror.getValue();
380 };
382 };
381
383
382
384
383 CodeCell.prototype.set_text = function (code) {
385 CodeCell.prototype.set_text = function (code) {
384 return this.code_mirror.setValue(code);
386 return this.code_mirror.setValue(code);
385 };
387 };
386
388
387
389
388 CodeCell.prototype.at_top = function () {
390 CodeCell.prototype.at_top = function () {
389 var cursor = this.code_mirror.getCursor();
391 var cursor = this.code_mirror.getCursor();
390 if (cursor.line === 0 && cursor.ch === 0) {
392 if (cursor.line === 0 && cursor.ch === 0) {
391 return true;
393 return true;
392 } else {
394 } else {
393 return false;
395 return false;
394 }
396 }
395 };
397 };
396
398
397
399
398 CodeCell.prototype.at_bottom = function () {
400 CodeCell.prototype.at_bottom = function () {
399 var cursor = this.code_mirror.getCursor();
401 var cursor = this.code_mirror.getCursor();
400 if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) {
402 if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) {
401 return true;
403 return true;
402 } else {
404 } else {
403 return false;
405 return false;
404 }
406 }
405 };
407 };
406
408
407
409
408 CodeCell.prototype.clear_output = function (wait) {
410 CodeCell.prototype.clear_output = function (wait) {
409 this.output_area.clear_output(wait);
411 this.output_area.clear_output(wait);
410 };
412 };
411
413
412
414
413 // JSON serialization
415 // JSON serialization
414
416
415 CodeCell.prototype.fromJSON = function (data) {
417 CodeCell.prototype.fromJSON = function (data) {
416 IPython.Cell.prototype.fromJSON.apply(this, arguments);
418 IPython.Cell.prototype.fromJSON.apply(this, arguments);
417 if (data.cell_type === 'code') {
419 if (data.cell_type === 'code') {
418 if (data.input !== undefined) {
420 if (data.input !== undefined) {
419 this.set_text(data.input);
421 this.set_text(data.input);
420 // make this value the starting point, so that we can only undo
422 // make this value the starting point, so that we can only undo
421 // to this state, instead of a blank cell
423 // to this state, instead of a blank cell
422 this.code_mirror.clearHistory();
424 this.code_mirror.clearHistory();
423 this.auto_highlight();
425 this.auto_highlight();
424 }
426 }
425 if (data.prompt_number !== undefined) {
427 if (data.prompt_number !== undefined) {
426 this.set_input_prompt(data.prompt_number);
428 this.set_input_prompt(data.prompt_number);
427 } else {
429 } else {
428 this.set_input_prompt();
430 this.set_input_prompt();
429 }
431 }
430 this.output_area.fromJSON(data.outputs);
432 this.output_area.fromJSON(data.outputs);
431 if (data.collapsed !== undefined) {
433 if (data.collapsed !== undefined) {
432 if (data.collapsed) {
434 if (data.collapsed) {
433 this.collapse();
435 this.collapse();
434 } else {
436 } else {
435 this.expand();
437 this.expand();
436 }
438 }
437 }
439 }
438 }
440 }
439 };
441 };
440
442
441
443
442 CodeCell.prototype.toJSON = function () {
444 CodeCell.prototype.toJSON = function () {
443 var data = IPython.Cell.prototype.toJSON.apply(this);
445 var data = IPython.Cell.prototype.toJSON.apply(this);
444 data.input = this.get_text();
446 data.input = this.get_text();
445 data.cell_type = 'code';
447 data.cell_type = 'code';
446 if (this.input_prompt_number) {
448 // is finite protect against undefined and '*' value
449 if (isFinite(this.input_prompt_number)) {
447 data.prompt_number = this.input_prompt_number;
450 data.prompt_number = this.input_prompt_number;
448 }
451 }
449 var outputs = this.output_area.toJSON();
452 var outputs = this.output_area.toJSON();
450 data.outputs = outputs;
453 data.outputs = outputs;
451 data.language = 'python';
454 data.language = 'python';
452 data.collapsed = this.collapsed;
455 data.collapsed = this.collapsed;
453 return data;
456 return data;
454 };
457 };
455
458
456
459
457 IPython.CodeCell = CodeCell;
460 IPython.CodeCell = CodeCell;
458
461
459 return IPython;
462 return IPython;
460 }(IPython));
463 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now