##// END OF EJS Templates
dblclick/double click for humans
MinRK -
Show More
@@ -1,518 +1,518 b''
1 1 //----------------------------------------------------------------------------
2 2 // Copyright (C) 2008-2011 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 var IPython = (function (IPython) {
13 13 "use strict";
14 14
15 15 var utils = IPython.utils;
16 16
17 17 var OutputArea = function (selector, prompt_area) {
18 18 this.selector = selector;
19 19 this.wrapper = $(selector);
20 20 this.outputs = [];
21 21 this.collapsed = false;
22 22 this.scrolled = false;
23 23 this.clear_out_timeout = null;
24 24 if (prompt_area === undefined) {
25 25 this.prompt_area = true;
26 26 } else {
27 27 this.prompt_area = prompt_area;
28 28 };
29 29 this.create_elements();
30 30 this.style();
31 31 this.bind_events();
32 32 };
33 33
34 34 OutputArea.prototype.create_elements = function () {
35 35 this.element = $("<div/>");
36 36 this.collapse_button = $("<div/>");
37 37 this.prompt_overlay = $("<div/>");
38 38 this.wrapper.append(this.prompt_overlay);
39 39 this.wrapper.append(this.element);
40 40 this.wrapper.append(this.collapse_button);
41 41 };
42 42
43 43
44 44 OutputArea.prototype.style = function () {
45 45 this.collapse_button.hide();
46 46 this.prompt_overlay.hide();
47 47
48 48 this.wrapper.addClass('output_wrapper');
49 49 this.element.addClass('output vbox');
50 50
51 51 this.collapse_button.button();
52 52 this.collapse_button.addClass('output_collapsed vbox');
53 53 this.collapse_button.attr('title', 'click to expand outout');
54 54 this.collapse_button.html('. . .');
55 55
56 56 this.prompt_overlay.addClass('out_prompt_overlay prompt');
57 this.prompt_overlay.attr('title', 'click to expand outout; dblclick to hide output');
57 this.prompt_overlay.attr('title', 'click to expand outout; double click to hide output');
58 58
59 59 this.collapse();
60 60 };
61 61
62 62
63 63 OutputArea.prototype._should_scroll = function (lines) {
64 64 if (!lines) {
65 65 lines = 50;
66 66 }
67 67 // line-height from http://stackoverflow.com/questions/1185151
68 68 var fontSize = this.element.css('font-size');
69 69 var lineHeight = Math.floor(parseInt(fontSize.replace('px','')) * 1.5);
70 70
71 71 return (this.element.height() > lines * lineHeight);
72 72 };
73 73
74 74
75 75 OutputArea.prototype.bind_events = function () {
76 76 var that = this;
77 77 this.prompt_overlay.dblclick(function () { that.toggle_output(); });
78 78 this.prompt_overlay.click(function () { that.toggle_scroll(); });
79 79
80 80 this.element.resize(function () {
81 81 // maybe scroll output,
82 82 // if it's grown large enough and hasn't already been scrolled.
83 83 if ( !that.scrolled && that._should_scroll()) {
84 84 that.scroll_area();
85 85 }
86 86 });
87 87 this.collapse_button.click(function () {
88 88 that.expand();
89 89 });
90 90 this.collapse_button.hover(function () {
91 91 $(this).addClass("ui-state-hover");
92 92 }, function () {
93 93 $(this).removeClass("ui-state-hover");
94 94 });
95 95 };
96 96
97 97
98 98 OutputArea.prototype.collapse = function () {
99 99 if (!this.collapsed) {
100 100 this.element.hide();
101 101 this.prompt_overlay.hide();
102 102 if (this.element.html()){
103 103 this.collapse_button.show();
104 104 }
105 105 this.collapsed = true;
106 106 };
107 107 };
108 108
109 109
110 110 OutputArea.prototype.expand = function () {
111 111 if (this.collapsed) {
112 112 this.collapse_button.hide();
113 113 this.element.show();
114 114 this.prompt_overlay.show();
115 115 this.collapsed = false;
116 116 };
117 117 };
118 118
119 119
120 120 OutputArea.prototype.toggle_output = function () {
121 121 if (this.collapsed) {
122 122 this.expand();
123 123 } else {
124 124 this.collapse();
125 125 };
126 126 };
127 127
128 128
129 129 OutputArea.prototype.scroll_area = function () {
130 130 this.element.addClass('output_scroll');
131 this.prompt_overlay.attr('title', 'click to unscroll output; dblclick to hide');
131 this.prompt_overlay.attr('title', 'click to unscroll output; double click to hide');
132 132 this.scrolled = true;
133 133 };
134 134
135 135
136 136 OutputArea.prototype.unscroll_area = function () {
137 137 this.element.removeClass('output_scroll');
138 this.prompt_overlay.attr('title', 'click to scroll output; dblclick to hide');
138 this.prompt_overlay.attr('title', 'click to scroll output; double click to hide');
139 139 this.scrolled = false;
140 140 };
141 141
142 142
143 143 OutputArea.prototype.scroll_if_long = function (lines) {
144 144 if (this._should_scroll(lines)) {
145 145 // only allow scrolling long-enough output
146 146 this.scroll_area();
147 147 };
148 148 };
149 149
150 150
151 151 OutputArea.prototype.toggle_scroll = function () {
152 152 if (this.scrolled) {
153 153 this.unscroll_area();
154 154 } else {
155 155 // only allow scrolling long-enough output
156 156 this.scroll_if_long(20);
157 157 };
158 158 };
159 159
160 160
161 161 // typeset with MathJax if MathJax is available
162 162 OutputArea.prototype.typeset = function () {
163 163 if (window.MathJax){
164 164 MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
165 165 }
166 166 };
167 167
168 168
169 169 OutputArea.prototype.handle_output = function (msg_type, content) {
170 170 var json = {};
171 171 json.output_type = msg_type;
172 172 if (msg_type === "stream") {
173 173 json.text = content.data;
174 174 json.stream = content.name;
175 175 } else if (msg_type === "display_data") {
176 176 json = this.convert_mime_types(json, content.data);
177 177 } else if (msg_type === "pyout") {
178 178 json.prompt_number = content.execution_count;
179 179 json = this.convert_mime_types(json, content.data);
180 180 } else if (msg_type === "pyerr") {
181 181 json.ename = content.ename;
182 182 json.evalue = content.evalue;
183 183 json.traceback = content.traceback;
184 184 };
185 185 // append with dynamic=true
186 186 this.append_output(json, true);
187 187 };
188 188
189 189
190 190 OutputArea.prototype.convert_mime_types = function (json, data) {
191 191 if (data['text/plain'] !== undefined) {
192 192 json.text = data['text/plain'];
193 193 };
194 194 if (data['text/html'] !== undefined) {
195 195 json.html = data['text/html'];
196 196 };
197 197 if (data['image/svg+xml'] !== undefined) {
198 198 json.svg = data['image/svg+xml'];
199 199 };
200 200 if (data['image/png'] !== undefined) {
201 201 json.png = data['image/png'];
202 202 };
203 203 if (data['image/jpeg'] !== undefined) {
204 204 json.jpeg = data['image/jpeg'];
205 205 };
206 206 if (data['text/latex'] !== undefined) {
207 207 json.latex = data['text/latex'];
208 208 };
209 209 if (data['application/json'] !== undefined) {
210 210 json.json = data['application/json'];
211 211 };
212 212 if (data['application/javascript'] !== undefined) {
213 213 json.javascript = data['application/javascript'];
214 214 }
215 215 return json;
216 216 };
217 217
218 218
219 219 OutputArea.prototype.append_output = function (json, dynamic) {
220 220 // If dynamic is true, javascript output will be eval'd.
221 221 this.expand();
222 222 this.flush_clear_timeout();
223 223 if (json.output_type === 'pyout') {
224 224 this.append_pyout(json, dynamic);
225 225 } else if (json.output_type === 'pyerr') {
226 226 this.append_pyerr(json);
227 227 } else if (json.output_type === 'display_data') {
228 228 this.append_display_data(json, dynamic);
229 229 } else if (json.output_type === 'stream') {
230 230 this.append_stream(json);
231 231 };
232 232 this.outputs.push(json);
233 233 var that = this;
234 234 setTimeout(function(){that.element.trigger('resize');}, 100);
235 235 };
236 236
237 237
238 238 OutputArea.prototype.create_output_area = function () {
239 239 var oa = $("<div/>").addClass("hbox output_area");
240 240 if (this.prompt_area) {
241 241 oa.append($('<div/>').addClass('prompt'));
242 242 }
243 243 return oa;
244 244 };
245 245
246 246
247 247 OutputArea.prototype.append_pyout = function (json, dynamic) {
248 248 var n = json.prompt_number || ' ';
249 249 var toinsert = this.create_output_area();
250 250 if (this.prompt_area) {
251 251 toinsert.find('div.prompt').addClass('output_prompt').html('Out[' + n + ']:');
252 252 }
253 253 this.append_mime_type(json, toinsert, dynamic);
254 254 this.element.append(toinsert);
255 255 // If we just output latex, typeset it.
256 256 if ((json.latex !== undefined) || (json.html !== undefined)) {
257 257 this.typeset();
258 258 };
259 259 };
260 260
261 261
262 262 OutputArea.prototype.append_pyerr = function (json) {
263 263 var tb = json.traceback;
264 264 if (tb !== undefined && tb.length > 0) {
265 265 var s = '';
266 266 var len = tb.length;
267 267 for (var i=0; i<len; i++) {
268 268 s = s + tb[i] + '\n';
269 269 }
270 270 s = s + '\n';
271 271 var toinsert = this.create_output_area();
272 272 this.append_text(s, toinsert);
273 273 this.element.append(toinsert);
274 274 };
275 275 };
276 276
277 277
278 278 OutputArea.prototype.append_stream = function (json) {
279 279 // temporary fix: if stream undefined (json file written prior to this patch),
280 280 // default to most likely stdout:
281 281 if (json.stream == undefined){
282 282 json.stream = 'stdout';
283 283 }
284 284 var text = json.text;
285 285 var subclass = "output_"+json.stream;
286 286 if (this.outputs.length > 0){
287 287 // have at least one output to consider
288 288 var last = this.outputs[this.outputs.length-1];
289 289 if (last.output_type == 'stream' && json.stream == last.stream){
290 290 // latest output was in the same stream,
291 291 // so append directly into its pre tag
292 292 // escape ANSI & HTML specials:
293 293 var pre = this.element.find('div.'+subclass).last().find('pre');
294 294 var html = utils.fixCarriageReturn(
295 295 pre.html() + utils.fixConsole(text));
296 296 pre.html(html);
297 297 return;
298 298 }
299 299 }
300 300
301 301 if (!text.replace("\r", "")) {
302 302 // text is nothing (empty string, \r, etc.)
303 303 // so don't append any elements, which might add undesirable space
304 304 return;
305 305 }
306 306
307 307 // If we got here, attach a new div
308 308 var toinsert = this.create_output_area();
309 309 this.append_text(text, toinsert, "output_stream "+subclass);
310 310 this.element.append(toinsert);
311 311 };
312 312
313 313
314 314 OutputArea.prototype.append_display_data = function (json, dynamic) {
315 315 var toinsert = this.create_output_area();
316 316 this.append_mime_type(json, toinsert, dynamic);
317 317 this.element.append(toinsert);
318 318 // If we just output latex, typeset it.
319 319 if ( (json.latex !== undefined) || (json.html !== undefined) ) {
320 320 this.typeset();
321 321 };
322 322 };
323 323
324 324
325 325 OutputArea.prototype.append_mime_type = function (json, element, dynamic) {
326 326 if (json.javascript !== undefined && dynamic) {
327 327 this.append_javascript(json.javascript, element, dynamic);
328 328 } else if (json.html !== undefined) {
329 329 this.append_html(json.html, element);
330 330 } else if (json.latex !== undefined) {
331 331 this.append_latex(json.latex, element);
332 332 } else if (json.svg !== undefined) {
333 333 this.append_svg(json.svg, element);
334 334 } else if (json.png !== undefined) {
335 335 this.append_png(json.png, element);
336 336 } else if (json.jpeg !== undefined) {
337 337 this.append_jpeg(json.jpeg, element);
338 338 } else if (json.text !== undefined) {
339 339 this.append_text(json.text, element);
340 340 };
341 341 };
342 342
343 343
344 344 OutputArea.prototype.append_html = function (html, element) {
345 345 var toinsert = $("<div/>").addClass("box-flex1 output_subarea output_html rendered_html");
346 346 toinsert.append(html);
347 347 element.append(toinsert);
348 348 };
349 349
350 350
351 351 OutputArea.prototype.append_javascript = function (js, container) {
352 352 // We just eval the JS code, element appears in the local scope.
353 353 var element = $("<div/>").addClass("box-flex1 output_subarea");
354 354 container.append(element);
355 355 // Div for js shouldn't be drawn, as it will add empty height to the area.
356 356 container.hide();
357 357 // If the Javascript appends content to `element` that should be drawn, then
358 358 // it must also call `container.show()`.
359 359 eval(js);
360 360 }
361 361
362 362
363 363 OutputArea.prototype.append_text = function (data, element, extra_class) {
364 364 var toinsert = $("<div/>").addClass("box-flex1 output_subarea output_text");
365 365 // escape ANSI & HTML specials in plaintext:
366 366 data = utils.fixConsole(data);
367 367 data = utils.fixCarriageReturn(data);
368 368 if (extra_class){
369 369 toinsert.addClass(extra_class);
370 370 }
371 371 toinsert.append($("<pre/>").html(data));
372 372 element.append(toinsert);
373 373 };
374 374
375 375
376 376 OutputArea.prototype.append_svg = function (svg, element) {
377 377 var toinsert = $("<div/>").addClass("box-flex1 output_subarea output_svg");
378 378 toinsert.append(svg);
379 379 element.append(toinsert);
380 380 };
381 381
382 382
383 383 OutputArea.prototype.append_png = function (png, element) {
384 384 var toinsert = $("<div/>").addClass("box-flex1 output_subarea output_png");
385 385 var img = $("<img/>").attr('src','data:image/png;base64,'+png);
386 386 img.load(function () {
387 387 $(this).resizable({'aspectRatio': true, 'autoHide': true})
388 388 });
389 389 toinsert.append(img);
390 390 element.append(toinsert);
391 391 };
392 392
393 393
394 394 OutputArea.prototype.append_jpeg = function (jpeg, element) {
395 395 var toinsert = $("<div/>").addClass("box-flex1 output_subarea output_jpeg");
396 396 var img = $("<img/>").attr('src','data:image/jpeg;base64,'+jpeg);
397 397 img.load(function () {
398 398 $(this).resizable({'aspectRatio': true, 'autoHide': true})
399 399 });
400 400 toinsert.append(img);
401 401 element.append(toinsert);
402 402 };
403 403
404 404
405 405 OutputArea.prototype.append_latex = function (latex, element) {
406 406 // This method cannot do the typesetting because the latex first has to
407 407 // be on the page.
408 408 var toinsert = $("<div/>").addClass("box-flex1 output_subarea output_latex");
409 409 toinsert.append(latex);
410 410 element.append(toinsert);
411 411 };
412 412
413 413
414 414 OutputArea.prototype.handle_clear_output = function (content) {
415 415 this.clear_output(content.stdout, content.stderr, content.other);
416 416 }
417 417
418 418
419 419 OutputArea.prototype.clear_output = function (stdout, stderr, other) {
420 420 var that = this;
421 421 if (this.clear_out_timeout != null){
422 422 // fire previous pending clear *immediately*
423 423 clearTimeout(this.clear_out_timeout);
424 424 this.clear_out_timeout = null;
425 425 this.clear_output_callback(this._clear_stdout, this._clear_stderr, this._clear_other);
426 426 }
427 427 // store flags for flushing the timeout
428 428 this._clear_stdout = stdout;
429 429 this._clear_stderr = stderr;
430 430 this._clear_other = other;
431 431 this.clear_out_timeout = setTimeout(function() {
432 432 // really clear timeout only after a short delay
433 433 // this reduces flicker in 'clear_output; print' cases
434 434 that.clear_out_timeout = null;
435 435 that._clear_stdout = that._clear_stderr = that._clear_other = null;
436 436 that.clear_output_callback(stdout, stderr, other);
437 437 }, 500
438 438 );
439 439 };
440 440
441 441
442 442 OutputArea.prototype.clear_output_callback = function (stdout, stderr, other) {
443 443 var output_div = this.element;
444 444
445 445 if (stdout && stderr && other){
446 446 // clear all, no need for logic
447 447 output_div.html("");
448 448 this.outputs = [];
449 449 this.unscroll_area();
450 450 return;
451 451 }
452 452 // remove html output
453 453 // each output_subarea that has an identifying class is in an output_area
454 454 // which is the element to be removed.
455 455 if (stdout) {
456 456 output_div.find("div.output_stdout").parent().remove();
457 457 }
458 458 if (stderr) {
459 459 output_div.find("div.output_stderr").parent().remove();
460 460 }
461 461 if (other) {
462 462 output_div.find("div.output_subarea").not("div.output_stderr").not("div.output_stdout").parent().remove();
463 463 }
464 464 this.unscroll_area();
465 465
466 466 // remove cleared outputs from JSON list:
467 467 for (var i = this.outputs.length - 1; i >= 0; i--) {
468 468 var out = this.outputs[i];
469 469 var output_type = out.output_type;
470 470 if (output_type == "display_data" && other) {
471 471 this.outputs.splice(i,1);
472 472 } else if (output_type == "stream") {
473 473 if (stdout && out.stream == "stdout") {
474 474 this.outputs.splice(i,1);
475 475 } else if (stderr && out.stream == "stderr") {
476 476 this.outputs.splice(i,1);
477 477 }
478 478 }
479 479 }
480 480 };
481 481
482 482
483 483 OutputArea.prototype.flush_clear_timeout = function() {
484 484 var output_div = this.element;
485 485 if (this.clear_out_timeout){
486 486 clearTimeout(this.clear_out_timeout);
487 487 this.clear_out_timeout = null;
488 488 this.clear_output_callback(this._clear_stdout, this._clear_stderr, this._clear_other);
489 489 };
490 490 }
491 491
492 492
493 493 // JSON serialization
494 494
495 495 OutputArea.prototype.fromJSON = function (outputs) {
496 496 var len = outputs.length;
497 497 for (var i=0; i<len; i++) {
498 498 // append with dynamic=false.
499 499 this.append_output(outputs[i], false);
500 500 };
501 501 };
502 502
503 503
504 504 OutputArea.prototype.toJSON = function () {
505 505 var outputs = [];
506 506 var len = this.outputs.length;
507 507 for (var i=0; i<len; i++) {
508 508 outputs[i] = this.outputs[i];
509 509 };
510 510 return outputs;
511 511 };
512 512
513 513
514 514 IPython.OutputArea = OutputArea;
515 515
516 516 return IPython;
517 517
518 518 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now