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