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