##// END OF EJS Templates
Move should cancel blur into codecell
Jonathan Frederic -
Show More
@@ -1,494 +1,494 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 // Cell
9 // Cell
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 Cell
15 * @submodule Cell
16 */
16 */
17
17
18 var IPython = (function (IPython) {
18 var IPython = (function (IPython) {
19 "use strict";
19 "use strict";
20
20
21 var utils = IPython.utils;
21 var utils = IPython.utils;
22
22
23 /**
23 /**
24 * The Base `Cell` class from which to inherit
24 * The Base `Cell` class from which to inherit
25 * @class Cell
25 * @class Cell
26 **/
26 **/
27
27
28 /*
28 /*
29 * @constructor
29 * @constructor
30 *
30 *
31 * @param {object|undefined} [options]
31 * @param {object|undefined} [options]
32 * @param [options.cm_config] {object} config to pass to CodeMirror, will extend default parameters
32 * @param [options.cm_config] {object} config to pass to CodeMirror, will extend default parameters
33 */
33 */
34 var Cell = function (options) {
34 var Cell = function (options) {
35
35
36 options = this.mergeopt(Cell, options);
36 options = this.mergeopt(Cell, options);
37 // superclass default overwrite our default
37 // superclass default overwrite our default
38
38
39 this.placeholder = options.placeholder || '';
39 this.placeholder = options.placeholder || '';
40 this.read_only = options.cm_config.readOnly;
40 this.read_only = options.cm_config.readOnly;
41 this.selected = false;
41 this.selected = false;
42 this.rendered = false;
42 this.rendered = false;
43 this.mode = 'command';
43 this.mode = 'command';
44 this.metadata = {};
44 this.metadata = {};
45 // load this from metadata later ?
45 // load this from metadata later ?
46 this.user_highlight = 'auto';
46 this.user_highlight = 'auto';
47 this.cm_config = options.cm_config;
47 this.cm_config = options.cm_config;
48 this.cell_id = utils.uuid();
48 this.cell_id = utils.uuid();
49 this._options = options;
49 this._options = options;
50
50
51 // For JS VM engines optimization, attributes should be all set (even
51 // For JS VM engines optimization, attributes should be all set (even
52 // to null) in the constructor, and if possible, if different subclass
52 // to null) in the constructor, and if possible, if different subclass
53 // have new attributes with same name, they should be created in the
53 // have new attributes with same name, they should be created in the
54 // same order. Easiest is to create and set to null in parent class.
54 // same order. Easiest is to create and set to null in parent class.
55
55
56 this.element = null;
56 this.element = null;
57 this.cell_type = this.cell_type || null;
57 this.cell_type = this.cell_type || null;
58 this.code_mirror = null;
58 this.code_mirror = null;
59
59
60 this.create_element();
60 this.create_element();
61 if (this.element !== null) {
61 if (this.element !== null) {
62 this.element.data("cell", this);
62 this.element.data("cell", this);
63 this.bind_events();
63 this.bind_events();
64 this.init_classes();
64 this.init_classes();
65 }
65 }
66 };
66 };
67
67
68 Cell.options_default = {
68 Cell.options_default = {
69 cm_config : {
69 cm_config : {
70 indentUnit : 4,
70 indentUnit : 4,
71 readOnly: false,
71 readOnly: false,
72 theme: "default"
72 theme: "default"
73 }
73 }
74 };
74 };
75
75
76 // FIXME: Workaround CM Bug #332 (Safari segfault on drag)
76 // FIXME: Workaround CM Bug #332 (Safari segfault on drag)
77 // by disabling drag/drop altogether on Safari
77 // by disabling drag/drop altogether on Safari
78 // https://github.com/marijnh/CodeMirror/issues/332
78 // https://github.com/marijnh/CodeMirror/issues/332
79 if (utils.browser[0] == "Safari") {
79 if (utils.browser[0] == "Safari") {
80 Cell.options_default.cm_config.dragDrop = false;
80 Cell.options_default.cm_config.dragDrop = false;
81 }
81 }
82
82
83 Cell.prototype.mergeopt = function(_class, options, overwrite){
83 Cell.prototype.mergeopt = function(_class, options, overwrite){
84 options = options || {};
84 options = options || {};
85 overwrite = overwrite || {};
85 overwrite = overwrite || {};
86 return $.extend(true, {}, _class.options_default, options, overwrite);
86 return $.extend(true, {}, _class.options_default, options, overwrite);
87 };
87 };
88
88
89 /**
89 /**
90 * Empty. Subclasses must implement create_element.
90 * Empty. Subclasses must implement create_element.
91 * This should contain all the code to create the DOM element in notebook
91 * This should contain all the code to create the DOM element in notebook
92 * and will be called by Base Class constructor.
92 * and will be called by Base Class constructor.
93 * @method create_element
93 * @method create_element
94 */
94 */
95 Cell.prototype.create_element = function () {
95 Cell.prototype.create_element = function () {
96 };
96 };
97
97
98 Cell.prototype.init_classes = function () {
98 Cell.prototype.init_classes = function () {
99 // Call after this.element exists to initialize the css classes
99 // Call after this.element exists to initialize the css classes
100 // related to selected, rendered and mode.
100 // related to selected, rendered and mode.
101 if (this.selected) {
101 if (this.selected) {
102 this.element.addClass('selected');
102 this.element.addClass('selected');
103 } else {
103 } else {
104 this.element.addClass('unselected');
104 this.element.addClass('unselected');
105 }
105 }
106 if (this.rendered) {
106 if (this.rendered) {
107 this.element.addClass('rendered');
107 this.element.addClass('rendered');
108 } else {
108 } else {
109 this.element.addClass('unrendered');
109 this.element.addClass('unrendered');
110 }
110 }
111 if (this.mode === 'edit') {
111 if (this.mode === 'edit') {
112 this.element.addClass('edit_mode');
112 this.element.addClass('edit_mode');
113 } else {
113 } else {
114 this.element.addClass('command_mode');
114 this.element.addClass('command_mode');
115 }
115 }
116 };
116 };
117
117
118 /**
118 /**
119 * Subclasses can implement override bind_events.
119 * Subclasses can implement override bind_events.
120 * Be carefull to call the parent method when overwriting as it fires event.
120 * Be carefull to call the parent method when overwriting as it fires event.
121 * this will be triggerd after create_element in constructor.
121 * this will be triggerd after create_element in constructor.
122 * @method bind_events
122 * @method bind_events
123 */
123 */
124 Cell.prototype.bind_events = function () {
124 Cell.prototype.bind_events = function () {
125 var that = this;
125 var that = this;
126 // We trigger events so that Cell doesn't have to depend on Notebook.
126 // We trigger events so that Cell doesn't have to depend on Notebook.
127 that.element.click(function (event) {
127 that.element.click(function (event) {
128 if (!that.selected) {
128 if (!that.selected) {
129 $([IPython.events]).trigger('select.Cell', {'cell':that});
129 $([IPython.events]).trigger('select.Cell', {'cell':that});
130 }
130 }
131 });
131 });
132 that.element.focusin(function (event) {
132 that.element.focusin(function (event) {
133 if (!that.selected) {
133 if (!that.selected) {
134 $([IPython.events]).trigger('select.Cell', {'cell':that});
134 $([IPython.events]).trigger('select.Cell', {'cell':that});
135 }
135 }
136 });
136 });
137 if (this.code_mirror) {
137 if (this.code_mirror) {
138 this.code_mirror.on("change", function(cm, change) {
138 this.code_mirror.on("change", function(cm, change) {
139 $([IPython.events]).trigger("set_dirty.Notebook", {value: true});
139 $([IPython.events]).trigger("set_dirty.Notebook", {value: true});
140 });
140 });
141 }
141 }
142 if (this.code_mirror) {
142 if (this.code_mirror) {
143 this.code_mirror.on('focus', function(cm, change) {
143 this.code_mirror.on('focus', function(cm, change) {
144 $([IPython.events]).trigger('edit_mode.Cell', {cell: that});
144 $([IPython.events]).trigger('edit_mode.Cell', {cell: that});
145 });
145 });
146 }
146 }
147 if (this.code_mirror) {
147 if (this.code_mirror) {
148 this.code_mirror.on('blur', function(cm, change) {
148 this.code_mirror.on('blur', function(cm, change) {
149 // Check if this unfocus event is legit.
149 // Check if this unfocus event is legit.
150 if (!that.should_cancel_blur()) {
150 if (!that.should_cancel_blur()) {
151 $([IPython.events]).trigger('command_mode.Cell', {cell: that});
151 $([IPython.events]).trigger('command_mode.Cell', {cell: that});
152 }
152 }
153 });
153 });
154 }
154 }
155 };
155 };
156
156
157 /**
157 /**
158 * Triger typsetting of math by mathjax on current cell element
158 * Triger typsetting of math by mathjax on current cell element
159 * @method typeset
159 * @method typeset
160 */
160 */
161 Cell.prototype.typeset = function () {
161 Cell.prototype.typeset = function () {
162 if (window.MathJax) {
162 if (window.MathJax) {
163 var cell_math = this.element.get(0);
163 var cell_math = this.element.get(0);
164 MathJax.Hub.Queue(["Typeset", MathJax.Hub, cell_math]);
164 MathJax.Hub.Queue(["Typeset", MathJax.Hub, cell_math]);
165 }
165 }
166 };
166 };
167
167
168 /**
168 /**
169 * handle cell level logic when a cell is selected
169 * handle cell level logic when a cell is selected
170 * @method select
170 * @method select
171 * @return is the action being taken
171 * @return is the action being taken
172 */
172 */
173 Cell.prototype.select = function () {
173 Cell.prototype.select = function () {
174 if (!this.selected) {
174 if (!this.selected) {
175 this.element.addClass('selected');
175 this.element.addClass('selected');
176 this.element.removeClass('unselected');
176 this.element.removeClass('unselected');
177 this.selected = true;
177 this.selected = true;
178 return true;
178 return true;
179 } else {
179 } else {
180 return false;
180 return false;
181 }
181 }
182 };
182 };
183
183
184 /**
184 /**
185 * handle cell level logic when a cell is unselected
185 * handle cell level logic when a cell is unselected
186 * @method unselect
186 * @method unselect
187 * @return is the action being taken
187 * @return is the action being taken
188 */
188 */
189 Cell.prototype.unselect = function () {
189 Cell.prototype.unselect = function () {
190 if (this.selected) {
190 if (this.selected) {
191 this.element.addClass('unselected');
191 this.element.addClass('unselected');
192 this.element.removeClass('selected');
192 this.element.removeClass('selected');
193 this.selected = false;
193 this.selected = false;
194 return true;
194 return true;
195 } else {
195 } else {
196 return false;
196 return false;
197 }
197 }
198 };
198 };
199
199
200 /**
200 /**
201 * handle cell level logic when a cell is rendered
201 * handle cell level logic when a cell is rendered
202 * @method render
202 * @method render
203 * @return is the action being taken
203 * @return is the action being taken
204 */
204 */
205 Cell.prototype.render = function () {
205 Cell.prototype.render = function () {
206 if (!this.rendered) {
206 if (!this.rendered) {
207 this.element.addClass('rendered');
207 this.element.addClass('rendered');
208 this.element.removeClass('unrendered');
208 this.element.removeClass('unrendered');
209 this.rendered = true;
209 this.rendered = true;
210 return true;
210 return true;
211 } else {
211 } else {
212 return false;
212 return false;
213 }
213 }
214 };
214 };
215
215
216 /**
216 /**
217 * handle cell level logic when a cell is unrendered
217 * handle cell level logic when a cell is unrendered
218 * @method unrender
218 * @method unrender
219 * @return is the action being taken
219 * @return is the action being taken
220 */
220 */
221 Cell.prototype.unrender = function () {
221 Cell.prototype.unrender = function () {
222 if (this.rendered) {
222 if (this.rendered) {
223 this.element.addClass('unrendered');
223 this.element.addClass('unrendered');
224 this.element.removeClass('rendered');
224 this.element.removeClass('rendered');
225 this.rendered = false;
225 this.rendered = false;
226 return true;
226 return true;
227 } else {
227 } else {
228 return false;
228 return false;
229 }
229 }
230 };
230 };
231
231
232 /**
232 /**
233 * enter the command mode for the cell
233 * enter the command mode for the cell
234 * @method command_mode
234 * @method command_mode
235 * @return is the action being taken
235 * @return is the action being taken
236 */
236 */
237 Cell.prototype.command_mode = function () {
237 Cell.prototype.command_mode = function () {
238 if (this.mode !== 'command') {
238 if (this.mode !== 'command') {
239 this.element.addClass('command_mode');
239 this.element.addClass('command_mode');
240 this.element.removeClass('edit_mode');
240 this.element.removeClass('edit_mode');
241 this.mode = 'command';
241 this.mode = 'command';
242 return true;
242 return true;
243 } else {
243 } else {
244 return false;
244 return false;
245 }
245 }
246 };
246 };
247
247
248 /**
248 /**
249 * enter the edit mode for the cell
249 * enter the edit mode for the cell
250 * @method command_mode
250 * @method command_mode
251 * @return is the action being taken
251 * @return is the action being taken
252 */
252 */
253 Cell.prototype.edit_mode = function () {
253 Cell.prototype.edit_mode = function () {
254 if (this.mode !== 'edit') {
254 if (this.mode !== 'edit') {
255 this.element.addClass('edit_mode');
255 this.element.addClass('edit_mode');
256 this.element.removeClass('command_mode');
256 this.element.removeClass('command_mode');
257 this.mode = 'edit';
257 this.mode = 'edit';
258 return true;
258 return true;
259 } else {
259 } else {
260 return false;
260 return false;
261 }
261 }
262 };
262 };
263
263
264 /**
264 /**
265 * Determine whether or not the unfocus event should be aknowledged.
265 * Determine whether or not the unfocus event should be aknowledged.
266 *
266 *
267 * @method should_cancel_blur
267 * @method should_cancel_blur
268 *
268 *
269 * @return results {bool} Whether or not to ignore the cell's blur event.
269 * @return results {bool} Whether or not to ignore the cell's blur event.
270 **/
270 **/
271 Cell.prototype.should_cancel_blur = function () {
271 Cell.prototype.should_cancel_blur = function () {
272 return IPython.tooltip && IPython.tooltip.is_visible();
272 return false;
273 };
273 };
274
274
275 /**
275 /**
276 * Focus the cell in the DOM sense
276 * Focus the cell in the DOM sense
277 * @method focus_cell
277 * @method focus_cell
278 */
278 */
279 Cell.prototype.focus_cell = function () {
279 Cell.prototype.focus_cell = function () {
280 this.element.focus();
280 this.element.focus();
281 };
281 };
282
282
283 /**
283 /**
284 * Focus the editor area so a user can type
284 * Focus the editor area so a user can type
285 *
285 *
286 * NOTE: If codemirror is focused via a mouse click event, you don't want to
286 * NOTE: If codemirror is focused via a mouse click event, you don't want to
287 * call this because it will cause a page jump.
287 * call this because it will cause a page jump.
288 * @method focus_editor
288 * @method focus_editor
289 */
289 */
290 Cell.prototype.focus_editor = function () {
290 Cell.prototype.focus_editor = function () {
291 this.code_mirror.focus();
291 this.code_mirror.focus();
292 };
292 };
293
293
294 /**
294 /**
295 * Refresh codemirror instance
295 * Refresh codemirror instance
296 * @method refresh
296 * @method refresh
297 */
297 */
298 Cell.prototype.refresh = function () {
298 Cell.prototype.refresh = function () {
299 this.code_mirror.refresh();
299 this.code_mirror.refresh();
300 };
300 };
301
301
302 /**
302 /**
303 * should be overritten by subclass
303 * should be overritten by subclass
304 * @method get_text
304 * @method get_text
305 */
305 */
306 Cell.prototype.get_text = function () {
306 Cell.prototype.get_text = function () {
307 };
307 };
308
308
309 /**
309 /**
310 * should be overritten by subclass
310 * should be overritten by subclass
311 * @method set_text
311 * @method set_text
312 * @param {string} text
312 * @param {string} text
313 */
313 */
314 Cell.prototype.set_text = function (text) {
314 Cell.prototype.set_text = function (text) {
315 };
315 };
316
316
317 /**
317 /**
318 * should be overritten by subclass
318 * should be overritten by subclass
319 * serialise cell to json.
319 * serialise cell to json.
320 * @method toJSON
320 * @method toJSON
321 **/
321 **/
322 Cell.prototype.toJSON = function () {
322 Cell.prototype.toJSON = function () {
323 var data = {};
323 var data = {};
324 data.metadata = this.metadata;
324 data.metadata = this.metadata;
325 data.cell_type = this.cell_type;
325 data.cell_type = this.cell_type;
326 return data;
326 return data;
327 };
327 };
328
328
329
329
330 /**
330 /**
331 * should be overritten by subclass
331 * should be overritten by subclass
332 * @method fromJSON
332 * @method fromJSON
333 **/
333 **/
334 Cell.prototype.fromJSON = function (data) {
334 Cell.prototype.fromJSON = function (data) {
335 if (data.metadata !== undefined) {
335 if (data.metadata !== undefined) {
336 this.metadata = data.metadata;
336 this.metadata = data.metadata;
337 }
337 }
338 this.celltoolbar.rebuild();
338 this.celltoolbar.rebuild();
339 };
339 };
340
340
341
341
342 /**
342 /**
343 * can the cell be split into two cells
343 * can the cell be split into two cells
344 * @method is_splittable
344 * @method is_splittable
345 **/
345 **/
346 Cell.prototype.is_splittable = function () {
346 Cell.prototype.is_splittable = function () {
347 return true;
347 return true;
348 };
348 };
349
349
350
350
351 /**
351 /**
352 * can the cell be merged with other cells
352 * can the cell be merged with other cells
353 * @method is_mergeable
353 * @method is_mergeable
354 **/
354 **/
355 Cell.prototype.is_mergeable = function () {
355 Cell.prototype.is_mergeable = function () {
356 return true;
356 return true;
357 };
357 };
358
358
359
359
360 /**
360 /**
361 * @return {String} - the text before the cursor
361 * @return {String} - the text before the cursor
362 * @method get_pre_cursor
362 * @method get_pre_cursor
363 **/
363 **/
364 Cell.prototype.get_pre_cursor = function () {
364 Cell.prototype.get_pre_cursor = function () {
365 var cursor = this.code_mirror.getCursor();
365 var cursor = this.code_mirror.getCursor();
366 var text = this.code_mirror.getRange({line:0, ch:0}, cursor);
366 var text = this.code_mirror.getRange({line:0, ch:0}, cursor);
367 text = text.replace(/^\n+/, '').replace(/\n+$/, '');
367 text = text.replace(/^\n+/, '').replace(/\n+$/, '');
368 return text;
368 return text;
369 };
369 };
370
370
371
371
372 /**
372 /**
373 * @return {String} - the text after the cursor
373 * @return {String} - the text after the cursor
374 * @method get_post_cursor
374 * @method get_post_cursor
375 **/
375 **/
376 Cell.prototype.get_post_cursor = function () {
376 Cell.prototype.get_post_cursor = function () {
377 var cursor = this.code_mirror.getCursor();
377 var cursor = this.code_mirror.getCursor();
378 var last_line_num = this.code_mirror.lineCount()-1;
378 var last_line_num = this.code_mirror.lineCount()-1;
379 var last_line_len = this.code_mirror.getLine(last_line_num).length;
379 var last_line_len = this.code_mirror.getLine(last_line_num).length;
380 var end = {line:last_line_num, ch:last_line_len};
380 var end = {line:last_line_num, ch:last_line_len};
381 var text = this.code_mirror.getRange(cursor, end);
381 var text = this.code_mirror.getRange(cursor, end);
382 text = text.replace(/^\n+/, '').replace(/\n+$/, '');
382 text = text.replace(/^\n+/, '').replace(/\n+$/, '');
383 return text;
383 return text;
384 };
384 };
385
385
386 /**
386 /**
387 * Show/Hide CodeMirror LineNumber
387 * Show/Hide CodeMirror LineNumber
388 * @method show_line_numbers
388 * @method show_line_numbers
389 *
389 *
390 * @param value {Bool} show (true), or hide (false) the line number in CodeMirror
390 * @param value {Bool} show (true), or hide (false) the line number in CodeMirror
391 **/
391 **/
392 Cell.prototype.show_line_numbers = function (value) {
392 Cell.prototype.show_line_numbers = function (value) {
393 this.code_mirror.setOption('lineNumbers', value);
393 this.code_mirror.setOption('lineNumbers', value);
394 this.code_mirror.refresh();
394 this.code_mirror.refresh();
395 };
395 };
396
396
397 /**
397 /**
398 * Toggle CodeMirror LineNumber
398 * Toggle CodeMirror LineNumber
399 * @method toggle_line_numbers
399 * @method toggle_line_numbers
400 **/
400 **/
401 Cell.prototype.toggle_line_numbers = function () {
401 Cell.prototype.toggle_line_numbers = function () {
402 var val = this.code_mirror.getOption('lineNumbers');
402 var val = this.code_mirror.getOption('lineNumbers');
403 this.show_line_numbers(!val);
403 this.show_line_numbers(!val);
404 };
404 };
405
405
406 /**
406 /**
407 * Force codemirror highlight mode
407 * Force codemirror highlight mode
408 * @method force_highlight
408 * @method force_highlight
409 * @param {object} - CodeMirror mode
409 * @param {object} - CodeMirror mode
410 **/
410 **/
411 Cell.prototype.force_highlight = function(mode) {
411 Cell.prototype.force_highlight = function(mode) {
412 this.user_highlight = mode;
412 this.user_highlight = mode;
413 this.auto_highlight();
413 this.auto_highlight();
414 };
414 };
415
415
416 /**
416 /**
417 * Try to autodetect cell highlight mode, or use selected mode
417 * Try to autodetect cell highlight mode, or use selected mode
418 * @methods _auto_highlight
418 * @methods _auto_highlight
419 * @private
419 * @private
420 * @param {String|object|undefined} - CodeMirror mode | 'auto'
420 * @param {String|object|undefined} - CodeMirror mode | 'auto'
421 **/
421 **/
422 Cell.prototype._auto_highlight = function (modes) {
422 Cell.prototype._auto_highlight = function (modes) {
423 //Here we handle manually selected modes
423 //Here we handle manually selected modes
424 var mode;
424 var mode;
425 if( this.user_highlight !== undefined && this.user_highlight != 'auto' )
425 if( this.user_highlight !== undefined && this.user_highlight != 'auto' )
426 {
426 {
427 mode = this.user_highlight;
427 mode = this.user_highlight;
428 CodeMirror.autoLoadMode(this.code_mirror, mode);
428 CodeMirror.autoLoadMode(this.code_mirror, mode);
429 this.code_mirror.setOption('mode', mode);
429 this.code_mirror.setOption('mode', mode);
430 return;
430 return;
431 }
431 }
432 var current_mode = this.code_mirror.getOption('mode', mode);
432 var current_mode = this.code_mirror.getOption('mode', mode);
433 var first_line = this.code_mirror.getLine(0);
433 var first_line = this.code_mirror.getLine(0);
434 // loop on every pairs
434 // loop on every pairs
435 for(mode in modes) {
435 for(mode in modes) {
436 var regs = modes[mode].reg;
436 var regs = modes[mode].reg;
437 // only one key every time but regexp can't be keys...
437 // only one key every time but regexp can't be keys...
438 for(var i=0; i<regs.length; i++) {
438 for(var i=0; i<regs.length; i++) {
439 // here we handle non magic_modes
439 // here we handle non magic_modes
440 if(first_line.match(regs[i]) !== null) {
440 if(first_line.match(regs[i]) !== null) {
441 if(current_mode == mode){
441 if(current_mode == mode){
442 return;
442 return;
443 }
443 }
444 if (mode.search('magic_') !== 0) {
444 if (mode.search('magic_') !== 0) {
445 this.code_mirror.setOption('mode', mode);
445 this.code_mirror.setOption('mode', mode);
446 CodeMirror.autoLoadMode(this.code_mirror, mode);
446 CodeMirror.autoLoadMode(this.code_mirror, mode);
447 return;
447 return;
448 }
448 }
449 var open = modes[mode].open || "%%";
449 var open = modes[mode].open || "%%";
450 var close = modes[mode].close || "%%end";
450 var close = modes[mode].close || "%%end";
451 var mmode = mode;
451 var mmode = mode;
452 mode = mmode.substr(6);
452 mode = mmode.substr(6);
453 if(current_mode == mode){
453 if(current_mode == mode){
454 return;
454 return;
455 }
455 }
456 CodeMirror.autoLoadMode(this.code_mirror, mode);
456 CodeMirror.autoLoadMode(this.code_mirror, mode);
457 // create on the fly a mode that swhitch between
457 // create on the fly a mode that swhitch between
458 // plain/text and smth else otherwise `%%` is
458 // plain/text and smth else otherwise `%%` is
459 // source of some highlight issues.
459 // source of some highlight issues.
460 // we use patchedGetMode to circumvent a bug in CM
460 // we use patchedGetMode to circumvent a bug in CM
461 CodeMirror.defineMode(mmode , function(config) {
461 CodeMirror.defineMode(mmode , function(config) {
462 return CodeMirror.multiplexingMode(
462 return CodeMirror.multiplexingMode(
463 CodeMirror.patchedGetMode(config, 'text/plain'),
463 CodeMirror.patchedGetMode(config, 'text/plain'),
464 // always set someting on close
464 // always set someting on close
465 {open: open, close: close,
465 {open: open, close: close,
466 mode: CodeMirror.patchedGetMode(config, mode),
466 mode: CodeMirror.patchedGetMode(config, mode),
467 delimStyle: "delimit"
467 delimStyle: "delimit"
468 }
468 }
469 );
469 );
470 });
470 });
471 this.code_mirror.setOption('mode', mmode);
471 this.code_mirror.setOption('mode', mmode);
472 return;
472 return;
473 }
473 }
474 }
474 }
475 }
475 }
476 // fallback on default
476 // fallback on default
477 var default_mode;
477 var default_mode;
478 try {
478 try {
479 default_mode = this._options.cm_config.mode;
479 default_mode = this._options.cm_config.mode;
480 } catch(e) {
480 } catch(e) {
481 default_mode = 'text/plain';
481 default_mode = 'text/plain';
482 }
482 }
483 if( current_mode === default_mode){
483 if( current_mode === default_mode){
484 return;
484 return;
485 }
485 }
486 this.code_mirror.setOption('mode', default_mode);
486 this.code_mirror.setOption('mode', default_mode);
487 };
487 };
488
488
489 IPython.Cell = Cell;
489 IPython.Cell = Cell;
490
490
491 return IPython;
491 return IPython;
492
492
493 }(IPython));
493 }(IPython));
494
494
@@ -1,577 +1,578 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 autoCloseBrackets: true
105 autoCloseBrackets: true
106 }
106 }
107 };
107 };
108
108
109 CodeCell.msg_cells = {};
109 CodeCell.msg_cells = {};
110
110
111 CodeCell.prototype = new IPython.Cell();
111 CodeCell.prototype = new IPython.Cell();
112
112
113 /**
113 /**
114 * @method auto_highlight
114 * @method auto_highlight
115 */
115 */
116 CodeCell.prototype.auto_highlight = function () {
116 CodeCell.prototype.auto_highlight = function () {
117 this._auto_highlight(IPython.config.cell_magic_highlight);
117 this._auto_highlight(IPython.config.cell_magic_highlight);
118 };
118 };
119
119
120 /** @method create_element */
120 /** @method create_element */
121 CodeCell.prototype.create_element = function () {
121 CodeCell.prototype.create_element = function () {
122 IPython.Cell.prototype.create_element.apply(this, arguments);
122 IPython.Cell.prototype.create_element.apply(this, arguments);
123
123
124 var cell = $('<div></div>').addClass('cell border-box-sizing code_cell');
124 var cell = $('<div></div>').addClass('cell border-box-sizing code_cell');
125 cell.attr('tabindex','2');
125 cell.attr('tabindex','2');
126
126
127 var input = $('<div></div>').addClass('input');
127 var input = $('<div></div>').addClass('input');
128 var prompt = $('<div/>').addClass('prompt input_prompt');
128 var prompt = $('<div/>').addClass('prompt input_prompt');
129 var inner_cell = $('<div/>').addClass('inner_cell');
129 var inner_cell = $('<div/>').addClass('inner_cell');
130 this.celltoolbar = new IPython.CellToolbar(this);
130 this.celltoolbar = new IPython.CellToolbar(this);
131 inner_cell.append(this.celltoolbar.element);
131 inner_cell.append(this.celltoolbar.element);
132 var input_area = $('<div/>').addClass('input_area');
132 var input_area = $('<div/>').addClass('input_area');
133 this.code_mirror = CodeMirror(input_area.get(0), this.cm_config);
133 this.code_mirror = CodeMirror(input_area.get(0), this.cm_config);
134 $(this.code_mirror.getInputField()).attr("spellcheck", "false");
134 $(this.code_mirror.getInputField()).attr("spellcheck", "false");
135 inner_cell.append(input_area);
135 inner_cell.append(input_area);
136 input.append(prompt).append(inner_cell);
136 input.append(prompt).append(inner_cell);
137
137
138 var widget_area = $('<div/>')
138 var widget_area = $('<div/>')
139 .addClass('widget-area')
139 .addClass('widget-area')
140 .hide();
140 .hide();
141 this.widget_area = widget_area;
141 this.widget_area = widget_area;
142 var widget_prompt = $('<div/>')
142 var widget_prompt = $('<div/>')
143 .addClass('prompt')
143 .addClass('prompt')
144 .appendTo(widget_area);
144 .appendTo(widget_area);
145 var widget_subarea = $('<div/>')
145 var widget_subarea = $('<div/>')
146 .addClass('widget-subarea')
146 .addClass('widget-subarea')
147 .appendTo(widget_area);
147 .appendTo(widget_area);
148 this.widget_subarea = widget_subarea;
148 this.widget_subarea = widget_subarea;
149 var widget_clear_buton = $('<button />')
149 var widget_clear_buton = $('<button />')
150 .addClass('close')
150 .addClass('close')
151 .html('&times;')
151 .html('&times;')
152 .click(function() {
152 .click(function() {
153 widget_area.slideUp('', function(){ widget_subarea.html(''); });
153 widget_area.slideUp('', function(){ widget_subarea.html(''); });
154 })
154 })
155 .appendTo(widget_prompt);
155 .appendTo(widget_prompt);
156
156
157 var output = $('<div></div>');
157 var output = $('<div></div>');
158 cell.append(input).append(widget_area).append(output);
158 cell.append(input).append(widget_area).append(output);
159 this.element = cell;
159 this.element = cell;
160 this.output_area = new IPython.OutputArea(output, true);
160 this.output_area = new IPython.OutputArea(output, true);
161 this.completer = new IPython.Completer(this);
161 this.completer = new IPython.Completer(this);
162 };
162 };
163
163
164 /** @method bind_events */
164 /** @method bind_events */
165 CodeCell.prototype.bind_events = function () {
165 CodeCell.prototype.bind_events = function () {
166 IPython.Cell.prototype.bind_events.apply(this);
166 IPython.Cell.prototype.bind_events.apply(this);
167 var that = this;
167 var that = this;
168
168
169 this.element.focusout(
169 this.element.focusout(
170 function() { that.auto_highlight(); }
170 function() { that.auto_highlight(); }
171 );
171 );
172 };
172 };
173
173
174 CodeCell.prototype.handle_keyevent = function (editor, event) {
174 CodeCell.prototype.handle_keyevent = function (editor, event) {
175
175
176 // console.log('CM', this.mode, event.which, event.type)
176 // console.log('CM', this.mode, event.which, event.type)
177
177
178 if (this.mode === 'command') {
178 if (this.mode === 'command') {
179 return true;
179 return true;
180 } else if (this.mode === 'edit') {
180 } else if (this.mode === 'edit') {
181 return this.handle_codemirror_keyevent(editor, event);
181 return this.handle_codemirror_keyevent(editor, event);
182 }
182 }
183 };
183 };
184
184
185 /**
185 /**
186 * This method gets called in CodeMirror's onKeyDown/onKeyPress
186 * This method gets called in CodeMirror's onKeyDown/onKeyPress
187 * handlers and is used to provide custom key handling. Its return
187 * handlers and is used to provide custom key handling. Its return
188 * value is used to determine if CodeMirror should ignore the event:
188 * value is used to determine if CodeMirror should ignore the event:
189 * true = ignore, false = don't ignore.
189 * true = ignore, false = don't ignore.
190 * @method handle_codemirror_keyevent
190 * @method handle_codemirror_keyevent
191 */
191 */
192 CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) {
192 CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) {
193
193
194 var that = this;
194 var that = this;
195 // whatever key is pressed, first, cancel the tooltip request before
195 // whatever key is pressed, first, cancel the tooltip request before
196 // they are sent, and remove tooltip if any, except for tab again
196 // they are sent, and remove tooltip if any, except for tab again
197 var tooltip_closed = null;
197 var tooltip_closed = null;
198 if (event.type === 'keydown' && event.which != key.TAB ) {
198 if (event.type === 'keydown' && event.which != key.TAB ) {
199 tooltip_closed = IPython.tooltip.remove_and_cancel_tooltip();
199 tooltip_closed = IPython.tooltip.remove_and_cancel_tooltip();
200 }
200 }
201
201
202 var cur = editor.getCursor();
202 var cur = editor.getCursor();
203 if (event.keyCode === key.ENTER){
203 if (event.keyCode === key.ENTER){
204 this.auto_highlight();
204 this.auto_highlight();
205 }
205 }
206
206
207 if (event.keyCode === key.ENTER && (event.shiftKey || event.ctrlKey || event.altKey)) {
207 if (event.keyCode === key.ENTER && (event.shiftKey || event.ctrlKey || event.altKey)) {
208 // Always ignore shift-enter in CodeMirror as we handle it.
208 // Always ignore shift-enter in CodeMirror as we handle it.
209 return true;
209 return true;
210 } else if (event.which === 40 && event.type === 'keypress' && IPython.tooltip.time_before_tooltip >= 0) {
210 } else if (event.which === 40 && event.type === 'keypress' && IPython.tooltip.time_before_tooltip >= 0) {
211 // triger on keypress (!) otherwise inconsistent event.which depending on plateform
211 // triger on keypress (!) otherwise inconsistent event.which depending on plateform
212 // browser and keyboard layout !
212 // browser and keyboard layout !
213 // Pressing '(' , request tooltip, don't forget to reappend it
213 // Pressing '(' , request tooltip, don't forget to reappend it
214 // The second argument says to hide the tooltip if the docstring
214 // The second argument says to hide the tooltip if the docstring
215 // is actually empty
215 // is actually empty
216 IPython.tooltip.pending(that, true);
216 IPython.tooltip.pending(that, true);
217 } else if (event.which === key.UPARROW && event.type === 'keydown') {
217 } else if (event.which === key.UPARROW && event.type === 'keydown') {
218 // If we are not at the top, let CM handle the up arrow and
218 // If we are not at the top, let CM handle the up arrow and
219 // prevent the global keydown handler from handling it.
219 // prevent the global keydown handler from handling it.
220 if (!that.at_top()) {
220 if (!that.at_top()) {
221 event.stop();
221 event.stop();
222 return false;
222 return false;
223 } else {
223 } else {
224 return true;
224 return true;
225 }
225 }
226 } else if (event.which === key.ESC && event.type === 'keydown') {
226 } else if (event.which === key.ESC && event.type === 'keydown') {
227 // First see if the tooltip is active and if so cancel it.
227 // First see if the tooltip is active and if so cancel it.
228 if (tooltip_closed) {
228 if (tooltip_closed) {
229 // The call to remove_and_cancel_tooltip above in L177 doesn't pass
229 // The call to remove_and_cancel_tooltip above in L177 doesn't pass
230 // force=true. Because of this it won't actually close the tooltip
230 // force=true. Because of this it won't actually close the tooltip
231 // if it is in sticky mode. Thus, we have to check again if it is open
231 // if it is in sticky mode. Thus, we have to check again if it is open
232 // and close it with force=true.
232 // and close it with force=true.
233 if (!IPython.tooltip._hidden) {
233 if (!IPython.tooltip._hidden) {
234 IPython.tooltip.remove_and_cancel_tooltip(true);
234 IPython.tooltip.remove_and_cancel_tooltip(true);
235 }
235 }
236 // If we closed the tooltip, don't let CM or the global handlers
236 // If we closed the tooltip, don't let CM or the global handlers
237 // handle this event.
237 // handle this event.
238 event.stop();
238 event.stop();
239 return true;
239 return true;
240 }
240 }
241 if (that.code_mirror.options.keyMap === "vim-insert") {
241 if (that.code_mirror.options.keyMap === "vim-insert") {
242 // vim keyMap is active and in insert mode. In this case we leave vim
242 // vim keyMap is active and in insert mode. In this case we leave vim
243 // insert mode, but remain in notebook edit mode.
243 // insert mode, but remain in notebook edit mode.
244 // Let' CM handle this event and prevent global handling.
244 // Let' CM handle this event and prevent global handling.
245 event.stop();
245 event.stop();
246 return false;
246 return false;
247 } else {
247 } else {
248 // vim keyMap is not active. Leave notebook edit mode.
248 // vim keyMap is not active. Leave notebook edit mode.
249 // Don't let CM handle the event, defer to global handling.
249 // Don't let CM handle the event, defer to global handling.
250 return true;
250 return true;
251 }
251 }
252 } else if (event.which === key.DOWNARROW && event.type === 'keydown') {
252 } else if (event.which === key.DOWNARROW && event.type === 'keydown') {
253 // If we are not at the bottom, let CM handle the down arrow and
253 // If we are not at the bottom, let CM handle the down arrow and
254 // prevent the global keydown handler from handling it.
254 // prevent the global keydown handler from handling it.
255 if (!that.at_bottom()) {
255 if (!that.at_bottom()) {
256 event.stop();
256 event.stop();
257 return false;
257 return false;
258 } else {
258 } else {
259 return true;
259 return true;
260 }
260 }
261 } else if (event.keyCode === key.TAB && event.type === 'keydown' && event.shiftKey) {
261 } else if (event.keyCode === key.TAB && event.type === 'keydown' && event.shiftKey) {
262 if (editor.somethingSelected()){
262 if (editor.somethingSelected()){
263 var anchor = editor.getCursor("anchor");
263 var anchor = editor.getCursor("anchor");
264 var head = editor.getCursor("head");
264 var head = editor.getCursor("head");
265 if( anchor.line != head.line){
265 if( anchor.line != head.line){
266 return false;
266 return false;
267 }
267 }
268 }
268 }
269 IPython.tooltip.request(that);
269 IPython.tooltip.request(that);
270 event.stop();
270 event.stop();
271 return true;
271 return true;
272 } else if (event.keyCode === key.TAB && event.type == 'keydown') {
272 } else if (event.keyCode === key.TAB && event.type == 'keydown') {
273 // Tab completion.
273 // Tab completion.
274 IPython.tooltip.remove_and_cancel_tooltip();
274 IPython.tooltip.remove_and_cancel_tooltip();
275 if (editor.somethingSelected()) {
275 if (editor.somethingSelected()) {
276 return false;
276 return false;
277 }
277 }
278 var pre_cursor = editor.getRange({line:cur.line,ch:0},cur);
278 var pre_cursor = editor.getRange({line:cur.line,ch:0},cur);
279 if (pre_cursor.trim() === "") {
279 if (pre_cursor.trim() === "") {
280 // Don't autocomplete if the part of the line before the cursor
280 // Don't autocomplete if the part of the line before the cursor
281 // is empty. In this case, let CodeMirror handle indentation.
281 // is empty. In this case, let CodeMirror handle indentation.
282 return false;
282 return false;
283 } else {
283 } else {
284 event.stop();
284 event.stop();
285 this.completer.startCompletion();
285 this.completer.startCompletion();
286 return true;
286 return true;
287 }
287 }
288 } else {
288 } else {
289 // keypress/keyup also trigger on TAB press, and we don't want to
289 // keypress/keyup also trigger on TAB press, and we don't want to
290 // use those to disable tab completion.
290 // use those to disable tab completion.
291 return false;
291 return false;
292 }
292 }
293 return false;
293 return false;
294 };
294 };
295
295
296 // Kernel related calls.
296 // Kernel related calls.
297
297
298 CodeCell.prototype.set_kernel = function (kernel) {
298 CodeCell.prototype.set_kernel = function (kernel) {
299 this.kernel = kernel;
299 this.kernel = kernel;
300 };
300 };
301
301
302 /**
302 /**
303 * Execute current code cell to the kernel
303 * Execute current code cell to the kernel
304 * @method execute
304 * @method execute
305 */
305 */
306 CodeCell.prototype.execute = function () {
306 CodeCell.prototype.execute = function () {
307 this.output_area.clear_output();
307 this.output_area.clear_output();
308
308
309 // Clear widget area
309 // Clear widget area
310 this.widget_subarea.html('');
310 this.widget_subarea.html('');
311 this.widget_subarea.height('');
311 this.widget_subarea.height('');
312 this.widget_area.height('');
312 this.widget_area.height('');
313 this.widget_area.hide();
313 this.widget_area.hide();
314
314
315 this.set_input_prompt('*');
315 this.set_input_prompt('*');
316 this.element.addClass("running");
316 this.element.addClass("running");
317 if (this.last_msg_id) {
317 if (this.last_msg_id) {
318 this.kernel.clear_callbacks_for_msg(this.last_msg_id);
318 this.kernel.clear_callbacks_for_msg(this.last_msg_id);
319 }
319 }
320 var callbacks = this.get_callbacks();
320 var callbacks = this.get_callbacks();
321
321
322 var old_msg_id = this.last_msg_id;
322 var old_msg_id = this.last_msg_id;
323 this.last_msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false, store_history: true});
323 this.last_msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false, store_history: true});
324 if (old_msg_id) {
324 if (old_msg_id) {
325 delete CodeCell.msg_cells[old_msg_id];
325 delete CodeCell.msg_cells[old_msg_id];
326 }
326 }
327 CodeCell.msg_cells[this.last_msg_id] = this;
327 CodeCell.msg_cells[this.last_msg_id] = this;
328 };
328 };
329
329
330 /**
330 /**
331 * Construct the default callbacks for
331 * Construct the default callbacks for
332 * @method get_callbacks
332 * @method get_callbacks
333 */
333 */
334 CodeCell.prototype.get_callbacks = function () {
334 CodeCell.prototype.get_callbacks = function () {
335 return {
335 return {
336 shell : {
336 shell : {
337 reply : $.proxy(this._handle_execute_reply, this),
337 reply : $.proxy(this._handle_execute_reply, this),
338 payload : {
338 payload : {
339 set_next_input : $.proxy(this._handle_set_next_input, this),
339 set_next_input : $.proxy(this._handle_set_next_input, this),
340 page : $.proxy(this._open_with_pager, this)
340 page : $.proxy(this._open_with_pager, this)
341 }
341 }
342 },
342 },
343 iopub : {
343 iopub : {
344 output : $.proxy(this.output_area.handle_output, this.output_area),
344 output : $.proxy(this.output_area.handle_output, this.output_area),
345 clear_output : $.proxy(this.output_area.handle_clear_output, this.output_area),
345 clear_output : $.proxy(this.output_area.handle_clear_output, this.output_area),
346 },
346 },
347 input : $.proxy(this._handle_input_request, this)
347 input : $.proxy(this._handle_input_request, this)
348 };
348 };
349 };
349 };
350
350
351 CodeCell.prototype._open_with_pager = function (payload) {
351 CodeCell.prototype._open_with_pager = function (payload) {
352 $([IPython.events]).trigger('open_with_text.Pager', payload);
352 $([IPython.events]).trigger('open_with_text.Pager', payload);
353 };
353 };
354
354
355 /**
355 /**
356 * @method _handle_execute_reply
356 * @method _handle_execute_reply
357 * @private
357 * @private
358 */
358 */
359 CodeCell.prototype._handle_execute_reply = function (msg) {
359 CodeCell.prototype._handle_execute_reply = function (msg) {
360 this.set_input_prompt(msg.content.execution_count);
360 this.set_input_prompt(msg.content.execution_count);
361 this.element.removeClass("running");
361 this.element.removeClass("running");
362 $([IPython.events]).trigger('set_dirty.Notebook', {value: true});
362 $([IPython.events]).trigger('set_dirty.Notebook', {value: true});
363 };
363 };
364
364
365 /**
365 /**
366 * @method _handle_set_next_input
366 * @method _handle_set_next_input
367 * @private
367 * @private
368 */
368 */
369 CodeCell.prototype._handle_set_next_input = function (payload) {
369 CodeCell.prototype._handle_set_next_input = function (payload) {
370 var data = {'cell': this, 'text': payload.text};
370 var data = {'cell': this, 'text': payload.text};
371 $([IPython.events]).trigger('set_next_input.Notebook', data);
371 $([IPython.events]).trigger('set_next_input.Notebook', data);
372 };
372 };
373
373
374 /**
374 /**
375 * @method _handle_input_request
375 * @method _handle_input_request
376 * @private
376 * @private
377 */
377 */
378 CodeCell.prototype._handle_input_request = function (msg) {
378 CodeCell.prototype._handle_input_request = function (msg) {
379 this.output_area.append_raw_input(msg);
379 this.output_area.append_raw_input(msg);
380 };
380 };
381
381
382
382
383 // Basic cell manipulation.
383 // Basic cell manipulation.
384
384
385 CodeCell.prototype.select = function () {
385 CodeCell.prototype.select = function () {
386 var cont = IPython.Cell.prototype.select.apply(this);
386 var cont = IPython.Cell.prototype.select.apply(this);
387 if (cont) {
387 if (cont) {
388 this.code_mirror.refresh();
388 this.code_mirror.refresh();
389 this.auto_highlight();
389 this.auto_highlight();
390 }
390 }
391 return cont;
391 return cont;
392 };
392 };
393
393
394 CodeCell.prototype.render = function () {
394 CodeCell.prototype.render = function () {
395 var cont = IPython.Cell.prototype.render.apply(this);
395 var cont = IPython.Cell.prototype.render.apply(this);
396 // Always execute, even if we are already in the rendered state
396 // Always execute, even if we are already in the rendered state
397 return cont;
397 return cont;
398 };
398 };
399
399
400 CodeCell.prototype.unrender = function () {
400 CodeCell.prototype.unrender = function () {
401 // CodeCell is always rendered
401 // CodeCell is always rendered
402 return false;
402 return false;
403 };
403 };
404
404
405 /**
405 /**
406 * Determine whether or not the unfocus event should be aknowledged.
406 * Determine whether or not the unfocus event should be aknowledged.
407 *
407 *
408 * @method should_cancel_blur
408 * @method should_cancel_blur
409 *
409 *
410 * @return results {bool} Whether or not to ignore the cell's blur event.
410 * @return results {bool} Whether or not to ignore the cell's blur event.
411 **/
411 **/
412 CodeCell.prototype.should_cancel_blur = function () {
412 CodeCell.prototype.should_cancel_blur = function () {
413 // Cancel this unfocus event if the base wants to cancel or the cell
413 // Cancel this unfocus event if the base wants to cancel or the cell
414 // completer is open.
414 // completer is open or the tooltip is open.
415 return IPython.Cell.prototype.should_cancel_blur.apply(this) ||
415 return IPython.Cell.prototype.should_cancel_blur.apply(this) ||
416 (this.completer && this.completer.is_visible());
416 (this.completer && this.completer.is_visible()) ||
417 (IPython.tooltip && IPython.tooltip.is_visible());
417 };
418 };
418
419
419 CodeCell.prototype.select_all = function () {
420 CodeCell.prototype.select_all = function () {
420 var start = {line: 0, ch: 0};
421 var start = {line: 0, ch: 0};
421 var nlines = this.code_mirror.lineCount();
422 var nlines = this.code_mirror.lineCount();
422 var last_line = this.code_mirror.getLine(nlines-1);
423 var last_line = this.code_mirror.getLine(nlines-1);
423 var end = {line: nlines-1, ch: last_line.length};
424 var end = {line: nlines-1, ch: last_line.length};
424 this.code_mirror.setSelection(start, end);
425 this.code_mirror.setSelection(start, end);
425 };
426 };
426
427
427
428
428 CodeCell.prototype.collapse_output = function () {
429 CodeCell.prototype.collapse_output = function () {
429 this.collapsed = true;
430 this.collapsed = true;
430 this.output_area.collapse();
431 this.output_area.collapse();
431 };
432 };
432
433
433
434
434 CodeCell.prototype.expand_output = function () {
435 CodeCell.prototype.expand_output = function () {
435 this.collapsed = false;
436 this.collapsed = false;
436 this.output_area.expand();
437 this.output_area.expand();
437 this.output_area.unscroll_area();
438 this.output_area.unscroll_area();
438 };
439 };
439
440
440 CodeCell.prototype.scroll_output = function () {
441 CodeCell.prototype.scroll_output = function () {
441 this.output_area.expand();
442 this.output_area.expand();
442 this.output_area.scroll_if_long();
443 this.output_area.scroll_if_long();
443 };
444 };
444
445
445 CodeCell.prototype.toggle_output = function () {
446 CodeCell.prototype.toggle_output = function () {
446 this.collapsed = Boolean(1 - this.collapsed);
447 this.collapsed = Boolean(1 - this.collapsed);
447 this.output_area.toggle_output();
448 this.output_area.toggle_output();
448 };
449 };
449
450
450 CodeCell.prototype.toggle_output_scroll = function () {
451 CodeCell.prototype.toggle_output_scroll = function () {
451 this.output_area.toggle_scroll();
452 this.output_area.toggle_scroll();
452 };
453 };
453
454
454
455
455 CodeCell.input_prompt_classical = function (prompt_value, lines_number) {
456 CodeCell.input_prompt_classical = function (prompt_value, lines_number) {
456 var ns;
457 var ns;
457 if (prompt_value == undefined) {
458 if (prompt_value == undefined) {
458 ns = "&nbsp;";
459 ns = "&nbsp;";
459 } else {
460 } else {
460 ns = encodeURIComponent(prompt_value);
461 ns = encodeURIComponent(prompt_value);
461 }
462 }
462 return 'In&nbsp;[' + ns + ']:';
463 return 'In&nbsp;[' + ns + ']:';
463 };
464 };
464
465
465 CodeCell.input_prompt_continuation = function (prompt_value, lines_number) {
466 CodeCell.input_prompt_continuation = function (prompt_value, lines_number) {
466 var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)];
467 var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)];
467 for(var i=1; i < lines_number; i++) {
468 for(var i=1; i < lines_number; i++) {
468 html.push(['...:']);
469 html.push(['...:']);
469 }
470 }
470 return html.join('<br/>');
471 return html.join('<br/>');
471 };
472 };
472
473
473 CodeCell.input_prompt_function = CodeCell.input_prompt_classical;
474 CodeCell.input_prompt_function = CodeCell.input_prompt_classical;
474
475
475
476
476 CodeCell.prototype.set_input_prompt = function (number) {
477 CodeCell.prototype.set_input_prompt = function (number) {
477 var nline = 1;
478 var nline = 1;
478 if (this.code_mirror !== undefined) {
479 if (this.code_mirror !== undefined) {
479 nline = this.code_mirror.lineCount();
480 nline = this.code_mirror.lineCount();
480 }
481 }
481 this.input_prompt_number = number;
482 this.input_prompt_number = number;
482 var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline);
483 var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline);
483 this.element.find('div.input_prompt').html(prompt_html);
484 this.element.find('div.input_prompt').html(prompt_html);
484 };
485 };
485
486
486
487
487 CodeCell.prototype.clear_input = function () {
488 CodeCell.prototype.clear_input = function () {
488 this.code_mirror.setValue('');
489 this.code_mirror.setValue('');
489 };
490 };
490
491
491
492
492 CodeCell.prototype.get_text = function () {
493 CodeCell.prototype.get_text = function () {
493 return this.code_mirror.getValue();
494 return this.code_mirror.getValue();
494 };
495 };
495
496
496
497
497 CodeCell.prototype.set_text = function (code) {
498 CodeCell.prototype.set_text = function (code) {
498 return this.code_mirror.setValue(code);
499 return this.code_mirror.setValue(code);
499 };
500 };
500
501
501
502
502 CodeCell.prototype.at_top = function () {
503 CodeCell.prototype.at_top = function () {
503 var cursor = this.code_mirror.getCursor();
504 var cursor = this.code_mirror.getCursor();
504 if (cursor.line === 0 && cursor.ch === 0) {
505 if (cursor.line === 0 && cursor.ch === 0) {
505 return true;
506 return true;
506 } else {
507 } else {
507 return false;
508 return false;
508 }
509 }
509 };
510 };
510
511
511
512
512 CodeCell.prototype.at_bottom = function () {
513 CodeCell.prototype.at_bottom = function () {
513 var cursor = this.code_mirror.getCursor();
514 var cursor = this.code_mirror.getCursor();
514 if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) {
515 if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) {
515 return true;
516 return true;
516 } else {
517 } else {
517 return false;
518 return false;
518 }
519 }
519 };
520 };
520
521
521
522
522 CodeCell.prototype.clear_output = function (wait) {
523 CodeCell.prototype.clear_output = function (wait) {
523 this.output_area.clear_output(wait);
524 this.output_area.clear_output(wait);
524 this.set_input_prompt();
525 this.set_input_prompt();
525 };
526 };
526
527
527
528
528 // JSON serialization
529 // JSON serialization
529
530
530 CodeCell.prototype.fromJSON = function (data) {
531 CodeCell.prototype.fromJSON = function (data) {
531 IPython.Cell.prototype.fromJSON.apply(this, arguments);
532 IPython.Cell.prototype.fromJSON.apply(this, arguments);
532 if (data.cell_type === 'code') {
533 if (data.cell_type === 'code') {
533 if (data.input !== undefined) {
534 if (data.input !== undefined) {
534 this.set_text(data.input);
535 this.set_text(data.input);
535 // make this value the starting point, so that we can only undo
536 // make this value the starting point, so that we can only undo
536 // to this state, instead of a blank cell
537 // to this state, instead of a blank cell
537 this.code_mirror.clearHistory();
538 this.code_mirror.clearHistory();
538 this.auto_highlight();
539 this.auto_highlight();
539 }
540 }
540 if (data.prompt_number !== undefined) {
541 if (data.prompt_number !== undefined) {
541 this.set_input_prompt(data.prompt_number);
542 this.set_input_prompt(data.prompt_number);
542 } else {
543 } else {
543 this.set_input_prompt();
544 this.set_input_prompt();
544 }
545 }
545 this.output_area.trusted = data.trusted || false;
546 this.output_area.trusted = data.trusted || false;
546 this.output_area.fromJSON(data.outputs);
547 this.output_area.fromJSON(data.outputs);
547 if (data.collapsed !== undefined) {
548 if (data.collapsed !== undefined) {
548 if (data.collapsed) {
549 if (data.collapsed) {
549 this.collapse_output();
550 this.collapse_output();
550 } else {
551 } else {
551 this.expand_output();
552 this.expand_output();
552 }
553 }
553 }
554 }
554 }
555 }
555 };
556 };
556
557
557
558
558 CodeCell.prototype.toJSON = function () {
559 CodeCell.prototype.toJSON = function () {
559 var data = IPython.Cell.prototype.toJSON.apply(this);
560 var data = IPython.Cell.prototype.toJSON.apply(this);
560 data.input = this.get_text();
561 data.input = this.get_text();
561 // is finite protect against undefined and '*' value
562 // is finite protect against undefined and '*' value
562 if (isFinite(this.input_prompt_number)) {
563 if (isFinite(this.input_prompt_number)) {
563 data.prompt_number = this.input_prompt_number;
564 data.prompt_number = this.input_prompt_number;
564 }
565 }
565 var outputs = this.output_area.toJSON();
566 var outputs = this.output_area.toJSON();
566 data.outputs = outputs;
567 data.outputs = outputs;
567 data.language = 'python';
568 data.language = 'python';
568 data.trusted = this.output_area.trusted;
569 data.trusted = this.output_area.trusted;
569 data.collapsed = this.collapsed;
570 data.collapsed = this.collapsed;
570 return data;
571 return data;
571 };
572 };
572
573
573
574
574 IPython.CodeCell = CodeCell;
575 IPython.CodeCell = CodeCell;
575
576
576 return IPython;
577 return IPython;
577 }(IPython));
578 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now