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