##// END OF EJS Templates
Merge pull request #4136 from minrk/catch-js...
Min RK -
r12438:fbc27192 merge
parent child Browse files
Show More
@@ -1,705 +1,712 b''
1 //----------------------------------------------------------------------------
1 //----------------------------------------------------------------------------
2 // Copyright (C) 2008 The IPython Development Team
2 // Copyright (C) 2008 The IPython Development Team
3 //
3 //
4 // Distributed under the terms of the BSD License. The full license is in
4 // Distributed under the terms of the BSD License. The full license is in
5 // the file COPYING, distributed as part of this software.
5 // the file COPYING, distributed as part of this software.
6 //----------------------------------------------------------------------------
6 //----------------------------------------------------------------------------
7
7
8 //============================================================================
8 //============================================================================
9 // OutputArea
9 // OutputArea
10 //============================================================================
10 //============================================================================
11
11
12 /**
12 /**
13 * @module IPython
13 * @module IPython
14 * @namespace IPython
14 * @namespace IPython
15 * @submodule OutputArea
15 * @submodule OutputArea
16 */
16 */
17 var IPython = (function (IPython) {
17 var IPython = (function (IPython) {
18 "use strict";
18 "use strict";
19
19
20 var utils = IPython.utils;
20 var utils = IPython.utils;
21
21
22 /**
22 /**
23 * @class OutputArea
23 * @class OutputArea
24 *
24 *
25 * @constructor
25 * @constructor
26 */
26 */
27
27
28 var OutputArea = function (selector, prompt_area) {
28 var OutputArea = function (selector, prompt_area) {
29 this.selector = selector;
29 this.selector = selector;
30 this.wrapper = $(selector);
30 this.wrapper = $(selector);
31 this.outputs = [];
31 this.outputs = [];
32 this.collapsed = false;
32 this.collapsed = false;
33 this.scrolled = false;
33 this.scrolled = false;
34 this.clear_out_timeout = null;
34 this.clear_out_timeout = null;
35 if (prompt_area === undefined) {
35 if (prompt_area === undefined) {
36 this.prompt_area = true;
36 this.prompt_area = true;
37 } else {
37 } else {
38 this.prompt_area = prompt_area;
38 this.prompt_area = prompt_area;
39 }
39 }
40 this.create_elements();
40 this.create_elements();
41 this.style();
41 this.style();
42 this.bind_events();
42 this.bind_events();
43 };
43 };
44
44
45 OutputArea.prototype.create_elements = function () {
45 OutputArea.prototype.create_elements = function () {
46 this.element = $("<div/>");
46 this.element = $("<div/>");
47 this.collapse_button = $("<div/>");
47 this.collapse_button = $("<div/>");
48 this.prompt_overlay = $("<div/>");
48 this.prompt_overlay = $("<div/>");
49 this.wrapper.append(this.prompt_overlay);
49 this.wrapper.append(this.prompt_overlay);
50 this.wrapper.append(this.element);
50 this.wrapper.append(this.element);
51 this.wrapper.append(this.collapse_button);
51 this.wrapper.append(this.collapse_button);
52 };
52 };
53
53
54
54
55 OutputArea.prototype.style = function () {
55 OutputArea.prototype.style = function () {
56 this.collapse_button.hide();
56 this.collapse_button.hide();
57 this.prompt_overlay.hide();
57 this.prompt_overlay.hide();
58
58
59 this.wrapper.addClass('output_wrapper');
59 this.wrapper.addClass('output_wrapper');
60 this.element.addClass('output vbox');
60 this.element.addClass('output vbox');
61
61
62 this.collapse_button.addClass("btn output_collapsed");
62 this.collapse_button.addClass("btn output_collapsed");
63 this.collapse_button.attr('title', 'click to expand output');
63 this.collapse_button.attr('title', 'click to expand output');
64 this.collapse_button.html('. . .');
64 this.collapse_button.html('. . .');
65
65
66 this.prompt_overlay.addClass('out_prompt_overlay prompt');
66 this.prompt_overlay.addClass('out_prompt_overlay prompt');
67 this.prompt_overlay.attr('title', 'click to expand output; double click to hide output');
67 this.prompt_overlay.attr('title', 'click to expand output; double click to hide output');
68
68
69 this.collapse();
69 this.collapse();
70 };
70 };
71
71
72 /**
72 /**
73 * Should the OutputArea scroll?
73 * Should the OutputArea scroll?
74 * Returns whether the height (in lines) exceeds a threshold.
74 * Returns whether the height (in lines) exceeds a threshold.
75 *
75 *
76 * @private
76 * @private
77 * @method _should_scroll
77 * @method _should_scroll
78 * @param [lines=100]{Integer}
78 * @param [lines=100]{Integer}
79 * @return {Bool}
79 * @return {Bool}
80 *
80 *
81 */
81 */
82 OutputArea.prototype._should_scroll = function (lines) {
82 OutputArea.prototype._should_scroll = function (lines) {
83 if (lines <=0 ){ return }
83 if (lines <=0 ){ return }
84 if (!lines) {
84 if (!lines) {
85 lines = 100;
85 lines = 100;
86 }
86 }
87 // line-height from http://stackoverflow.com/questions/1185151
87 // line-height from http://stackoverflow.com/questions/1185151
88 var fontSize = this.element.css('font-size');
88 var fontSize = this.element.css('font-size');
89 var lineHeight = Math.floor(parseInt(fontSize.replace('px','')) * 1.5);
89 var lineHeight = Math.floor(parseInt(fontSize.replace('px','')) * 1.5);
90
90
91 return (this.element.height() > lines * lineHeight);
91 return (this.element.height() > lines * lineHeight);
92 };
92 };
93
93
94
94
95 OutputArea.prototype.bind_events = function () {
95 OutputArea.prototype.bind_events = function () {
96 var that = this;
96 var that = this;
97 this.prompt_overlay.dblclick(function () { that.toggle_output(); });
97 this.prompt_overlay.dblclick(function () { that.toggle_output(); });
98 this.prompt_overlay.click(function () { that.toggle_scroll(); });
98 this.prompt_overlay.click(function () { that.toggle_scroll(); });
99
99
100 this.element.resize(function () {
100 this.element.resize(function () {
101 // FIXME: Firefox on Linux misbehaves, so automatic scrolling is disabled
101 // FIXME: Firefox on Linux misbehaves, so automatic scrolling is disabled
102 if ( IPython.utils.browser[0] === "Firefox" ) {
102 if ( IPython.utils.browser[0] === "Firefox" ) {
103 return;
103 return;
104 }
104 }
105 // maybe scroll output,
105 // maybe scroll output,
106 // if it's grown large enough and hasn't already been scrolled.
106 // if it's grown large enough and hasn't already been scrolled.
107 if ( !that.scrolled && that._should_scroll(OutputArea.auto_scroll_threshold)) {
107 if ( !that.scrolled && that._should_scroll(OutputArea.auto_scroll_threshold)) {
108 that.scroll_area();
108 that.scroll_area();
109 }
109 }
110 });
110 });
111 this.collapse_button.click(function () {
111 this.collapse_button.click(function () {
112 that.expand();
112 that.expand();
113 });
113 });
114 };
114 };
115
115
116
116
117 OutputArea.prototype.collapse = function () {
117 OutputArea.prototype.collapse = function () {
118 if (!this.collapsed) {
118 if (!this.collapsed) {
119 this.element.hide();
119 this.element.hide();
120 this.prompt_overlay.hide();
120 this.prompt_overlay.hide();
121 if (this.element.html()){
121 if (this.element.html()){
122 this.collapse_button.show();
122 this.collapse_button.show();
123 }
123 }
124 this.collapsed = true;
124 this.collapsed = true;
125 }
125 }
126 };
126 };
127
127
128
128
129 OutputArea.prototype.expand = function () {
129 OutputArea.prototype.expand = function () {
130 if (this.collapsed) {
130 if (this.collapsed) {
131 this.collapse_button.hide();
131 this.collapse_button.hide();
132 this.element.show();
132 this.element.show();
133 this.prompt_overlay.show();
133 this.prompt_overlay.show();
134 this.collapsed = false;
134 this.collapsed = false;
135 }
135 }
136 };
136 };
137
137
138
138
139 OutputArea.prototype.toggle_output = function () {
139 OutputArea.prototype.toggle_output = function () {
140 if (this.collapsed) {
140 if (this.collapsed) {
141 this.expand();
141 this.expand();
142 } else {
142 } else {
143 this.collapse();
143 this.collapse();
144 }
144 }
145 };
145 };
146
146
147
147
148 OutputArea.prototype.scroll_area = function () {
148 OutputArea.prototype.scroll_area = function () {
149 this.element.addClass('output_scroll');
149 this.element.addClass('output_scroll');
150 this.prompt_overlay.attr('title', 'click to unscroll output; double click to hide');
150 this.prompt_overlay.attr('title', 'click to unscroll output; double click to hide');
151 this.scrolled = true;
151 this.scrolled = true;
152 };
152 };
153
153
154
154
155 OutputArea.prototype.unscroll_area = function () {
155 OutputArea.prototype.unscroll_area = function () {
156 this.element.removeClass('output_scroll');
156 this.element.removeClass('output_scroll');
157 this.prompt_overlay.attr('title', 'click to scroll output; double click to hide');
157 this.prompt_overlay.attr('title', 'click to scroll output; double click to hide');
158 this.scrolled = false;
158 this.scrolled = false;
159 };
159 };
160
160
161 /**
161 /**
162 * Threshold to trigger autoscroll when the OutputArea is resized,
162 * Threshold to trigger autoscroll when the OutputArea is resized,
163 * typically when new outputs are added.
163 * typically when new outputs are added.
164 *
164 *
165 * Behavior is undefined if autoscroll is lower than minimum_scroll_threshold,
165 * Behavior is undefined if autoscroll is lower than minimum_scroll_threshold,
166 * unless it is < 0, in which case autoscroll will never be triggered
166 * unless it is < 0, in which case autoscroll will never be triggered
167 *
167 *
168 * @property auto_scroll_threshold
168 * @property auto_scroll_threshold
169 * @type Number
169 * @type Number
170 * @default 100
170 * @default 100
171 *
171 *
172 **/
172 **/
173 OutputArea.auto_scroll_threshold = 100;
173 OutputArea.auto_scroll_threshold = 100;
174
174
175
175
176 /**
176 /**
177 * Lower limit (in lines) for OutputArea to be made scrollable. OutputAreas
177 * Lower limit (in lines) for OutputArea to be made scrollable. OutputAreas
178 * shorter than this are never scrolled.
178 * shorter than this are never scrolled.
179 *
179 *
180 * @property minimum_scroll_threshold
180 * @property minimum_scroll_threshold
181 * @type Number
181 * @type Number
182 * @default 20
182 * @default 20
183 *
183 *
184 **/
184 **/
185 OutputArea.minimum_scroll_threshold = 20;
185 OutputArea.minimum_scroll_threshold = 20;
186
186
187
187
188 /**
188 /**
189 *
189 *
190 * Scroll OutputArea if height supperior than a threshold (in lines).
190 * Scroll OutputArea if height supperior than a threshold (in lines).
191 *
191 *
192 * Threshold is a maximum number of lines. If unspecified, defaults to
192 * Threshold is a maximum number of lines. If unspecified, defaults to
193 * OutputArea.minimum_scroll_threshold.
193 * OutputArea.minimum_scroll_threshold.
194 *
194 *
195 * Negative threshold will prevent the OutputArea from ever scrolling.
195 * Negative threshold will prevent the OutputArea from ever scrolling.
196 *
196 *
197 * @method scroll_if_long
197 * @method scroll_if_long
198 *
198 *
199 * @param [lines=20]{Number} Default to 20 if not set,
199 * @param [lines=20]{Number} Default to 20 if not set,
200 * behavior undefined for value of `0`.
200 * behavior undefined for value of `0`.
201 *
201 *
202 **/
202 **/
203 OutputArea.prototype.scroll_if_long = function (lines) {
203 OutputArea.prototype.scroll_if_long = function (lines) {
204 var n = lines | OutputArea.minimum_scroll_threshold;
204 var n = lines | OutputArea.minimum_scroll_threshold;
205 if(n <= 0){
205 if(n <= 0){
206 return
206 return
207 }
207 }
208
208
209 if (this._should_scroll(n)) {
209 if (this._should_scroll(n)) {
210 // only allow scrolling long-enough output
210 // only allow scrolling long-enough output
211 this.scroll_area();
211 this.scroll_area();
212 }
212 }
213 };
213 };
214
214
215
215
216 OutputArea.prototype.toggle_scroll = function () {
216 OutputArea.prototype.toggle_scroll = function () {
217 if (this.scrolled) {
217 if (this.scrolled) {
218 this.unscroll_area();
218 this.unscroll_area();
219 } else {
219 } else {
220 // only allow scrolling long-enough output
220 // only allow scrolling long-enough output
221 this.scroll_if_long();
221 this.scroll_if_long();
222 }
222 }
223 };
223 };
224
224
225
225
226 // typeset with MathJax if MathJax is available
226 // typeset with MathJax if MathJax is available
227 OutputArea.prototype.typeset = function () {
227 OutputArea.prototype.typeset = function () {
228 if (window.MathJax){
228 if (window.MathJax){
229 MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
229 MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
230 }
230 }
231 };
231 };
232
232
233
233
234 OutputArea.prototype.handle_output = function (msg_type, content) {
234 OutputArea.prototype.handle_output = function (msg_type, content) {
235 var json = {};
235 var json = {};
236 json.output_type = msg_type;
236 json.output_type = msg_type;
237 if (msg_type === "stream") {
237 if (msg_type === "stream") {
238 json.text = content.data;
238 json.text = content.data;
239 json.stream = content.name;
239 json.stream = content.name;
240 } else if (msg_type === "display_data") {
240 } else if (msg_type === "display_data") {
241 json = this.convert_mime_types(json, content.data);
241 json = this.convert_mime_types(json, content.data);
242 json.metadata = this.convert_mime_types({}, content.metadata);
242 json.metadata = this.convert_mime_types({}, content.metadata);
243 } else if (msg_type === "pyout") {
243 } else if (msg_type === "pyout") {
244 json.prompt_number = content.execution_count;
244 json.prompt_number = content.execution_count;
245 json = this.convert_mime_types(json, content.data);
245 json = this.convert_mime_types(json, content.data);
246 json.metadata = this.convert_mime_types({}, content.metadata);
246 json.metadata = this.convert_mime_types({}, content.metadata);
247 } else if (msg_type === "pyerr") {
247 } else if (msg_type === "pyerr") {
248 json.ename = content.ename;
248 json.ename = content.ename;
249 json.evalue = content.evalue;
249 json.evalue = content.evalue;
250 json.traceback = content.traceback;
250 json.traceback = content.traceback;
251 }
251 }
252 // append with dynamic=true
252 // append with dynamic=true
253 this.append_output(json, true);
253 this.append_output(json, true);
254 };
254 };
255
255
256
256
257 OutputArea.prototype.convert_mime_types = function (json, data) {
257 OutputArea.prototype.convert_mime_types = function (json, data) {
258 if (data === undefined) {
258 if (data === undefined) {
259 return json;
259 return json;
260 }
260 }
261 if (data['text/plain'] !== undefined) {
261 if (data['text/plain'] !== undefined) {
262 json.text = data['text/plain'];
262 json.text = data['text/plain'];
263 }
263 }
264 if (data['text/html'] !== undefined) {
264 if (data['text/html'] !== undefined) {
265 json.html = data['text/html'];
265 json.html = data['text/html'];
266 }
266 }
267 if (data['image/svg+xml'] !== undefined) {
267 if (data['image/svg+xml'] !== undefined) {
268 json.svg = data['image/svg+xml'];
268 json.svg = data['image/svg+xml'];
269 }
269 }
270 if (data['image/png'] !== undefined) {
270 if (data['image/png'] !== undefined) {
271 json.png = data['image/png'];
271 json.png = data['image/png'];
272 }
272 }
273 if (data['image/jpeg'] !== undefined) {
273 if (data['image/jpeg'] !== undefined) {
274 json.jpeg = data['image/jpeg'];
274 json.jpeg = data['image/jpeg'];
275 }
275 }
276 if (data['text/latex'] !== undefined) {
276 if (data['text/latex'] !== undefined) {
277 json.latex = data['text/latex'];
277 json.latex = data['text/latex'];
278 }
278 }
279 if (data['application/json'] !== undefined) {
279 if (data['application/json'] !== undefined) {
280 json.json = data['application/json'];
280 json.json = data['application/json'];
281 }
281 }
282 if (data['application/javascript'] !== undefined) {
282 if (data['application/javascript'] !== undefined) {
283 json.javascript = data['application/javascript'];
283 json.javascript = data['application/javascript'];
284 }
284 }
285 return json;
285 return json;
286 };
286 };
287
287
288
288
289 OutputArea.prototype.append_output = function (json, dynamic) {
289 OutputArea.prototype.append_output = function (json, dynamic) {
290 // If dynamic is true, javascript output will be eval'd.
290 // If dynamic is true, javascript output will be eval'd.
291 this.expand();
291 this.expand();
292 this.flush_clear_timeout();
292 this.flush_clear_timeout();
293 if (json.output_type === 'pyout') {
293 if (json.output_type === 'pyout') {
294 this.append_pyout(json, dynamic);
294 this.append_pyout(json, dynamic);
295 } else if (json.output_type === 'pyerr') {
295 } else if (json.output_type === 'pyerr') {
296 this.append_pyerr(json);
296 this.append_pyerr(json);
297 } else if (json.output_type === 'display_data') {
297 } else if (json.output_type === 'display_data') {
298 this.append_display_data(json, dynamic);
298 this.append_display_data(json, dynamic);
299 } else if (json.output_type === 'stream') {
299 } else if (json.output_type === 'stream') {
300 this.append_stream(json);
300 this.append_stream(json);
301 }
301 }
302 this.outputs.push(json);
302 this.outputs.push(json);
303 var that = this;
303 var that = this;
304 setTimeout(function(){that.element.trigger('resize');}, 100);
304 setTimeout(function(){that.element.trigger('resize');}, 100);
305 };
305 };
306
306
307
307
308 OutputArea.prototype.create_output_area = function () {
308 OutputArea.prototype.create_output_area = function () {
309 var oa = $("<div/>").addClass("output_area");
309 var oa = $("<div/>").addClass("output_area");
310 if (this.prompt_area) {
310 if (this.prompt_area) {
311 oa.append($('<div/>').addClass('prompt'));
311 oa.append($('<div/>').addClass('prompt'));
312 }
312 }
313 return oa;
313 return oa;
314 };
314 };
315
316 OutputArea.prototype._append_javascript_error = function (err, container) {
317 // display a message when a javascript error occurs in display output
318 var msg = "Javascript error adding output!"
319 console.log(msg, err);
320 if ( container === undefined ) return;
321 container.append(
322 $('<div/>').html(msg + "<br/>" +
323 err.toString() +
324 '<br/>See your browser Javascript console for more details.'
325 ).addClass('js-error')
326 );
327 container.show();
328 };
329
330 OutputArea.prototype._safe_append = function (toinsert) {
331 // safely append an item to the document
332 // this is an object created by user code,
333 // and may have errors, which should not be raised
334 // under any circumstances.
335 try {
336 this.element.append(toinsert);
337 } catch(err) {
338 console.log(err);
339 this._append_javascript_error(err, this.element);
340 }
341 };
315
342
316
343
317 OutputArea.prototype.append_pyout = function (json, dynamic) {
344 OutputArea.prototype.append_pyout = function (json, dynamic) {
318 var n = json.prompt_number || ' ';
345 var n = json.prompt_number || ' ';
319 var toinsert = this.create_output_area();
346 var toinsert = this.create_output_area();
320 if (this.prompt_area) {
347 if (this.prompt_area) {
321 toinsert.find('div.prompt').addClass('output_prompt').html('Out[' + n + ']:');
348 toinsert.find('div.prompt').addClass('output_prompt').html('Out[' + n + ']:');
322 }
349 }
323 this.append_mime_type(json, toinsert, dynamic);
350 this.append_mime_type(json, toinsert, dynamic);
324 try {
351 this._safe_append(toinsert);
325 this.element.append(toinsert);
326 } catch(err) {
327 console.log("Error attaching output!");
328 console.log(err);
329 this.element.show();
330 toinsert.html($('<div/>')
331 .html("Javascript error adding output!<br/>" +
332 err.toString() +
333 '<br/>See your browser Javascript console for more details.')
334 .addClass('js-error')
335 );
336 }
337 // If we just output latex, typeset it.
352 // If we just output latex, typeset it.
338 if ((json.latex !== undefined) || (json.html !== undefined)) {
353 if ((json.latex !== undefined) || (json.html !== undefined)) {
339 this.typeset();
354 this.typeset();
340 }
355 }
341 };
356 };
342
357
343
358
344 OutputArea.prototype.append_pyerr = function (json) {
359 OutputArea.prototype.append_pyerr = function (json) {
345 var tb = json.traceback;
360 var tb = json.traceback;
346 if (tb !== undefined && tb.length > 0) {
361 if (tb !== undefined && tb.length > 0) {
347 var s = '';
362 var s = '';
348 var len = tb.length;
363 var len = tb.length;
349 for (var i=0; i<len; i++) {
364 for (var i=0; i<len; i++) {
350 s = s + tb[i] + '\n';
365 s = s + tb[i] + '\n';
351 }
366 }
352 s = s + '\n';
367 s = s + '\n';
353 var toinsert = this.create_output_area();
368 var toinsert = this.create_output_area();
354 this.append_text(s, {}, toinsert);
369 this.append_text(s, {}, toinsert);
355 this.element.append(toinsert);
370 this._safe_append(toinsert);
356 }
371 }
357 };
372 };
358
373
359
374
360 OutputArea.prototype.append_stream = function (json) {
375 OutputArea.prototype.append_stream = function (json) {
361 // temporary fix: if stream undefined (json file written prior to this patch),
376 // temporary fix: if stream undefined (json file written prior to this patch),
362 // default to most likely stdout:
377 // default to most likely stdout:
363 if (json.stream == undefined){
378 if (json.stream == undefined){
364 json.stream = 'stdout';
379 json.stream = 'stdout';
365 }
380 }
366 var text = json.text;
381 var text = json.text;
367 var subclass = "output_"+json.stream;
382 var subclass = "output_"+json.stream;
368 if (this.outputs.length > 0){
383 if (this.outputs.length > 0){
369 // have at least one output to consider
384 // have at least one output to consider
370 var last = this.outputs[this.outputs.length-1];
385 var last = this.outputs[this.outputs.length-1];
371 if (last.output_type == 'stream' && json.stream == last.stream){
386 if (last.output_type == 'stream' && json.stream == last.stream){
372 // latest output was in the same stream,
387 // latest output was in the same stream,
373 // so append directly into its pre tag
388 // so append directly into its pre tag
374 // escape ANSI & HTML specials:
389 // escape ANSI & HTML specials:
375 var pre = this.element.find('div.'+subclass).last().find('pre');
390 var pre = this.element.find('div.'+subclass).last().find('pre');
376 var html = utils.fixCarriageReturn(
391 var html = utils.fixCarriageReturn(
377 pre.html() + utils.fixConsole(text));
392 pre.html() + utils.fixConsole(text));
378 pre.html(html);
393 pre.html(html);
379 return;
394 return;
380 }
395 }
381 }
396 }
382
397
383 if (!text.replace("\r", "")) {
398 if (!text.replace("\r", "")) {
384 // text is nothing (empty string, \r, etc.)
399 // text is nothing (empty string, \r, etc.)
385 // so don't append any elements, which might add undesirable space
400 // so don't append any elements, which might add undesirable space
386 return;
401 return;
387 }
402 }
388
403
389 // If we got here, attach a new div
404 // If we got here, attach a new div
390 var toinsert = this.create_output_area();
405 var toinsert = this.create_output_area();
391 this.append_text(text, {}, toinsert, "output_stream "+subclass);
406 this.append_text(text, {}, toinsert, "output_stream "+subclass);
392 this.element.append(toinsert);
407 this._safe_append(toinsert);
393 };
408 };
394
409
395
410
396 OutputArea.prototype.append_display_data = function (json, dynamic) {
411 OutputArea.prototype.append_display_data = function (json, dynamic) {
397 var toinsert = this.create_output_area();
412 var toinsert = this.create_output_area();
398 this.append_mime_type(json, toinsert, dynamic);
413 this.append_mime_type(json, toinsert, dynamic);
399 this.element.append(toinsert);
414 this._safe_append(toinsert);
400 // If we just output latex, typeset it.
415 // If we just output latex, typeset it.
401 if ( (json.latex !== undefined) || (json.html !== undefined) ) {
416 if ( (json.latex !== undefined) || (json.html !== undefined) ) {
402 this.typeset();
417 this.typeset();
403 }
418 }
404 };
419 };
405
420
406 OutputArea.display_order = ['javascript','html','latex','svg','png','jpeg','text'];
421 OutputArea.display_order = ['javascript','html','latex','svg','png','jpeg','text'];
407
422
408 OutputArea.prototype.append_mime_type = function (json, element, dynamic) {
423 OutputArea.prototype.append_mime_type = function (json, element, dynamic) {
409 for(var type_i in OutputArea.display_order){
424 for(var type_i in OutputArea.display_order){
410 var type = OutputArea.display_order[type_i];
425 var type = OutputArea.display_order[type_i];
411 if(json[type] != undefined ){
426 if(json[type] != undefined ){
412 var md = {};
427 var md = {};
413 if (json.metadata && json.metadata[type]) {
428 if (json.metadata && json.metadata[type]) {
414 md = json.metadata[type];
429 md = json.metadata[type];
415 };
430 };
416 if(type == 'javascript'){
431 if(type == 'javascript'){
417 if (dynamic) {
432 if (dynamic) {
418 this.append_javascript(json.javascript, md, element, dynamic);
433 this.append_javascript(json.javascript, md, element, dynamic);
419 }
434 }
420 } else {
435 } else {
421 this['append_'+type](json[type], md, element);
436 this['append_'+type](json[type], md, element);
422 }
437 }
423 return;
438 return;
424 }
439 }
425 }
440 }
426 };
441 };
427
442
428
443
429 OutputArea.prototype.append_html = function (html, md, element) {
444 OutputArea.prototype.append_html = function (html, md, element) {
430 var toinsert = $("<div/>").addClass("output_subarea output_html rendered_html");
445 var toinsert = $("<div/>").addClass("output_subarea output_html rendered_html");
431 toinsert.append(html);
446 toinsert.append(html);
432 element.append(toinsert);
447 element.append(toinsert);
433 };
448 };
434
449
435
450
436 OutputArea.prototype.append_javascript = function (js, md, container) {
451 OutputArea.prototype.append_javascript = function (js, md, container) {
437 // We just eval the JS code, element appears in the local scope.
452 // We just eval the JS code, element appears in the local scope.
438 var element = $("<div/>").addClass("output_subarea");
453 var element = $("<div/>").addClass("output_subarea");
439 container.append(element);
454 container.append(element);
440 // Div for js shouldn't be drawn, as it will add empty height to the area.
455 // Div for js shouldn't be drawn, as it will add empty height to the area.
441 container.hide();
456 container.hide();
442 // If the Javascript appends content to `element` that should be drawn, then
457 // If the Javascript appends content to `element` that should be drawn, then
443 // it must also call `container.show()`.
458 // it must also call `container.show()`.
444 try {
459 try {
445 eval(js);
460 eval(js);
446 } catch(err) {
461 } catch(err) {
447 console.log('Error in Javascript!');
462 this._append_javascript_error(err, container);
448 console.log(err);
449 container.show();
450 element.append($('<div/>')
451 .html("Error in Javascript !<br/>"+
452 err.toString()+
453 '<br/>See your browser Javascript console for more details.')
454 .addClass('js-error')
455 );
456 }
463 }
457 };
464 };
458
465
459
466
460 OutputArea.prototype.append_text = function (data, md, element, extra_class) {
467 OutputArea.prototype.append_text = function (data, md, element, extra_class) {
461 var toinsert = $("<div/>").addClass("output_subarea output_text");
468 var toinsert = $("<div/>").addClass("output_subarea output_text");
462 // escape ANSI & HTML specials in plaintext:
469 // escape ANSI & HTML specials in plaintext:
463 data = utils.fixConsole(data);
470 data = utils.fixConsole(data);
464 data = utils.fixCarriageReturn(data);
471 data = utils.fixCarriageReturn(data);
465 data = utils.autoLinkUrls(data);
472 data = utils.autoLinkUrls(data);
466 if (extra_class){
473 if (extra_class){
467 toinsert.addClass(extra_class);
474 toinsert.addClass(extra_class);
468 }
475 }
469 toinsert.append($("<pre/>").html(data));
476 toinsert.append($("<pre/>").html(data));
470 element.append(toinsert);
477 element.append(toinsert);
471 };
478 };
472
479
473
480
474 OutputArea.prototype.append_svg = function (svg, md, element) {
481 OutputArea.prototype.append_svg = function (svg, md, element) {
475 var toinsert = $("<div/>").addClass("output_subarea output_svg");
482 var toinsert = $("<div/>").addClass("output_subarea output_svg");
476 toinsert.append(svg);
483 toinsert.append(svg);
477 element.append(toinsert);
484 element.append(toinsert);
478 };
485 };
479
486
480
487
481 OutputArea.prototype._dblclick_to_reset_size = function (img) {
488 OutputArea.prototype._dblclick_to_reset_size = function (img) {
482 // schedule wrapping image in resizable after a delay,
489 // schedule wrapping image in resizable after a delay,
483 // so we don't end up calling resize on a zero-size object
490 // so we don't end up calling resize on a zero-size object
484 var that = this;
491 var that = this;
485 setTimeout(function () {
492 setTimeout(function () {
486 var h0 = img.height();
493 var h0 = img.height();
487 var w0 = img.width();
494 var w0 = img.width();
488 if (!(h0 && w0)) {
495 if (!(h0 && w0)) {
489 // zero size, schedule another timeout
496 // zero size, schedule another timeout
490 that._dblclick_to_reset_size(img);
497 that._dblclick_to_reset_size(img);
491 return;
498 return;
492 }
499 }
493 img.resizable({
500 img.resizable({
494 aspectRatio: true,
501 aspectRatio: true,
495 autoHide: true
502 autoHide: true
496 });
503 });
497 img.dblclick(function () {
504 img.dblclick(function () {
498 // resize wrapper & image together for some reason:
505 // resize wrapper & image together for some reason:
499 img.parent().height(h0);
506 img.parent().height(h0);
500 img.height(h0);
507 img.height(h0);
501 img.parent().width(w0);
508 img.parent().width(w0);
502 img.width(w0);
509 img.width(w0);
503 });
510 });
504 }, 250);
511 }, 250);
505 };
512 };
506
513
507
514
508 OutputArea.prototype.append_png = function (png, md, element) {
515 OutputArea.prototype.append_png = function (png, md, element) {
509 var toinsert = $("<div/>").addClass("output_subarea output_png");
516 var toinsert = $("<div/>").addClass("output_subarea output_png");
510 var img = $("<img/>").attr('src','data:image/png;base64,'+png);
517 var img = $("<img/>").attr('src','data:image/png;base64,'+png);
511 if (md['height']) {
518 if (md['height']) {
512 img.attr('height', md['height']);
519 img.attr('height', md['height']);
513 }
520 }
514 if (md['width']) {
521 if (md['width']) {
515 img.attr('width', md['width']);
522 img.attr('width', md['width']);
516 }
523 }
517 this._dblclick_to_reset_size(img);
524 this._dblclick_to_reset_size(img);
518 toinsert.append(img);
525 toinsert.append(img);
519 element.append(toinsert);
526 element.append(toinsert);
520 };
527 };
521
528
522
529
523 OutputArea.prototype.append_jpeg = function (jpeg, md, element) {
530 OutputArea.prototype.append_jpeg = function (jpeg, md, element) {
524 var toinsert = $("<div/>").addClass("output_subarea output_jpeg");
531 var toinsert = $("<div/>").addClass("output_subarea output_jpeg");
525 var img = $("<img/>").attr('src','data:image/jpeg;base64,'+jpeg);
532 var img = $("<img/>").attr('src','data:image/jpeg;base64,'+jpeg);
526 if (md['height']) {
533 if (md['height']) {
527 img.attr('height', md['height']);
534 img.attr('height', md['height']);
528 }
535 }
529 if (md['width']) {
536 if (md['width']) {
530 img.attr('width', md['width']);
537 img.attr('width', md['width']);
531 }
538 }
532 this._dblclick_to_reset_size(img);
539 this._dblclick_to_reset_size(img);
533 toinsert.append(img);
540 toinsert.append(img);
534 element.append(toinsert);
541 element.append(toinsert);
535 };
542 };
536
543
537
544
538 OutputArea.prototype.append_latex = function (latex, md, element) {
545 OutputArea.prototype.append_latex = function (latex, md, element) {
539 // This method cannot do the typesetting because the latex first has to
546 // This method cannot do the typesetting because the latex first has to
540 // be on the page.
547 // be on the page.
541 var toinsert = $("<div/>").addClass("output_subarea output_latex");
548 var toinsert = $("<div/>").addClass("output_subarea output_latex");
542 toinsert.append(latex);
549 toinsert.append(latex);
543 element.append(toinsert);
550 element.append(toinsert);
544 };
551 };
545
552
546 OutputArea.prototype.append_raw_input = function (content) {
553 OutputArea.prototype.append_raw_input = function (content) {
547 var that = this;
554 var that = this;
548 this.expand();
555 this.expand();
549 this.flush_clear_timeout();
556 this.flush_clear_timeout();
550 var area = this.create_output_area();
557 var area = this.create_output_area();
551
558
552 // disable any other raw_inputs, if they are left around
559 // disable any other raw_inputs, if they are left around
553 $("div.output_subarea.raw_input").remove();
560 $("div.output_subarea.raw_input").remove();
554
561
555 area.append(
562 area.append(
556 $("<div/>")
563 $("<div/>")
557 .addClass("box-flex1 output_subarea raw_input")
564 .addClass("box-flex1 output_subarea raw_input")
558 .append(
565 .append(
559 $("<span/>")
566 $("<span/>")
560 .addClass("input_prompt")
567 .addClass("input_prompt")
561 .text(content.prompt)
568 .text(content.prompt)
562 )
569 )
563 .append(
570 .append(
564 $("<input/>")
571 $("<input/>")
565 .addClass("raw_input")
572 .addClass("raw_input")
566 .attr('type', 'text')
573 .attr('type', 'text')
567 .attr("size", 47)
574 .attr("size", 47)
568 .keydown(function (event, ui) {
575 .keydown(function (event, ui) {
569 // make sure we submit on enter,
576 // make sure we submit on enter,
570 // and don't re-execute the *cell* on shift-enter
577 // and don't re-execute the *cell* on shift-enter
571 if (event.which === utils.keycodes.ENTER) {
578 if (event.which === utils.keycodes.ENTER) {
572 that._submit_raw_input();
579 that._submit_raw_input();
573 return false;
580 return false;
574 }
581 }
575 })
582 })
576 )
583 )
577 );
584 );
578 this.element.append(area);
585 this.element.append(area);
579 // weirdly need double-focus now,
586 // weirdly need double-focus now,
580 // otherwise only the cell will be focused
587 // otherwise only the cell will be focused
581 area.find("input.raw_input").focus().focus();
588 area.find("input.raw_input").focus().focus();
582 }
589 }
583 OutputArea.prototype._submit_raw_input = function (evt) {
590 OutputArea.prototype._submit_raw_input = function (evt) {
584 var container = this.element.find("div.raw_input");
591 var container = this.element.find("div.raw_input");
585 var theprompt = container.find("span.input_prompt");
592 var theprompt = container.find("span.input_prompt");
586 var theinput = container.find("input.raw_input");
593 var theinput = container.find("input.raw_input");
587 var value = theinput.val();
594 var value = theinput.val();
588 var content = {
595 var content = {
589 output_type : 'stream',
596 output_type : 'stream',
590 name : 'stdout',
597 name : 'stdout',
591 text : theprompt.text() + value + '\n'
598 text : theprompt.text() + value + '\n'
592 }
599 }
593 // remove form container
600 // remove form container
594 container.parent().remove();
601 container.parent().remove();
595 // replace with plaintext version in stdout
602 // replace with plaintext version in stdout
596 this.append_output(content, false);
603 this.append_output(content, false);
597 $([IPython.events]).trigger('send_input_reply.Kernel', value);
604 $([IPython.events]).trigger('send_input_reply.Kernel', value);
598 }
605 }
599
606
600
607
601 OutputArea.prototype.handle_clear_output = function (content) {
608 OutputArea.prototype.handle_clear_output = function (content) {
602 this.clear_output(content.stdout, content.stderr, content.other);
609 this.clear_output(content.stdout, content.stderr, content.other);
603 };
610 };
604
611
605
612
606 OutputArea.prototype.clear_output = function (stdout, stderr, other) {
613 OutputArea.prototype.clear_output = function (stdout, stderr, other) {
607 var that = this;
614 var that = this;
608 if (this.clear_out_timeout != null){
615 if (this.clear_out_timeout != null){
609 // fire previous pending clear *immediately*
616 // fire previous pending clear *immediately*
610 clearTimeout(this.clear_out_timeout);
617 clearTimeout(this.clear_out_timeout);
611 this.clear_out_timeout = null;
618 this.clear_out_timeout = null;
612 this.clear_output_callback(this._clear_stdout, this._clear_stderr, this._clear_other);
619 this.clear_output_callback(this._clear_stdout, this._clear_stderr, this._clear_other);
613 }
620 }
614 // store flags for flushing the timeout
621 // store flags for flushing the timeout
615 this._clear_stdout = stdout;
622 this._clear_stdout = stdout;
616 this._clear_stderr = stderr;
623 this._clear_stderr = stderr;
617 this._clear_other = other;
624 this._clear_other = other;
618 this.clear_out_timeout = setTimeout(function() {
625 this.clear_out_timeout = setTimeout(function() {
619 // really clear timeout only after a short delay
626 // really clear timeout only after a short delay
620 // this reduces flicker in 'clear_output; print' cases
627 // this reduces flicker in 'clear_output; print' cases
621 that.clear_out_timeout = null;
628 that.clear_out_timeout = null;
622 that._clear_stdout = that._clear_stderr = that._clear_other = null;
629 that._clear_stdout = that._clear_stderr = that._clear_other = null;
623 that.clear_output_callback(stdout, stderr, other);
630 that.clear_output_callback(stdout, stderr, other);
624 }, 500
631 }, 500
625 );
632 );
626 };
633 };
627
634
628
635
629 OutputArea.prototype.clear_output_callback = function (stdout, stderr, other) {
636 OutputArea.prototype.clear_output_callback = function (stdout, stderr, other) {
630 var output_div = this.element;
637 var output_div = this.element;
631
638
632 if (stdout && stderr && other){
639 if (stdout && stderr && other){
633 // clear all, no need for logic
640 // clear all, no need for logic
634 output_div.html("");
641 output_div.html("");
635 this.outputs = [];
642 this.outputs = [];
636 this.unscroll_area();
643 this.unscroll_area();
637 return;
644 return;
638 }
645 }
639 // remove html output
646 // remove html output
640 // each output_subarea that has an identifying class is in an output_area
647 // each output_subarea that has an identifying class is in an output_area
641 // which is the element to be removed.
648 // which is the element to be removed.
642 if (stdout) {
649 if (stdout) {
643 output_div.find("div.output_stdout").parent().remove();
650 output_div.find("div.output_stdout").parent().remove();
644 }
651 }
645 if (stderr) {
652 if (stderr) {
646 output_div.find("div.output_stderr").parent().remove();
653 output_div.find("div.output_stderr").parent().remove();
647 }
654 }
648 if (other) {
655 if (other) {
649 output_div.find("div.output_subarea").not("div.output_stderr").not("div.output_stdout").parent().remove();
656 output_div.find("div.output_subarea").not("div.output_stderr").not("div.output_stdout").parent().remove();
650 }
657 }
651 this.unscroll_area();
658 this.unscroll_area();
652
659
653 // remove cleared outputs from JSON list:
660 // remove cleared outputs from JSON list:
654 for (var i = this.outputs.length - 1; i >= 0; i--) {
661 for (var i = this.outputs.length - 1; i >= 0; i--) {
655 var out = this.outputs[i];
662 var out = this.outputs[i];
656 var output_type = out.output_type;
663 var output_type = out.output_type;
657 if (output_type == "display_data" && other) {
664 if (output_type == "display_data" && other) {
658 this.outputs.splice(i,1);
665 this.outputs.splice(i,1);
659 } else if (output_type == "stream") {
666 } else if (output_type == "stream") {
660 if (stdout && out.stream == "stdout") {
667 if (stdout && out.stream == "stdout") {
661 this.outputs.splice(i,1);
668 this.outputs.splice(i,1);
662 } else if (stderr && out.stream == "stderr") {
669 } else if (stderr && out.stream == "stderr") {
663 this.outputs.splice(i,1);
670 this.outputs.splice(i,1);
664 }
671 }
665 }
672 }
666 }
673 }
667 };
674 };
668
675
669
676
670 OutputArea.prototype.flush_clear_timeout = function() {
677 OutputArea.prototype.flush_clear_timeout = function() {
671 var output_div = this.element;
678 var output_div = this.element;
672 if (this.clear_out_timeout){
679 if (this.clear_out_timeout){
673 clearTimeout(this.clear_out_timeout);
680 clearTimeout(this.clear_out_timeout);
674 this.clear_out_timeout = null;
681 this.clear_out_timeout = null;
675 this.clear_output_callback(this._clear_stdout, this._clear_stderr, this._clear_other);
682 this.clear_output_callback(this._clear_stdout, this._clear_stderr, this._clear_other);
676 }
683 }
677 };
684 };
678
685
679
686
680 // JSON serialization
687 // JSON serialization
681
688
682 OutputArea.prototype.fromJSON = function (outputs) {
689 OutputArea.prototype.fromJSON = function (outputs) {
683 var len = outputs.length;
690 var len = outputs.length;
684 for (var i=0; i<len; i++) {
691 for (var i=0; i<len; i++) {
685 // append with dynamic=false.
692 // append with dynamic=false.
686 this.append_output(outputs[i], false);
693 this.append_output(outputs[i], false);
687 }
694 }
688 };
695 };
689
696
690
697
691 OutputArea.prototype.toJSON = function () {
698 OutputArea.prototype.toJSON = function () {
692 var outputs = [];
699 var outputs = [];
693 var len = this.outputs.length;
700 var len = this.outputs.length;
694 for (var i=0; i<len; i++) {
701 for (var i=0; i<len; i++) {
695 outputs[i] = this.outputs[i];
702 outputs[i] = this.outputs[i];
696 }
703 }
697 return outputs;
704 return outputs;
698 };
705 };
699
706
700
707
701 IPython.OutputArea = OutputArea;
708 IPython.OutputArea = OutputArea;
702
709
703 return IPython;
710 return IPython;
704
711
705 }(IPython));
712 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now