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