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