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