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