Show More
@@ -1,726 +1,725 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 vbox'); |
|
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 | |
|
258 | 258 | OutputArea.prototype.convert_mime_types = function (json, data) { |
|
259 | 259 | if (data === undefined) { |
|
260 | 260 | return json; |
|
261 | 261 | } |
|
262 | 262 | if (data['text/plain'] !== undefined) { |
|
263 | 263 | json.text = data['text/plain']; |
|
264 | 264 | } |
|
265 | 265 | if (data['text/html'] !== undefined) { |
|
266 | 266 | json.html = data['text/html']; |
|
267 | 267 | } |
|
268 | 268 | if (data['image/svg+xml'] !== undefined) { |
|
269 | 269 | json.svg = data['image/svg+xml']; |
|
270 | 270 | } |
|
271 | 271 | if (data['image/png'] !== undefined) { |
|
272 | 272 | json.png = data['image/png']; |
|
273 | 273 | } |
|
274 | 274 | if (data['image/jpeg'] !== undefined) { |
|
275 | 275 | json.jpeg = data['image/jpeg']; |
|
276 | 276 | } |
|
277 | 277 | if (data['text/latex'] !== undefined) { |
|
278 | 278 | json.latex = data['text/latex']; |
|
279 | 279 | } |
|
280 | 280 | if (data['application/json'] !== undefined) { |
|
281 | 281 | json.json = data['application/json']; |
|
282 | 282 | } |
|
283 | 283 | if (data['application/javascript'] !== undefined) { |
|
284 | 284 | json.javascript = data['application/javascript']; |
|
285 | 285 | } |
|
286 | 286 | return json; |
|
287 | 287 | }; |
|
288 | 288 | |
|
289 | 289 | |
|
290 | 290 | OutputArea.prototype.append_output = function (json, dynamic) { |
|
291 | 291 | // If dynamic is true, javascript output will be eval'd. |
|
292 | 292 | this.expand(); |
|
293 | 293 | |
|
294 | 294 | // Clear the output if clear is queued. |
|
295 | 295 | var needs_height_reset = false; |
|
296 | 296 | if (this.clear_queued) { |
|
297 | 297 | this.clear_output(false); |
|
298 | 298 | needs_height_reset = true; |
|
299 | 299 | } |
|
300 | 300 | |
|
301 | 301 | if (json.output_type === 'pyout') { |
|
302 | 302 | this.append_pyout(json, dynamic); |
|
303 | 303 | } else if (json.output_type === 'pyerr') { |
|
304 | 304 | this.append_pyerr(json); |
|
305 | 305 | } else if (json.output_type === 'display_data') { |
|
306 | 306 | this.append_display_data(json, dynamic); |
|
307 | 307 | } else if (json.output_type === 'stream') { |
|
308 | 308 | this.append_stream(json); |
|
309 | 309 | } |
|
310 | 310 | this.outputs.push(json); |
|
311 | 311 | |
|
312 | 312 | // Only reset the height to automatic if the height is currently |
|
313 | 313 | // fixed (done by wait=True flag on clear_output). |
|
314 | 314 | if (needs_height_reset) { |
|
315 | 315 | this.element.height(''); |
|
316 | 316 | } |
|
317 | 317 | |
|
318 | 318 | var that = this; |
|
319 | 319 | setTimeout(function(){that.element.trigger('resize');}, 100); |
|
320 | 320 | }; |
|
321 | 321 | |
|
322 | 322 | |
|
323 | 323 | OutputArea.prototype.create_output_area = function () { |
|
324 | 324 | var oa = $("<div/>").addClass("output_area"); |
|
325 | 325 | if (this.prompt_area) { |
|
326 | 326 | oa.append($('<div/>').addClass('prompt')); |
|
327 | 327 | } |
|
328 | 328 | return oa; |
|
329 | 329 | }; |
|
330 | 330 | |
|
331 | 331 | |
|
332 | 332 | OutputArea.prototype.create_output_subarea = function(md, classes) { |
|
333 | 333 | var subarea = $('<div/>').addClass('output_subarea').addClass(classes); |
|
334 | 334 | if (md['isolated']) { |
|
335 | 335 | // Create an iframe to isolate the subarea from the rest of the |
|
336 | 336 | // document |
|
337 | 337 | var iframe = $('<iframe/>').addClass('box-flex1'); |
|
338 |
iframe. |
|
|
339 | iframe.css({'width':'100%', 'display':'block'}); | |
|
338 | iframe.css({'height':1, 'width':'100%', 'display':'block'}); | |
|
340 | 339 | iframe.attr('frameborder', 0); |
|
341 | 340 | iframe.attr('scrolling', 'auto'); |
|
342 | 341 | |
|
343 | 342 | // Once the iframe is loaded, the subarea is dynamically inserted |
|
344 | 343 | iframe.on('load', function() { |
|
345 | 344 | // Workaround needed by Firefox, to properly render svg inside |
|
346 | 345 | // iframes, see http://stackoverflow.com/questions/10177190/ |
|
347 | 346 | // svg-dynamically-added-to-iframe-does-not-render-correctly |
|
348 | 347 | this.contentDocument.open(); |
|
349 | 348 | |
|
350 | 349 | // Insert the subarea into the iframe |
|
351 | 350 | // We must directly write the html. When using Jquery's append |
|
352 | 351 | // method, javascript is evaluated in the parent document and |
|
353 | 352 | // not in the iframe document. |
|
354 | 353 | this.contentDocument.write(subarea.html()); |
|
355 | 354 | |
|
356 | 355 | this.contentDocument.close(); |
|
357 | 356 | |
|
358 | 357 | var body = this.contentDocument.body; |
|
359 | 358 | // Adjust the iframe height automatically |
|
360 | 359 | iframe.height(body.scrollHeight + 'px'); |
|
361 | 360 | }); |
|
362 | 361 | |
|
363 | 362 | // Elements should be appended to the inner subarea and not to the |
|
364 | 363 | // iframe |
|
365 | 364 | iframe.append = function(that) { |
|
366 | 365 | subarea.append(that); |
|
367 | 366 | }; |
|
368 | 367 | |
|
369 | 368 | return iframe; |
|
370 | 369 | } else { |
|
371 | 370 | return subarea; |
|
372 | 371 | } |
|
373 | 372 | } |
|
374 | 373 | |
|
375 | 374 | |
|
376 | 375 | OutputArea.prototype._append_javascript_error = function (err, container) { |
|
377 | 376 | // display a message when a javascript error occurs in display output |
|
378 | 377 | var msg = "Javascript error adding output!" |
|
379 | 378 | console.log(msg, err); |
|
380 | 379 | if ( container === undefined ) return; |
|
381 | 380 | container.append( |
|
382 | 381 | $('<div/>').html(msg + "<br/>" + |
|
383 | 382 | err.toString() + |
|
384 | 383 | '<br/>See your browser Javascript console for more details.' |
|
385 | 384 | ).addClass('js-error') |
|
386 | 385 | ); |
|
387 | 386 | container.show(); |
|
388 | 387 | }; |
|
389 | 388 | |
|
390 | 389 | OutputArea.prototype._safe_append = function (toinsert) { |
|
391 | 390 | // safely append an item to the document |
|
392 | 391 | // this is an object created by user code, |
|
393 | 392 | // and may have errors, which should not be raised |
|
394 | 393 | // under any circumstances. |
|
395 | 394 | try { |
|
396 | 395 | this.element.append(toinsert); |
|
397 | 396 | } catch(err) { |
|
398 | 397 | console.log(err); |
|
399 | 398 | this._append_javascript_error(err, this.element); |
|
400 | 399 | } |
|
401 | 400 | }; |
|
402 | 401 | |
|
403 | 402 | |
|
404 | 403 | OutputArea.prototype.append_pyout = function (json, dynamic) { |
|
405 | 404 | var n = json.prompt_number || ' '; |
|
406 | 405 | var toinsert = this.create_output_area(); |
|
407 | 406 | if (this.prompt_area) { |
|
408 | 407 | toinsert.find('div.prompt').addClass('output_prompt').html('Out[' + n + ']:'); |
|
409 | 408 | } |
|
410 | 409 | this.append_mime_type(json, toinsert, dynamic); |
|
411 | 410 | this._safe_append(toinsert); |
|
412 | 411 | // If we just output latex, typeset it. |
|
413 | 412 | if ((json.latex !== undefined) || (json.html !== undefined)) { |
|
414 | 413 | this.typeset(); |
|
415 | 414 | } |
|
416 | 415 | }; |
|
417 | 416 | |
|
418 | 417 | |
|
419 | 418 | OutputArea.prototype.append_pyerr = function (json) { |
|
420 | 419 | var tb = json.traceback; |
|
421 | 420 | if (tb !== undefined && tb.length > 0) { |
|
422 | 421 | var s = ''; |
|
423 | 422 | var len = tb.length; |
|
424 | 423 | for (var i=0; i<len; i++) { |
|
425 | 424 | s = s + tb[i] + '\n'; |
|
426 | 425 | } |
|
427 | 426 | s = s + '\n'; |
|
428 | 427 | var toinsert = this.create_output_area(); |
|
429 | 428 | this.append_text(s, {}, toinsert); |
|
430 | 429 | this._safe_append(toinsert); |
|
431 | 430 | } |
|
432 | 431 | }; |
|
433 | 432 | |
|
434 | 433 | |
|
435 | 434 | OutputArea.prototype.append_stream = function (json) { |
|
436 | 435 | // temporary fix: if stream undefined (json file written prior to this patch), |
|
437 | 436 | // default to most likely stdout: |
|
438 | 437 | if (json.stream == undefined){ |
|
439 | 438 | json.stream = 'stdout'; |
|
440 | 439 | } |
|
441 | 440 | var text = json.text; |
|
442 | 441 | var subclass = "output_"+json.stream; |
|
443 | 442 | if (this.outputs.length > 0){ |
|
444 | 443 | // have at least one output to consider |
|
445 | 444 | var last = this.outputs[this.outputs.length-1]; |
|
446 | 445 | if (last.output_type == 'stream' && json.stream == last.stream){ |
|
447 | 446 | // latest output was in the same stream, |
|
448 | 447 | // so append directly into its pre tag |
|
449 | 448 | // escape ANSI & HTML specials: |
|
450 | 449 | var pre = this.element.find('div.'+subclass).last().find('pre'); |
|
451 | 450 | var html = utils.fixCarriageReturn( |
|
452 | 451 | pre.html() + utils.fixConsole(text)); |
|
453 | 452 | pre.html(html); |
|
454 | 453 | return; |
|
455 | 454 | } |
|
456 | 455 | } |
|
457 | 456 | |
|
458 | 457 | if (!text.replace("\r", "")) { |
|
459 | 458 | // text is nothing (empty string, \r, etc.) |
|
460 | 459 | // so don't append any elements, which might add undesirable space |
|
461 | 460 | return; |
|
462 | 461 | } |
|
463 | 462 | |
|
464 | 463 | // If we got here, attach a new div |
|
465 | 464 | var toinsert = this.create_output_area(); |
|
466 | 465 | this.append_text(text, {}, toinsert, "output_stream "+subclass); |
|
467 | 466 | this._safe_append(toinsert); |
|
468 | 467 | }; |
|
469 | 468 | |
|
470 | 469 | |
|
471 | 470 | OutputArea.prototype.append_display_data = function (json, dynamic) { |
|
472 | 471 | var toinsert = this.create_output_area(); |
|
473 | 472 | this.append_mime_type(json, toinsert, dynamic); |
|
474 | 473 | this._safe_append(toinsert); |
|
475 | 474 | // If we just output latex, typeset it. |
|
476 | 475 | if ( (json.latex !== undefined) || (json.html !== undefined) ) { |
|
477 | 476 | this.typeset(); |
|
478 | 477 | } |
|
479 | 478 | }; |
|
480 | 479 | |
|
481 | 480 | OutputArea.display_order = ['javascript','html','latex','svg','png','jpeg','text']; |
|
482 | 481 | |
|
483 | 482 | OutputArea.prototype.append_mime_type = function (json, element, dynamic) { |
|
484 | 483 | for(var type_i in OutputArea.display_order){ |
|
485 | 484 | var type = OutputArea.display_order[type_i]; |
|
486 | 485 | if(json[type] != undefined ){ |
|
487 | 486 | var md = {}; |
|
488 | 487 | if (json.metadata && json.metadata[type]) { |
|
489 | 488 | md = json.metadata[type]; |
|
490 | 489 | }; |
|
491 | 490 | if(type == 'javascript'){ |
|
492 | 491 | if (dynamic) { |
|
493 | 492 | this.append_javascript(json.javascript, md, element, dynamic); |
|
494 | 493 | } |
|
495 | 494 | } else { |
|
496 | 495 | this['append_'+type](json[type], md, element); |
|
497 | 496 | } |
|
498 | 497 | return; |
|
499 | 498 | } |
|
500 | 499 | } |
|
501 | 500 | }; |
|
502 | 501 | |
|
503 | 502 | |
|
504 | 503 | OutputArea.prototype.append_html = function (html, md, element) { |
|
505 | 504 | var toinsert = this.create_output_subarea(md, "output_html rendered_html"); |
|
506 | 505 | toinsert.append(html); |
|
507 | 506 | element.append(toinsert); |
|
508 | 507 | }; |
|
509 | 508 | |
|
510 | 509 | |
|
511 | 510 | OutputArea.prototype.append_javascript = function (js, md, container) { |
|
512 | 511 | // We just eval the JS code, element appears in the local scope. |
|
513 | 512 | var element = this.create_output_subarea(md, ""); |
|
514 | 513 | container.append(element); |
|
515 | 514 | // Div for js shouldn't be drawn, as it will add empty height to the area. |
|
516 | 515 | container.hide(); |
|
517 | 516 | // If the Javascript appends content to `element` that should be drawn, then |
|
518 | 517 | // it must also call `container.show()`. |
|
519 | 518 | try { |
|
520 | 519 | eval(js); |
|
521 | 520 | } catch(err) { |
|
522 | 521 | this._append_javascript_error(err, container); |
|
523 | 522 | } |
|
524 | 523 | }; |
|
525 | 524 | |
|
526 | 525 | |
|
527 | 526 | OutputArea.prototype.append_text = function (data, md, element, extra_class) { |
|
528 | 527 | var toinsert = this.create_output_subarea(md, "output_text"); |
|
529 | 528 | // escape ANSI & HTML specials in plaintext: |
|
530 | 529 | data = utils.fixConsole(data); |
|
531 | 530 | data = utils.fixCarriageReturn(data); |
|
532 | 531 | data = utils.autoLinkUrls(data); |
|
533 | 532 | if (extra_class){ |
|
534 | 533 | toinsert.addClass(extra_class); |
|
535 | 534 | } |
|
536 | 535 | toinsert.append($("<pre/>").html(data)); |
|
537 | 536 | element.append(toinsert); |
|
538 | 537 | }; |
|
539 | 538 | |
|
540 | 539 | |
|
541 | 540 | OutputArea.prototype.append_svg = function (svg, md, element) { |
|
542 | 541 | var toinsert = this.create_output_subarea(md, "output_svg"); |
|
543 | 542 | toinsert.append(svg); |
|
544 | 543 | element.append(toinsert); |
|
545 | 544 | }; |
|
546 | 545 | |
|
547 | 546 | |
|
548 | 547 | OutputArea.prototype._dblclick_to_reset_size = function (img) { |
|
549 | 548 | // schedule wrapping image in resizable after a delay, |
|
550 | 549 | // so we don't end up calling resize on a zero-size object |
|
551 | 550 | var that = this; |
|
552 | 551 | setTimeout(function () { |
|
553 | 552 | var h0 = img.height(); |
|
554 | 553 | var w0 = img.width(); |
|
555 | 554 | if (!(h0 && w0)) { |
|
556 | 555 | // zero size, schedule another timeout |
|
557 | 556 | that._dblclick_to_reset_size(img); |
|
558 | 557 | return; |
|
559 | 558 | } |
|
560 | 559 | img.resizable({ |
|
561 | 560 | aspectRatio: true, |
|
562 | 561 | autoHide: true |
|
563 | 562 | }); |
|
564 | 563 | img.dblclick(function () { |
|
565 | 564 | // resize wrapper & image together for some reason: |
|
566 | 565 | img.parent().height(h0); |
|
567 | 566 | img.height(h0); |
|
568 | 567 | img.parent().width(w0); |
|
569 | 568 | img.width(w0); |
|
570 | 569 | }); |
|
571 | 570 | }, 250); |
|
572 | 571 | }; |
|
573 | 572 | |
|
574 | 573 | |
|
575 | 574 | OutputArea.prototype.append_png = function (png, md, element) { |
|
576 | 575 | var toinsert = this.create_output_subarea(md, "output_png"); |
|
577 | 576 | var img = $("<img/>").attr('src','data:image/png;base64,'+png); |
|
578 | 577 | if (md['height']) { |
|
579 | 578 | img.attr('height', md['height']); |
|
580 | 579 | } |
|
581 | 580 | if (md['width']) { |
|
582 | 581 | img.attr('width', md['width']); |
|
583 | 582 | } |
|
584 | 583 | this._dblclick_to_reset_size(img); |
|
585 | 584 | toinsert.append(img); |
|
586 | 585 | element.append(toinsert); |
|
587 | 586 | }; |
|
588 | 587 | |
|
589 | 588 | |
|
590 | 589 | OutputArea.prototype.append_jpeg = function (jpeg, md, element) { |
|
591 | 590 | var toinsert = this.create_output_subarea(md, "output_jpeg"); |
|
592 | 591 | var img = $("<img/>").attr('src','data:image/jpeg;base64,'+jpeg); |
|
593 | 592 | if (md['height']) { |
|
594 | 593 | img.attr('height', md['height']); |
|
595 | 594 | } |
|
596 | 595 | if (md['width']) { |
|
597 | 596 | img.attr('width', md['width']); |
|
598 | 597 | } |
|
599 | 598 | this._dblclick_to_reset_size(img); |
|
600 | 599 | toinsert.append(img); |
|
601 | 600 | element.append(toinsert); |
|
602 | 601 | }; |
|
603 | 602 | |
|
604 | 603 | |
|
605 | 604 | OutputArea.prototype.append_latex = function (latex, md, element) { |
|
606 | 605 | // This method cannot do the typesetting because the latex first has to |
|
607 | 606 | // be on the page. |
|
608 | 607 | var toinsert = this.create_output_subarea(md, "output_latex"); |
|
609 | 608 | toinsert.append(latex); |
|
610 | 609 | element.append(toinsert); |
|
611 | 610 | }; |
|
612 | 611 | |
|
613 | 612 | OutputArea.prototype.append_raw_input = function (msg) { |
|
614 | 613 | var that = this; |
|
615 | 614 | this.expand(); |
|
616 | 615 | var content = msg.content; |
|
617 | 616 | var area = this.create_output_area(); |
|
618 | 617 | |
|
619 | 618 | // disable any other raw_inputs, if they are left around |
|
620 | 619 | $("div.output_subarea.raw_input").remove(); |
|
621 | 620 | |
|
622 | 621 | area.append( |
|
623 | 622 | $("<div/>") |
|
624 | 623 | .addClass("box-flex1 output_subarea raw_input") |
|
625 | 624 | .append( |
|
626 | 625 | $("<span/>") |
|
627 | 626 | .addClass("input_prompt") |
|
628 | 627 | .text(content.prompt) |
|
629 | 628 | ) |
|
630 | 629 | .append( |
|
631 | 630 | $("<input/>") |
|
632 | 631 | .addClass("raw_input") |
|
633 | 632 | .attr('type', 'text') |
|
634 | 633 | .attr("size", 47) |
|
635 | 634 | .keydown(function (event, ui) { |
|
636 | 635 | // make sure we submit on enter, |
|
637 | 636 | // and don't re-execute the *cell* on shift-enter |
|
638 | 637 | if (event.which === utils.keycodes.ENTER) { |
|
639 | 638 | that._submit_raw_input(); |
|
640 | 639 | return false; |
|
641 | 640 | } |
|
642 | 641 | }) |
|
643 | 642 | ) |
|
644 | 643 | ); |
|
645 | 644 | this.element.append(area); |
|
646 | 645 | // weirdly need double-focus now, |
|
647 | 646 | // otherwise only the cell will be focused |
|
648 | 647 | area.find("input.raw_input").focus().focus(); |
|
649 | 648 | } |
|
650 | 649 | OutputArea.prototype._submit_raw_input = function (evt) { |
|
651 | 650 | var container = this.element.find("div.raw_input"); |
|
652 | 651 | var theprompt = container.find("span.input_prompt"); |
|
653 | 652 | var theinput = container.find("input.raw_input"); |
|
654 | 653 | var value = theinput.val(); |
|
655 | 654 | var content = { |
|
656 | 655 | output_type : 'stream', |
|
657 | 656 | name : 'stdout', |
|
658 | 657 | text : theprompt.text() + value + '\n' |
|
659 | 658 | } |
|
660 | 659 | // remove form container |
|
661 | 660 | container.parent().remove(); |
|
662 | 661 | // replace with plaintext version in stdout |
|
663 | 662 | this.append_output(content, false); |
|
664 | 663 | $([IPython.events]).trigger('send_input_reply.Kernel', value); |
|
665 | 664 | } |
|
666 | 665 | |
|
667 | 666 | |
|
668 | 667 | OutputArea.prototype.handle_clear_output = function (msg) { |
|
669 | 668 | this.clear_output(msg.content.wait); |
|
670 | 669 | }; |
|
671 | 670 | |
|
672 | 671 | |
|
673 | 672 | OutputArea.prototype.clear_output = function(wait) { |
|
674 | 673 | if (wait) { |
|
675 | 674 | |
|
676 | 675 | // If a clear is queued, clear before adding another to the queue. |
|
677 | 676 | if (this.clear_queued) { |
|
678 | 677 | this.clear_output(false); |
|
679 | 678 | }; |
|
680 | 679 | |
|
681 | 680 | this.clear_queued = true; |
|
682 | 681 | } else { |
|
683 | 682 | |
|
684 | 683 | // Fix the output div's height if the clear_output is waiting for |
|
685 | 684 | // new output (it is being used in an animation). |
|
686 | 685 | if (this.clear_queued) { |
|
687 | 686 | var height = this.element.height(); |
|
688 | 687 | this.element.height(height); |
|
689 | 688 | this.clear_queued = false; |
|
690 | 689 | } |
|
691 | 690 | |
|
692 | 691 | // clear all, no need for logic |
|
693 | 692 | this.element.html(""); |
|
694 | 693 | this.outputs = []; |
|
695 | 694 | this.unscroll_area(); |
|
696 | 695 | return; |
|
697 | 696 | }; |
|
698 | 697 | }; |
|
699 | 698 | |
|
700 | 699 | |
|
701 | 700 | // JSON serialization |
|
702 | 701 | |
|
703 | 702 | OutputArea.prototype.fromJSON = function (outputs) { |
|
704 | 703 | var len = outputs.length; |
|
705 | 704 | for (var i=0; i<len; i++) { |
|
706 | 705 | // append with dynamic=false. |
|
707 | 706 | this.append_output(outputs[i], false); |
|
708 | 707 | } |
|
709 | 708 | }; |
|
710 | 709 | |
|
711 | 710 | |
|
712 | 711 | OutputArea.prototype.toJSON = function () { |
|
713 | 712 | var outputs = []; |
|
714 | 713 | var len = this.outputs.length; |
|
715 | 714 | for (var i=0; i<len; i++) { |
|
716 | 715 | outputs[i] = this.outputs[i]; |
|
717 | 716 | } |
|
718 | 717 | return outputs; |
|
719 | 718 | }; |
|
720 | 719 | |
|
721 | 720 | |
|
722 | 721 | IPython.OutputArea = OutputArea; |
|
723 | 722 | |
|
724 | 723 | return IPython; |
|
725 | 724 | |
|
726 | 725 | }(IPython)); |
General Comments 0
You need to be logged in to leave comments.
Login now