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