##// END OF EJS Templates
Make output widget reactive.
Jonathan Frederic -
Show More
@@ -1,932 +1,939
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
404 // Notify others of changes.
405 this.element.trigger('changed');
403 };
406 };
404
407
405
408
406 OutputArea.prototype.append_execute_result = function (json) {
409 OutputArea.prototype.append_execute_result = function (json) {
407 var n = json.execution_count || ' ';
410 var n = json.execution_count || ' ';
408 var toinsert = this.create_output_area();
411 var toinsert = this.create_output_area();
409 if (this.prompt_area) {
412 if (this.prompt_area) {
410 toinsert.find('div.prompt').addClass('output_prompt').text('Out[' + n + ']:');
413 toinsert.find('div.prompt').addClass('output_prompt').text('Out[' + n + ']:');
411 }
414 }
412 var inserted = this.append_mime_type(json, toinsert);
415 var inserted = this.append_mime_type(json, toinsert);
413 if (inserted) {
416 if (inserted) {
414 inserted.addClass('output_result');
417 inserted.addClass('output_result');
415 }
418 }
416 this._safe_append(toinsert);
419 this._safe_append(toinsert);
417 // If we just output latex, typeset it.
420 // If we just output latex, typeset it.
418 if ((json['text/latex'] !== undefined) ||
421 if ((json['text/latex'] !== undefined) ||
419 (json['text/html'] !== undefined) ||
422 (json['text/html'] !== undefined) ||
420 (json['text/markdown'] !== undefined)) {
423 (json['text/markdown'] !== undefined)) {
421 this.typeset();
424 this.typeset();
422 }
425 }
423 };
426 };
424
427
425
428
426 OutputArea.prototype.append_error = function (json) {
429 OutputArea.prototype.append_error = function (json) {
427 var tb = json.traceback;
430 var tb = json.traceback;
428 if (tb !== undefined && tb.length > 0) {
431 if (tb !== undefined && tb.length > 0) {
429 var s = '';
432 var s = '';
430 var len = tb.length;
433 var len = tb.length;
431 for (var i=0; i<len; i++) {
434 for (var i=0; i<len; i++) {
432 s = s + tb[i] + '\n';
435 s = s + tb[i] + '\n';
433 }
436 }
434 s = s + '\n';
437 s = s + '\n';
435 var toinsert = this.create_output_area();
438 var toinsert = this.create_output_area();
436 var append_text = OutputArea.append_map['text/plain'];
439 var append_text = OutputArea.append_map['text/plain'];
437 if (append_text) {
440 if (append_text) {
438 append_text.apply(this, [s, {}, toinsert]).addClass('output_error');
441 append_text.apply(this, [s, {}, toinsert]).addClass('output_error');
439 }
442 }
440 this._safe_append(toinsert);
443 this._safe_append(toinsert);
441 }
444 }
442 };
445 };
443
446
444
447
445 OutputArea.prototype.append_stream = function (json) {
448 OutputArea.prototype.append_stream = function (json) {
446 var text = json.text;
449 var text = json.text;
447 var subclass = "output_"+json.name;
450 var subclass = "output_"+json.name;
448 if (this.outputs.length > 0){
451 if (this.outputs.length > 0){
449 // have at least one output to consider
452 // have at least one output to consider
450 var last = this.outputs[this.outputs.length-1];
453 var last = this.outputs[this.outputs.length-1];
451 if (last.output_type == 'stream' && json.name == last.name){
454 if (last.output_type == 'stream' && json.name == last.name){
452 // latest output was in the same stream,
455 // latest output was in the same stream,
453 // so append directly into its pre tag
456 // so append directly into its pre tag
454 // escape ANSI & HTML specials:
457 // escape ANSI & HTML specials:
455 last.text = utils.fixCarriageReturn(last.text + json.text);
458 last.text = utils.fixCarriageReturn(last.text + json.text);
456 var pre = this.element.find('div.'+subclass).last().find('pre');
459 var pre = this.element.find('div.'+subclass).last().find('pre');
457 var html = utils.fixConsole(last.text);
460 var html = utils.fixConsole(last.text);
458 // The only user content injected with this HTML call is
461 // The only user content injected with this HTML call is
459 // escaped by the fixConsole() method.
462 // escaped by the fixConsole() method.
460 pre.html(html);
463 pre.html(html);
461 // 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,
462 // and the new output shouldn't be recorded.
465 // and the new output shouldn't be recorded.
463 return false;
466 return false;
464 }
467 }
465 }
468 }
466
469
467 if (!text.replace("\r", "")) {
470 if (!text.replace("\r", "")) {
468 // text is nothing (empty string, \r, etc.)
471 // text is nothing (empty string, \r, etc.)
469 // so don't append any elements, which might add undesirable space
472 // so don't append any elements, which might add undesirable space
470 // return true to indicate the output should be recorded.
473 // return true to indicate the output should be recorded.
471 return true;
474 return true;
472 }
475 }
473
476
474 // If we got here, attach a new div
477 // If we got here, attach a new div
475 var toinsert = this.create_output_area();
478 var toinsert = this.create_output_area();
476 var append_text = OutputArea.append_map['text/plain'];
479 var append_text = OutputArea.append_map['text/plain'];
477 if (append_text) {
480 if (append_text) {
478 append_text.apply(this, [text, {}, toinsert]).addClass("output_stream " + subclass);
481 append_text.apply(this, [text, {}, toinsert]).addClass("output_stream " + subclass);
479 }
482 }
480 this._safe_append(toinsert);
483 this._safe_append(toinsert);
481 return true;
484 return true;
482 };
485 };
483
486
484
487
485 OutputArea.prototype.append_display_data = function (json, handle_inserted) {
488 OutputArea.prototype.append_display_data = function (json, handle_inserted) {
486 var toinsert = this.create_output_area();
489 var toinsert = this.create_output_area();
487 if (this.append_mime_type(json, toinsert, handle_inserted)) {
490 if (this.append_mime_type(json, toinsert, handle_inserted)) {
488 this._safe_append(toinsert);
491 this._safe_append(toinsert);
489 // If we just output latex, typeset it.
492 // If we just output latex, typeset it.
490 if ((json['text/latex'] !== undefined) ||
493 if ((json['text/latex'] !== undefined) ||
491 (json['text/html'] !== undefined) ||
494 (json['text/html'] !== undefined) ||
492 (json['text/markdown'] !== undefined)) {
495 (json['text/markdown'] !== undefined)) {
493 this.typeset();
496 this.typeset();
494 }
497 }
495 }
498 }
496 };
499 };
497
500
498
501
499 OutputArea.safe_outputs = {
502 OutputArea.safe_outputs = {
500 'text/plain' : true,
503 'text/plain' : true,
501 'text/latex' : true,
504 'text/latex' : true,
502 'image/png' : true,
505 'image/png' : true,
503 'image/jpeg' : true
506 'image/jpeg' : true
504 };
507 };
505
508
506 OutputArea.prototype.append_mime_type = function (json, element, handle_inserted) {
509 OutputArea.prototype.append_mime_type = function (json, element, handle_inserted) {
507 for (var i=0; i < OutputArea.display_order.length; i++) {
510 for (var i=0; i < OutputArea.display_order.length; i++) {
508 var type = OutputArea.display_order[i];
511 var type = OutputArea.display_order[i];
509 var append = OutputArea.append_map[type];
512 var append = OutputArea.append_map[type];
510 if ((json.data[type] !== undefined) && append) {
513 if ((json.data[type] !== undefined) && append) {
511 var value = json.data[type];
514 var value = json.data[type];
512 if (!this.trusted && !OutputArea.safe_outputs[type]) {
515 if (!this.trusted && !OutputArea.safe_outputs[type]) {
513 // not trusted, sanitize HTML
516 // not trusted, sanitize HTML
514 if (type==='text/html' || type==='text/svg') {
517 if (type==='text/html' || type==='text/svg') {
515 value = security.sanitize_html(value);
518 value = security.sanitize_html(value);
516 } else {
519 } else {
517 // 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
518 console.log("Ignoring untrusted " + type + " output.");
521 console.log("Ignoring untrusted " + type + " output.");
519 continue;
522 continue;
520 }
523 }
521 }
524 }
522 var md = json.metadata || {};
525 var md = json.metadata || {};
523 var toinsert = append.apply(this, [value, md, element, handle_inserted]);
526 var toinsert = append.apply(this, [value, md, element, handle_inserted]);
524 // Since only the png and jpeg mime types call the inserted
527 // Since only the png and jpeg mime types call the inserted
525 // 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
526 // inserted callback only when the element is actually inserted
529 // inserted callback only when the element is actually inserted
527 // into the DOM. Use a timeout of 0 to do this.
530 // into the DOM. Use a timeout of 0 to do this.
528 if (['image/png', 'image/jpeg'].indexOf(type) < 0 && handle_inserted !== undefined) {
531 if (['image/png', 'image/jpeg'].indexOf(type) < 0 && handle_inserted !== undefined) {
529 setTimeout(handle_inserted, 0);
532 setTimeout(handle_inserted, 0);
530 }
533 }
531 this.events.trigger('output_appended.OutputArea', [type, value, md, toinsert]);
534 this.events.trigger('output_appended.OutputArea', [type, value, md, toinsert]);
532 return toinsert;
535 return toinsert;
533 }
536 }
534 }
537 }
535 return null;
538 return null;
536 };
539 };
537
540
538
541
539 var append_html = function (html, md, element) {
542 var append_html = function (html, md, element) {
540 var type = 'text/html';
543 var type = 'text/html';
541 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);
542 this.keyboard_manager.register_events(toinsert);
545 this.keyboard_manager.register_events(toinsert);
543 toinsert.append(html);
546 toinsert.append(html);
544 element.append(toinsert);
547 element.append(toinsert);
545 return toinsert;
548 return toinsert;
546 };
549 };
547
550
548
551
549 var append_markdown = function(markdown, md, element) {
552 var append_markdown = function(markdown, md, element) {
550 var type = 'text/markdown';
553 var type = 'text/markdown';
551 var toinsert = this.create_output_subarea(md, "output_markdown", type);
554 var toinsert = this.create_output_subarea(md, "output_markdown", type);
552 var text_and_math = mathjaxutils.remove_math(markdown);
555 var text_and_math = mathjaxutils.remove_math(markdown);
553 var text = text_and_math[0];
556 var text = text_and_math[0];
554 var math = text_and_math[1];
557 var math = text_and_math[1];
555 marked(text, function (err, html) {
558 marked(text, function (err, html) {
556 html = mathjaxutils.replace_math(html, math);
559 html = mathjaxutils.replace_math(html, math);
557 toinsert.append(html);
560 toinsert.append(html);
558 });
561 });
559 element.append(toinsert);
562 element.append(toinsert);
560 return toinsert;
563 return toinsert;
561 };
564 };
562
565
563
566
564 var append_javascript = function (js, md, element) {
567 var append_javascript = function (js, md, element) {
565 // 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.
566 var type = 'application/javascript';
569 var type = 'application/javascript';
567 var toinsert = this.create_output_subarea(md, "output_javascript", type);
570 var toinsert = this.create_output_subarea(md, "output_javascript", type);
568 this.keyboard_manager.register_events(toinsert);
571 this.keyboard_manager.register_events(toinsert);
569 element.append(toinsert);
572 element.append(toinsert);
570
573
571 // 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
572 // output can be inserted into at the time of JS execution.
575 // output can be inserted into at the time of JS execution.
573 element = toinsert;
576 element = toinsert;
574 try {
577 try {
575 eval(js);
578 eval(js);
576 } catch(err) {
579 } catch(err) {
577 console.log(err);
580 console.log(err);
578 this._append_javascript_error(err, toinsert);
581 this._append_javascript_error(err, toinsert);
579 }
582 }
580 return toinsert;
583 return toinsert;
581 };
584 };
582
585
583
586
584 var append_text = function (data, md, element) {
587 var append_text = function (data, md, element) {
585 var type = 'text/plain';
588 var type = 'text/plain';
586 var toinsert = this.create_output_subarea(md, "output_text", type);
589 var toinsert = this.create_output_subarea(md, "output_text", type);
587 // escape ANSI & HTML specials in plaintext:
590 // escape ANSI & HTML specials in plaintext:
588 data = utils.fixConsole(data);
591 data = utils.fixConsole(data);
589 data = utils.fixCarriageReturn(data);
592 data = utils.fixCarriageReturn(data);
590 data = utils.autoLinkUrls(data);
593 data = utils.autoLinkUrls(data);
591 // The only user content injected with this HTML call is
594 // The only user content injected with this HTML call is
592 // escaped by the fixConsole() method.
595 // escaped by the fixConsole() method.
593 toinsert.append($("<pre/>").html(data));
596 toinsert.append($("<pre/>").html(data));
594 element.append(toinsert);
597 element.append(toinsert);
595 return toinsert;
598 return toinsert;
596 };
599 };
597
600
598
601
599 var append_svg = function (svg_html, md, element) {
602 var append_svg = function (svg_html, md, element) {
600 var type = 'image/svg+xml';
603 var type = 'image/svg+xml';
601 var toinsert = this.create_output_subarea(md, "output_svg", type);
604 var toinsert = this.create_output_subarea(md, "output_svg", type);
602
605
603 // Get the svg element from within the HTML.
606 // Get the svg element from within the HTML.
604 var svg = $('<div />').html(svg_html).find('svg');
607 var svg = $('<div />').html(svg_html).find('svg');
605 var svg_area = $('<div />');
608 var svg_area = $('<div />');
606 var width = svg.attr('width');
609 var width = svg.attr('width');
607 var height = svg.attr('height');
610 var height = svg.attr('height');
608 svg
611 svg
609 .width('100%')
612 .width('100%')
610 .height('100%');
613 .height('100%');
611 svg_area
614 svg_area
612 .width(width)
615 .width(width)
613 .height(height);
616 .height(height);
614
617
615 // 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.
616 // 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
617 // 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
618 // div and make the parent div resizable.
621 // div and make the parent div resizable.
619 this._dblclick_to_reset_size(svg_area, true, false);
622 this._dblclick_to_reset_size(svg_area, true, false);
620
623
621 svg_area.append(svg);
624 svg_area.append(svg);
622 toinsert.append(svg_area);
625 toinsert.append(svg_area);
623 element.append(toinsert);
626 element.append(toinsert);
624
627
625 return toinsert;
628 return toinsert;
626 };
629 };
627
630
628 OutputArea.prototype._dblclick_to_reset_size = function (img, immediately, resize_parent) {
631 OutputArea.prototype._dblclick_to_reset_size = function (img, immediately, resize_parent) {
629 // Add a resize handler to an element
632 // Add a resize handler to an element
630 //
633 //
631 // img: jQuery element
634 // img: jQuery element
632 // immediately: bool=False
635 // immediately: bool=False
633 // Wait for the element to load before creating the handle.
636 // Wait for the element to load before creating the handle.
634 // resize_parent: bool=True
637 // resize_parent: bool=True
635 // 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
636 // reset (by double click).
639 // reset (by double click).
637 var callback = function (){
640 var callback = function (){
638 var h0 = img.height();
641 var h0 = img.height();
639 var w0 = img.width();
642 var w0 = img.width();
640 if (!(h0 && w0)) {
643 if (!(h0 && w0)) {
641 // zero size, don't make it resizable
644 // zero size, don't make it resizable
642 return;
645 return;
643 }
646 }
644 img.resizable({
647 img.resizable({
645 aspectRatio: true,
648 aspectRatio: true,
646 autoHide: true
649 autoHide: true
647 });
650 });
648 img.dblclick(function () {
651 img.dblclick(function () {
649 // resize wrapper & image together for some reason:
652 // resize wrapper & image together for some reason:
650 img.height(h0);
653 img.height(h0);
651 img.width(w0);
654 img.width(w0);
652 if (resize_parent === undefined || resize_parent) {
655 if (resize_parent === undefined || resize_parent) {
653 img.parent().height(h0);
656 img.parent().height(h0);
654 img.parent().width(w0);
657 img.parent().width(w0);
655 }
658 }
656 });
659 });
657 };
660 };
658
661
659 if (immediately) {
662 if (immediately) {
660 callback();
663 callback();
661 } else {
664 } else {
662 img.on("load", callback);
665 img.on("load", callback);
663 }
666 }
664 };
667 };
665
668
666 var set_width_height = function (img, md, mime) {
669 var set_width_height = function (img, md, mime) {
667 // set width and height of an img element from metadata
670 // set width and height of an img element from metadata
668 var height = _get_metadata_key(md, 'height', mime);
671 var height = _get_metadata_key(md, 'height', mime);
669 if (height !== undefined) img.attr('height', height);
672 if (height !== undefined) img.attr('height', height);
670 var width = _get_metadata_key(md, 'width', mime);
673 var width = _get_metadata_key(md, 'width', mime);
671 if (width !== undefined) img.attr('width', width);
674 if (width !== undefined) img.attr('width', width);
672 };
675 };
673
676
674 var append_png = function (png, md, element, handle_inserted) {
677 var append_png = function (png, md, element, handle_inserted) {
675 var type = 'image/png';
678 var type = 'image/png';
676 var toinsert = this.create_output_subarea(md, "output_png", type);
679 var toinsert = this.create_output_subarea(md, "output_png", type);
677 var img = $("<img/>");
680 var img = $("<img/>");
678 if (handle_inserted !== undefined) {
681 if (handle_inserted !== undefined) {
679 img.on('load', function(){
682 img.on('load', function(){
680 handle_inserted(img);
683 handle_inserted(img);
681 });
684 });
682 }
685 }
683 img[0].src = 'data:image/png;base64,'+ png;
686 img[0].src = 'data:image/png;base64,'+ png;
684 set_width_height(img, md, 'image/png');
687 set_width_height(img, md, 'image/png');
685 this._dblclick_to_reset_size(img);
688 this._dblclick_to_reset_size(img);
686 toinsert.append(img);
689 toinsert.append(img);
687 element.append(toinsert);
690 element.append(toinsert);
688 return toinsert;
691 return toinsert;
689 };
692 };
690
693
691
694
692 var append_jpeg = function (jpeg, md, element, handle_inserted) {
695 var append_jpeg = function (jpeg, md, element, handle_inserted) {
693 var type = 'image/jpeg';
696 var type = 'image/jpeg';
694 var toinsert = this.create_output_subarea(md, "output_jpeg", type);
697 var toinsert = this.create_output_subarea(md, "output_jpeg", type);
695 var img = $("<img/>");
698 var img = $("<img/>");
696 if (handle_inserted !== undefined) {
699 if (handle_inserted !== undefined) {
697 img.on('load', function(){
700 img.on('load', function(){
698 handle_inserted(img);
701 handle_inserted(img);
699 });
702 });
700 }
703 }
701 img[0].src = 'data:image/jpeg;base64,'+ jpeg;
704 img[0].src = 'data:image/jpeg;base64,'+ jpeg;
702 set_width_height(img, md, 'image/jpeg');
705 set_width_height(img, md, 'image/jpeg');
703 this._dblclick_to_reset_size(img);
706 this._dblclick_to_reset_size(img);
704 toinsert.append(img);
707 toinsert.append(img);
705 element.append(toinsert);
708 element.append(toinsert);
706 return toinsert;
709 return toinsert;
707 };
710 };
708
711
709
712
710 var append_pdf = function (pdf, md, element) {
713 var append_pdf = function (pdf, md, element) {
711 var type = 'application/pdf';
714 var type = 'application/pdf';
712 var toinsert = this.create_output_subarea(md, "output_pdf", type);
715 var toinsert = this.create_output_subarea(md, "output_pdf", type);
713 var a = $('<a/>').attr('href', 'data:application/pdf;base64,'+pdf);
716 var a = $('<a/>').attr('href', 'data:application/pdf;base64,'+pdf);
714 a.attr('target', '_blank');
717 a.attr('target', '_blank');
715 a.text('View PDF');
718 a.text('View PDF');
716 toinsert.append(a);
719 toinsert.append(a);
717 element.append(toinsert);
720 element.append(toinsert);
718 return toinsert;
721 return toinsert;
719 };
722 };
720
723
721 var append_latex = function (latex, md, element) {
724 var append_latex = function (latex, md, element) {
722 // 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
723 // be on the page.
726 // be on the page.
724 var type = 'text/latex';
727 var type = 'text/latex';
725 var toinsert = this.create_output_subarea(md, "output_latex", type);
728 var toinsert = this.create_output_subarea(md, "output_latex", type);
726 toinsert.append(latex);
729 toinsert.append(latex);
727 element.append(toinsert);
730 element.append(toinsert);
728 return toinsert;
731 return toinsert;
729 };
732 };
730
733
731
734
732 OutputArea.prototype.append_raw_input = function (msg) {
735 OutputArea.prototype.append_raw_input = function (msg) {
733 var that = this;
736 var that = this;
734 this.expand();
737 this.expand();
735 var content = msg.content;
738 var content = msg.content;
736 var area = this.create_output_area();
739 var area = this.create_output_area();
737
740
738 // disable any other raw_inputs, if they are left around
741 // disable any other raw_inputs, if they are left around
739 $("div.output_subarea.raw_input_container").remove();
742 $("div.output_subarea.raw_input_container").remove();
740
743
741 var input_type = content.password ? 'password' : 'text';
744 var input_type = content.password ? 'password' : 'text';
742
745
743 area.append(
746 area.append(
744 $("<div/>")
747 $("<div/>")
745 .addClass("box-flex1 output_subarea raw_input_container")
748 .addClass("box-flex1 output_subarea raw_input_container")
746 .append(
749 .append(
747 $("<span/>")
750 $("<span/>")
748 .addClass("raw_input_prompt")
751 .addClass("raw_input_prompt")
749 .text(content.prompt)
752 .text(content.prompt)
750 )
753 )
751 .append(
754 .append(
752 $("<input/>")
755 $("<input/>")
753 .addClass("raw_input")
756 .addClass("raw_input")
754 .attr('type', input_type)
757 .attr('type', input_type)
755 .attr("size", 47)
758 .attr("size", 47)
756 .keydown(function (event, ui) {
759 .keydown(function (event, ui) {
757 // make sure we submit on enter,
760 // make sure we submit on enter,
758 // and don't re-execute the *cell* on shift-enter
761 // and don't re-execute the *cell* on shift-enter
759 if (event.which === keyboard.keycodes.enter) {
762 if (event.which === keyboard.keycodes.enter) {
760 that._submit_raw_input();
763 that._submit_raw_input();
761 return false;
764 return false;
762 }
765 }
763 })
766 })
764 )
767 )
765 );
768 );
766
769
767 this.element.append(area);
770 this.element.append(area);
768 var raw_input = area.find('input.raw_input');
771 var raw_input = area.find('input.raw_input');
769 // Register events that enable/disable the keyboard manager while raw
772 // Register events that enable/disable the keyboard manager while raw
770 // input is focused.
773 // input is focused.
771 this.keyboard_manager.register_events(raw_input);
774 this.keyboard_manager.register_events(raw_input);
772 // Note, the following line used to read raw_input.focus().focus().
775 // Note, the following line used to read raw_input.focus().focus().
773 // 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.
774 // 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().
775 raw_input.focus();
778 raw_input.focus();
776 };
779 };
777
780
778 OutputArea.prototype._submit_raw_input = function (evt) {
781 OutputArea.prototype._submit_raw_input = function (evt) {
779 var container = this.element.find("div.raw_input_container");
782 var container = this.element.find("div.raw_input_container");
780 var theprompt = container.find("span.raw_input_prompt");
783 var theprompt = container.find("span.raw_input_prompt");
781 var theinput = container.find("input.raw_input");
784 var theinput = container.find("input.raw_input");
782 var value = theinput.val();
785 var value = theinput.val();
783 var echo = value;
786 var echo = value;
784 // don't echo if it's a password
787 // don't echo if it's a password
785 if (theinput.attr('type') == 'password') {
788 if (theinput.attr('type') == 'password') {
786 echo = 'Β·Β·Β·Β·Β·Β·Β·Β·';
789 echo = 'Β·Β·Β·Β·Β·Β·Β·Β·';
787 }
790 }
788 var content = {
791 var content = {
789 output_type : 'stream',
792 output_type : 'stream',
790 stream : 'stdout',
793 stream : 'stdout',
791 text : theprompt.text() + echo + '\n'
794 text : theprompt.text() + echo + '\n'
792 };
795 };
793 // remove form container
796 // remove form container
794 container.parent().remove();
797 container.parent().remove();
795 // replace with plaintext version in stdout
798 // replace with plaintext version in stdout
796 this.append_output(content, false);
799 this.append_output(content, false);
797 this.events.trigger('send_input_reply.Kernel', value);
800 this.events.trigger('send_input_reply.Kernel', value);
798 };
801 };
799
802
800
803
801 OutputArea.prototype.handle_clear_output = function (msg) {
804 OutputArea.prototype.handle_clear_output = function (msg) {
802 // msg spec v4 had stdout, stderr, display keys
805 // msg spec v4 had stdout, stderr, display keys
803 // v4.1 replaced these with just wait
806 // v4.1 replaced these with just wait
804 // 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),
805 // so v4 messages will still be properly handled,
808 // so v4 messages will still be properly handled,
806 // except for the rarely used clearing less than all output.
809 // except for the rarely used clearing less than all output.
807 this.clear_output(msg.content.wait || false);
810 this.clear_output(msg.content.wait || false);
808 };
811 };
809
812
810
813
811 OutputArea.prototype.clear_output = function(wait) {
814 OutputArea.prototype.clear_output = function(wait) {
812 if (wait) {
815 if (wait) {
813
816
814 // 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.
815 if (this.clear_queued) {
818 if (this.clear_queued) {
816 this.clear_output(false);
819 this.clear_output(false);
817 }
820 }
818
821
819 this.clear_queued = true;
822 this.clear_queued = true;
820 } else {
823 } else {
821
824
822 // 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
823 // new output (it is being used in an animation).
826 // new output (it is being used in an animation).
824 if (this.clear_queued) {
827 if (this.clear_queued) {
825 var height = this.element.height();
828 var height = this.element.height();
826 this.element.height(height);
829 this.element.height(height);
827 this.clear_queued = false;
830 this.clear_queued = false;
828 }
831 }
829
832
830 // Clear all
833 // Clear all
831 // 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
832 // 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.
833 this.element.find('img').off('load');
836 this.element.find('img').off('load');
834 this.element.html("");
837 this.element.html("");
838
839 // Notify others of changes.
840 this.element.trigger('changed');
841
835 this.outputs = [];
842 this.outputs = [];
836 this.trusted = true;
843 this.trusted = true;
837 this.unscroll_area();
844 this.unscroll_area();
838 return;
845 return;
839 }
846 }
840 };
847 };
841
848
842
849
843 // JSON serialization
850 // JSON serialization
844
851
845 OutputArea.prototype.fromJSON = function (outputs, metadata) {
852 OutputArea.prototype.fromJSON = function (outputs, metadata) {
846 var len = outputs.length;
853 var len = outputs.length;
847 metadata = metadata || {};
854 metadata = metadata || {};
848
855
849 for (var i=0; i<len; i++) {
856 for (var i=0; i<len; i++) {
850 this.append_output(outputs[i]);
857 this.append_output(outputs[i]);
851 }
858 }
852
859
853 if (metadata.collapsed !== undefined) {
860 if (metadata.collapsed !== undefined) {
854 this.collapsed = metadata.collapsed;
861 this.collapsed = metadata.collapsed;
855 if (metadata.collapsed) {
862 if (metadata.collapsed) {
856 this.collapse_output();
863 this.collapse_output();
857 }
864 }
858 }
865 }
859 if (metadata.autoscroll !== undefined) {
866 if (metadata.autoscroll !== undefined) {
860 this.collapsed = metadata.collapsed;
867 this.collapsed = metadata.collapsed;
861 if (metadata.collapsed) {
868 if (metadata.collapsed) {
862 this.collapse_output();
869 this.collapse_output();
863 } else {
870 } else {
864 this.expand_output();
871 this.expand_output();
865 }
872 }
866 }
873 }
867 };
874 };
868
875
869
876
870 OutputArea.prototype.toJSON = function () {
877 OutputArea.prototype.toJSON = function () {
871 return this.outputs;
878 return this.outputs;
872 };
879 };
873
880
874 /**
881 /**
875 * Class properties
882 * Class properties
876 **/
883 **/
877
884
878 /**
885 /**
879 * Threshold to trigger autoscroll when the OutputArea is resized,
886 * Threshold to trigger autoscroll when the OutputArea is resized,
880 * typically when new outputs are added.
887 * typically when new outputs are added.
881 *
888 *
882 * Behavior is undefined if autoscroll is lower than minimum_scroll_threshold,
889 * Behavior is undefined if autoscroll is lower than minimum_scroll_threshold,
883 * 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
884 *
891 *
885 * @property auto_scroll_threshold
892 * @property auto_scroll_threshold
886 * @type Number
893 * @type Number
887 * @default 100
894 * @default 100
888 *
895 *
889 **/
896 **/
890 OutputArea.auto_scroll_threshold = 100;
897 OutputArea.auto_scroll_threshold = 100;
891
898
892 /**
899 /**
893 * Lower limit (in lines) for OutputArea to be made scrollable. OutputAreas
900 * Lower limit (in lines) for OutputArea to be made scrollable. OutputAreas
894 * shorter than this are never scrolled.
901 * shorter than this are never scrolled.
895 *
902 *
896 * @property minimum_scroll_threshold
903 * @property minimum_scroll_threshold
897 * @type Number
904 * @type Number
898 * @default 20
905 * @default 20
899 *
906 *
900 **/
907 **/
901 OutputArea.minimum_scroll_threshold = 20;
908 OutputArea.minimum_scroll_threshold = 20;
902
909
903
910
904 OutputArea.display_order = [
911 OutputArea.display_order = [
905 'application/javascript',
912 'application/javascript',
906 'text/html',
913 'text/html',
907 'text/markdown',
914 'text/markdown',
908 'text/latex',
915 'text/latex',
909 'image/svg+xml',
916 'image/svg+xml',
910 'image/png',
917 'image/png',
911 'image/jpeg',
918 'image/jpeg',
912 'application/pdf',
919 'application/pdf',
913 'text/plain'
920 'text/plain'
914 ];
921 ];
915
922
916 OutputArea.append_map = {
923 OutputArea.append_map = {
917 "text/plain" : append_text,
924 "text/plain" : append_text,
918 "text/html" : append_html,
925 "text/html" : append_html,
919 "text/markdown": append_markdown,
926 "text/markdown": append_markdown,
920 "image/svg+xml" : append_svg,
927 "image/svg+xml" : append_svg,
921 "image/png" : append_png,
928 "image/png" : append_png,
922 "image/jpeg" : append_jpeg,
929 "image/jpeg" : append_jpeg,
923 "text/latex" : append_latex,
930 "text/latex" : append_latex,
924 "application/javascript" : append_javascript,
931 "application/javascript" : append_javascript,
925 "application/pdf" : append_pdf
932 "application/pdf" : append_pdf
926 };
933 };
927
934
928 // For backwards compatability.
935 // For backwards compatability.
929 IPython.OutputArea = OutputArea;
936 IPython.OutputArea = OutputArea;
930
937
931 return {'OutputArea': OutputArea};
938 return {'OutputArea': OutputArea};
932 });
939 });
@@ -1,41 +1,56
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 "widgets/js/widget",
5 "widgets/js/widget",
6 "jquery",
6 "jquery",
7 'notebook/js/outputarea',
7 'notebook/js/outputarea',
8 ], function(widget, $, outputarea){
8 ], function(widget, $, outputarea){
9
9
10 var OutputView = widget.DOMWidgetView.extend({
10 var OutputView = widget.DOMWidgetView.extend({
11 initialize: function (parameters) {
11 initialize: function (parameters) {
12 // Public constructor
12 // Public constructor
13 OutputView.__super__.initialize.apply(this, [parameters]);
13 OutputView.__super__.initialize.apply(this, [parameters]);
14 this.model.on('msg:custom', this._handle_route_msg, this);
14 this.model.on('msg:custom', this._handle_route_msg, this);
15 },
15 },
16
16
17 render: function(){
17 render: function(){
18 // Called when view is rendered.
18 // Called when view is rendered.
19 this.output_area = new outputarea.OutputArea({
19 this.output_area = new outputarea.OutputArea({
20 selector: this.$el,
20 selector: this.$el,
21 prompt_area: false,
21 prompt_area: false,
22 events: this.model.widget_manager.notebook.events,
22 events: this.model.widget_manager.notebook.events,
23 keyboard_manager: this.model.widget_manager.keyboard_manager });
23 keyboard_manager: this.model.widget_manager.keyboard_manager });
24
25 // Make output area reactive.
26 var that = this;
27 this.output_area.element.on('changed', function() {
28 that.model.set('contents', that.output_area.element.html());
29 });
30 this.model.on('change:contents', function(){
31 var html = this.model.get('contents');
32 if (this.output_area.element.html() != html) {
33 this.output_area.element.html(html);
34 }
35 }, this);
36
37 // Set initial contents.
38 this.output_area.element.html(this.model.get('contents'));
24 },
39 },
25
40
26 _handle_route_msg: function(content) {
41 _handle_route_msg: function(content) {
27 var cell = this.options.cell;
42 var cell = this.options.cell;
28 if (content && cell) {
43 if (content && cell) {
29 if (content.method == 'push') {
44 if (content.method == 'push') {
30 cell.push_output_area(this.output_area);
45 cell.push_output_area(this.output_area);
31 } else if (content.method == 'pop') {
46 } else if (content.method == 'pop') {
32 cell.pop_output_area(this.output_area);
47 cell.pop_output_area(this.output_area);
33 }
48 }
34 }
49 }
35 },
50 },
36 });
51 });
37
52
38 return {
53 return {
39 'OutputView': OutputView,
54 'OutputView': OutputView,
40 };
55 };
41 });
56 });
General Comments 0
You need to be logged in to leave comments. Login now