##// END OF EJS Templates
Cleanup some inappropriate javascript...
MinRK -
Show More
@@ -1,795 +1,797
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 // CodeCell
10 10 //============================================================================
11 11
12 12 var IPython = (function (IPython) {
13 13
14 14 var utils = IPython.utils;
15 15
16 16 var CodeCell = function (notebook) {
17 17 this.code_mirror = null;
18 18 this.input_prompt_number = ' ';
19 19 this.is_completing = false;
20 20 this.completion_cursor = null;
21 21 this.outputs = [];
22 22 this.collapsed = false;
23 this.tooltip_timeout = null;
23 24 IPython.Cell.apply(this, arguments);
24 25 };
25 26
26 27
27 28 CodeCell.prototype = new IPython.Cell();
28 29
29 30
30 31 CodeCell.prototype.create_element = function () {
31 32 var cell = $('<div></div>').addClass('cell border-box-sizing code_cell vbox');
32 33 cell.attr('tabindex','2');
33 34 var input = $('<div></div>').addClass('input hbox');
34 35 input.append($('<div/>').addClass('prompt input_prompt'));
35 36 var input_area = $('<div/>').addClass('input_area box-flex1');
36 37 this.code_mirror = CodeMirror(input_area.get(0), {
37 38 indentUnit : 4,
38 39 mode: 'python',
39 40 theme: 'ipython',
40 41 readOnly: this.read_only,
41 42 onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this)
42 43 });
43 44 input.append(input_area);
44 45 var output = $('<div></div>').addClass('output vbox');
45 46 cell.append(input).append(output);
46 47 this.element = cell;
47 48 this.collapse();
48 49 };
49 50
50 51 //TODO, try to diminish the number of parameters.
51 CodeCell.prototype.request_tooltip_after_time = function (pre_cursor,time,that){
52 CodeCell.prototype.request_tooltip_after_time = function (pre_cursor,time){
53 var that = this;
52 54 if (pre_cursor === "" || pre_cursor === "(" ) {
53 55 // don't do anything if line beggin with '(' or is empty
54 56 } else {
55 57 // Will set a timer to request tooltip in `time`
56 58 that.tooltip_timeout = setTimeout(function(){
57 59 IPython.notebook.request_tool_tip(that, pre_cursor)
58 60 },time);
59 61 }
60 62 };
61 63
62 64 CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) {
63 65 // This method gets called in CodeMirror's onKeyDown/onKeyPress
64 66 // handlers and is used to provide custom key handling. Its return
65 67 // value is used to determine if CodeMirror should ignore the event:
66 68 // true = ignore, false = don't ignore.
67 69
68 70 // note that we are comparing and setting the time to wait at each key press.
69 71 // a better wqy might be to generate a new function on each time change and
70 72 // assign it to CodeCell.prototype.request_tooltip_after_time
71 73 tooltip_wait_time = this.notebook.time_before_tooltip;
72 74 tooltip_on_tab = this.notebook.tooltip_on_tab;
73 75 var that = this;
74 76 // whatever key is pressed, first, cancel the tooltip request before
75 77 // they are sent, and remove tooltip if any
76 78 if(event.type === 'keydown' ){
77 CodeCell.prototype.remove_and_cancel_tooltip(that.tooltip_timeout);
78 that.tooltip_timeout=null;
79 that.remove_and_cancel_tooltip();
79 80 }
80 81
81 82 if (event.keyCode === 13 && (event.shiftKey || event.ctrlKey)) {
82 83 // Always ignore shift-enter in CodeMirror as we handle it.
83 84 return true;
84 85 }else if (event.which === 40 && event.type === 'keypress' && tooltip_wait_time >= 0) {
85 86 // triger aon keypress (!) otherwise inconsistent event.which depending on plateform
86 87 // browser and keyboard layout !
87 88 // Pressing '(' , request tooltip, don't forget to reappend it
88 89 var cursor = editor.getCursor();
89 90 var pre_cursor = editor.getRange({line:cursor.line,ch:0},cursor).trim()+'(';
90 CodeCell.prototype.request_tooltip_after_time(pre_cursor,tooltip_wait_time,that);
91 that.request_tooltip_after_time(pre_cursor,tooltip_wait_time);
91 92 } else if (event.keyCode === 9 && event.type == 'keydown') {
92 93 // Tab completion.
93 94 var cur = editor.getCursor();
94 95 //Do not trim here because of tooltip
95 96 var pre_cursor = editor.getRange({line:cur.line,ch:0},cur);
96 97 if (pre_cursor.trim() === "") {
97 98 // Don't autocomplete if the part of the line before the cursor
98 99 // is empty. In this case, let CodeMirror handle indentation.
99 100 return false;
100 101 } else if ((pre_cursor.substr(-1) === "("|| pre_cursor.substr(-1) === " ") && tooltip_on_tab ) {
101 CodeCell.prototype.request_tooltip_after_time(pre_cursor,0,that);
102 that.request_tooltip_after_time(pre_cursor,0);
102 103 } else {
103 104 pre_cursor.trim();
104 105 // Autocomplete the current line.
105 106 event.stop();
106 107 var line = editor.getLine(cur.line);
107 108 this.is_completing = true;
108 109 this.completion_cursor = cur;
109 110 IPython.notebook.complete_cell(this, line, cur.ch);
110 111 return true;
111 112 }
112 113 } else if (event.keyCode === 8 && event.type == 'keydown') {
113 114 // If backspace and the line ends with 4 spaces, remove them.
114 115 var cur = editor.getCursor();
115 116 var line = editor.getLine(cur.line);
116 117 var ending = line.slice(-4);
117 118 if (ending === ' ') {
118 119 editor.replaceRange('',
119 120 {line: cur.line, ch: cur.ch-4},
120 121 {line: cur.line, ch: cur.ch}
121 122 );
122 123 event.stop();
123 124 return true;
124 125 } else {
125 126 return false;
126 127 }
127 128 } else if (event.keyCode === 76 && event.ctrlKey && event.shiftKey
128 129 && event.type == 'keydown') {
129 130 // toggle line numbers with Ctrl-Shift-L
130 131 this.toggle_line_numbers();
131 132 }
132 133 else {
133 134 // keypress/keyup also trigger on TAB press, and we don't want to
134 135 // use those to disable tab completion.
135 136 if (this.is_completing && event.keyCode !== 9) {
136 137 var ed_cur = editor.getCursor();
137 138 var cc_cur = this.completion_cursor;
138 139 if (ed_cur.line !== cc_cur.line || ed_cur.ch !== cc_cur.ch) {
139 140 this.is_completing = false;
140 141 this.completion_cursor = null;
141 142 }
142 143 }
143 144 return false;
144 145 };
145 146 return false;
146 147 };
147 148
148 CodeCell.prototype.remove_and_cancel_tooltip = function(timeout)
149 {
149 CodeCell.prototype.remove_and_cancel_tooltip = function() {
150 150 // note that we don't handle closing directly inside the calltip
151 151 // as in the completer, because it is not focusable, so won't
152 152 // get the event.
153 if(timeout != null)
154 { clearTimeout(timeout);}
155 $('#tooltip').remove();
153 if (this.tooltip_timeout != null){
154 clearTimeout(this.tooltip_timeout);
155 $('#tooltip').remove();
156 this.tooltip_timeout = null;
157 }
156 158 }
157 159
158 160 CodeCell.prototype.finish_tooltip = function (reply) {
159 161 defstring=reply.definition;
160 162 docstring=reply.docstring;
161 163 if(docstring == null){docstring="<empty docstring>"};
162 164 name=reply.name;
163 165
164 166 var that = this;
165 167 var tooltip = $('<div/>').attr('id', 'tooltip').addClass('tooltip');
166 168 // remove to have the tooltip not Limited in X and Y
167 169 tooltip.addClass('smalltooltip');
168 170 var pre=$('<pre/>').html(utils.fixConsole(docstring));
169 171 var expandlink=$('<a/>').attr('href',"#");
170 172 expandlink.addClass("ui-corner-all"); //rounded corner
171 173 expandlink.attr('role',"button");
172 174 //expandlink.addClass('ui-button');
173 175 //expandlink.addClass('ui-state-default');
174 176 var expandspan=$('<span/>').text('Expand');
175 177 expandspan.addClass('ui-icon');
176 178 expandspan.addClass('ui-icon-plus');
177 179 expandlink.append(expandspan);
178 180 expandlink.attr('id','expanbutton');
179 181 expandlink.click(function(){
180 182 tooltip.removeClass('smalltooltip');
181 183 tooltip.addClass('bigtooltip');
182 184 $('#expanbutton').remove();
183 185 setTimeout(function(){that.code_mirror.focus();}, 50);
184 186 });
185 187 var morelink=$('<a/>').attr('href',"#");
186 188 morelink.attr('role',"button");
187 189 morelink.addClass('ui-button');
188 190 //morelink.addClass("ui-corner-all"); //rounded corner
189 191 //morelink.addClass('ui-state-default');
190 192 var morespan=$('<span/>').text('Open in Pager');
191 193 morespan.addClass('ui-icon');
192 194 morespan.addClass('ui-icon-arrowstop-l-n');
193 195 morelink.append(morespan);
194 196 morelink.click(function(){
195 197 var msg_id = IPython.notebook.kernel.execute(name+"?");
196 198 IPython.notebook.msg_cell_map[msg_id] = IPython.notebook.selected_cell().cell_id;
197 CodeCell.prototype.remove_and_cancel_tooltip(that.tooltip_timeout);
199 that.remove_and_cancel_tooltip();
198 200 setTimeout(function(){that.code_mirror.focus();}, 50);
199 201 });
200 202
201 203 var closelink=$('<a/>').attr('href',"#");
202 204 closelink.attr('role',"button");
203 205 closelink.addClass('ui-button');
204 206 //closelink.addClass("ui-corner-all"); //rounded corner
205 207 //closelink.adClass('ui-state-default'); // grey background and blue cross
206 208 var closespan=$('<span/>').text('Close');
207 209 closespan.addClass('ui-icon');
208 210 closespan.addClass('ui-icon-close');
209 211 closelink.append(closespan);
210 212 closelink.click(function(){
211 CodeCell.prototype.remove_and_cancel_tooltip(that.tooltip_timeout);
213 that.remove_and_cancel_tooltip();
212 214 setTimeout(function(){that.code_mirror.focus();}, 50);
213 215 });
214 216 //construct the tooltip
215 217 tooltip.append(closelink);
216 218 tooltip.append(expandlink);
217 219 tooltip.append(morelink);
218 220 if(defstring){
219 221 defstring_html= $('<pre/>').html(utils.fixConsole(defstring));
220 222 tooltip.append(defstring_html);
221 223 }
222 224 tooltip.append(pre);
223 225 var pos = this.code_mirror.cursorCoords();
224 226 tooltip.css('left',pos.x+'px');
225 227 tooltip.css('top',pos.yBot+'px');
226 228 $('body').append(tooltip);
227 229
228 230 // issues with cross-closing if multiple tooltip in less than 5sec
229 231 // keep it comented for now
230 // setTimeout(CodeCell.prototype.remove_and_cancel_tooltip, 5000);
232 // setTimeout(that.remove_and_cancel_tooltip, 5000);
231 233 };
232 234
233 235 // As you type completer
234 236 CodeCell.prototype.finish_completing = function (matched_text, matches) {
235 237 //return if not completing or nothing to complete
236 238 if (!this.is_completing || matches.length === 0) {return;}
237 239
238 240 // for later readability
239 241 var key = { tab:9,
240 242 esc:27,
241 243 backspace:8,
242 244 space:13,
243 245 shift:16,
244 246 enter:32,
245 247 // _ is 189
246 248 isCompSymbol : function (code)
247 249 {return ((code>64 && code <=122)|| code == 189)}
248 250 }
249 251
250 252 // smart completion, sort kwarg ending with '='
251 253 var newm = new Array();
252 254 if(this.notebook.smart_completer)
253 255 {
254 256 kwargs = new Array();
255 257 other = new Array();
256 258 for(var i=0;i<matches.length; ++i){
257 259 if(matches[i].substr(-1) === '='){
258 260 kwargs.push(matches[i]);
259 261 }else{other.push(matches[i]);}
260 262 }
261 263 newm = kwargs.concat(other);
262 264 matches=newm;
263 265 }
264 266 // end sort kwargs
265 267
266 268 // give common prefix of a array of string
267 269 function sharedStart(A){
268 270 if(A.length > 1 ){
269 271 var tem1, tem2, s, A= A.slice(0).sort();
270 272 tem1= A[0];
271 273 s= tem1.length;
272 274 tem2= A.pop();
273 275 while(s && tem2.indexOf(tem1)== -1){
274 276 tem1= tem1.substring(0, --s);
275 277 }
276 278 return tem1;
277 279 }
278 280 return "";
279 281 }
280 282
281 283
282 284 //try to check if the user is typing tab at least twice after a word
283 285 // and completion is "done"
284 286 fallback_on_tooltip_after=2
285 287 if(matches.length==1 && matched_text === matches[0])
286 288 {
287 289 if(this.npressed >fallback_on_tooltip_after && this.prevmatch==matched_text)
288 290 {
289 291 console.log('Ok, you really want to complete after pressing tab '+this.npressed+' times !');
290 292 console.log('You should understand that there is no (more) completion for that !');
291 293 console.log("I'll show you the tooltip, will you stop bothering me ?");
292 this.request_tooltip_after_time(matched_text+'(',0,this);
294 this.request_tooltip_after_time(matched_text+'(',0);
293 295 return;
294 296 }
295 297 this.prevmatch=matched_text
296 298 this.npressed=this.npressed+1;
297 299 }
298 300 else
299 301 {
300 302 this.prevmatch="";
301 303 this.npressed=0;
302 304 }
303 305 // end fallback on tooltip
304 306 //==================================
305 307 // Real completion logic start here
306 308 var that = this;
307 309 var cur = this.completion_cursor;
308 310 var done = false;
309 311
310 312 // call to dismmiss the completer
311 313 var close = function () {
312 314 if (done) return;
313 315 done = true;
314 316 if (complete!=undefined)
315 317 {complete.remove();}
316 318 that.is_completing = false;
317 319 that.completion_cursor = null;
318 320 };
319 321
320 322 // insert the given text and exit the completer
321 323 var insert = function (selected_text, event) {
322 324 that.code_mirror.replaceRange(
323 325 selected_text,
324 326 {line: cur.line, ch: (cur.ch-matched_text.length)},
325 327 {line: cur.line, ch: cur.ch}
326 328 );
327 329 if(event != null){
328 330 event.stopPropagation();
329 331 event.preventDefault();
330 332 }
331 333 close();
332 334 setTimeout(function(){that.code_mirror.focus();}, 50);
333 335 };
334 336
335 337 // insert the curent highlited selection and exit
336 338 var pick = function () {
337 339 insert(select.val()[0],null);
338 340 };
339 341
340 342
341 343 // Define function to clear the completer, refill it with the new
342 344 // matches, update the pseuso typing field. autopick insert match if
343 345 // only one left, in no matches (anymore) dismiss itself by pasting
344 346 // what the user have typed until then
345 347 var complete_with = function(matches,typed_text,autopick,event)
346 348 {
347 349 // If autopick an only one match, past.
348 350 // Used to 'pick' when pressing tab
349 351 if (matches.length < 1) {
350 352 insert(typed_text,event);
351 353 if(event !=null){
352 354 event.stopPropagation();
353 355 event.preventDefault();
354 356 }
355 357 } else if (autopick && matches.length==1) {
356 358 insert(matches[0],event);
357 359 if(event !=null){
358 360 event.stopPropagation();
359 361 event.preventDefault();
360 362 }
361 363 }
362 364 //clear the previous completion if any
363 365 complete.children().children().remove();
364 366 $('#asyoutype').text(typed_text);
365 367 select=$('#asyoutypeselect');
366 368 for (var i=0; i<matches.length; ++i) {
367 369 select.append($('<option/>').html(matches[i]));
368 370 }
369 371 select.children().first().attr('selected','true');
370 372 }
371 373
372 374 // create html for completer
373 375 var complete = $('<div/>').addClass('completions');
374 376 complete.attr('id','complete');
375 377 complete.append($('<p/>').attr('id', 'asyoutype').html(matched_text));//pseudo input field
376 378
377 379 var select = $('<select/>').attr('multiple','true');
378 380 select.attr('id', 'asyoutypeselect')
379 381 select.attr('size',Math.min(10,matches.length));
380 382 var pos = this.code_mirror.cursorCoords();
381 383
382 384 // TODO: I propose to remove enough horizontal pixel
383 385 // to align the text later
384 386 complete.css('left',pos.x+'px');
385 387 complete.css('top',pos.yBot+'px');
386 388 complete.append(select);
387 389
388 390 $('body').append(complete);
389 391
390 392 // So a first actual completion. see if all the completion start wit
391 393 // the same letter and complete if necessary
392 394 fastForward = sharedStart(matches)
393 395 typed_characters= fastForward.substr(matched_text.length);
394 396 complete_with(matches,matched_text+typed_characters,true,null);
395 397 filterd=matches;
396 398 // Give focus to select, and make it filter the match as the user type
397 399 // by filtering the previous matches. Called by .keypress and .keydown
398 400 var downandpress = function (event,press_or_down) {
399 401 var code = event.which;
400 402 var autopick = false; // auto 'pick' if only one match
401 403 if (press_or_down === 0){
402 404 press=true; down=false; //Are we called from keypress or keydown
403 405 } else if (press_or_down == 1){
404 406 press=false; down=true;
405 407 }
406 408 if (code === key.shift) {
407 409 // nothing on Shift
408 410 return;
409 411 }
410 412 if (code === key.space || code === key.enter) {
411 413 // Pressing SPACE or ENTER will cause a pick
412 414 event.stopPropagation();
413 415 event.preventDefault();
414 416 pick();
415 417 } else if (code === 38 || code === 40) {
416 418 // We don't want the document keydown handler to handle UP/DOWN,
417 419 // but we want the default action.
418 420 event.stopPropagation();
419 421 //} else if ( key.isCompSymbol(code)|| (code==key.backspace)||(code==key.tab && down)){
420 422 } else if ( (code==key.backspace)||(code==key.tab && down) || press || key.isCompSymbol(code)){
421 423 if( key.isCompSymbol(code) && press)
422 424 {
423 425 var newchar = String.fromCharCode(code);
424 426 typed_characters=typed_characters+newchar;
425 427 } else if (code == key.tab) {
426 428 fastForward = sharedStart(filterd)
427 429 ffsub = fastForward.substr(matched_text.length+typed_characters.length);
428 430 typed_characters=typed_characters+ffsub;
429 431 autopick=true;
430 432 event.stopPropagation();
431 433 event.preventDefault();
432 434 } else if (code == key.backspace && down) {
433 435 // cancel if user have erase everything, otherwise decrease
434 436 // what we filter with
435 437 if (typed_characters.length <= 0)
436 438 {
437 439 insert(matched_text,event)
438 440 }
439 441 typed_characters=typed_characters.substr(0,typed_characters.length-1);
440 442 }else{return}
441 443 re = new RegExp("^"+"\%?"+matched_text+typed_characters,"");
442 444 filterd = matches.filter(function(x){return re.test(x)});
443 445 complete_with(filterd,matched_text+typed_characters,autopick,event);
444 446 } else if(down){ // abort only on .keydown
445 447 // abort with what the user have pressed until now
446 448 console.log('aborting with keycode : '+code+' is down :'+down);
447 449 insert(matched_text+typed_characters,event);
448 450 }
449 451 }
450 452 select.keydown(function (event) {
451 453 downandpress(event,1)
452 454 });
453 455 select.keypress(function (event) {
454 456 downandpress(event,0)
455 457 });
456 458 // Double click also causes a pick.
457 459 // and bind the last actions.
458 460 select.dblclick(pick);
459 461 select.blur(close);
460 462 select.focus();
461 463 };
462 464
463 465 CodeCell.prototype.toggle_line_numbers = function () {
464 466 if (this.code_mirror.getOption('lineNumbers') == false) {
465 467 this.code_mirror.setOption('lineNumbers', true);
466 468 } else {
467 469 this.code_mirror.setOption('lineNumbers', false);
468 470 }
469 471 this.code_mirror.refresh();
470 472 };
471 473
472 474 CodeCell.prototype.select = function () {
473 475 IPython.Cell.prototype.select.apply(this);
474 476 // Todo: this dance is needed because as of CodeMirror 2.12, focus is
475 477 // not causing the cursor to blink if the editor is empty initially.
476 478 // While this seems to fix the issue, this should be fixed
477 479 // in CodeMirror proper.
478 480 var s = this.code_mirror.getValue();
479 481 this.code_mirror.focus();
480 482 if (s === '') this.code_mirror.setValue('');
481 483 };
482 484
483 485
484 486 CodeCell.prototype.select_all = function () {
485 487 var start = {line: 0, ch: 0};
486 488 var nlines = this.code_mirror.lineCount();
487 489 var last_line = this.code_mirror.getLine(nlines-1);
488 490 var end = {line: nlines-1, ch: last_line.length};
489 491 this.code_mirror.setSelection(start, end);
490 492 };
491 493
492 494
493 495 CodeCell.prototype.append_output = function (json) {
494 496 this.expand();
495 497 if (json.output_type === 'pyout') {
496 498 this.append_pyout(json);
497 499 } else if (json.output_type === 'pyerr') {
498 500 this.append_pyerr(json);
499 501 } else if (json.output_type === 'display_data') {
500 502 this.append_display_data(json);
501 503 } else if (json.output_type === 'stream') {
502 504 this.append_stream(json);
503 505 };
504 506 this.outputs.push(json);
505 507 };
506 508
507 509
508 510 CodeCell.prototype.create_output_area = function () {
509 511 var oa = $("<div/>").addClass("hbox output_area");
510 512 oa.append($('<div/>').addClass('prompt'));
511 513 return oa;
512 514 };
513 515
514 516
515 517 CodeCell.prototype.append_pyout = function (json) {
516 518 n = json.prompt_number || ' ';
517 519 var toinsert = this.create_output_area();
518 520 toinsert.find('div.prompt').addClass('output_prompt').html('Out[' + n + ']:');
519 521 this.append_mime_type(json, toinsert);
520 522 this.element.find('div.output').append(toinsert);
521 523 // If we just output latex, typeset it.
522 524 if ((json.latex !== undefined) || (json.html !== undefined)) {
523 525 MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
524 526 };
525 527 };
526 528
527 529
528 530 CodeCell.prototype.append_pyerr = function (json) {
529 531 var tb = json.traceback;
530 532 if (tb !== undefined && tb.length > 0) {
531 533 var s = '';
532 534 var len = tb.length;
533 535 for (var i=0; i<len; i++) {
534 536 s = s + tb[i] + '\n';
535 537 }
536 538 s = s + '\n';
537 539 var toinsert = this.create_output_area();
538 540 this.append_text(s, toinsert);
539 541 this.element.find('div.output').append(toinsert);
540 542 };
541 543 };
542 544
543 545
544 546 CodeCell.prototype.append_stream = function (json) {
545 547 // temporary fix: if stream undefined (json file written prior to this patch),
546 548 // default to most likely stdout:
547 549 if (json.stream == undefined){
548 550 json.stream = 'stdout';
549 551 }
550 552 var subclass = "output_"+json.stream;
551 553 if (this.outputs.length > 0){
552 554 // have at least one output to consider
553 555 var last = this.outputs[this.outputs.length-1];
554 556 if (last.output_type == 'stream' && json.stream == last.stream){
555 557 // latest output was in the same stream,
556 558 // so append directly into its pre tag
557 559 this.element.find('div.'+subclass).last().find('pre').append(json.text);
558 560 return;
559 561 }
560 562 }
561 563
562 564 // If we got here, attach a new div
563 565 var toinsert = this.create_output_area();
564 566 this.append_text(json.text, toinsert, "output_stream "+subclass);
565 567 this.element.find('div.output').append(toinsert);
566 568 };
567 569
568 570
569 571 CodeCell.prototype.append_display_data = function (json) {
570 572 var toinsert = this.create_output_area();
571 573 this.append_mime_type(json, toinsert);
572 574 this.element.find('div.output').append(toinsert);
573 575 // If we just output latex, typeset it.
574 576 if ( (json.latex !== undefined) || (json.html !== undefined) ) {
575 577 MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
576 578 };
577 579 };
578 580
579 581
580 582 CodeCell.prototype.append_mime_type = function (json, element) {
581 583 if (json.html !== undefined) {
582 584 this.append_html(json.html, element);
583 585 } else if (json.latex !== undefined) {
584 586 this.append_latex(json.latex, element);
585 587 } else if (json.svg !== undefined) {
586 588 this.append_svg(json.svg, element);
587 589 } else if (json.png !== undefined) {
588 590 this.append_png(json.png, element);
589 591 } else if (json.jpeg !== undefined) {
590 592 this.append_jpeg(json.jpeg, element);
591 593 } else if (json.text !== undefined) {
592 594 this.append_text(json.text, element);
593 595 };
594 596 };
595 597
596 598
597 599 CodeCell.prototype.append_html = function (html, element) {
598 600 var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_html rendered_html");
599 601 toinsert.append(html);
600 602 element.append(toinsert);
601 603 };
602 604
603 605
604 606 CodeCell.prototype.append_text = function (data, element, extra_class) {
605 607 var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_text");
606 608 if (extra_class){
607 609 toinsert.addClass(extra_class);
608 610 }
609 611 toinsert.append($("<pre/>").html(data));
610 612 element.append(toinsert);
611 613 };
612 614
613 615
614 616 CodeCell.prototype.append_svg = function (svg, element) {
615 617 var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_svg");
616 618 toinsert.append(svg);
617 619 element.append(toinsert);
618 620 };
619 621
620 622
621 623 CodeCell.prototype.append_png = function (png, element) {
622 624 var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_png");
623 625 toinsert.append($("<img/>").attr('src','data:image/png;base64,'+png));
624 626 element.append(toinsert);
625 627 };
626 628
627 629
628 630 CodeCell.prototype.append_jpeg = function (jpeg, element) {
629 631 var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_jpeg");
630 632 toinsert.append($("<img/>").attr('src','data:image/jpeg;base64,'+jpeg));
631 633 element.append(toinsert);
632 634 };
633 635
634 636
635 637 CodeCell.prototype.append_latex = function (latex, element) {
636 638 // This method cannot do the typesetting because the latex first has to
637 639 // be on the page.
638 640 var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_latex");
639 641 toinsert.append(latex);
640 642 element.append(toinsert);
641 643 };
642 644
643 645
644 646 CodeCell.prototype.clear_output = function (stdout, stderr, other) {
645 647 var output_div = this.element.find("div.output");
646 648 if (stdout && stderr && other){
647 649 // clear all, no need for logic
648 650 output_div.html("");
649 651 this.outputs = [];
650 652 return;
651 653 }
652 654 // remove html output
653 655 // each output_subarea that has an identifying class is in an output_area
654 656 // which is the element to be removed.
655 657 if (stdout){
656 658 output_div.find("div.output_stdout").parent().remove();
657 659 }
658 660 if (stderr){
659 661 output_div.find("div.output_stderr").parent().remove();
660 662 }
661 663 if (other){
662 664 output_div.find("div.output_subarea").not("div.output_stderr").not("div.output_stdout").parent().remove();
663 665 }
664 666
665 667 // remove cleared outputs from JSON list:
666 668 for (var i = this.outputs.length - 1; i >= 0; i--){
667 669 var out = this.outputs[i];
668 670 var output_type = out.output_type;
669 671 if (output_type == "display_data" && other){
670 672 this.outputs.splice(i,1);
671 673 }else if (output_type == "stream"){
672 674 if (stdout && out.stream == "stdout"){
673 675 this.outputs.splice(i,1);
674 676 }else if (stderr && out.stream == "stderr"){
675 677 this.outputs.splice(i,1);
676 678 }
677 679 }
678 680 }
679 681 };
680 682
681 683
682 684 CodeCell.prototype.clear_input = function () {
683 685 this.code_mirror.setValue('');
684 686 };
685 687
686 688
687 689 CodeCell.prototype.collapse = function () {
688 690 if (!this.collapsed) {
689 691 this.element.find('div.output').hide();
690 692 this.collapsed = true;
691 693 };
692 694 };
693 695
694 696
695 697 CodeCell.prototype.expand = function () {
696 698 if (this.collapsed) {
697 699 this.element.find('div.output').show();
698 700 this.collapsed = false;
699 701 };
700 702 };
701 703
702 704
703 705 CodeCell.prototype.toggle_output = function () {
704 706 if (this.collapsed) {
705 707 this.expand();
706 708 } else {
707 709 this.collapse();
708 710 };
709 711 };
710 712
711 713 CodeCell.prototype.set_input_prompt = function (number) {
712 714 var n = number || '&nbsp;';
713 715 this.input_prompt_number = n;
714 716 this.element.find('div.input_prompt').html('In&nbsp;[' + n + ']:');
715 717 };
716 718
717 719
718 720 CodeCell.prototype.get_code = function () {
719 721 return this.code_mirror.getValue();
720 722 };
721 723
722 724
723 725 CodeCell.prototype.set_code = function (code) {
724 726 return this.code_mirror.setValue(code);
725 727 };
726 728
727 729
728 730 CodeCell.prototype.at_top = function () {
729 731 var cursor = this.code_mirror.getCursor();
730 732 if (cursor.line === 0) {
731 733 return true;
732 734 } else {
733 735 return false;
734 736 }
735 737 };
736 738
737 739
738 740 CodeCell.prototype.at_bottom = function () {
739 741 var cursor = this.code_mirror.getCursor();
740 742 if (cursor.line === (this.code_mirror.lineCount()-1)) {
741 743 return true;
742 744 } else {
743 745 return false;
744 746 }
745 747 };
746 748
747 749
748 750 CodeCell.prototype.fromJSON = function (data) {
749 751 console.log('Import from JSON:', data);
750 752 if (data.cell_type === 'code') {
751 753 if (data.input !== undefined) {
752 754 this.set_code(data.input);
753 755 }
754 756 if (data.prompt_number !== undefined) {
755 757 this.set_input_prompt(data.prompt_number);
756 758 } else {
757 759 this.set_input_prompt();
758 760 };
759 761 var len = data.outputs.length;
760 762 for (var i=0; i<len; i++) {
761 763 this.append_output(data.outputs[i]);
762 764 };
763 765 if (data.collapsed !== undefined) {
764 766 if (data.collapsed) {
765 767 this.collapse();
766 768 };
767 769 };
768 770 };
769 771 };
770 772
771 773
772 774 CodeCell.prototype.toJSON = function () {
773 775 var data = {};
774 776 data.input = this.get_code();
775 777 data.cell_type = 'code';
776 778 if (this.input_prompt_number !== ' ') {
777 779 data.prompt_number = this.input_prompt_number;
778 780 };
779 781 var outputs = [];
780 782 var len = this.outputs.length;
781 783 for (var i=0; i<len; i++) {
782 784 outputs[i] = this.outputs[i];
783 785 };
784 786 data.outputs = outputs;
785 787 data.language = 'python';
786 788 data.collapsed = this.collapsed;
787 789 // console.log('Export to JSON:',data);
788 790 return data;
789 791 };
790 792
791 793
792 794 IPython.CodeCell = CodeCell;
793 795
794 796 return IPython;
795 797 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now