##// END OF EJS Templates
Fixed glitch when widgetarea wouldn't get completely hidden upon re-execution
Jonathan Frederic -
Show More
@@ -1,549 +1,553
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
308 // Clear widget area
307 this.widget_subarea.html('');
309 this.widget_subarea.html('');
308 this.widget_subarea.height('');
310 this.widget_subarea.height('');
309 this.widget_area.height('');
311 this.widget_area.height('');
312 this.widget_area.hide();
313
310 this.set_input_prompt('*');
314 this.set_input_prompt('*');
311 this.element.addClass("running");
315 this.element.addClass("running");
312 if (this.last_msg_id) {
316 if (this.last_msg_id) {
313 this.kernel.clear_callbacks_for_msg(this.last_msg_id);
317 this.kernel.clear_callbacks_for_msg(this.last_msg_id);
314 }
318 }
315 var callbacks = this.get_callbacks();
319 var callbacks = this.get_callbacks();
316
320
317 this.last_msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false, store_history: true});
321 this.last_msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false, store_history: true});
318 };
322 };
319
323
320 /**
324 /**
321 * Construct the default callbacks for
325 * Construct the default callbacks for
322 * @method get_callbacks
326 * @method get_callbacks
323 */
327 */
324 CodeCell.prototype.get_callbacks = function () {
328 CodeCell.prototype.get_callbacks = function () {
325 return {
329 return {
326 shell : {
330 shell : {
327 reply : $.proxy(this._handle_execute_reply, this),
331 reply : $.proxy(this._handle_execute_reply, this),
328 payload : {
332 payload : {
329 set_next_input : $.proxy(this._handle_set_next_input, this),
333 set_next_input : $.proxy(this._handle_set_next_input, this),
330 page : $.proxy(this._open_with_pager, this)
334 page : $.proxy(this._open_with_pager, this)
331 }
335 }
332 },
336 },
333 iopub : {
337 iopub : {
334 output : $.proxy(this.output_area.handle_output, this.output_area),
338 output : $.proxy(this.output_area.handle_output, this.output_area),
335 clear_output : $.proxy(this.output_area.handle_clear_output, this.output_area),
339 clear_output : $.proxy(this.output_area.handle_clear_output, this.output_area),
336 },
340 },
337 input : $.proxy(this._handle_input_request, this)
341 input : $.proxy(this._handle_input_request, this)
338 };
342 };
339 };
343 };
340
344
341 CodeCell.prototype._open_with_pager = function (payload) {
345 CodeCell.prototype._open_with_pager = function (payload) {
342 $([IPython.events]).trigger('open_with_text.Pager', payload);
346 $([IPython.events]).trigger('open_with_text.Pager', payload);
343 };
347 };
344
348
345 /**
349 /**
346 * @method _handle_execute_reply
350 * @method _handle_execute_reply
347 * @private
351 * @private
348 */
352 */
349 CodeCell.prototype._handle_execute_reply = function (msg) {
353 CodeCell.prototype._handle_execute_reply = function (msg) {
350 this.set_input_prompt(msg.content.execution_count);
354 this.set_input_prompt(msg.content.execution_count);
351 this.element.removeClass("running");
355 this.element.removeClass("running");
352 $([IPython.events]).trigger('set_dirty.Notebook', {value: true});
356 $([IPython.events]).trigger('set_dirty.Notebook', {value: true});
353 };
357 };
354
358
355 /**
359 /**
356 * @method _handle_set_next_input
360 * @method _handle_set_next_input
357 * @private
361 * @private
358 */
362 */
359 CodeCell.prototype._handle_set_next_input = function (payload) {
363 CodeCell.prototype._handle_set_next_input = function (payload) {
360 var data = {'cell': this, 'text': payload.text};
364 var data = {'cell': this, 'text': payload.text};
361 $([IPython.events]).trigger('set_next_input.Notebook', data);
365 $([IPython.events]).trigger('set_next_input.Notebook', data);
362 };
366 };
363
367
364 /**
368 /**
365 * @method _handle_input_request
369 * @method _handle_input_request
366 * @private
370 * @private
367 */
371 */
368 CodeCell.prototype._handle_input_request = function (msg) {
372 CodeCell.prototype._handle_input_request = function (msg) {
369 this.output_area.append_raw_input(msg);
373 this.output_area.append_raw_input(msg);
370 };
374 };
371
375
372
376
373 // Basic cell manipulation.
377 // Basic cell manipulation.
374
378
375 CodeCell.prototype.select = function () {
379 CodeCell.prototype.select = function () {
376 var cont = IPython.Cell.prototype.select.apply(this);
380 var cont = IPython.Cell.prototype.select.apply(this);
377 if (cont) {
381 if (cont) {
378 this.code_mirror.refresh();
382 this.code_mirror.refresh();
379 this.auto_highlight();
383 this.auto_highlight();
380 }
384 }
381 return cont;
385 return cont;
382 };
386 };
383
387
384 CodeCell.prototype.render = function () {
388 CodeCell.prototype.render = function () {
385 var cont = IPython.Cell.prototype.render.apply(this);
389 var cont = IPython.Cell.prototype.render.apply(this);
386 // Always execute, even if we are already in the rendered state
390 // Always execute, even if we are already in the rendered state
387 return cont;
391 return cont;
388 };
392 };
389
393
390 CodeCell.prototype.unrender = function () {
394 CodeCell.prototype.unrender = function () {
391 // CodeCell is always rendered
395 // CodeCell is always rendered
392 return false;
396 return false;
393 };
397 };
394
398
395 CodeCell.prototype.edit_mode = function () {
399 CodeCell.prototype.edit_mode = function () {
396 var cont = IPython.Cell.prototype.edit_mode.apply(this);
400 var cont = IPython.Cell.prototype.edit_mode.apply(this);
397 if (cont) {
401 if (cont) {
398 this.focus_editor();
402 this.focus_editor();
399 }
403 }
400 return cont;
404 return cont;
401 }
405 }
402
406
403 CodeCell.prototype.select_all = function () {
407 CodeCell.prototype.select_all = function () {
404 var start = {line: 0, ch: 0};
408 var start = {line: 0, ch: 0};
405 var nlines = this.code_mirror.lineCount();
409 var nlines = this.code_mirror.lineCount();
406 var last_line = this.code_mirror.getLine(nlines-1);
410 var last_line = this.code_mirror.getLine(nlines-1);
407 var end = {line: nlines-1, ch: last_line.length};
411 var end = {line: nlines-1, ch: last_line.length};
408 this.code_mirror.setSelection(start, end);
412 this.code_mirror.setSelection(start, end);
409 };
413 };
410
414
411
415
412 CodeCell.prototype.collapse = function () {
416 CodeCell.prototype.collapse = function () {
413 this.collapsed = true;
417 this.collapsed = true;
414 this.output_area.collapse();
418 this.output_area.collapse();
415 };
419 };
416
420
417
421
418 CodeCell.prototype.expand = function () {
422 CodeCell.prototype.expand = function () {
419 this.collapsed = false;
423 this.collapsed = false;
420 this.output_area.expand();
424 this.output_area.expand();
421 };
425 };
422
426
423
427
424 CodeCell.prototype.toggle_output = function () {
428 CodeCell.prototype.toggle_output = function () {
425 this.collapsed = Boolean(1 - this.collapsed);
429 this.collapsed = Boolean(1 - this.collapsed);
426 this.output_area.toggle_output();
430 this.output_area.toggle_output();
427 };
431 };
428
432
429
433
430 CodeCell.prototype.toggle_output_scroll = function () {
434 CodeCell.prototype.toggle_output_scroll = function () {
431 this.output_area.toggle_scroll();
435 this.output_area.toggle_scroll();
432 };
436 };
433
437
434
438
435 CodeCell.input_prompt_classical = function (prompt_value, lines_number) {
439 CodeCell.input_prompt_classical = function (prompt_value, lines_number) {
436 var ns = prompt_value || "&nbsp;";
440 var ns = prompt_value || "&nbsp;";
437 return 'In&nbsp;[' + ns + ']:';
441 return 'In&nbsp;[' + ns + ']:';
438 };
442 };
439
443
440 CodeCell.input_prompt_continuation = function (prompt_value, lines_number) {
444 CodeCell.input_prompt_continuation = function (prompt_value, lines_number) {
441 var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)];
445 var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)];
442 for(var i=1; i < lines_number; i++) {
446 for(var i=1; i < lines_number; i++) {
443 html.push(['...:']);
447 html.push(['...:']);
444 }
448 }
445 return html.join('<br/>');
449 return html.join('<br/>');
446 };
450 };
447
451
448 CodeCell.input_prompt_function = CodeCell.input_prompt_classical;
452 CodeCell.input_prompt_function = CodeCell.input_prompt_classical;
449
453
450
454
451 CodeCell.prototype.set_input_prompt = function (number) {
455 CodeCell.prototype.set_input_prompt = function (number) {
452 var nline = 1;
456 var nline = 1;
453 if (this.code_mirror !== undefined) {
457 if (this.code_mirror !== undefined) {
454 nline = this.code_mirror.lineCount();
458 nline = this.code_mirror.lineCount();
455 }
459 }
456 this.input_prompt_number = number;
460 this.input_prompt_number = number;
457 var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline);
461 var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline);
458 this.element.find('div.input_prompt').html(prompt_html);
462 this.element.find('div.input_prompt').html(prompt_html);
459 };
463 };
460
464
461
465
462 CodeCell.prototype.clear_input = function () {
466 CodeCell.prototype.clear_input = function () {
463 this.code_mirror.setValue('');
467 this.code_mirror.setValue('');
464 };
468 };
465
469
466
470
467 CodeCell.prototype.get_text = function () {
471 CodeCell.prototype.get_text = function () {
468 return this.code_mirror.getValue();
472 return this.code_mirror.getValue();
469 };
473 };
470
474
471
475
472 CodeCell.prototype.set_text = function (code) {
476 CodeCell.prototype.set_text = function (code) {
473 return this.code_mirror.setValue(code);
477 return this.code_mirror.setValue(code);
474 };
478 };
475
479
476
480
477 CodeCell.prototype.at_top = function () {
481 CodeCell.prototype.at_top = function () {
478 var cursor = this.code_mirror.getCursor();
482 var cursor = this.code_mirror.getCursor();
479 if (cursor.line === 0 && cursor.ch === 0) {
483 if (cursor.line === 0 && cursor.ch === 0) {
480 return true;
484 return true;
481 } else {
485 } else {
482 return false;
486 return false;
483 }
487 }
484 };
488 };
485
489
486
490
487 CodeCell.prototype.at_bottom = function () {
491 CodeCell.prototype.at_bottom = function () {
488 var cursor = this.code_mirror.getCursor();
492 var cursor = this.code_mirror.getCursor();
489 if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) {
493 if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) {
490 return true;
494 return true;
491 } else {
495 } else {
492 return false;
496 return false;
493 }
497 }
494 };
498 };
495
499
496
500
497 CodeCell.prototype.clear_output = function (wait) {
501 CodeCell.prototype.clear_output = function (wait) {
498 this.output_area.clear_output(wait);
502 this.output_area.clear_output(wait);
499 };
503 };
500
504
501
505
502 // JSON serialization
506 // JSON serialization
503
507
504 CodeCell.prototype.fromJSON = function (data) {
508 CodeCell.prototype.fromJSON = function (data) {
505 IPython.Cell.prototype.fromJSON.apply(this, arguments);
509 IPython.Cell.prototype.fromJSON.apply(this, arguments);
506 if (data.cell_type === 'code') {
510 if (data.cell_type === 'code') {
507 if (data.input !== undefined) {
511 if (data.input !== undefined) {
508 this.set_text(data.input);
512 this.set_text(data.input);
509 // make this value the starting point, so that we can only undo
513 // make this value the starting point, so that we can only undo
510 // to this state, instead of a blank cell
514 // to this state, instead of a blank cell
511 this.code_mirror.clearHistory();
515 this.code_mirror.clearHistory();
512 this.auto_highlight();
516 this.auto_highlight();
513 }
517 }
514 if (data.prompt_number !== undefined) {
518 if (data.prompt_number !== undefined) {
515 this.set_input_prompt(data.prompt_number);
519 this.set_input_prompt(data.prompt_number);
516 } else {
520 } else {
517 this.set_input_prompt();
521 this.set_input_prompt();
518 }
522 }
519 this.output_area.fromJSON(data.outputs);
523 this.output_area.fromJSON(data.outputs);
520 if (data.collapsed !== undefined) {
524 if (data.collapsed !== undefined) {
521 if (data.collapsed) {
525 if (data.collapsed) {
522 this.collapse();
526 this.collapse();
523 } else {
527 } else {
524 this.expand();
528 this.expand();
525 }
529 }
526 }
530 }
527 }
531 }
528 };
532 };
529
533
530
534
531 CodeCell.prototype.toJSON = function () {
535 CodeCell.prototype.toJSON = function () {
532 var data = IPython.Cell.prototype.toJSON.apply(this);
536 var data = IPython.Cell.prototype.toJSON.apply(this);
533 data.input = this.get_text();
537 data.input = this.get_text();
534 // is finite protect against undefined and '*' value
538 // is finite protect against undefined and '*' value
535 if (isFinite(this.input_prompt_number)) {
539 if (isFinite(this.input_prompt_number)) {
536 data.prompt_number = this.input_prompt_number;
540 data.prompt_number = this.input_prompt_number;
537 }
541 }
538 var outputs = this.output_area.toJSON();
542 var outputs = this.output_area.toJSON();
539 data.outputs = outputs;
543 data.outputs = outputs;
540 data.language = 'python';
544 data.language = 'python';
541 data.collapsed = this.collapsed;
545 data.collapsed = this.collapsed;
542 return data;
546 return data;
543 };
547 };
544
548
545
549
546 IPython.CodeCell = CodeCell;
550 IPython.CodeCell = CodeCell;
547
551
548 return IPython;
552 return IPython;
549 }(IPython));
553 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now