##// END OF EJS Templates
Don't preserve height when clear_output(wait=False) is called
Jonathan Frederic -
Show More
@@ -1,668 +1,671 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_queued = null;
34 this.clear_queued = 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
292
293 // Clear the output if clear is queued.
293 // Clear the output if clear is queued.
294 if (this.clear_queued) {
294 if (this.clear_queued) {
295 this.clear_output(false);
295 this.clear_output(false);
296 }
296 }
297
297
298 if (json.output_type === 'pyout') {
298 if (json.output_type === 'pyout') {
299 this.append_pyout(json, dynamic);
299 this.append_pyout(json, dynamic);
300 } else if (json.output_type === 'pyerr') {
300 } else if (json.output_type === 'pyerr') {
301 this.append_pyerr(json);
301 this.append_pyerr(json);
302 } else if (json.output_type === 'display_data') {
302 } else if (json.output_type === 'display_data') {
303 this.append_display_data(json, dynamic);
303 this.append_display_data(json, dynamic);
304 } else if (json.output_type === 'stream') {
304 } else if (json.output_type === 'stream') {
305 this.append_stream(json);
305 this.append_stream(json);
306 }
306 }
307 this.outputs.push(json);
307 this.outputs.push(json);
308 this.element.height('auto');
308 this.element.height('auto');
309 var that = this;
309 var that = this;
310 setTimeout(function(){that.element.trigger('resize');}, 100);
310 setTimeout(function(){that.element.trigger('resize');}, 100);
311 };
311 };
312
312
313
313
314 OutputArea.prototype.create_output_area = function () {
314 OutputArea.prototype.create_output_area = function () {
315 var oa = $("<div/>").addClass("output_area");
315 var oa = $("<div/>").addClass("output_area");
316 if (this.prompt_area) {
316 if (this.prompt_area) {
317 oa.append($('<div/>').addClass('prompt'));
317 oa.append($('<div/>').addClass('prompt'));
318 }
318 }
319 return oa;
319 return oa;
320 };
320 };
321
321
322 OutputArea.prototype._append_javascript_error = function (err, container) {
322 OutputArea.prototype._append_javascript_error = function (err, container) {
323 // display a message when a javascript error occurs in display output
323 // display a message when a javascript error occurs in display output
324 var msg = "Javascript error adding output!"
324 var msg = "Javascript error adding output!"
325 console.log(msg, err);
325 console.log(msg, err);
326 if ( container === undefined ) return;
326 if ( container === undefined ) return;
327 container.append(
327 container.append(
328 $('<div/>').html(msg + "<br/>" +
328 $('<div/>').html(msg + "<br/>" +
329 err.toString() +
329 err.toString() +
330 '<br/>See your browser Javascript console for more details.'
330 '<br/>See your browser Javascript console for more details.'
331 ).addClass('js-error')
331 ).addClass('js-error')
332 );
332 );
333 container.show();
333 container.show();
334 };
334 };
335
335
336 OutputArea.prototype._safe_append = function (toinsert) {
336 OutputArea.prototype._safe_append = function (toinsert) {
337 // safely append an item to the document
337 // safely append an item to the document
338 // this is an object created by user code,
338 // this is an object created by user code,
339 // and may have errors, which should not be raised
339 // and may have errors, which should not be raised
340 // under any circumstances.
340 // under any circumstances.
341 try {
341 try {
342 this.element.append(toinsert);
342 this.element.append(toinsert);
343 } catch(err) {
343 } catch(err) {
344 console.log(err);
344 console.log(err);
345 this._append_javascript_error(err, this.element);
345 this._append_javascript_error(err, this.element);
346 }
346 }
347 };
347 };
348
348
349
349
350 OutputArea.prototype.append_pyout = function (json, dynamic) {
350 OutputArea.prototype.append_pyout = function (json, dynamic) {
351 var n = json.prompt_number || ' ';
351 var n = json.prompt_number || ' ';
352 var toinsert = this.create_output_area();
352 var toinsert = this.create_output_area();
353 if (this.prompt_area) {
353 if (this.prompt_area) {
354 toinsert.find('div.prompt').addClass('output_prompt').html('Out[' + n + ']:');
354 toinsert.find('div.prompt').addClass('output_prompt').html('Out[' + n + ']:');
355 }
355 }
356 this.append_mime_type(json, toinsert, dynamic);
356 this.append_mime_type(json, toinsert, dynamic);
357 this._safe_append(toinsert);
357 this._safe_append(toinsert);
358 // If we just output latex, typeset it.
358 // If we just output latex, typeset it.
359 if ((json.latex !== undefined) || (json.html !== undefined)) {
359 if ((json.latex !== undefined) || (json.html !== undefined)) {
360 this.typeset();
360 this.typeset();
361 }
361 }
362 };
362 };
363
363
364
364
365 OutputArea.prototype.append_pyerr = function (json) {
365 OutputArea.prototype.append_pyerr = function (json) {
366 var tb = json.traceback;
366 var tb = json.traceback;
367 if (tb !== undefined && tb.length > 0) {
367 if (tb !== undefined && tb.length > 0) {
368 var s = '';
368 var s = '';
369 var len = tb.length;
369 var len = tb.length;
370 for (var i=0; i<len; i++) {
370 for (var i=0; i<len; i++) {
371 s = s + tb[i] + '\n';
371 s = s + tb[i] + '\n';
372 }
372 }
373 s = s + '\n';
373 s = s + '\n';
374 var toinsert = this.create_output_area();
374 var toinsert = this.create_output_area();
375 this.append_text(s, {}, toinsert);
375 this.append_text(s, {}, toinsert);
376 this._safe_append(toinsert);
376 this._safe_append(toinsert);
377 }
377 }
378 };
378 };
379
379
380
380
381 OutputArea.prototype.append_stream = function (json) {
381 OutputArea.prototype.append_stream = function (json) {
382 // temporary fix: if stream undefined (json file written prior to this patch),
382 // temporary fix: if stream undefined (json file written prior to this patch),
383 // default to most likely stdout:
383 // default to most likely stdout:
384 if (json.stream == undefined){
384 if (json.stream == undefined){
385 json.stream = 'stdout';
385 json.stream = 'stdout';
386 }
386 }
387 var text = json.text;
387 var text = json.text;
388 var subclass = "output_"+json.stream;
388 var subclass = "output_"+json.stream;
389 if (this.outputs.length > 0){
389 if (this.outputs.length > 0){
390 // have at least one output to consider
390 // have at least one output to consider
391 var last = this.outputs[this.outputs.length-1];
391 var last = this.outputs[this.outputs.length-1];
392 if (last.output_type == 'stream' && json.stream == last.stream){
392 if (last.output_type == 'stream' && json.stream == last.stream){
393 // latest output was in the same stream,
393 // latest output was in the same stream,
394 // so append directly into its pre tag
394 // so append directly into its pre tag
395 // escape ANSI & HTML specials:
395 // escape ANSI & HTML specials:
396 var pre = this.element.find('div.'+subclass).last().find('pre');
396 var pre = this.element.find('div.'+subclass).last().find('pre');
397 var html = utils.fixCarriageReturn(
397 var html = utils.fixCarriageReturn(
398 pre.html() + utils.fixConsole(text));
398 pre.html() + utils.fixConsole(text));
399 pre.html(html);
399 pre.html(html);
400 return;
400 return;
401 }
401 }
402 }
402 }
403
403
404 if (!text.replace("\r", "")) {
404 if (!text.replace("\r", "")) {
405 // text is nothing (empty string, \r, etc.)
405 // text is nothing (empty string, \r, etc.)
406 // so don't append any elements, which might add undesirable space
406 // so don't append any elements, which might add undesirable space
407 return;
407 return;
408 }
408 }
409
409
410 // If we got here, attach a new div
410 // If we got here, attach a new div
411 var toinsert = this.create_output_area();
411 var toinsert = this.create_output_area();
412 this.append_text(text, {}, toinsert, "output_stream "+subclass);
412 this.append_text(text, {}, toinsert, "output_stream "+subclass);
413 this._safe_append(toinsert);
413 this._safe_append(toinsert);
414 };
414 };
415
415
416
416
417 OutputArea.prototype.append_display_data = function (json, dynamic) {
417 OutputArea.prototype.append_display_data = function (json, dynamic) {
418 var toinsert = this.create_output_area();
418 var toinsert = this.create_output_area();
419 this.append_mime_type(json, toinsert, dynamic);
419 this.append_mime_type(json, toinsert, dynamic);
420 this._safe_append(toinsert);
420 this._safe_append(toinsert);
421 // If we just output latex, typeset it.
421 // If we just output latex, typeset it.
422 if ( (json.latex !== undefined) || (json.html !== undefined) ) {
422 if ( (json.latex !== undefined) || (json.html !== undefined) ) {
423 this.typeset();
423 this.typeset();
424 }
424 }
425 };
425 };
426
426
427 OutputArea.display_order = ['javascript','html','latex','svg','png','jpeg','text'];
427 OutputArea.display_order = ['javascript','html','latex','svg','png','jpeg','text'];
428
428
429 OutputArea.prototype.append_mime_type = function (json, element, dynamic) {
429 OutputArea.prototype.append_mime_type = function (json, element, dynamic) {
430 for(var type_i in OutputArea.display_order){
430 for(var type_i in OutputArea.display_order){
431 var type = OutputArea.display_order[type_i];
431 var type = OutputArea.display_order[type_i];
432 if(json[type] != undefined ){
432 if(json[type] != undefined ){
433 var md = {};
433 var md = {};
434 if (json.metadata && json.metadata[type]) {
434 if (json.metadata && json.metadata[type]) {
435 md = json.metadata[type];
435 md = json.metadata[type];
436 };
436 };
437 if(type == 'javascript'){
437 if(type == 'javascript'){
438 if (dynamic) {
438 if (dynamic) {
439 this.append_javascript(json.javascript, md, element, dynamic);
439 this.append_javascript(json.javascript, md, element, dynamic);
440 }
440 }
441 } else {
441 } else {
442 this['append_'+type](json[type], md, element);
442 this['append_'+type](json[type], md, element);
443 }
443 }
444 return;
444 return;
445 }
445 }
446 }
446 }
447 };
447 };
448
448
449
449
450 OutputArea.prototype.append_html = function (html, md, element) {
450 OutputArea.prototype.append_html = function (html, md, element) {
451 var toinsert = $("<div/>").addClass("output_subarea output_html rendered_html");
451 var toinsert = $("<div/>").addClass("output_subarea output_html rendered_html");
452 toinsert.append(html);
452 toinsert.append(html);
453 element.append(toinsert);
453 element.append(toinsert);
454 };
454 };
455
455
456
456
457 OutputArea.prototype.append_javascript = function (js, md, container) {
457 OutputArea.prototype.append_javascript = function (js, md, container) {
458 // We just eval the JS code, element appears in the local scope.
458 // We just eval the JS code, element appears in the local scope.
459 var element = $("<div/>").addClass("output_subarea");
459 var element = $("<div/>").addClass("output_subarea");
460 container.append(element);
460 container.append(element);
461 // Div for js shouldn't be drawn, as it will add empty height to the area.
461 // Div for js shouldn't be drawn, as it will add empty height to the area.
462 container.hide();
462 container.hide();
463 // If the Javascript appends content to `element` that should be drawn, then
463 // If the Javascript appends content to `element` that should be drawn, then
464 // it must also call `container.show()`.
464 // it must also call `container.show()`.
465 try {
465 try {
466 eval(js);
466 eval(js);
467 } catch(err) {
467 } catch(err) {
468 this._append_javascript_error(err, container);
468 this._append_javascript_error(err, container);
469 }
469 }
470 };
470 };
471
471
472
472
473 OutputArea.prototype.append_text = function (data, md, element, extra_class) {
473 OutputArea.prototype.append_text = function (data, md, element, extra_class) {
474 var toinsert = $("<div/>").addClass("output_subarea output_text");
474 var toinsert = $("<div/>").addClass("output_subarea output_text");
475 // escape ANSI & HTML specials in plaintext:
475 // escape ANSI & HTML specials in plaintext:
476 data = utils.fixConsole(data);
476 data = utils.fixConsole(data);
477 data = utils.fixCarriageReturn(data);
477 data = utils.fixCarriageReturn(data);
478 data = utils.autoLinkUrls(data);
478 data = utils.autoLinkUrls(data);
479 if (extra_class){
479 if (extra_class){
480 toinsert.addClass(extra_class);
480 toinsert.addClass(extra_class);
481 }
481 }
482 toinsert.append($("<pre/>").html(data));
482 toinsert.append($("<pre/>").html(data));
483 element.append(toinsert);
483 element.append(toinsert);
484 };
484 };
485
485
486
486
487 OutputArea.prototype.append_svg = function (svg, md, element) {
487 OutputArea.prototype.append_svg = function (svg, md, element) {
488 var toinsert = $("<div/>").addClass("output_subarea output_svg");
488 var toinsert = $("<div/>").addClass("output_subarea output_svg");
489 toinsert.append(svg);
489 toinsert.append(svg);
490 element.append(toinsert);
490 element.append(toinsert);
491 };
491 };
492
492
493
493
494 OutputArea.prototype._dblclick_to_reset_size = function (img) {
494 OutputArea.prototype._dblclick_to_reset_size = function (img) {
495 // schedule wrapping image in resizable after a delay,
495 // schedule wrapping image in resizable after a delay,
496 // so we don't end up calling resize on a zero-size object
496 // so we don't end up calling resize on a zero-size object
497 var that = this;
497 var that = this;
498 setTimeout(function () {
498 setTimeout(function () {
499 var h0 = img.height();
499 var h0 = img.height();
500 var w0 = img.width();
500 var w0 = img.width();
501 if (!(h0 && w0)) {
501 if (!(h0 && w0)) {
502 // zero size, schedule another timeout
502 // zero size, schedule another timeout
503 that._dblclick_to_reset_size(img);
503 that._dblclick_to_reset_size(img);
504 return;
504 return;
505 }
505 }
506 img.resizable({
506 img.resizable({
507 aspectRatio: true,
507 aspectRatio: true,
508 autoHide: true
508 autoHide: true
509 });
509 });
510 img.dblclick(function () {
510 img.dblclick(function () {
511 // resize wrapper & image together for some reason:
511 // resize wrapper & image together for some reason:
512 img.parent().height(h0);
512 img.parent().height(h0);
513 img.height(h0);
513 img.height(h0);
514 img.parent().width(w0);
514 img.parent().width(w0);
515 img.width(w0);
515 img.width(w0);
516 });
516 });
517 }, 250);
517 }, 250);
518 };
518 };
519
519
520
520
521 OutputArea.prototype.append_png = function (png, md, element) {
521 OutputArea.prototype.append_png = function (png, md, element) {
522 var toinsert = $("<div/>").addClass("output_subarea output_png");
522 var toinsert = $("<div/>").addClass("output_subarea output_png");
523 var img = $("<img/>").attr('src','data:image/png;base64,'+png);
523 var img = $("<img/>").attr('src','data:image/png;base64,'+png);
524 if (md['height']) {
524 if (md['height']) {
525 img.attr('height', md['height']);
525 img.attr('height', md['height']);
526 }
526 }
527 if (md['width']) {
527 if (md['width']) {
528 img.attr('width', md['width']);
528 img.attr('width', md['width']);
529 }
529 }
530 this._dblclick_to_reset_size(img);
530 this._dblclick_to_reset_size(img);
531 toinsert.append(img);
531 toinsert.append(img);
532 element.append(toinsert);
532 element.append(toinsert);
533 };
533 };
534
534
535
535
536 OutputArea.prototype.append_jpeg = function (jpeg, md, element) {
536 OutputArea.prototype.append_jpeg = function (jpeg, md, element) {
537 var toinsert = $("<div/>").addClass("output_subarea output_jpeg");
537 var toinsert = $("<div/>").addClass("output_subarea output_jpeg");
538 var img = $("<img/>").attr('src','data:image/jpeg;base64,'+jpeg);
538 var img = $("<img/>").attr('src','data:image/jpeg;base64,'+jpeg);
539 if (md['height']) {
539 if (md['height']) {
540 img.attr('height', md['height']);
540 img.attr('height', md['height']);
541 }
541 }
542 if (md['width']) {
542 if (md['width']) {
543 img.attr('width', md['width']);
543 img.attr('width', md['width']);
544 }
544 }
545 this._dblclick_to_reset_size(img);
545 this._dblclick_to_reset_size(img);
546 toinsert.append(img);
546 toinsert.append(img);
547 element.append(toinsert);
547 element.append(toinsert);
548 };
548 };
549
549
550
550
551 OutputArea.prototype.append_latex = function (latex, md, element) {
551 OutputArea.prototype.append_latex = function (latex, md, element) {
552 // This method cannot do the typesetting because the latex first has to
552 // This method cannot do the typesetting because the latex first has to
553 // be on the page.
553 // be on the page.
554 var toinsert = $("<div/>").addClass("output_subarea output_latex");
554 var toinsert = $("<div/>").addClass("output_subarea output_latex");
555 toinsert.append(latex);
555 toinsert.append(latex);
556 element.append(toinsert);
556 element.append(toinsert);
557 };
557 };
558
558
559 OutputArea.prototype.append_raw_input = function (content) {
559 OutputArea.prototype.append_raw_input = function (content) {
560 var that = this;
560 var that = this;
561 this.expand();
561 this.expand();
562 var area = this.create_output_area();
562 var area = this.create_output_area();
563
563
564 // disable any other raw_inputs, if they are left around
564 // disable any other raw_inputs, if they are left around
565 $("div.output_subarea.raw_input").remove();
565 $("div.output_subarea.raw_input").remove();
566
566
567 area.append(
567 area.append(
568 $("<div/>")
568 $("<div/>")
569 .addClass("box-flex1 output_subarea raw_input")
569 .addClass("box-flex1 output_subarea raw_input")
570 .append(
570 .append(
571 $("<span/>")
571 $("<span/>")
572 .addClass("input_prompt")
572 .addClass("input_prompt")
573 .text(content.prompt)
573 .text(content.prompt)
574 )
574 )
575 .append(
575 .append(
576 $("<input/>")
576 $("<input/>")
577 .addClass("raw_input")
577 .addClass("raw_input")
578 .attr('type', 'text')
578 .attr('type', 'text')
579 .attr("size", 47)
579 .attr("size", 47)
580 .keydown(function (event, ui) {
580 .keydown(function (event, ui) {
581 // make sure we submit on enter,
581 // make sure we submit on enter,
582 // and don't re-execute the *cell* on shift-enter
582 // and don't re-execute the *cell* on shift-enter
583 if (event.which === utils.keycodes.ENTER) {
583 if (event.which === utils.keycodes.ENTER) {
584 that._submit_raw_input();
584 that._submit_raw_input();
585 return false;
585 return false;
586 }
586 }
587 })
587 })
588 )
588 )
589 );
589 );
590 this.element.append(area);
590 this.element.append(area);
591 // weirdly need double-focus now,
591 // weirdly need double-focus now,
592 // otherwise only the cell will be focused
592 // otherwise only the cell will be focused
593 area.find("input.raw_input").focus().focus();
593 area.find("input.raw_input").focus().focus();
594 }
594 }
595 OutputArea.prototype._submit_raw_input = function (evt) {
595 OutputArea.prototype._submit_raw_input = function (evt) {
596 var container = this.element.find("div.raw_input");
596 var container = this.element.find("div.raw_input");
597 var theprompt = container.find("span.input_prompt");
597 var theprompt = container.find("span.input_prompt");
598 var theinput = container.find("input.raw_input");
598 var theinput = container.find("input.raw_input");
599 var value = theinput.val();
599 var value = theinput.val();
600 var content = {
600 var content = {
601 output_type : 'stream',
601 output_type : 'stream',
602 name : 'stdout',
602 name : 'stdout',
603 text : theprompt.text() + value + '\n'
603 text : theprompt.text() + value + '\n'
604 }
604 }
605 // remove form container
605 // remove form container
606 container.parent().remove();
606 container.parent().remove();
607 // replace with plaintext version in stdout
607 // replace with plaintext version in stdout
608 this.append_output(content, false);
608 this.append_output(content, false);
609 $([IPython.events]).trigger('send_input_reply.Kernel', value);
609 $([IPython.events]).trigger('send_input_reply.Kernel', value);
610 }
610 }
611
611
612
612
613 OutputArea.prototype.handle_clear_output = function (content) {
613 OutputArea.prototype.handle_clear_output = function (content) {
614 this.clear_output(content.wait);
614 this.clear_output(content.wait);
615 };
615 };
616
616
617
617
618 OutputArea.prototype.clear_output = function(wait) {
618 OutputArea.prototype.clear_output = function(wait) {
619 if (wait) {
619 if (wait) {
620
620
621 // If a clear is queued, clear before adding another to the queue.
621 // If a clear is queued, clear before adding another to the queue.
622 if (this.clear_queued) {
622 if (this.clear_queued) {
623 this.clear_output(false);
623 this.clear_output(false);
624 };
624 };
625
625
626 this.clear_queued = true;
626 this.clear_queued = true;
627 } else {
627 } else {
628 this.clear_queued = false;
629
630 // Fix the output div's height
631 var height = this.element.height();
632 this.element.height(height);
633
628
629 // Fix the output div's height if the clear_output is waiting for
630 // new output (it is being used in an animation).
631 if (this.clear_queued) {
632 var height = this.element.height();
633 this.element.height(height);
634 this.clear_queued = false;
635 }
636
634 // clear all, no need for logic
637 // clear all, no need for logic
635 this.element.html("");
638 this.element.html("");
636 this.outputs = [];
639 this.outputs = [];
637 this.unscroll_area();
640 this.unscroll_area();
638 return;
641 return;
639 };
642 };
640 };
643 };
641
644
642
645
643 // JSON serialization
646 // JSON serialization
644
647
645 OutputArea.prototype.fromJSON = function (outputs) {
648 OutputArea.prototype.fromJSON = function (outputs) {
646 var len = outputs.length;
649 var len = outputs.length;
647 for (var i=0; i<len; i++) {
650 for (var i=0; i<len; i++) {
648 // append with dynamic=false.
651 // append with dynamic=false.
649 this.append_output(outputs[i], false);
652 this.append_output(outputs[i], false);
650 }
653 }
651 };
654 };
652
655
653
656
654 OutputArea.prototype.toJSON = function () {
657 OutputArea.prototype.toJSON = function () {
655 var outputs = [];
658 var outputs = [];
656 var len = this.outputs.length;
659 var len = this.outputs.length;
657 for (var i=0; i<len; i++) {
660 for (var i=0; i<len; i++) {
658 outputs[i] = this.outputs[i];
661 outputs[i] = this.outputs[i];
659 }
662 }
660 return outputs;
663 return outputs;
661 };
664 };
662
665
663
666
664 IPython.OutputArea = OutputArea;
667 IPython.OutputArea = OutputArea;
665
668
666 return IPython;
669 return IPython;
667
670
668 }(IPython));
671 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now