Show More
@@ -1,882 +1,901 | |||||
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.trusted = true; |
|
34 | this.trusted = true; | |
35 | this.clear_queued = null; |
|
35 | this.clear_queued = null; | |
36 | if (prompt_area === undefined) { |
|
36 | if (prompt_area === undefined) { | |
37 | this.prompt_area = true; |
|
37 | this.prompt_area = true; | |
38 | } else { |
|
38 | } else { | |
39 | this.prompt_area = prompt_area; |
|
39 | this.prompt_area = prompt_area; | |
40 | } |
|
40 | } | |
41 | this.create_elements(); |
|
41 | this.create_elements(); | |
42 | this.style(); |
|
42 | this.style(); | |
43 | this.bind_events(); |
|
43 | this.bind_events(); | |
44 | }; |
|
44 | }; | |
45 |
|
45 | |||
46 |
|
46 | |||
47 | /** |
|
47 | /** | |
48 | * Class prototypes |
|
48 | * Class prototypes | |
49 | **/ |
|
49 | **/ | |
50 |
|
50 | |||
51 | OutputArea.prototype.create_elements = function () { |
|
51 | OutputArea.prototype.create_elements = function () { | |
52 | this.element = $("<div/>"); |
|
52 | this.element = $("<div/>"); | |
53 | this.collapse_button = $("<div/>"); |
|
53 | this.collapse_button = $("<div/>"); | |
54 | this.prompt_overlay = $("<div/>"); |
|
54 | this.prompt_overlay = $("<div/>"); | |
55 | this.wrapper.append(this.prompt_overlay); |
|
55 | this.wrapper.append(this.prompt_overlay); | |
56 | this.wrapper.append(this.element); |
|
56 | this.wrapper.append(this.element); | |
57 | this.wrapper.append(this.collapse_button); |
|
57 | this.wrapper.append(this.collapse_button); | |
58 | }; |
|
58 | }; | |
59 |
|
59 | |||
60 |
|
60 | |||
61 | OutputArea.prototype.style = function () { |
|
61 | OutputArea.prototype.style = function () { | |
62 | this.collapse_button.hide(); |
|
62 | this.collapse_button.hide(); | |
63 | this.prompt_overlay.hide(); |
|
63 | this.prompt_overlay.hide(); | |
64 |
|
64 | |||
65 | this.wrapper.addClass('output_wrapper'); |
|
65 | this.wrapper.addClass('output_wrapper'); | |
66 | this.element.addClass('output'); |
|
66 | this.element.addClass('output'); | |
67 |
|
67 | |||
68 | this.collapse_button.addClass("btn output_collapsed"); |
|
68 | this.collapse_button.addClass("btn output_collapsed"); | |
69 | this.collapse_button.attr('title', 'click to expand output'); |
|
69 | this.collapse_button.attr('title', 'click to expand output'); | |
70 | this.collapse_button.text('. . .'); |
|
70 | this.collapse_button.text('. . .'); | |
71 |
|
71 | |||
72 | this.prompt_overlay.addClass('out_prompt_overlay prompt'); |
|
72 | this.prompt_overlay.addClass('out_prompt_overlay prompt'); | |
73 | this.prompt_overlay.attr('title', 'click to expand output; double click to hide output'); |
|
73 | this.prompt_overlay.attr('title', 'click to expand output; double click to hide output'); | |
74 |
|
74 | |||
75 | this.collapse(); |
|
75 | this.collapse(); | |
76 | }; |
|
76 | }; | |
77 |
|
77 | |||
78 | /** |
|
78 | /** | |
79 | * Should the OutputArea scroll? |
|
79 | * Should the OutputArea scroll? | |
80 | * Returns whether the height (in lines) exceeds a threshold. |
|
80 | * Returns whether the height (in lines) exceeds a threshold. | |
81 | * |
|
81 | * | |
82 | * @private |
|
82 | * @private | |
83 | * @method _should_scroll |
|
83 | * @method _should_scroll | |
84 | * @param [lines=100]{Integer} |
|
84 | * @param [lines=100]{Integer} | |
85 | * @return {Bool} |
|
85 | * @return {Bool} | |
86 | * |
|
86 | * | |
87 | */ |
|
87 | */ | |
88 | OutputArea.prototype._should_scroll = function (lines) { |
|
88 | OutputArea.prototype._should_scroll = function (lines) { | |
89 | if (lines <=0 ){ return } |
|
89 | if (lines <=0 ){ return } | |
90 | if (!lines) { |
|
90 | if (!lines) { | |
91 | lines = 100; |
|
91 | lines = 100; | |
92 | } |
|
92 | } | |
93 | // line-height from http://stackoverflow.com/questions/1185151 |
|
93 | // line-height from http://stackoverflow.com/questions/1185151 | |
94 | var fontSize = this.element.css('font-size'); |
|
94 | var fontSize = this.element.css('font-size'); | |
95 | var lineHeight = Math.floor(parseInt(fontSize.replace('px','')) * 1.5); |
|
95 | var lineHeight = Math.floor(parseInt(fontSize.replace('px','')) * 1.5); | |
96 |
|
96 | |||
97 | return (this.element.height() > lines * lineHeight); |
|
97 | return (this.element.height() > lines * lineHeight); | |
98 | }; |
|
98 | }; | |
99 |
|
99 | |||
100 |
|
100 | |||
101 | OutputArea.prototype.bind_events = function () { |
|
101 | OutputArea.prototype.bind_events = function () { | |
102 | var that = this; |
|
102 | var that = this; | |
103 | this.prompt_overlay.dblclick(function () { that.toggle_output(); }); |
|
103 | this.prompt_overlay.dblclick(function () { that.toggle_output(); }); | |
104 | this.prompt_overlay.click(function () { that.toggle_scroll(); }); |
|
104 | this.prompt_overlay.click(function () { that.toggle_scroll(); }); | |
105 |
|
105 | |||
106 | this.element.resize(function () { |
|
106 | this.element.resize(function () { | |
107 | // FIXME: Firefox on Linux misbehaves, so automatic scrolling is disabled |
|
107 | // FIXME: Firefox on Linux misbehaves, so automatic scrolling is disabled | |
108 | if ( IPython.utils.browser[0] === "Firefox" ) { |
|
108 | if ( IPython.utils.browser[0] === "Firefox" ) { | |
109 | return; |
|
109 | return; | |
110 | } |
|
110 | } | |
111 | // maybe scroll output, |
|
111 | // maybe scroll output, | |
112 | // if it's grown large enough and hasn't already been scrolled. |
|
112 | // if it's grown large enough and hasn't already been scrolled. | |
113 | if ( !that.scrolled && that._should_scroll(OutputArea.auto_scroll_threshold)) { |
|
113 | if ( !that.scrolled && that._should_scroll(OutputArea.auto_scroll_threshold)) { | |
114 | that.scroll_area(); |
|
114 | that.scroll_area(); | |
115 | } |
|
115 | } | |
116 | }); |
|
116 | }); | |
117 | this.collapse_button.click(function () { |
|
117 | this.collapse_button.click(function () { | |
118 | that.expand(); |
|
118 | that.expand(); | |
119 | }); |
|
119 | }); | |
120 | }; |
|
120 | }; | |
121 |
|
121 | |||
122 |
|
122 | |||
123 | OutputArea.prototype.collapse = function () { |
|
123 | OutputArea.prototype.collapse = function () { | |
124 | if (!this.collapsed) { |
|
124 | if (!this.collapsed) { | |
125 | this.element.hide(); |
|
125 | this.element.hide(); | |
126 | this.prompt_overlay.hide(); |
|
126 | this.prompt_overlay.hide(); | |
127 | if (this.element.html()){ |
|
127 | if (this.element.html()){ | |
128 | this.collapse_button.show(); |
|
128 | this.collapse_button.show(); | |
129 | } |
|
129 | } | |
130 | this.collapsed = true; |
|
130 | this.collapsed = true; | |
131 | } |
|
131 | } | |
132 | }; |
|
132 | }; | |
133 |
|
133 | |||
134 |
|
134 | |||
135 | OutputArea.prototype.expand = function () { |
|
135 | OutputArea.prototype.expand = function () { | |
136 | if (this.collapsed) { |
|
136 | if (this.collapsed) { | |
137 | this.collapse_button.hide(); |
|
137 | this.collapse_button.hide(); | |
138 | this.element.show(); |
|
138 | this.element.show(); | |
139 | this.prompt_overlay.show(); |
|
139 | this.prompt_overlay.show(); | |
140 | this.collapsed = false; |
|
140 | this.collapsed = false; | |
141 | } |
|
141 | } | |
142 | }; |
|
142 | }; | |
143 |
|
143 | |||
144 |
|
144 | |||
145 | OutputArea.prototype.toggle_output = function () { |
|
145 | OutputArea.prototype.toggle_output = function () { | |
146 | if (this.collapsed) { |
|
146 | if (this.collapsed) { | |
147 | this.expand(); |
|
147 | this.expand(); | |
148 | } else { |
|
148 | } else { | |
149 | this.collapse(); |
|
149 | this.collapse(); | |
150 | } |
|
150 | } | |
151 | }; |
|
151 | }; | |
152 |
|
152 | |||
153 |
|
153 | |||
154 | OutputArea.prototype.scroll_area = function () { |
|
154 | OutputArea.prototype.scroll_area = function () { | |
155 | this.element.addClass('output_scroll'); |
|
155 | this.element.addClass('output_scroll'); | |
156 | this.prompt_overlay.attr('title', 'click to unscroll output; double click to hide'); |
|
156 | this.prompt_overlay.attr('title', 'click to unscroll output; double click to hide'); | |
157 | this.scrolled = true; |
|
157 | this.scrolled = true; | |
158 | }; |
|
158 | }; | |
159 |
|
159 | |||
160 |
|
160 | |||
161 | OutputArea.prototype.unscroll_area = function () { |
|
161 | OutputArea.prototype.unscroll_area = function () { | |
162 | this.element.removeClass('output_scroll'); |
|
162 | this.element.removeClass('output_scroll'); | |
163 | this.prompt_overlay.attr('title', 'click to scroll output; double click to hide'); |
|
163 | this.prompt_overlay.attr('title', 'click to scroll output; double click to hide'); | |
164 | this.scrolled = false; |
|
164 | this.scrolled = false; | |
165 | }; |
|
165 | }; | |
166 |
|
166 | |||
167 | /** |
|
167 | /** | |
168 | * |
|
168 | * | |
169 | * Scroll OutputArea if height supperior than a threshold (in lines). |
|
169 | * Scroll OutputArea if height supperior than a threshold (in lines). | |
170 | * |
|
170 | * | |
171 | * Threshold is a maximum number of lines. If unspecified, defaults to |
|
171 | * Threshold is a maximum number of lines. If unspecified, defaults to | |
172 | * OutputArea.minimum_scroll_threshold. |
|
172 | * OutputArea.minimum_scroll_threshold. | |
173 | * |
|
173 | * | |
174 | * Negative threshold will prevent the OutputArea from ever scrolling. |
|
174 | * Negative threshold will prevent the OutputArea from ever scrolling. | |
175 | * |
|
175 | * | |
176 | * @method scroll_if_long |
|
176 | * @method scroll_if_long | |
177 | * |
|
177 | * | |
178 | * @param [lines=20]{Number} Default to 20 if not set, |
|
178 | * @param [lines=20]{Number} Default to 20 if not set, | |
179 | * behavior undefined for value of `0`. |
|
179 | * behavior undefined for value of `0`. | |
180 | * |
|
180 | * | |
181 | **/ |
|
181 | **/ | |
182 | OutputArea.prototype.scroll_if_long = function (lines) { |
|
182 | OutputArea.prototype.scroll_if_long = function (lines) { | |
183 | var n = lines | OutputArea.minimum_scroll_threshold; |
|
183 | var n = lines | OutputArea.minimum_scroll_threshold; | |
184 | if(n <= 0){ |
|
184 | if(n <= 0){ | |
185 | return |
|
185 | return | |
186 | } |
|
186 | } | |
187 |
|
187 | |||
188 | if (this._should_scroll(n)) { |
|
188 | if (this._should_scroll(n)) { | |
189 | // only allow scrolling long-enough output |
|
189 | // only allow scrolling long-enough output | |
190 | this.scroll_area(); |
|
190 | this.scroll_area(); | |
191 | } |
|
191 | } | |
192 | }; |
|
192 | }; | |
193 |
|
193 | |||
194 |
|
194 | |||
195 | OutputArea.prototype.toggle_scroll = function () { |
|
195 | OutputArea.prototype.toggle_scroll = function () { | |
196 | if (this.scrolled) { |
|
196 | if (this.scrolled) { | |
197 | this.unscroll_area(); |
|
197 | this.unscroll_area(); | |
198 | } else { |
|
198 | } else { | |
199 | // only allow scrolling long-enough output |
|
199 | // only allow scrolling long-enough output | |
200 | this.scroll_if_long(); |
|
200 | this.scroll_if_long(); | |
201 | } |
|
201 | } | |
202 | }; |
|
202 | }; | |
203 |
|
203 | |||
204 |
|
204 | |||
205 | // typeset with MathJax if MathJax is available |
|
205 | // typeset with MathJax if MathJax is available | |
206 | OutputArea.prototype.typeset = function () { |
|
206 | OutputArea.prototype.typeset = function () { | |
207 | if (window.MathJax){ |
|
207 | if (window.MathJax){ | |
208 | MathJax.Hub.Queue(["Typeset",MathJax.Hub]); |
|
208 | MathJax.Hub.Queue(["Typeset",MathJax.Hub]); | |
209 | } |
|
209 | } | |
210 | }; |
|
210 | }; | |
211 |
|
211 | |||
212 |
|
212 | |||
213 | OutputArea.prototype.handle_output = function (msg) { |
|
213 | OutputArea.prototype.handle_output = function (msg) { | |
214 | var json = {}; |
|
214 | var json = {}; | |
215 | var msg_type = json.output_type = msg.header.msg_type; |
|
215 | var msg_type = json.output_type = msg.header.msg_type; | |
216 | var content = msg.content; |
|
216 | var content = msg.content; | |
217 | if (msg_type === "stream") { |
|
217 | if (msg_type === "stream") { | |
218 | json.text = content.data; |
|
218 | json.text = content.data; | |
219 | json.stream = content.name; |
|
219 | json.stream = content.name; | |
220 | } else if (msg_type === "display_data") { |
|
220 | } else if (msg_type === "display_data") { | |
221 | json = content.data; |
|
221 | json = content.data; | |
222 | json.output_type = msg_type; |
|
222 | json.output_type = msg_type; | |
223 | json.metadata = content.metadata; |
|
223 | json.metadata = content.metadata; | |
224 | } else if (msg_type === "pyout") { |
|
224 | } else if (msg_type === "pyout") { | |
225 | json = content.data; |
|
225 | json = content.data; | |
226 | json.output_type = msg_type; |
|
226 | json.output_type = msg_type; | |
227 | json.metadata = content.metadata; |
|
227 | json.metadata = content.metadata; | |
228 | json.prompt_number = content.execution_count; |
|
228 | json.prompt_number = content.execution_count; | |
229 | } else if (msg_type === "pyerr") { |
|
229 | } else if (msg_type === "pyerr") { | |
230 | json.ename = content.ename; |
|
230 | json.ename = content.ename; | |
231 | json.evalue = content.evalue; |
|
231 | json.evalue = content.evalue; | |
232 | json.traceback = content.traceback; |
|
232 | json.traceback = content.traceback; | |
233 | } |
|
233 | } | |
234 | this.append_output(json); |
|
234 | this.append_output(json); | |
235 | }; |
|
235 | }; | |
236 |
|
236 | |||
237 |
|
237 | |||
238 | OutputArea.prototype.rename_keys = function (data, key_map) { |
|
238 | OutputArea.prototype.rename_keys = function (data, key_map) { | |
239 | var remapped = {}; |
|
239 | var remapped = {}; | |
240 | for (var key in data) { |
|
240 | for (var key in data) { | |
241 | var new_key = key_map[key] || key; |
|
241 | var new_key = key_map[key] || key; | |
242 | remapped[new_key] = data[key]; |
|
242 | remapped[new_key] = data[key]; | |
243 | } |
|
243 | } | |
244 | return remapped; |
|
244 | return remapped; | |
245 | }; |
|
245 | }; | |
246 |
|
246 | |||
247 |
|
247 | |||
248 | OutputArea.output_types = [ |
|
248 | OutputArea.output_types = [ | |
249 | 'application/javascript', |
|
249 | 'application/javascript', | |
250 | 'text/html', |
|
250 | 'text/html', | |
251 | 'text/latex', |
|
251 | 'text/latex', | |
252 | 'image/svg+xml', |
|
252 | 'image/svg+xml', | |
253 | 'image/png', |
|
253 | 'image/png', | |
254 | 'image/jpeg', |
|
254 | 'image/jpeg', | |
255 | 'application/pdf', |
|
255 | 'application/pdf', | |
256 | 'text/plain' |
|
256 | 'text/plain' | |
257 | ]; |
|
257 | ]; | |
258 |
|
258 | |||
259 | OutputArea.prototype.validate_output = function (json) { |
|
259 | OutputArea.prototype.validate_output = function (json) { | |
260 | // scrub invalid outputs |
|
260 | // scrub invalid outputs | |
261 | // TODO: right now everything is a string, but JSON really shouldn't be. |
|
261 | // TODO: right now everything is a string, but JSON really shouldn't be. | |
262 | // nbformat 4 will fix that. |
|
262 | // nbformat 4 will fix that. | |
263 | $.map(OutputArea.output_types, function(key){ |
|
263 | $.map(OutputArea.output_types, function(key){ | |
264 | if (json[key] !== undefined && typeof json[key] !== 'string') { |
|
264 | if (json[key] !== undefined && typeof json[key] !== 'string') { | |
265 | console.log("Invalid type for " + key, json[key]); |
|
265 | console.log("Invalid type for " + key, json[key]); | |
266 | delete json[key]; |
|
266 | delete json[key]; | |
267 | } |
|
267 | } | |
268 | }); |
|
268 | }); | |
269 | return json; |
|
269 | return json; | |
270 | }; |
|
270 | }; | |
271 |
|
271 | |||
272 | OutputArea.prototype.append_output = function (json) { |
|
272 | OutputArea.prototype.append_output = function (json) { | |
273 | this.expand(); |
|
273 | this.expand(); | |
274 | // Clear the output if clear is queued. |
|
274 | // Clear the output if clear is queued. | |
275 | var needs_height_reset = false; |
|
275 | var needs_height_reset = false; | |
276 | if (this.clear_queued) { |
|
276 | if (this.clear_queued) { | |
277 | this.clear_output(false); |
|
277 | this.clear_output(false); | |
278 | needs_height_reset = true; |
|
278 | needs_height_reset = true; | |
279 | } |
|
279 | } | |
280 |
|
280 | |||
|
281 | // We must release the animation fixed height in a callback since Gecko | |||
|
282 | // (FireFox) doesn't render the image immediately as the data is | |||
|
283 | // available. | |||
|
284 | var that = this; | |||
|
285 | var handle_appended = function () { | |||
|
286 | // Only reset the height to automatic if the height is currently | |||
|
287 | // fixed (done by wait=True flag on clear_output). | |||
|
288 | console.log('appended'); | |||
|
289 | if (needs_height_reset) { | |||
|
290 | that.element.height(''); | |||
|
291 | } | |||
|
292 | that.element.trigger('resize'); | |||
|
293 | }; | |||
|
294 | ||||
281 | // validate output data types |
|
295 | // validate output data types | |
282 | json = this.validate_output(json); |
|
296 | json = this.validate_output(json); | |
283 |
|
297 | |||
284 | if (json.output_type === 'pyout') { |
|
298 | if (json.output_type === 'pyout') { | |
285 | this.append_pyout(json); |
|
299 | this.append_pyout(json); | |
286 | } else if (json.output_type === 'pyerr') { |
|
300 | } else if (json.output_type === 'pyerr') { | |
287 | this.append_pyerr(json); |
|
301 | this.append_pyerr(json); | |
288 | } else if (json.output_type === 'display_data') { |
|
|||
289 | this.append_display_data(json); |
|
|||
290 | } else if (json.output_type === 'stream') { |
|
302 | } else if (json.output_type === 'stream') { | |
291 | this.append_stream(json); |
|
303 | this.append_stream(json); | |
292 | } |
|
304 | } | |
293 |
|
305 | |||
|
306 | if (json.output_type === 'display_data') { | |||
|
307 | this.append_display_data(json, handle_appended); | |||
|
308 | } else { | |||
|
309 | handle_appended(); | |||
|
310 | } | |||
|
311 | ||||
294 | this.outputs.push(json); |
|
312 | this.outputs.push(json); | |
295 |
|
313 | |||
296 | // We must release the animation fixed height in a timeout since Gecko |
|
|||
297 | // (FireFox) doesn't render the image immediately as the data is |
|
|||
298 | // available. |
|
|||
299 | var that = this; |
|
|||
300 | setTimeout(function(){ |
|
|||
301 | // Only reset the height to automatic if the height is currently |
|
|||
302 | // fixed (done by wait=True flag on clear_output). |
|
|||
303 | if (needs_height_reset) { |
|
|||
304 | that.element.height(''); |
|
|||
305 | } |
|
|||
306 | that.element.trigger('resize'); |
|
|||
307 | }, 250); |
|
|||
308 | }; |
|
314 | }; | |
309 |
|
315 | |||
310 |
|
316 | |||
311 | OutputArea.prototype.create_output_area = function () { |
|
317 | OutputArea.prototype.create_output_area = function () { | |
312 | var oa = $("<div/>").addClass("output_area"); |
|
318 | var oa = $("<div/>").addClass("output_area"); | |
313 | if (this.prompt_area) { |
|
319 | if (this.prompt_area) { | |
314 | oa.append($('<div/>').addClass('prompt')); |
|
320 | oa.append($('<div/>').addClass('prompt')); | |
315 | } |
|
321 | } | |
316 | return oa; |
|
322 | return oa; | |
317 | }; |
|
323 | }; | |
318 |
|
324 | |||
319 |
|
325 | |||
320 | function _get_metadata_key(metadata, key, mime) { |
|
326 | function _get_metadata_key(metadata, key, mime) { | |
321 | var mime_md = metadata[mime]; |
|
327 | var mime_md = metadata[mime]; | |
322 | // mime-specific higher priority |
|
328 | // mime-specific higher priority | |
323 | if (mime_md && mime_md[key] !== undefined) { |
|
329 | if (mime_md && mime_md[key] !== undefined) { | |
324 | return mime_md[key]; |
|
330 | return mime_md[key]; | |
325 | } |
|
331 | } | |
326 | // fallback on global |
|
332 | // fallback on global | |
327 | return metadata[key]; |
|
333 | return metadata[key]; | |
328 | } |
|
334 | } | |
329 |
|
335 | |||
330 | OutputArea.prototype.create_output_subarea = function(md, classes, mime) { |
|
336 | OutputArea.prototype.create_output_subarea = function(md, classes, mime) { | |
331 | var subarea = $('<div/>').addClass('output_subarea').addClass(classes); |
|
337 | var subarea = $('<div/>').addClass('output_subarea').addClass(classes); | |
332 | if (_get_metadata_key(md, 'isolated', mime)) { |
|
338 | if (_get_metadata_key(md, 'isolated', mime)) { | |
333 | // Create an iframe to isolate the subarea from the rest of the |
|
339 | // Create an iframe to isolate the subarea from the rest of the | |
334 | // document |
|
340 | // document | |
335 | var iframe = $('<iframe/>').addClass('box-flex1'); |
|
341 | var iframe = $('<iframe/>').addClass('box-flex1'); | |
336 | iframe.css({'height':1, 'width':'100%', 'display':'block'}); |
|
342 | iframe.css({'height':1, 'width':'100%', 'display':'block'}); | |
337 | iframe.attr('frameborder', 0); |
|
343 | iframe.attr('frameborder', 0); | |
338 | iframe.attr('scrolling', 'auto'); |
|
344 | iframe.attr('scrolling', 'auto'); | |
339 |
|
345 | |||
340 | // Once the iframe is loaded, the subarea is dynamically inserted |
|
346 | // Once the iframe is loaded, the subarea is dynamically inserted | |
341 | iframe.on('load', function() { |
|
347 | iframe.on('load', function() { | |
342 | // Workaround needed by Firefox, to properly render svg inside |
|
348 | // Workaround needed by Firefox, to properly render svg inside | |
343 | // iframes, see http://stackoverflow.com/questions/10177190/ |
|
349 | // iframes, see http://stackoverflow.com/questions/10177190/ | |
344 | // svg-dynamically-added-to-iframe-does-not-render-correctly |
|
350 | // svg-dynamically-added-to-iframe-does-not-render-correctly | |
345 | this.contentDocument.open(); |
|
351 | this.contentDocument.open(); | |
346 |
|
352 | |||
347 | // Insert the subarea into the iframe |
|
353 | // Insert the subarea into the iframe | |
348 | // We must directly write the html. When using Jquery's append |
|
354 | // We must directly write the html. When using Jquery's append | |
349 | // method, javascript is evaluated in the parent document and |
|
355 | // method, javascript is evaluated in the parent document and | |
350 | // not in the iframe document. At this point, subarea doesn't |
|
356 | // not in the iframe document. At this point, subarea doesn't | |
351 | // contain any user content. |
|
357 | // contain any user content. | |
352 | this.contentDocument.write(subarea.html()); |
|
358 | this.contentDocument.write(subarea.html()); | |
353 |
|
359 | |||
354 | this.contentDocument.close(); |
|
360 | this.contentDocument.close(); | |
355 |
|
361 | |||
356 | var body = this.contentDocument.body; |
|
362 | var body = this.contentDocument.body; | |
357 | // Adjust the iframe height automatically |
|
363 | // Adjust the iframe height automatically | |
358 | iframe.height(body.scrollHeight + 'px'); |
|
364 | iframe.height(body.scrollHeight + 'px'); | |
359 | }); |
|
365 | }); | |
360 |
|
366 | |||
361 | // Elements should be appended to the inner subarea and not to the |
|
367 | // Elements should be appended to the inner subarea and not to the | |
362 | // iframe |
|
368 | // iframe | |
363 | iframe.append = function(that) { |
|
369 | iframe.append = function(that) { | |
364 | subarea.append(that); |
|
370 | subarea.append(that); | |
365 | }; |
|
371 | }; | |
366 |
|
372 | |||
367 | return iframe; |
|
373 | return iframe; | |
368 | } else { |
|
374 | } else { | |
369 | return subarea; |
|
375 | return subarea; | |
370 | } |
|
376 | } | |
371 | } |
|
377 | } | |
372 |
|
378 | |||
373 |
|
379 | |||
374 | OutputArea.prototype._append_javascript_error = function (err, element) { |
|
380 | OutputArea.prototype._append_javascript_error = function (err, element) { | |
375 | // display a message when a javascript error occurs in display output |
|
381 | // display a message when a javascript error occurs in display output | |
376 | var msg = "Javascript error adding output!" |
|
382 | var msg = "Javascript error adding output!" | |
377 | if ( element === undefined ) return; |
|
383 | if ( element === undefined ) return; | |
378 | element |
|
384 | element | |
379 | .append($('<div/>').text(msg).addClass('js-error')) |
|
385 | .append($('<div/>').text(msg).addClass('js-error')) | |
380 | .append($('<div/>').text(err.toString()).addClass('js-error')) |
|
386 | .append($('<div/>').text(err.toString()).addClass('js-error')) | |
381 | .append($('<div/>').text('See your browser Javascript console for more details.').addClass('js-error')); |
|
387 | .append($('<div/>').text('See your browser Javascript console for more details.').addClass('js-error')); | |
382 | }; |
|
388 | }; | |
383 |
|
389 | |||
384 | OutputArea.prototype._safe_append = function (toinsert) { |
|
390 | OutputArea.prototype._safe_append = function (toinsert) { | |
385 | // safely append an item to the document |
|
391 | // safely append an item to the document | |
386 | // this is an object created by user code, |
|
392 | // this is an object created by user code, | |
387 | // and may have errors, which should not be raised |
|
393 | // and may have errors, which should not be raised | |
388 | // under any circumstances. |
|
394 | // under any circumstances. | |
389 | try { |
|
395 | try { | |
390 | this.element.append(toinsert); |
|
396 | this.element.append(toinsert); | |
391 | } catch(err) { |
|
397 | } catch(err) { | |
392 | console.log(err); |
|
398 | console.log(err); | |
393 | // Create an actual output_area and output_subarea, which creates |
|
399 | // Create an actual output_area and output_subarea, which creates | |
394 | // the prompt area and the proper indentation. |
|
400 | // the prompt area and the proper indentation. | |
395 | var toinsert = this.create_output_area(); |
|
401 | var toinsert = this.create_output_area(); | |
396 | var subarea = $('<div/>').addClass('output_subarea'); |
|
402 | var subarea = $('<div/>').addClass('output_subarea'); | |
397 | toinsert.append(subarea); |
|
403 | toinsert.append(subarea); | |
398 | this._append_javascript_error(err, subarea); |
|
404 | this._append_javascript_error(err, subarea); | |
399 | this.element.append(toinsert); |
|
405 | this.element.append(toinsert); | |
400 | } |
|
406 | } | |
401 | }; |
|
407 | }; | |
402 |
|
408 | |||
403 |
|
409 | |||
404 | OutputArea.prototype.append_pyout = function (json) { |
|
410 | OutputArea.prototype.append_pyout = function (json) { | |
405 | var n = json.prompt_number || ' '; |
|
411 | var n = json.prompt_number || ' '; | |
406 | var toinsert = this.create_output_area(); |
|
412 | var toinsert = this.create_output_area(); | |
407 | if (this.prompt_area) { |
|
413 | if (this.prompt_area) { | |
408 | toinsert.find('div.prompt').addClass('output_prompt').text('Out[' + n + ']:'); |
|
414 | toinsert.find('div.prompt').addClass('output_prompt').text('Out[' + n + ']:'); | |
409 | } |
|
415 | } | |
410 | var inserted = this.append_mime_type(json, toinsert); |
|
416 | var inserted = this.append_mime_type(json, toinsert); | |
411 | if (inserted) { |
|
417 | if (inserted) { | |
412 | inserted.addClass('output_pyout'); |
|
418 | inserted.addClass('output_pyout'); | |
413 | } |
|
419 | } | |
414 | this._safe_append(toinsert); |
|
420 | this._safe_append(toinsert); | |
415 | // If we just output latex, typeset it. |
|
421 | // If we just output latex, typeset it. | |
416 | if ((json['text/latex'] !== undefined) || (json['text/html'] !== undefined)) { |
|
422 | if ((json['text/latex'] !== undefined) || (json['text/html'] !== undefined)) { | |
417 | this.typeset(); |
|
423 | this.typeset(); | |
418 | } |
|
424 | } | |
419 | }; |
|
425 | }; | |
420 |
|
426 | |||
421 |
|
427 | |||
422 | OutputArea.prototype.append_pyerr = function (json) { |
|
428 | OutputArea.prototype.append_pyerr = function (json) { | |
423 | var tb = json.traceback; |
|
429 | var tb = json.traceback; | |
424 | if (tb !== undefined && tb.length > 0) { |
|
430 | if (tb !== undefined && tb.length > 0) { | |
425 | var s = ''; |
|
431 | var s = ''; | |
426 | var len = tb.length; |
|
432 | var len = tb.length; | |
427 | for (var i=0; i<len; i++) { |
|
433 | for (var i=0; i<len; i++) { | |
428 | s = s + tb[i] + '\n'; |
|
434 | s = s + tb[i] + '\n'; | |
429 | } |
|
435 | } | |
430 | s = s + '\n'; |
|
436 | s = s + '\n'; | |
431 | var toinsert = this.create_output_area(); |
|
437 | var toinsert = this.create_output_area(); | |
432 | var append_text = OutputArea.append_map['text/plain']; |
|
438 | var append_text = OutputArea.append_map['text/plain']; | |
433 | if (append_text) { |
|
439 | if (append_text) { | |
434 | append_text.apply(this, [s, {}, toinsert]).addClass('output_pyerr'); |
|
440 | append_text.apply(this, [s, {}, toinsert]).addClass('output_pyerr'); | |
435 | } |
|
441 | } | |
436 | this._safe_append(toinsert); |
|
442 | this._safe_append(toinsert); | |
437 | } |
|
443 | } | |
438 | }; |
|
444 | }; | |
439 |
|
445 | |||
440 |
|
446 | |||
441 | OutputArea.prototype.append_stream = function (json) { |
|
447 | OutputArea.prototype.append_stream = function (json) { | |
442 | // temporary fix: if stream undefined (json file written prior to this patch), |
|
448 | // temporary fix: if stream undefined (json file written prior to this patch), | |
443 | // default to most likely stdout: |
|
449 | // default to most likely stdout: | |
444 | if (json.stream === undefined){ |
|
450 | if (json.stream === undefined){ | |
445 | json.stream = 'stdout'; |
|
451 | json.stream = 'stdout'; | |
446 | } |
|
452 | } | |
447 | var text = json.text; |
|
453 | var text = json.text; | |
448 | var subclass = "output_"+json.stream; |
|
454 | var subclass = "output_"+json.stream; | |
449 | if (this.outputs.length > 0){ |
|
455 | if (this.outputs.length > 0){ | |
450 | // have at least one output to consider |
|
456 | // have at least one output to consider | |
451 | var last = this.outputs[this.outputs.length-1]; |
|
457 | var last = this.outputs[this.outputs.length-1]; | |
452 | if (last.output_type == 'stream' && json.stream == last.stream){ |
|
458 | if (last.output_type == 'stream' && json.stream == last.stream){ | |
453 | // latest output was in the same stream, |
|
459 | // latest output was in the same stream, | |
454 | // so append directly into its pre tag |
|
460 | // so append directly into its pre tag | |
455 | // escape ANSI & HTML specials: |
|
461 | // escape ANSI & HTML specials: | |
456 | var pre = this.element.find('div.'+subclass).last().find('pre'); |
|
462 | var pre = this.element.find('div.'+subclass).last().find('pre'); | |
457 | var html = utils.fixCarriageReturn( |
|
463 | var html = utils.fixCarriageReturn( | |
458 | pre.html() + utils.fixConsole(text)); |
|
464 | pre.html() + utils.fixConsole(text)); | |
459 | // The only user content injected with this HTML call is |
|
465 | // The only user content injected with this HTML call is | |
460 | // escaped by the fixConsole() method. |
|
466 | // escaped by the fixConsole() method. | |
461 | pre.html(html); |
|
467 | pre.html(html); | |
462 | return; |
|
468 | return; | |
463 | } |
|
469 | } | |
464 | } |
|
470 | } | |
465 |
|
471 | |||
466 | if (!text.replace("\r", "")) { |
|
472 | if (!text.replace("\r", "")) { | |
467 | // text is nothing (empty string, \r, etc.) |
|
473 | // text is nothing (empty string, \r, etc.) | |
468 | // so don't append any elements, which might add undesirable space |
|
474 | // so don't append any elements, which might add undesirable space | |
469 | return; |
|
475 | return; | |
470 | } |
|
476 | } | |
471 |
|
477 | |||
472 | // If we got here, attach a new div |
|
478 | // If we got here, attach a new div | |
473 | var toinsert = this.create_output_area(); |
|
479 | var toinsert = this.create_output_area(); | |
474 | var append_text = OutputArea.append_map['text/plain']; |
|
480 | var append_text = OutputArea.append_map['text/plain']; | |
475 | if (append_text) { |
|
481 | if (append_text) { | |
476 | append_text.apply(this, [text, {}, toinsert]).addClass("output_stream " + subclass); |
|
482 | append_text.apply(this, [text, {}, toinsert]).addClass("output_stream " + subclass); | |
477 | } |
|
483 | } | |
478 | this._safe_append(toinsert); |
|
484 | this._safe_append(toinsert); | |
479 | }; |
|
485 | }; | |
480 |
|
486 | |||
481 |
|
487 | |||
482 | OutputArea.prototype.append_display_data = function (json) { |
|
488 | OutputArea.prototype.append_display_data = function (json, handle_inserted) { | |
483 | var toinsert = this.create_output_area(); |
|
489 | var toinsert = this.create_output_area(); | |
484 | if (this.append_mime_type(json, toinsert)) { |
|
490 | if (this.append_mime_type(json, toinsert, handle_inserted)) { | |
485 | this._safe_append(toinsert); |
|
491 | this._safe_append(toinsert); | |
486 | // If we just output latex, typeset it. |
|
492 | // If we just output latex, typeset it. | |
487 | if ((json['text/latex'] !== undefined) || (json['text/html'] !== undefined)) { |
|
493 | if ((json['text/latex'] !== undefined) || (json['text/html'] !== undefined)) { | |
488 | this.typeset(); |
|
494 | this.typeset(); | |
489 | } |
|
495 | } | |
490 | } |
|
496 | } | |
491 | }; |
|
497 | }; | |
492 |
|
498 | |||
493 |
|
499 | |||
494 | OutputArea.safe_outputs = { |
|
500 | OutputArea.safe_outputs = { | |
495 | 'text/plain' : true, |
|
501 | 'text/plain' : true, | |
496 | 'text/latex' : true, |
|
502 | 'text/latex' : true, | |
497 | 'image/png' : true, |
|
503 | 'image/png' : true, | |
498 | 'image/jpeg' : true |
|
504 | 'image/jpeg' : true | |
499 | }; |
|
505 | }; | |
500 |
|
506 | |||
501 | OutputArea.prototype.append_mime_type = function (json, element) { |
|
507 | OutputArea.prototype.append_mime_type = function (json, element, handle_inserted) { | |
502 | for (var type_i in OutputArea.display_order) { |
|
508 | for (var type_i in OutputArea.display_order) { | |
503 | var type = OutputArea.display_order[type_i]; |
|
509 | var type = OutputArea.display_order[type_i]; | |
504 | var append = OutputArea.append_map[type]; |
|
510 | var append = OutputArea.append_map[type]; | |
505 | if ((json[type] !== undefined) && append) { |
|
511 | if ((json[type] !== undefined) && append) { | |
506 | var value = json[type]; |
|
512 | var value = json[type]; | |
507 | if (!this.trusted && !OutputArea.safe_outputs[type]) { |
|
513 | if (!this.trusted && !OutputArea.safe_outputs[type]) { | |
508 | // not trusted, sanitize HTML |
|
514 | // not trusted, sanitize HTML | |
509 | if (type==='text/html' || type==='text/svg') { |
|
515 | if (type==='text/html' || type==='text/svg') { | |
510 | value = IPython.security.sanitize_html(value); |
|
516 | value = IPython.security.sanitize_html(value); | |
511 | } else { |
|
517 | } else { | |
512 | // don't display if we don't know how to sanitize it |
|
518 | // don't display if we don't know how to sanitize it | |
513 | console.log("Ignoring untrusted " + type + " output."); |
|
519 | console.log("Ignoring untrusted " + type + " output."); | |
514 | continue; |
|
520 | continue; | |
515 | } |
|
521 | } | |
516 | } |
|
522 | } | |
517 | var md = json.metadata || {}; |
|
523 | var md = json.metadata || {}; | |
518 | var toinsert = append.apply(this, [value, md, element]); |
|
524 | var toinsert = append.apply(this, [value, md, element, handle_inserted]); | |
|
525 | // Since only the png and jpeg mime types call the inserted | |||
|
526 | // callback, if the mim type is something other we must call the | |||
|
527 | // inserted callback only when the element is actually inserted | |||
|
528 | // into the DOM. Use a timeout of 0 to do this. | |||
|
529 | if (['image/png', 'image/jpeg'].indexOf() < 0 && handle_inserted !== undefined) { | |||
|
530 | setTimeout(handle_inserted, 0); | |||
|
531 | } | |||
519 | $([IPython.events]).trigger('output_appended.OutputArea', [type, value, md, toinsert]); |
|
532 | $([IPython.events]).trigger('output_appended.OutputArea', [type, value, md, toinsert]); | |
520 | return toinsert; |
|
533 | return toinsert; | |
521 | } |
|
534 | } | |
522 | } |
|
535 | } | |
523 | return null; |
|
536 | return null; | |
524 | }; |
|
537 | }; | |
525 |
|
538 | |||
526 |
|
539 | |||
527 | var append_html = function (html, md, element) { |
|
540 | var append_html = function (html, md, element) { | |
528 | var type = 'text/html'; |
|
541 | var type = 'text/html'; | |
529 | var toinsert = this.create_output_subarea(md, "output_html rendered_html", type); |
|
542 | var toinsert = this.create_output_subarea(md, "output_html rendered_html", type); | |
530 | IPython.keyboard_manager.register_events(toinsert); |
|
543 | IPython.keyboard_manager.register_events(toinsert); | |
531 | toinsert.append(html); |
|
544 | toinsert.append(html); | |
532 | element.append(toinsert); |
|
545 | element.append(toinsert); | |
533 | return toinsert; |
|
546 | return toinsert; | |
534 | }; |
|
547 | }; | |
535 |
|
548 | |||
536 |
|
549 | |||
537 | var append_javascript = function (js, md, element) { |
|
550 | var append_javascript = function (js, md, element) { | |
538 | // We just eval the JS code, element appears in the local scope. |
|
551 | // We just eval the JS code, element appears in the local scope. | |
539 | var type = 'application/javascript'; |
|
552 | var type = 'application/javascript'; | |
540 | var toinsert = this.create_output_subarea(md, "output_javascript", type); |
|
553 | var toinsert = this.create_output_subarea(md, "output_javascript", type); | |
541 | IPython.keyboard_manager.register_events(toinsert); |
|
554 | IPython.keyboard_manager.register_events(toinsert); | |
542 | element.append(toinsert); |
|
555 | element.append(toinsert); | |
543 | // FIXME TODO : remove `container element for 3.0` |
|
556 | // FIXME TODO : remove `container element for 3.0` | |
544 | //backward compat, js should be eval'ed in a context where `container` is defined. |
|
557 | //backward compat, js should be eval'ed in a context where `container` is defined. | |
545 | var container = element; |
|
558 | var container = element; | |
546 | container.show = function(){console.log('Warning "container.show()" is deprecated.')}; |
|
559 | container.show = function(){console.log('Warning "container.show()" is deprecated.')}; | |
547 | // end backward compat |
|
560 | // end backward compat | |
548 |
|
561 | |||
549 | // Fix for ipython/issues/5293, make sure `element` is the area which |
|
562 | // Fix for ipython/issues/5293, make sure `element` is the area which | |
550 | // output can be inserted into at the time of JS execution. |
|
563 | // output can be inserted into at the time of JS execution. | |
551 | element = toinsert; |
|
564 | element = toinsert; | |
552 | try { |
|
565 | try { | |
553 | eval(js); |
|
566 | eval(js); | |
554 | } catch(err) { |
|
567 | } catch(err) { | |
555 | console.log(err); |
|
568 | console.log(err); | |
556 | this._append_javascript_error(err, toinsert); |
|
569 | this._append_javascript_error(err, toinsert); | |
557 | } |
|
570 | } | |
558 | return toinsert; |
|
571 | return toinsert; | |
559 | }; |
|
572 | }; | |
560 |
|
573 | |||
561 |
|
574 | |||
562 | var append_text = function (data, md, element) { |
|
575 | var append_text = function (data, md, element) { | |
563 | var type = 'text/plain'; |
|
576 | var type = 'text/plain'; | |
564 | var toinsert = this.create_output_subarea(md, "output_text", type); |
|
577 | var toinsert = this.create_output_subarea(md, "output_text", type); | |
565 | // escape ANSI & HTML specials in plaintext: |
|
578 | // escape ANSI & HTML specials in plaintext: | |
566 | data = utils.fixConsole(data); |
|
579 | data = utils.fixConsole(data); | |
567 | data = utils.fixCarriageReturn(data); |
|
580 | data = utils.fixCarriageReturn(data); | |
568 | data = utils.autoLinkUrls(data); |
|
581 | data = utils.autoLinkUrls(data); | |
569 | // The only user content injected with this HTML call is |
|
582 | // The only user content injected with this HTML call is | |
570 | // escaped by the fixConsole() method. |
|
583 | // escaped by the fixConsole() method. | |
571 | toinsert.append($("<pre/>").html(data)); |
|
584 | toinsert.append($("<pre/>").html(data)); | |
572 | element.append(toinsert); |
|
585 | element.append(toinsert); | |
573 | return toinsert; |
|
586 | return toinsert; | |
574 | }; |
|
587 | }; | |
575 |
|
588 | |||
576 |
|
589 | |||
577 | var append_svg = function (svg, md, element) { |
|
590 | var append_svg = function (svg, md, element) { | |
578 | var type = 'image/svg+xml'; |
|
591 | var type = 'image/svg+xml'; | |
579 | var toinsert = this.create_output_subarea(md, "output_svg", type); |
|
592 | var toinsert = this.create_output_subarea(md, "output_svg", type); | |
580 | toinsert.append(svg); |
|
593 | toinsert.append(svg); | |
581 | element.append(toinsert); |
|
594 | element.append(toinsert); | |
582 | return toinsert; |
|
595 | return toinsert; | |
583 | }; |
|
596 | }; | |
584 |
|
597 | |||
585 |
|
598 | |||
586 | OutputArea.prototype._dblclick_to_reset_size = function (img) { |
|
599 | OutputArea.prototype._dblclick_to_reset_size = function (img) { | |
587 | // wrap image after it's loaded on the page, |
|
600 | // wrap image after it's loaded on the page, | |
588 | // otherwise the measured initial size will be incorrect |
|
601 | // otherwise the measured initial size will be incorrect | |
589 | img.on("load", function (){ |
|
602 | img.on("load", function (){ | |
590 | var h0 = img.height(); |
|
603 | var h0 = img.height(); | |
591 | var w0 = img.width(); |
|
604 | var w0 = img.width(); | |
592 | if (!(h0 && w0)) { |
|
605 | if (!(h0 && w0)) { | |
593 | // zero size, don't make it resizable |
|
606 | // zero size, don't make it resizable | |
594 | return; |
|
607 | return; | |
595 | } |
|
608 | } | |
596 | img.resizable({ |
|
609 | img.resizable({ | |
597 | aspectRatio: true, |
|
610 | aspectRatio: true, | |
598 | autoHide: true |
|
611 | autoHide: true | |
599 | }); |
|
612 | }); | |
600 | img.dblclick(function () { |
|
613 | img.dblclick(function () { | |
601 | // resize wrapper & image together for some reason: |
|
614 | // resize wrapper & image together for some reason: | |
602 | img.parent().height(h0); |
|
615 | img.parent().height(h0); | |
603 | img.height(h0); |
|
616 | img.height(h0); | |
604 | img.parent().width(w0); |
|
617 | img.parent().width(w0); | |
605 | img.width(w0); |
|
618 | img.width(w0); | |
606 | }); |
|
619 | }); | |
607 | }); |
|
620 | }); | |
608 | }; |
|
621 | }; | |
609 |
|
622 | |||
610 | var set_width_height = function (img, md, mime) { |
|
623 | var set_width_height = function (img, md, mime) { | |
611 | // set width and height of an img element from metadata |
|
624 | // set width and height of an img element from metadata | |
612 | var height = _get_metadata_key(md, 'height', mime); |
|
625 | var height = _get_metadata_key(md, 'height', mime); | |
613 | if (height !== undefined) img.attr('height', height); |
|
626 | if (height !== undefined) img.attr('height', height); | |
614 | var width = _get_metadata_key(md, 'width', mime); |
|
627 | var width = _get_metadata_key(md, 'width', mime); | |
615 | if (width !== undefined) img.attr('width', width); |
|
628 | if (width !== undefined) img.attr('width', width); | |
616 | }; |
|
629 | }; | |
617 |
|
630 | |||
618 | var append_png = function (png, md, element) { |
|
631 | var append_png = function (png, md, element, handle_inserted) { | |
619 | var type = 'image/png'; |
|
632 | var type = 'image/png'; | |
620 | var toinsert = this.create_output_subarea(md, "output_png", type); |
|
633 | var toinsert = this.create_output_subarea(md, "output_png", type); | |
621 | var img = $("<img/>").attr('src','data:image/png;base64,'+png); |
|
634 | var img = $("<img/>").attr('src','data:image/png;base64,'+png); | |
|
635 | if (handle_inserted !== undefined) { | |||
|
636 | img.load(handle_inserted); | |||
|
637 | } | |||
622 | set_width_height(img, md, 'image/png'); |
|
638 | set_width_height(img, md, 'image/png'); | |
623 | this._dblclick_to_reset_size(img); |
|
639 | this._dblclick_to_reset_size(img); | |
624 | toinsert.append(img); |
|
640 | toinsert.append(img); | |
625 | element.append(toinsert); |
|
641 | element.append(toinsert); | |
626 | return toinsert; |
|
642 | return toinsert; | |
627 | }; |
|
643 | }; | |
628 |
|
644 | |||
629 |
|
645 | |||
630 | var append_jpeg = function (jpeg, md, element) { |
|
646 | var append_jpeg = function (jpeg, md, element, handle_inserted) { | |
631 | var type = 'image/jpeg'; |
|
647 | var type = 'image/jpeg'; | |
632 | var toinsert = this.create_output_subarea(md, "output_jpeg", type); |
|
648 | var toinsert = this.create_output_subarea(md, "output_jpeg", type); | |
633 | var img = $("<img/>").attr('src','data:image/jpeg;base64,'+jpeg); |
|
649 | var img = $("<img/>").attr('src','data:image/jpeg;base64,'+jpeg); | |
|
650 | if (handle_inserted !== undefined) { | |||
|
651 | img.load(handle_inserted); | |||
|
652 | } | |||
634 | set_width_height(img, md, 'image/jpeg'); |
|
653 | set_width_height(img, md, 'image/jpeg'); | |
635 | this._dblclick_to_reset_size(img); |
|
654 | this._dblclick_to_reset_size(img); | |
636 | toinsert.append(img); |
|
655 | toinsert.append(img); | |
637 | element.append(toinsert); |
|
656 | element.append(toinsert); | |
638 | return toinsert; |
|
657 | return toinsert; | |
639 | }; |
|
658 | }; | |
640 |
|
659 | |||
641 |
|
660 | |||
642 | var append_pdf = function (pdf, md, element) { |
|
661 | var append_pdf = function (pdf, md, element) { | |
643 | var type = 'application/pdf'; |
|
662 | var type = 'application/pdf'; | |
644 | var toinsert = this.create_output_subarea(md, "output_pdf", type); |
|
663 | var toinsert = this.create_output_subarea(md, "output_pdf", type); | |
645 | var a = $('<a/>').attr('href', 'data:application/pdf;base64,'+pdf); |
|
664 | var a = $('<a/>').attr('href', 'data:application/pdf;base64,'+pdf); | |
646 | a.attr('target', '_blank'); |
|
665 | a.attr('target', '_blank'); | |
647 | a.text('View PDF') |
|
666 | a.text('View PDF') | |
648 | toinsert.append(a); |
|
667 | toinsert.append(a); | |
649 | element.append(toinsert); |
|
668 | element.append(toinsert); | |
650 | return toinsert; |
|
669 | return toinsert; | |
651 | } |
|
670 | } | |
652 |
|
671 | |||
653 | var append_latex = function (latex, md, element) { |
|
672 | var append_latex = function (latex, md, element) { | |
654 | // This method cannot do the typesetting because the latex first has to |
|
673 | // This method cannot do the typesetting because the latex first has to | |
655 | // be on the page. |
|
674 | // be on the page. | |
656 | var type = 'text/latex'; |
|
675 | var type = 'text/latex'; | |
657 | var toinsert = this.create_output_subarea(md, "output_latex", type); |
|
676 | var toinsert = this.create_output_subarea(md, "output_latex", type); | |
658 | toinsert.append(latex); |
|
677 | toinsert.append(latex); | |
659 | element.append(toinsert); |
|
678 | element.append(toinsert); | |
660 | return toinsert; |
|
679 | return toinsert; | |
661 | }; |
|
680 | }; | |
662 |
|
681 | |||
663 |
|
682 | |||
664 | OutputArea.prototype.append_raw_input = function (msg) { |
|
683 | OutputArea.prototype.append_raw_input = function (msg) { | |
665 | var that = this; |
|
684 | var that = this; | |
666 | this.expand(); |
|
685 | this.expand(); | |
667 | var content = msg.content; |
|
686 | var content = msg.content; | |
668 | var area = this.create_output_area(); |
|
687 | var area = this.create_output_area(); | |
669 |
|
688 | |||
670 | // disable any other raw_inputs, if they are left around |
|
689 | // disable any other raw_inputs, if they are left around | |
671 | $("div.output_subarea.raw_input_container").remove(); |
|
690 | $("div.output_subarea.raw_input_container").remove(); | |
672 |
|
691 | |||
673 | area.append( |
|
692 | area.append( | |
674 | $("<div/>") |
|
693 | $("<div/>") | |
675 | .addClass("box-flex1 output_subarea raw_input_container") |
|
694 | .addClass("box-flex1 output_subarea raw_input_container") | |
676 | .append( |
|
695 | .append( | |
677 | $("<span/>") |
|
696 | $("<span/>") | |
678 | .addClass("raw_input_prompt") |
|
697 | .addClass("raw_input_prompt") | |
679 | .text(content.prompt) |
|
698 | .text(content.prompt) | |
680 | ) |
|
699 | ) | |
681 | .append( |
|
700 | .append( | |
682 | $("<input/>") |
|
701 | $("<input/>") | |
683 | .addClass("raw_input") |
|
702 | .addClass("raw_input") | |
684 | .attr('type', 'text') |
|
703 | .attr('type', 'text') | |
685 | .attr("size", 47) |
|
704 | .attr("size", 47) | |
686 | .keydown(function (event, ui) { |
|
705 | .keydown(function (event, ui) { | |
687 | // make sure we submit on enter, |
|
706 | // make sure we submit on enter, | |
688 | // and don't re-execute the *cell* on shift-enter |
|
707 | // and don't re-execute the *cell* on shift-enter | |
689 | if (event.which === IPython.keyboard.keycodes.enter) { |
|
708 | if (event.which === IPython.keyboard.keycodes.enter) { | |
690 | that._submit_raw_input(); |
|
709 | that._submit_raw_input(); | |
691 | return false; |
|
710 | return false; | |
692 | } |
|
711 | } | |
693 | }) |
|
712 | }) | |
694 | ) |
|
713 | ) | |
695 | ); |
|
714 | ); | |
696 |
|
715 | |||
697 | this.element.append(area); |
|
716 | this.element.append(area); | |
698 | var raw_input = area.find('input.raw_input'); |
|
717 | var raw_input = area.find('input.raw_input'); | |
699 | // Register events that enable/disable the keyboard manager while raw |
|
718 | // Register events that enable/disable the keyboard manager while raw | |
700 | // input is focused. |
|
719 | // input is focused. | |
701 | IPython.keyboard_manager.register_events(raw_input); |
|
720 | IPython.keyboard_manager.register_events(raw_input); | |
702 | // Note, the following line used to read raw_input.focus().focus(). |
|
721 | // Note, the following line used to read raw_input.focus().focus(). | |
703 | // This seemed to be needed otherwise only the cell would be focused. |
|
722 | // This seemed to be needed otherwise only the cell would be focused. | |
704 | // But with the modal UI, this seems to work fine with one call to focus(). |
|
723 | // But with the modal UI, this seems to work fine with one call to focus(). | |
705 | raw_input.focus(); |
|
724 | raw_input.focus(); | |
706 | } |
|
725 | } | |
707 |
|
726 | |||
708 | OutputArea.prototype._submit_raw_input = function (evt) { |
|
727 | OutputArea.prototype._submit_raw_input = function (evt) { | |
709 | var container = this.element.find("div.raw_input_container"); |
|
728 | var container = this.element.find("div.raw_input_container"); | |
710 | var theprompt = container.find("span.raw_input_prompt"); |
|
729 | var theprompt = container.find("span.raw_input_prompt"); | |
711 | var theinput = container.find("input.raw_input"); |
|
730 | var theinput = container.find("input.raw_input"); | |
712 | var value = theinput.val(); |
|
731 | var value = theinput.val(); | |
713 | var content = { |
|
732 | var content = { | |
714 | output_type : 'stream', |
|
733 | output_type : 'stream', | |
715 | name : 'stdout', |
|
734 | name : 'stdout', | |
716 | text : theprompt.text() + value + '\n' |
|
735 | text : theprompt.text() + value + '\n' | |
717 | } |
|
736 | } | |
718 | // remove form container |
|
737 | // remove form container | |
719 | container.parent().remove(); |
|
738 | container.parent().remove(); | |
720 | // replace with plaintext version in stdout |
|
739 | // replace with plaintext version in stdout | |
721 | this.append_output(content, false); |
|
740 | this.append_output(content, false); | |
722 | $([IPython.events]).trigger('send_input_reply.Kernel', value); |
|
741 | $([IPython.events]).trigger('send_input_reply.Kernel', value); | |
723 | } |
|
742 | } | |
724 |
|
743 | |||
725 |
|
744 | |||
726 | OutputArea.prototype.handle_clear_output = function (msg) { |
|
745 | OutputArea.prototype.handle_clear_output = function (msg) { | |
727 | // msg spec v4 had stdout, stderr, display keys |
|
746 | // msg spec v4 had stdout, stderr, display keys | |
728 | // v4.1 replaced these with just wait |
|
747 | // v4.1 replaced these with just wait | |
729 | // The default behavior is the same (stdout=stderr=display=True, wait=False), |
|
748 | // The default behavior is the same (stdout=stderr=display=True, wait=False), | |
730 | // so v4 messages will still be properly handled, |
|
749 | // so v4 messages will still be properly handled, | |
731 | // except for the rarely used clearing less than all output. |
|
750 | // except for the rarely used clearing less than all output. | |
732 | this.clear_output(msg.content.wait || false); |
|
751 | this.clear_output(msg.content.wait || false); | |
733 | }; |
|
752 | }; | |
734 |
|
753 | |||
735 |
|
754 | |||
736 | OutputArea.prototype.clear_output = function(wait) { |
|
755 | OutputArea.prototype.clear_output = function(wait) { | |
737 | if (wait) { |
|
756 | if (wait) { | |
738 |
|
757 | |||
739 | // If a clear is queued, clear before adding another to the queue. |
|
758 | // If a clear is queued, clear before adding another to the queue. | |
740 | if (this.clear_queued) { |
|
759 | if (this.clear_queued) { | |
741 | this.clear_output(false); |
|
760 | this.clear_output(false); | |
742 | }; |
|
761 | }; | |
743 |
|
762 | |||
744 | this.clear_queued = true; |
|
763 | this.clear_queued = true; | |
745 | } else { |
|
764 | } else { | |
746 |
|
765 | |||
747 | // Fix the output div's height if the clear_output is waiting for |
|
766 | // Fix the output div's height if the clear_output is waiting for | |
748 | // new output (it is being used in an animation). |
|
767 | // new output (it is being used in an animation). | |
749 | if (this.clear_queued) { |
|
768 | if (this.clear_queued) { | |
750 | var height = this.element.height(); |
|
769 | var height = this.element.height(); | |
751 | this.element.height(height); |
|
770 | this.element.height(height); | |
752 | this.clear_queued = false; |
|
771 | this.clear_queued = false; | |
753 | } |
|
772 | } | |
754 |
|
773 | |||
755 | // clear all, no need for logic |
|
774 | // clear all, no need for logic | |
756 | this.element.html(""); |
|
775 | this.element.html(""); | |
757 | this.outputs = []; |
|
776 | this.outputs = []; | |
758 | this.trusted = true; |
|
777 | this.trusted = true; | |
759 | this.unscroll_area(); |
|
778 | this.unscroll_area(); | |
760 | return; |
|
779 | return; | |
761 | }; |
|
780 | }; | |
762 | }; |
|
781 | }; | |
763 |
|
782 | |||
764 |
|
783 | |||
765 | // JSON serialization |
|
784 | // JSON serialization | |
766 |
|
785 | |||
767 | OutputArea.prototype.fromJSON = function (outputs) { |
|
786 | OutputArea.prototype.fromJSON = function (outputs) { | |
768 | var len = outputs.length; |
|
787 | var len = outputs.length; | |
769 | var data; |
|
788 | var data; | |
770 |
|
789 | |||
771 | for (var i=0; i<len; i++) { |
|
790 | for (var i=0; i<len; i++) { | |
772 | data = outputs[i]; |
|
791 | data = outputs[i]; | |
773 | var msg_type = data.output_type; |
|
792 | var msg_type = data.output_type; | |
774 | if (msg_type === "display_data" || msg_type === "pyout") { |
|
793 | if (msg_type === "display_data" || msg_type === "pyout") { | |
775 | // convert short keys to mime keys |
|
794 | // convert short keys to mime keys | |
776 | // TODO: remove mapping of short keys when we update to nbformat 4 |
|
795 | // TODO: remove mapping of short keys when we update to nbformat 4 | |
777 | data = this.rename_keys(data, OutputArea.mime_map_r); |
|
796 | data = this.rename_keys(data, OutputArea.mime_map_r); | |
778 | data.metadata = this.rename_keys(data.metadata, OutputArea.mime_map_r); |
|
797 | data.metadata = this.rename_keys(data.metadata, OutputArea.mime_map_r); | |
779 | } |
|
798 | } | |
780 |
|
799 | |||
781 | this.append_output(data); |
|
800 | this.append_output(data); | |
782 | } |
|
801 | } | |
783 | }; |
|
802 | }; | |
784 |
|
803 | |||
785 |
|
804 | |||
786 | OutputArea.prototype.toJSON = function () { |
|
805 | OutputArea.prototype.toJSON = function () { | |
787 | var outputs = []; |
|
806 | var outputs = []; | |
788 | var len = this.outputs.length; |
|
807 | var len = this.outputs.length; | |
789 | var data; |
|
808 | var data; | |
790 | for (var i=0; i<len; i++) { |
|
809 | for (var i=0; i<len; i++) { | |
791 | data = this.outputs[i]; |
|
810 | data = this.outputs[i]; | |
792 | var msg_type = data.output_type; |
|
811 | var msg_type = data.output_type; | |
793 | if (msg_type === "display_data" || msg_type === "pyout") { |
|
812 | if (msg_type === "display_data" || msg_type === "pyout") { | |
794 | // convert mime keys to short keys |
|
813 | // convert mime keys to short keys | |
795 | data = this.rename_keys(data, OutputArea.mime_map); |
|
814 | data = this.rename_keys(data, OutputArea.mime_map); | |
796 | data.metadata = this.rename_keys(data.metadata, OutputArea.mime_map); |
|
815 | data.metadata = this.rename_keys(data.metadata, OutputArea.mime_map); | |
797 | } |
|
816 | } | |
798 | outputs[i] = data; |
|
817 | outputs[i] = data; | |
799 | } |
|
818 | } | |
800 | return outputs; |
|
819 | return outputs; | |
801 | }; |
|
820 | }; | |
802 |
|
821 | |||
803 | /** |
|
822 | /** | |
804 | * Class properties |
|
823 | * Class properties | |
805 | **/ |
|
824 | **/ | |
806 |
|
825 | |||
807 | /** |
|
826 | /** | |
808 | * Threshold to trigger autoscroll when the OutputArea is resized, |
|
827 | * Threshold to trigger autoscroll when the OutputArea is resized, | |
809 | * typically when new outputs are added. |
|
828 | * typically when new outputs are added. | |
810 | * |
|
829 | * | |
811 | * Behavior is undefined if autoscroll is lower than minimum_scroll_threshold, |
|
830 | * Behavior is undefined if autoscroll is lower than minimum_scroll_threshold, | |
812 | * unless it is < 0, in which case autoscroll will never be triggered |
|
831 | * unless it is < 0, in which case autoscroll will never be triggered | |
813 | * |
|
832 | * | |
814 | * @property auto_scroll_threshold |
|
833 | * @property auto_scroll_threshold | |
815 | * @type Number |
|
834 | * @type Number | |
816 | * @default 100 |
|
835 | * @default 100 | |
817 | * |
|
836 | * | |
818 | **/ |
|
837 | **/ | |
819 | OutputArea.auto_scroll_threshold = 100; |
|
838 | OutputArea.auto_scroll_threshold = 100; | |
820 |
|
839 | |||
821 | /** |
|
840 | /** | |
822 | * Lower limit (in lines) for OutputArea to be made scrollable. OutputAreas |
|
841 | * Lower limit (in lines) for OutputArea to be made scrollable. OutputAreas | |
823 | * shorter than this are never scrolled. |
|
842 | * shorter than this are never scrolled. | |
824 | * |
|
843 | * | |
825 | * @property minimum_scroll_threshold |
|
844 | * @property minimum_scroll_threshold | |
826 | * @type Number |
|
845 | * @type Number | |
827 | * @default 20 |
|
846 | * @default 20 | |
828 | * |
|
847 | * | |
829 | **/ |
|
848 | **/ | |
830 | OutputArea.minimum_scroll_threshold = 20; |
|
849 | OutputArea.minimum_scroll_threshold = 20; | |
831 |
|
850 | |||
832 |
|
851 | |||
833 |
|
852 | |||
834 | OutputArea.mime_map = { |
|
853 | OutputArea.mime_map = { | |
835 | "text/plain" : "text", |
|
854 | "text/plain" : "text", | |
836 | "text/html" : "html", |
|
855 | "text/html" : "html", | |
837 | "image/svg+xml" : "svg", |
|
856 | "image/svg+xml" : "svg", | |
838 | "image/png" : "png", |
|
857 | "image/png" : "png", | |
839 | "image/jpeg" : "jpeg", |
|
858 | "image/jpeg" : "jpeg", | |
840 | "text/latex" : "latex", |
|
859 | "text/latex" : "latex", | |
841 | "application/json" : "json", |
|
860 | "application/json" : "json", | |
842 | "application/javascript" : "javascript", |
|
861 | "application/javascript" : "javascript", | |
843 | }; |
|
862 | }; | |
844 |
|
863 | |||
845 | OutputArea.mime_map_r = { |
|
864 | OutputArea.mime_map_r = { | |
846 | "text" : "text/plain", |
|
865 | "text" : "text/plain", | |
847 | "html" : "text/html", |
|
866 | "html" : "text/html", | |
848 | "svg" : "image/svg+xml", |
|
867 | "svg" : "image/svg+xml", | |
849 | "png" : "image/png", |
|
868 | "png" : "image/png", | |
850 | "jpeg" : "image/jpeg", |
|
869 | "jpeg" : "image/jpeg", | |
851 | "latex" : "text/latex", |
|
870 | "latex" : "text/latex", | |
852 | "json" : "application/json", |
|
871 | "json" : "application/json", | |
853 | "javascript" : "application/javascript", |
|
872 | "javascript" : "application/javascript", | |
854 | }; |
|
873 | }; | |
855 |
|
874 | |||
856 | OutputArea.display_order = [ |
|
875 | OutputArea.display_order = [ | |
857 | 'application/javascript', |
|
876 | 'application/javascript', | |
858 | 'text/html', |
|
877 | 'text/html', | |
859 | 'text/latex', |
|
878 | 'text/latex', | |
860 | 'image/svg+xml', |
|
879 | 'image/svg+xml', | |
861 | 'image/png', |
|
880 | 'image/png', | |
862 | 'image/jpeg', |
|
881 | 'image/jpeg', | |
863 | 'application/pdf', |
|
882 | 'application/pdf', | |
864 | 'text/plain' |
|
883 | 'text/plain' | |
865 | ]; |
|
884 | ]; | |
866 |
|
885 | |||
867 | OutputArea.append_map = { |
|
886 | OutputArea.append_map = { | |
868 | "text/plain" : append_text, |
|
887 | "text/plain" : append_text, | |
869 | "text/html" : append_html, |
|
888 | "text/html" : append_html, | |
870 | "image/svg+xml" : append_svg, |
|
889 | "image/svg+xml" : append_svg, | |
871 | "image/png" : append_png, |
|
890 | "image/png" : append_png, | |
872 | "image/jpeg" : append_jpeg, |
|
891 | "image/jpeg" : append_jpeg, | |
873 | "text/latex" : append_latex, |
|
892 | "text/latex" : append_latex, | |
874 | "application/javascript" : append_javascript, |
|
893 | "application/javascript" : append_javascript, | |
875 | "application/pdf" : append_pdf |
|
894 | "application/pdf" : append_pdf | |
876 | }; |
|
895 | }; | |
877 |
|
896 | |||
878 | IPython.OutputArea = OutputArea; |
|
897 | IPython.OutputArea = OutputArea; | |
879 |
|
898 | |||
880 | return IPython; |
|
899 | return IPython; | |
881 |
|
900 | |||
882 | }(IPython)); |
|
901 | }(IPython)); |
General Comments 0
You need to be logged in to leave comments.
Login now