##// END OF EJS Templates
dblclick/double click for humans
MinRK -
Show More
@@ -1,518 +1,518 b''
1 //----------------------------------------------------------------------------
1 //----------------------------------------------------------------------------
2 // Copyright (C) 2008-2011 The IPython Development Team
2 // Copyright (C) 2008-2011 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 var IPython = (function (IPython) {
12 var IPython = (function (IPython) {
13 "use strict";
13 "use strict";
14
14
15 var utils = IPython.utils;
15 var utils = IPython.utils;
16
16
17 var OutputArea = function (selector, prompt_area) {
17 var OutputArea = function (selector, prompt_area) {
18 this.selector = selector;
18 this.selector = selector;
19 this.wrapper = $(selector);
19 this.wrapper = $(selector);
20 this.outputs = [];
20 this.outputs = [];
21 this.collapsed = false;
21 this.collapsed = false;
22 this.scrolled = false;
22 this.scrolled = false;
23 this.clear_out_timeout = null;
23 this.clear_out_timeout = null;
24 if (prompt_area === undefined) {
24 if (prompt_area === undefined) {
25 this.prompt_area = true;
25 this.prompt_area = true;
26 } else {
26 } else {
27 this.prompt_area = prompt_area;
27 this.prompt_area = prompt_area;
28 };
28 };
29 this.create_elements();
29 this.create_elements();
30 this.style();
30 this.style();
31 this.bind_events();
31 this.bind_events();
32 };
32 };
33
33
34 OutputArea.prototype.create_elements = function () {
34 OutputArea.prototype.create_elements = function () {
35 this.element = $("<div/>");
35 this.element = $("<div/>");
36 this.collapse_button = $("<div/>");
36 this.collapse_button = $("<div/>");
37 this.prompt_overlay = $("<div/>");
37 this.prompt_overlay = $("<div/>");
38 this.wrapper.append(this.prompt_overlay);
38 this.wrapper.append(this.prompt_overlay);
39 this.wrapper.append(this.element);
39 this.wrapper.append(this.element);
40 this.wrapper.append(this.collapse_button);
40 this.wrapper.append(this.collapse_button);
41 };
41 };
42
42
43
43
44 OutputArea.prototype.style = function () {
44 OutputArea.prototype.style = function () {
45 this.collapse_button.hide();
45 this.collapse_button.hide();
46 this.prompt_overlay.hide();
46 this.prompt_overlay.hide();
47
47
48 this.wrapper.addClass('output_wrapper');
48 this.wrapper.addClass('output_wrapper');
49 this.element.addClass('output vbox');
49 this.element.addClass('output vbox');
50
50
51 this.collapse_button.button();
51 this.collapse_button.button();
52 this.collapse_button.addClass('output_collapsed vbox');
52 this.collapse_button.addClass('output_collapsed vbox');
53 this.collapse_button.attr('title', 'click to expand outout');
53 this.collapse_button.attr('title', 'click to expand outout');
54 this.collapse_button.html('. . .');
54 this.collapse_button.html('. . .');
55
55
56 this.prompt_overlay.addClass('out_prompt_overlay prompt');
56 this.prompt_overlay.addClass('out_prompt_overlay prompt');
57 this.prompt_overlay.attr('title', 'click to expand outout; dblclick to hide output');
57 this.prompt_overlay.attr('title', 'click to expand outout; double click to hide output');
58
58
59 this.collapse();
59 this.collapse();
60 };
60 };
61
61
62
62
63 OutputArea.prototype._should_scroll = function (lines) {
63 OutputArea.prototype._should_scroll = function (lines) {
64 if (!lines) {
64 if (!lines) {
65 lines = 50;
65 lines = 50;
66 }
66 }
67 // line-height from http://stackoverflow.com/questions/1185151
67 // line-height from http://stackoverflow.com/questions/1185151
68 var fontSize = this.element.css('font-size');
68 var fontSize = this.element.css('font-size');
69 var lineHeight = Math.floor(parseInt(fontSize.replace('px','')) * 1.5);
69 var lineHeight = Math.floor(parseInt(fontSize.replace('px','')) * 1.5);
70
70
71 return (this.element.height() > lines * lineHeight);
71 return (this.element.height() > lines * lineHeight);
72 };
72 };
73
73
74
74
75 OutputArea.prototype.bind_events = function () {
75 OutputArea.prototype.bind_events = function () {
76 var that = this;
76 var that = this;
77 this.prompt_overlay.dblclick(function () { that.toggle_output(); });
77 this.prompt_overlay.dblclick(function () { that.toggle_output(); });
78 this.prompt_overlay.click(function () { that.toggle_scroll(); });
78 this.prompt_overlay.click(function () { that.toggle_scroll(); });
79
79
80 this.element.resize(function () {
80 this.element.resize(function () {
81 // maybe scroll output,
81 // maybe scroll output,
82 // if it's grown large enough and hasn't already been scrolled.
82 // if it's grown large enough and hasn't already been scrolled.
83 if ( !that.scrolled && that._should_scroll()) {
83 if ( !that.scrolled && that._should_scroll()) {
84 that.scroll_area();
84 that.scroll_area();
85 }
85 }
86 });
86 });
87 this.collapse_button.click(function () {
87 this.collapse_button.click(function () {
88 that.expand();
88 that.expand();
89 });
89 });
90 this.collapse_button.hover(function () {
90 this.collapse_button.hover(function () {
91 $(this).addClass("ui-state-hover");
91 $(this).addClass("ui-state-hover");
92 }, function () {
92 }, function () {
93 $(this).removeClass("ui-state-hover");
93 $(this).removeClass("ui-state-hover");
94 });
94 });
95 };
95 };
96
96
97
97
98 OutputArea.prototype.collapse = function () {
98 OutputArea.prototype.collapse = function () {
99 if (!this.collapsed) {
99 if (!this.collapsed) {
100 this.element.hide();
100 this.element.hide();
101 this.prompt_overlay.hide();
101 this.prompt_overlay.hide();
102 if (this.element.html()){
102 if (this.element.html()){
103 this.collapse_button.show();
103 this.collapse_button.show();
104 }
104 }
105 this.collapsed = true;
105 this.collapsed = true;
106 };
106 };
107 };
107 };
108
108
109
109
110 OutputArea.prototype.expand = function () {
110 OutputArea.prototype.expand = function () {
111 if (this.collapsed) {
111 if (this.collapsed) {
112 this.collapse_button.hide();
112 this.collapse_button.hide();
113 this.element.show();
113 this.element.show();
114 this.prompt_overlay.show();
114 this.prompt_overlay.show();
115 this.collapsed = false;
115 this.collapsed = false;
116 };
116 };
117 };
117 };
118
118
119
119
120 OutputArea.prototype.toggle_output = function () {
120 OutputArea.prototype.toggle_output = function () {
121 if (this.collapsed) {
121 if (this.collapsed) {
122 this.expand();
122 this.expand();
123 } else {
123 } else {
124 this.collapse();
124 this.collapse();
125 };
125 };
126 };
126 };
127
127
128
128
129 OutputArea.prototype.scroll_area = function () {
129 OutputArea.prototype.scroll_area = function () {
130 this.element.addClass('output_scroll');
130 this.element.addClass('output_scroll');
131 this.prompt_overlay.attr('title', 'click to unscroll output; dblclick to hide');
131 this.prompt_overlay.attr('title', 'click to unscroll output; double click to hide');
132 this.scrolled = true;
132 this.scrolled = true;
133 };
133 };
134
134
135
135
136 OutputArea.prototype.unscroll_area = function () {
136 OutputArea.prototype.unscroll_area = function () {
137 this.element.removeClass('output_scroll');
137 this.element.removeClass('output_scroll');
138 this.prompt_overlay.attr('title', 'click to scroll output; dblclick to hide');
138 this.prompt_overlay.attr('title', 'click to scroll output; double click to hide');
139 this.scrolled = false;
139 this.scrolled = false;
140 };
140 };
141
141
142
142
143 OutputArea.prototype.scroll_if_long = function (lines) {
143 OutputArea.prototype.scroll_if_long = function (lines) {
144 if (this._should_scroll(lines)) {
144 if (this._should_scroll(lines)) {
145 // only allow scrolling long-enough output
145 // only allow scrolling long-enough output
146 this.scroll_area();
146 this.scroll_area();
147 };
147 };
148 };
148 };
149
149
150
150
151 OutputArea.prototype.toggle_scroll = function () {
151 OutputArea.prototype.toggle_scroll = function () {
152 if (this.scrolled) {
152 if (this.scrolled) {
153 this.unscroll_area();
153 this.unscroll_area();
154 } else {
154 } else {
155 // only allow scrolling long-enough output
155 // only allow scrolling long-enough output
156 this.scroll_if_long(20);
156 this.scroll_if_long(20);
157 };
157 };
158 };
158 };
159
159
160
160
161 // typeset with MathJax if MathJax is available
161 // typeset with MathJax if MathJax is available
162 OutputArea.prototype.typeset = function () {
162 OutputArea.prototype.typeset = function () {
163 if (window.MathJax){
163 if (window.MathJax){
164 MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
164 MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
165 }
165 }
166 };
166 };
167
167
168
168
169 OutputArea.prototype.handle_output = function (msg_type, content) {
169 OutputArea.prototype.handle_output = function (msg_type, content) {
170 var json = {};
170 var json = {};
171 json.output_type = msg_type;
171 json.output_type = msg_type;
172 if (msg_type === "stream") {
172 if (msg_type === "stream") {
173 json.text = content.data;
173 json.text = content.data;
174 json.stream = content.name;
174 json.stream = content.name;
175 } else if (msg_type === "display_data") {
175 } else if (msg_type === "display_data") {
176 json = this.convert_mime_types(json, content.data);
176 json = this.convert_mime_types(json, content.data);
177 } else if (msg_type === "pyout") {
177 } else if (msg_type === "pyout") {
178 json.prompt_number = content.execution_count;
178 json.prompt_number = content.execution_count;
179 json = this.convert_mime_types(json, content.data);
179 json = this.convert_mime_types(json, content.data);
180 } else if (msg_type === "pyerr") {
180 } else if (msg_type === "pyerr") {
181 json.ename = content.ename;
181 json.ename = content.ename;
182 json.evalue = content.evalue;
182 json.evalue = content.evalue;
183 json.traceback = content.traceback;
183 json.traceback = content.traceback;
184 };
184 };
185 // append with dynamic=true
185 // append with dynamic=true
186 this.append_output(json, true);
186 this.append_output(json, true);
187 };
187 };
188
188
189
189
190 OutputArea.prototype.convert_mime_types = function (json, data) {
190 OutputArea.prototype.convert_mime_types = function (json, data) {
191 if (data['text/plain'] !== undefined) {
191 if (data['text/plain'] !== undefined) {
192 json.text = data['text/plain'];
192 json.text = data['text/plain'];
193 };
193 };
194 if (data['text/html'] !== undefined) {
194 if (data['text/html'] !== undefined) {
195 json.html = data['text/html'];
195 json.html = data['text/html'];
196 };
196 };
197 if (data['image/svg+xml'] !== undefined) {
197 if (data['image/svg+xml'] !== undefined) {
198 json.svg = data['image/svg+xml'];
198 json.svg = data['image/svg+xml'];
199 };
199 };
200 if (data['image/png'] !== undefined) {
200 if (data['image/png'] !== undefined) {
201 json.png = data['image/png'];
201 json.png = data['image/png'];
202 };
202 };
203 if (data['image/jpeg'] !== undefined) {
203 if (data['image/jpeg'] !== undefined) {
204 json.jpeg = data['image/jpeg'];
204 json.jpeg = data['image/jpeg'];
205 };
205 };
206 if (data['text/latex'] !== undefined) {
206 if (data['text/latex'] !== undefined) {
207 json.latex = data['text/latex'];
207 json.latex = data['text/latex'];
208 };
208 };
209 if (data['application/json'] !== undefined) {
209 if (data['application/json'] !== undefined) {
210 json.json = data['application/json'];
210 json.json = data['application/json'];
211 };
211 };
212 if (data['application/javascript'] !== undefined) {
212 if (data['application/javascript'] !== undefined) {
213 json.javascript = data['application/javascript'];
213 json.javascript = data['application/javascript'];
214 }
214 }
215 return json;
215 return json;
216 };
216 };
217
217
218
218
219 OutputArea.prototype.append_output = function (json, dynamic) {
219 OutputArea.prototype.append_output = function (json, dynamic) {
220 // If dynamic is true, javascript output will be eval'd.
220 // If dynamic is true, javascript output will be eval'd.
221 this.expand();
221 this.expand();
222 this.flush_clear_timeout();
222 this.flush_clear_timeout();
223 if (json.output_type === 'pyout') {
223 if (json.output_type === 'pyout') {
224 this.append_pyout(json, dynamic);
224 this.append_pyout(json, dynamic);
225 } else if (json.output_type === 'pyerr') {
225 } else if (json.output_type === 'pyerr') {
226 this.append_pyerr(json);
226 this.append_pyerr(json);
227 } else if (json.output_type === 'display_data') {
227 } else if (json.output_type === 'display_data') {
228 this.append_display_data(json, dynamic);
228 this.append_display_data(json, dynamic);
229 } else if (json.output_type === 'stream') {
229 } else if (json.output_type === 'stream') {
230 this.append_stream(json);
230 this.append_stream(json);
231 };
231 };
232 this.outputs.push(json);
232 this.outputs.push(json);
233 var that = this;
233 var that = this;
234 setTimeout(function(){that.element.trigger('resize');}, 100);
234 setTimeout(function(){that.element.trigger('resize');}, 100);
235 };
235 };
236
236
237
237
238 OutputArea.prototype.create_output_area = function () {
238 OutputArea.prototype.create_output_area = function () {
239 var oa = $("<div/>").addClass("hbox output_area");
239 var oa = $("<div/>").addClass("hbox output_area");
240 if (this.prompt_area) {
240 if (this.prompt_area) {
241 oa.append($('<div/>').addClass('prompt'));
241 oa.append($('<div/>').addClass('prompt'));
242 }
242 }
243 return oa;
243 return oa;
244 };
244 };
245
245
246
246
247 OutputArea.prototype.append_pyout = function (json, dynamic) {
247 OutputArea.prototype.append_pyout = function (json, dynamic) {
248 var n = json.prompt_number || ' ';
248 var n = json.prompt_number || ' ';
249 var toinsert = this.create_output_area();
249 var toinsert = this.create_output_area();
250 if (this.prompt_area) {
250 if (this.prompt_area) {
251 toinsert.find('div.prompt').addClass('output_prompt').html('Out[' + n + ']:');
251 toinsert.find('div.prompt').addClass('output_prompt').html('Out[' + n + ']:');
252 }
252 }
253 this.append_mime_type(json, toinsert, dynamic);
253 this.append_mime_type(json, toinsert, dynamic);
254 this.element.append(toinsert);
254 this.element.append(toinsert);
255 // If we just output latex, typeset it.
255 // If we just output latex, typeset it.
256 if ((json.latex !== undefined) || (json.html !== undefined)) {
256 if ((json.latex !== undefined) || (json.html !== undefined)) {
257 this.typeset();
257 this.typeset();
258 };
258 };
259 };
259 };
260
260
261
261
262 OutputArea.prototype.append_pyerr = function (json) {
262 OutputArea.prototype.append_pyerr = function (json) {
263 var tb = json.traceback;
263 var tb = json.traceback;
264 if (tb !== undefined && tb.length > 0) {
264 if (tb !== undefined && tb.length > 0) {
265 var s = '';
265 var s = '';
266 var len = tb.length;
266 var len = tb.length;
267 for (var i=0; i<len; i++) {
267 for (var i=0; i<len; i++) {
268 s = s + tb[i] + '\n';
268 s = s + tb[i] + '\n';
269 }
269 }
270 s = s + '\n';
270 s = s + '\n';
271 var toinsert = this.create_output_area();
271 var toinsert = this.create_output_area();
272 this.append_text(s, toinsert);
272 this.append_text(s, toinsert);
273 this.element.append(toinsert);
273 this.element.append(toinsert);
274 };
274 };
275 };
275 };
276
276
277
277
278 OutputArea.prototype.append_stream = function (json) {
278 OutputArea.prototype.append_stream = function (json) {
279 // temporary fix: if stream undefined (json file written prior to this patch),
279 // temporary fix: if stream undefined (json file written prior to this patch),
280 // default to most likely stdout:
280 // default to most likely stdout:
281 if (json.stream == undefined){
281 if (json.stream == undefined){
282 json.stream = 'stdout';
282 json.stream = 'stdout';
283 }
283 }
284 var text = json.text;
284 var text = json.text;
285 var subclass = "output_"+json.stream;
285 var subclass = "output_"+json.stream;
286 if (this.outputs.length > 0){
286 if (this.outputs.length > 0){
287 // have at least one output to consider
287 // have at least one output to consider
288 var last = this.outputs[this.outputs.length-1];
288 var last = this.outputs[this.outputs.length-1];
289 if (last.output_type == 'stream' && json.stream == last.stream){
289 if (last.output_type == 'stream' && json.stream == last.stream){
290 // latest output was in the same stream,
290 // latest output was in the same stream,
291 // so append directly into its pre tag
291 // so append directly into its pre tag
292 // escape ANSI & HTML specials:
292 // escape ANSI & HTML specials:
293 var pre = this.element.find('div.'+subclass).last().find('pre');
293 var pre = this.element.find('div.'+subclass).last().find('pre');
294 var html = utils.fixCarriageReturn(
294 var html = utils.fixCarriageReturn(
295 pre.html() + utils.fixConsole(text));
295 pre.html() + utils.fixConsole(text));
296 pre.html(html);
296 pre.html(html);
297 return;
297 return;
298 }
298 }
299 }
299 }
300
300
301 if (!text.replace("\r", "")) {
301 if (!text.replace("\r", "")) {
302 // text is nothing (empty string, \r, etc.)
302 // text is nothing (empty string, \r, etc.)
303 // so don't append any elements, which might add undesirable space
303 // so don't append any elements, which might add undesirable space
304 return;
304 return;
305 }
305 }
306
306
307 // If we got here, attach a new div
307 // If we got here, attach a new div
308 var toinsert = this.create_output_area();
308 var toinsert = this.create_output_area();
309 this.append_text(text, toinsert, "output_stream "+subclass);
309 this.append_text(text, toinsert, "output_stream "+subclass);
310 this.element.append(toinsert);
310 this.element.append(toinsert);
311 };
311 };
312
312
313
313
314 OutputArea.prototype.append_display_data = function (json, dynamic) {
314 OutputArea.prototype.append_display_data = function (json, dynamic) {
315 var toinsert = this.create_output_area();
315 var toinsert = this.create_output_area();
316 this.append_mime_type(json, toinsert, dynamic);
316 this.append_mime_type(json, toinsert, dynamic);
317 this.element.append(toinsert);
317 this.element.append(toinsert);
318 // If we just output latex, typeset it.
318 // If we just output latex, typeset it.
319 if ( (json.latex !== undefined) || (json.html !== undefined) ) {
319 if ( (json.latex !== undefined) || (json.html !== undefined) ) {
320 this.typeset();
320 this.typeset();
321 };
321 };
322 };
322 };
323
323
324
324
325 OutputArea.prototype.append_mime_type = function (json, element, dynamic) {
325 OutputArea.prototype.append_mime_type = function (json, element, dynamic) {
326 if (json.javascript !== undefined && dynamic) {
326 if (json.javascript !== undefined && dynamic) {
327 this.append_javascript(json.javascript, element, dynamic);
327 this.append_javascript(json.javascript, element, dynamic);
328 } else if (json.html !== undefined) {
328 } else if (json.html !== undefined) {
329 this.append_html(json.html, element);
329 this.append_html(json.html, element);
330 } else if (json.latex !== undefined) {
330 } else if (json.latex !== undefined) {
331 this.append_latex(json.latex, element);
331 this.append_latex(json.latex, element);
332 } else if (json.svg !== undefined) {
332 } else if (json.svg !== undefined) {
333 this.append_svg(json.svg, element);
333 this.append_svg(json.svg, element);
334 } else if (json.png !== undefined) {
334 } else if (json.png !== undefined) {
335 this.append_png(json.png, element);
335 this.append_png(json.png, element);
336 } else if (json.jpeg !== undefined) {
336 } else if (json.jpeg !== undefined) {
337 this.append_jpeg(json.jpeg, element);
337 this.append_jpeg(json.jpeg, element);
338 } else if (json.text !== undefined) {
338 } else if (json.text !== undefined) {
339 this.append_text(json.text, element);
339 this.append_text(json.text, element);
340 };
340 };
341 };
341 };
342
342
343
343
344 OutputArea.prototype.append_html = function (html, element) {
344 OutputArea.prototype.append_html = function (html, element) {
345 var toinsert = $("<div/>").addClass("box-flex1 output_subarea output_html rendered_html");
345 var toinsert = $("<div/>").addClass("box-flex1 output_subarea output_html rendered_html");
346 toinsert.append(html);
346 toinsert.append(html);
347 element.append(toinsert);
347 element.append(toinsert);
348 };
348 };
349
349
350
350
351 OutputArea.prototype.append_javascript = function (js, container) {
351 OutputArea.prototype.append_javascript = function (js, container) {
352 // We just eval the JS code, element appears in the local scope.
352 // We just eval the JS code, element appears in the local scope.
353 var element = $("<div/>").addClass("box-flex1 output_subarea");
353 var element = $("<div/>").addClass("box-flex1 output_subarea");
354 container.append(element);
354 container.append(element);
355 // Div for js shouldn't be drawn, as it will add empty height to the area.
355 // Div for js shouldn't be drawn, as it will add empty height to the area.
356 container.hide();
356 container.hide();
357 // If the Javascript appends content to `element` that should be drawn, then
357 // If the Javascript appends content to `element` that should be drawn, then
358 // it must also call `container.show()`.
358 // it must also call `container.show()`.
359 eval(js);
359 eval(js);
360 }
360 }
361
361
362
362
363 OutputArea.prototype.append_text = function (data, element, extra_class) {
363 OutputArea.prototype.append_text = function (data, element, extra_class) {
364 var toinsert = $("<div/>").addClass("box-flex1 output_subarea output_text");
364 var toinsert = $("<div/>").addClass("box-flex1 output_subarea output_text");
365 // escape ANSI & HTML specials in plaintext:
365 // escape ANSI & HTML specials in plaintext:
366 data = utils.fixConsole(data);
366 data = utils.fixConsole(data);
367 data = utils.fixCarriageReturn(data);
367 data = utils.fixCarriageReturn(data);
368 if (extra_class){
368 if (extra_class){
369 toinsert.addClass(extra_class);
369 toinsert.addClass(extra_class);
370 }
370 }
371 toinsert.append($("<pre/>").html(data));
371 toinsert.append($("<pre/>").html(data));
372 element.append(toinsert);
372 element.append(toinsert);
373 };
373 };
374
374
375
375
376 OutputArea.prototype.append_svg = function (svg, element) {
376 OutputArea.prototype.append_svg = function (svg, element) {
377 var toinsert = $("<div/>").addClass("box-flex1 output_subarea output_svg");
377 var toinsert = $("<div/>").addClass("box-flex1 output_subarea output_svg");
378 toinsert.append(svg);
378 toinsert.append(svg);
379 element.append(toinsert);
379 element.append(toinsert);
380 };
380 };
381
381
382
382
383 OutputArea.prototype.append_png = function (png, element) {
383 OutputArea.prototype.append_png = function (png, element) {
384 var toinsert = $("<div/>").addClass("box-flex1 output_subarea output_png");
384 var toinsert = $("<div/>").addClass("box-flex1 output_subarea output_png");
385 var img = $("<img/>").attr('src','data:image/png;base64,'+png);
385 var img = $("<img/>").attr('src','data:image/png;base64,'+png);
386 img.load(function () {
386 img.load(function () {
387 $(this).resizable({'aspectRatio': true, 'autoHide': true})
387 $(this).resizable({'aspectRatio': true, 'autoHide': true})
388 });
388 });
389 toinsert.append(img);
389 toinsert.append(img);
390 element.append(toinsert);
390 element.append(toinsert);
391 };
391 };
392
392
393
393
394 OutputArea.prototype.append_jpeg = function (jpeg, element) {
394 OutputArea.prototype.append_jpeg = function (jpeg, element) {
395 var toinsert = $("<div/>").addClass("box-flex1 output_subarea output_jpeg");
395 var toinsert = $("<div/>").addClass("box-flex1 output_subarea output_jpeg");
396 var img = $("<img/>").attr('src','data:image/jpeg;base64,'+jpeg);
396 var img = $("<img/>").attr('src','data:image/jpeg;base64,'+jpeg);
397 img.load(function () {
397 img.load(function () {
398 $(this).resizable({'aspectRatio': true, 'autoHide': true})
398 $(this).resizable({'aspectRatio': true, 'autoHide': true})
399 });
399 });
400 toinsert.append(img);
400 toinsert.append(img);
401 element.append(toinsert);
401 element.append(toinsert);
402 };
402 };
403
403
404
404
405 OutputArea.prototype.append_latex = function (latex, element) {
405 OutputArea.prototype.append_latex = function (latex, element) {
406 // This method cannot do the typesetting because the latex first has to
406 // This method cannot do the typesetting because the latex first has to
407 // be on the page.
407 // be on the page.
408 var toinsert = $("<div/>").addClass("box-flex1 output_subarea output_latex");
408 var toinsert = $("<div/>").addClass("box-flex1 output_subarea output_latex");
409 toinsert.append(latex);
409 toinsert.append(latex);
410 element.append(toinsert);
410 element.append(toinsert);
411 };
411 };
412
412
413
413
414 OutputArea.prototype.handle_clear_output = function (content) {
414 OutputArea.prototype.handle_clear_output = function (content) {
415 this.clear_output(content.stdout, content.stderr, content.other);
415 this.clear_output(content.stdout, content.stderr, content.other);
416 }
416 }
417
417
418
418
419 OutputArea.prototype.clear_output = function (stdout, stderr, other) {
419 OutputArea.prototype.clear_output = function (stdout, stderr, other) {
420 var that = this;
420 var that = this;
421 if (this.clear_out_timeout != null){
421 if (this.clear_out_timeout != null){
422 // fire previous pending clear *immediately*
422 // fire previous pending clear *immediately*
423 clearTimeout(this.clear_out_timeout);
423 clearTimeout(this.clear_out_timeout);
424 this.clear_out_timeout = null;
424 this.clear_out_timeout = null;
425 this.clear_output_callback(this._clear_stdout, this._clear_stderr, this._clear_other);
425 this.clear_output_callback(this._clear_stdout, this._clear_stderr, this._clear_other);
426 }
426 }
427 // store flags for flushing the timeout
427 // store flags for flushing the timeout
428 this._clear_stdout = stdout;
428 this._clear_stdout = stdout;
429 this._clear_stderr = stderr;
429 this._clear_stderr = stderr;
430 this._clear_other = other;
430 this._clear_other = other;
431 this.clear_out_timeout = setTimeout(function() {
431 this.clear_out_timeout = setTimeout(function() {
432 // really clear timeout only after a short delay
432 // really clear timeout only after a short delay
433 // this reduces flicker in 'clear_output; print' cases
433 // this reduces flicker in 'clear_output; print' cases
434 that.clear_out_timeout = null;
434 that.clear_out_timeout = null;
435 that._clear_stdout = that._clear_stderr = that._clear_other = null;
435 that._clear_stdout = that._clear_stderr = that._clear_other = null;
436 that.clear_output_callback(stdout, stderr, other);
436 that.clear_output_callback(stdout, stderr, other);
437 }, 500
437 }, 500
438 );
438 );
439 };
439 };
440
440
441
441
442 OutputArea.prototype.clear_output_callback = function (stdout, stderr, other) {
442 OutputArea.prototype.clear_output_callback = function (stdout, stderr, other) {
443 var output_div = this.element;
443 var output_div = this.element;
444
444
445 if (stdout && stderr && other){
445 if (stdout && stderr && other){
446 // clear all, no need for logic
446 // clear all, no need for logic
447 output_div.html("");
447 output_div.html("");
448 this.outputs = [];
448 this.outputs = [];
449 this.unscroll_area();
449 this.unscroll_area();
450 return;
450 return;
451 }
451 }
452 // remove html output
452 // remove html output
453 // each output_subarea that has an identifying class is in an output_area
453 // each output_subarea that has an identifying class is in an output_area
454 // which is the element to be removed.
454 // which is the element to be removed.
455 if (stdout) {
455 if (stdout) {
456 output_div.find("div.output_stdout").parent().remove();
456 output_div.find("div.output_stdout").parent().remove();
457 }
457 }
458 if (stderr) {
458 if (stderr) {
459 output_div.find("div.output_stderr").parent().remove();
459 output_div.find("div.output_stderr").parent().remove();
460 }
460 }
461 if (other) {
461 if (other) {
462 output_div.find("div.output_subarea").not("div.output_stderr").not("div.output_stdout").parent().remove();
462 output_div.find("div.output_subarea").not("div.output_stderr").not("div.output_stdout").parent().remove();
463 }
463 }
464 this.unscroll_area();
464 this.unscroll_area();
465
465
466 // remove cleared outputs from JSON list:
466 // remove cleared outputs from JSON list:
467 for (var i = this.outputs.length - 1; i >= 0; i--) {
467 for (var i = this.outputs.length - 1; i >= 0; i--) {
468 var out = this.outputs[i];
468 var out = this.outputs[i];
469 var output_type = out.output_type;
469 var output_type = out.output_type;
470 if (output_type == "display_data" && other) {
470 if (output_type == "display_data" && other) {
471 this.outputs.splice(i,1);
471 this.outputs.splice(i,1);
472 } else if (output_type == "stream") {
472 } else if (output_type == "stream") {
473 if (stdout && out.stream == "stdout") {
473 if (stdout && out.stream == "stdout") {
474 this.outputs.splice(i,1);
474 this.outputs.splice(i,1);
475 } else if (stderr && out.stream == "stderr") {
475 } else if (stderr && out.stream == "stderr") {
476 this.outputs.splice(i,1);
476 this.outputs.splice(i,1);
477 }
477 }
478 }
478 }
479 }
479 }
480 };
480 };
481
481
482
482
483 OutputArea.prototype.flush_clear_timeout = function() {
483 OutputArea.prototype.flush_clear_timeout = function() {
484 var output_div = this.element;
484 var output_div = this.element;
485 if (this.clear_out_timeout){
485 if (this.clear_out_timeout){
486 clearTimeout(this.clear_out_timeout);
486 clearTimeout(this.clear_out_timeout);
487 this.clear_out_timeout = null;
487 this.clear_out_timeout = null;
488 this.clear_output_callback(this._clear_stdout, this._clear_stderr, this._clear_other);
488 this.clear_output_callback(this._clear_stdout, this._clear_stderr, this._clear_other);
489 };
489 };
490 }
490 }
491
491
492
492
493 // JSON serialization
493 // JSON serialization
494
494
495 OutputArea.prototype.fromJSON = function (outputs) {
495 OutputArea.prototype.fromJSON = function (outputs) {
496 var len = outputs.length;
496 var len = outputs.length;
497 for (var i=0; i<len; i++) {
497 for (var i=0; i<len; i++) {
498 // append with dynamic=false.
498 // append with dynamic=false.
499 this.append_output(outputs[i], false);
499 this.append_output(outputs[i], false);
500 };
500 };
501 };
501 };
502
502
503
503
504 OutputArea.prototype.toJSON = function () {
504 OutputArea.prototype.toJSON = function () {
505 var outputs = [];
505 var outputs = [];
506 var len = this.outputs.length;
506 var len = this.outputs.length;
507 for (var i=0; i<len; i++) {
507 for (var i=0; i<len; i++) {
508 outputs[i] = this.outputs[i];
508 outputs[i] = this.outputs[i];
509 };
509 };
510 return outputs;
510 return outputs;
511 };
511 };
512
512
513
513
514 IPython.OutputArea = OutputArea;
514 IPython.OutputArea = OutputArea;
515
515
516 return IPython;
516 return IPython;
517
517
518 }(IPython));
518 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now