Show More
@@ -1,685 +1,687 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_out_timeout = 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_type, content) { |
|
235 | 235 | var json = {}; |
|
236 | 236 | json.output_type = msg_type; |
|
237 | 237 | if (msg_type === "stream") { |
|
238 | 238 | json.text = content.data; |
|
239 | 239 | json.stream = content.name; |
|
240 | 240 | } else if (msg_type === "display_data") { |
|
241 | 241 | json = this.convert_mime_types(json, content.data); |
|
242 | 242 | json.metadata = this.convert_mime_types({}, content.metadata); |
|
243 | 243 | } else if (msg_type === "pyout") { |
|
244 | 244 | json.prompt_number = content.execution_count; |
|
245 | 245 | json = this.convert_mime_types(json, content.data); |
|
246 | 246 | json.metadata = this.convert_mime_types({}, content.metadata); |
|
247 | 247 | } else if (msg_type === "pyerr") { |
|
248 | 248 | json.ename = content.ename; |
|
249 | 249 | json.evalue = content.evalue; |
|
250 | 250 | json.traceback = content.traceback; |
|
251 | 251 | } |
|
252 | 252 | // append with dynamic=true |
|
253 | 253 | this.append_output(json, true); |
|
254 | 254 | }; |
|
255 | 255 | |
|
256 | 256 | |
|
257 | 257 | OutputArea.prototype.convert_mime_types = function (json, data) { |
|
258 | 258 | if (data['text/plain'] !== undefined) { |
|
259 | 259 | json.text = data['text/plain']; |
|
260 | 260 | } |
|
261 | 261 | if (data['text/html'] !== undefined) { |
|
262 | 262 | json.html = data['text/html']; |
|
263 | 263 | } |
|
264 | 264 | if (data['image/svg+xml'] !== undefined) { |
|
265 | 265 | json.svg = data['image/svg+xml']; |
|
266 | 266 | } |
|
267 | 267 | if (data['image/png'] !== undefined) { |
|
268 | 268 | json.png = data['image/png']; |
|
269 | 269 | } |
|
270 | 270 | if (data['image/jpeg'] !== undefined) { |
|
271 | 271 | json.jpeg = data['image/jpeg']; |
|
272 | 272 | } |
|
273 | 273 | if (data['text/latex'] !== undefined) { |
|
274 | 274 | json.latex = data['text/latex']; |
|
275 | 275 | } |
|
276 | 276 | if (data['application/json'] !== undefined) { |
|
277 | 277 | json.json = data['application/json']; |
|
278 | 278 | } |
|
279 | 279 | if (data['application/javascript'] !== undefined) { |
|
280 | 280 | json.javascript = data['application/javascript']; |
|
281 | 281 | } |
|
282 | 282 | return json; |
|
283 | 283 | }; |
|
284 | 284 | |
|
285 | 285 | |
|
286 | 286 | OutputArea.prototype.append_output = function (json, dynamic) { |
|
287 | 287 | // If dynamic is true, javascript output will be eval'd. |
|
288 | 288 | this.expand(); |
|
289 | 289 | this.flush_clear_timeout(); |
|
290 | 290 | if (json.output_type === 'pyout') { |
|
291 | 291 | this.append_pyout(json, dynamic); |
|
292 | 292 | } else if (json.output_type === 'pyerr') { |
|
293 | 293 | this.append_pyerr(json); |
|
294 | 294 | } else if (json.output_type === 'display_data') { |
|
295 | 295 | this.append_display_data(json, dynamic); |
|
296 | 296 | } else if (json.output_type === 'stream') { |
|
297 | 297 | this.append_stream(json); |
|
298 | 298 | } |
|
299 | 299 | this.outputs.push(json); |
|
300 | 300 | var that = this; |
|
301 | 301 | setTimeout(function(){that.element.trigger('resize');}, 100); |
|
302 | 302 | }; |
|
303 | 303 | |
|
304 | 304 | |
|
305 | 305 | OutputArea.prototype.create_output_area = function () { |
|
306 | 306 | var oa = $("<div/>").addClass("output_area"); |
|
307 | 307 | if (this.prompt_area) { |
|
308 | 308 | oa.append($('<div/>').addClass('prompt')); |
|
309 | 309 | } |
|
310 | 310 | return oa; |
|
311 | 311 | }; |
|
312 | 312 | |
|
313 | 313 | |
|
314 | 314 | OutputArea.prototype.append_pyout = function (json, dynamic) { |
|
315 | 315 | var n = json.prompt_number || ' '; |
|
316 | 316 | var toinsert = this.create_output_area(); |
|
317 | 317 | if (this.prompt_area) { |
|
318 | 318 | toinsert.find('div.prompt').addClass('output_prompt').html('Out[' + n + ']:'); |
|
319 | 319 | } |
|
320 | 320 | this.append_mime_type(json, toinsert, dynamic); |
|
321 | 321 | this.element.append(toinsert); |
|
322 | 322 | // If we just output latex, typeset it. |
|
323 | 323 | if ((json.latex !== undefined) || (json.html !== undefined)) { |
|
324 | 324 | this.typeset(); |
|
325 | 325 | } |
|
326 | 326 | }; |
|
327 | 327 | |
|
328 | 328 | |
|
329 | 329 | OutputArea.prototype.append_pyerr = function (json) { |
|
330 | 330 | var tb = json.traceback; |
|
331 | 331 | if (tb !== undefined && tb.length > 0) { |
|
332 | 332 | var s = ''; |
|
333 | 333 | var len = tb.length; |
|
334 | 334 | for (var i=0; i<len; i++) { |
|
335 | 335 | s = s + tb[i] + '\n'; |
|
336 | 336 | } |
|
337 | 337 | s = s + '\n'; |
|
338 | 338 | var toinsert = this.create_output_area(); |
|
339 | 339 | this.append_text(s, {}, toinsert); |
|
340 | 340 | this.element.append(toinsert); |
|
341 | 341 | } |
|
342 | 342 | }; |
|
343 | 343 | |
|
344 | 344 | |
|
345 | 345 | OutputArea.prototype.append_stream = function (json) { |
|
346 | 346 | // temporary fix: if stream undefined (json file written prior to this patch), |
|
347 | 347 | // default to most likely stdout: |
|
348 | 348 | if (json.stream == undefined){ |
|
349 | 349 | json.stream = 'stdout'; |
|
350 | 350 | } |
|
351 | 351 | var text = json.text; |
|
352 | 352 | var subclass = "output_"+json.stream; |
|
353 | 353 | if (this.outputs.length > 0){ |
|
354 | 354 | // have at least one output to consider |
|
355 | 355 | var last = this.outputs[this.outputs.length-1]; |
|
356 | 356 | if (last.output_type == 'stream' && json.stream == last.stream){ |
|
357 | 357 | // latest output was in the same stream, |
|
358 | 358 | // so append directly into its pre tag |
|
359 | 359 | // escape ANSI & HTML specials: |
|
360 | 360 | var pre = this.element.find('div.'+subclass).last().find('pre'); |
|
361 | 361 | var html = utils.fixCarriageReturn( |
|
362 | 362 | pre.html() + utils.fixConsole(text)); |
|
363 | 363 | pre.html(html); |
|
364 | 364 | return; |
|
365 | 365 | } |
|
366 | 366 | } |
|
367 | 367 | |
|
368 | 368 | if (!text.replace("\r", "")) { |
|
369 | 369 | // text is nothing (empty string, \r, etc.) |
|
370 | 370 | // so don't append any elements, which might add undesirable space |
|
371 | 371 | return; |
|
372 | 372 | } |
|
373 | 373 | |
|
374 | 374 | // If we got here, attach a new div |
|
375 | 375 | var toinsert = this.create_output_area(); |
|
376 | 376 | this.append_text(text, {}, toinsert, "output_stream "+subclass); |
|
377 | 377 | this.element.append(toinsert); |
|
378 | 378 | }; |
|
379 | 379 | |
|
380 | 380 | |
|
381 | 381 | OutputArea.prototype.append_display_data = function (json, dynamic) { |
|
382 | 382 | var toinsert = this.create_output_area(); |
|
383 | 383 | this.append_mime_type(json, toinsert, dynamic); |
|
384 | 384 | this.element.append(toinsert); |
|
385 | 385 | // If we just output latex, typeset it. |
|
386 | 386 | if ( (json.latex !== undefined) || (json.html !== undefined) ) { |
|
387 | 387 | this.typeset(); |
|
388 | 388 | } |
|
389 | 389 | }; |
|
390 | 390 | |
|
391 | 391 | OutputArea.display_order = ['javascript','html','latex','svg','png','jpeg','text']; |
|
392 | 392 | |
|
393 | 393 | OutputArea.prototype.append_mime_type = function (json, element, dynamic) { |
|
394 | 394 | for(var type_i in OutputArea.display_order){ |
|
395 | 395 | var type = OutputArea.display_order[type_i]; |
|
396 | 396 | if(json[type] != undefined ){ |
|
397 | 397 | var md = {}; |
|
398 | 398 | if (json.metadata && json.metadata[type]) { |
|
399 | 399 | md = json.metadata[type]; |
|
400 | 400 | }; |
|
401 | 401 | if(type == 'javascript'){ |
|
402 | 402 | if (dynamic) { |
|
403 | 403 | this.append_javascript(json.javascript, md, element, dynamic); |
|
404 | 404 | } |
|
405 | 405 | } else { |
|
406 | 406 | this['append_'+type](json[type], md, element); |
|
407 | 407 | } |
|
408 | 408 | return; |
|
409 | 409 | } |
|
410 | 410 | } |
|
411 | 411 | }; |
|
412 | 412 | |
|
413 | 413 | |
|
414 | 414 | OutputArea.prototype.append_html = function (html, md, element) { |
|
415 | 415 | var toinsert = $("<div/>").addClass("output_subarea output_html rendered_html"); |
|
416 | 416 | toinsert.append(html); |
|
417 | 417 | element.append(toinsert); |
|
418 | 418 | }; |
|
419 | 419 | |
|
420 | 420 | |
|
421 | 421 | OutputArea.prototype.append_javascript = function (js, md, container) { |
|
422 | 422 | // We just eval the JS code, element appears in the local scope. |
|
423 | 423 | var element = $("<div/>").addClass("output_subarea"); |
|
424 | 424 | container.append(element); |
|
425 | 425 | // Div for js shouldn't be drawn, as it will add empty height to the area. |
|
426 | 426 | container.hide(); |
|
427 | 427 | // If the Javascript appends content to `element` that should be drawn, then |
|
428 | 428 | // it must also call `container.show()`. |
|
429 | 429 | try { |
|
430 | 430 | eval(js); |
|
431 | 431 | } catch(err) { |
|
432 | 432 | console.log('Error in Javascript!'); |
|
433 | 433 | console.log(err); |
|
434 | 434 | container.show(); |
|
435 | 435 | element.append($('<div/>') |
|
436 | 436 | .html("Error in Javascript !<br/>"+ |
|
437 | 437 | err.toString()+ |
|
438 | 438 | '<br/>See your browser Javascript console for more details.') |
|
439 | 439 | .addClass('js-error') |
|
440 | 440 | ); |
|
441 | 441 | } |
|
442 | 442 | }; |
|
443 | 443 | |
|
444 | 444 | |
|
445 | 445 | OutputArea.prototype.append_text = function (data, md, element, extra_class) { |
|
446 | 446 | var toinsert = $("<div/>").addClass("output_subarea output_text"); |
|
447 | 447 | // escape ANSI & HTML specials in plaintext: |
|
448 | 448 | data = utils.fixConsole(data); |
|
449 | 449 | data = utils.fixCarriageReturn(data); |
|
450 | 450 | data = utils.autoLinkUrls(data); |
|
451 | 451 | if (extra_class){ |
|
452 | 452 | toinsert.addClass(extra_class); |
|
453 | 453 | } |
|
454 | 454 | toinsert.append($("<pre/>").html(data)); |
|
455 | 455 | element.append(toinsert); |
|
456 | 456 | }; |
|
457 | 457 | |
|
458 | 458 | |
|
459 | 459 | OutputArea.prototype.append_svg = function (svg, md, element) { |
|
460 | 460 | var toinsert = $("<div/>").addClass("output_subarea output_svg"); |
|
461 | 461 | toinsert.append(svg); |
|
462 | 462 | element.append(toinsert); |
|
463 | 463 | }; |
|
464 | 464 | |
|
465 | 465 | |
|
466 | 466 | OutputArea.prototype._dblclick_to_reset_size = function (img) { |
|
467 | 467 | // schedule wrapping image in resizable after a delay, |
|
468 | 468 | // so we don't end up calling resize on a zero-size object |
|
469 | 469 | var that = this; |
|
470 | 470 | setTimeout(function () { |
|
471 | 471 | var h0 = img.height(); |
|
472 | 472 | var w0 = img.width(); |
|
473 | 473 | if (!(h0 && w0)) { |
|
474 | 474 | // zero size, schedule another timeout |
|
475 | 475 | that._dblclick_to_reset_size(img); |
|
476 | 476 | return; |
|
477 | 477 | } |
|
478 | 478 | img.resizable({ |
|
479 | 479 | aspectRatio: true, |
|
480 | 480 | autoHide: true |
|
481 | 481 | }); |
|
482 | 482 | img.dblclick(function () { |
|
483 | 483 | // resize wrapper & image together for some reason: |
|
484 | 484 | img.parent().height(h0); |
|
485 | 485 | img.height(h0); |
|
486 | 486 | img.parent().width(w0); |
|
487 | 487 | img.width(w0); |
|
488 | 488 | }); |
|
489 | 489 | }, 250); |
|
490 | 490 | }; |
|
491 | 491 | |
|
492 | 492 | |
|
493 | 493 | OutputArea.prototype.append_png = function (png, md, element) { |
|
494 | 494 | var toinsert = $("<div/>").addClass("output_subarea output_png"); |
|
495 | 495 | var img = $("<img/>").attr('src','data:image/png;base64,'+png); |
|
496 | 496 | if (md['height']) { |
|
497 | 497 | img.attr('height', md['height']); |
|
498 | 498 | } |
|
499 | 499 | if (md['width']) { |
|
500 | 500 | img.attr('width', md['width']); |
|
501 | 501 | } |
|
502 | 502 | this._dblclick_to_reset_size(img); |
|
503 | 503 | toinsert.append(img); |
|
504 | 504 | element.append(toinsert); |
|
505 | 505 | }; |
|
506 | 506 | |
|
507 | 507 | |
|
508 | 508 | OutputArea.prototype.append_jpeg = function (jpeg, md, element) { |
|
509 | 509 | var toinsert = $("<div/>").addClass("output_subarea output_jpeg"); |
|
510 | 510 | var img = $("<img/>").attr('src','data:image/jpeg;base64,'+jpeg); |
|
511 | 511 | if (md['height']) { |
|
512 | 512 | img.attr('height', md['height']); |
|
513 | 513 | } |
|
514 | 514 | if (md['width']) { |
|
515 | 515 | img.attr('width', md['width']); |
|
516 | 516 | } |
|
517 | 517 | this._dblclick_to_reset_size(img); |
|
518 | 518 | toinsert.append(img); |
|
519 | 519 | element.append(toinsert); |
|
520 | 520 | }; |
|
521 | 521 | |
|
522 | 522 | |
|
523 | 523 | OutputArea.prototype.append_latex = function (latex, md, element) { |
|
524 | 524 | // This method cannot do the typesetting because the latex first has to |
|
525 | 525 | // be on the page. |
|
526 | 526 | var toinsert = $("<div/>").addClass("output_subarea output_latex"); |
|
527 | 527 | toinsert.append(latex); |
|
528 | 528 | element.append(toinsert); |
|
529 | 529 | }; |
|
530 | 530 | |
|
531 | 531 | OutputArea.prototype.append_raw_input = function (content) { |
|
532 | 532 | var that = this; |
|
533 | 533 | this.expand(); |
|
534 | 534 | this.flush_clear_timeout(); |
|
535 | 535 | var area = this.create_output_area(); |
|
536 | 536 | |
|
537 | 537 | area.append( |
|
538 | 538 | $("<div/>") |
|
539 | 539 | .addClass("box-flex1 output_subarea raw_input") |
|
540 | 540 | .append( |
|
541 | 541 | $("<span/>") |
|
542 | 542 | .addClass("input_prompt") |
|
543 | 543 | .text(content.prompt) |
|
544 | 544 | ) |
|
545 | 545 | .append( |
|
546 | 546 | $("<input/>") |
|
547 | 547 | .addClass("raw_input") |
|
548 | 548 | .attr('type', 'text') |
|
549 |
.attr("size", |
|
|
549 | .attr("size", 47) | |
|
550 | 550 | .keydown(function (event, ui) { |
|
551 | 551 | // make sure we submit on enter, |
|
552 | 552 | // and don't re-execute the *cell* on shift-enter |
|
553 | 553 | if (event.which === utils.keycodes.ENTER) { |
|
554 | 554 | that._submit_raw_input(); |
|
555 | 555 | return false; |
|
556 | 556 | } |
|
557 | 557 | }) |
|
558 | 558 | ) |
|
559 | 559 | ); |
|
560 | 560 | this.element.append(area); |
|
561 | area.find("input.raw_input").focus(); | |
|
561 | // weirdly need double-focus now, | |
|
562 | // otherwise only the cell will be focused | |
|
563 | area.find("input.raw_input").focus().focus(); | |
|
562 | 564 | } |
|
563 | 565 | OutputArea.prototype._submit_raw_input = function (evt) { |
|
564 | 566 | var container = this.element.find("div.raw_input"); |
|
565 | 567 | var theprompt = container.find("span.input_prompt"); |
|
566 | 568 | var theinput = container.find("input.raw_input"); |
|
567 |
var value = theinput. |
|
|
569 | var value = theinput.val(); | |
|
568 | 570 | var content = { |
|
569 | 571 | output_type : 'stream', |
|
570 | 572 | name : 'stdout', |
|
571 | 573 | text : theprompt.text() + value + '\n' |
|
572 | 574 | } |
|
573 | 575 | // remove form container |
|
574 | 576 | container.parent().remove(); |
|
575 | 577 | // replace with plaintext version in stdout |
|
576 | 578 | this.append_output(content, false); |
|
577 | 579 | $([IPython.events]).trigger('send_input_reply.Kernel', value); |
|
578 | 580 | } |
|
579 | 581 | |
|
580 | 582 | |
|
581 | 583 | OutputArea.prototype.handle_clear_output = function (content) { |
|
582 | 584 | this.clear_output(content.stdout, content.stderr, content.other); |
|
583 | 585 | }; |
|
584 | 586 | |
|
585 | 587 | |
|
586 | 588 | OutputArea.prototype.clear_output = function (stdout, stderr, other) { |
|
587 | 589 | var that = this; |
|
588 | 590 | if (this.clear_out_timeout != null){ |
|
589 | 591 | // fire previous pending clear *immediately* |
|
590 | 592 | clearTimeout(this.clear_out_timeout); |
|
591 | 593 | this.clear_out_timeout = null; |
|
592 | 594 | this.clear_output_callback(this._clear_stdout, this._clear_stderr, this._clear_other); |
|
593 | 595 | } |
|
594 | 596 | // store flags for flushing the timeout |
|
595 | 597 | this._clear_stdout = stdout; |
|
596 | 598 | this._clear_stderr = stderr; |
|
597 | 599 | this._clear_other = other; |
|
598 | 600 | this.clear_out_timeout = setTimeout(function() { |
|
599 | 601 | // really clear timeout only after a short delay |
|
600 | 602 | // this reduces flicker in 'clear_output; print' cases |
|
601 | 603 | that.clear_out_timeout = null; |
|
602 | 604 | that._clear_stdout = that._clear_stderr = that._clear_other = null; |
|
603 | 605 | that.clear_output_callback(stdout, stderr, other); |
|
604 | 606 | }, 500 |
|
605 | 607 | ); |
|
606 | 608 | }; |
|
607 | 609 | |
|
608 | 610 | |
|
609 | 611 | OutputArea.prototype.clear_output_callback = function (stdout, stderr, other) { |
|
610 | 612 | var output_div = this.element; |
|
611 | 613 | |
|
612 | 614 | if (stdout && stderr && other){ |
|
613 | 615 | // clear all, no need for logic |
|
614 | 616 | output_div.html(""); |
|
615 | 617 | this.outputs = []; |
|
616 | 618 | this.unscroll_area(); |
|
617 | 619 | return; |
|
618 | 620 | } |
|
619 | 621 | // remove html output |
|
620 | 622 | // each output_subarea that has an identifying class is in an output_area |
|
621 | 623 | // which is the element to be removed. |
|
622 | 624 | if (stdout) { |
|
623 | 625 | output_div.find("div.output_stdout").parent().remove(); |
|
624 | 626 | } |
|
625 | 627 | if (stderr) { |
|
626 | 628 | output_div.find("div.output_stderr").parent().remove(); |
|
627 | 629 | } |
|
628 | 630 | if (other) { |
|
629 | 631 | output_div.find("div.output_subarea").not("div.output_stderr").not("div.output_stdout").parent().remove(); |
|
630 | 632 | } |
|
631 | 633 | this.unscroll_area(); |
|
632 | 634 | |
|
633 | 635 | // remove cleared outputs from JSON list: |
|
634 | 636 | for (var i = this.outputs.length - 1; i >= 0; i--) { |
|
635 | 637 | var out = this.outputs[i]; |
|
636 | 638 | var output_type = out.output_type; |
|
637 | 639 | if (output_type == "display_data" && other) { |
|
638 | 640 | this.outputs.splice(i,1); |
|
639 | 641 | } else if (output_type == "stream") { |
|
640 | 642 | if (stdout && out.stream == "stdout") { |
|
641 | 643 | this.outputs.splice(i,1); |
|
642 | 644 | } else if (stderr && out.stream == "stderr") { |
|
643 | 645 | this.outputs.splice(i,1); |
|
644 | 646 | } |
|
645 | 647 | } |
|
646 | 648 | } |
|
647 | 649 | }; |
|
648 | 650 | |
|
649 | 651 | |
|
650 | 652 | OutputArea.prototype.flush_clear_timeout = function() { |
|
651 | 653 | var output_div = this.element; |
|
652 | 654 | if (this.clear_out_timeout){ |
|
653 | 655 | clearTimeout(this.clear_out_timeout); |
|
654 | 656 | this.clear_out_timeout = null; |
|
655 | 657 | this.clear_output_callback(this._clear_stdout, this._clear_stderr, this._clear_other); |
|
656 | 658 | } |
|
657 | 659 | }; |
|
658 | 660 | |
|
659 | 661 | |
|
660 | 662 | // JSON serialization |
|
661 | 663 | |
|
662 | 664 | OutputArea.prototype.fromJSON = function (outputs) { |
|
663 | 665 | var len = outputs.length; |
|
664 | 666 | for (var i=0; i<len; i++) { |
|
665 | 667 | // append with dynamic=false. |
|
666 | 668 | this.append_output(outputs[i], false); |
|
667 | 669 | } |
|
668 | 670 | }; |
|
669 | 671 | |
|
670 | 672 | |
|
671 | 673 | OutputArea.prototype.toJSON = function () { |
|
672 | 674 | var outputs = []; |
|
673 | 675 | var len = this.outputs.length; |
|
674 | 676 | for (var i=0; i<len; i++) { |
|
675 | 677 | outputs[i] = this.outputs[i]; |
|
676 | 678 | } |
|
677 | 679 | return outputs; |
|
678 | 680 | }; |
|
679 | 681 | |
|
680 | 682 | |
|
681 | 683 | IPython.OutputArea = OutputArea; |
|
682 | 684 | |
|
683 | 685 | return IPython; |
|
684 | 686 | |
|
685 | 687 | }(IPython)); |
General Comments 0
You need to be logged in to leave comments.
Login now