Show More
@@ -1,878 +1,912 | |||||
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 | // validate output data types |
|
281 | // validate output data types | |
282 | json = this.validate_output(json); |
|
282 | json = this.validate_output(json); | |
283 |
|
283 | |||
284 | if (json.output_type === 'pyout') { |
|
284 | if (json.output_type === 'pyout') { | |
285 | this.append_pyout(json); |
|
285 | this.append_pyout(json); | |
286 | } else if (json.output_type === 'pyerr') { |
|
286 | } else if (json.output_type === 'pyerr') { | |
287 | this.append_pyerr(json); |
|
287 | this.append_pyerr(json); | |
288 | } else if (json.output_type === 'display_data') { |
|
288 | } else if (json.output_type === 'display_data') { | |
289 | this.append_display_data(json); |
|
289 | this.append_display_data(json); | |
290 | } else if (json.output_type === 'stream') { |
|
290 | } else if (json.output_type === 'stream') { | |
291 | this.append_stream(json); |
|
291 | this.append_stream(json); | |
292 | } |
|
292 | } | |
293 |
|
293 | |||
294 | this.outputs.push(json); |
|
294 | this.outputs.push(json); | |
295 |
|
295 | |||
296 | // Only reset the height to automatic if the height is currently |
|
296 | // Only reset the height to automatic if the height is currently | |
297 | // fixed (done by wait=True flag on clear_output). |
|
297 | // fixed (done by wait=True flag on clear_output). | |
298 | if (needs_height_reset) { |
|
298 | if (needs_height_reset) { | |
299 | this.element.height(''); |
|
299 | this.element.height(''); | |
300 | } |
|
300 | } | |
301 |
|
301 | |||
302 | var that = this; |
|
302 | var that = this; | |
303 | setTimeout(function(){that.element.trigger('resize');}, 100); |
|
303 | setTimeout(function(){that.element.trigger('resize');}, 100); | |
304 | }; |
|
304 | }; | |
305 |
|
305 | |||
306 |
|
306 | |||
307 | OutputArea.prototype.create_output_area = function () { |
|
307 | OutputArea.prototype.create_output_area = function () { | |
308 | var oa = $("<div/>").addClass("output_area"); |
|
308 | var oa = $("<div/>").addClass("output_area"); | |
309 | if (this.prompt_area) { |
|
309 | if (this.prompt_area) { | |
310 | oa.append($('<div/>').addClass('prompt')); |
|
310 | oa.append($('<div/>').addClass('prompt')); | |
311 | } |
|
311 | } | |
312 | return oa; |
|
312 | return oa; | |
313 | }; |
|
313 | }; | |
314 |
|
314 | |||
315 |
|
315 | |||
316 | function _get_metadata_key(metadata, key, mime) { |
|
316 | function _get_metadata_key(metadata, key, mime) { | |
317 | var mime_md = metadata[mime]; |
|
317 | var mime_md = metadata[mime]; | |
318 | // mime-specific higher priority |
|
318 | // mime-specific higher priority | |
319 | if (mime_md && mime_md[key] !== undefined) { |
|
319 | if (mime_md && mime_md[key] !== undefined) { | |
320 | return mime_md[key]; |
|
320 | return mime_md[key]; | |
321 | } |
|
321 | } | |
322 | // fallback on global |
|
322 | // fallback on global | |
323 | return metadata[key]; |
|
323 | return metadata[key]; | |
324 | } |
|
324 | } | |
325 |
|
325 | |||
326 | OutputArea.prototype.create_output_subarea = function(md, classes, mime) { |
|
326 | OutputArea.prototype.create_output_subarea = function(md, classes, mime) { | |
327 | var subarea = $('<div/>').addClass('output_subarea').addClass(classes); |
|
327 | var subarea = $('<div/>').addClass('output_subarea').addClass(classes); | |
328 | if (_get_metadata_key(md, 'isolated', mime)) { |
|
328 | if (_get_metadata_key(md, 'isolated', mime)) { | |
329 | // Create an iframe to isolate the subarea from the rest of the |
|
329 | // Create an iframe to isolate the subarea from the rest of the | |
330 | // document |
|
330 | // document | |
331 | var iframe = $('<iframe/>').addClass('box-flex1'); |
|
331 | var iframe = $('<iframe/>').addClass('box-flex1'); | |
332 | iframe.css({'height':1, 'width':'100%', 'display':'block'}); |
|
332 | iframe.css({'height':1, 'width':'100%', 'display':'block'}); | |
333 | iframe.attr('frameborder', 0); |
|
333 | iframe.attr('frameborder', 0); | |
334 | iframe.attr('scrolling', 'auto'); |
|
334 | iframe.attr('scrolling', 'auto'); | |
335 |
|
335 | |||
336 | // Once the iframe is loaded, the subarea is dynamically inserted |
|
336 | // Once the iframe is loaded, the subarea is dynamically inserted | |
337 | iframe.on('load', function() { |
|
337 | iframe.on('load', function() { | |
338 | // Workaround needed by Firefox, to properly render svg inside |
|
338 | // Workaround needed by Firefox, to properly render svg inside | |
339 | // iframes, see http://stackoverflow.com/questions/10177190/ |
|
339 | // iframes, see http://stackoverflow.com/questions/10177190/ | |
340 | // svg-dynamically-added-to-iframe-does-not-render-correctly |
|
340 | // svg-dynamically-added-to-iframe-does-not-render-correctly | |
341 | this.contentDocument.open(); |
|
341 | this.contentDocument.open(); | |
342 |
|
342 | |||
343 | // Insert the subarea into the iframe |
|
343 | // Insert the subarea into the iframe | |
344 | // We must directly write the html. When using Jquery's append |
|
344 | // We must directly write the html. When using Jquery's append | |
345 | // method, javascript is evaluated in the parent document and |
|
345 | // method, javascript is evaluated in the parent document and | |
346 | // not in the iframe document. At this point, subarea doesn't |
|
346 | // not in the iframe document. At this point, subarea doesn't | |
347 | // contain any user content. |
|
347 | // contain any user content. | |
348 | this.contentDocument.write(subarea.html()); |
|
348 | this.contentDocument.write(subarea.html()); | |
349 |
|
349 | |||
350 | this.contentDocument.close(); |
|
350 | this.contentDocument.close(); | |
351 |
|
351 | |||
352 | var body = this.contentDocument.body; |
|
352 | var body = this.contentDocument.body; | |
353 | // Adjust the iframe height automatically |
|
353 | // Adjust the iframe height automatically | |
354 | iframe.height(body.scrollHeight + 'px'); |
|
354 | iframe.height(body.scrollHeight + 'px'); | |
355 | }); |
|
355 | }); | |
356 |
|
356 | |||
357 | // Elements should be appended to the inner subarea and not to the |
|
357 | // Elements should be appended to the inner subarea and not to the | |
358 | // iframe |
|
358 | // iframe | |
359 | iframe.append = function(that) { |
|
359 | iframe.append = function(that) { | |
360 | subarea.append(that); |
|
360 | subarea.append(that); | |
361 | }; |
|
361 | }; | |
362 |
|
362 | |||
363 | return iframe; |
|
363 | return iframe; | |
364 | } else { |
|
364 | } else { | |
365 | return subarea; |
|
365 | return subarea; | |
366 | } |
|
366 | } | |
367 | } |
|
367 | } | |
368 |
|
368 | |||
369 |
|
369 | |||
370 | OutputArea.prototype._append_javascript_error = function (err, element) { |
|
370 | OutputArea.prototype._append_javascript_error = function (err, element) { | |
371 | // display a message when a javascript error occurs in display output |
|
371 | // display a message when a javascript error occurs in display output | |
372 | var msg = "Javascript error adding output!" |
|
372 | var msg = "Javascript error adding output!" | |
373 | if ( element === undefined ) return; |
|
373 | if ( element === undefined ) return; | |
374 | element |
|
374 | element | |
375 | .append($('<div/>').text(msg).addClass('js-error')) |
|
375 | .append($('<div/>').text(msg).addClass('js-error')) | |
376 | .append($('<div/>').text(err.toString()).addClass('js-error')) |
|
376 | .append($('<div/>').text(err.toString()).addClass('js-error')) | |
377 | .append($('<div/>').text('See your browser Javascript console for more details.').addClass('js-error')); |
|
377 | .append($('<div/>').text('See your browser Javascript console for more details.').addClass('js-error')); | |
378 | }; |
|
378 | }; | |
379 |
|
379 | |||
380 | OutputArea.prototype._safe_append = function (toinsert) { |
|
380 | OutputArea.prototype._safe_append = function (toinsert) { | |
381 | // safely append an item to the document |
|
381 | // safely append an item to the document | |
382 | // this is an object created by user code, |
|
382 | // this is an object created by user code, | |
383 | // and may have errors, which should not be raised |
|
383 | // and may have errors, which should not be raised | |
384 | // under any circumstances. |
|
384 | // under any circumstances. | |
385 | try { |
|
385 | try { | |
386 | this.element.append(toinsert); |
|
386 | this.element.append(toinsert); | |
387 | } catch(err) { |
|
387 | } catch(err) { | |
388 | console.log(err); |
|
388 | console.log(err); | |
389 | // Create an actual output_area and output_subarea, which creates |
|
389 | // Create an actual output_area and output_subarea, which creates | |
390 | // the prompt area and the proper indentation. |
|
390 | // the prompt area and the proper indentation. | |
391 | var toinsert = this.create_output_area(); |
|
391 | var toinsert = this.create_output_area(); | |
392 | var subarea = $('<div/>').addClass('output_subarea'); |
|
392 | var subarea = $('<div/>').addClass('output_subarea'); | |
393 | toinsert.append(subarea); |
|
393 | toinsert.append(subarea); | |
394 | this._append_javascript_error(err, subarea); |
|
394 | this._append_javascript_error(err, subarea); | |
395 | this.element.append(toinsert); |
|
395 | this.element.append(toinsert); | |
396 | } |
|
396 | } | |
397 | }; |
|
397 | }; | |
398 |
|
398 | |||
399 |
|
399 | |||
400 | OutputArea.prototype.append_pyout = function (json) { |
|
400 | OutputArea.prototype.append_pyout = function (json) { | |
401 | var n = json.prompt_number || ' '; |
|
401 | var n = json.prompt_number || ' '; | |
402 | var toinsert = this.create_output_area(); |
|
402 | var toinsert = this.create_output_area(); | |
403 | if (this.prompt_area) { |
|
403 | if (this.prompt_area) { | |
404 | toinsert.find('div.prompt').addClass('output_prompt').text('Out[' + n + ']:'); |
|
404 | toinsert.find('div.prompt').addClass('output_prompt').text('Out[' + n + ']:'); | |
405 | } |
|
405 | } | |
406 | var inserted = this.append_mime_type(json, toinsert); |
|
406 | var inserted = this.append_mime_type(json, toinsert); | |
407 | if (inserted) { |
|
407 | if (inserted) { | |
408 | inserted.addClass('output_pyout'); |
|
408 | inserted.addClass('output_pyout'); | |
409 | } |
|
409 | } | |
410 | this._safe_append(toinsert); |
|
410 | this._safe_append(toinsert); | |
411 | // If we just output latex, typeset it. |
|
411 | // If we just output latex, typeset it. | |
412 | if ((json['text/latex'] !== undefined) || (json['text/html'] !== undefined)) { |
|
412 | if ((json['text/latex'] !== undefined) || (json['text/html'] !== undefined)) { | |
413 | this.typeset(); |
|
413 | this.typeset(); | |
414 | } |
|
414 | } | |
415 | }; |
|
415 | }; | |
416 |
|
416 | |||
417 |
|
417 | |||
418 | OutputArea.prototype.append_pyerr = function (json) { |
|
418 | OutputArea.prototype.append_pyerr = function (json) { | |
419 | var tb = json.traceback; |
|
419 | var tb = json.traceback; | |
420 | if (tb !== undefined && tb.length > 0) { |
|
420 | if (tb !== undefined && tb.length > 0) { | |
421 | var s = ''; |
|
421 | var s = ''; | |
422 | var len = tb.length; |
|
422 | var len = tb.length; | |
423 | for (var i=0; i<len; i++) { |
|
423 | for (var i=0; i<len; i++) { | |
424 | s = s + tb[i] + '\n'; |
|
424 | s = s + tb[i] + '\n'; | |
425 | } |
|
425 | } | |
426 | s = s + '\n'; |
|
426 | s = s + '\n'; | |
427 | var toinsert = this.create_output_area(); |
|
427 | var toinsert = this.create_output_area(); | |
428 | var append_text = OutputArea.append_map['text/plain']; |
|
428 | var append_text = OutputArea.append_map['text/plain']; | |
429 | if (append_text) { |
|
429 | if (append_text) { | |
430 | append_text.apply(this, [s, {}, toinsert]).addClass('output_pyerr'); |
|
430 | append_text.apply(this, [s, {}, toinsert]).addClass('output_pyerr'); | |
431 | } |
|
431 | } | |
432 | this._safe_append(toinsert); |
|
432 | this._safe_append(toinsert); | |
433 | } |
|
433 | } | |
434 | }; |
|
434 | }; | |
435 |
|
435 | |||
436 |
|
436 | |||
437 | OutputArea.prototype.append_stream = function (json) { |
|
437 | OutputArea.prototype.append_stream = function (json) { | |
438 | // temporary fix: if stream undefined (json file written prior to this patch), |
|
438 | // temporary fix: if stream undefined (json file written prior to this patch), | |
439 | // default to most likely stdout: |
|
439 | // default to most likely stdout: | |
440 | if (json.stream === undefined){ |
|
440 | if (json.stream === undefined){ | |
441 | json.stream = 'stdout'; |
|
441 | json.stream = 'stdout'; | |
442 | } |
|
442 | } | |
443 | var text = json.text; |
|
443 | var text = json.text; | |
444 | var subclass = "output_"+json.stream; |
|
444 | var subclass = "output_"+json.stream; | |
445 | if (this.outputs.length > 0){ |
|
445 | if (this.outputs.length > 0){ | |
446 | // have at least one output to consider |
|
446 | // have at least one output to consider | |
447 | var last = this.outputs[this.outputs.length-1]; |
|
447 | var last = this.outputs[this.outputs.length-1]; | |
448 | if (last.output_type == 'stream' && json.stream == last.stream){ |
|
448 | if (last.output_type == 'stream' && json.stream == last.stream){ | |
449 | // latest output was in the same stream, |
|
449 | // latest output was in the same stream, | |
450 | // so append directly into its pre tag |
|
450 | // so append directly into its pre tag | |
451 | // escape ANSI & HTML specials: |
|
451 | // escape ANSI & HTML specials: | |
452 | var pre = this.element.find('div.'+subclass).last().find('pre'); |
|
452 | var pre = this.element.find('div.'+subclass).last().find('pre'); | |
453 | var html = utils.fixCarriageReturn( |
|
453 | var html = utils.fixCarriageReturn( | |
454 | pre.html() + utils.fixConsole(text)); |
|
454 | pre.html() + utils.fixConsole(text)); | |
455 | // The only user content injected with this HTML call is |
|
455 | // The only user content injected with this HTML call is | |
456 | // escaped by the fixConsole() method. |
|
456 | // escaped by the fixConsole() method. | |
457 | pre.html(html); |
|
457 | pre.html(html); | |
458 | return; |
|
458 | return; | |
459 | } |
|
459 | } | |
460 | } |
|
460 | } | |
461 |
|
461 | |||
462 | if (!text.replace("\r", "")) { |
|
462 | if (!text.replace("\r", "")) { | |
463 | // text is nothing (empty string, \r, etc.) |
|
463 | // text is nothing (empty string, \r, etc.) | |
464 | // so don't append any elements, which might add undesirable space |
|
464 | // so don't append any elements, which might add undesirable space | |
465 | return; |
|
465 | return; | |
466 | } |
|
466 | } | |
467 |
|
467 | |||
468 | // If we got here, attach a new div |
|
468 | // If we got here, attach a new div | |
469 | var toinsert = this.create_output_area(); |
|
469 | var toinsert = this.create_output_area(); | |
470 | var append_text = OutputArea.append_map['text/plain']; |
|
470 | var append_text = OutputArea.append_map['text/plain']; | |
471 | if (append_text) { |
|
471 | if (append_text) { | |
472 | append_text.apply(this, [text, {}, toinsert]).addClass("output_stream " + subclass); |
|
472 | append_text.apply(this, [text, {}, toinsert]).addClass("output_stream " + subclass); | |
473 | } |
|
473 | } | |
474 | this._safe_append(toinsert); |
|
474 | this._safe_append(toinsert); | |
475 | }; |
|
475 | }; | |
476 |
|
476 | |||
477 |
|
477 | |||
478 | OutputArea.prototype.append_display_data = function (json) { |
|
478 | OutputArea.prototype.append_display_data = function (json) { | |
479 | var toinsert = this.create_output_area(); |
|
479 | var toinsert = this.create_output_area(); | |
480 | if (this.append_mime_type(json, toinsert)) { |
|
480 | if (this.append_mime_type(json, toinsert)) { | |
481 | this._safe_append(toinsert); |
|
481 | this._safe_append(toinsert); | |
482 | // If we just output latex, typeset it. |
|
482 | // If we just output latex, typeset it. | |
483 | if ((json['text/latex'] !== undefined) || (json['text/html'] !== undefined)) { |
|
483 | if ((json['text/latex'] !== undefined) || (json['text/html'] !== undefined)) { | |
484 | this.typeset(); |
|
484 | this.typeset(); | |
485 | } |
|
485 | } | |
486 | } |
|
486 | } | |
487 | }; |
|
487 | }; | |
488 |
|
488 | |||
489 |
|
489 | |||
490 | OutputArea.safe_outputs = { |
|
490 | OutputArea.safe_outputs = { | |
491 | 'text/plain' : true, |
|
491 | 'text/plain' : true, | |
492 | 'text/latex' : true, |
|
492 | 'text/latex' : true, | |
493 | 'image/png' : true, |
|
493 | 'image/png' : true, | |
494 | 'image/jpeg' : true |
|
494 | 'image/jpeg' : true | |
495 | }; |
|
495 | }; | |
496 |
|
496 | |||
497 | OutputArea.prototype.append_mime_type = function (json, element) { |
|
497 | OutputArea.prototype.append_mime_type = function (json, element) { | |
498 | for (var type_i in OutputArea.display_order) { |
|
498 | for (var type_i in OutputArea.display_order) { | |
499 | var type = OutputArea.display_order[type_i]; |
|
499 | var type = OutputArea.display_order[type_i]; | |
500 | var append = OutputArea.append_map[type]; |
|
500 | var append = OutputArea.append_map[type]; | |
501 | if ((json[type] !== undefined) && append) { |
|
501 | if ((json[type] !== undefined) && append) { | |
502 | var value = json[type]; |
|
502 | var value = json[type]; | |
503 | if (!this.trusted && !OutputArea.safe_outputs[type]) { |
|
503 | if (!this.trusted && !OutputArea.safe_outputs[type]) { | |
504 | // not trusted, sanitize HTML |
|
504 | // not trusted, sanitize HTML | |
505 | if (type==='text/html' || type==='text/svg') { |
|
505 | if (type==='text/html' || type==='text/svg') { | |
506 | value = IPython.security.sanitize_html(value); |
|
506 | value = IPython.security.sanitize_html(value); | |
507 | } else { |
|
507 | } else { | |
508 | // don't display if we don't know how to sanitize it |
|
508 | // don't display if we don't know how to sanitize it | |
509 | console.log("Ignoring untrusted " + type + " output."); |
|
509 | console.log("Ignoring untrusted " + type + " output."); | |
510 | continue; |
|
510 | continue; | |
511 | } |
|
511 | } | |
512 | } |
|
512 | } | |
513 | var md = json.metadata || {}; |
|
513 | var md = json.metadata || {}; | |
514 | var toinsert = append.apply(this, [value, md, element]); |
|
514 | var toinsert = append.apply(this, [value, md, element]); | |
515 | $([IPython.events]).trigger('output_appended.OutputArea', [type, value, md, toinsert]); |
|
515 | $([IPython.events]).trigger('output_appended.OutputArea', [type, value, md, toinsert]); | |
516 | return toinsert; |
|
516 | return toinsert; | |
517 | } |
|
517 | } | |
518 | } |
|
518 | } | |
519 | return null; |
|
519 | return null; | |
520 | }; |
|
520 | }; | |
521 |
|
521 | |||
522 |
|
522 | |||
523 | var append_html = function (html, md, element) { |
|
523 | var append_html = function (html, md, element) { | |
524 | var type = 'text/html'; |
|
524 | var type = 'text/html'; | |
525 | var toinsert = this.create_output_subarea(md, "output_html rendered_html", type); |
|
525 | var toinsert = this.create_output_subarea(md, "output_html rendered_html", type); | |
526 | IPython.keyboard_manager.register_events(toinsert); |
|
526 | IPython.keyboard_manager.register_events(toinsert); | |
527 | toinsert.append(html); |
|
527 | toinsert.append(html); | |
528 | element.append(toinsert); |
|
528 | element.append(toinsert); | |
529 | return toinsert; |
|
529 | return toinsert; | |
530 | }; |
|
530 | }; | |
531 |
|
531 | |||
532 |
|
532 | |||
533 | var append_javascript = function (js, md, element) { |
|
533 | var append_javascript = function (js, md, element) { | |
534 | // We just eval the JS code, element appears in the local scope. |
|
534 | // We just eval the JS code, element appears in the local scope. | |
535 | var type = 'application/javascript'; |
|
535 | var type = 'application/javascript'; | |
536 | var toinsert = this.create_output_subarea(md, "output_javascript", type); |
|
536 | var toinsert = this.create_output_subarea(md, "output_javascript", type); | |
537 | IPython.keyboard_manager.register_events(toinsert); |
|
537 | IPython.keyboard_manager.register_events(toinsert); | |
538 | element.append(toinsert); |
|
538 | element.append(toinsert); | |
539 | // FIXME TODO : remove `container element for 3.0` |
|
539 | // FIXME TODO : remove `container element for 3.0` | |
540 | //backward compat, js should be eval'ed in a context where `container` is defined. |
|
540 | //backward compat, js should be eval'ed in a context where `container` is defined. | |
541 | var container = element; |
|
541 | var container = element; | |
542 | container.show = function(){console.log('Warning "container.show()" is deprecated.')}; |
|
542 | container.show = function(){console.log('Warning "container.show()" is deprecated.')}; | |
543 | // end backward compat |
|
543 | // end backward compat | |
544 |
|
544 | |||
545 | // Fix for ipython/issues/5293, make sure `element` is the area which |
|
545 | // Fix for ipython/issues/5293, make sure `element` is the area which | |
546 | // output can be inserted into at the time of JS execution. |
|
546 | // output can be inserted into at the time of JS execution. | |
547 | element = toinsert; |
|
547 | element = toinsert; | |
548 | try { |
|
548 | try { | |
549 | eval(js); |
|
549 | eval(js); | |
550 | } catch(err) { |
|
550 | } catch(err) { | |
551 | console.log(err); |
|
551 | console.log(err); | |
552 | this._append_javascript_error(err, toinsert); |
|
552 | this._append_javascript_error(err, toinsert); | |
553 | } |
|
553 | } | |
554 | return toinsert; |
|
554 | return toinsert; | |
555 | }; |
|
555 | }; | |
556 |
|
556 | |||
557 |
|
557 | |||
558 | var append_text = function (data, md, element) { |
|
558 | var append_text = function (data, md, element) { | |
559 | var type = 'text/plain'; |
|
559 | var type = 'text/plain'; | |
560 | var toinsert = this.create_output_subarea(md, "output_text", type); |
|
560 | var toinsert = this.create_output_subarea(md, "output_text", type); | |
561 | // escape ANSI & HTML specials in plaintext: |
|
561 | // escape ANSI & HTML specials in plaintext: | |
562 | data = utils.fixConsole(data); |
|
562 | data = utils.fixConsole(data); | |
563 | data = utils.fixCarriageReturn(data); |
|
563 | data = utils.fixCarriageReturn(data); | |
564 | data = utils.autoLinkUrls(data); |
|
564 | data = utils.autoLinkUrls(data); | |
565 | // The only user content injected with this HTML call is |
|
565 | // The only user content injected with this HTML call is | |
566 | // escaped by the fixConsole() method. |
|
566 | // escaped by the fixConsole() method. | |
567 | toinsert.append($("<pre/>").html(data)); |
|
567 | toinsert.append($("<pre/>").html(data)); | |
568 | element.append(toinsert); |
|
568 | element.append(toinsert); | |
569 | return toinsert; |
|
569 | return toinsert; | |
570 | }; |
|
570 | }; | |
571 |
|
571 | |||
572 |
|
572 | |||
573 | var append_svg = function (svg, md, element) { |
|
573 | var append_svg = function (svg_html, md, element) { | |
574 | var type = 'image/svg+xml'; |
|
574 | var type = 'image/svg+xml'; | |
575 | var toinsert = this.create_output_subarea(md, "output_svg", type); |
|
575 | var toinsert = this.create_output_subarea(md, "output_svg", type); | |
576 | toinsert.append(svg); |
|
576 | ||
|
577 | // Get the svg element from within the HTML. | |||
|
578 | var svg = $('<div />').html(svg_html).find('svg'); | |||
|
579 | var svg_area = $('<div />'); | |||
|
580 | var width = svg.attr('width'); | |||
|
581 | var height = svg.attr('height'); | |||
|
582 | svg | |||
|
583 | .width('100%') | |||
|
584 | .height('100%'); | |||
|
585 | svg_area | |||
|
586 | .width(width) | |||
|
587 | .height(height); | |||
|
588 | ||||
|
589 | // The jQuery resize handlers don't seem to work on the svg element. | |||
|
590 | // When the svg renders completely, measure it's size and set the parent | |||
|
591 | // div to that size. Then set the svg to 100% the size of the parent | |||
|
592 | // div and make the parent div resizable. | |||
|
593 | this._dblclick_to_reset_size(svg_area, true, false); | |||
|
594 | ||||
|
595 | svg_area.append(svg); | |||
|
596 | toinsert.append(svg_area); | |||
577 | element.append(toinsert); |
|
597 | element.append(toinsert); | |
|
598 | ||||
578 | return toinsert; |
|
599 | return toinsert; | |
579 | }; |
|
600 | }; | |
580 |
|
601 | |||
581 |
|
602 | OutputArea.prototype._dblclick_to_reset_size = function (img, immediately, resize_parent) { | ||
582 | OutputArea.prototype._dblclick_to_reset_size = function (img) { |
|
603 | // Add a resize handler to an element | |
583 | // wrap image after it's loaded on the page, |
|
604 | // | |
584 | // otherwise the measured initial size will be incorrect |
|
605 | // img: jQuery element | |
585 | img.on("load", function (){ |
|
606 | // immediately: bool=False | |
|
607 | // Wait for the element to load before creating the handle. | |||
|
608 | // resize_parent: bool=True | |||
|
609 | // Should the parent of the element be resized when the element is | |||
|
610 | // reset (by double click). | |||
|
611 | var callback = function (){ | |||
586 | var h0 = img.height(); |
|
612 | var h0 = img.height(); | |
587 | var w0 = img.width(); |
|
613 | var w0 = img.width(); | |
588 | if (!(h0 && w0)) { |
|
614 | if (!(h0 && w0)) { | |
589 | // zero size, don't make it resizable |
|
615 | // zero size, don't make it resizable | |
590 | return; |
|
616 | return; | |
591 | } |
|
617 | } | |
592 | img.resizable({ |
|
618 | img.resizable({ | |
593 | aspectRatio: true, |
|
619 | aspectRatio: true, | |
594 | autoHide: true |
|
620 | autoHide: true | |
595 | }); |
|
621 | }); | |
596 | img.dblclick(function () { |
|
622 | img.dblclick(function () { | |
597 | // resize wrapper & image together for some reason: |
|
623 | // resize wrapper & image together for some reason: | |
598 | img.parent().height(h0); |
|
|||
599 | img.height(h0); |
|
624 | img.height(h0); | |
600 | img.parent().width(w0); |
|
|||
601 | img.width(w0); |
|
625 | img.width(w0); | |
|
626 | if (resize_parent === undefined || resize_parent) { | |||
|
627 | img.parent().height(h0); | |||
|
628 | img.parent().width(w0); | |||
|
629 | } | |||
602 | }); |
|
630 | }); | |
603 |
} |
|
631 | }; | |
|
632 | ||||
|
633 | if (immediately) { | |||
|
634 | callback(); | |||
|
635 | } else { | |||
|
636 | img.on("load", callback); | |||
|
637 | } | |||
604 | }; |
|
638 | }; | |
605 |
|
639 | |||
606 | var set_width_height = function (img, md, mime) { |
|
640 | var set_width_height = function (img, md, mime) { | |
607 | // set width and height of an img element from metadata |
|
641 | // set width and height of an img element from metadata | |
608 | var height = _get_metadata_key(md, 'height', mime); |
|
642 | var height = _get_metadata_key(md, 'height', mime); | |
609 | if (height !== undefined) img.attr('height', height); |
|
643 | if (height !== undefined) img.attr('height', height); | |
610 | var width = _get_metadata_key(md, 'width', mime); |
|
644 | var width = _get_metadata_key(md, 'width', mime); | |
611 | if (width !== undefined) img.attr('width', width); |
|
645 | if (width !== undefined) img.attr('width', width); | |
612 | }; |
|
646 | }; | |
613 |
|
647 | |||
614 | var append_png = function (png, md, element) { |
|
648 | var append_png = function (png, md, element) { | |
615 | var type = 'image/png'; |
|
649 | var type = 'image/png'; | |
616 | var toinsert = this.create_output_subarea(md, "output_png", type); |
|
650 | var toinsert = this.create_output_subarea(md, "output_png", type); | |
617 | var img = $("<img/>").attr('src','data:image/png;base64,'+png); |
|
651 | var img = $("<img/>").attr('src','data:image/png;base64,'+png); | |
618 | set_width_height(img, md, 'image/png'); |
|
652 | set_width_height(img, md, 'image/png'); | |
619 | this._dblclick_to_reset_size(img); |
|
653 | this._dblclick_to_reset_size(img); | |
620 | toinsert.append(img); |
|
654 | toinsert.append(img); | |
621 | element.append(toinsert); |
|
655 | element.append(toinsert); | |
622 | return toinsert; |
|
656 | return toinsert; | |
623 | }; |
|
657 | }; | |
624 |
|
658 | |||
625 |
|
659 | |||
626 | var append_jpeg = function (jpeg, md, element) { |
|
660 | var append_jpeg = function (jpeg, md, element) { | |
627 | var type = 'image/jpeg'; |
|
661 | var type = 'image/jpeg'; | |
628 | var toinsert = this.create_output_subarea(md, "output_jpeg", type); |
|
662 | var toinsert = this.create_output_subarea(md, "output_jpeg", type); | |
629 | var img = $("<img/>").attr('src','data:image/jpeg;base64,'+jpeg); |
|
663 | var img = $("<img/>").attr('src','data:image/jpeg;base64,'+jpeg); | |
630 | set_width_height(img, md, 'image/jpeg'); |
|
664 | set_width_height(img, md, 'image/jpeg'); | |
631 | this._dblclick_to_reset_size(img); |
|
665 | this._dblclick_to_reset_size(img); | |
632 | toinsert.append(img); |
|
666 | toinsert.append(img); | |
633 | element.append(toinsert); |
|
667 | element.append(toinsert); | |
634 | return toinsert; |
|
668 | return toinsert; | |
635 | }; |
|
669 | }; | |
636 |
|
670 | |||
637 |
|
671 | |||
638 | var append_pdf = function (pdf, md, element) { |
|
672 | var append_pdf = function (pdf, md, element) { | |
639 | var type = 'application/pdf'; |
|
673 | var type = 'application/pdf'; | |
640 | var toinsert = this.create_output_subarea(md, "output_pdf", type); |
|
674 | var toinsert = this.create_output_subarea(md, "output_pdf", type); | |
641 | var a = $('<a/>').attr('href', 'data:application/pdf;base64,'+pdf); |
|
675 | var a = $('<a/>').attr('href', 'data:application/pdf;base64,'+pdf); | |
642 | a.attr('target', '_blank'); |
|
676 | a.attr('target', '_blank'); | |
643 | a.text('View PDF') |
|
677 | a.text('View PDF') | |
644 | toinsert.append(a); |
|
678 | toinsert.append(a); | |
645 | element.append(toinsert); |
|
679 | element.append(toinsert); | |
646 | return toinsert; |
|
680 | return toinsert; | |
647 | } |
|
681 | } | |
648 |
|
682 | |||
649 | var append_latex = function (latex, md, element) { |
|
683 | var append_latex = function (latex, md, element) { | |
650 | // This method cannot do the typesetting because the latex first has to |
|
684 | // This method cannot do the typesetting because the latex first has to | |
651 | // be on the page. |
|
685 | // be on the page. | |
652 | var type = 'text/latex'; |
|
686 | var type = 'text/latex'; | |
653 | var toinsert = this.create_output_subarea(md, "output_latex", type); |
|
687 | var toinsert = this.create_output_subarea(md, "output_latex", type); | |
654 | toinsert.append(latex); |
|
688 | toinsert.append(latex); | |
655 | element.append(toinsert); |
|
689 | element.append(toinsert); | |
656 | return toinsert; |
|
690 | return toinsert; | |
657 | }; |
|
691 | }; | |
658 |
|
692 | |||
659 |
|
693 | |||
660 | OutputArea.prototype.append_raw_input = function (msg) { |
|
694 | OutputArea.prototype.append_raw_input = function (msg) { | |
661 | var that = this; |
|
695 | var that = this; | |
662 | this.expand(); |
|
696 | this.expand(); | |
663 | var content = msg.content; |
|
697 | var content = msg.content; | |
664 | var area = this.create_output_area(); |
|
698 | var area = this.create_output_area(); | |
665 |
|
699 | |||
666 | // disable any other raw_inputs, if they are left around |
|
700 | // disable any other raw_inputs, if they are left around | |
667 | $("div.output_subarea.raw_input_container").remove(); |
|
701 | $("div.output_subarea.raw_input_container").remove(); | |
668 |
|
702 | |||
669 | area.append( |
|
703 | area.append( | |
670 | $("<div/>") |
|
704 | $("<div/>") | |
671 | .addClass("box-flex1 output_subarea raw_input_container") |
|
705 | .addClass("box-flex1 output_subarea raw_input_container") | |
672 | .append( |
|
706 | .append( | |
673 | $("<span/>") |
|
707 | $("<span/>") | |
674 | .addClass("raw_input_prompt") |
|
708 | .addClass("raw_input_prompt") | |
675 | .text(content.prompt) |
|
709 | .text(content.prompt) | |
676 | ) |
|
710 | ) | |
677 | .append( |
|
711 | .append( | |
678 | $("<input/>") |
|
712 | $("<input/>") | |
679 | .addClass("raw_input") |
|
713 | .addClass("raw_input") | |
680 | .attr('type', 'text') |
|
714 | .attr('type', 'text') | |
681 | .attr("size", 47) |
|
715 | .attr("size", 47) | |
682 | .keydown(function (event, ui) { |
|
716 | .keydown(function (event, ui) { | |
683 | // make sure we submit on enter, |
|
717 | // make sure we submit on enter, | |
684 | // and don't re-execute the *cell* on shift-enter |
|
718 | // and don't re-execute the *cell* on shift-enter | |
685 | if (event.which === IPython.keyboard.keycodes.enter) { |
|
719 | if (event.which === IPython.keyboard.keycodes.enter) { | |
686 | that._submit_raw_input(); |
|
720 | that._submit_raw_input(); | |
687 | return false; |
|
721 | return false; | |
688 | } |
|
722 | } | |
689 | }) |
|
723 | }) | |
690 | ) |
|
724 | ) | |
691 | ); |
|
725 | ); | |
692 |
|
726 | |||
693 | this.element.append(area); |
|
727 | this.element.append(area); | |
694 | var raw_input = area.find('input.raw_input'); |
|
728 | var raw_input = area.find('input.raw_input'); | |
695 | // Register events that enable/disable the keyboard manager while raw |
|
729 | // Register events that enable/disable the keyboard manager while raw | |
696 | // input is focused. |
|
730 | // input is focused. | |
697 | IPython.keyboard_manager.register_events(raw_input); |
|
731 | IPython.keyboard_manager.register_events(raw_input); | |
698 | // Note, the following line used to read raw_input.focus().focus(). |
|
732 | // Note, the following line used to read raw_input.focus().focus(). | |
699 | // This seemed to be needed otherwise only the cell would be focused. |
|
733 | // This seemed to be needed otherwise only the cell would be focused. | |
700 | // But with the modal UI, this seems to work fine with one call to focus(). |
|
734 | // But with the modal UI, this seems to work fine with one call to focus(). | |
701 | raw_input.focus(); |
|
735 | raw_input.focus(); | |
702 | } |
|
736 | } | |
703 |
|
737 | |||
704 | OutputArea.prototype._submit_raw_input = function (evt) { |
|
738 | OutputArea.prototype._submit_raw_input = function (evt) { | |
705 | var container = this.element.find("div.raw_input_container"); |
|
739 | var container = this.element.find("div.raw_input_container"); | |
706 | var theprompt = container.find("span.raw_input_prompt"); |
|
740 | var theprompt = container.find("span.raw_input_prompt"); | |
707 | var theinput = container.find("input.raw_input"); |
|
741 | var theinput = container.find("input.raw_input"); | |
708 | var value = theinput.val(); |
|
742 | var value = theinput.val(); | |
709 | var content = { |
|
743 | var content = { | |
710 | output_type : 'stream', |
|
744 | output_type : 'stream', | |
711 | name : 'stdout', |
|
745 | name : 'stdout', | |
712 | text : theprompt.text() + value + '\n' |
|
746 | text : theprompt.text() + value + '\n' | |
713 | } |
|
747 | } | |
714 | // remove form container |
|
748 | // remove form container | |
715 | container.parent().remove(); |
|
749 | container.parent().remove(); | |
716 | // replace with plaintext version in stdout |
|
750 | // replace with plaintext version in stdout | |
717 | this.append_output(content, false); |
|
751 | this.append_output(content, false); | |
718 | $([IPython.events]).trigger('send_input_reply.Kernel', value); |
|
752 | $([IPython.events]).trigger('send_input_reply.Kernel', value); | |
719 | } |
|
753 | } | |
720 |
|
754 | |||
721 |
|
755 | |||
722 | OutputArea.prototype.handle_clear_output = function (msg) { |
|
756 | OutputArea.prototype.handle_clear_output = function (msg) { | |
723 | // msg spec v4 had stdout, stderr, display keys |
|
757 | // msg spec v4 had stdout, stderr, display keys | |
724 | // v4.1 replaced these with just wait |
|
758 | // v4.1 replaced these with just wait | |
725 | // The default behavior is the same (stdout=stderr=display=True, wait=False), |
|
759 | // The default behavior is the same (stdout=stderr=display=True, wait=False), | |
726 | // so v4 messages will still be properly handled, |
|
760 | // so v4 messages will still be properly handled, | |
727 | // except for the rarely used clearing less than all output. |
|
761 | // except for the rarely used clearing less than all output. | |
728 | this.clear_output(msg.content.wait || false); |
|
762 | this.clear_output(msg.content.wait || false); | |
729 | }; |
|
763 | }; | |
730 |
|
764 | |||
731 |
|
765 | |||
732 | OutputArea.prototype.clear_output = function(wait) { |
|
766 | OutputArea.prototype.clear_output = function(wait) { | |
733 | if (wait) { |
|
767 | if (wait) { | |
734 |
|
768 | |||
735 | // If a clear is queued, clear before adding another to the queue. |
|
769 | // If a clear is queued, clear before adding another to the queue. | |
736 | if (this.clear_queued) { |
|
770 | if (this.clear_queued) { | |
737 | this.clear_output(false); |
|
771 | this.clear_output(false); | |
738 | }; |
|
772 | }; | |
739 |
|
773 | |||
740 | this.clear_queued = true; |
|
774 | this.clear_queued = true; | |
741 | } else { |
|
775 | } else { | |
742 |
|
776 | |||
743 | // Fix the output div's height if the clear_output is waiting for |
|
777 | // Fix the output div's height if the clear_output is waiting for | |
744 | // new output (it is being used in an animation). |
|
778 | // new output (it is being used in an animation). | |
745 | if (this.clear_queued) { |
|
779 | if (this.clear_queued) { | |
746 | var height = this.element.height(); |
|
780 | var height = this.element.height(); | |
747 | this.element.height(height); |
|
781 | this.element.height(height); | |
748 | this.clear_queued = false; |
|
782 | this.clear_queued = false; | |
749 | } |
|
783 | } | |
750 |
|
784 | |||
751 | // clear all, no need for logic |
|
785 | // clear all, no need for logic | |
752 | this.element.html(""); |
|
786 | this.element.html(""); | |
753 | this.outputs = []; |
|
787 | this.outputs = []; | |
754 | this.trusted = true; |
|
788 | this.trusted = true; | |
755 | this.unscroll_area(); |
|
789 | this.unscroll_area(); | |
756 | return; |
|
790 | return; | |
757 | }; |
|
791 | }; | |
758 | }; |
|
792 | }; | |
759 |
|
793 | |||
760 |
|
794 | |||
761 | // JSON serialization |
|
795 | // JSON serialization | |
762 |
|
796 | |||
763 | OutputArea.prototype.fromJSON = function (outputs) { |
|
797 | OutputArea.prototype.fromJSON = function (outputs) { | |
764 | var len = outputs.length; |
|
798 | var len = outputs.length; | |
765 | var data; |
|
799 | var data; | |
766 |
|
800 | |||
767 | for (var i=0; i<len; i++) { |
|
801 | for (var i=0; i<len; i++) { | |
768 | data = outputs[i]; |
|
802 | data = outputs[i]; | |
769 | var msg_type = data.output_type; |
|
803 | var msg_type = data.output_type; | |
770 | if (msg_type === "display_data" || msg_type === "pyout") { |
|
804 | if (msg_type === "display_data" || msg_type === "pyout") { | |
771 | // convert short keys to mime keys |
|
805 | // convert short keys to mime keys | |
772 | // TODO: remove mapping of short keys when we update to nbformat 4 |
|
806 | // TODO: remove mapping of short keys when we update to nbformat 4 | |
773 | data = this.rename_keys(data, OutputArea.mime_map_r); |
|
807 | data = this.rename_keys(data, OutputArea.mime_map_r); | |
774 | data.metadata = this.rename_keys(data.metadata, OutputArea.mime_map_r); |
|
808 | data.metadata = this.rename_keys(data.metadata, OutputArea.mime_map_r); | |
775 | } |
|
809 | } | |
776 |
|
810 | |||
777 | this.append_output(data); |
|
811 | this.append_output(data); | |
778 | } |
|
812 | } | |
779 | }; |
|
813 | }; | |
780 |
|
814 | |||
781 |
|
815 | |||
782 | OutputArea.prototype.toJSON = function () { |
|
816 | OutputArea.prototype.toJSON = function () { | |
783 | var outputs = []; |
|
817 | var outputs = []; | |
784 | var len = this.outputs.length; |
|
818 | var len = this.outputs.length; | |
785 | var data; |
|
819 | var data; | |
786 | for (var i=0; i<len; i++) { |
|
820 | for (var i=0; i<len; i++) { | |
787 | data = this.outputs[i]; |
|
821 | data = this.outputs[i]; | |
788 | var msg_type = data.output_type; |
|
822 | var msg_type = data.output_type; | |
789 | if (msg_type === "display_data" || msg_type === "pyout") { |
|
823 | if (msg_type === "display_data" || msg_type === "pyout") { | |
790 | // convert mime keys to short keys |
|
824 | // convert mime keys to short keys | |
791 | data = this.rename_keys(data, OutputArea.mime_map); |
|
825 | data = this.rename_keys(data, OutputArea.mime_map); | |
792 | data.metadata = this.rename_keys(data.metadata, OutputArea.mime_map); |
|
826 | data.metadata = this.rename_keys(data.metadata, OutputArea.mime_map); | |
793 | } |
|
827 | } | |
794 | outputs[i] = data; |
|
828 | outputs[i] = data; | |
795 | } |
|
829 | } | |
796 | return outputs; |
|
830 | return outputs; | |
797 | }; |
|
831 | }; | |
798 |
|
832 | |||
799 | /** |
|
833 | /** | |
800 | * Class properties |
|
834 | * Class properties | |
801 | **/ |
|
835 | **/ | |
802 |
|
836 | |||
803 | /** |
|
837 | /** | |
804 | * Threshold to trigger autoscroll when the OutputArea is resized, |
|
838 | * Threshold to trigger autoscroll when the OutputArea is resized, | |
805 | * typically when new outputs are added. |
|
839 | * typically when new outputs are added. | |
806 | * |
|
840 | * | |
807 | * Behavior is undefined if autoscroll is lower than minimum_scroll_threshold, |
|
841 | * Behavior is undefined if autoscroll is lower than minimum_scroll_threshold, | |
808 | * unless it is < 0, in which case autoscroll will never be triggered |
|
842 | * unless it is < 0, in which case autoscroll will never be triggered | |
809 | * |
|
843 | * | |
810 | * @property auto_scroll_threshold |
|
844 | * @property auto_scroll_threshold | |
811 | * @type Number |
|
845 | * @type Number | |
812 | * @default 100 |
|
846 | * @default 100 | |
813 | * |
|
847 | * | |
814 | **/ |
|
848 | **/ | |
815 | OutputArea.auto_scroll_threshold = 100; |
|
849 | OutputArea.auto_scroll_threshold = 100; | |
816 |
|
850 | |||
817 | /** |
|
851 | /** | |
818 | * Lower limit (in lines) for OutputArea to be made scrollable. OutputAreas |
|
852 | * Lower limit (in lines) for OutputArea to be made scrollable. OutputAreas | |
819 | * shorter than this are never scrolled. |
|
853 | * shorter than this are never scrolled. | |
820 | * |
|
854 | * | |
821 | * @property minimum_scroll_threshold |
|
855 | * @property minimum_scroll_threshold | |
822 | * @type Number |
|
856 | * @type Number | |
823 | * @default 20 |
|
857 | * @default 20 | |
824 | * |
|
858 | * | |
825 | **/ |
|
859 | **/ | |
826 | OutputArea.minimum_scroll_threshold = 20; |
|
860 | OutputArea.minimum_scroll_threshold = 20; | |
827 |
|
861 | |||
828 |
|
862 | |||
829 |
|
863 | |||
830 | OutputArea.mime_map = { |
|
864 | OutputArea.mime_map = { | |
831 | "text/plain" : "text", |
|
865 | "text/plain" : "text", | |
832 | "text/html" : "html", |
|
866 | "text/html" : "html", | |
833 | "image/svg+xml" : "svg", |
|
867 | "image/svg+xml" : "svg", | |
834 | "image/png" : "png", |
|
868 | "image/png" : "png", | |
835 | "image/jpeg" : "jpeg", |
|
869 | "image/jpeg" : "jpeg", | |
836 | "text/latex" : "latex", |
|
870 | "text/latex" : "latex", | |
837 | "application/json" : "json", |
|
871 | "application/json" : "json", | |
838 | "application/javascript" : "javascript", |
|
872 | "application/javascript" : "javascript", | |
839 | }; |
|
873 | }; | |
840 |
|
874 | |||
841 | OutputArea.mime_map_r = { |
|
875 | OutputArea.mime_map_r = { | |
842 | "text" : "text/plain", |
|
876 | "text" : "text/plain", | |
843 | "html" : "text/html", |
|
877 | "html" : "text/html", | |
844 | "svg" : "image/svg+xml", |
|
878 | "svg" : "image/svg+xml", | |
845 | "png" : "image/png", |
|
879 | "png" : "image/png", | |
846 | "jpeg" : "image/jpeg", |
|
880 | "jpeg" : "image/jpeg", | |
847 | "latex" : "text/latex", |
|
881 | "latex" : "text/latex", | |
848 | "json" : "application/json", |
|
882 | "json" : "application/json", | |
849 | "javascript" : "application/javascript", |
|
883 | "javascript" : "application/javascript", | |
850 | }; |
|
884 | }; | |
851 |
|
885 | |||
852 | OutputArea.display_order = [ |
|
886 | OutputArea.display_order = [ | |
853 | 'application/javascript', |
|
887 | 'application/javascript', | |
854 | 'text/html', |
|
888 | 'text/html', | |
855 | 'text/latex', |
|
889 | 'text/latex', | |
856 | 'image/svg+xml', |
|
890 | 'image/svg+xml', | |
857 | 'image/png', |
|
891 | 'image/png', | |
858 | 'image/jpeg', |
|
892 | 'image/jpeg', | |
859 | 'application/pdf', |
|
893 | 'application/pdf', | |
860 | 'text/plain' |
|
894 | 'text/plain' | |
861 | ]; |
|
895 | ]; | |
862 |
|
896 | |||
863 | OutputArea.append_map = { |
|
897 | OutputArea.append_map = { | |
864 | "text/plain" : append_text, |
|
898 | "text/plain" : append_text, | |
865 | "text/html" : append_html, |
|
899 | "text/html" : append_html, | |
866 | "image/svg+xml" : append_svg, |
|
900 | "image/svg+xml" : append_svg, | |
867 | "image/png" : append_png, |
|
901 | "image/png" : append_png, | |
868 | "image/jpeg" : append_jpeg, |
|
902 | "image/jpeg" : append_jpeg, | |
869 | "text/latex" : append_latex, |
|
903 | "text/latex" : append_latex, | |
870 | "application/javascript" : append_javascript, |
|
904 | "application/javascript" : append_javascript, | |
871 | "application/pdf" : append_pdf |
|
905 | "application/pdf" : append_pdf | |
872 | }; |
|
906 | }; | |
873 |
|
907 | |||
874 | IPython.OutputArea = OutputArea; |
|
908 | IPython.OutputArea = OutputArea; | |
875 |
|
909 | |||
876 | return IPython; |
|
910 | return IPython; | |
877 |
|
911 | |||
878 | }(IPython)); |
|
912 | }(IPython)); |
General Comments 0
You need to be logged in to leave comments.
Login now