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