##// END OF EJS Templates
fix setting image height/width from metadata
MinRK -
Show More
@@ -1,819 +1,816 b''
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 this.append_output(json);
256 256 };
257 257
258 258 OutputArea.mime_map = {
259 259 "text/plain" : "text",
260 260 "text/html" : "html",
261 261 "image/svg+xml" : "svg",
262 262 "image/png" : "png",
263 263 "image/jpeg" : "jpeg",
264 264 "text/latex" : "latex",
265 265 "application/json" : "json",
266 266 "application/javascript" : "javascript",
267 267 };
268 268
269 269 OutputArea.mime_map_r = {
270 270 "text" : "text/plain",
271 271 "html" : "text/html",
272 272 "svg" : "image/svg+xml",
273 273 "png" : "image/png",
274 274 "jpeg" : "image/jpeg",
275 275 "latex" : "text/latex",
276 276 "json" : "application/json",
277 277 "javascript" : "application/javascript",
278 278 };
279 279
280 280 OutputArea.prototype.rename_keys = function (data, key_map) {
281 281 var remapped = {};
282 282 for (var key in data) {
283 283 var new_key = key_map[key] || key;
284 284 remapped[new_key] = data[key];
285 285 }
286 286 return remapped;
287 287 };
288 288
289 289
290 290 OutputArea.output_types = [
291 291 'application/javascript',
292 292 'text/html',
293 293 'text/latex',
294 294 'image/svg+xml',
295 295 'image/png',
296 296 'image/jpeg',
297 297 'text/plain'
298 298 ];
299 299
300 300 OutputArea.prototype.validate_output = function (json) {
301 301 // scrub invalid outputs
302 302 // TODO: right now everything is a string, but JSON really shouldn't be.
303 303 // nbformat 4 will fix that.
304 304 $.map(OutputArea.output_types, function(key){
305 305 if (json[key] !== undefined && typeof json[key] !== 'string') {
306 306 console.log("Invalid type for " + key, json[key]);
307 307 delete json[key];
308 308 }
309 309 });
310 310 return json;
311 311 };
312 312
313 313 OutputArea.prototype.append_output = function (json) {
314 314 this.expand();
315 315 // Clear the output if clear is queued.
316 316 var needs_height_reset = false;
317 317 if (this.clear_queued) {
318 318 this.clear_output(false);
319 319 needs_height_reset = true;
320 320 }
321 321
322 322 // validate output data types
323 323 json = this.validate_output(json);
324 324
325 325 if (json.output_type === 'pyout') {
326 326 this.append_pyout(json);
327 327 } else if (json.output_type === 'pyerr') {
328 328 this.append_pyerr(json);
329 329 } else if (json.output_type === 'display_data') {
330 330 this.append_display_data(json);
331 331 } else if (json.output_type === 'stream') {
332 332 this.append_stream(json);
333 333 }
334 334 this.outputs.push(json);
335 335
336 336 // Only reset the height to automatic if the height is currently
337 337 // fixed (done by wait=True flag on clear_output).
338 338 if (needs_height_reset) {
339 339 this.element.height('');
340 340 }
341 341
342 342 var that = this;
343 343 setTimeout(function(){that.element.trigger('resize');}, 100);
344 344 };
345 345
346 346
347 347 OutputArea.prototype.create_output_area = function () {
348 348 var oa = $("<div/>").addClass("output_area");
349 349 if (this.prompt_area) {
350 350 oa.append($('<div/>').addClass('prompt'));
351 351 }
352 352 return oa;
353 353 };
354 354
355 355
356 356 function _get_metadata_key(metadata, key, mime) {
357 357 var mime_md = metadata[mime];
358 358 // mime-specific higher priority
359 359 if (mime_md && mime_md[key] !== undefined) {
360 360 return mime_md[key];
361 361 }
362 362 // fallback on global
363 363 return metadata[key];
364 364 }
365 365
366 366 OutputArea.prototype.create_output_subarea = function(md, classes, mime) {
367 367 var subarea = $('<div/>').addClass('output_subarea').addClass(classes);
368 368 if (_get_metadata_key(md, 'isolated', mime)) {
369 369 // Create an iframe to isolate the subarea from the rest of the
370 370 // document
371 371 var iframe = $('<iframe/>').addClass('box-flex1');
372 372 iframe.css({'height':1, 'width':'100%', 'display':'block'});
373 373 iframe.attr('frameborder', 0);
374 374 iframe.attr('scrolling', 'auto');
375 375
376 376 // Once the iframe is loaded, the subarea is dynamically inserted
377 377 iframe.on('load', function() {
378 378 // Workaround needed by Firefox, to properly render svg inside
379 379 // iframes, see http://stackoverflow.com/questions/10177190/
380 380 // svg-dynamically-added-to-iframe-does-not-render-correctly
381 381 this.contentDocument.open();
382 382
383 383 // Insert the subarea into the iframe
384 384 // We must directly write the html. When using Jquery's append
385 385 // method, javascript is evaluated in the parent document and
386 386 // not in the iframe document.
387 387 this.contentDocument.write(subarea.html());
388 388
389 389 this.contentDocument.close();
390 390
391 391 var body = this.contentDocument.body;
392 392 // Adjust the iframe height automatically
393 393 iframe.height(body.scrollHeight + 'px');
394 394 });
395 395
396 396 // Elements should be appended to the inner subarea and not to the
397 397 // iframe
398 398 iframe.append = function(that) {
399 399 subarea.append(that);
400 400 };
401 401
402 402 return iframe;
403 403 } else {
404 404 return subarea;
405 405 }
406 406 }
407 407
408 408
409 409 OutputArea.prototype._append_javascript_error = function (err, element) {
410 410 // display a message when a javascript error occurs in display output
411 411 var msg = "Javascript error adding output!"
412 412 if ( element === undefined ) return;
413 413 element.append(
414 414 $('<div/>').html(msg + "<br/>" +
415 415 err.toString() +
416 416 '<br/>See your browser Javascript console for more details.'
417 417 ).addClass('js-error')
418 418 );
419 419 };
420 420
421 421 OutputArea.prototype._safe_append = function (toinsert) {
422 422 // safely append an item to the document
423 423 // this is an object created by user code,
424 424 // and may have errors, which should not be raised
425 425 // under any circumstances.
426 426 try {
427 427 this.element.append(toinsert);
428 428 } catch(err) {
429 429 console.log(err);
430 430 // Create an actual output_area and output_subarea, which creates
431 431 // the prompt area and the proper indentation.
432 432 var toinsert = this.create_output_area();
433 433 var subarea = $('<div/>').addClass('output_subarea');
434 434 toinsert.append(subarea);
435 435 this._append_javascript_error(err, subarea);
436 436 this.element.append(toinsert);
437 437 }
438 438 };
439 439
440 440
441 441 OutputArea.prototype.append_pyout = function (json) {
442 442 var n = json.prompt_number || ' ';
443 443 var toinsert = this.create_output_area();
444 444 if (this.prompt_area) {
445 445 toinsert.find('div.prompt').addClass('output_prompt').html('Out[' + n + ']:');
446 446 }
447 447 this.append_mime_type(json, toinsert);
448 448 this._safe_append(toinsert);
449 449 // If we just output latex, typeset it.
450 450 if ((json['text/latex'] !== undefined) || (json['text/html'] !== undefined)) {
451 451 this.typeset();
452 452 }
453 453 };
454 454
455 455
456 456 OutputArea.prototype.append_pyerr = function (json) {
457 457 var tb = json.traceback;
458 458 if (tb !== undefined && tb.length > 0) {
459 459 var s = '';
460 460 var len = tb.length;
461 461 for (var i=0; i<len; i++) {
462 462 s = s + tb[i] + '\n';
463 463 }
464 464 s = s + '\n';
465 465 var toinsert = this.create_output_area();
466 466 this.append_text(s, {}, toinsert);
467 467 this._safe_append(toinsert);
468 468 }
469 469 };
470 470
471 471
472 472 OutputArea.prototype.append_stream = function (json) {
473 473 // temporary fix: if stream undefined (json file written prior to this patch),
474 474 // default to most likely stdout:
475 475 if (json.stream == undefined){
476 476 json.stream = 'stdout';
477 477 }
478 478 var text = json.text;
479 479 var subclass = "output_"+json.stream;
480 480 if (this.outputs.length > 0){
481 481 // have at least one output to consider
482 482 var last = this.outputs[this.outputs.length-1];
483 483 if (last.output_type == 'stream' && json.stream == last.stream){
484 484 // latest output was in the same stream,
485 485 // so append directly into its pre tag
486 486 // escape ANSI & HTML specials:
487 487 var pre = this.element.find('div.'+subclass).last().find('pre');
488 488 var html = utils.fixCarriageReturn(
489 489 pre.html() + utils.fixConsole(text));
490 490 pre.html(html);
491 491 return;
492 492 }
493 493 }
494 494
495 495 if (!text.replace("\r", "")) {
496 496 // text is nothing (empty string, \r, etc.)
497 497 // so don't append any elements, which might add undesirable space
498 498 return;
499 499 }
500 500
501 501 // If we got here, attach a new div
502 502 var toinsert = this.create_output_area();
503 503 this.append_text(text, {}, toinsert, "output_stream "+subclass);
504 504 this._safe_append(toinsert);
505 505 };
506 506
507 507
508 508 OutputArea.prototype.append_display_data = function (json) {
509 509 var toinsert = this.create_output_area();
510 510 if (this.append_mime_type(json, toinsert)) {
511 511 this._safe_append(toinsert);
512 512 // If we just output latex, typeset it.
513 513 if ((json['text/latex'] !== undefined) || (json['text/html'] !== undefined)) {
514 514 this.typeset();
515 515 }
516 516 }
517 517 };
518 518
519 519 OutputArea.display_order = [
520 520 'application/javascript',
521 521 'text/html',
522 522 'text/latex',
523 523 'image/svg+xml',
524 524 'image/png',
525 525 'image/jpeg',
526 526 'text/plain'
527 527 ];
528 528
529 529 OutputArea.prototype.append_mime_type = function (json, element) {
530 530
531 531 for (var type_i in OutputArea.display_order) {
532 532 var type = OutputArea.display_order[type_i];
533 533 var append = OutputArea.append_map[type];
534 534 if ((json[type] !== undefined) && append) {
535 535 var md = json.metadata || {};
536 536 append.apply(this, [json[type], md, element]);
537 537 return true;
538 538 }
539 539 }
540 540 return false;
541 541 };
542 542
543 543
544 544 OutputArea.prototype.append_html = function (html, md, element) {
545 545 var type = 'text/html';
546 546 var toinsert = this.create_output_subarea(md, "output_html rendered_html", type);
547 547 IPython.keyboard_manager.register_events(toinsert);
548 548 toinsert.append(html);
549 549 element.append(toinsert);
550 550 };
551 551
552 552
553 553 OutputArea.prototype.append_javascript = function (js, md, container) {
554 554 // We just eval the JS code, element appears in the local scope.
555 555 var type = 'application/javascript';
556 556 var element = this.create_output_subarea(md, "output_javascript", type);
557 557 IPython.keyboard_manager.register_events(element);
558 558 container.append(element);
559 559 try {
560 560 eval(js);
561 561 } catch(err) {
562 562 console.log(err);
563 563 this._append_javascript_error(err, element);
564 564 }
565 565 };
566 566
567 567
568 568 OutputArea.prototype.append_text = function (data, md, element, extra_class) {
569 569 var type = 'text/plain';
570 570 var toinsert = this.create_output_subarea(md, "output_text", type);
571 571 // escape ANSI & HTML specials in plaintext:
572 572 data = utils.fixConsole(data);
573 573 data = utils.fixCarriageReturn(data);
574 574 data = utils.autoLinkUrls(data);
575 575 if (extra_class){
576 576 toinsert.addClass(extra_class);
577 577 }
578 578 toinsert.append($("<pre/>").html(data));
579 579 element.append(toinsert);
580 580 };
581 581
582 582
583 583 OutputArea.prototype.append_svg = function (svg, md, element) {
584 584 var type = 'image/svg+xml';
585 585 var toinsert = this.create_output_subarea(md, "output_svg", type);
586 586 toinsert.append(svg);
587 587 element.append(toinsert);
588 588 };
589 589
590 590
591 591 OutputArea.prototype._dblclick_to_reset_size = function (img) {
592 592 // schedule wrapping image in resizable after a delay,
593 593 // so we don't end up calling resize on a zero-size object
594 594 var that = this;
595 595 setTimeout(function () {
596 596 var h0 = img.height();
597 597 var w0 = img.width();
598 598 if (!(h0 && w0)) {
599 599 // zero size, schedule another timeout
600 600 that._dblclick_to_reset_size(img);
601 601 return;
602 602 }
603 603 img.resizable({
604 604 aspectRatio: true,
605 605 autoHide: true
606 606 });
607 607 img.dblclick(function () {
608 608 // resize wrapper & image together for some reason:
609 609 img.parent().height(h0);
610 610 img.height(h0);
611 611 img.parent().width(w0);
612 612 img.width(w0);
613 613 });
614 614 }, 250);
615 615 };
616 616
617 var set_width_height = function (img, md, mime) {
618 // set width and height of an img element from metadata
619 var height = _get_metadata_key(md, 'height', mime);
620 if (height !== undefined) img.setAttribute('height', height);
621 var width = _get_metadata_key(md, 'width', mime);
622 if (width !== undefined) img.setAttribute('width', width);
623 };
617 624
618 625 OutputArea.prototype.append_png = function (png, md, element) {
619 626 var type = 'image/png';
620 627 var toinsert = this.create_output_subarea(md, "output_png", type);
621 628 var img = $("<img/>");
622 629 img[0].setAttribute('src','data:image/png;base64,'+png);
623 if (md['height']) {
624 img[0].setAttribute('height', md['height']);
625 }
626 if (md['width']) {
627 img[0].setAttribute('width', md['width']);
628 }
630 set_width_height(img[0], md, 'image/png');
629 631 this._dblclick_to_reset_size(img);
630 632 toinsert.append(img);
631 633 element.append(toinsert);
632 634 };
633 635
634 636
635 637 OutputArea.prototype.append_jpeg = function (jpeg, md, element) {
636 638 var type = 'image/jpeg';
637 639 var toinsert = this.create_output_subarea(md, "output_jpeg", type);
638 640 var img = $("<img/>").attr('src','data:image/jpeg;base64,'+jpeg);
639 if (md['height']) {
640 img.attr('height', md['height']);
641 }
642 if (md['width']) {
643 img.attr('width', md['width']);
644 }
641 set_width_height(img[0], md, 'image/jpeg');
645 642 this._dblclick_to_reset_size(img);
646 643 toinsert.append(img);
647 644 element.append(toinsert);
648 645 };
649 646
650 647
651 648 OutputArea.prototype.append_latex = function (latex, md, element) {
652 649 // This method cannot do the typesetting because the latex first has to
653 650 // be on the page.
654 651 var type = 'text/latex';
655 652 var toinsert = this.create_output_subarea(md, "output_latex", type);
656 653 toinsert.append(latex);
657 654 element.append(toinsert);
658 655 };
659 656
660 657 OutputArea.append_map = {
661 658 "text/plain" : OutputArea.prototype.append_text,
662 659 "text/html" : OutputArea.prototype.append_html,
663 660 "image/svg+xml" : OutputArea.prototype.append_svg,
664 661 "image/png" : OutputArea.prototype.append_png,
665 662 "image/jpeg" : OutputArea.prototype.append_jpeg,
666 663 "text/latex" : OutputArea.prototype.append_latex,
667 664 "application/json" : OutputArea.prototype.append_json,
668 665 "application/javascript" : OutputArea.prototype.append_javascript,
669 666 };
670 667
671 668 OutputArea.prototype.append_raw_input = function (msg) {
672 669 var that = this;
673 670 this.expand();
674 671 var content = msg.content;
675 672 var area = this.create_output_area();
676 673
677 674 // disable any other raw_inputs, if they are left around
678 675 $("div.output_subarea.raw_input").remove();
679 676
680 677 area.append(
681 678 $("<div/>")
682 679 .addClass("box-flex1 output_subarea raw_input")
683 680 .append(
684 681 $("<span/>")
685 682 .addClass("input_prompt")
686 683 .text(content.prompt)
687 684 )
688 685 .append(
689 686 $("<input/>")
690 687 .addClass("raw_input")
691 688 .attr('type', 'text')
692 689 .attr("size", 47)
693 690 .keydown(function (event, ui) {
694 691 // make sure we submit on enter,
695 692 // and don't re-execute the *cell* on shift-enter
696 693 if (event.which === utils.keycodes.ENTER) {
697 694 that._submit_raw_input();
698 695 return false;
699 696 }
700 697 })
701 698 )
702 699 );
703 700
704 701 this.element.append(area);
705 702 var raw_input = area.find('input.raw_input');
706 703 // Register events that enable/disable the keyboard manager while raw
707 704 // input is focused.
708 705 IPython.keyboard_manager.register_events(raw_input);
709 706 // Note, the following line used to read raw_input.focus().focus().
710 707 // This seemed to be needed otherwise only the cell would be focused.
711 708 // But with the modal UI, this seems to work fine with one call to focus().
712 709 raw_input.focus();
713 710 }
714 711
715 712 OutputArea.prototype._submit_raw_input = function (evt) {
716 713 var container = this.element.find("div.raw_input");
717 714 var theprompt = container.find("span.input_prompt");
718 715 var theinput = container.find("input.raw_input");
719 716 var value = theinput.val();
720 717 var content = {
721 718 output_type : 'stream',
722 719 name : 'stdout',
723 720 text : theprompt.text() + value + '\n'
724 721 }
725 722 // remove form container
726 723 container.parent().remove();
727 724 // replace with plaintext version in stdout
728 725 this.append_output(content, false);
729 726 $([IPython.events]).trigger('send_input_reply.Kernel', value);
730 727 }
731 728
732 729
733 730 OutputArea.prototype.handle_clear_output = function (msg) {
734 731 this.clear_output(msg.content.wait);
735 732 };
736 733
737 734
738 735 OutputArea.prototype.clear_output = function(wait) {
739 736 if (wait) {
740 737
741 738 // If a clear is queued, clear before adding another to the queue.
742 739 if (this.clear_queued) {
743 740 this.clear_output(false);
744 741 };
745 742
746 743 this.clear_queued = true;
747 744 } else {
748 745
749 746 // Fix the output div's height if the clear_output is waiting for
750 747 // new output (it is being used in an animation).
751 748 if (this.clear_queued) {
752 749 var height = this.element.height();
753 750 this.element.height(height);
754 751 this.clear_queued = false;
755 752 }
756 753
757 754 // clear all, no need for logic
758 755 this.element.html("");
759 756 this.outputs = [];
760 757 this.unscroll_area();
761 758 return;
762 759 };
763 760 };
764 761
765 762
766 763 // JSON serialization
767 764
768 765 OutputArea.prototype.fromJSON = function (outputs) {
769 766 var len = outputs.length;
770 767 var data;
771 768
772 769 // We don't want to display javascript on load, so remove it from the
773 770 // display order for the duration of this function call, but be sure to
774 771 // put it back in there so incoming messages that contain javascript
775 772 // representations get displayed
776 773 var js_index = OutputArea.display_order.indexOf('application/javascript');
777 774 OutputArea.display_order.splice(js_index, 1);
778 775
779 776 for (var i=0; i<len; i++) {
780 777 data = outputs[i];
781 778 var msg_type = data.output_type;
782 779 if (msg_type === "display_data" || msg_type === "pyout") {
783 780 // convert short keys to mime keys
784 781 // TODO: remove mapping of short keys when we update to nbformat 4
785 782 data = this.rename_keys(data, OutputArea.mime_map_r);
786 783 data.metadata = this.rename_keys(data.metadata, OutputArea.mime_map_r);
787 784 }
788 785
789 786 this.append_output(data);
790 787 }
791 788
792 789 // reinsert javascript into display order, see note above
793 790 OutputArea.display_order.splice(js_index, 0, 'application/javascript');
794 791 };
795 792
796 793
797 794 OutputArea.prototype.toJSON = function () {
798 795 var outputs = [];
799 796 var len = this.outputs.length;
800 797 var data;
801 798 for (var i=0; i<len; i++) {
802 799 data = this.outputs[i];
803 800 var msg_type = data.output_type;
804 801 if (msg_type === "display_data" || msg_type === "pyout") {
805 802 // convert mime keys to short keys
806 803 data = this.rename_keys(data, OutputArea.mime_map);
807 804 data.metadata = this.rename_keys(data.metadata, OutputArea.mime_map);
808 805 }
809 806 outputs[i] = data;
810 807 }
811 808 return outputs;
812 809 };
813 810
814 811
815 812 IPython.OutputArea = OutputArea;
816 813
817 814 return IPython;
818 815
819 816 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now