##// END OF EJS Templates
Fix scrolling output not working...
Jonathan Frederic -
Show More
@@ -1,671 +1,679
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 vbox');
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_type, content) {
235 235 var json = {};
236 236 json.output_type = msg_type;
237 237 if (msg_type === "stream") {
238 238 json.text = content.data;
239 239 json.stream = content.name;
240 240 } else if (msg_type === "display_data") {
241 241 json = this.convert_mime_types(json, content.data);
242 242 json.metadata = this.convert_mime_types({}, content.metadata);
243 243 } else if (msg_type === "pyout") {
244 244 json.prompt_number = content.execution_count;
245 245 json = this.convert_mime_types(json, content.data);
246 246 json.metadata = this.convert_mime_types({}, content.metadata);
247 247 } else if (msg_type === "pyerr") {
248 248 json.ename = content.ename;
249 249 json.evalue = content.evalue;
250 250 json.traceback = content.traceback;
251 251 }
252 252 // append with dynamic=true
253 253 this.append_output(json, true);
254 254 };
255 255
256 256
257 257 OutputArea.prototype.convert_mime_types = function (json, data) {
258 258 if (data === undefined) {
259 259 return json;
260 260 }
261 261 if (data['text/plain'] !== undefined) {
262 262 json.text = data['text/plain'];
263 263 }
264 264 if (data['text/html'] !== undefined) {
265 265 json.html = data['text/html'];
266 266 }
267 267 if (data['image/svg+xml'] !== undefined) {
268 268 json.svg = data['image/svg+xml'];
269 269 }
270 270 if (data['image/png'] !== undefined) {
271 271 json.png = data['image/png'];
272 272 }
273 273 if (data['image/jpeg'] !== undefined) {
274 274 json.jpeg = data['image/jpeg'];
275 275 }
276 276 if (data['text/latex'] !== undefined) {
277 277 json.latex = data['text/latex'];
278 278 }
279 279 if (data['application/json'] !== undefined) {
280 280 json.json = data['application/json'];
281 281 }
282 282 if (data['application/javascript'] !== undefined) {
283 283 json.javascript = data['application/javascript'];
284 284 }
285 285 return json;
286 286 };
287 287
288 288
289 289 OutputArea.prototype.append_output = function (json, dynamic) {
290 290 // If dynamic is true, javascript output will be eval'd.
291 291 this.expand();
292 292
293 293 // Clear the output if clear is queued.
294 var needs_height_reset = false;
294 295 if (this.clear_queued) {
295 296 this.clear_output(false);
297 needs_height_reset = true;
296 298 }
297 299
298 300 if (json.output_type === 'pyout') {
299 301 this.append_pyout(json, dynamic);
300 302 } else if (json.output_type === 'pyerr') {
301 303 this.append_pyerr(json);
302 304 } else if (json.output_type === 'display_data') {
303 305 this.append_display_data(json, dynamic);
304 306 } else if (json.output_type === 'stream') {
305 307 this.append_stream(json);
306 308 }
307 309 this.outputs.push(json);
308 this.element.height('auto');
310
311 // Only reset the height to automatic if the height is currently
312 // fixed (done by wait=True flag on clear_output).
313 if (needs_height_reset) {
314 this.element.height('auto');
315 }
316
309 317 var that = this;
310 318 setTimeout(function(){that.element.trigger('resize');}, 100);
311 319 };
312 320
313 321
314 322 OutputArea.prototype.create_output_area = function () {
315 323 var oa = $("<div/>").addClass("output_area");
316 324 if (this.prompt_area) {
317 325 oa.append($('<div/>').addClass('prompt'));
318 326 }
319 327 return oa;
320 328 };
321 329
322 330 OutputArea.prototype._append_javascript_error = function (err, container) {
323 331 // display a message when a javascript error occurs in display output
324 332 var msg = "Javascript error adding output!"
325 333 console.log(msg, err);
326 334 if ( container === undefined ) return;
327 335 container.append(
328 336 $('<div/>').html(msg + "<br/>" +
329 337 err.toString() +
330 338 '<br/>See your browser Javascript console for more details.'
331 339 ).addClass('js-error')
332 340 );
333 341 container.show();
334 342 };
335 343
336 344 OutputArea.prototype._safe_append = function (toinsert) {
337 345 // safely append an item to the document
338 346 // this is an object created by user code,
339 347 // and may have errors, which should not be raised
340 348 // under any circumstances.
341 349 try {
342 350 this.element.append(toinsert);
343 351 } catch(err) {
344 352 console.log(err);
345 353 this._append_javascript_error(err, this.element);
346 354 }
347 355 };
348 356
349 357
350 358 OutputArea.prototype.append_pyout = function (json, dynamic) {
351 359 var n = json.prompt_number || ' ';
352 360 var toinsert = this.create_output_area();
353 361 if (this.prompt_area) {
354 362 toinsert.find('div.prompt').addClass('output_prompt').html('Out[' + n + ']:');
355 363 }
356 364 this.append_mime_type(json, toinsert, dynamic);
357 365 this._safe_append(toinsert);
358 366 // If we just output latex, typeset it.
359 367 if ((json.latex !== undefined) || (json.html !== undefined)) {
360 368 this.typeset();
361 369 }
362 370 };
363 371
364 372
365 373 OutputArea.prototype.append_pyerr = function (json) {
366 374 var tb = json.traceback;
367 375 if (tb !== undefined && tb.length > 0) {
368 376 var s = '';
369 377 var len = tb.length;
370 378 for (var i=0; i<len; i++) {
371 379 s = s + tb[i] + '\n';
372 380 }
373 381 s = s + '\n';
374 382 var toinsert = this.create_output_area();
375 383 this.append_text(s, {}, toinsert);
376 384 this._safe_append(toinsert);
377 385 }
378 386 };
379 387
380 388
381 389 OutputArea.prototype.append_stream = function (json) {
382 390 // temporary fix: if stream undefined (json file written prior to this patch),
383 391 // default to most likely stdout:
384 392 if (json.stream == undefined){
385 393 json.stream = 'stdout';
386 394 }
387 395 var text = json.text;
388 396 var subclass = "output_"+json.stream;
389 397 if (this.outputs.length > 0){
390 398 // have at least one output to consider
391 399 var last = this.outputs[this.outputs.length-1];
392 400 if (last.output_type == 'stream' && json.stream == last.stream){
393 401 // latest output was in the same stream,
394 402 // so append directly into its pre tag
395 403 // escape ANSI & HTML specials:
396 404 var pre = this.element.find('div.'+subclass).last().find('pre');
397 405 var html = utils.fixCarriageReturn(
398 406 pre.html() + utils.fixConsole(text));
399 407 pre.html(html);
400 408 return;
401 409 }
402 410 }
403 411
404 412 if (!text.replace("\r", "")) {
405 413 // text is nothing (empty string, \r, etc.)
406 414 // so don't append any elements, which might add undesirable space
407 415 return;
408 416 }
409 417
410 418 // If we got here, attach a new div
411 419 var toinsert = this.create_output_area();
412 420 this.append_text(text, {}, toinsert, "output_stream "+subclass);
413 421 this._safe_append(toinsert);
414 422 };
415 423
416 424
417 425 OutputArea.prototype.append_display_data = function (json, dynamic) {
418 426 var toinsert = this.create_output_area();
419 427 this.append_mime_type(json, toinsert, dynamic);
420 428 this._safe_append(toinsert);
421 429 // If we just output latex, typeset it.
422 430 if ( (json.latex !== undefined) || (json.html !== undefined) ) {
423 431 this.typeset();
424 432 }
425 433 };
426 434
427 435 OutputArea.display_order = ['javascript','html','latex','svg','png','jpeg','text'];
428 436
429 437 OutputArea.prototype.append_mime_type = function (json, element, dynamic) {
430 438 for(var type_i in OutputArea.display_order){
431 439 var type = OutputArea.display_order[type_i];
432 440 if(json[type] != undefined ){
433 441 var md = {};
434 442 if (json.metadata && json.metadata[type]) {
435 443 md = json.metadata[type];
436 444 };
437 445 if(type == 'javascript'){
438 446 if (dynamic) {
439 447 this.append_javascript(json.javascript, md, element, dynamic);
440 448 }
441 449 } else {
442 450 this['append_'+type](json[type], md, element);
443 451 }
444 452 return;
445 453 }
446 454 }
447 455 };
448 456
449 457
450 458 OutputArea.prototype.append_html = function (html, md, element) {
451 459 var toinsert = $("<div/>").addClass("output_subarea output_html rendered_html");
452 460 toinsert.append(html);
453 461 element.append(toinsert);
454 462 };
455 463
456 464
457 465 OutputArea.prototype.append_javascript = function (js, md, container) {
458 466 // We just eval the JS code, element appears in the local scope.
459 467 var element = $("<div/>").addClass("output_subarea");
460 468 container.append(element);
461 469 // Div for js shouldn't be drawn, as it will add empty height to the area.
462 470 container.hide();
463 471 // If the Javascript appends content to `element` that should be drawn, then
464 472 // it must also call `container.show()`.
465 473 try {
466 474 eval(js);
467 475 } catch(err) {
468 476 this._append_javascript_error(err, container);
469 477 }
470 478 };
471 479
472 480
473 481 OutputArea.prototype.append_text = function (data, md, element, extra_class) {
474 482 var toinsert = $("<div/>").addClass("output_subarea output_text");
475 483 // escape ANSI & HTML specials in plaintext:
476 484 data = utils.fixConsole(data);
477 485 data = utils.fixCarriageReturn(data);
478 486 data = utils.autoLinkUrls(data);
479 487 if (extra_class){
480 488 toinsert.addClass(extra_class);
481 489 }
482 490 toinsert.append($("<pre/>").html(data));
483 491 element.append(toinsert);
484 492 };
485 493
486 494
487 495 OutputArea.prototype.append_svg = function (svg, md, element) {
488 496 var toinsert = $("<div/>").addClass("output_subarea output_svg");
489 497 toinsert.append(svg);
490 498 element.append(toinsert);
491 499 };
492 500
493 501
494 502 OutputArea.prototype._dblclick_to_reset_size = function (img) {
495 503 // schedule wrapping image in resizable after a delay,
496 504 // so we don't end up calling resize on a zero-size object
497 505 var that = this;
498 506 setTimeout(function () {
499 507 var h0 = img.height();
500 508 var w0 = img.width();
501 509 if (!(h0 && w0)) {
502 510 // zero size, schedule another timeout
503 511 that._dblclick_to_reset_size(img);
504 512 return;
505 513 }
506 514 img.resizable({
507 515 aspectRatio: true,
508 516 autoHide: true
509 517 });
510 518 img.dblclick(function () {
511 519 // resize wrapper & image together for some reason:
512 520 img.parent().height(h0);
513 521 img.height(h0);
514 522 img.parent().width(w0);
515 523 img.width(w0);
516 524 });
517 525 }, 250);
518 526 };
519 527
520 528
521 529 OutputArea.prototype.append_png = function (png, md, element) {
522 530 var toinsert = $("<div/>").addClass("output_subarea output_png");
523 531 var img = $("<img/>").attr('src','data:image/png;base64,'+png);
524 532 if (md['height']) {
525 533 img.attr('height', md['height']);
526 534 }
527 535 if (md['width']) {
528 536 img.attr('width', md['width']);
529 537 }
530 538 this._dblclick_to_reset_size(img);
531 539 toinsert.append(img);
532 540 element.append(toinsert);
533 541 };
534 542
535 543
536 544 OutputArea.prototype.append_jpeg = function (jpeg, md, element) {
537 545 var toinsert = $("<div/>").addClass("output_subarea output_jpeg");
538 546 var img = $("<img/>").attr('src','data:image/jpeg;base64,'+jpeg);
539 547 if (md['height']) {
540 548 img.attr('height', md['height']);
541 549 }
542 550 if (md['width']) {
543 551 img.attr('width', md['width']);
544 552 }
545 553 this._dblclick_to_reset_size(img);
546 554 toinsert.append(img);
547 555 element.append(toinsert);
548 556 };
549 557
550 558
551 559 OutputArea.prototype.append_latex = function (latex, md, element) {
552 560 // This method cannot do the typesetting because the latex first has to
553 561 // be on the page.
554 562 var toinsert = $("<div/>").addClass("output_subarea output_latex");
555 563 toinsert.append(latex);
556 564 element.append(toinsert);
557 565 };
558 566
559 567 OutputArea.prototype.append_raw_input = function (content) {
560 568 var that = this;
561 569 this.expand();
562 570 var area = this.create_output_area();
563 571
564 572 // disable any other raw_inputs, if they are left around
565 573 $("div.output_subarea.raw_input").remove();
566 574
567 575 area.append(
568 576 $("<div/>")
569 577 .addClass("box-flex1 output_subarea raw_input")
570 578 .append(
571 579 $("<span/>")
572 580 .addClass("input_prompt")
573 581 .text(content.prompt)
574 582 )
575 583 .append(
576 584 $("<input/>")
577 585 .addClass("raw_input")
578 586 .attr('type', 'text')
579 587 .attr("size", 47)
580 588 .keydown(function (event, ui) {
581 589 // make sure we submit on enter,
582 590 // and don't re-execute the *cell* on shift-enter
583 591 if (event.which === utils.keycodes.ENTER) {
584 592 that._submit_raw_input();
585 593 return false;
586 594 }
587 595 })
588 596 )
589 597 );
590 598 this.element.append(area);
591 599 // weirdly need double-focus now,
592 600 // otherwise only the cell will be focused
593 601 area.find("input.raw_input").focus().focus();
594 602 }
595 603 OutputArea.prototype._submit_raw_input = function (evt) {
596 604 var container = this.element.find("div.raw_input");
597 605 var theprompt = container.find("span.input_prompt");
598 606 var theinput = container.find("input.raw_input");
599 607 var value = theinput.val();
600 608 var content = {
601 609 output_type : 'stream',
602 610 name : 'stdout',
603 611 text : theprompt.text() + value + '\n'
604 612 }
605 613 // remove form container
606 614 container.parent().remove();
607 615 // replace with plaintext version in stdout
608 616 this.append_output(content, false);
609 617 $([IPython.events]).trigger('send_input_reply.Kernel', value);
610 618 }
611 619
612 620
613 621 OutputArea.prototype.handle_clear_output = function (content) {
614 622 this.clear_output(content.wait);
615 623 };
616 624
617 625
618 626 OutputArea.prototype.clear_output = function(wait) {
619 627 if (wait) {
620 628
621 629 // If a clear is queued, clear before adding another to the queue.
622 630 if (this.clear_queued) {
623 631 this.clear_output(false);
624 632 };
625 633
626 634 this.clear_queued = true;
627 635 } else {
628 636
629 637 // Fix the output div's height if the clear_output is waiting for
630 638 // new output (it is being used in an animation).
631 639 if (this.clear_queued) {
632 640 var height = this.element.height();
633 641 this.element.height(height);
634 642 this.clear_queued = false;
635 643 }
636 644
637 645 // clear all, no need for logic
638 646 this.element.html("");
639 647 this.outputs = [];
640 648 this.unscroll_area();
641 649 return;
642 650 };
643 651 };
644 652
645 653
646 654 // JSON serialization
647 655
648 656 OutputArea.prototype.fromJSON = function (outputs) {
649 657 var len = outputs.length;
650 658 for (var i=0; i<len; i++) {
651 659 // append with dynamic=false.
652 660 this.append_output(outputs[i], false);
653 661 }
654 662 };
655 663
656 664
657 665 OutputArea.prototype.toJSON = function () {
658 666 var outputs = [];
659 667 var len = this.outputs.length;
660 668 for (var i=0; i<len; i++) {
661 669 outputs[i] = this.outputs[i];
662 670 }
663 671 return outputs;
664 672 };
665 673
666 674
667 675 IPython.OutputArea = OutputArea;
668 676
669 677 return IPython;
670 678
671 679 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now