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