##// END OF EJS Templates
unify key renaming for to/from json short keys
Paul Ivanov -
Show More
@@ -1,796 +1,782 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_queued = null;
34 this.clear_queued = 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');
60 this.element.addClass('output');
61
61
62 this.collapse_button.addClass("btn output_collapsed");
62 this.collapse_button.addClass("btn output_collapsed");
63 this.collapse_button.attr('title', 'click to expand output');
63 this.collapse_button.attr('title', 'click to expand output');
64 this.collapse_button.html('. . .');
64 this.collapse_button.html('. . .');
65
65
66 this.prompt_overlay.addClass('out_prompt_overlay prompt');
66 this.prompt_overlay.addClass('out_prompt_overlay prompt');
67 this.prompt_overlay.attr('title', 'click to expand output; double click to hide output');
67 this.prompt_overlay.attr('title', 'click to expand output; double click to hide output');
68
68
69 this.collapse();
69 this.collapse();
70 };
70 };
71
71
72 /**
72 /**
73 * Should the OutputArea scroll?
73 * Should the OutputArea scroll?
74 * Returns whether the height (in lines) exceeds a threshold.
74 * Returns whether the height (in lines) exceeds a threshold.
75 *
75 *
76 * @private
76 * @private
77 * @method _should_scroll
77 * @method _should_scroll
78 * @param [lines=100]{Integer}
78 * @param [lines=100]{Integer}
79 * @return {Bool}
79 * @return {Bool}
80 *
80 *
81 */
81 */
82 OutputArea.prototype._should_scroll = function (lines) {
82 OutputArea.prototype._should_scroll = function (lines) {
83 if (lines <=0 ){ return }
83 if (lines <=0 ){ return }
84 if (!lines) {
84 if (!lines) {
85 lines = 100;
85 lines = 100;
86 }
86 }
87 // line-height from http://stackoverflow.com/questions/1185151
87 // line-height from http://stackoverflow.com/questions/1185151
88 var fontSize = this.element.css('font-size');
88 var fontSize = this.element.css('font-size');
89 var lineHeight = Math.floor(parseInt(fontSize.replace('px','')) * 1.5);
89 var lineHeight = Math.floor(parseInt(fontSize.replace('px','')) * 1.5);
90
90
91 return (this.element.height() > lines * lineHeight);
91 return (this.element.height() > lines * lineHeight);
92 };
92 };
93
93
94
94
95 OutputArea.prototype.bind_events = function () {
95 OutputArea.prototype.bind_events = function () {
96 var that = this;
96 var that = this;
97 this.prompt_overlay.dblclick(function () { that.toggle_output(); });
97 this.prompt_overlay.dblclick(function () { that.toggle_output(); });
98 this.prompt_overlay.click(function () { that.toggle_scroll(); });
98 this.prompt_overlay.click(function () { that.toggle_scroll(); });
99
99
100 this.element.resize(function () {
100 this.element.resize(function () {
101 // FIXME: Firefox on Linux misbehaves, so automatic scrolling is disabled
101 // FIXME: Firefox on Linux misbehaves, so automatic scrolling is disabled
102 if ( IPython.utils.browser[0] === "Firefox" ) {
102 if ( IPython.utils.browser[0] === "Firefox" ) {
103 return;
103 return;
104 }
104 }
105 // maybe scroll output,
105 // maybe scroll output,
106 // if it's grown large enough and hasn't already been scrolled.
106 // if it's grown large enough and hasn't already been scrolled.
107 if ( !that.scrolled && that._should_scroll(OutputArea.auto_scroll_threshold)) {
107 if ( !that.scrolled && that._should_scroll(OutputArea.auto_scroll_threshold)) {
108 that.scroll_area();
108 that.scroll_area();
109 }
109 }
110 });
110 });
111 this.collapse_button.click(function () {
111 this.collapse_button.click(function () {
112 that.expand();
112 that.expand();
113 });
113 });
114 };
114 };
115
115
116
116
117 OutputArea.prototype.collapse = function () {
117 OutputArea.prototype.collapse = function () {
118 if (!this.collapsed) {
118 if (!this.collapsed) {
119 this.element.hide();
119 this.element.hide();
120 this.prompt_overlay.hide();
120 this.prompt_overlay.hide();
121 if (this.element.html()){
121 if (this.element.html()){
122 this.collapse_button.show();
122 this.collapse_button.show();
123 }
123 }
124 this.collapsed = true;
124 this.collapsed = true;
125 }
125 }
126 };
126 };
127
127
128
128
129 OutputArea.prototype.expand = function () {
129 OutputArea.prototype.expand = function () {
130 if (this.collapsed) {
130 if (this.collapsed) {
131 this.collapse_button.hide();
131 this.collapse_button.hide();
132 this.element.show();
132 this.element.show();
133 this.prompt_overlay.show();
133 this.prompt_overlay.show();
134 this.collapsed = false;
134 this.collapsed = false;
135 }
135 }
136 };
136 };
137
137
138
138
139 OutputArea.prototype.toggle_output = function () {
139 OutputArea.prototype.toggle_output = function () {
140 if (this.collapsed) {
140 if (this.collapsed) {
141 this.expand();
141 this.expand();
142 } else {
142 } else {
143 this.collapse();
143 this.collapse();
144 }
144 }
145 };
145 };
146
146
147
147
148 OutputArea.prototype.scroll_area = function () {
148 OutputArea.prototype.scroll_area = function () {
149 this.element.addClass('output_scroll');
149 this.element.addClass('output_scroll');
150 this.prompt_overlay.attr('title', 'click to unscroll output; double click to hide');
150 this.prompt_overlay.attr('title', 'click to unscroll output; double click to hide');
151 this.scrolled = true;
151 this.scrolled = true;
152 };
152 };
153
153
154
154
155 OutputArea.prototype.unscroll_area = function () {
155 OutputArea.prototype.unscroll_area = function () {
156 this.element.removeClass('output_scroll');
156 this.element.removeClass('output_scroll');
157 this.prompt_overlay.attr('title', 'click to scroll output; double click to hide');
157 this.prompt_overlay.attr('title', 'click to scroll output; double click to hide');
158 this.scrolled = false;
158 this.scrolled = false;
159 };
159 };
160
160
161 /**
161 /**
162 * Threshold to trigger autoscroll when the OutputArea is resized,
162 * Threshold to trigger autoscroll when the OutputArea is resized,
163 * typically when new outputs are added.
163 * typically when new outputs are added.
164 *
164 *
165 * Behavior is undefined if autoscroll is lower than minimum_scroll_threshold,
165 * Behavior is undefined if autoscroll is lower than minimum_scroll_threshold,
166 * unless it is < 0, in which case autoscroll will never be triggered
166 * unless it is < 0, in which case autoscroll will never be triggered
167 *
167 *
168 * @property auto_scroll_threshold
168 * @property auto_scroll_threshold
169 * @type Number
169 * @type Number
170 * @default 100
170 * @default 100
171 *
171 *
172 **/
172 **/
173 OutputArea.auto_scroll_threshold = 100;
173 OutputArea.auto_scroll_threshold = 100;
174
174
175
175
176 /**
176 /**
177 * Lower limit (in lines) for OutputArea to be made scrollable. OutputAreas
177 * Lower limit (in lines) for OutputArea to be made scrollable. OutputAreas
178 * shorter than this are never scrolled.
178 * shorter than this are never scrolled.
179 *
179 *
180 * @property minimum_scroll_threshold
180 * @property minimum_scroll_threshold
181 * @type Number
181 * @type Number
182 * @default 20
182 * @default 20
183 *
183 *
184 **/
184 **/
185 OutputArea.minimum_scroll_threshold = 20;
185 OutputArea.minimum_scroll_threshold = 20;
186
186
187
187
188 /**
188 /**
189 *
189 *
190 * Scroll OutputArea if height supperior than a threshold (in lines).
190 * Scroll OutputArea if height supperior than a threshold (in lines).
191 *
191 *
192 * Threshold is a maximum number of lines. If unspecified, defaults to
192 * Threshold is a maximum number of lines. If unspecified, defaults to
193 * OutputArea.minimum_scroll_threshold.
193 * OutputArea.minimum_scroll_threshold.
194 *
194 *
195 * Negative threshold will prevent the OutputArea from ever scrolling.
195 * Negative threshold will prevent the OutputArea from ever scrolling.
196 *
196 *
197 * @method scroll_if_long
197 * @method scroll_if_long
198 *
198 *
199 * @param [lines=20]{Number} Default to 20 if not set,
199 * @param [lines=20]{Number} Default to 20 if not set,
200 * behavior undefined for value of `0`.
200 * behavior undefined for value of `0`.
201 *
201 *
202 **/
202 **/
203 OutputArea.prototype.scroll_if_long = function (lines) {
203 OutputArea.prototype.scroll_if_long = function (lines) {
204 var n = lines | OutputArea.minimum_scroll_threshold;
204 var n = lines | OutputArea.minimum_scroll_threshold;
205 if(n <= 0){
205 if(n <= 0){
206 return
206 return
207 }
207 }
208
208
209 if (this._should_scroll(n)) {
209 if (this._should_scroll(n)) {
210 // only allow scrolling long-enough output
210 // only allow scrolling long-enough output
211 this.scroll_area();
211 this.scroll_area();
212 }
212 }
213 };
213 };
214
214
215
215
216 OutputArea.prototype.toggle_scroll = function () {
216 OutputArea.prototype.toggle_scroll = function () {
217 if (this.scrolled) {
217 if (this.scrolled) {
218 this.unscroll_area();
218 this.unscroll_area();
219 } else {
219 } else {
220 // only allow scrolling long-enough output
220 // only allow scrolling long-enough output
221 this.scroll_if_long();
221 this.scroll_if_long();
222 }
222 }
223 };
223 };
224
224
225
225
226 // typeset with MathJax if MathJax is available
226 // typeset with MathJax if MathJax is available
227 OutputArea.prototype.typeset = function () {
227 OutputArea.prototype.typeset = function () {
228 if (window.MathJax){
228 if (window.MathJax){
229 MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
229 MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
230 }
230 }
231 };
231 };
232
232
233
233
234 OutputArea.prototype.handle_output = function (msg) {
234 OutputArea.prototype.handle_output = function (msg) {
235 var json = {};
235 var json = {};
236 var msg_type = json.output_type = msg.header.msg_type;
236 var msg_type = json.output_type = msg.header.msg_type;
237 var content = msg.content;
237 var content = msg.content;
238 if (msg_type === "stream") {
238 if (msg_type === "stream") {
239 json.text = content.data;
239 json.text = content.data;
240 json.stream = content.name;
240 json.stream = content.name;
241 } else if (msg_type === "display_data") {
241 } else if (msg_type === "display_data") {
242 json = content.data
242 json = content.data
243 json.output_type = msg_type
243 json.output_type = msg_type
244 json.metadata = content.metadata
244 json.metadata = content.metadata
245 } else if (msg_type === "pyout") {
245 } else if (msg_type === "pyout") {
246 json = content.data
246 json = content.data
247 json.output_type = msg_type
247 json.output_type = msg_type
248 json.metadata = content.metadata
248 json.metadata = content.metadata
249 json.prompt_number = content.execution_count;
249 json.prompt_number = content.execution_count;
250 } else if (msg_type === "pyerr") {
250 } else if (msg_type === "pyerr") {
251 json.ename = content.ename;
251 json.ename = content.ename;
252 json.evalue = content.evalue;
252 json.evalue = content.evalue;
253 json.traceback = content.traceback;
253 json.traceback = content.traceback;
254 }
254 }
255 // append with dynamic=true
255 // append with dynamic=true
256 this.append_output(json, true);
256 this.append_output(json, true);
257 };
257 };
258
258
259 OutputArea.mime_map = {
259 OutputArea.mime_map = {
260 "text/plain" : "text",
260 "text/plain" : "text",
261 "text/html" : "html",
261 "text/html" : "html",
262 "image/svg+xml" : "svg",
262 "image/svg+xml" : "svg",
263 "image/png" : "png",
263 "image/png" : "png",
264 "image/jpeg" : "jpeg",
264 "image/jpeg" : "jpeg",
265 "text/latex" : "latex",
265 "text/latex" : "latex",
266 "application/json" : "json",
266 "application/json" : "json",
267 "application/javascript" : "javascript",
267 "application/javascript" : "javascript",
268 };
268 };
269
269
270 OutputArea.mime_map_r = {
270 OutputArea.mime_map_r = {
271 "text" : "text/plain",
271 "text" : "text/plain",
272 "html" : "text/html",
272 "html" : "text/html",
273 "svg" : "image/svg+xml",
273 "svg" : "image/svg+xml",
274 "png" : "image/png",
274 "png" : "image/png",
275 "jpeg" : "image/jpeg",
275 "jpeg" : "image/jpeg",
276 "latex" : "text/latex",
276 "latex" : "text/latex",
277 "json" : "application/json",
277 "json" : "application/json",
278 "javascript" : "application/javascript",
278 "javascript" : "application/javascript",
279 };
279 };
280
280
281 OutputArea.prototype.convert_mime_types = function (data) {
281 OutputArea.prototype.rename_keys = function (data, key_map) {
282 for (var key in data) {
282 for (var key in data) {
283 var json_key = OutputArea.mime_map[key] || key;
283 var new_key = key_map[key] || key;
284 console.log("converting ", key, "to", json_key)
284 if (new_key !== key) {
285 if (json_key !== key) {
286 // move mime-type keys to short name
285 // move mime-type keys to short name
287 console.log("converting ", key, "to", json_key)
286 data[new_key] = data[key];
288 data[json_key] = data[key];
289 delete data[key];
287 delete data[key];
290 }
288 }
291 }
289 }
292 return data;
290 return data;
293 };
291 };
294
292
295 OutputArea.prototype.convert_mime_types_r = function (data) {
296 for (var key in data) {
297 var mkey = OutputArea.mime_map_r[key] || key;
298 if (mkey !== key) {
299 // move short keys to mime-type keys
300 data[mkey] = data[key];
301 delete data[key];
302 }
303 }
304 return data;
305 };
306
307
293
308 OutputArea.prototype.append_output = function (json, dynamic) {
294 OutputArea.prototype.append_output = function (json, dynamic) {
309 // If dynamic is true, javascript output will be eval'd.
295 // If dynamic is true, javascript output will be eval'd.
310 this.expand();
296 this.expand();
311 // Clear the output if clear is queued.
297 // Clear the output if clear is queued.
312 var needs_height_reset = false;
298 var needs_height_reset = false;
313 if (this.clear_queued) {
299 if (this.clear_queued) {
314 this.clear_output(false);
300 this.clear_output(false);
315 needs_height_reset = true;
301 needs_height_reset = true;
316 }
302 }
317
303
318 if (json.output_type === 'pyout') {
304 if (json.output_type === 'pyout') {
319 this.append_pyout(json, dynamic);
305 this.append_pyout(json, dynamic);
320 } else if (json.output_type === 'pyerr') {
306 } else if (json.output_type === 'pyerr') {
321 this.append_pyerr(json);
307 this.append_pyerr(json);
322 } else if (json.output_type === 'display_data') {
308 } else if (json.output_type === 'display_data') {
323 this.append_display_data(json, dynamic);
309 this.append_display_data(json, dynamic);
324 } else if (json.output_type === 'stream') {
310 } else if (json.output_type === 'stream') {
325 this.append_stream(json);
311 this.append_stream(json);
326 }
312 }
327 this.outputs.push(json);
313 this.outputs.push(json);
328
314
329 // Only reset the height to automatic if the height is currently
315 // Only reset the height to automatic if the height is currently
330 // fixed (done by wait=True flag on clear_output).
316 // fixed (done by wait=True flag on clear_output).
331 if (needs_height_reset) {
317 if (needs_height_reset) {
332 this.element.height('');
318 this.element.height('');
333 }
319 }
334
320
335 var that = this;
321 var that = this;
336 setTimeout(function(){that.element.trigger('resize');}, 100);
322 setTimeout(function(){that.element.trigger('resize');}, 100);
337 };
323 };
338
324
339
325
340 OutputArea.prototype.create_output_area = function () {
326 OutputArea.prototype.create_output_area = function () {
341 var oa = $("<div/>").addClass("output_area");
327 var oa = $("<div/>").addClass("output_area");
342 if (this.prompt_area) {
328 if (this.prompt_area) {
343 oa.append($('<div/>').addClass('prompt'));
329 oa.append($('<div/>').addClass('prompt'));
344 }
330 }
345 return oa;
331 return oa;
346 };
332 };
347
333
348
334
349 function _get_metadata_key(metadata, key, mime) {
335 function _get_metadata_key(metadata, key, mime) {
350 var mime_md = metadata[mime];
336 var mime_md = metadata[mime];
351 // mime-specific higher priority
337 // mime-specific higher priority
352 if (mime_md && mime_md[key] !== undefined) {
338 if (mime_md && mime_md[key] !== undefined) {
353 console.log("got" + key + " "+ mime_md[key]);
339 console.log("got" + key + " "+ mime_md[key]);
354 return mime_md[key];
340 return mime_md[key];
355 }
341 }
356 // fallback on global
342 // fallback on global
357 return metadata[key];
343 return metadata[key];
358 }
344 }
359
345
360 OutputArea.prototype.create_output_subarea = function(md, classes, mime) {
346 OutputArea.prototype.create_output_subarea = function(md, classes, mime) {
361 var subarea = $('<div/>').addClass('output_subarea').addClass(classes);
347 var subarea = $('<div/>').addClass('output_subarea').addClass(classes);
362 if (_get_metadata_key(md, 'isolated', mime)) {
348 if (_get_metadata_key(md, 'isolated', mime)) {
363 // Create an iframe to isolate the subarea from the rest of the
349 // Create an iframe to isolate the subarea from the rest of the
364 // document
350 // document
365 var iframe = $('<iframe/>').addClass('box-flex1');
351 var iframe = $('<iframe/>').addClass('box-flex1');
366 iframe.css({'height':1, 'width':'100%', 'display':'block'});
352 iframe.css({'height':1, 'width':'100%', 'display':'block'});
367 iframe.attr('frameborder', 0);
353 iframe.attr('frameborder', 0);
368 iframe.attr('scrolling', 'auto');
354 iframe.attr('scrolling', 'auto');
369
355
370 // Once the iframe is loaded, the subarea is dynamically inserted
356 // Once the iframe is loaded, the subarea is dynamically inserted
371 iframe.on('load', function() {
357 iframe.on('load', function() {
372 // Workaround needed by Firefox, to properly render svg inside
358 // Workaround needed by Firefox, to properly render svg inside
373 // iframes, see http://stackoverflow.com/questions/10177190/
359 // iframes, see http://stackoverflow.com/questions/10177190/
374 // svg-dynamically-added-to-iframe-does-not-render-correctly
360 // svg-dynamically-added-to-iframe-does-not-render-correctly
375 this.contentDocument.open();
361 this.contentDocument.open();
376
362
377 // Insert the subarea into the iframe
363 // Insert the subarea into the iframe
378 // We must directly write the html. When using Jquery's append
364 // We must directly write the html. When using Jquery's append
379 // method, javascript is evaluated in the parent document and
365 // method, javascript is evaluated in the parent document and
380 // not in the iframe document.
366 // not in the iframe document.
381 this.contentDocument.write(subarea.html());
367 this.contentDocument.write(subarea.html());
382
368
383 this.contentDocument.close();
369 this.contentDocument.close();
384
370
385 var body = this.contentDocument.body;
371 var body = this.contentDocument.body;
386 // Adjust the iframe height automatically
372 // Adjust the iframe height automatically
387 iframe.height(body.scrollHeight + 'px');
373 iframe.height(body.scrollHeight + 'px');
388 });
374 });
389
375
390 // Elements should be appended to the inner subarea and not to the
376 // Elements should be appended to the inner subarea and not to the
391 // iframe
377 // iframe
392 iframe.append = function(that) {
378 iframe.append = function(that) {
393 subarea.append(that);
379 subarea.append(that);
394 };
380 };
395
381
396 return iframe;
382 return iframe;
397 } else {
383 } else {
398 return subarea;
384 return subarea;
399 }
385 }
400 }
386 }
401
387
402
388
403 OutputArea.prototype._append_javascript_error = function (err, element) {
389 OutputArea.prototype._append_javascript_error = function (err, element) {
404 // display a message when a javascript error occurs in display output
390 // display a message when a javascript error occurs in display output
405 var msg = "Javascript error adding output!"
391 var msg = "Javascript error adding output!"
406 if ( element === undefined ) return;
392 if ( element === undefined ) return;
407 element.append(
393 element.append(
408 $('<div/>').html(msg + "<br/>" +
394 $('<div/>').html(msg + "<br/>" +
409 err.toString() +
395 err.toString() +
410 '<br/>See your browser Javascript console for more details.'
396 '<br/>See your browser Javascript console for more details.'
411 ).addClass('js-error')
397 ).addClass('js-error')
412 );
398 );
413 };
399 };
414
400
415 OutputArea.prototype._safe_append = function (toinsert) {
401 OutputArea.prototype._safe_append = function (toinsert) {
416 // safely append an item to the document
402 // safely append an item to the document
417 // this is an object created by user code,
403 // this is an object created by user code,
418 // and may have errors, which should not be raised
404 // and may have errors, which should not be raised
419 // under any circumstances.
405 // under any circumstances.
420 try {
406 try {
421 this.element.append(toinsert);
407 this.element.append(toinsert);
422 } catch(err) {
408 } catch(err) {
423 console.log(err);
409 console.log(err);
424 // Create an actual output_area and output_subarea, which creates
410 // Create an actual output_area and output_subarea, which creates
425 // the prompt area and the proper indentation.
411 // the prompt area and the proper indentation.
426 var toinsert = this.create_output_area();
412 var toinsert = this.create_output_area();
427 var subarea = $('<div/>').addClass('output_subarea');
413 var subarea = $('<div/>').addClass('output_subarea');
428 toinsert.append(subarea);
414 toinsert.append(subarea);
429 this._append_javascript_error(err, subarea);
415 this._append_javascript_error(err, subarea);
430 this.element.append(toinsert);
416 this.element.append(toinsert);
431 }
417 }
432 };
418 };
433
419
434
420
435 OutputArea.prototype.append_pyout = function (json, dynamic) {
421 OutputArea.prototype.append_pyout = function (json, dynamic) {
436 var n = json.prompt_number || ' ';
422 var n = json.prompt_number || ' ';
437 var toinsert = this.create_output_area();
423 var toinsert = this.create_output_area();
438 if (this.prompt_area) {
424 if (this.prompt_area) {
439 toinsert.find('div.prompt').addClass('output_prompt').html('Out[' + n + ']:');
425 toinsert.find('div.prompt').addClass('output_prompt').html('Out[' + n + ']:');
440 }
426 }
441 this.append_mime_type(json, toinsert, dynamic);
427 this.append_mime_type(json, toinsert, dynamic);
442 this._safe_append(toinsert);
428 this._safe_append(toinsert);
443 // If we just output latex, typeset it.
429 // If we just output latex, typeset it.
444 if ((json.latex !== undefined) || (json.html !== undefined)) {
430 if ((json.latex !== undefined) || (json.html !== undefined)) {
445 this.typeset();
431 this.typeset();
446 }
432 }
447 };
433 };
448
434
449
435
450 OutputArea.prototype.append_pyerr = function (json) {
436 OutputArea.prototype.append_pyerr = function (json) {
451 var tb = json.traceback;
437 var tb = json.traceback;
452 if (tb !== undefined && tb.length > 0) {
438 if (tb !== undefined && tb.length > 0) {
453 var s = '';
439 var s = '';
454 var len = tb.length;
440 var len = tb.length;
455 for (var i=0; i<len; i++) {
441 for (var i=0; i<len; i++) {
456 s = s + tb[i] + '\n';
442 s = s + tb[i] + '\n';
457 }
443 }
458 s = s + '\n';
444 s = s + '\n';
459 var toinsert = this.create_output_area();
445 var toinsert = this.create_output_area();
460 this.append_text(s, {}, toinsert);
446 this.append_text(s, {}, toinsert);
461 this._safe_append(toinsert);
447 this._safe_append(toinsert);
462 }
448 }
463 };
449 };
464
450
465
451
466 OutputArea.prototype.append_stream = function (json) {
452 OutputArea.prototype.append_stream = function (json) {
467 // temporary fix: if stream undefined (json file written prior to this patch),
453 // temporary fix: if stream undefined (json file written prior to this patch),
468 // default to most likely stdout:
454 // default to most likely stdout:
469 if (json.stream == undefined){
455 if (json.stream == undefined){
470 json.stream = 'stdout';
456 json.stream = 'stdout';
471 }
457 }
472 var text = json.text;
458 var text = json.text;
473 var subclass = "output_"+json.stream;
459 var subclass = "output_"+json.stream;
474 if (this.outputs.length > 0){
460 if (this.outputs.length > 0){
475 // have at least one output to consider
461 // have at least one output to consider
476 var last = this.outputs[this.outputs.length-1];
462 var last = this.outputs[this.outputs.length-1];
477 if (last.output_type == 'stream' && json.stream == last.stream){
463 if (last.output_type == 'stream' && json.stream == last.stream){
478 // latest output was in the same stream,
464 // latest output was in the same stream,
479 // so append directly into its pre tag
465 // so append directly into its pre tag
480 // escape ANSI & HTML specials:
466 // escape ANSI & HTML specials:
481 var pre = this.element.find('div.'+subclass).last().find('pre');
467 var pre = this.element.find('div.'+subclass).last().find('pre');
482 var html = utils.fixCarriageReturn(
468 var html = utils.fixCarriageReturn(
483 pre.html() + utils.fixConsole(text));
469 pre.html() + utils.fixConsole(text));
484 pre.html(html);
470 pre.html(html);
485 return;
471 return;
486 }
472 }
487 }
473 }
488
474
489 if (!text.replace("\r", "")) {
475 if (!text.replace("\r", "")) {
490 // text is nothing (empty string, \r, etc.)
476 // text is nothing (empty string, \r, etc.)
491 // so don't append any elements, which might add undesirable space
477 // so don't append any elements, which might add undesirable space
492 return;
478 return;
493 }
479 }
494
480
495 // If we got here, attach a new div
481 // If we got here, attach a new div
496 var toinsert = this.create_output_area();
482 var toinsert = this.create_output_area();
497 this.append_text(text, {}, toinsert, "output_stream "+subclass);
483 this.append_text(text, {}, toinsert, "output_stream "+subclass);
498 this._safe_append(toinsert);
484 this._safe_append(toinsert);
499 };
485 };
500
486
501
487
502 OutputArea.prototype.append_display_data = function (json, dynamic) {
488 OutputArea.prototype.append_display_data = function (json, dynamic) {
503 var toinsert = this.create_output_area();
489 var toinsert = this.create_output_area();
504 if (this.append_mime_type(json, toinsert, dynamic)) {
490 if (this.append_mime_type(json, toinsert, dynamic)) {
505 this._safe_append(toinsert);
491 this._safe_append(toinsert);
506 // If we just output latex, typeset it.
492 // If we just output latex, typeset it.
507 if ( (json.latex !== undefined) || (json.html !== undefined) ) {
493 if ( (json.latex !== undefined) || (json.html !== undefined) ) {
508 this.typeset();
494 this.typeset();
509 }
495 }
510 }
496 }
511 };
497 };
512
498
513 OutputArea.display_order = [
499 OutputArea.display_order = [
514 'application/javascript',
500 'application/javascript',
515 'text/html',
501 'text/html',
516 'text/latex',
502 'text/latex',
517 'image/svg+xml',
503 'image/svg+xml',
518 'image/png',
504 'image/png',
519 'image/jpeg',
505 'image/jpeg',
520 'text/plain'
506 'text/plain'
521 ];
507 ];
522
508
523 OutputArea.prototype.append_mime_type = function (json, element, dynamic) {
509 OutputArea.prototype.append_mime_type = function (json, element, dynamic) {
524
510
525 for(var type_i in OutputArea.display_order){
511 for(var type_i in OutputArea.display_order){
526 var type = OutputArea.display_order[type_i];
512 var type = OutputArea.display_order[type_i];
527 if(json[type] != undefined ){
513 if(json[type] != undefined ){
528 var md = {};
514 var md = {};
529 if (json.metadata) {
515 if (json.metadata) {
530 md = json.metadata;
516 md = json.metadata;
531 }
517 }
532 if(type == 'javascript'){
518 if(type == 'javascript'){
533 if (dynamic) {
519 if (dynamic) {
534 this.append_javascript(json.javascript, md, element, dynamic);
520 this.append_javascript(json.javascript, md, element, dynamic);
535 return true;
521 return true;
536 }
522 }
537 } else {
523 } else {
538 var old_name = OutputArea.mime_map[type]
524 var old_name = OutputArea.mime_map[type]
539 this['append_'+old_name](json[type], md, element, type);
525 this['append_'+old_name](json[type], md, element, type);
540 return true;
526 return true;
541 }
527 }
542 return false;
528 return false;
543 }
529 }
544 }
530 }
545 return false;
531 return false;
546 };
532 };
547
533
548
534
549 OutputArea.prototype.append_html = function (html, md, element, type) {
535 OutputArea.prototype.append_html = function (html, md, element, type) {
550 var toinsert = this.create_output_subarea(md, "output_html rendered_html", type);
536 var toinsert = this.create_output_subarea(md, "output_html rendered_html", type);
551 IPython.keyboard_manager.register_events(toinsert);
537 IPython.keyboard_manager.register_events(toinsert);
552 toinsert.append(html);
538 toinsert.append(html);
553 element.append(toinsert);
539 element.append(toinsert);
554 };
540 };
555
541
556
542
557 OutputArea.prototype.append_javascript = function (js, md, container, type) {
543 OutputArea.prototype.append_javascript = function (js, md, container, type) {
558 // We just eval the JS code, element appears in the local scope.
544 // We just eval the JS code, element appears in the local scope.
559 var element = this.create_output_subarea(md, "output_javascript", type);
545 var element = this.create_output_subarea(md, "output_javascript", type);
560 IPython.keyboard_manager.register_events(element);
546 IPython.keyboard_manager.register_events(element);
561 container.append(element);
547 container.append(element);
562 try {
548 try {
563 eval(js);
549 eval(js);
564 } catch(err) {
550 } catch(err) {
565 console.log(err);
551 console.log(err);
566 this._append_javascript_error(err, element);
552 this._append_javascript_error(err, element);
567 }
553 }
568 };
554 };
569
555
570
556
571 OutputArea.prototype.append_text = function (data, md, element, extra_class, type) {
557 OutputArea.prototype.append_text = function (data, md, element, extra_class, type) {
572 var toinsert = this.create_output_subarea(md, "output_text", type);
558 var toinsert = this.create_output_subarea(md, "output_text", type);
573 // escape ANSI & HTML specials in plaintext:
559 // escape ANSI & HTML specials in plaintext:
574 data = utils.fixConsole(data);
560 data = utils.fixConsole(data);
575 data = utils.fixCarriageReturn(data);
561 data = utils.fixCarriageReturn(data);
576 data = utils.autoLinkUrls(data);
562 data = utils.autoLinkUrls(data);
577 if (extra_class){
563 if (extra_class){
578 toinsert.addClass(extra_class);
564 toinsert.addClass(extra_class);
579 }
565 }
580 toinsert.append($("<pre/>").html(data));
566 toinsert.append($("<pre/>").html(data));
581 element.append(toinsert);
567 element.append(toinsert);
582 };
568 };
583
569
584
570
585 OutputArea.prototype.append_svg = function (svg, md, element, type) {
571 OutputArea.prototype.append_svg = function (svg, md, element, type) {
586 var toinsert = this.create_output_subarea(md, "output_svg", type);
572 var toinsert = this.create_output_subarea(md, "output_svg", type);
587 toinsert.append(svg);
573 toinsert.append(svg);
588 element.append(toinsert);
574 element.append(toinsert);
589 };
575 };
590
576
591
577
592 OutputArea.prototype._dblclick_to_reset_size = function (img) {
578 OutputArea.prototype._dblclick_to_reset_size = function (img) {
593 // schedule wrapping image in resizable after a delay,
579 // schedule wrapping image in resizable after a delay,
594 // so we don't end up calling resize on a zero-size object
580 // so we don't end up calling resize on a zero-size object
595 var that = this;
581 var that = this;
596 setTimeout(function () {
582 setTimeout(function () {
597 var h0 = img.height();
583 var h0 = img.height();
598 var w0 = img.width();
584 var w0 = img.width();
599 if (!(h0 && w0)) {
585 if (!(h0 && w0)) {
600 // zero size, schedule another timeout
586 // zero size, schedule another timeout
601 that._dblclick_to_reset_size(img);
587 that._dblclick_to_reset_size(img);
602 return;
588 return;
603 }
589 }
604 img.resizable({
590 img.resizable({
605 aspectRatio: true,
591 aspectRatio: true,
606 autoHide: true
592 autoHide: true
607 });
593 });
608 img.dblclick(function () {
594 img.dblclick(function () {
609 // resize wrapper & image together for some reason:
595 // resize wrapper & image together for some reason:
610 img.parent().height(h0);
596 img.parent().height(h0);
611 img.height(h0);
597 img.height(h0);
612 img.parent().width(w0);
598 img.parent().width(w0);
613 img.width(w0);
599 img.width(w0);
614 });
600 });
615 }, 250);
601 }, 250);
616 };
602 };
617
603
618
604
619 OutputArea.prototype.append_png = function (png, md, element, type) {
605 OutputArea.prototype.append_png = function (png, md, element, type) {
620 var toinsert = this.create_output_subarea(md, "output_png", type);
606 var toinsert = this.create_output_subarea(md, "output_png", type);
621 var img = $("<img/>");
607 var img = $("<img/>");
622 img[0].setAttribute('src','data:image/png;base64,'+png);
608 img[0].setAttribute('src','data:image/png;base64,'+png);
623 if (md['height']) {
609 if (md['height']) {
624 img[0].setAttribute('height', md['height']);
610 img[0].setAttribute('height', md['height']);
625 }
611 }
626 if (md['width']) {
612 if (md['width']) {
627 img[0].setAttribute('width', md['width']);
613 img[0].setAttribute('width', md['width']);
628 }
614 }
629 this._dblclick_to_reset_size(img);
615 this._dblclick_to_reset_size(img);
630 toinsert.append(img);
616 toinsert.append(img);
631 element.append(toinsert);
617 element.append(toinsert);
632 };
618 };
633
619
634
620
635 OutputArea.prototype.append_jpeg = function (jpeg, md, element, type) {
621 OutputArea.prototype.append_jpeg = function (jpeg, md, element, type) {
636 var toinsert = this.create_output_subarea(md, "output_jpeg", type);
622 var toinsert = this.create_output_subarea(md, "output_jpeg", type);
637 var img = $("<img/>").attr('src','data:image/jpeg;base64,'+jpeg);
623 var img = $("<img/>").attr('src','data:image/jpeg;base64,'+jpeg);
638 if (md['height']) {
624 if (md['height']) {
639 img.attr('height', md['height']);
625 img.attr('height', md['height']);
640 }
626 }
641 if (md['width']) {
627 if (md['width']) {
642 img.attr('width', md['width']);
628 img.attr('width', md['width']);
643 }
629 }
644 this._dblclick_to_reset_size(img);
630 this._dblclick_to_reset_size(img);
645 toinsert.append(img);
631 toinsert.append(img);
646 element.append(toinsert);
632 element.append(toinsert);
647 };
633 };
648
634
649
635
650 OutputArea.prototype.append_latex = function (latex, md, element, type) {
636 OutputArea.prototype.append_latex = function (latex, md, element, type) {
651 // This method cannot do the typesetting because the latex first has to
637 // This method cannot do the typesetting because the latex first has to
652 // be on the page.
638 // be on the page.
653 var toinsert = this.create_output_subarea(md, "output_latex", type);
639 var toinsert = this.create_output_subarea(md, "output_latex", type);
654 toinsert.append(latex);
640 toinsert.append(latex);
655 element.append(toinsert);
641 element.append(toinsert);
656 };
642 };
657
643
658 OutputArea.prototype.append_raw_input = function (msg) {
644 OutputArea.prototype.append_raw_input = function (msg) {
659 var that = this;
645 var that = this;
660 this.expand();
646 this.expand();
661 var content = msg.content;
647 var content = msg.content;
662 var area = this.create_output_area();
648 var area = this.create_output_area();
663
649
664 // disable any other raw_inputs, if they are left around
650 // disable any other raw_inputs, if they are left around
665 $("div.output_subarea.raw_input").remove();
651 $("div.output_subarea.raw_input").remove();
666
652
667 area.append(
653 area.append(
668 $("<div/>")
654 $("<div/>")
669 .addClass("box-flex1 output_subarea raw_input")
655 .addClass("box-flex1 output_subarea raw_input")
670 .append(
656 .append(
671 $("<span/>")
657 $("<span/>")
672 .addClass("input_prompt")
658 .addClass("input_prompt")
673 .text(content.prompt)
659 .text(content.prompt)
674 )
660 )
675 .append(
661 .append(
676 $("<input/>")
662 $("<input/>")
677 .addClass("raw_input")
663 .addClass("raw_input")
678 .attr('type', 'text')
664 .attr('type', 'text')
679 .attr("size", 47)
665 .attr("size", 47)
680 .keydown(function (event, ui) {
666 .keydown(function (event, ui) {
681 // make sure we submit on enter,
667 // make sure we submit on enter,
682 // and don't re-execute the *cell* on shift-enter
668 // and don't re-execute the *cell* on shift-enter
683 if (event.which === utils.keycodes.ENTER) {
669 if (event.which === utils.keycodes.ENTER) {
684 that._submit_raw_input();
670 that._submit_raw_input();
685 return false;
671 return false;
686 }
672 }
687 })
673 })
688 )
674 )
689 );
675 );
690
676
691 this.element.append(area);
677 this.element.append(area);
692 var raw_input = area.find('input.raw_input');
678 var raw_input = area.find('input.raw_input');
693 // Register events that enable/disable the keyboard manager while raw
679 // Register events that enable/disable the keyboard manager while raw
694 // input is focused.
680 // input is focused.
695 IPython.keyboard_manager.register_events(raw_input);
681 IPython.keyboard_manager.register_events(raw_input);
696 // Note, the following line used to read raw_input.focus().focus().
682 // Note, the following line used to read raw_input.focus().focus().
697 // This seemed to be needed otherwise only the cell would be focused.
683 // This seemed to be needed otherwise only the cell would be focused.
698 // But with the modal UI, this seems to work fine with one call to focus().
684 // But with the modal UI, this seems to work fine with one call to focus().
699 raw_input.focus();
685 raw_input.focus();
700 }
686 }
701
687
702 OutputArea.prototype._submit_raw_input = function (evt) {
688 OutputArea.prototype._submit_raw_input = function (evt) {
703 var container = this.element.find("div.raw_input");
689 var container = this.element.find("div.raw_input");
704 var theprompt = container.find("span.input_prompt");
690 var theprompt = container.find("span.input_prompt");
705 var theinput = container.find("input.raw_input");
691 var theinput = container.find("input.raw_input");
706 var value = theinput.val();
692 var value = theinput.val();
707 var content = {
693 var content = {
708 output_type : 'stream',
694 output_type : 'stream',
709 name : 'stdout',
695 name : 'stdout',
710 text : theprompt.text() + value + '\n'
696 text : theprompt.text() + value + '\n'
711 }
697 }
712 // remove form container
698 // remove form container
713 container.parent().remove();
699 container.parent().remove();
714 // replace with plaintext version in stdout
700 // replace with plaintext version in stdout
715 this.append_output(content, false);
701 this.append_output(content, false);
716 $([IPython.events]).trigger('send_input_reply.Kernel', value);
702 $([IPython.events]).trigger('send_input_reply.Kernel', value);
717 }
703 }
718
704
719
705
720 OutputArea.prototype.handle_clear_output = function (msg) {
706 OutputArea.prototype.handle_clear_output = function (msg) {
721 this.clear_output(msg.content.wait);
707 this.clear_output(msg.content.wait);
722 };
708 };
723
709
724
710
725 OutputArea.prototype.clear_output = function(wait) {
711 OutputArea.prototype.clear_output = function(wait) {
726 if (wait) {
712 if (wait) {
727
713
728 // If a clear is queued, clear before adding another to the queue.
714 // If a clear is queued, clear before adding another to the queue.
729 if (this.clear_queued) {
715 if (this.clear_queued) {
730 this.clear_output(false);
716 this.clear_output(false);
731 };
717 };
732
718
733 this.clear_queued = true;
719 this.clear_queued = true;
734 } else {
720 } else {
735
721
736 // Fix the output div's height if the clear_output is waiting for
722 // Fix the output div's height if the clear_output is waiting for
737 // new output (it is being used in an animation).
723 // new output (it is being used in an animation).
738 if (this.clear_queued) {
724 if (this.clear_queued) {
739 var height = this.element.height();
725 var height = this.element.height();
740 this.element.height(height);
726 this.element.height(height);
741 this.clear_queued = false;
727 this.clear_queued = false;
742 }
728 }
743
729
744 // clear all, no need for logic
730 // clear all, no need for logic
745 this.element.html("");
731 this.element.html("");
746 this.outputs = [];
732 this.outputs = [];
747 this.unscroll_area();
733 this.unscroll_area();
748 return;
734 return;
749 };
735 };
750 };
736 };
751
737
752
738
753 // JSON serialization
739 // JSON serialization
754
740
755 OutputArea.prototype.fromJSON = function (outputs) {
741 OutputArea.prototype.fromJSON = function (outputs) {
756 // add here the mapping of short key to mime type
742 // add here the mapping of short key to mime type
757 // TODO: remove this when we update to nbformat 4
743 // TODO: remove this when we update to nbformat 4
758 var len = outputs.length;
744 var len = outputs.length;
759 for (var i=0; i<len; i++) {
745 for (var i=0; i<len; i++) {
760 var data = outputs[i];
746 var data = outputs[i];
761 var msg_type = data.output_type;
747 var msg_type = data.output_type;
762 if (msg_type === "display_data" || msg_type === "pyout") {
748 if (msg_type === "display_data" || msg_type === "pyout") {
763 // convert short keys to mime keys
749 // convert short keys to mime keys
764 this.convert_mime_types_r(data);
750 this.rename_keys(data, OutputArea.mime_map_r);
765 this.convert_mime_types_r(data.metadata);
751 this.rename_keys(data.metadata, OutputArea.mime_map_r);
766 }
752 }
767
753
768 // append with dynamic=false.
754 // append with dynamic=false.
769 this.append_output(data, false);
755 this.append_output(data, false);
770 }
756 }
771 };
757 };
772
758
773
759
774 OutputArea.prototype.toJSON = function () {
760 OutputArea.prototype.toJSON = function () {
775 var outputs = [];
761 var outputs = [];
776 var len = this.outputs.length;
762 var len = this.outputs.length;
777 for (var i=0; i<len; i++) {
763 for (var i=0; i<len; i++) {
778 var data = this.outputs[i];
764 var data = this.outputs[i];
779 var msg_type = data.output_type;
765 var msg_type = data.output_type;
780 console.log("msg type is ", msg_type)
766 console.log("msg type is ", msg_type)
781 if (msg_type === "display_data" || msg_type === "pyout") {
767 if (msg_type === "display_data" || msg_type === "pyout") {
782 // convert mime keys to short keys
768 // convert mime keys to short keys
783 this.convert_mime_types(data);
769 this.rename_keys(data, OutputArea.mime_map);
784 //this.convert_mime_types(data.metadata);
770 this.rename_keys(data.metadata, OutputArea.mime_map);
785 }
771 }
786 outputs[i] = data;
772 outputs[i] = data;
787 }
773 }
788 return outputs;
774 return outputs;
789 };
775 };
790
776
791
777
792 IPython.OutputArea = OutputArea;
778 IPython.OutputArea = OutputArea;
793
779
794 return IPython;
780 return IPython;
795
781
796 }(IPython));
782 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now