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