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