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