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