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