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