##// END OF EJS Templates
Address @carreau 's comments
Jonathan Frederic -
Show More
@@ -1,573 +1,569
1 1 // Copyright (c) IPython Development Team.
2 2 // Distributed under the terms of the Modified BSD License.
3 3 /**
4 4 *
5 5 *
6 6 * @module codecell
7 7 * @namespace codecell
8 8 * @class CodeCell
9 9 */
10 10
11 11
12 12 define([
13 13 'base/js/namespace',
14 14 'jquery',
15 15 'base/js/utils',
16 16 'base/js/keyboard',
17 17 'notebook/js/cell',
18 18 'notebook/js/outputarea',
19 19 'notebook/js/completer',
20 20 'notebook/js/celltoolbar',
21 21 'codemirror/lib/codemirror',
22 22 'codemirror/mode/python/python',
23 23 'notebook/js/codemirror-ipython'
24 24 ], function(IPython, $, utils, keyboard, cell, outputarea, completer, celltoolbar, CodeMirror, cmpython, cmip) {
25 25 "use strict";
26 26
27 27 var Cell = cell.Cell;
28 28
29 29 /* local util for codemirror */
30 30 var posEq = function(a, b) {return a.line == b.line && a.ch == b.ch;};
31 31
32 32 /**
33 33 *
34 34 * function to delete until previous non blanking space character
35 35 * or first multiple of 4 tabstop.
36 36 * @private
37 37 */
38 38 CodeMirror.commands.delSpaceToPrevTabStop = function(cm){
39 39 var from = cm.getCursor(true), to = cm.getCursor(false), sel = !posEq(from, to);
40 40 if (!posEq(from, to)) { cm.replaceRange("", from, to); return; }
41 41 var cur = cm.getCursor(), line = cm.getLine(cur.line);
42 42 var tabsize = cm.getOption('tabSize');
43 43 var chToPrevTabStop = cur.ch-(Math.ceil(cur.ch/tabsize)-1)*tabsize;
44 44 from = {ch:cur.ch-chToPrevTabStop,line:cur.line};
45 45 var select = cm.getRange(from,cur);
46 46 if( select.match(/^\ +$/) !== null){
47 47 cm.replaceRange("",from,cur);
48 48 } else {
49 49 cm.deleteH(-1,"char");
50 50 }
51 51 };
52 52
53 53 var keycodes = keyboard.keycodes;
54 54
55 55 var CodeCell = function (kernel, options) {
56 56 // Constructor
57 57 //
58 58 // A Cell conceived to write code.
59 59 //
60 60 // Parameters:
61 61 // kernel: Kernel instance
62 62 // The kernel doesn't have to be set at creation time, in that case
63 63 // it will be null and set_kernel has to be called later.
64 64 // options: dictionary
65 65 // Dictionary of keyword arguments.
66 66 // events: $(Events) instance
67 67 // config: dictionary
68 68 // keyboard_manager: KeyboardManager instance
69 69 // notebook: Notebook instance
70 70 // tooltip: Tooltip instance
71 71 this.kernel = kernel || null;
72 72 this.notebook = options.notebook;
73 73 this.collapsed = false;
74 74 this.events = options.events;
75 75 this.tooltip = options.tooltip;
76 76 this.config = options.config;
77 77
78 78 // create all attributed in constructor function
79 79 // even if null for V8 VM optimisation
80 80 this.input_prompt_number = null;
81 81 this.celltoolbar = null;
82 82 this.output_area = null;
83 83 // Keep a stack of the 'active' output areas (where active means the
84 84 // output area that recieves output). When a user activates an output
85 85 // area, it gets pushed to the stack. Then, when the output area is
86 86 // deactivated, it's popped from the stack. When the stack is empty,
87 87 // the cell's output area is used.
88 88 this.active_output_areas = [];
89 var that = this;
90 Object.defineProperty(this, 'active_output_area', {
91 get: function() {
92 if (that.active_output_areas && that.active_output_areas.length > 0) {
93 return that.active_output_areas[that.active_output_areas.length-1];
94 } else {
95 return that.output_area;
96 }
97 },
98 });
99
89 100 this.last_msg_id = null;
90 101 this.completer = null;
91 102
92 103
93 104 var config = utils.mergeopt(CodeCell, this.config);
94 105 Cell.apply(this,[{
95 106 config: config,
96 107 keyboard_manager: options.keyboard_manager,
97 108 events: this.events}]);
98 109
99 110 // Attributes we want to override in this subclass.
100 111 this.cell_type = "code";
101
102 var that = this;
103 112 this.element.focusout(
104 113 function() { that.auto_highlight(); }
105 114 );
106 115 };
107 116
108 117 CodeCell.options_default = {
109 118 cm_config : {
110 119 extraKeys: {
111 120 "Tab" : "indentMore",
112 121 "Shift-Tab" : "indentLess",
113 122 "Backspace" : "delSpaceToPrevTabStop",
114 123 "Cmd-/" : "toggleComment",
115 124 "Ctrl-/" : "toggleComment"
116 125 },
117 126 mode: 'ipython',
118 127 theme: 'ipython',
119 128 matchBrackets: true
120 129 }
121 130 };
122 131
123 132 CodeCell.msg_cells = {};
124 133
125 134 CodeCell.prototype = Object.create(Cell.prototype);
126 135
127 136 /**
128 * @method get_output_area
129 */
130 CodeCell.prototype.get_output_area = function () {
131 if (this.active_output_areas && this.active_output_areas.length > 0) {
132 return this.active_output_areas[this.active_output_areas.length-1];
133 } else {
134 return this.output_area;
135 }
136 };
137
138 /**
139 137 * @method push_output_area
140 138 */
141 139 CodeCell.prototype.push_output_area = function (output_area) {
142 140 this.active_output_areas.push(output_area);
143 141 };
144 142
145 143 /**
146 144 * @method pop_output_area
147 145 */
148 146 CodeCell.prototype.pop_output_area = function (output_area) {
149 147 var index = this.active_output_areas.lastIndexOf(output_area);
150 148 if (index > -1) {
151 149 this.active_output_areas.splice(index, 1);
152 150 }
153 151 };
154 152
155 153 /**
156 154 * @method auto_highlight
157 155 */
158 156 CodeCell.prototype.auto_highlight = function () {
159 157 this._auto_highlight(this.config.cell_magic_highlight);
160 158 };
161 159
162 160 /** @method create_element */
163 161 CodeCell.prototype.create_element = function () {
164 162 Cell.prototype.create_element.apply(this, arguments);
165 163
166 164 var cell = $('<div></div>').addClass('cell code_cell');
167 165 cell.attr('tabindex','2');
168 166
169 167 var input = $('<div></div>').addClass('input');
170 168 var prompt = $('<div/>').addClass('prompt input_prompt');
171 169 var inner_cell = $('<div/>').addClass('inner_cell');
172 170 this.celltoolbar = new celltoolbar.CellToolbar({
173 171 cell: this,
174 172 notebook: this.notebook});
175 173 inner_cell.append(this.celltoolbar.element);
176 174 var input_area = $('<div/>').addClass('input_area');
177 175 this.code_mirror = new CodeMirror(input_area.get(0), this.cm_config);
178 176 this.code_mirror.on('keydown', $.proxy(this.handle_keyevent,this))
179 177 $(this.code_mirror.getInputField()).attr("spellcheck", "false");
180 178 inner_cell.append(input_area);
181 179 input.append(prompt).append(inner_cell);
182 180
183 181 var widget_area = $('<div/>')
184 182 .addClass('widget-area')
185 183 .hide();
186 184 this.widget_area = widget_area;
187 185 var widget_prompt = $('<div/>')
188 186 .addClass('prompt')
189 187 .appendTo(widget_area);
190 188 var widget_subarea = $('<div/>')
191 189 .addClass('widget-subarea')
192 190 .appendTo(widget_area);
193 191 this.widget_subarea = widget_subarea;
194 192 var widget_clear_buton = $('<button />')
195 193 .addClass('close')
196 194 .html('&times;')
197 195 .click(function() {
198 196 widget_area.slideUp('', function(){ widget_subarea.html(''); });
199 197 })
200 198 .appendTo(widget_prompt);
201 199
202 200 var output = $('<div></div>');
203 201 cell.append(input).append(widget_area).append(output);
204 202 this.element = cell;
205 203 this.output_area = new outputarea.OutputArea({
206 204 selector: output,
207 205 prompt_area: true,
208 206 events: this.events,
209 207 keyboard_manager: this.keyboard_manager});
210 208 this.completer = new completer.Completer(this, this.events);
211 209 };
212 210
213 211 /** @method bind_events */
214 212 CodeCell.prototype.bind_events = function () {
215 213 Cell.prototype.bind_events.apply(this);
216 214 var that = this;
217 215
218 216 this.element.focusout(
219 217 function() { that.auto_highlight(); }
220 218 );
221 219 };
222 220
223 221
224 222 /**
225 223 * This method gets called in CodeMirror's onKeyDown/onKeyPress
226 224 * handlers and is used to provide custom key handling. Its return
227 225 * value is used to determine if CodeMirror should ignore the event:
228 226 * true = ignore, false = don't ignore.
229 227 * @method handle_codemirror_keyevent
230 228 */
231 229 CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) {
232 230
233 231 var that = this;
234 232 // whatever key is pressed, first, cancel the tooltip request before
235 233 // they are sent, and remove tooltip if any, except for tab again
236 234 var tooltip_closed = null;
237 235 if (event.type === 'keydown' && event.which != keycodes.tab ) {
238 236 tooltip_closed = this.tooltip.remove_and_cancel_tooltip();
239 237 }
240 238
241 239 var cur = editor.getCursor();
242 240 if (event.keyCode === keycodes.enter){
243 241 this.auto_highlight();
244 242 }
245 243
246 244 if (event.which === keycodes.down && event.type === 'keypress' && this.tooltip.time_before_tooltip >= 0) {
247 245 // triger on keypress (!) otherwise inconsistent event.which depending on plateform
248 246 // browser and keyboard layout !
249 247 // Pressing '(' , request tooltip, don't forget to reappend it
250 248 // The second argument says to hide the tooltip if the docstring
251 249 // is actually empty
252 250 this.tooltip.pending(that, true);
253 251 } else if ( tooltip_closed && event.which === keycodes.esc && event.type === 'keydown') {
254 252 // If tooltip is active, cancel it. The call to
255 253 // remove_and_cancel_tooltip above doesn't pass, force=true.
256 254 // Because of this it won't actually close the tooltip
257 255 // if it is in sticky mode. Thus, we have to check again if it is open
258 256 // and close it with force=true.
259 257 if (!this.tooltip._hidden) {
260 258 this.tooltip.remove_and_cancel_tooltip(true);
261 259 }
262 260 // If we closed the tooltip, don't let CM or the global handlers
263 261 // handle this event.
264 262 event.codemirrorIgnore = true;
265 263 event.preventDefault();
266 264 return true;
267 265 } else if (event.keyCode === keycodes.tab && event.type === 'keydown' && event.shiftKey) {
268 266 if (editor.somethingSelected() || editor.getSelections().length !== 1){
269 267 var anchor = editor.getCursor("anchor");
270 268 var head = editor.getCursor("head");
271 269 if( anchor.line != head.line){
272 270 return false;
273 271 }
274 272 }
275 273 this.tooltip.request(that);
276 274 event.codemirrorIgnore = true;
277 275 event.preventDefault();
278 276 return true;
279 277 } else if (event.keyCode === keycodes.tab && event.type == 'keydown') {
280 278 // Tab completion.
281 279 this.tooltip.remove_and_cancel_tooltip();
282 280
283 281 // completion does not work on multicursor, it might be possible though in some cases
284 282 if (editor.somethingSelected() || editor.getSelections().length > 1) {
285 283 return false;
286 284 }
287 285 var pre_cursor = editor.getRange({line:cur.line,ch:0},cur);
288 286 if (pre_cursor.trim() === "") {
289 287 // Don't autocomplete if the part of the line before the cursor
290 288 // is empty. In this case, let CodeMirror handle indentation.
291 289 return false;
292 290 } else {
293 291 event.codemirrorIgnore = true;
294 292 event.preventDefault();
295 293 this.completer.startCompletion();
296 294 return true;
297 295 }
298 296 }
299 297
300 298 // keyboard event wasn't one of those unique to code cells, let's see
301 299 // if it's one of the generic ones (i.e. check edit mode shortcuts)
302 300 return Cell.prototype.handle_codemirror_keyevent.apply(this, [editor, event]);
303 301 };
304 302
305 303 // Kernel related calls.
306 304
307 305 CodeCell.prototype.set_kernel = function (kernel) {
308 306 this.kernel = kernel;
309 307 };
310 308
311 309 /**
312 310 * Execute current code cell to the kernel
313 311 * @method execute
314 312 */
315 313 CodeCell.prototype.execute = function () {
316 314 if (!this.kernel || !this.kernel.is_connected()) {
317 315 console.log("Can't execute, kernel is not connected.");
318 316 return;
319 317 }
320 318
321 this.get_output_area().clear_output();
319 this.active_output_area.clear_output();
322 320
323 321 // Clear widget area
324 322 this.widget_subarea.html('');
325 323 this.widget_subarea.height('');
326 324 this.widget_area.height('');
327 325 this.widget_area.hide();
328 326
329 327 this.set_input_prompt('*');
330 328 this.element.addClass("running");
331 329 if (this.last_msg_id) {
332 330 this.kernel.clear_callbacks_for_msg(this.last_msg_id);
333 331 }
334 332 var callbacks = this.get_callbacks();
335 333
336 334 var old_msg_id = this.last_msg_id;
337 335 this.last_msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false, store_history: true});
338 336 if (old_msg_id) {
339 337 delete CodeCell.msg_cells[old_msg_id];
340 338 }
341 339 CodeCell.msg_cells[this.last_msg_id] = this;
342 340 this.render();
343 341 };
344 342
345 343 /**
346 344 * Construct the default callbacks for
347 345 * @method get_callbacks
348 346 */
349 347 CodeCell.prototype.get_callbacks = function () {
350 348 var that = this;
351 349 return {
352 350 shell : {
353 351 reply : $.proxy(this._handle_execute_reply, this),
354 352 payload : {
355 353 set_next_input : $.proxy(this._handle_set_next_input, this),
356 354 page : $.proxy(this._open_with_pager, this)
357 355 }
358 356 },
359 357 iopub : {
360 358 output : function() {
361 var output_area = that.get_output_area();
362 output_area.handle_output.apply(output_area, arguments);
359 that.active_output_area.handle_output.apply(output_area, arguments);
363 360 },
364 361 clear_output : function() {
365 var output_area = that.get_output_area();
366 output_area.handle_clear_output.apply(output_area, arguments);
362 that.active_output_area.handle_clear_output.apply(output_area, arguments);
367 363 },
368 364 },
369 365 input : $.proxy(this._handle_input_request, this)
370 366 };
371 367 };
372 368
373 369 CodeCell.prototype._open_with_pager = function (payload) {
374 370 this.events.trigger('open_with_text.Pager', payload);
375 371 };
376 372
377 373 /**
378 374 * @method _handle_execute_reply
379 375 * @private
380 376 */
381 377 CodeCell.prototype._handle_execute_reply = function (msg) {
382 378 this.set_input_prompt(msg.content.execution_count);
383 379 this.element.removeClass("running");
384 380 this.events.trigger('set_dirty.Notebook', {value: true});
385 381 };
386 382
387 383 /**
388 384 * @method _handle_set_next_input
389 385 * @private
390 386 */
391 387 CodeCell.prototype._handle_set_next_input = function (payload) {
392 388 var data = {'cell': this, 'text': payload.text};
393 389 this.events.trigger('set_next_input.Notebook', data);
394 390 };
395 391
396 392 /**
397 393 * @method _handle_input_request
398 394 * @private
399 395 */
400 396 CodeCell.prototype._handle_input_request = function (msg) {
401 this.get_output_area().append_raw_input(msg);
397 this.active_output_area.append_raw_input(msg);
402 398 };
403 399
404 400
405 401 // Basic cell manipulation.
406 402
407 403 CodeCell.prototype.select = function () {
408 404 var cont = Cell.prototype.select.apply(this);
409 405 if (cont) {
410 406 this.code_mirror.refresh();
411 407 this.auto_highlight();
412 408 }
413 409 return cont;
414 410 };
415 411
416 412 CodeCell.prototype.render = function () {
417 413 var cont = Cell.prototype.render.apply(this);
418 414 // Always execute, even if we are already in the rendered state
419 415 return cont;
420 416 };
421 417
422 418 CodeCell.prototype.select_all = function () {
423 419 var start = {line: 0, ch: 0};
424 420 var nlines = this.code_mirror.lineCount();
425 421 var last_line = this.code_mirror.getLine(nlines-1);
426 422 var end = {line: nlines-1, ch: last_line.length};
427 423 this.code_mirror.setSelection(start, end);
428 424 };
429 425
430 426
431 427 CodeCell.prototype.collapse_output = function () {
432 428 this.output_area.collapse();
433 429 };
434 430
435 431
436 432 CodeCell.prototype.expand_output = function () {
437 433 this.output_area.expand();
438 434 this.output_area.unscroll_area();
439 435 };
440 436
441 437 CodeCell.prototype.scroll_output = function () {
442 438 this.output_area.expand();
443 439 this.output_area.scroll_if_long();
444 440 };
445 441
446 442 CodeCell.prototype.toggle_output = function () {
447 443 this.output_area.toggle_output();
448 444 };
449 445
450 446 CodeCell.prototype.toggle_output_scroll = function () {
451 447 this.output_area.toggle_scroll();
452 448 };
453 449
454 450
455 451 CodeCell.input_prompt_classical = function (prompt_value, lines_number) {
456 452 var ns;
457 453 if (prompt_value === undefined || prompt_value === null) {
458 454 ns = "&nbsp;";
459 455 } else {
460 456 ns = encodeURIComponent(prompt_value);
461 457 }
462 458 return 'In&nbsp;[' + ns + ']:';
463 459 };
464 460
465 461 CodeCell.input_prompt_continuation = function (prompt_value, lines_number) {
466 462 var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)];
467 463 for(var i=1; i < lines_number; i++) {
468 464 html.push(['...:']);
469 465 }
470 466 return html.join('<br/>');
471 467 };
472 468
473 469 CodeCell.input_prompt_function = CodeCell.input_prompt_classical;
474 470
475 471
476 472 CodeCell.prototype.set_input_prompt = function (number) {
477 473 var nline = 1;
478 474 if (this.code_mirror !== undefined) {
479 475 nline = this.code_mirror.lineCount();
480 476 }
481 477 this.input_prompt_number = number;
482 478 var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline);
483 479 // This HTML call is okay because the user contents are escaped.
484 480 this.element.find('div.input_prompt').html(prompt_html);
485 481 };
486 482
487 483
488 484 CodeCell.prototype.clear_input = function () {
489 485 this.code_mirror.setValue('');
490 486 };
491 487
492 488
493 489 CodeCell.prototype.get_text = function () {
494 490 return this.code_mirror.getValue();
495 491 };
496 492
497 493
498 494 CodeCell.prototype.set_text = function (code) {
499 495 return this.code_mirror.setValue(code);
500 496 };
501 497
502 498
503 499 CodeCell.prototype.clear_output = function (wait) {
504 this.get_output_area().clear_output(wait);
500 this.active_output_area.clear_output(wait);
505 501 this.set_input_prompt();
506 502 };
507 503
508 504
509 505 // JSON serialization
510 506
511 507 CodeCell.prototype.fromJSON = function (data) {
512 508 Cell.prototype.fromJSON.apply(this, arguments);
513 509 if (data.cell_type === 'code') {
514 510 if (data.source !== undefined) {
515 511 this.set_text(data.source);
516 512 // make this value the starting point, so that we can only undo
517 513 // to this state, instead of a blank cell
518 514 this.code_mirror.clearHistory();
519 515 this.auto_highlight();
520 516 }
521 517 this.set_input_prompt(data.execution_count);
522 518 this.output_area.trusted = data.metadata.trusted || false;
523 519 this.output_area.fromJSON(data.outputs);
524 520 if (data.metadata.collapsed !== undefined) {
525 521 if (data.metadata.collapsed) {
526 522 this.collapse_output();
527 523 } else {
528 524 this.expand_output();
529 525 }
530 526 }
531 527 }
532 528 };
533 529
534 530
535 531 CodeCell.prototype.toJSON = function () {
536 532 var data = Cell.prototype.toJSON.apply(this);
537 533 data.source = this.get_text();
538 534 // is finite protect against undefined and '*' value
539 535 if (isFinite(this.input_prompt_number)) {
540 536 data.execution_count = this.input_prompt_number;
541 537 } else {
542 538 data.execution_count = null;
543 539 }
544 540 var outputs = this.output_area.toJSON();
545 541 data.outputs = outputs;
546 542 data.metadata.trusted = this.output_area.trusted;
547 543 data.metadata.collapsed = this.output_area.collapsed;
548 544 return data;
549 545 };
550 546
551 547 /**
552 548 * handle cell level logic when a cell is unselected
553 549 * @method unselect
554 550 * @return is the action being taken
555 551 */
556 552 CodeCell.prototype.unselect = function () {
557 553 var cont = Cell.prototype.unselect.apply(this);
558 554 if (cont) {
559 555 // When a code cell is usnelected, make sure that the corresponding
560 556 // tooltip and completer to that cell is closed.
561 557 this.tooltip.remove_and_cancel_tooltip(true);
562 558 if (this.completer !== null) {
563 559 this.completer.close();
564 560 }
565 561 }
566 562 return cont;
567 563 };
568 564
569 565 // Backwards compatability.
570 566 IPython.CodeCell = CodeCell;
571 567
572 568 return {'CodeCell': CodeCell};
573 569 });
@@ -1,56 +1,57
1 1 // Copyright (c) IPython Development Team.
2 2 // Distributed under the terms of the Modified BSD License.
3 3
4 4 define([
5 5 "widgets/js/widget",
6 6 "jquery",
7 7 'notebook/js/outputarea',
8 ], function(widget, $, outputarea){
8 ], function(widget, $, outputarea) {
9 'use strict';
9 10
10 11 var OutputView = widget.DOMWidgetView.extend({
11 12 initialize: function (parameters) {
12 13 // Public constructor
13 14 OutputView.__super__.initialize.apply(this, [parameters]);
14 15 this.model.on('msg:custom', this._handle_route_msg, this);
15 16 },
16 17
17 18 render: function(){
18 19 // Called when view is rendered.
19 20 this.output_area = new outputarea.OutputArea({
20 21 selector: this.$el,
21 22 prompt_area: false,
22 23 events: this.model.widget_manager.notebook.events,
23 24 keyboard_manager: this.model.widget_manager.keyboard_manager });
24 25
25 26 // Make output area reactive.
26 27 var that = this;
27 28 this.output_area.element.on('changed', function() {
28 29 that.model.set('contents', that.output_area.element.html());
29 30 });
30 31 this.model.on('change:contents', function(){
31 32 var html = this.model.get('contents');
32 33 if (this.output_area.element.html() != html) {
33 34 this.output_area.element.html(html);
34 35 }
35 36 }, this);
36 37
37 38 // Set initial contents.
38 39 this.output_area.element.html(this.model.get('contents'));
39 40 },
40 41
41 42 _handle_route_msg: function(content) {
42 43 var cell = this.options.cell;
43 44 if (content && cell) {
44 45 if (content.method == 'push') {
45 46 cell.push_output_area(this.output_area);
46 47 } else if (content.method == 'pop') {
47 48 cell.pop_output_area(this.output_area);
48 49 }
49 50 }
50 51 },
51 52 });
52 53
53 54 return {
54 55 'OutputView': OutputView,
55 56 };
56 57 });
General Comments 0
You need to be logged in to leave comments. Login now