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