Show More
@@ -1,868 +1,874 b'' | |||||
1 | //---------------------------------------------------------------------------- |
|
1 | //---------------------------------------------------------------------------- | |
2 | // Copyright (C) 2008 The IPython Development Team |
|
2 | // Copyright (C) 2008 The IPython Development Team | |
3 | // |
|
3 | // | |
4 | // Distributed under the terms of the BSD License. The full license is in |
|
4 | // Distributed under the terms of the BSD License. The full license is in | |
5 | // the file COPYING, distributed as part of this software. |
|
5 | // the file COPYING, distributed as part of this software. | |
6 | //---------------------------------------------------------------------------- |
|
6 | //---------------------------------------------------------------------------- | |
7 |
|
7 | |||
8 | //============================================================================ |
|
8 | //============================================================================ | |
9 | // OutputArea |
|
9 | // OutputArea | |
10 | //============================================================================ |
|
10 | //============================================================================ | |
11 |
|
11 | |||
12 | /** |
|
12 | /** | |
13 | * @module IPython |
|
13 | * @module IPython | |
14 | * @namespace IPython |
|
14 | * @namespace IPython | |
15 | * @submodule OutputArea |
|
15 | * @submodule OutputArea | |
16 | */ |
|
16 | */ | |
17 | var IPython = (function (IPython) { |
|
17 | var IPython = (function (IPython) { | |
18 | "use strict"; |
|
18 | "use strict"; | |
19 |
|
19 | |||
20 | var utils = IPython.utils; |
|
20 | var utils = IPython.utils; | |
21 |
|
21 | |||
22 | /** |
|
22 | /** | |
23 | * @class OutputArea |
|
23 | * @class OutputArea | |
24 | * |
|
24 | * | |
25 | * @constructor |
|
25 | * @constructor | |
26 | */ |
|
26 | */ | |
27 |
|
27 | |||
28 | var OutputArea = function (selector, prompt_area) { |
|
28 | var OutputArea = function (selector, prompt_area) { | |
29 | this.selector = selector; |
|
29 | this.selector = selector; | |
30 | this.wrapper = $(selector); |
|
30 | this.wrapper = $(selector); | |
31 | this.outputs = []; |
|
31 | this.outputs = []; | |
32 | this.collapsed = false; |
|
32 | this.collapsed = false; | |
33 | this.scrolled = false; |
|
33 | this.scrolled = false; | |
34 | this.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 | this.append_text(s, {}, toinsert).addClass('output_pyerr'); |
|
428 | var append_text = OutputArea.append_map['text/plain']; | |
|
429 | if (append_text) { | |||
|
430 | append_text.apply(this, [s, {}, toinsert]).addClass('output_pyerr'); | |||
|
431 | } | |||
429 | this._safe_append(toinsert); |
|
432 | this._safe_append(toinsert); | |
430 | } |
|
433 | } | |
431 | }; |
|
434 | }; | |
432 |
|
435 | |||
433 |
|
436 | |||
434 | OutputArea.prototype.append_stream = function (json) { |
|
437 | OutputArea.prototype.append_stream = function (json) { | |
435 | // 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), | |
436 | // default to most likely stdout: |
|
439 | // default to most likely stdout: | |
437 | if (json.stream == undefined){ |
|
440 | if (json.stream === undefined){ | |
438 | json.stream = 'stdout'; |
|
441 | json.stream = 'stdout'; | |
439 | } |
|
442 | } | |
440 | var text = json.text; |
|
443 | var text = json.text; | |
441 | var subclass = "output_"+json.stream; |
|
444 | var subclass = "output_"+json.stream; | |
442 | if (this.outputs.length > 0){ |
|
445 | if (this.outputs.length > 0){ | |
443 | // have at least one output to consider |
|
446 | // have at least one output to consider | |
444 | var last = this.outputs[this.outputs.length-1]; |
|
447 | var last = this.outputs[this.outputs.length-1]; | |
445 | if (last.output_type == 'stream' && json.stream == last.stream){ |
|
448 | if (last.output_type == 'stream' && json.stream == last.stream){ | |
446 | // latest output was in the same stream, |
|
449 | // latest output was in the same stream, | |
447 | // so append directly into its pre tag |
|
450 | // so append directly into its pre tag | |
448 | // escape ANSI & HTML specials: |
|
451 | // escape ANSI & HTML specials: | |
449 | var pre = this.element.find('div.'+subclass).last().find('pre'); |
|
452 | var pre = this.element.find('div.'+subclass).last().find('pre'); | |
450 | var html = utils.fixCarriageReturn( |
|
453 | var html = utils.fixCarriageReturn( | |
451 | pre.html() + utils.fixConsole(text)); |
|
454 | pre.html() + utils.fixConsole(text)); | |
452 | // The only user content injected with this HTML call is |
|
455 | // The only user content injected with this HTML call is | |
453 | // escaped by the fixConsole() method. |
|
456 | // escaped by the fixConsole() method. | |
454 | pre.html(html); |
|
457 | pre.html(html); | |
455 | return; |
|
458 | return; | |
456 | } |
|
459 | } | |
457 | } |
|
460 | } | |
458 |
|
461 | |||
459 | if (!text.replace("\r", "")) { |
|
462 | if (!text.replace("\r", "")) { | |
460 | // text is nothing (empty string, \r, etc.) |
|
463 | // text is nothing (empty string, \r, etc.) | |
461 | // so don't append any elements, which might add undesirable space |
|
464 | // so don't append any elements, which might add undesirable space | |
462 | return; |
|
465 | return; | |
463 | } |
|
466 | } | |
464 |
|
467 | |||
465 | // If we got here, attach a new div |
|
468 | // If we got here, attach a new div | |
466 | var toinsert = this.create_output_area(); |
|
469 | var toinsert = this.create_output_area(); | |
467 | this.append_text(text, {}, toinsert).addClass("output_stream "+subclass); |
|
470 | var append_text = OutputArea.append_map['text/plain']; | |
|
471 | if (append_text) { | |||
|
472 | append_text.apply(this, [text, {}, toinsert]).addClass("output_stream " + subclass); | |||
|
473 | } | |||
468 | this._safe_append(toinsert); |
|
474 | this._safe_append(toinsert); | |
469 | }; |
|
475 | }; | |
470 |
|
476 | |||
471 |
|
477 | |||
472 | OutputArea.prototype.append_display_data = function (json) { |
|
478 | OutputArea.prototype.append_display_data = function (json) { | |
473 | var toinsert = this.create_output_area(); |
|
479 | var toinsert = this.create_output_area(); | |
474 | if (this.append_mime_type(json, toinsert)) { |
|
480 | if (this.append_mime_type(json, toinsert)) { | |
475 | this._safe_append(toinsert); |
|
481 | this._safe_append(toinsert); | |
476 | // If we just output latex, typeset it. |
|
482 | // If we just output latex, typeset it. | |
477 | if ((json['text/latex'] !== undefined) || (json['text/html'] !== undefined)) { |
|
483 | if ((json['text/latex'] !== undefined) || (json['text/html'] !== undefined)) { | |
478 | this.typeset(); |
|
484 | this.typeset(); | |
479 | } |
|
485 | } | |
480 | } |
|
486 | } | |
481 | }; |
|
487 | }; | |
482 |
|
488 | |||
483 |
|
489 | |||
484 | OutputArea.safe_outputs = { |
|
490 | OutputArea.safe_outputs = { | |
485 | 'text/plain' : true, |
|
491 | 'text/plain' : true, | |
486 | 'text/latex' : true, |
|
492 | 'text/latex' : true, | |
487 | 'image/png' : true, |
|
493 | 'image/png' : true, | |
488 | 'image/jpeg' : true |
|
494 | 'image/jpeg' : true | |
489 | }; |
|
495 | }; | |
490 |
|
496 | |||
491 | OutputArea.prototype.append_mime_type = function (json, element) { |
|
497 | OutputArea.prototype.append_mime_type = function (json, element) { | |
492 | for (var type_i in OutputArea.display_order) { |
|
498 | for (var type_i in OutputArea.display_order) { | |
493 | var type = OutputArea.display_order[type_i]; |
|
499 | var type = OutputArea.display_order[type_i]; | |
494 | var append = OutputArea.append_map[type]; |
|
500 | var append = OutputArea.append_map[type]; | |
495 | if ((json[type] !== undefined) && append) { |
|
501 | if ((json[type] !== undefined) && append) { | |
496 | var value = json[type]; |
|
502 | var value = json[type]; | |
497 | if (!this.trusted && !OutputArea.safe_outputs[type]) { |
|
503 | if (!this.trusted && !OutputArea.safe_outputs[type]) { | |
498 | // not trusted, sanitize HTML |
|
504 | // not trusted, sanitize HTML | |
499 | if (type==='text/html' || type==='text/svg') { |
|
505 | if (type==='text/html' || type==='text/svg') { | |
500 | value = IPython.security.sanitize_html(value); |
|
506 | value = IPython.security.sanitize_html(value); | |
501 | } else { |
|
507 | } else { | |
502 | // 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 | |
503 | console.log("Ignoring untrusted " + type + " output."); |
|
509 | console.log("Ignoring untrusted " + type + " output."); | |
504 | continue; |
|
510 | continue; | |
505 | } |
|
511 | } | |
506 | } |
|
512 | } | |
507 | var md = json.metadata || {}; |
|
513 | var md = json.metadata || {}; | |
508 | var toinsert = append.apply(this, [value, md, element]); |
|
514 | var toinsert = append.apply(this, [value, md, element]); | |
509 | $([IPython.events]).trigger('output_appended.OutputArea', [type, value, md, toinsert]); |
|
515 | $([IPython.events]).trigger('output_appended.OutputArea', [type, value, md, toinsert]); | |
510 | return toinsert; |
|
516 | return toinsert; | |
511 | } |
|
517 | } | |
512 | } |
|
518 | } | |
513 | return null; |
|
519 | return null; | |
514 | }; |
|
520 | }; | |
515 |
|
521 | |||
516 |
|
522 | |||
517 | var append_html = function (html, md, element) { |
|
523 | var append_html = function (html, md, element) { | |
518 | var type = 'text/html'; |
|
524 | var type = 'text/html'; | |
519 | 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); | |
520 | IPython.keyboard_manager.register_events(toinsert); |
|
526 | IPython.keyboard_manager.register_events(toinsert); | |
521 | toinsert.append(html); |
|
527 | toinsert.append(html); | |
522 | element.append(toinsert); |
|
528 | element.append(toinsert); | |
523 | return toinsert; |
|
529 | return toinsert; | |
524 | }; |
|
530 | }; | |
525 |
|
531 | |||
526 |
|
532 | |||
527 | var append_javascript = function (js, md, element) { |
|
533 | var append_javascript = function (js, md, element) { | |
528 | // 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. | |
529 | var type = 'application/javascript'; |
|
535 | var type = 'application/javascript'; | |
530 | var toinsert = this.create_output_subarea(md, "output_javascript", type); |
|
536 | var toinsert = this.create_output_subarea(md, "output_javascript", type); | |
531 | IPython.keyboard_manager.register_events(toinsert); |
|
537 | IPython.keyboard_manager.register_events(toinsert); | |
532 | element.append(toinsert); |
|
538 | element.append(toinsert); | |
533 | // FIXME TODO : remove `container element for 3.0` |
|
539 | // FIXME TODO : remove `container element for 3.0` | |
534 | //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. | |
535 | var container = element; |
|
541 | var container = element; | |
536 | container.show = function(){console.log('Warning "container.show()" is deprecated.')}; |
|
542 | container.show = function(){console.log('Warning "container.show()" is deprecated.')}; | |
537 | // end backward compat |
|
543 | // end backward compat | |
538 | try { |
|
544 | try { | |
539 | eval(js); |
|
545 | eval(js); | |
540 | } catch(err) { |
|
546 | } catch(err) { | |
541 | console.log(err); |
|
547 | console.log(err); | |
542 | this._append_javascript_error(err, toinsert); |
|
548 | this._append_javascript_error(err, toinsert); | |
543 | } |
|
549 | } | |
544 | return toinsert; |
|
550 | return toinsert; | |
545 | }; |
|
551 | }; | |
546 |
|
552 | |||
547 |
|
553 | |||
548 | var append_text = function (data, md, element) { |
|
554 | var append_text = function (data, md, element) { | |
549 | var type = 'text/plain'; |
|
555 | var type = 'text/plain'; | |
550 | var toinsert = this.create_output_subarea(md, "output_text", type); |
|
556 | var toinsert = this.create_output_subarea(md, "output_text", type); | |
551 | // escape ANSI & HTML specials in plaintext: |
|
557 | // escape ANSI & HTML specials in plaintext: | |
552 | data = utils.fixConsole(data); |
|
558 | data = utils.fixConsole(data); | |
553 | data = utils.fixCarriageReturn(data); |
|
559 | data = utils.fixCarriageReturn(data); | |
554 | data = utils.autoLinkUrls(data); |
|
560 | data = utils.autoLinkUrls(data); | |
555 | // The only user content injected with this HTML call is |
|
561 | // The only user content injected with this HTML call is | |
556 | // escaped by the fixConsole() method. |
|
562 | // escaped by the fixConsole() method. | |
557 | toinsert.append($("<pre/>").html(data)); |
|
563 | toinsert.append($("<pre/>").html(data)); | |
558 | element.append(toinsert); |
|
564 | element.append(toinsert); | |
559 | return toinsert; |
|
565 | return toinsert; | |
560 | }; |
|
566 | }; | |
561 |
|
567 | |||
562 |
|
568 | |||
563 | var append_svg = function (svg, md, element) { |
|
569 | var append_svg = function (svg, md, element) { | |
564 | var type = 'image/svg+xml'; |
|
570 | var type = 'image/svg+xml'; | |
565 | var toinsert = this.create_output_subarea(md, "output_svg", type); |
|
571 | var toinsert = this.create_output_subarea(md, "output_svg", type); | |
566 | toinsert.append(svg); |
|
572 | toinsert.append(svg); | |
567 | element.append(toinsert); |
|
573 | element.append(toinsert); | |
568 | return toinsert; |
|
574 | return toinsert; | |
569 | }; |
|
575 | }; | |
570 |
|
576 | |||
571 |
|
577 | |||
572 | OutputArea.prototype._dblclick_to_reset_size = function (img) { |
|
578 | OutputArea.prototype._dblclick_to_reset_size = function (img) { | |
573 | // wrap image after it's loaded on the page, |
|
579 | // wrap image after it's loaded on the page, | |
574 | // otherwise the measured initial size will be incorrect |
|
580 | // otherwise the measured initial size will be incorrect | |
575 | img.on("load", function (){ |
|
581 | img.on("load", function (){ | |
576 | var h0 = img.height(); |
|
582 | var h0 = img.height(); | |
577 | var w0 = img.width(); |
|
583 | var w0 = img.width(); | |
578 | if (!(h0 && w0)) { |
|
584 | if (!(h0 && w0)) { | |
579 | // zero size, don't make it resizable |
|
585 | // zero size, don't make it resizable | |
580 | return; |
|
586 | return; | |
581 | } |
|
587 | } | |
582 | img.resizable({ |
|
588 | img.resizable({ | |
583 | aspectRatio: true, |
|
589 | aspectRatio: true, | |
584 | autoHide: true |
|
590 | autoHide: true | |
585 | }); |
|
591 | }); | |
586 | img.dblclick(function () { |
|
592 | img.dblclick(function () { | |
587 | // resize wrapper & image together for some reason: |
|
593 | // resize wrapper & image together for some reason: | |
588 | img.parent().height(h0); |
|
594 | img.parent().height(h0); | |
589 | img.height(h0); |
|
595 | img.height(h0); | |
590 | img.parent().width(w0); |
|
596 | img.parent().width(w0); | |
591 | img.width(w0); |
|
597 | img.width(w0); | |
592 | }); |
|
598 | }); | |
593 | }); |
|
599 | }); | |
594 | }; |
|
600 | }; | |
595 |
|
601 | |||
596 | var set_width_height = function (img, md, mime) { |
|
602 | var set_width_height = function (img, md, mime) { | |
597 | // set width and height of an img element from metadata |
|
603 | // set width and height of an img element from metadata | |
598 | var height = _get_metadata_key(md, 'height', mime); |
|
604 | var height = _get_metadata_key(md, 'height', mime); | |
599 | if (height !== undefined) img.attr('height', height); |
|
605 | if (height !== undefined) img.attr('height', height); | |
600 | var width = _get_metadata_key(md, 'width', mime); |
|
606 | var width = _get_metadata_key(md, 'width', mime); | |
601 | if (width !== undefined) img.attr('width', width); |
|
607 | if (width !== undefined) img.attr('width', width); | |
602 | }; |
|
608 | }; | |
603 |
|
609 | |||
604 | var append_png = function (png, md, element) { |
|
610 | var append_png = function (png, md, element) { | |
605 | var type = 'image/png'; |
|
611 | var type = 'image/png'; | |
606 | var toinsert = this.create_output_subarea(md, "output_png", type); |
|
612 | var toinsert = this.create_output_subarea(md, "output_png", type); | |
607 | var img = $("<img/>").attr('src','data:image/png;base64,'+png); |
|
613 | var img = $("<img/>").attr('src','data:image/png;base64,'+png); | |
608 | set_width_height(img, md, 'image/png'); |
|
614 | set_width_height(img, md, 'image/png'); | |
609 | this._dblclick_to_reset_size(img); |
|
615 | this._dblclick_to_reset_size(img); | |
610 | toinsert.append(img); |
|
616 | toinsert.append(img); | |
611 | element.append(toinsert); |
|
617 | element.append(toinsert); | |
612 | return toinsert; |
|
618 | return toinsert; | |
613 | }; |
|
619 | }; | |
614 |
|
620 | |||
615 |
|
621 | |||
616 | var append_jpeg = function (jpeg, md, element) { |
|
622 | var append_jpeg = function (jpeg, md, element) { | |
617 | var type = 'image/jpeg'; |
|
623 | var type = 'image/jpeg'; | |
618 | var toinsert = this.create_output_subarea(md, "output_jpeg", type); |
|
624 | var toinsert = this.create_output_subarea(md, "output_jpeg", type); | |
619 | var img = $("<img/>").attr('src','data:image/jpeg;base64,'+jpeg); |
|
625 | var img = $("<img/>").attr('src','data:image/jpeg;base64,'+jpeg); | |
620 | set_width_height(img, md, 'image/jpeg'); |
|
626 | set_width_height(img, md, 'image/jpeg'); | |
621 | this._dblclick_to_reset_size(img); |
|
627 | this._dblclick_to_reset_size(img); | |
622 | toinsert.append(img); |
|
628 | toinsert.append(img); | |
623 | element.append(toinsert); |
|
629 | element.append(toinsert); | |
624 | return toinsert; |
|
630 | return toinsert; | |
625 | }; |
|
631 | }; | |
626 |
|
632 | |||
627 |
|
633 | |||
628 | var append_pdf = function (pdf, md, element) { |
|
634 | var append_pdf = function (pdf, md, element) { | |
629 | var type = 'application/pdf'; |
|
635 | var type = 'application/pdf'; | |
630 | var toinsert = this.create_output_subarea(md, "output_pdf", type); |
|
636 | var toinsert = this.create_output_subarea(md, "output_pdf", type); | |
631 | var a = $('<a/>').attr('href', 'data:application/pdf;base64,'+pdf); |
|
637 | var a = $('<a/>').attr('href', 'data:application/pdf;base64,'+pdf); | |
632 | a.attr('target', '_blank'); |
|
638 | a.attr('target', '_blank'); | |
633 | a.text('View PDF') |
|
639 | a.text('View PDF') | |
634 | toinsert.append(a); |
|
640 | toinsert.append(a); | |
635 | element.append(toinsert); |
|
641 | element.append(toinsert); | |
636 | return toinsert; |
|
642 | return toinsert; | |
637 | } |
|
643 | } | |
638 |
|
644 | |||
639 | var append_latex = function (latex, md, element) { |
|
645 | var append_latex = function (latex, md, element) { | |
640 | // This method cannot do the typesetting because the latex first has to |
|
646 | // This method cannot do the typesetting because the latex first has to | |
641 | // be on the page. |
|
647 | // be on the page. | |
642 | var type = 'text/latex'; |
|
648 | var type = 'text/latex'; | |
643 | var toinsert = this.create_output_subarea(md, "output_latex", type); |
|
649 | var toinsert = this.create_output_subarea(md, "output_latex", type); | |
644 | toinsert.append(latex); |
|
650 | toinsert.append(latex); | |
645 | element.append(toinsert); |
|
651 | element.append(toinsert); | |
646 | return toinsert; |
|
652 | return toinsert; | |
647 | }; |
|
653 | }; | |
648 |
|
654 | |||
649 |
|
655 | |||
650 | OutputArea.prototype.append_raw_input = function (msg) { |
|
656 | OutputArea.prototype.append_raw_input = function (msg) { | |
651 | var that = this; |
|
657 | var that = this; | |
652 | this.expand(); |
|
658 | this.expand(); | |
653 | var content = msg.content; |
|
659 | var content = msg.content; | |
654 | var area = this.create_output_area(); |
|
660 | var area = this.create_output_area(); | |
655 |
|
661 | |||
656 | // disable any other raw_inputs, if they are left around |
|
662 | // disable any other raw_inputs, if they are left around | |
657 | $("div.output_subarea.raw_input").remove(); |
|
663 | $("div.output_subarea.raw_input").remove(); | |
658 |
|
664 | |||
659 | area.append( |
|
665 | area.append( | |
660 | $("<div/>") |
|
666 | $("<div/>") | |
661 | .addClass("box-flex1 output_subarea raw_input") |
|
667 | .addClass("box-flex1 output_subarea raw_input") | |
662 | .append( |
|
668 | .append( | |
663 | $("<span/>") |
|
669 | $("<span/>") | |
664 | .addClass("input_prompt") |
|
670 | .addClass("input_prompt") | |
665 | .text(content.prompt) |
|
671 | .text(content.prompt) | |
666 | ) |
|
672 | ) | |
667 | .append( |
|
673 | .append( | |
668 | $("<input/>") |
|
674 | $("<input/>") | |
669 | .addClass("raw_input") |
|
675 | .addClass("raw_input") | |
670 | .attr('type', 'text') |
|
676 | .attr('type', 'text') | |
671 | .attr("size", 47) |
|
677 | .attr("size", 47) | |
672 | .keydown(function (event, ui) { |
|
678 | .keydown(function (event, ui) { | |
673 | // make sure we submit on enter, |
|
679 | // make sure we submit on enter, | |
674 | // and don't re-execute the *cell* on shift-enter |
|
680 | // and don't re-execute the *cell* on shift-enter | |
675 | if (event.which === IPython.keyboard.keycodes.enter) { |
|
681 | if (event.which === IPython.keyboard.keycodes.enter) { | |
676 | that._submit_raw_input(); |
|
682 | that._submit_raw_input(); | |
677 | return false; |
|
683 | return false; | |
678 | } |
|
684 | } | |
679 | }) |
|
685 | }) | |
680 | ) |
|
686 | ) | |
681 | ); |
|
687 | ); | |
682 |
|
688 | |||
683 | this.element.append(area); |
|
689 | this.element.append(area); | |
684 | var raw_input = area.find('input.raw_input'); |
|
690 | var raw_input = area.find('input.raw_input'); | |
685 | // Register events that enable/disable the keyboard manager while raw |
|
691 | // Register events that enable/disable the keyboard manager while raw | |
686 | // input is focused. |
|
692 | // input is focused. | |
687 | IPython.keyboard_manager.register_events(raw_input); |
|
693 | IPython.keyboard_manager.register_events(raw_input); | |
688 | // Note, the following line used to read raw_input.focus().focus(). |
|
694 | // Note, the following line used to read raw_input.focus().focus(). | |
689 | // This seemed to be needed otherwise only the cell would be focused. |
|
695 | // This seemed to be needed otherwise only the cell would be focused. | |
690 | // But with the modal UI, this seems to work fine with one call to focus(). |
|
696 | // But with the modal UI, this seems to work fine with one call to focus(). | |
691 | raw_input.focus(); |
|
697 | raw_input.focus(); | |
692 | } |
|
698 | } | |
693 |
|
699 | |||
694 | OutputArea.prototype._submit_raw_input = function (evt) { |
|
700 | OutputArea.prototype._submit_raw_input = function (evt) { | |
695 | var container = this.element.find("div.raw_input"); |
|
701 | var container = this.element.find("div.raw_input"); | |
696 | var theprompt = container.find("span.input_prompt"); |
|
702 | var theprompt = container.find("span.input_prompt"); | |
697 | var theinput = container.find("input.raw_input"); |
|
703 | var theinput = container.find("input.raw_input"); | |
698 | var value = theinput.val(); |
|
704 | var value = theinput.val(); | |
699 | var content = { |
|
705 | var content = { | |
700 | output_type : 'stream', |
|
706 | output_type : 'stream', | |
701 | name : 'stdout', |
|
707 | name : 'stdout', | |
702 | text : theprompt.text() + value + '\n' |
|
708 | text : theprompt.text() + value + '\n' | |
703 | } |
|
709 | } | |
704 | // remove form container |
|
710 | // remove form container | |
705 | container.parent().remove(); |
|
711 | container.parent().remove(); | |
706 | // replace with plaintext version in stdout |
|
712 | // replace with plaintext version in stdout | |
707 | this.append_output(content, false); |
|
713 | this.append_output(content, false); | |
708 | $([IPython.events]).trigger('send_input_reply.Kernel', value); |
|
714 | $([IPython.events]).trigger('send_input_reply.Kernel', value); | |
709 | } |
|
715 | } | |
710 |
|
716 | |||
711 |
|
717 | |||
712 | OutputArea.prototype.handle_clear_output = function (msg) { |
|
718 | OutputArea.prototype.handle_clear_output = function (msg) { | |
713 | // msg spec v4 had stdout, stderr, display keys |
|
719 | // msg spec v4 had stdout, stderr, display keys | |
714 | // v4.1 replaced these with just wait |
|
720 | // v4.1 replaced these with just wait | |
715 | // The default behavior is the same (stdout=stderr=display=True, wait=False), |
|
721 | // The default behavior is the same (stdout=stderr=display=True, wait=False), | |
716 | // so v4 messages will still be properly handled, |
|
722 | // so v4 messages will still be properly handled, | |
717 | // except for the rarely used clearing less than all output. |
|
723 | // except for the rarely used clearing less than all output. | |
718 | this.clear_output(msg.content.wait || false); |
|
724 | this.clear_output(msg.content.wait || false); | |
719 | }; |
|
725 | }; | |
720 |
|
726 | |||
721 |
|
727 | |||
722 | OutputArea.prototype.clear_output = function(wait) { |
|
728 | OutputArea.prototype.clear_output = function(wait) { | |
723 | if (wait) { |
|
729 | if (wait) { | |
724 |
|
730 | |||
725 | // If a clear is queued, clear before adding another to the queue. |
|
731 | // If a clear is queued, clear before adding another to the queue. | |
726 | if (this.clear_queued) { |
|
732 | if (this.clear_queued) { | |
727 | this.clear_output(false); |
|
733 | this.clear_output(false); | |
728 | }; |
|
734 | }; | |
729 |
|
735 | |||
730 | this.clear_queued = true; |
|
736 | this.clear_queued = true; | |
731 | } else { |
|
737 | } else { | |
732 |
|
738 | |||
733 | // Fix the output div's height if the clear_output is waiting for |
|
739 | // Fix the output div's height if the clear_output is waiting for | |
734 | // new output (it is being used in an animation). |
|
740 | // new output (it is being used in an animation). | |
735 | if (this.clear_queued) { |
|
741 | if (this.clear_queued) { | |
736 | var height = this.element.height(); |
|
742 | var height = this.element.height(); | |
737 | this.element.height(height); |
|
743 | this.element.height(height); | |
738 | this.clear_queued = false; |
|
744 | this.clear_queued = false; | |
739 | } |
|
745 | } | |
740 |
|
746 | |||
741 | // clear all, no need for logic |
|
747 | // clear all, no need for logic | |
742 | this.element.html(""); |
|
748 | this.element.html(""); | |
743 | this.outputs = []; |
|
749 | this.outputs = []; | |
744 | this.trusted = true; |
|
750 | this.trusted = true; | |
745 | this.unscroll_area(); |
|
751 | this.unscroll_area(); | |
746 | return; |
|
752 | return; | |
747 | }; |
|
753 | }; | |
748 | }; |
|
754 | }; | |
749 |
|
755 | |||
750 |
|
756 | |||
751 | // JSON serialization |
|
757 | // JSON serialization | |
752 |
|
758 | |||
753 | OutputArea.prototype.fromJSON = function (outputs) { |
|
759 | OutputArea.prototype.fromJSON = function (outputs) { | |
754 | var len = outputs.length; |
|
760 | var len = outputs.length; | |
755 | var data; |
|
761 | var data; | |
756 |
|
762 | |||
757 | for (var i=0; i<len; i++) { |
|
763 | for (var i=0; i<len; i++) { | |
758 | data = outputs[i]; |
|
764 | data = outputs[i]; | |
759 | var msg_type = data.output_type; |
|
765 | var msg_type = data.output_type; | |
760 | if (msg_type === "display_data" || msg_type === "pyout") { |
|
766 | if (msg_type === "display_data" || msg_type === "pyout") { | |
761 | // convert short keys to mime keys |
|
767 | // convert short keys to mime keys | |
762 | // TODO: remove mapping of short keys when we update to nbformat 4 |
|
768 | // TODO: remove mapping of short keys when we update to nbformat 4 | |
763 | data = this.rename_keys(data, OutputArea.mime_map_r); |
|
769 | data = this.rename_keys(data, OutputArea.mime_map_r); | |
764 | data.metadata = this.rename_keys(data.metadata, OutputArea.mime_map_r); |
|
770 | data.metadata = this.rename_keys(data.metadata, OutputArea.mime_map_r); | |
765 | } |
|
771 | } | |
766 |
|
772 | |||
767 | this.append_output(data); |
|
773 | this.append_output(data); | |
768 | } |
|
774 | } | |
769 | }; |
|
775 | }; | |
770 |
|
776 | |||
771 |
|
777 | |||
772 | OutputArea.prototype.toJSON = function () { |
|
778 | OutputArea.prototype.toJSON = function () { | |
773 | var outputs = []; |
|
779 | var outputs = []; | |
774 | var len = this.outputs.length; |
|
780 | var len = this.outputs.length; | |
775 | var data; |
|
781 | var data; | |
776 | for (var i=0; i<len; i++) { |
|
782 | for (var i=0; i<len; i++) { | |
777 | data = this.outputs[i]; |
|
783 | data = this.outputs[i]; | |
778 | var msg_type = data.output_type; |
|
784 | var msg_type = data.output_type; | |
779 | if (msg_type === "display_data" || msg_type === "pyout") { |
|
785 | if (msg_type === "display_data" || msg_type === "pyout") { | |
780 | // convert mime keys to short keys |
|
786 | // convert mime keys to short keys | |
781 | data = this.rename_keys(data, OutputArea.mime_map); |
|
787 | data = this.rename_keys(data, OutputArea.mime_map); | |
782 | data.metadata = this.rename_keys(data.metadata, OutputArea.mime_map); |
|
788 | data.metadata = this.rename_keys(data.metadata, OutputArea.mime_map); | |
783 | } |
|
789 | } | |
784 | outputs[i] = data; |
|
790 | outputs[i] = data; | |
785 | } |
|
791 | } | |
786 | return outputs; |
|
792 | return outputs; | |
787 | }; |
|
793 | }; | |
788 |
|
794 | |||
789 | /** |
|
795 | /** | |
790 | * Class properties |
|
796 | * Class properties | |
791 | **/ |
|
797 | **/ | |
792 |
|
798 | |||
793 | /** |
|
799 | /** | |
794 | * Threshold to trigger autoscroll when the OutputArea is resized, |
|
800 | * Threshold to trigger autoscroll when the OutputArea is resized, | |
795 | * typically when new outputs are added. |
|
801 | * typically when new outputs are added. | |
796 | * |
|
802 | * | |
797 | * Behavior is undefined if autoscroll is lower than minimum_scroll_threshold, |
|
803 | * Behavior is undefined if autoscroll is lower than minimum_scroll_threshold, | |
798 | * unless it is < 0, in which case autoscroll will never be triggered |
|
804 | * unless it is < 0, in which case autoscroll will never be triggered | |
799 | * |
|
805 | * | |
800 | * @property auto_scroll_threshold |
|
806 | * @property auto_scroll_threshold | |
801 | * @type Number |
|
807 | * @type Number | |
802 | * @default 100 |
|
808 | * @default 100 | |
803 | * |
|
809 | * | |
804 | **/ |
|
810 | **/ | |
805 | OutputArea.auto_scroll_threshold = 100; |
|
811 | OutputArea.auto_scroll_threshold = 100; | |
806 |
|
812 | |||
807 | /** |
|
813 | /** | |
808 | * Lower limit (in lines) for OutputArea to be made scrollable. OutputAreas |
|
814 | * Lower limit (in lines) for OutputArea to be made scrollable. OutputAreas | |
809 | * shorter than this are never scrolled. |
|
815 | * shorter than this are never scrolled. | |
810 | * |
|
816 | * | |
811 | * @property minimum_scroll_threshold |
|
817 | * @property minimum_scroll_threshold | |
812 | * @type Number |
|
818 | * @type Number | |
813 | * @default 20 |
|
819 | * @default 20 | |
814 | * |
|
820 | * | |
815 | **/ |
|
821 | **/ | |
816 | OutputArea.minimum_scroll_threshold = 20; |
|
822 | OutputArea.minimum_scroll_threshold = 20; | |
817 |
|
823 | |||
818 |
|
824 | |||
819 |
|
825 | |||
820 | OutputArea.mime_map = { |
|
826 | OutputArea.mime_map = { | |
821 | "text/plain" : "text", |
|
827 | "text/plain" : "text", | |
822 | "text/html" : "html", |
|
828 | "text/html" : "html", | |
823 | "image/svg+xml" : "svg", |
|
829 | "image/svg+xml" : "svg", | |
824 | "image/png" : "png", |
|
830 | "image/png" : "png", | |
825 | "image/jpeg" : "jpeg", |
|
831 | "image/jpeg" : "jpeg", | |
826 | "text/latex" : "latex", |
|
832 | "text/latex" : "latex", | |
827 | "application/json" : "json", |
|
833 | "application/json" : "json", | |
828 | "application/javascript" : "javascript", |
|
834 | "application/javascript" : "javascript", | |
829 | }; |
|
835 | }; | |
830 |
|
836 | |||
831 | OutputArea.mime_map_r = { |
|
837 | OutputArea.mime_map_r = { | |
832 | "text" : "text/plain", |
|
838 | "text" : "text/plain", | |
833 | "html" : "text/html", |
|
839 | "html" : "text/html", | |
834 | "svg" : "image/svg+xml", |
|
840 | "svg" : "image/svg+xml", | |
835 | "png" : "image/png", |
|
841 | "png" : "image/png", | |
836 | "jpeg" : "image/jpeg", |
|
842 | "jpeg" : "image/jpeg", | |
837 | "latex" : "text/latex", |
|
843 | "latex" : "text/latex", | |
838 | "json" : "application/json", |
|
844 | "json" : "application/json", | |
839 | "javascript" : "application/javascript", |
|
845 | "javascript" : "application/javascript", | |
840 | }; |
|
846 | }; | |
841 |
|
847 | |||
842 | OutputArea.display_order = [ |
|
848 | OutputArea.display_order = [ | |
843 | 'application/javascript', |
|
849 | 'application/javascript', | |
844 | 'text/html', |
|
850 | 'text/html', | |
845 | 'text/latex', |
|
851 | 'text/latex', | |
846 | 'image/svg+xml', |
|
852 | 'image/svg+xml', | |
847 | 'image/png', |
|
853 | 'image/png', | |
848 | 'image/jpeg', |
|
854 | 'image/jpeg', | |
849 | 'application/pdf', |
|
855 | 'application/pdf', | |
850 | 'text/plain' |
|
856 | 'text/plain' | |
851 | ]; |
|
857 | ]; | |
852 |
|
858 | |||
853 | OutputArea.append_map = { |
|
859 | OutputArea.append_map = { | |
854 | "text/plain" : append_text, |
|
860 | "text/plain" : append_text, | |
855 | "text/html" : append_html, |
|
861 | "text/html" : append_html, | |
856 | "image/svg+xml" : append_svg, |
|
862 | "image/svg+xml" : append_svg, | |
857 | "image/png" : append_png, |
|
863 | "image/png" : append_png, | |
858 | "image/jpeg" : append_jpeg, |
|
864 | "image/jpeg" : append_jpeg, | |
859 | "text/latex" : append_latex, |
|
865 | "text/latex" : append_latex, | |
860 | "application/javascript" : append_javascript, |
|
866 | "application/javascript" : append_javascript, | |
861 | "application/pdf" : append_pdf |
|
867 | "application/pdf" : append_pdf | |
862 | }; |
|
868 | }; | |
863 |
|
869 | |||
864 | IPython.OutputArea = OutputArea; |
|
870 | IPython.OutputArea = OutputArea; | |
865 |
|
871 | |||
866 | return IPython; |
|
872 | return IPython; | |
867 |
|
873 | |||
868 | }(IPython)); |
|
874 | }(IPython)); |
General Comments 0
You need to be logged in to leave comments.
Login now