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