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