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