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