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