outputarea.js
681 lines
| 21.7 KiB
| application/javascript
|
JavascriptLexer
Brian Granger
|
r7177 | //---------------------------------------------------------------------------- | ||
Matthias BUSSONNIER
|
r10776 | // Copyright (C) 2008 The IPython Development Team | ||
Brian Granger
|
r7177 | // | ||
// Distributed under the terms of the BSD License. The full license is in | ||||
// the file COPYING, distributed as part of this software. | ||||
//---------------------------------------------------------------------------- | ||||
//============================================================================ | ||||
// OutputArea | ||||
//============================================================================ | ||||
Matthias BUSSONNIER
|
r10776 | /** | ||
* @module IPython | ||||
* @namespace IPython | ||||
* @submodule OutputArea | ||||
*/ | ||||
Brian Granger
|
r7177 | var IPython = (function (IPython) { | ||
"use strict"; | ||||
var utils = IPython.utils; | ||||
Matthias BUSSONNIER
|
r10776 | /** | ||
* @class OutputArea | ||||
* | ||||
* @constructor | ||||
*/ | ||||
Brian Granger
|
r7177 | var OutputArea = function (selector, prompt_area) { | ||
this.selector = selector; | ||||
MinRK
|
r7362 | this.wrapper = $(selector); | ||
Brian Granger
|
r7177 | this.outputs = []; | ||
this.collapsed = false; | ||||
MinRK
|
r7362 | this.scrolled = false; | ||
Jonathan Frederic
|
r12592 | this.clear_queued = null; | ||
Brian Granger
|
r7177 | if (prompt_area === undefined) { | ||
this.prompt_area = true; | ||||
Brian Granger
|
r7181 | } else { | ||
this.prompt_area = prompt_area; | ||||
Matthias BUSSONNIER
|
r9555 | } | ||
MinRK
|
r7362 | this.create_elements(); | ||
Brian Granger
|
r7177 | this.style(); | ||
MinRK
|
r7362 | this.bind_events(); | ||
}; | ||||
OutputArea.prototype.create_elements = function () { | ||||
this.element = $("<div/>"); | ||||
this.collapse_button = $("<div/>"); | ||||
this.prompt_overlay = $("<div/>"); | ||||
this.wrapper.append(this.prompt_overlay); | ||||
this.wrapper.append(this.element); | ||||
this.wrapper.append(this.collapse_button); | ||||
Brian Granger
|
r7177 | }; | ||
OutputArea.prototype.style = function () { | ||||
MinRK
|
r7362 | this.collapse_button.hide(); | ||
this.prompt_overlay.hide(); | ||||
this.wrapper.addClass('output_wrapper'); | ||||
Brian Granger
|
r7177 | this.element.addClass('output vbox'); | ||
MinRK
|
r7362 | |||
MinRK
|
r10897 | this.collapse_button.addClass("btn output_collapsed"); | ||
Harry Moreno
|
r9903 | this.collapse_button.attr('title', 'click to expand output'); | ||
MinRK
|
r7362 | this.collapse_button.html('. . .'); | ||
this.prompt_overlay.addClass('out_prompt_overlay prompt'); | ||||
Harry Moreno
|
r9903 | this.prompt_overlay.attr('title', 'click to expand output; double click to hide output'); | ||
MinRK
|
r7362 | |||
Brian Granger
|
r7177 | this.collapse(); | ||
}; | ||||
Matthias BUSSONNIER
|
r10776 | /** | ||
* Should the OutputArea scroll? | ||||
* Returns whether the height (in lines) exceeds a threshold. | ||||
* | ||||
* @private | ||||
* @method _should_scroll | ||||
* @param [lines=100]{Integer} | ||||
* @return {Bool} | ||||
* | ||||
*/ | ||||
MinRK
|
r7362 | OutputArea.prototype._should_scroll = function (lines) { | ||
Matthias BUSSONNIER
|
r10776 | if (lines <=0 ){ return } | ||
MinRK
|
r7362 | if (!lines) { | ||
MinRK
|
r7728 | lines = 100; | ||
MinRK
|
r7362 | } | ||
// line-height from http://stackoverflow.com/questions/1185151 | ||||
var fontSize = this.element.css('font-size'); | ||||
var lineHeight = Math.floor(parseInt(fontSize.replace('px','')) * 1.5); | ||||
return (this.element.height() > lines * lineHeight); | ||||
}; | ||||
OutputArea.prototype.bind_events = function () { | ||||
var that = this; | ||||
this.prompt_overlay.dblclick(function () { that.toggle_output(); }); | ||||
this.prompt_overlay.click(function () { that.toggle_scroll(); }); | ||||
this.element.resize(function () { | ||||
MinRK
|
r7733 | // FIXME: Firefox on Linux misbehaves, so automatic scrolling is disabled | ||
Brian E. Granger
|
r9227 | if ( IPython.utils.browser[0] === "Firefox" ) { | ||
MinRK
|
r7733 | return; | ||
} | ||||
MinRK
|
r7362 | // maybe scroll output, | ||
// if it's grown large enough and hasn't already been scrolled. | ||||
Matthias BUSSONNIER
|
r10776 | if ( !that.scrolled && that._should_scroll(OutputArea.auto_scroll_threshold)) { | ||
MinRK
|
r7362 | that.scroll_area(); | ||
} | ||||
}); | ||||
this.collapse_button.click(function () { | ||||
that.expand(); | ||||
}); | ||||
}; | ||||
Brian Granger
|
r7177 | OutputArea.prototype.collapse = function () { | ||
if (!this.collapsed) { | ||||
this.element.hide(); | ||||
MinRK
|
r7362 | this.prompt_overlay.hide(); | ||
if (this.element.html()){ | ||||
this.collapse_button.show(); | ||||
} | ||||
Brian Granger
|
r7177 | this.collapsed = true; | ||
Matthias BUSSONNIER
|
r9555 | } | ||
Brian Granger
|
r7177 | }; | ||
OutputArea.prototype.expand = function () { | ||||
if (this.collapsed) { | ||||
MinRK
|
r7362 | this.collapse_button.hide(); | ||
Brian Granger
|
r7177 | this.element.show(); | ||
MinRK
|
r7362 | this.prompt_overlay.show(); | ||
Brian Granger
|
r7177 | this.collapsed = false; | ||
Matthias BUSSONNIER
|
r9555 | } | ||
Brian Granger
|
r7177 | }; | ||
OutputArea.prototype.toggle_output = function () { | ||||
if (this.collapsed) { | ||||
this.expand(); | ||||
} else { | ||||
this.collapse(); | ||||
Matthias BUSSONNIER
|
r9555 | } | ||
Brian Granger
|
r7177 | }; | ||
MinRK
|
r7362 | OutputArea.prototype.scroll_area = function () { | ||
this.element.addClass('output_scroll'); | ||||
MinRK
|
r7423 | this.prompt_overlay.attr('title', 'click to unscroll output; double click to hide'); | ||
MinRK
|
r7362 | this.scrolled = true; | ||
}; | ||||
OutputArea.prototype.unscroll_area = function () { | ||||
this.element.removeClass('output_scroll'); | ||||
MinRK
|
r7423 | this.prompt_overlay.attr('title', 'click to scroll output; double click to hide'); | ||
MinRK
|
r7362 | this.scrolled = false; | ||
}; | ||||
Matthias BUSSONNIER
|
r10776 | /** | ||
* Threshold to trigger autoscroll when the OutputArea is resized, | ||||
* typically when new outputs are added. | ||||
* | ||||
Matthias BUSSONNIER
|
r10782 | * Behavior is undefined if autoscroll is lower than minimum_scroll_threshold, | ||
Matthias BUSSONNIER
|
r10778 | * unless it is < 0, in which case autoscroll will never be triggered | ||
Matthias BUSSONNIER
|
r10776 | * | ||
* @property auto_scroll_threshold | ||||
* @type Number | ||||
Matthias Bussonnier
|
r10812 | * @default 100 | ||
Matthias BUSSONNIER
|
r10776 | * | ||
**/ | ||||
Matthias Bussonnier
|
r10812 | OutputArea.auto_scroll_threshold = 100; | ||
Matthias BUSSONNIER
|
r10776 | |||
/** | ||||
Matthias BUSSONNIER
|
r10778 | * Lower limit (in lines) for OutputArea to be made scrollable. OutputAreas | ||
* shorter than this are never scrolled. | ||||
Matthias BUSSONNIER
|
r10776 | * | ||
Matthias BUSSONNIER
|
r10782 | * @property minimum_scroll_threshold | ||
Matthias BUSSONNIER
|
r10776 | * @type Number | ||
* @default 20 | ||||
* | ||||
**/ | ||||
Matthias BUSSONNIER
|
r10782 | OutputArea.minimum_scroll_threshold = 20; | ||
Matthias BUSSONNIER
|
r10776 | |||
/** | ||||
* | ||||
Matthias BUSSONNIER
|
r10778 | * Scroll OutputArea if height supperior than a threshold (in lines). | ||
Matthias BUSSONNIER
|
r10776 | * | ||
Matthias BUSSONNIER
|
r10778 | * Threshold is a maximum number of lines. If unspecified, defaults to | ||
Matthias BUSSONNIER
|
r10782 | * OutputArea.minimum_scroll_threshold. | ||
Matthias BUSSONNIER
|
r10778 | * | ||
Matthias BUSSONNIER
|
r10793 | * Negative threshold will prevent the OutputArea from ever scrolling. | ||
Matthias BUSSONNIER
|
r10776 | * | ||
* @method scroll_if_long | ||||
Matthias BUSSONNIER
|
r10793 | * | ||
* @param [lines=20]{Number} Default to 20 if not set, | ||||
* behavior undefined for value of `0`. | ||||
Matthias BUSSONNIER
|
r10776 | * | ||
**/ | ||||
MinRK
|
r7362 | OutputArea.prototype.scroll_if_long = function (lines) { | ||
Matthias BUSSONNIER
|
r10782 | var n = lines | OutputArea.minimum_scroll_threshold; | ||
Matthias BUSSONNIER
|
r10776 | if(n <= 0){ | ||
return | ||||
} | ||||
if (this._should_scroll(n)) { | ||||
MinRK
|
r7362 | // only allow scrolling long-enough output | ||
this.scroll_area(); | ||||
Matthias BUSSONNIER
|
r9555 | } | ||
MinRK
|
r7362 | }; | ||
OutputArea.prototype.toggle_scroll = function () { | ||||
if (this.scrolled) { | ||||
this.unscroll_area(); | ||||
} else { | ||||
// only allow scrolling long-enough output | ||||
Matthias BUSSONNIER
|
r10776 | this.scroll_if_long(); | ||
Matthias BUSSONNIER
|
r9555 | } | ||
MinRK
|
r7362 | }; | ||
Brian Granger
|
r7177 | // typeset with MathJax if MathJax is available | ||
OutputArea.prototype.typeset = function () { | ||||
if (window.MathJax){ | ||||
MathJax.Hub.Queue(["Typeset",MathJax.Hub]); | ||||
} | ||||
}; | ||||
MinRK
|
r13207 | OutputArea.prototype.handle_output = function (msg) { | ||
Brian Granger
|
r7177 | var json = {}; | ||
MinRK
|
r13207 | var msg_type = json.output_type = msg.header.msg_type; | ||
var content = msg.content; | ||||
Brian Granger
|
r7177 | if (msg_type === "stream") { | ||
json.text = content.data; | ||||
json.stream = content.name; | ||||
} else if (msg_type === "display_data") { | ||||
json = this.convert_mime_types(json, content.data); | ||||
MinRK
|
r10445 | json.metadata = this.convert_mime_types({}, content.metadata); | ||
Brian Granger
|
r7177 | } else if (msg_type === "pyout") { | ||
json.prompt_number = content.execution_count; | ||||
json = this.convert_mime_types(json, content.data); | ||||
MinRK
|
r10445 | json.metadata = this.convert_mime_types({}, content.metadata); | ||
Brian Granger
|
r7177 | } else if (msg_type === "pyerr") { | ||
json.ename = content.ename; | ||||
json.evalue = content.evalue; | ||||
json.traceback = content.traceback; | ||||
Matthias BUSSONNIER
|
r9555 | } | ||
Brian Granger
|
r7177 | // append with dynamic=true | ||
this.append_output(json, true); | ||||
}; | ||||
OutputArea.prototype.convert_mime_types = function (json, data) { | ||||
Stefan Karpinski
|
r11673 | if (data === undefined) { | ||
return json; | ||||
} | ||||
Brian Granger
|
r7177 | if (data['text/plain'] !== undefined) { | ||
json.text = data['text/plain']; | ||||
Matthias BUSSONNIER
|
r9555 | } | ||
Brian Granger
|
r7177 | if (data['text/html'] !== undefined) { | ||
json.html = data['text/html']; | ||||
Matthias BUSSONNIER
|
r9555 | } | ||
Brian Granger
|
r7177 | if (data['image/svg+xml'] !== undefined) { | ||
json.svg = data['image/svg+xml']; | ||||
Matthias BUSSONNIER
|
r9555 | } | ||
Brian Granger
|
r7177 | if (data['image/png'] !== undefined) { | ||
json.png = data['image/png']; | ||||
Matthias BUSSONNIER
|
r9555 | } | ||
Brian Granger
|
r7177 | if (data['image/jpeg'] !== undefined) { | ||
json.jpeg = data['image/jpeg']; | ||||
Matthias BUSSONNIER
|
r9555 | } | ||
Brian Granger
|
r7177 | if (data['text/latex'] !== undefined) { | ||
json.latex = data['text/latex']; | ||||
Matthias BUSSONNIER
|
r9555 | } | ||
Brian Granger
|
r7177 | if (data['application/json'] !== undefined) { | ||
json.json = data['application/json']; | ||||
Matthias BUSSONNIER
|
r9555 | } | ||
Brian Granger
|
r7177 | if (data['application/javascript'] !== undefined) { | ||
json.javascript = data['application/javascript']; | ||||
} | ||||
return json; | ||||
}; | ||||
OutputArea.prototype.append_output = function (json, dynamic) { | ||||
// If dynamic is true, javascript output will be eval'd. | ||||
this.expand(); | ||||
Jonathan Frederic
|
r12592 | |||
// Clear the output if clear is queued. | ||||
Jonathan Frederic
|
r12817 | var needs_height_reset = false; | ||
Jonathan Frederic
|
r12592 | if (this.clear_queued) { | ||
this.clear_output(false); | ||||
Jonathan Frederic
|
r12817 | needs_height_reset = true; | ||
Jonathan Frederic
|
r12592 | } | ||
Brian Granger
|
r7177 | if (json.output_type === 'pyout') { | ||
this.append_pyout(json, dynamic); | ||||
} else if (json.output_type === 'pyerr') { | ||||
this.append_pyerr(json); | ||||
} else if (json.output_type === 'display_data') { | ||||
this.append_display_data(json, dynamic); | ||||
} else if (json.output_type === 'stream') { | ||||
this.append_stream(json); | ||||
Matthias BUSSONNIER
|
r9555 | } | ||
Brian Granger
|
r7177 | this.outputs.push(json); | ||
Jonathan Frederic
|
r12817 | |||
// Only reset the height to automatic if the height is currently | ||||
// fixed (done by wait=True flag on clear_output). | ||||
if (needs_height_reset) { | ||||
Jonathan Frederic
|
r12878 | this.element.height(''); | ||
Jonathan Frederic
|
r12817 | } | ||
MinRK
|
r7362 | var that = this; | ||
setTimeout(function(){that.element.trigger('resize');}, 100); | ||||
Brian Granger
|
r7177 | }; | ||
OutputArea.prototype.create_output_area = function () { | ||||
Matthias BUSSONNIER
|
r10216 | var oa = $("<div/>").addClass("output_area"); | ||
Brian Granger
|
r7177 | if (this.prompt_area) { | ||
oa.append($('<div/>').addClass('prompt')); | ||||
} | ||||
return oa; | ||||
}; | ||||
MinRK
|
r12332 | |||
OutputArea.prototype._append_javascript_error = function (err, container) { | ||||
// display a message when a javascript error occurs in display output | ||||
var msg = "Javascript error adding output!" | ||||
console.log(msg, err); | ||||
if ( container === undefined ) return; | ||||
container.append( | ||||
$('<div/>').html(msg + "<br/>" + | ||||
err.toString() + | ||||
'<br/>See your browser Javascript console for more details.' | ||||
).addClass('js-error') | ||||
); | ||||
container.show(); | ||||
}; | ||||
OutputArea.prototype._safe_append = function (toinsert) { | ||||
// safely append an item to the document | ||||
// this is an object created by user code, | ||||
// and may have errors, which should not be raised | ||||
// under any circumstances. | ||||
try { | ||||
this.element.append(toinsert); | ||||
} catch(err) { | ||||
console.log(err); | ||||
this._append_javascript_error(err, this.element); | ||||
} | ||||
}; | ||||
Brian Granger
|
r7177 | |||
OutputArea.prototype.append_pyout = function (json, dynamic) { | ||||
var n = json.prompt_number || ' '; | ||||
var toinsert = this.create_output_area(); | ||||
if (this.prompt_area) { | ||||
toinsert.find('div.prompt').addClass('output_prompt').html('Out[' + n + ']:'); | ||||
} | ||||
this.append_mime_type(json, toinsert, dynamic); | ||||
MinRK
|
r12332 | this._safe_append(toinsert); | ||
Brian Granger
|
r7177 | // If we just output latex, typeset it. | ||
if ((json.latex !== undefined) || (json.html !== undefined)) { | ||||
this.typeset(); | ||||
Matthias BUSSONNIER
|
r9555 | } | ||
Brian Granger
|
r7177 | }; | ||
OutputArea.prototype.append_pyerr = function (json) { | ||||
var tb = json.traceback; | ||||
if (tb !== undefined && tb.length > 0) { | ||||
var s = ''; | ||||
var len = tb.length; | ||||
for (var i=0; i<len; i++) { | ||||
s = s + tb[i] + '\n'; | ||||
} | ||||
s = s + '\n'; | ||||
var toinsert = this.create_output_area(); | ||||
MinRK
|
r10445 | this.append_text(s, {}, toinsert); | ||
MinRK
|
r12332 | this._safe_append(toinsert); | ||
Matthias BUSSONNIER
|
r9555 | } | ||
Brian Granger
|
r7177 | }; | ||
OutputArea.prototype.append_stream = function (json) { | ||||
// temporary fix: if stream undefined (json file written prior to this patch), | ||||
// default to most likely stdout: | ||||
if (json.stream == undefined){ | ||||
json.stream = 'stdout'; | ||||
} | ||||
Michael Droettboom
|
r7350 | var text = json.text; | ||
Brian Granger
|
r7177 | var subclass = "output_"+json.stream; | ||
if (this.outputs.length > 0){ | ||||
// have at least one output to consider | ||||
var last = this.outputs[this.outputs.length-1]; | ||||
if (last.output_type == 'stream' && json.stream == last.stream){ | ||||
// latest output was in the same stream, | ||||
// so append directly into its pre tag | ||||
// escape ANSI & HTML specials: | ||||
Michael Droettboom
|
r7350 | var pre = this.element.find('div.'+subclass).last().find('pre'); | ||
var html = utils.fixCarriageReturn( | ||||
Michael Droettboom
|
r7339 | pre.html() + utils.fixConsole(text)); | ||
pre.html(html); | ||||
Brian Granger
|
r7177 | return; | ||
} | ||||
} | ||||
Michael Droettboom
|
r7339 | |||
if (!text.replace("\r", "")) { | ||||
// text is nothing (empty string, \r, etc.) | ||||
// so don't append any elements, which might add undesirable space | ||||
return; | ||||
} | ||||
Brian Granger
|
r7177 | // If we got here, attach a new div | ||
var toinsert = this.create_output_area(); | ||||
MinRK
|
r10445 | this.append_text(text, {}, toinsert, "output_stream "+subclass); | ||
MinRK
|
r12332 | this._safe_append(toinsert); | ||
Brian Granger
|
r7177 | }; | ||
OutputArea.prototype.append_display_data = function (json, dynamic) { | ||||
var toinsert = this.create_output_area(); | ||||
this.append_mime_type(json, toinsert, dynamic); | ||||
MinRK
|
r12332 | this._safe_append(toinsert); | ||
Brian Granger
|
r7177 | // If we just output latex, typeset it. | ||
if ( (json.latex !== undefined) || (json.html !== undefined) ) { | ||||
this.typeset(); | ||||
Matthias BUSSONNIER
|
r9555 | } | ||
Brian Granger
|
r7177 | }; | ||
Brian E. Granger
|
r10398 | OutputArea.display_order = ['javascript','html','latex','svg','png','jpeg','text']; | ||
Brian Granger
|
r7177 | |||
OutputArea.prototype.append_mime_type = function (json, element, dynamic) { | ||||
Matthias BUSSONNIER
|
r9540 | for(var type_i in OutputArea.display_order){ | ||
var type = OutputArea.display_order[type_i]; | ||||
if(json[type] != undefined ){ | ||||
MinRK
|
r10445 | var md = {}; | ||
if (json.metadata && json.metadata[type]) { | ||||
md = json.metadata[type]; | ||||
}; | ||||
MinRK
|
r9715 | if(type == 'javascript'){ | ||
if (dynamic) { | ||||
MinRK
|
r10445 | this.append_javascript(json.javascript, md, element, dynamic); | ||
MinRK
|
r9715 | } | ||
Matthias BUSSONNIER
|
r9553 | } else { | ||
MinRK
|
r10445 | this['append_'+type](json[type], md, element); | ||
Matthias BUSSONNIER
|
r9540 | } | ||
Matthias BUSSONNIER
|
r9555 | return; | ||
Matthias BUSSONNIER
|
r9540 | } | ||
} | ||||
Brian Granger
|
r7177 | }; | ||
MinRK
|
r10445 | OutputArea.prototype.append_html = function (html, md, element) { | ||
Matthias BUSSONNIER
|
r10218 | var toinsert = $("<div/>").addClass("output_subarea output_html rendered_html"); | ||
Brian Granger
|
r7177 | toinsert.append(html); | ||
element.append(toinsert); | ||||
}; | ||||
MinRK
|
r10445 | OutputArea.prototype.append_javascript = function (js, md, container) { | ||
Brian Granger
|
r7177 | // We just eval the JS code, element appears in the local scope. | ||
Matthias BUSSONNIER
|
r10218 | var element = $("<div/>").addClass("output_subarea"); | ||
Brian Granger
|
r7177 | container.append(element); | ||
// Div for js shouldn't be drawn, as it will add empty height to the area. | ||||
container.hide(); | ||||
// If the Javascript appends content to `element` that should be drawn, then | ||||
// it must also call `container.show()`. | ||||
Matthias BUSSONNIER
|
r8071 | try { | ||
eval(js); | ||||
} catch(err) { | ||||
MinRK
|
r12332 | this._append_javascript_error(err, container); | ||
Matthias BUSSONNIER
|
r8071 | } | ||
Matthias BUSSONNIER
|
r9555 | }; | ||
Brian Granger
|
r7177 | |||
MinRK
|
r10445 | OutputArea.prototype.append_text = function (data, md, element, extra_class) { | ||
Matthias BUSSONNIER
|
r10218 | var toinsert = $("<div/>").addClass("output_subarea output_text"); | ||
Brian Granger
|
r7177 | // escape ANSI & HTML specials in plaintext: | ||
data = utils.fixConsole(data); | ||||
Michael Droettboom
|
r7339 | data = utils.fixCarriageReturn(data); | ||
Erik M. Bray
|
r8528 | data = utils.autoLinkUrls(data); | ||
Brian Granger
|
r7177 | if (extra_class){ | ||
toinsert.addClass(extra_class); | ||||
} | ||||
toinsert.append($("<pre/>").html(data)); | ||||
element.append(toinsert); | ||||
}; | ||||
MinRK
|
r10445 | OutputArea.prototype.append_svg = function (svg, md, element) { | ||
Matthias BUSSONNIER
|
r10218 | var toinsert = $("<div/>").addClass("output_subarea output_svg"); | ||
Brian Granger
|
r7355 | toinsert.append(svg); | ||
element.append(toinsert); | ||||
Brian Granger
|
r7177 | }; | ||
MinRK
|
r7601 | OutputArea.prototype._dblclick_to_reset_size = function (img) { | ||
// schedule wrapping image in resizable after a delay, | ||||
// so we don't end up calling resize on a zero-size object | ||||
var that = this; | ||||
MinRK
|
r7599 | setTimeout(function () { | ||
MinRK
|
r7601 | var h0 = img.height(); | ||
var w0 = img.width(); | ||||
if (!(h0 && w0)) { | ||||
// zero size, schedule another timeout | ||||
that._dblclick_to_reset_size(img); | ||||
Matthias BUSSONNIER
|
r9555 | return; | ||
MinRK
|
r7601 | } | ||
MinRK
|
r7599 | img.resizable({ | ||
aspectRatio: true, | ||||
MinRK
|
r7601 | autoHide: true | ||
}); | ||||
img.dblclick(function () { | ||||
// resize wrapper & image together for some reason: | ||||
img.parent().height(h0); | ||||
img.height(h0); | ||||
img.parent().width(w0); | ||||
img.width(w0); | ||||
MinRK
|
r7599 | }); | ||
}, 250); | ||||
Matthias BUSSONNIER
|
r9555 | }; | ||
MinRK
|
r7601 | |||
MinRK
|
r10445 | OutputArea.prototype.append_png = function (png, md, element) { | ||
Matthias BUSSONNIER
|
r10218 | var toinsert = $("<div/>").addClass("output_subarea output_png"); | ||
Brian Granger
|
r7352 | var img = $("<img/>").attr('src','data:image/png;base64,'+png); | ||
MinRK
|
r10445 | if (md['height']) { | ||
img.attr('height', md['height']); | ||||
} | ||||
if (md['width']) { | ||||
img.attr('width', md['width']); | ||||
} | ||||
MinRK
|
r7601 | this._dblclick_to_reset_size(img); | ||
Brian Granger
|
r7352 | toinsert.append(img); | ||
Brian Granger
|
r7177 | element.append(toinsert); | ||
}; | ||||
MinRK
|
r10445 | OutputArea.prototype.append_jpeg = function (jpeg, md, element) { | ||
Matthias BUSSONNIER
|
r10218 | var toinsert = $("<div/>").addClass("output_subarea output_jpeg"); | ||
Brian Granger
|
r7352 | var img = $("<img/>").attr('src','data:image/jpeg;base64,'+jpeg); | ||
MinRK
|
r10445 | if (md['height']) { | ||
img.attr('height', md['height']); | ||||
} | ||||
if (md['width']) { | ||||
img.attr('width', md['width']); | ||||
} | ||||
MinRK
|
r7601 | this._dblclick_to_reset_size(img); | ||
Brian Granger
|
r7352 | toinsert.append(img); | ||
Brian Granger
|
r7177 | element.append(toinsert); | ||
}; | ||||
MinRK
|
r10445 | OutputArea.prototype.append_latex = function (latex, md, element) { | ||
Brian Granger
|
r7177 | // This method cannot do the typesetting because the latex first has to | ||
// be on the page. | ||||
Matthias BUSSONNIER
|
r10218 | var toinsert = $("<div/>").addClass("output_subarea output_latex"); | ||
Brian Granger
|
r7177 | toinsert.append(latex); | ||
element.append(toinsert); | ||||
}; | ||||
Matthias BUSSONNIER
|
r10776 | |||
MinRK
|
r13207 | OutputArea.prototype.append_raw_input = function (msg) { | ||
MinRK
|
r10370 | var that = this; | ||
MinRK
|
r10368 | this.expand(); | ||
MinRK
|
r13207 | var content = msg.content; | ||
MinRK
|
r10368 | var area = this.create_output_area(); | ||
MinRK
|
r11612 | |||
// disable any other raw_inputs, if they are left around | ||||
$("div.output_subarea.raw_input").remove(); | ||||
MinRK
|
r10368 | area.append( | ||
$("<div/>") | ||||
.addClass("box-flex1 output_subarea raw_input") | ||||
.append( | ||||
MinRK
|
r10370 | $("<span/>") | ||
.addClass("input_prompt") | ||||
.text(content.prompt) | ||||
) | ||||
.append( | ||||
$("<input/>") | ||||
.addClass("raw_input") | ||||
.attr('type', 'text') | ||||
MinRK
|
r10934 | .attr("size", 47) | ||
MinRK
|
r10370 | .keydown(function (event, ui) { | ||
// make sure we submit on enter, | ||||
// and don't re-execute the *cell* on shift-enter | ||||
if (event.which === utils.keycodes.ENTER) { | ||||
that._submit_raw_input(); | ||||
return false; | ||||
} | ||||
}) | ||||
MinRK
|
r10368 | ) | ||
MinRK
|
r10370 | ); | ||
MinRK
|
r10368 | this.element.append(area); | ||
MinRK
|
r10934 | // weirdly need double-focus now, | ||
// otherwise only the cell will be focused | ||||
area.find("input.raw_input").focus().focus(); | ||||
MinRK
|
r10368 | } | ||
OutputArea.prototype._submit_raw_input = function (evt) { | ||||
var container = this.element.find("div.raw_input"); | ||||
var theprompt = container.find("span.input_prompt"); | ||||
var theinput = container.find("input.raw_input"); | ||||
MinRK
|
r10934 | var value = theinput.val(); | ||
MinRK
|
r10368 | var content = { | ||
output_type : 'stream', | ||||
name : 'stdout', | ||||
text : theprompt.text() + value + '\n' | ||||
} | ||||
// remove form container | ||||
container.parent().remove(); | ||||
// replace with plaintext version in stdout | ||||
this.append_output(content, false); | ||||
$([IPython.events]).trigger('send_input_reply.Kernel', value); | ||||
} | ||||
Brian Granger
|
r7177 | |||
MinRK
|
r13207 | OutputArea.prototype.handle_clear_output = function (msg) { | ||
this.clear_output(msg.content.wait); | ||||
Matthias BUSSONNIER
|
r9555 | }; | ||
Brian Granger
|
r7177 | |||
Jonathan Frederic
|
r12592 | OutputArea.prototype.clear_output = function(wait) { | ||
if (wait) { | ||||
Jonathan Frederic
|
r12582 | |||
Jonathan Frederic
|
r12592 | // If a clear is queued, clear before adding another to the queue. | ||
if (this.clear_queued) { | ||||
this.clear_output(false); | ||||
}; | ||||
this.clear_queued = true; | ||||
} else { | ||||
Jonathan Frederic
|
r12606 | // Fix the output div's height if the clear_output is waiting for | ||
// new output (it is being used in an animation). | ||||
if (this.clear_queued) { | ||||
var height = this.element.height(); | ||||
this.element.height(height); | ||||
this.clear_queued = false; | ||||
} | ||||
Jonathan Frederic
|
r12592 | // clear all, no need for logic | ||
this.element.html(""); | ||||
this.outputs = []; | ||||
this.unscroll_area(); | ||||
return; | ||||
}; | ||||
Brian Granger
|
r7177 | }; | ||
// JSON serialization | ||||
OutputArea.prototype.fromJSON = function (outputs) { | ||||
var len = outputs.length; | ||||
for (var i=0; i<len; i++) { | ||||
// append with dynamic=false. | ||||
this.append_output(outputs[i], false); | ||||
Matthias BUSSONNIER
|
r9555 | } | ||
Brian Granger
|
r7177 | }; | ||
OutputArea.prototype.toJSON = function () { | ||||
var outputs = []; | ||||
var len = this.outputs.length; | ||||
for (var i=0; i<len; i++) { | ||||
outputs[i] = this.outputs[i]; | ||||
Matthias BUSSONNIER
|
r9555 | } | ||
Brian Granger
|
r7177 | return outputs; | ||
}; | ||||
IPython.OutputArea = OutputArea; | ||||
return IPython; | ||||
}(IPython)); | ||||