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