##// END OF EJS Templates
tab pick if only one match left
Matthias BUSSONNIER -
Show More
@@ -1,771 +1,778
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 var key = { tab:9,
237 esc:8,
238 space:13,
239 shift:16,
240 enter:32,
241 // _ is 189
242 isCompSymbol : function (code) {return ((code>64 && code <=122)|| code == 189)}
243 }
236 244 var newm = new Array();
237 245 if(this.notebook.smart_completer)
238 246 {
239 247 kwargs = new Array();
240 248 other = new Array();
241 249 for(var i=0;i<matches.length; ++i){
242 250 if(matches[i].substr(-1) === '='){
243 251 kwargs.push(matches[i]);
244 252 }else{other.push(matches[i]);}
245 253 }
246 254 newm = kwargs.concat(other);
247 255 matches=newm;
248 256 }
249 257 // end sort kwargs
250 258
251 259 function sharedStart(A){
252 260 if(A.length > 1 ){
253 261 var tem1, tem2, s, A= A.slice(0).sort();
254 262 tem1= A[0];
255 263 s= tem1.length;
256 264 tem2= A.pop();
257 265 while(s && tem2.indexOf(tem1)== -1){
258 266 tem1= tem1.substring(0, --s);
259 267 }
260 268 return tem1;
261 269 }
262 270 return "";
263 271 }
264 272
265 273 if (!this.is_completing || matches.length === 0) {return;}
266 274
267 275 //try to check if the user is typing tab at least twice after a word
268 276 // and completion is "done"
269 277 fallback_on_tooltip_after=2
270 278 if(matches.length==1 && matched_text === matches[0])
271 279 {
272 280 if(this.npressed >fallback_on_tooltip_after && this.prevmatch==matched_text)
273 281 {
274 282 console.log('Ok, you really want to complete after pressing tab '+this.npressed+' times !');
275 283 console.log('You should understand that there is no (more) completion for that !');
276 284 console.log("I'll show you the tooltip, will you stop bothering me ?");
277 285 this.request_tooltip_after_time(matched_text+'(',0,this);
278 286 return;
279 287 }
280 288 this.prevmatch=matched_text
281 289 this.npressed=this.npressed+1;
282 290 }
283 291 else
284 292 {
285 293 this.prevmatch="";
286 294 this.npressed=0;
287 295 }
288 296 // end fallback on tooltip
289 297
290 298 // Real completion logic start here
291 299 var that = this;
292 300 var cur = this.completion_cursor;
293 301 var done = false;
294 302
295 303 // call to dismmiss the completer
296 304 var close = function () {
297 305 if (done) return;
298 306 done = true;
299 307 if (complete!=undefined)
300 308 {complete.remove();}
301 309 that.is_completing = false;
302 310 that.completion_cursor = null;
303 311 };
304 312
305 313 // insert the given text and exit the completer
306 314 var insert = function (selected_text) {
307 315 that.code_mirror.replaceRange(
308 316 selected_text,
309 317 {line: cur.line, ch: (cur.ch-matched_text.length)},
310 318 {line: cur.line, ch: cur.ch}
311 319 );
312 320 event.stopPropagation();
313 321 event.preventDefault();
314 322 close();
315 323 setTimeout(function(){that.code_mirror.focus();}, 50);
316 324 };
317 325
318 326 // insert the curent highlited selection and exit
319 327 var pick = function () {
320 328 insert(select.val()[0]);
321 329 };
322 330
323 // if only one match, complete to it, don't ask user
324 if (matches.length === 1) {
325 insert(matches[0]);
326 return;
327 };
328
329 331
330 332 // Define function to clear the completer, refill it with the new
331 333 // matches, update the pseuso typing field. Note that this is case
332 334 // insensitive for now
333 var complete_with = function(matches,typed_text)
335 var complete_with = function(matches,typed_text,autopick)
334 336 {
335 //clear the previous completion if any
337 // If autopick an only one match, past.
338 // Used to 'pick' when pressing tab
336 339 if (matches.length < 1) {
337 340 insert(typed_text);
341 } else if (autopick && matches.length==1) {
342 insert(matches[0]);
338 343 }
344 //clear the previous completion if any
339 345 complete.children().children().remove();
340 346 $('#asyoutype').text(typed_text);
341 347 select=$('#asyoutypeselect');
342 348 for (var i=0; i<matches.length; ++i) {
343 349 select.append($('<option/>').html(matches[i]));
344 350 }
345 351 select.children().first().attr('selected','true');
346 352 }
347 353
348 354 // create html for completer
349 355 var complete = $('<div/>').addClass('completions');
350 356 complete.attr('id','complete');
351 357 complete.append($('<p/>').attr('id', 'asyoutype').html(matched_text));//pseudo input field
352 358
353 359 var select = $('<select/>').attr('multiple','true');
354 360 select.attr('id', 'asyoutypeselect')
355 361 select.attr('size',Math.min(10,matches.length));
356 362 var pos = this.code_mirror.cursorCoords();
357 363
358 364 // TODO: I propose to remove enough horizontal pixel
359 365 // to align the text later
360 366 complete.css('left',pos.x+'px');
361 367 complete.css('top',pos.yBot+'px');
362 368 complete.append(select);
363 369
364 370 $('body').append(complete);
365 371
366 372 //do a first actual completion
367 complete_with(matches,matched_text);
368
373 fastForward = sharedStart(matches)
374 typed_characters= fastForward.substr(matched_text.length);
375 complete_with(matches,matched_text+typed_characters,true);
376 filterd=matches;
369 377 // Give focus to select, and make it filter the match as the user type
370 378 // by filtering the previous matches
371 typed_characters = "";
372 379 var downandpress = function (event,press_or_down) {
380 var code = event.which;
381 var autopick = false; // auto 'pick' if only one match
373 382 if (press_or_down === 0){
374 press=true;
375 down=false;
383 press=true; down=false; //Are we called from keypress or keydown
376 384 } else if (press_or_down == 1){
377 press=false;
378 down=true;
385 press=false; down=true;
379 386 }
380 var code = event.which;
381 if (code === 16) {
387 if (code === key.shift) {
382 388 // nothing on Shift
383 389 return;
384 390 }
385 if (code === 13 || code === 32) {
391 if (code === key.space || code === key.enter) {
386 392 // Pressing SPACE or ENTER will cause a pick
387 393 event.stopPropagation();
388 394 event.preventDefault();
389 395 pick();
390 396 } else if (code === 38 || code === 40) {
391 397 // We don't want the document keydown handler to handle UP/DOWN,
392 398 // but we want the default action.
393 399 event.stopPropagation();
394 } else if ((code>64 && code <=122)|| (code==8 && down)||(code==9 && down)){
400 //} else if ( key.isCompSymbol(code)|| (code==key.backspace)||(code==key.tab && down)){
401 } else if ( (code==key.backspace)||(code==key.tab) || press || key.isCompSymbol(code)){
395 402 // issues with _-.. on chrome at least
396 if(code != 8 && press)
403 if((code != key.backspace) && (code != key.tab) && press)
397 404 {
398 405 var newchar = String.fromCharCode(code);
399 406 typed_characters=typed_characters+newchar;
400 } else if (code == 9) {
407 } else if (code == key.tab) {
401 408 fastForward = sharedStart(filterd)
402 409 ffsub = fastForward.substr(matched_text.length+typed_characters.length);
403 410 typed_characters=typed_characters+ffsub;
404 console.log("Fast forded by :"+ffsub);
411 autopick=true;
405 412 event.stopPropagation();
406 413 event.preventDefault();
407 } else if (code == 8) {
414 } else if (code == key.backspace) {
408 415 // 8 is backspace remove 1 char cancel if
409 416 // user have erase everything, otherwise
410 417 // decrease what we filter with
411 418 if (typed_characters.length <= 0)
412 419 {
413 420 insert(matched_text)
414 421 }
415 422 typed_characters=typed_characters.substr(0,typed_characters.length-1);
416 423 }
417 424 re = new RegExp("^"+"\%?"+matched_text+typed_characters,"");
418 425 filterd= matches.filter(function(x){return re.test(x)});
419 complete_with(filterd,matched_text+typed_characters);
426 complete_with(filterd,matched_text+typed_characters,autopick);
420 427 } else if(down){ // abort only on press
421 428 // abort with what the user have pressed until now
422 console.log('aborting with keycode : '+code);
429 console.log('aborting with keycode : '+code+press);
423 430 insert(matched_text+typed_characters);
424 431 }
425 432 }
426 433 select.keydown(function (event) {
427 434 downandpress(event,1)
428 435 });
429 436 select.keypress(function (event) {
430 437 downandpress(event,0)
431 438 });
432 439 // Double click also causes a pick.
433 440 // and bind the last actions.
434 441 select.dblclick(pick);
435 442 select.blur(close);
436 443 select.focus();
437 444 };
438 445
439 446 CodeCell.prototype.toggle_line_numbers = function () {
440 447 if (this.code_mirror.getOption('lineNumbers') == false) {
441 448 this.code_mirror.setOption('lineNumbers', true);
442 449 } else {
443 450 this.code_mirror.setOption('lineNumbers', false);
444 451 }
445 452 this.code_mirror.refresh();
446 453 };
447 454
448 455 CodeCell.prototype.select = function () {
449 456 IPython.Cell.prototype.select.apply(this);
450 457 // Todo: this dance is needed because as of CodeMirror 2.12, focus is
451 458 // not causing the cursor to blink if the editor is empty initially.
452 459 // While this seems to fix the issue, this should be fixed
453 460 // in CodeMirror proper.
454 461 var s = this.code_mirror.getValue();
455 462 this.code_mirror.focus();
456 463 if (s === '') this.code_mirror.setValue('');
457 464 };
458 465
459 466
460 467 CodeCell.prototype.select_all = function () {
461 468 var start = {line: 0, ch: 0};
462 469 var nlines = this.code_mirror.lineCount();
463 470 var last_line = this.code_mirror.getLine(nlines-1);
464 471 var end = {line: nlines-1, ch: last_line.length};
465 472 this.code_mirror.setSelection(start, end);
466 473 };
467 474
468 475
469 476 CodeCell.prototype.append_output = function (json) {
470 477 this.expand();
471 478 if (json.output_type === 'pyout') {
472 479 this.append_pyout(json);
473 480 } else if (json.output_type === 'pyerr') {
474 481 this.append_pyerr(json);
475 482 } else if (json.output_type === 'display_data') {
476 483 this.append_display_data(json);
477 484 } else if (json.output_type === 'stream') {
478 485 this.append_stream(json);
479 486 };
480 487 this.outputs.push(json);
481 488 };
482 489
483 490
484 491 CodeCell.prototype.create_output_area = function () {
485 492 var oa = $("<div/>").addClass("hbox output_area");
486 493 oa.append($('<div/>').addClass('prompt'));
487 494 return oa;
488 495 };
489 496
490 497
491 498 CodeCell.prototype.append_pyout = function (json) {
492 499 n = json.prompt_number || ' ';
493 500 var toinsert = this.create_output_area();
494 501 toinsert.find('div.prompt').addClass('output_prompt').html('Out[' + n + ']:');
495 502 this.append_mime_type(json, toinsert);
496 503 this.element.find('div.output').append(toinsert);
497 504 // If we just output latex, typeset it.
498 505 if ((json.latex !== undefined) || (json.html !== undefined)) {
499 506 MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
500 507 };
501 508 };
502 509
503 510
504 511 CodeCell.prototype.append_pyerr = function (json) {
505 512 var tb = json.traceback;
506 513 if (tb !== undefined && tb.length > 0) {
507 514 var s = '';
508 515 var len = tb.length;
509 516 for (var i=0; i<len; i++) {
510 517 s = s + tb[i] + '\n';
511 518 }
512 519 s = s + '\n';
513 520 var toinsert = this.create_output_area();
514 521 this.append_text(s, toinsert);
515 522 this.element.find('div.output').append(toinsert);
516 523 };
517 524 };
518 525
519 526
520 527 CodeCell.prototype.append_stream = function (json) {
521 528 // temporary fix: if stream undefined (json file written prior to this patch),
522 529 // default to most likely stdout:
523 530 if (json.stream == undefined){
524 531 json.stream = 'stdout';
525 532 }
526 533 var subclass = "output_"+json.stream;
527 534 if (this.outputs.length > 0){
528 535 // have at least one output to consider
529 536 var last = this.outputs[this.outputs.length-1];
530 537 if (last.output_type == 'stream' && json.stream == last.stream){
531 538 // latest output was in the same stream,
532 539 // so append directly into its pre tag
533 540 this.element.find('div.'+subclass).last().find('pre').append(json.text);
534 541 return;
535 542 }
536 543 }
537 544
538 545 // If we got here, attach a new div
539 546 var toinsert = this.create_output_area();
540 547 this.append_text(json.text, toinsert, "output_stream "+subclass);
541 548 this.element.find('div.output').append(toinsert);
542 549 };
543 550
544 551
545 552 CodeCell.prototype.append_display_data = function (json) {
546 553 var toinsert = this.create_output_area();
547 554 this.append_mime_type(json, toinsert);
548 555 this.element.find('div.output').append(toinsert);
549 556 // If we just output latex, typeset it.
550 557 if ( (json.latex !== undefined) || (json.html !== undefined) ) {
551 558 MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
552 559 };
553 560 };
554 561
555 562
556 563 CodeCell.prototype.append_mime_type = function (json, element) {
557 564 if (json.html !== undefined) {
558 565 this.append_html(json.html, element);
559 566 } else if (json.latex !== undefined) {
560 567 this.append_latex(json.latex, element);
561 568 } else if (json.svg !== undefined) {
562 569 this.append_svg(json.svg, element);
563 570 } else if (json.png !== undefined) {
564 571 this.append_png(json.png, element);
565 572 } else if (json.jpeg !== undefined) {
566 573 this.append_jpeg(json.jpeg, element);
567 574 } else if (json.text !== undefined) {
568 575 this.append_text(json.text, element);
569 576 };
570 577 };
571 578
572 579
573 580 CodeCell.prototype.append_html = function (html, element) {
574 581 var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_html rendered_html");
575 582 toinsert.append(html);
576 583 element.append(toinsert);
577 584 };
578 585
579 586
580 587 CodeCell.prototype.append_text = function (data, element, extra_class) {
581 588 var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_text");
582 589 if (extra_class){
583 590 toinsert.addClass(extra_class);
584 591 }
585 592 toinsert.append($("<pre/>").html(data));
586 593 element.append(toinsert);
587 594 };
588 595
589 596
590 597 CodeCell.prototype.append_svg = function (svg, element) {
591 598 var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_svg");
592 599 toinsert.append(svg);
593 600 element.append(toinsert);
594 601 };
595 602
596 603
597 604 CodeCell.prototype.append_png = function (png, element) {
598 605 var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_png");
599 606 toinsert.append($("<img/>").attr('src','data:image/png;base64,'+png));
600 607 element.append(toinsert);
601 608 };
602 609
603 610
604 611 CodeCell.prototype.append_jpeg = function (jpeg, element) {
605 612 var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_jpeg");
606 613 toinsert.append($("<img/>").attr('src','data:image/jpeg;base64,'+jpeg));
607 614 element.append(toinsert);
608 615 };
609 616
610 617
611 618 CodeCell.prototype.append_latex = function (latex, element) {
612 619 // This method cannot do the typesetting because the latex first has to
613 620 // be on the page.
614 621 var toinsert = $("<div/>").addClass("box_flex1 output_subarea output_latex");
615 622 toinsert.append(latex);
616 623 element.append(toinsert);
617 624 };
618 625
619 626
620 627 CodeCell.prototype.clear_output = function (stdout, stderr, other) {
621 628 var output_div = this.element.find("div.output");
622 629 if (stdout && stderr && other){
623 630 // clear all, no need for logic
624 631 output_div.html("");
625 632 this.outputs = [];
626 633 return;
627 634 }
628 635 // remove html output
629 636 // each output_subarea that has an identifying class is in an output_area
630 637 // which is the element to be removed.
631 638 if (stdout){
632 639 output_div.find("div.output_stdout").parent().remove();
633 640 }
634 641 if (stderr){
635 642 output_div.find("div.output_stderr").parent().remove();
636 643 }
637 644 if (other){
638 645 output_div.find("div.output_subarea").not("div.output_stderr").not("div.output_stdout").parent().remove();
639 646 }
640 647
641 648 // remove cleared outputs from JSON list:
642 649 for (var i = this.outputs.length - 1; i >= 0; i--){
643 650 var out = this.outputs[i];
644 651 var output_type = out.output_type;
645 652 if (output_type == "display_data" && other){
646 653 this.outputs.splice(i,1);
647 654 }else if (output_type == "stream"){
648 655 if (stdout && out.stream == "stdout"){
649 656 this.outputs.splice(i,1);
650 657 }else if (stderr && out.stream == "stderr"){
651 658 this.outputs.splice(i,1);
652 659 }
653 660 }
654 661 }
655 662 };
656 663
657 664
658 665 CodeCell.prototype.clear_input = function () {
659 666 this.code_mirror.setValue('');
660 667 };
661 668
662 669
663 670 CodeCell.prototype.collapse = function () {
664 671 if (!this.collapsed) {
665 672 this.element.find('div.output').hide();
666 673 this.collapsed = true;
667 674 };
668 675 };
669 676
670 677
671 678 CodeCell.prototype.expand = function () {
672 679 if (this.collapsed) {
673 680 this.element.find('div.output').show();
674 681 this.collapsed = false;
675 682 };
676 683 };
677 684
678 685
679 686 CodeCell.prototype.toggle_output = function () {
680 687 if (this.collapsed) {
681 688 this.expand();
682 689 } else {
683 690 this.collapse();
684 691 };
685 692 };
686 693
687 694 CodeCell.prototype.set_input_prompt = function (number) {
688 695 var n = number || '&nbsp;';
689 696 this.input_prompt_number = n;
690 697 this.element.find('div.input_prompt').html('In&nbsp;[' + n + ']:');
691 698 };
692 699
693 700
694 701 CodeCell.prototype.get_code = function () {
695 702 return this.code_mirror.getValue();
696 703 };
697 704
698 705
699 706 CodeCell.prototype.set_code = function (code) {
700 707 return this.code_mirror.setValue(code);
701 708 };
702 709
703 710
704 711 CodeCell.prototype.at_top = function () {
705 712 var cursor = this.code_mirror.getCursor();
706 713 if (cursor.line === 0) {
707 714 return true;
708 715 } else {
709 716 return false;
710 717 }
711 718 };
712 719
713 720
714 721 CodeCell.prototype.at_bottom = function () {
715 722 var cursor = this.code_mirror.getCursor();
716 723 if (cursor.line === (this.code_mirror.lineCount()-1)) {
717 724 return true;
718 725 } else {
719 726 return false;
720 727 }
721 728 };
722 729
723 730
724 731 CodeCell.prototype.fromJSON = function (data) {
725 732 console.log('Import from JSON:', data);
726 733 if (data.cell_type === 'code') {
727 734 if (data.input !== undefined) {
728 735 this.set_code(data.input);
729 736 }
730 737 if (data.prompt_number !== undefined) {
731 738 this.set_input_prompt(data.prompt_number);
732 739 } else {
733 740 this.set_input_prompt();
734 741 };
735 742 var len = data.outputs.length;
736 743 for (var i=0; i<len; i++) {
737 744 this.append_output(data.outputs[i]);
738 745 };
739 746 if (data.collapsed !== undefined) {
740 747 if (data.collapsed) {
741 748 this.collapse();
742 749 };
743 750 };
744 751 };
745 752 };
746 753
747 754
748 755 CodeCell.prototype.toJSON = function () {
749 756 var data = {};
750 757 data.input = this.get_code();
751 758 data.cell_type = 'code';
752 759 if (this.input_prompt_number !== ' ') {
753 760 data.prompt_number = this.input_prompt_number;
754 761 };
755 762 var outputs = [];
756 763 var len = this.outputs.length;
757 764 for (var i=0; i<len; i++) {
758 765 outputs[i] = this.outputs[i];
759 766 };
760 767 data.outputs = outputs;
761 768 data.language = 'python';
762 769 data.collapsed = this.collapsed;
763 770 // console.log('Export to JSON:',data);
764 771 return data;
765 772 };
766 773
767 774
768 775 IPython.CodeCell = CodeCell;
769 776
770 777 return IPython;
771 778 }(IPython));
@@ -1,235 +1,235
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 // Kernel
10 10 //============================================================================
11 11
12 12 var IPython = (function (IPython) {
13 13
14 14 var utils = IPython.utils;
15 15
16 16 var Kernel = function () {
17 17 this.kernel_id = null;
18 18 this.shell_channel = null;
19 19 this.iopub_channel = null;
20 20 this.base_url = $('body').data('baseKernelUrl') + "kernels";
21 21 this.running = false;
22 22 this.username = "username";
23 23 this.session_id = utils.uuid();
24 24
25 25 if (typeof(WebSocket) !== 'undefined') {
26 26 this.WebSocket = WebSocket;
27 27 } else if (typeof(MozWebSocket) !== 'undefined') {
28 28 this.WebSocket = MozWebSocket;
29 29 } else {
30 30 alert('Your browser does not have WebSocket support, please try Chrome, Safari or Firefox β‰₯ 6. Firefox 4 and 5 are also supported by you have to enable WebSockets in about:config.');
31 31 };
32 32 };
33 33
34 34
35 35 Kernel.prototype.get_msg = function (msg_type, content) {
36 36 var msg = {
37 37 header : {
38 38 msg_id : utils.uuid(),
39 39 username : this.username,
40 40 session : this.session_id,
41 41 msg_type : msg_type
42 42 },
43 43 content : content,
44 44 parent_header : {}
45 45 };
46 46 return msg;
47 47 };
48 48
49 49 Kernel.prototype.start = function (notebook_id, callback) {
50 50 var that = this;
51 51 if (!this.running) {
52 52 var qs = $.param({notebook:notebook_id});
53 53 var url = this.base_url + '?' + qs;
54 54 $.post(url,
55 55 function (kernel_id) {
56 56 that._handle_start_kernel(kernel_id, callback);
57 57 },
58 58 'json'
59 59 );
60 60 };
61 61 };
62 62
63 63
64 64 Kernel.prototype.restart = function (callback) {
65 65 IPython.kernel_status_widget.status_restarting();
66 66 var url = this.kernel_url + "/restart";
67 67 var that = this;
68 68 if (this.running) {
69 69 this.stop_channels();
70 70 $.post(url,
71 71 function (kernel_id) {
72 72 that._handle_start_kernel(kernel_id, callback);
73 73 },
74 74 'json'
75 75 );
76 76 };
77 77 };
78 78
79 79
80 80 Kernel.prototype._handle_start_kernel = function (json, callback) {
81 81 this.running = true;
82 82 this.kernel_id = json.kernel_id;
83 83 this.ws_url = json.ws_url;
84 84 this.kernel_url = this.base_url + "/" + this.kernel_id;
85 85 this.start_channels();
86 86 callback();
87 87 IPython.kernel_status_widget.status_idle();
88 88 };
89 89
90 90 Kernel.prototype._websocket_closed = function(ws_url, early){
91 91 var msg;
92 92 var parent_item = $('body');
93 93 if (early) {
94 94 msg = "Websocket connection to " + ws_url + " could not be established.<br/>" +
95 95 " You will NOT be able to run code.<br/>" +
96 96 " Your browser may not be compatible with the websocket version in the server," +
97 97 " or if the url does not look right, there could be an error in the" +
98 98 " server's configuration.";
99 99 } else {
100 100 msg = "Websocket connection closed unexpectedly.<br/>" +
101 101 " The kernel will no longer be responsive.";
102 102 }
103 103 var dialog = $('<div/>');
104 104 dialog.html(msg);
105 105 parent_item.append(dialog);
106 106 dialog.dialog({
107 107 resizable: false,
108 108 modal: true,
109 109 title: "Websocket closed",
110 110 buttons : {
111 111 "Okay": function () {
112 112 $(this).dialog('close');
113 113 }
114 114 }
115 115 });
116 116
117 117 };
118 118
119 119 Kernel.prototype.start_channels = function () {
120 120 var that = this;
121 121 this.stop_channels();
122 122 var ws_url = this.ws_url + this.kernel_url;
123 123 console.log("Starting WS:", ws_url);
124 124 this.shell_channel = new this.WebSocket(ws_url + "/shell");
125 125 this.iopub_channel = new this.WebSocket(ws_url + "/iopub");
126 126 send_cookie = function(){
127 127 this.send(document.cookie);
128 128 };
129 129 var already_called_onclose = false; // only alert once
130 130 ws_closed_early = function(evt){
131 131 if (already_called_onclose){
132 132 return;
133 133 }
134 134 already_called_onclose = true;
135 135 if ( ! evt.wasClean ){
136 136 that._websocket_closed(ws_url, true);
137 137 }
138 138 };
139 139 ws_closed_late = function(evt){
140 140 if (already_called_onclose){
141 141 return;
142 142 }
143 143 already_called_onclose = true;
144 144 if ( ! evt.wasClean ){
145 145 that._websocket_closed(ws_url, false);
146 146 }
147 147 };
148 148 this.shell_channel.onopen = send_cookie;
149 149 this.shell_channel.onclose = ws_closed_early;
150 150 this.iopub_channel.onopen = send_cookie;
151 151 this.iopub_channel.onclose = ws_closed_early;
152 152 // switch from early-close to late-close message after 1s
153 153 setTimeout(function(){
154 154 that.shell_channel.onclose = ws_closed_late;
155 155 that.iopub_channel.onclose = ws_closed_late;
156 156 }, 1000);
157 157 };
158 158
159 159
160 160 Kernel.prototype.stop_channels = function () {
161 161 if (this.shell_channel !== null) {
162 162 this.shell_channel.onclose = function (evt) {};
163 163 this.shell_channel.close();
164 164 this.shell_channel = null;
165 165 };
166 166 if (this.iopub_channel !== null) {
167 167 this.iopub_channel.onclose = function (evt) {};
168 168 this.iopub_channel.close();
169 169 this.iopub_channel = null;
170 170 };
171 171 };
172 172
173 173 Kernel.prototype.object_info_request = function (objname) {
174 if(typeof(objname)!=null)
174 if(typeof(objname)!=null && objname!=null)
175 175 {
176 176 var content = {
177 177 oname : objname.toString(),
178 178 };
179 179 var msg = this.get_msg("object_info_request", content);
180 180 this.shell_channel.send(JSON.stringify(msg));
181 181 return msg.header.msg_id;
182 182 }
183 183 return;
184 184 }
185 185
186 186 Kernel.prototype.execute = function (code) {
187 187 var content = {
188 188 code : code,
189 189 silent : false,
190 190 user_variables : [],
191 191 user_expressions : {},
192 192 allow_stdin : false
193 193 };
194 194 var msg = this.get_msg("execute_request", content);
195 195 this.shell_channel.send(JSON.stringify(msg));
196 196 return msg.header.msg_id;
197 197 };
198 198
199 199
200 200 Kernel.prototype.complete = function (line, cursor_pos) {
201 201 var content = {
202 202 text : '',
203 203 line : line,
204 204 cursor_pos : cursor_pos
205 205 };
206 206 var msg = this.get_msg("complete_request", content);
207 207 this.shell_channel.send(JSON.stringify(msg));
208 208 return msg.header.msg_id;
209 209 };
210 210
211 211
212 212 Kernel.prototype.interrupt = function () {
213 213 if (this.running) {
214 214 $.post(this.kernel_url + "/interrupt");
215 215 };
216 216 };
217 217
218 218
219 219 Kernel.prototype.kill = function () {
220 220 if (this.running) {
221 221 this.running = false;
222 222 var settings = {
223 223 cache : false,
224 224 type : "DELETE"
225 225 };
226 226 $.ajax(this.kernel_url, settings);
227 227 };
228 228 };
229 229
230 230 IPython.Kernel = Kernel;
231 231
232 232 return IPython;
233 233
234 234 }(IPython));
235 235
General Comments 0
You need to be logged in to leave comments. Login now