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