##// END OF EJS Templates
Move append_output animation height lock release into timeout.
jon -
Show More
@@ -1,878 +1,882
1 1 //----------------------------------------------------------------------------
2 2 // Copyright (C) 2008 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 // OutputArea
10 10 //============================================================================
11 11
12 12 /**
13 13 * @module IPython
14 14 * @namespace IPython
15 15 * @submodule OutputArea
16 16 */
17 17 var IPython = (function (IPython) {
18 18 "use strict";
19 19
20 20 var utils = IPython.utils;
21 21
22 22 /**
23 23 * @class OutputArea
24 24 *
25 25 * @constructor
26 26 */
27 27
28 28 var OutputArea = function (selector, prompt_area) {
29 29 this.selector = selector;
30 30 this.wrapper = $(selector);
31 31 this.outputs = [];
32 32 this.collapsed = false;
33 33 this.scrolled = false;
34 34 this.trusted = true;
35 35 this.clear_queued = null;
36 36 if (prompt_area === undefined) {
37 37 this.prompt_area = true;
38 38 } else {
39 39 this.prompt_area = prompt_area;
40 40 }
41 41 this.create_elements();
42 42 this.style();
43 43 this.bind_events();
44 44 };
45 45
46 46
47 47 /**
48 48 * Class prototypes
49 49 **/
50 50
51 51 OutputArea.prototype.create_elements = function () {
52 52 this.element = $("<div/>");
53 53 this.collapse_button = $("<div/>");
54 54 this.prompt_overlay = $("<div/>");
55 55 this.wrapper.append(this.prompt_overlay);
56 56 this.wrapper.append(this.element);
57 57 this.wrapper.append(this.collapse_button);
58 58 };
59 59
60 60
61 61 OutputArea.prototype.style = function () {
62 62 this.collapse_button.hide();
63 63 this.prompt_overlay.hide();
64 64
65 65 this.wrapper.addClass('output_wrapper');
66 66 this.element.addClass('output');
67 67
68 68 this.collapse_button.addClass("btn output_collapsed");
69 69 this.collapse_button.attr('title', 'click to expand output');
70 70 this.collapse_button.text('. . .');
71 71
72 72 this.prompt_overlay.addClass('out_prompt_overlay prompt');
73 73 this.prompt_overlay.attr('title', 'click to expand output; double click to hide output');
74 74
75 75 this.collapse();
76 76 };
77 77
78 78 /**
79 79 * Should the OutputArea scroll?
80 80 * Returns whether the height (in lines) exceeds a threshold.
81 81 *
82 82 * @private
83 83 * @method _should_scroll
84 84 * @param [lines=100]{Integer}
85 85 * @return {Bool}
86 86 *
87 87 */
88 88 OutputArea.prototype._should_scroll = function (lines) {
89 89 if (lines <=0 ){ return }
90 90 if (!lines) {
91 91 lines = 100;
92 92 }
93 93 // line-height from http://stackoverflow.com/questions/1185151
94 94 var fontSize = this.element.css('font-size');
95 95 var lineHeight = Math.floor(parseInt(fontSize.replace('px','')) * 1.5);
96 96
97 97 return (this.element.height() > lines * lineHeight);
98 98 };
99 99
100 100
101 101 OutputArea.prototype.bind_events = function () {
102 102 var that = this;
103 103 this.prompt_overlay.dblclick(function () { that.toggle_output(); });
104 104 this.prompt_overlay.click(function () { that.toggle_scroll(); });
105 105
106 106 this.element.resize(function () {
107 107 // FIXME: Firefox on Linux misbehaves, so automatic scrolling is disabled
108 108 if ( IPython.utils.browser[0] === "Firefox" ) {
109 109 return;
110 110 }
111 111 // maybe scroll output,
112 112 // if it's grown large enough and hasn't already been scrolled.
113 113 if ( !that.scrolled && that._should_scroll(OutputArea.auto_scroll_threshold)) {
114 114 that.scroll_area();
115 115 }
116 116 });
117 117 this.collapse_button.click(function () {
118 118 that.expand();
119 119 });
120 120 };
121 121
122 122
123 123 OutputArea.prototype.collapse = function () {
124 124 if (!this.collapsed) {
125 125 this.element.hide();
126 126 this.prompt_overlay.hide();
127 127 if (this.element.html()){
128 128 this.collapse_button.show();
129 129 }
130 130 this.collapsed = true;
131 131 }
132 132 };
133 133
134 134
135 135 OutputArea.prototype.expand = function () {
136 136 if (this.collapsed) {
137 137 this.collapse_button.hide();
138 138 this.element.show();
139 139 this.prompt_overlay.show();
140 140 this.collapsed = false;
141 141 }
142 142 };
143 143
144 144
145 145 OutputArea.prototype.toggle_output = function () {
146 146 if (this.collapsed) {
147 147 this.expand();
148 148 } else {
149 149 this.collapse();
150 150 }
151 151 };
152 152
153 153
154 154 OutputArea.prototype.scroll_area = function () {
155 155 this.element.addClass('output_scroll');
156 156 this.prompt_overlay.attr('title', 'click to unscroll output; double click to hide');
157 157 this.scrolled = true;
158 158 };
159 159
160 160
161 161 OutputArea.prototype.unscroll_area = function () {
162 162 this.element.removeClass('output_scroll');
163 163 this.prompt_overlay.attr('title', 'click to scroll output; double click to hide');
164 164 this.scrolled = false;
165 165 };
166 166
167 167 /**
168 168 *
169 169 * Scroll OutputArea if height supperior than a threshold (in lines).
170 170 *
171 171 * Threshold is a maximum number of lines. If unspecified, defaults to
172 172 * OutputArea.minimum_scroll_threshold.
173 173 *
174 174 * Negative threshold will prevent the OutputArea from ever scrolling.
175 175 *
176 176 * @method scroll_if_long
177 177 *
178 178 * @param [lines=20]{Number} Default to 20 if not set,
179 179 * behavior undefined for value of `0`.
180 180 *
181 181 **/
182 182 OutputArea.prototype.scroll_if_long = function (lines) {
183 183 var n = lines | OutputArea.minimum_scroll_threshold;
184 184 if(n <= 0){
185 185 return
186 186 }
187 187
188 188 if (this._should_scroll(n)) {
189 189 // only allow scrolling long-enough output
190 190 this.scroll_area();
191 191 }
192 192 };
193 193
194 194
195 195 OutputArea.prototype.toggle_scroll = function () {
196 196 if (this.scrolled) {
197 197 this.unscroll_area();
198 198 } else {
199 199 // only allow scrolling long-enough output
200 200 this.scroll_if_long();
201 201 }
202 202 };
203 203
204 204
205 205 // typeset with MathJax if MathJax is available
206 206 OutputArea.prototype.typeset = function () {
207 207 if (window.MathJax){
208 208 MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
209 209 }
210 210 };
211 211
212 212
213 213 OutputArea.prototype.handle_output = function (msg) {
214 214 var json = {};
215 215 var msg_type = json.output_type = msg.header.msg_type;
216 216 var content = msg.content;
217 217 if (msg_type === "stream") {
218 218 json.text = content.data;
219 219 json.stream = content.name;
220 220 } else if (msg_type === "display_data") {
221 221 json = content.data;
222 222 json.output_type = msg_type;
223 223 json.metadata = content.metadata;
224 224 } else if (msg_type === "pyout") {
225 225 json = content.data;
226 226 json.output_type = msg_type;
227 227 json.metadata = content.metadata;
228 228 json.prompt_number = content.execution_count;
229 229 } else if (msg_type === "pyerr") {
230 230 json.ename = content.ename;
231 231 json.evalue = content.evalue;
232 232 json.traceback = content.traceback;
233 233 }
234 234 this.append_output(json);
235 235 };
236 236
237 237
238 238 OutputArea.prototype.rename_keys = function (data, key_map) {
239 239 var remapped = {};
240 240 for (var key in data) {
241 241 var new_key = key_map[key] || key;
242 242 remapped[new_key] = data[key];
243 243 }
244 244 return remapped;
245 245 };
246 246
247 247
248 248 OutputArea.output_types = [
249 249 'application/javascript',
250 250 'text/html',
251 251 'text/latex',
252 252 'image/svg+xml',
253 253 'image/png',
254 254 'image/jpeg',
255 255 'application/pdf',
256 256 'text/plain'
257 257 ];
258 258
259 259 OutputArea.prototype.validate_output = function (json) {
260 260 // scrub invalid outputs
261 261 // TODO: right now everything is a string, but JSON really shouldn't be.
262 262 // nbformat 4 will fix that.
263 263 $.map(OutputArea.output_types, function(key){
264 264 if (json[key] !== undefined && typeof json[key] !== 'string') {
265 265 console.log("Invalid type for " + key, json[key]);
266 266 delete json[key];
267 267 }
268 268 });
269 269 return json;
270 270 };
271 271
272 272 OutputArea.prototype.append_output = function (json) {
273 273 this.expand();
274 274 // Clear the output if clear is queued.
275 275 var needs_height_reset = false;
276 276 if (this.clear_queued) {
277 277 this.clear_output(false);
278 278 needs_height_reset = true;
279 279 }
280 280
281 281 // validate output data types
282 282 json = this.validate_output(json);
283 283
284 284 if (json.output_type === 'pyout') {
285 285 this.append_pyout(json);
286 286 } else if (json.output_type === 'pyerr') {
287 287 this.append_pyerr(json);
288 288 } else if (json.output_type === 'display_data') {
289 289 this.append_display_data(json);
290 290 } else if (json.output_type === 'stream') {
291 291 this.append_stream(json);
292 292 }
293 293
294 294 this.outputs.push(json);
295 295
296 // Only reset the height to automatic if the height is currently
297 // fixed (done by wait=True flag on clear_output).
298 if (needs_height_reset) {
299 this.element.height('');
300 }
301
296 // We must release the animation fixed height in a timeout since Gecko
297 // (FireFox) doesn't render the image immediately as the data is
298 // available.
302 299 var that = this;
303 setTimeout(function(){that.element.trigger('resize');}, 100);
300 setTimeout(function(){
301 // Only reset the height to automatic if the height is currently
302 // fixed (done by wait=True flag on clear_output).
303 if (needs_height_reset) {
304 that.element.height('');
305 }
306 that.element.trigger('resize');
307 }, 250);
304 308 };
305 309
306 310
307 311 OutputArea.prototype.create_output_area = function () {
308 312 var oa = $("<div/>").addClass("output_area");
309 313 if (this.prompt_area) {
310 314 oa.append($('<div/>').addClass('prompt'));
311 315 }
312 316 return oa;
313 317 };
314 318
315 319
316 320 function _get_metadata_key(metadata, key, mime) {
317 321 var mime_md = metadata[mime];
318 322 // mime-specific higher priority
319 323 if (mime_md && mime_md[key] !== undefined) {
320 324 return mime_md[key];
321 325 }
322 326 // fallback on global
323 327 return metadata[key];
324 328 }
325 329
326 330 OutputArea.prototype.create_output_subarea = function(md, classes, mime) {
327 331 var subarea = $('<div/>').addClass('output_subarea').addClass(classes);
328 332 if (_get_metadata_key(md, 'isolated', mime)) {
329 333 // Create an iframe to isolate the subarea from the rest of the
330 334 // document
331 335 var iframe = $('<iframe/>').addClass('box-flex1');
332 336 iframe.css({'height':1, 'width':'100%', 'display':'block'});
333 337 iframe.attr('frameborder', 0);
334 338 iframe.attr('scrolling', 'auto');
335 339
336 340 // Once the iframe is loaded, the subarea is dynamically inserted
337 341 iframe.on('load', function() {
338 342 // Workaround needed by Firefox, to properly render svg inside
339 343 // iframes, see http://stackoverflow.com/questions/10177190/
340 344 // svg-dynamically-added-to-iframe-does-not-render-correctly
341 345 this.contentDocument.open();
342 346
343 347 // Insert the subarea into the iframe
344 348 // We must directly write the html. When using Jquery's append
345 349 // method, javascript is evaluated in the parent document and
346 350 // not in the iframe document. At this point, subarea doesn't
347 351 // contain any user content.
348 352 this.contentDocument.write(subarea.html());
349 353
350 354 this.contentDocument.close();
351 355
352 356 var body = this.contentDocument.body;
353 357 // Adjust the iframe height automatically
354 358 iframe.height(body.scrollHeight + 'px');
355 359 });
356 360
357 361 // Elements should be appended to the inner subarea and not to the
358 362 // iframe
359 363 iframe.append = function(that) {
360 364 subarea.append(that);
361 365 };
362 366
363 367 return iframe;
364 368 } else {
365 369 return subarea;
366 370 }
367 371 }
368 372
369 373
370 374 OutputArea.prototype._append_javascript_error = function (err, element) {
371 375 // display a message when a javascript error occurs in display output
372 376 var msg = "Javascript error adding output!"
373 377 if ( element === undefined ) return;
374 378 element
375 379 .append($('<div/>').text(msg).addClass('js-error'))
376 380 .append($('<div/>').text(err.toString()).addClass('js-error'))
377 381 .append($('<div/>').text('See your browser Javascript console for more details.').addClass('js-error'));
378 382 };
379 383
380 384 OutputArea.prototype._safe_append = function (toinsert) {
381 385 // safely append an item to the document
382 386 // this is an object created by user code,
383 387 // and may have errors, which should not be raised
384 388 // under any circumstances.
385 389 try {
386 390 this.element.append(toinsert);
387 391 } catch(err) {
388 392 console.log(err);
389 393 // Create an actual output_area and output_subarea, which creates
390 394 // the prompt area and the proper indentation.
391 395 var toinsert = this.create_output_area();
392 396 var subarea = $('<div/>').addClass('output_subarea');
393 397 toinsert.append(subarea);
394 398 this._append_javascript_error(err, subarea);
395 399 this.element.append(toinsert);
396 400 }
397 401 };
398 402
399 403
400 404 OutputArea.prototype.append_pyout = function (json) {
401 405 var n = json.prompt_number || ' ';
402 406 var toinsert = this.create_output_area();
403 407 if (this.prompt_area) {
404 408 toinsert.find('div.prompt').addClass('output_prompt').text('Out[' + n + ']:');
405 409 }
406 410 var inserted = this.append_mime_type(json, toinsert);
407 411 if (inserted) {
408 412 inserted.addClass('output_pyout');
409 413 }
410 414 this._safe_append(toinsert);
411 415 // If we just output latex, typeset it.
412 416 if ((json['text/latex'] !== undefined) || (json['text/html'] !== undefined)) {
413 417 this.typeset();
414 418 }
415 419 };
416 420
417 421
418 422 OutputArea.prototype.append_pyerr = function (json) {
419 423 var tb = json.traceback;
420 424 if (tb !== undefined && tb.length > 0) {
421 425 var s = '';
422 426 var len = tb.length;
423 427 for (var i=0; i<len; i++) {
424 428 s = s + tb[i] + '\n';
425 429 }
426 430 s = s + '\n';
427 431 var toinsert = this.create_output_area();
428 432 var append_text = OutputArea.append_map['text/plain'];
429 433 if (append_text) {
430 434 append_text.apply(this, [s, {}, toinsert]).addClass('output_pyerr');
431 435 }
432 436 this._safe_append(toinsert);
433 437 }
434 438 };
435 439
436 440
437 441 OutputArea.prototype.append_stream = function (json) {
438 442 // temporary fix: if stream undefined (json file written prior to this patch),
439 443 // default to most likely stdout:
440 444 if (json.stream === undefined){
441 445 json.stream = 'stdout';
442 446 }
443 447 var text = json.text;
444 448 var subclass = "output_"+json.stream;
445 449 if (this.outputs.length > 0){
446 450 // have at least one output to consider
447 451 var last = this.outputs[this.outputs.length-1];
448 452 if (last.output_type == 'stream' && json.stream == last.stream){
449 453 // latest output was in the same stream,
450 454 // so append directly into its pre tag
451 455 // escape ANSI & HTML specials:
452 456 var pre = this.element.find('div.'+subclass).last().find('pre');
453 457 var html = utils.fixCarriageReturn(
454 458 pre.html() + utils.fixConsole(text));
455 459 // The only user content injected with this HTML call is
456 460 // escaped by the fixConsole() method.
457 461 pre.html(html);
458 462 return;
459 463 }
460 464 }
461 465
462 466 if (!text.replace("\r", "")) {
463 467 // text is nothing (empty string, \r, etc.)
464 468 // so don't append any elements, which might add undesirable space
465 469 return;
466 470 }
467 471
468 472 // If we got here, attach a new div
469 473 var toinsert = this.create_output_area();
470 474 var append_text = OutputArea.append_map['text/plain'];
471 475 if (append_text) {
472 476 append_text.apply(this, [text, {}, toinsert]).addClass("output_stream " + subclass);
473 477 }
474 478 this._safe_append(toinsert);
475 479 };
476 480
477 481
478 482 OutputArea.prototype.append_display_data = function (json) {
479 483 var toinsert = this.create_output_area();
480 484 if (this.append_mime_type(json, toinsert)) {
481 485 this._safe_append(toinsert);
482 486 // If we just output latex, typeset it.
483 487 if ((json['text/latex'] !== undefined) || (json['text/html'] !== undefined)) {
484 488 this.typeset();
485 489 }
486 490 }
487 491 };
488 492
489 493
490 494 OutputArea.safe_outputs = {
491 495 'text/plain' : true,
492 496 'text/latex' : true,
493 497 'image/png' : true,
494 498 'image/jpeg' : true
495 499 };
496 500
497 501 OutputArea.prototype.append_mime_type = function (json, element) {
498 502 for (var type_i in OutputArea.display_order) {
499 503 var type = OutputArea.display_order[type_i];
500 504 var append = OutputArea.append_map[type];
501 505 if ((json[type] !== undefined) && append) {
502 506 var value = json[type];
503 507 if (!this.trusted && !OutputArea.safe_outputs[type]) {
504 508 // not trusted, sanitize HTML
505 509 if (type==='text/html' || type==='text/svg') {
506 510 value = IPython.security.sanitize_html(value);
507 511 } else {
508 512 // don't display if we don't know how to sanitize it
509 513 console.log("Ignoring untrusted " + type + " output.");
510 514 continue;
511 515 }
512 516 }
513 517 var md = json.metadata || {};
514 518 var toinsert = append.apply(this, [value, md, element]);
515 519 $([IPython.events]).trigger('output_appended.OutputArea', [type, value, md, toinsert]);
516 520 return toinsert;
517 521 }
518 522 }
519 523 return null;
520 524 };
521 525
522 526
523 527 var append_html = function (html, md, element) {
524 528 var type = 'text/html';
525 529 var toinsert = this.create_output_subarea(md, "output_html rendered_html", type);
526 530 IPython.keyboard_manager.register_events(toinsert);
527 531 toinsert.append(html);
528 532 element.append(toinsert);
529 533 return toinsert;
530 534 };
531 535
532 536
533 537 var append_javascript = function (js, md, element) {
534 538 // We just eval the JS code, element appears in the local scope.
535 539 var type = 'application/javascript';
536 540 var toinsert = this.create_output_subarea(md, "output_javascript", type);
537 541 IPython.keyboard_manager.register_events(toinsert);
538 542 element.append(toinsert);
539 543 // FIXME TODO : remove `container element for 3.0`
540 544 //backward compat, js should be eval'ed in a context where `container` is defined.
541 545 var container = element;
542 546 container.show = function(){console.log('Warning "container.show()" is deprecated.')};
543 547 // end backward compat
544 548
545 549 // Fix for ipython/issues/5293, make sure `element` is the area which
546 550 // output can be inserted into at the time of JS execution.
547 551 element = toinsert;
548 552 try {
549 553 eval(js);
550 554 } catch(err) {
551 555 console.log(err);
552 556 this._append_javascript_error(err, toinsert);
553 557 }
554 558 return toinsert;
555 559 };
556 560
557 561
558 562 var append_text = function (data, md, element) {
559 563 var type = 'text/plain';
560 564 var toinsert = this.create_output_subarea(md, "output_text", type);
561 565 // escape ANSI & HTML specials in plaintext:
562 566 data = utils.fixConsole(data);
563 567 data = utils.fixCarriageReturn(data);
564 568 data = utils.autoLinkUrls(data);
565 569 // The only user content injected with this HTML call is
566 570 // escaped by the fixConsole() method.
567 571 toinsert.append($("<pre/>").html(data));
568 572 element.append(toinsert);
569 573 return toinsert;
570 574 };
571 575
572 576
573 577 var append_svg = function (svg, md, element) {
574 578 var type = 'image/svg+xml';
575 579 var toinsert = this.create_output_subarea(md, "output_svg", type);
576 580 toinsert.append(svg);
577 581 element.append(toinsert);
578 582 return toinsert;
579 583 };
580 584
581 585
582 586 OutputArea.prototype._dblclick_to_reset_size = function (img) {
583 587 // wrap image after it's loaded on the page,
584 588 // otherwise the measured initial size will be incorrect
585 589 img.on("load", function (){
586 590 var h0 = img.height();
587 591 var w0 = img.width();
588 592 if (!(h0 && w0)) {
589 593 // zero size, don't make it resizable
590 594 return;
591 595 }
592 596 img.resizable({
593 597 aspectRatio: true,
594 598 autoHide: true
595 599 });
596 600 img.dblclick(function () {
597 601 // resize wrapper & image together for some reason:
598 602 img.parent().height(h0);
599 603 img.height(h0);
600 604 img.parent().width(w0);
601 605 img.width(w0);
602 606 });
603 607 });
604 608 };
605 609
606 610 var set_width_height = function (img, md, mime) {
607 611 // set width and height of an img element from metadata
608 612 var height = _get_metadata_key(md, 'height', mime);
609 613 if (height !== undefined) img.attr('height', height);
610 614 var width = _get_metadata_key(md, 'width', mime);
611 615 if (width !== undefined) img.attr('width', width);
612 616 };
613 617
614 618 var append_png = function (png, md, element) {
615 619 var type = 'image/png';
616 620 var toinsert = this.create_output_subarea(md, "output_png", type);
617 621 var img = $("<img/>").attr('src','data:image/png;base64,'+png);
618 622 set_width_height(img, md, 'image/png');
619 623 this._dblclick_to_reset_size(img);
620 624 toinsert.append(img);
621 625 element.append(toinsert);
622 626 return toinsert;
623 627 };
624 628
625 629
626 630 var append_jpeg = function (jpeg, md, element) {
627 631 var type = 'image/jpeg';
628 632 var toinsert = this.create_output_subarea(md, "output_jpeg", type);
629 633 var img = $("<img/>").attr('src','data:image/jpeg;base64,'+jpeg);
630 634 set_width_height(img, md, 'image/jpeg');
631 635 this._dblclick_to_reset_size(img);
632 636 toinsert.append(img);
633 637 element.append(toinsert);
634 638 return toinsert;
635 639 };
636 640
637 641
638 642 var append_pdf = function (pdf, md, element) {
639 643 var type = 'application/pdf';
640 644 var toinsert = this.create_output_subarea(md, "output_pdf", type);
641 645 var a = $('<a/>').attr('href', 'data:application/pdf;base64,'+pdf);
642 646 a.attr('target', '_blank');
643 647 a.text('View PDF')
644 648 toinsert.append(a);
645 649 element.append(toinsert);
646 650 return toinsert;
647 651 }
648 652
649 653 var append_latex = function (latex, md, element) {
650 654 // This method cannot do the typesetting because the latex first has to
651 655 // be on the page.
652 656 var type = 'text/latex';
653 657 var toinsert = this.create_output_subarea(md, "output_latex", type);
654 658 toinsert.append(latex);
655 659 element.append(toinsert);
656 660 return toinsert;
657 661 };
658 662
659 663
660 664 OutputArea.prototype.append_raw_input = function (msg) {
661 665 var that = this;
662 666 this.expand();
663 667 var content = msg.content;
664 668 var area = this.create_output_area();
665 669
666 670 // disable any other raw_inputs, if they are left around
667 671 $("div.output_subarea.raw_input_container").remove();
668 672
669 673 area.append(
670 674 $("<div/>")
671 675 .addClass("box-flex1 output_subarea raw_input_container")
672 676 .append(
673 677 $("<span/>")
674 678 .addClass("raw_input_prompt")
675 679 .text(content.prompt)
676 680 )
677 681 .append(
678 682 $("<input/>")
679 683 .addClass("raw_input")
680 684 .attr('type', 'text')
681 685 .attr("size", 47)
682 686 .keydown(function (event, ui) {
683 687 // make sure we submit on enter,
684 688 // and don't re-execute the *cell* on shift-enter
685 689 if (event.which === IPython.keyboard.keycodes.enter) {
686 690 that._submit_raw_input();
687 691 return false;
688 692 }
689 693 })
690 694 )
691 695 );
692 696
693 697 this.element.append(area);
694 698 var raw_input = area.find('input.raw_input');
695 699 // Register events that enable/disable the keyboard manager while raw
696 700 // input is focused.
697 701 IPython.keyboard_manager.register_events(raw_input);
698 702 // Note, the following line used to read raw_input.focus().focus().
699 703 // This seemed to be needed otherwise only the cell would be focused.
700 704 // But with the modal UI, this seems to work fine with one call to focus().
701 705 raw_input.focus();
702 706 }
703 707
704 708 OutputArea.prototype._submit_raw_input = function (evt) {
705 709 var container = this.element.find("div.raw_input_container");
706 710 var theprompt = container.find("span.raw_input_prompt");
707 711 var theinput = container.find("input.raw_input");
708 712 var value = theinput.val();
709 713 var content = {
710 714 output_type : 'stream',
711 715 name : 'stdout',
712 716 text : theprompt.text() + value + '\n'
713 717 }
714 718 // remove form container
715 719 container.parent().remove();
716 720 // replace with plaintext version in stdout
717 721 this.append_output(content, false);
718 722 $([IPython.events]).trigger('send_input_reply.Kernel', value);
719 723 }
720 724
721 725
722 726 OutputArea.prototype.handle_clear_output = function (msg) {
723 727 // msg spec v4 had stdout, stderr, display keys
724 728 // v4.1 replaced these with just wait
725 729 // The default behavior is the same (stdout=stderr=display=True, wait=False),
726 730 // so v4 messages will still be properly handled,
727 731 // except for the rarely used clearing less than all output.
728 732 this.clear_output(msg.content.wait || false);
729 733 };
730 734
731 735
732 736 OutputArea.prototype.clear_output = function(wait) {
733 737 if (wait) {
734 738
735 739 // If a clear is queued, clear before adding another to the queue.
736 740 if (this.clear_queued) {
737 741 this.clear_output(false);
738 742 };
739 743
740 744 this.clear_queued = true;
741 745 } else {
742 746
743 747 // Fix the output div's height if the clear_output is waiting for
744 748 // new output (it is being used in an animation).
745 749 if (this.clear_queued) {
746 750 var height = this.element.height();
747 751 this.element.height(height);
748 752 this.clear_queued = false;
749 753 }
750 754
751 755 // clear all, no need for logic
752 756 this.element.html("");
753 757 this.outputs = [];
754 758 this.trusted = true;
755 759 this.unscroll_area();
756 760 return;
757 761 };
758 762 };
759 763
760 764
761 765 // JSON serialization
762 766
763 767 OutputArea.prototype.fromJSON = function (outputs) {
764 768 var len = outputs.length;
765 769 var data;
766 770
767 771 for (var i=0; i<len; i++) {
768 772 data = outputs[i];
769 773 var msg_type = data.output_type;
770 774 if (msg_type === "display_data" || msg_type === "pyout") {
771 775 // convert short keys to mime keys
772 776 // TODO: remove mapping of short keys when we update to nbformat 4
773 777 data = this.rename_keys(data, OutputArea.mime_map_r);
774 778 data.metadata = this.rename_keys(data.metadata, OutputArea.mime_map_r);
775 779 }
776 780
777 781 this.append_output(data);
778 782 }
779 783 };
780 784
781 785
782 786 OutputArea.prototype.toJSON = function () {
783 787 var outputs = [];
784 788 var len = this.outputs.length;
785 789 var data;
786 790 for (var i=0; i<len; i++) {
787 791 data = this.outputs[i];
788 792 var msg_type = data.output_type;
789 793 if (msg_type === "display_data" || msg_type === "pyout") {
790 794 // convert mime keys to short keys
791 795 data = this.rename_keys(data, OutputArea.mime_map);
792 796 data.metadata = this.rename_keys(data.metadata, OutputArea.mime_map);
793 797 }
794 798 outputs[i] = data;
795 799 }
796 800 return outputs;
797 801 };
798 802
799 803 /**
800 804 * Class properties
801 805 **/
802 806
803 807 /**
804 808 * Threshold to trigger autoscroll when the OutputArea is resized,
805 809 * typically when new outputs are added.
806 810 *
807 811 * Behavior is undefined if autoscroll is lower than minimum_scroll_threshold,
808 812 * unless it is < 0, in which case autoscroll will never be triggered
809 813 *
810 814 * @property auto_scroll_threshold
811 815 * @type Number
812 816 * @default 100
813 817 *
814 818 **/
815 819 OutputArea.auto_scroll_threshold = 100;
816 820
817 821 /**
818 822 * Lower limit (in lines) for OutputArea to be made scrollable. OutputAreas
819 823 * shorter than this are never scrolled.
820 824 *
821 825 * @property minimum_scroll_threshold
822 826 * @type Number
823 827 * @default 20
824 828 *
825 829 **/
826 830 OutputArea.minimum_scroll_threshold = 20;
827 831
828 832
829 833
830 834 OutputArea.mime_map = {
831 835 "text/plain" : "text",
832 836 "text/html" : "html",
833 837 "image/svg+xml" : "svg",
834 838 "image/png" : "png",
835 839 "image/jpeg" : "jpeg",
836 840 "text/latex" : "latex",
837 841 "application/json" : "json",
838 842 "application/javascript" : "javascript",
839 843 };
840 844
841 845 OutputArea.mime_map_r = {
842 846 "text" : "text/plain",
843 847 "html" : "text/html",
844 848 "svg" : "image/svg+xml",
845 849 "png" : "image/png",
846 850 "jpeg" : "image/jpeg",
847 851 "latex" : "text/latex",
848 852 "json" : "application/json",
849 853 "javascript" : "application/javascript",
850 854 };
851 855
852 856 OutputArea.display_order = [
853 857 'application/javascript',
854 858 'text/html',
855 859 'text/latex',
856 860 'image/svg+xml',
857 861 'image/png',
858 862 'image/jpeg',
859 863 'application/pdf',
860 864 'text/plain'
861 865 ];
862 866
863 867 OutputArea.append_map = {
864 868 "text/plain" : append_text,
865 869 "text/html" : append_html,
866 870 "image/svg+xml" : append_svg,
867 871 "image/png" : append_png,
868 872 "image/jpeg" : append_jpeg,
869 873 "text/latex" : append_latex,
870 874 "application/javascript" : append_javascript,
871 875 "application/pdf" : append_pdf
872 876 };
873 877
874 878 IPython.OutputArea = OutputArea;
875 879
876 880 return IPython;
877 881
878 882 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now