##// END OF EJS Templates
protect shift extend on Uppercase
Matthias BUSSONNIER -
Show More
@@ -1,733 +1,737 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 234
235 235 // smart completion, sort kwarg ending with '='
236 236 var newm = new Array();
237 237 if(this.notebook.smart_completer)
238 238 {
239 239 kwargs = new Array();
240 240 other = new Array();
241 241 for(var i=0;i<matches.length; ++i){
242 242 if(matches[i].substr(-1) === '='){
243 243 kwargs.push(matches[i]);
244 244 }else{other.push(matches[i]);}
245 245 }
246 246 newm = kwargs.concat(other);
247 247 matches=newm;
248 248 }
249 249 // end sort kwargs
250 250
251 251 if (!this.is_completing || matches.length === 0) {return;}
252 252
253 253 //try to check if the user is typing tab at least twice after a word
254 254 // and completion is "done"
255 255 fallback_on_tooltip_after=2
256 256 if(matches.length==1 && matched_text === matches[0])
257 257 {
258 258 if(this.npressed >fallback_on_tooltip_after && this.prevmatch==matched_text)
259 259 {
260 260 console.log('Ok, you really want to complete after pressing tab '+this.npressed+' times !');
261 261 console.log('You should understand that there is no (more) completion for that !');
262 262 console.log("I'll show you the tooltip, will you stop bothering me ?");
263 263 this.request_tooltip_after_time(matched_text+'(',0,this);
264 264 return;
265 265 }
266 266 this.prevmatch=matched_text
267 267 this.npressed=this.npressed+1;
268 268 }
269 269 else
270 270 {
271 271 this.prevmatch="";
272 272 this.npressed=0;
273 273 }
274 274 // end fallback on tooltip
275 275
276 276 // Real completion logic start here
277 277 var that = this;
278 278 var cur = this.completion_cursor;
279 279 var done = false;
280 280
281 281 // call to dismmiss the completer
282 282 var close = function () {
283 283 if (done) return;
284 284 done = true;
285 285 if (complete!=undefined)
286 286 {complete.remove();}
287 287 that.is_completing = false;
288 288 that.completion_cursor = null;
289 289 };
290 290
291 291 // insert the given text and exit the completer
292 292 var insert = function (selected_text) {
293 293 that.code_mirror.replaceRange(
294 294 selected_text,
295 295 {line: cur.line, ch: (cur.ch-matched_text.length)},
296 296 {line: cur.line, ch: cur.ch}
297 297 );
298 298 event.stopPropagation();
299 299 event.preventDefault();
300 300 close();
301 301 setTimeout(function(){that.code_mirror.focus();}, 50);
302 302 };
303 303
304 304 // insert the curent highlited selection and exit
305 305 var pick = function () {
306 306 insert(select.val()[0]);
307 307 };
308 308
309 309 // if only one match, complete to it, don't ask user
310 310 if (matches.length === 1) {
311 311 insert(matches[0]);
312 312 return;
313 313 };
314 314
315 315
316 316 // Define function to clear the completer, refill it with the new
317 317 // matches, update the pseuso typing field. Note that this is case
318 318 // insensitive for now
319 319 var complete_with = function(matches,typed_text)
320 320 {
321 321 //clear the previous completion if any
322 322 if (matches.length < 1) {
323 323 insert(typed_text);
324 324 }
325 325 complete.children().children().remove();
326 326 $('#asyoutype').text(typed_text);
327 327 select=$('#asyoutypeselect');
328 328 for (var i=0; i<matches.length; ++i) {
329 329 select.append($('<option/>').html(matches[i]));
330 330 }
331 331 select.children().first().attr('selected','true');
332 332 }
333 333
334 334 // create html for completer
335 335 var complete = $('<div/>').addClass('completions');
336 336 complete.attr('id','complete');
337 337 complete.append($('<p/>').attr('id', 'asyoutype').html(matched_text));//pseudo input field
338 338
339 339 var select = $('<select/>').attr('multiple','true');
340 340 select.attr('id', 'asyoutypeselect')
341 341 select.attr('size',Math.min(10,matches.length));
342 342 var pos = this.code_mirror.cursorCoords();
343 343
344 344 // TODO: I propose to remove enough horizontal pixel
345 345 // to align the text later
346 346 complete.css('left',pos.x+'px');
347 347 complete.css('top',pos.yBot+'px');
348 348 complete.append(select);
349 349
350 350 $('body').append(complete);
351 351
352 352 //do a first actual completion
353 353 complete_with(matches,matched_text);
354 354
355 355 // Give focus to select, and make it filter the match as the user type
356 356 // by filtering the previous matches
357 357 typed_characters = "";
358 358 select.keydown(function (event) {
359 359 var code = event.which;
360 if (code === 16) {
361 // nothing on Shift
362 return;
363 }
360 364 if (code === 13 || code === 32) {
361 365 // Pressing SPACE or ENTER will cause a pick
362 366 event.stopPropagation();
363 367 event.preventDefault();
364 368 pick();
365 369 } else if (code === 38 || code === 40) {
366 370 // We don't want the document keydown handler to handle UP/DOWN,
367 371 // but we want the default action.
368 372 event.stopPropagation();
369 } else if (code>64 && code <90 || code==8){
373 } else if (code>64 && code <=122 || code==8){
370 374 // issues with _-.. on chrome at least
371 375 if(code != 8)
372 376 {
373 377 var newchar = String.fromCharCode(code).toLowerCase();
374 378 typed_characters=typed_characters+newchar;
375 379 } else {
376 380 // 8 is backspace remove 1 char cancel if
377 381 // user have erase everything, otherwise
378 382 // decrease what we filter with
379 383 if (typed_characters.length <= 0)
380 384 {
381 385 insert(matched_text)
382 386 }
383 387 typed_characters=typed_characters.substr(0,typed_characters.length-1);
384 388 }
385 389 re = new RegExp("^"+"\%?"+matched_text+typed_characters,"i");
386 390 filterd= matches.filter(function(x){return re.test(x)});
387 391 complete_with(filterd,matched_text+typed_characters);
388 392 } else {
389 393 // abort with what the user have pressed until now
390 394 console.log('aborting with keycode : '+code);
391 395 insert(matched_text+typed_characters);
392 396 }
393 397 });
394 398 // Double click also causes a pick.
395 399 // and bind the last actions.
396 400 select.dblclick(pick);
397 401 select.blur(close);
398 402 select.focus();
399 403 };
400 404
401 405 CodeCell.prototype.toggle_line_numbers = function () {
402 406 if (this.code_mirror.getOption('lineNumbers') == false) {
403 407 this.code_mirror.setOption('lineNumbers', true);
404 408 } else {
405 409 this.code_mirror.setOption('lineNumbers', false);
406 410 }
407 411 this.code_mirror.refresh();
408 412 };
409 413
410 414 CodeCell.prototype.select = function () {
411 415 IPython.Cell.prototype.select.apply(this);
412 416 // Todo: this dance is needed because as of CodeMirror 2.12, focus is
413 417 // not causing the cursor to blink if the editor is empty initially.
414 418 // While this seems to fix the issue, this should be fixed
415 419 // in CodeMirror proper.
416 420 var s = this.code_mirror.getValue();
417 421 this.code_mirror.focus();
418 422 if (s === '') this.code_mirror.setValue('');
419 423 };
420 424
421 425
422 426 CodeCell.prototype.select_all = function () {
423 427 var start = {line: 0, ch: 0};
424 428 var nlines = this.code_mirror.lineCount();
425 429 var last_line = this.code_mirror.getLine(nlines-1);
426 430 var end = {line: nlines-1, ch: last_line.length};
427 431 this.code_mirror.setSelection(start, end);
428 432 };
429 433
430 434
431 435 CodeCell.prototype.append_output = function (json) {
432 436 this.expand();
433 437 if (json.output_type === 'pyout') {
434 438 this.append_pyout(json);
435 439 } else if (json.output_type === 'pyerr') {
436 440 this.append_pyerr(json);
437 441 } else if (json.output_type === 'display_data') {
438 442 this.append_display_data(json);
439 443 } else if (json.output_type === 'stream') {
440 444 this.append_stream(json);
441 445 };
442 446 this.outputs.push(json);
443 447 };
444 448
445 449
446 450 CodeCell.prototype.create_output_area = function () {
447 451 var oa = $("<div/>").addClass("hbox output_area");
448 452 oa.append($('<div/>').addClass('prompt'));
449 453 return oa;
450 454 };
451 455
452 456
453 457 CodeCell.prototype.append_pyout = function (json) {
454 458 n = json.prompt_number || ' ';
455 459 var toinsert = this.create_output_area();
456 460 toinsert.find('div.prompt').addClass('output_prompt').html('Out[' + n + ']:');
457 461 this.append_mime_type(json, toinsert);
458 462 this.element.find('div.output').append(toinsert);
459 463 // If we just output latex, typeset it.
460 464 if ((json.latex !== undefined) || (json.html !== undefined)) {
461 465 MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
462 466 };
463 467 };
464 468
465 469
466 470 CodeCell.prototype.append_pyerr = function (json) {
467 471 var tb = json.traceback;
468 472 if (tb !== undefined && tb.length > 0) {
469 473 var s = '';
470 474 var len = tb.length;
471 475 for (var i=0; i<len; i++) {
472 476 s = s + tb[i] + '\n';
473 477 }
474 478 s = s + '\n';
475 479 var toinsert = this.create_output_area();
476 480 this.append_text(s, toinsert);
477 481 this.element.find('div.output').append(toinsert);
478 482 };
479 483 };
480 484
481 485
482 486 CodeCell.prototype.append_stream = function (json) {
483 487 // temporary fix: if stream undefined (json file written prior to this patch),
484 488 // default to most likely stdout:
485 489 if (json.stream == undefined){
486 490 json.stream = 'stdout';
487 491 }
488 492 var subclass = "output_"+json.stream;
489 493 if (this.outputs.length > 0){
490 494 // have at least one output to consider
491 495 var last = this.outputs[this.outputs.length-1];
492 496 if (last.output_type == 'stream' && json.stream == last.stream){
493 497 // latest output was in the same stream,
494 498 // so append directly into its pre tag
495 499 this.element.find('div.'+subclass).last().find('pre').append(json.text);
496 500 return;
497 501 }
498 502 }
499 503
500 504 // If we got here, attach a new div
501 505 var toinsert = this.create_output_area();
502 506 this.append_text(json.text, toinsert, "output_stream "+subclass);
503 507 this.element.find('div.output').append(toinsert);
504 508 };
505 509
506 510
507 511 CodeCell.prototype.append_display_data = function (json) {
508 512 var toinsert = this.create_output_area();
509 513 this.append_mime_type(json, toinsert);
510 514 this.element.find('div.output').append(toinsert);
511 515 // If we just output latex, typeset it.
512 516 if ( (json.latex !== undefined) || (json.html !== undefined) ) {
513 517 MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
514 518 };
515 519 };
516 520
517 521
518 522 CodeCell.prototype.append_mime_type = function (json, element) {
519 523 if (json.html !== undefined) {
520 524 this.append_html(json.html, element);
521 525 } else if (json.latex !== undefined) {
522 526 this.append_latex(json.latex, element);
523 527 } else if (json.svg !== undefined) {
524 528 this.append_svg(json.svg, element);
525 529 } else if (json.png !== undefined) {
526 530 this.append_png(json.png, element);
527 531 } else if (json.jpeg !== undefined) {
528 532 this.append_jpeg(json.jpeg, element);
529 533 } else if (json.text !== undefined) {
530 534 this.append_text(json.text, element);
531 535 };
532 536 };
533 537
534 538
535 539 CodeCell.prototype.append_html = function (html, element) {
536 540 var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_html rendered_html");
537 541 toinsert.append(html);
538 542 element.append(toinsert);
539 543 };
540 544
541 545
542 546 CodeCell.prototype.append_text = function (data, element, extra_class) {
543 547 var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_text");
544 548 if (extra_class){
545 549 toinsert.addClass(extra_class);
546 550 }
547 551 toinsert.append($("<pre/>").html(data));
548 552 element.append(toinsert);
549 553 };
550 554
551 555
552 556 CodeCell.prototype.append_svg = function (svg, element) {
553 557 var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_svg");
554 558 toinsert.append(svg);
555 559 element.append(toinsert);
556 560 };
557 561
558 562
559 563 CodeCell.prototype.append_png = function (png, element) {
560 564 var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_png");
561 565 toinsert.append($("<img/>").attr('src','data:image/png;base64,'+png));
562 566 element.append(toinsert);
563 567 };
564 568
565 569
566 570 CodeCell.prototype.append_jpeg = function (jpeg, element) {
567 571 var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_jpeg");
568 572 toinsert.append($("<img/>").attr('src','data:image/jpeg;base64,'+jpeg));
569 573 element.append(toinsert);
570 574 };
571 575
572 576
573 577 CodeCell.prototype.append_latex = function (latex, element) {
574 578 // This method cannot do the typesetting because the latex first has to
575 579 // be on the page.
576 580 var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_latex");
577 581 toinsert.append(latex);
578 582 element.append(toinsert);
579 583 };
580 584
581 585
582 586 CodeCell.prototype.clear_output = function (stdout, stderr, other) {
583 587 var output_div = this.element.find("div.output");
584 588 if (stdout && stderr && other){
585 589 // clear all, no need for logic
586 590 output_div.html("");
587 591 this.outputs = [];
588 592 return;
589 593 }
590 594 // remove html output
591 595 // each output_subarea that has an identifying class is in an output_area
592 596 // which is the element to be removed.
593 597 if (stdout){
594 598 output_div.find("div.output_stdout").parent().remove();
595 599 }
596 600 if (stderr){
597 601 output_div.find("div.output_stderr").parent().remove();
598 602 }
599 603 if (other){
600 604 output_div.find("div.output_subarea").not("div.output_stderr").not("div.output_stdout").parent().remove();
601 605 }
602 606
603 607 // remove cleared outputs from JSON list:
604 608 for (var i = this.outputs.length - 1; i >= 0; i--){
605 609 var out = this.outputs[i];
606 610 var output_type = out.output_type;
607 611 if (output_type == "display_data" && other){
608 612 this.outputs.splice(i,1);
609 613 }else if (output_type == "stream"){
610 614 if (stdout && out.stream == "stdout"){
611 615 this.outputs.splice(i,1);
612 616 }else if (stderr && out.stream == "stderr"){
613 617 this.outputs.splice(i,1);
614 618 }
615 619 }
616 620 }
617 621 };
618 622
619 623
620 624 CodeCell.prototype.clear_input = function () {
621 625 this.code_mirror.setValue('');
622 626 };
623 627
624 628
625 629 CodeCell.prototype.collapse = function () {
626 630 if (!this.collapsed) {
627 631 this.element.find('div.output').hide();
628 632 this.collapsed = true;
629 633 };
630 634 };
631 635
632 636
633 637 CodeCell.prototype.expand = function () {
634 638 if (this.collapsed) {
635 639 this.element.find('div.output').show();
636 640 this.collapsed = false;
637 641 };
638 642 };
639 643
640 644
641 645 CodeCell.prototype.toggle_output = function () {
642 646 if (this.collapsed) {
643 647 this.expand();
644 648 } else {
645 649 this.collapse();
646 650 };
647 651 };
648 652
649 653 CodeCell.prototype.set_input_prompt = function (number) {
650 654 var n = number || '&nbsp;';
651 655 this.input_prompt_number = n;
652 656 this.element.find('div.input_prompt').html('In&nbsp;[' + n + ']:');
653 657 };
654 658
655 659
656 660 CodeCell.prototype.get_code = function () {
657 661 return this.code_mirror.getValue();
658 662 };
659 663
660 664
661 665 CodeCell.prototype.set_code = function (code) {
662 666 return this.code_mirror.setValue(code);
663 667 };
664 668
665 669
666 670 CodeCell.prototype.at_top = function () {
667 671 var cursor = this.code_mirror.getCursor();
668 672 if (cursor.line === 0) {
669 673 return true;
670 674 } else {
671 675 return false;
672 676 }
673 677 };
674 678
675 679
676 680 CodeCell.prototype.at_bottom = function () {
677 681 var cursor = this.code_mirror.getCursor();
678 682 if (cursor.line === (this.code_mirror.lineCount()-1)) {
679 683 return true;
680 684 } else {
681 685 return false;
682 686 }
683 687 };
684 688
685 689
686 690 CodeCell.prototype.fromJSON = function (data) {
687 691 console.log('Import from JSON:', data);
688 692 if (data.cell_type === 'code') {
689 693 if (data.input !== undefined) {
690 694 this.set_code(data.input);
691 695 }
692 696 if (data.prompt_number !== undefined) {
693 697 this.set_input_prompt(data.prompt_number);
694 698 } else {
695 699 this.set_input_prompt();
696 700 };
697 701 var len = data.outputs.length;
698 702 for (var i=0; i<len; i++) {
699 703 this.append_output(data.outputs[i]);
700 704 };
701 705 if (data.collapsed !== undefined) {
702 706 if (data.collapsed) {
703 707 this.collapse();
704 708 };
705 709 };
706 710 };
707 711 };
708 712
709 713
710 714 CodeCell.prototype.toJSON = function () {
711 715 var data = {};
712 716 data.input = this.get_code();
713 717 data.cell_type = 'code';
714 718 if (this.input_prompt_number !== ' ') {
715 719 data.prompt_number = this.input_prompt_number;
716 720 };
717 721 var outputs = [];
718 722 var len = this.outputs.length;
719 723 for (var i=0; i<len; i++) {
720 724 outputs[i] = this.outputs[i];
721 725 };
722 726 data.outputs = outputs;
723 727 data.language = 'python';
724 728 data.collapsed = this.collapsed;
725 729 // console.log('Export to JSON:',data);
726 730 return data;
727 731 };
728 732
729 733
730 734 IPython.CodeCell = CodeCell;
731 735
732 736 return IPython;
733 737 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now