##// END OF EJS Templates
Add YUIDoc in notebook.js.
David Wyde -
Show More
@@ -14,6 +14,14 b' var IPython = (function (IPython) {'
14 var utils = IPython.utils;
14 var utils = IPython.utils;
15 var key = IPython.utils.keycodes;
15 var key = IPython.utils.keycodes;
16
16
17 /**
18 * A notebook contains and manages cells.
19 *
20 * @class Notebook
21 * @constructor
22 * @param {String} selector A jQuery selector for the notebook's DOM element
23 * @param {Object} [options] A config object
24 */
17 var Notebook = function (selector, options) {
25 var Notebook = function (selector, options) {
18 var options = options || {};
26 var options = options || {};
19 this._baseProjectUrl = options.baseProjectUrl;
27 this._baseProjectUrl = options.baseProjectUrl;
@@ -44,16 +52,30 b' var IPython = (function (IPython) {'
44 this.bind_events();
52 this.bind_events();
45 };
53 };
46
54
47
55 /**
56 * Tweak the notebook's CSS style.
57 *
58 * @method style
59 */
48 Notebook.prototype.style = function () {
60 Notebook.prototype.style = function () {
49 $('div#notebook').addClass('border-box-sizing');
61 $('div#notebook').addClass('border-box-sizing');
50 };
62 };
51
63
64 /**
65 * Get the root URL of the notebook server.
66 *
67 * @method baseProjectUrl
68 * @return {String} The base project URL
69 */
52 Notebook.prototype.baseProjectUrl = function(){
70 Notebook.prototype.baseProjectUrl = function(){
53 return this._baseProjectUrl || $('body').data('baseProjectUrl');
71 return this._baseProjectUrl || $('body').data('baseProjectUrl');
54 };
72 };
55
73
56
74 /**
75 * Create an HTML and CSS representation of the notebook.
76 *
77 * @method create_elements
78 */
57 Notebook.prototype.create_elements = function () {
79 Notebook.prototype.create_elements = function () {
58 // We add this end_space div to the end of the notebook div to:
80 // We add this end_space div to the end of the notebook div to:
59 // i) provide a margin between the last cell and the end of the notebook
81 // i) provide a margin between the last cell and the end of the notebook
@@ -70,7 +92,11 b' var IPython = (function (IPython) {'
70 $('div#notebook').addClass('border-box-sizing');
92 $('div#notebook').addClass('border-box-sizing');
71 };
93 };
72
94
73
95 /**
96 * Bind JavaScript events: key presses and custom IPython events.
97 *
98 * @method bind_events
99 */
74 Notebook.prototype.bind_events = function () {
100 Notebook.prototype.bind_events = function () {
75 var that = this;
101 var that = this;
76
102
@@ -319,6 +345,14 b' var IPython = (function (IPython) {'
319 });
345 });
320 };
346 };
321
347
348 /**
349 * Scroll the top of the page to a given cell.
350 *
351 * @method scroll_to_cell
352 * @param {Number} cell_number An index of the cell to view
353 * @param {Number} time Animation time in milliseconds
354 * @return {Number} Pixel offset from the top of the container
355 */
322 Notebook.prototype.scroll_to_cell = function (cell_number, time) {
356 Notebook.prototype.scroll_to_cell = function (cell_number, time) {
323 var cells = this.get_cells();
357 var cells = this.get_cells();
324 var time = time || 0;
358 var time = time || 0;
@@ -329,12 +363,20 b' var IPython = (function (IPython) {'
329 return scroll_value;
363 return scroll_value;
330 };
364 };
331
365
332
366 /**
367 * Scroll to the bottom of the page.
368 *
369 * @method scroll_to_bottom
370 */
333 Notebook.prototype.scroll_to_bottom = function () {
371 Notebook.prototype.scroll_to_bottom = function () {
334 this.element.animate({scrollTop:this.element.get(0).scrollHeight}, 0);
372 this.element.animate({scrollTop:this.element.get(0).scrollHeight}, 0);
335 };
373 };
336
374
337
375 /**
376 * Scroll to the top of the page.
377 *
378 * @method scroll_to_top
379 */
338 Notebook.prototype.scroll_to_top = function () {
380 Notebook.prototype.scroll_to_top = function () {
339 this.element.animate({scrollTop:0}, 0);
381 this.element.animate({scrollTop:0}, 0);
340 };
382 };
@@ -342,11 +384,23 b' var IPython = (function (IPython) {'
342
384
343 // Cell indexing, retrieval, etc.
385 // Cell indexing, retrieval, etc.
344
386
387 /**
388 * Get all cell elements in the notebook.
389 *
390 * @method get_cell_elements
391 * @return {jQuery} A selector of all cell elements
392 */
345 Notebook.prototype.get_cell_elements = function () {
393 Notebook.prototype.get_cell_elements = function () {
346 return this.element.children("div.cell");
394 return this.element.children("div.cell");
347 };
395 };
348
396
349
397 /**
398 * Get a particular cell element.
399 *
400 * @method get_cell_element
401 * @param {Number} index An index of a cell to select
402 * @return {jQuery} A selector of the given cell.
403 */
350 Notebook.prototype.get_cell_element = function (index) {
404 Notebook.prototype.get_cell_element = function (index) {
351 var result = null;
405 var result = null;
352 var e = this.get_cell_elements().eq(index);
406 var e = this.get_cell_elements().eq(index);
@@ -356,12 +410,22 b' var IPython = (function (IPython) {'
356 return result;
410 return result;
357 };
411 };
358
412
359
413 /**
360 Notebook.prototype.ncells = function (cell) {
414 * Count the cells in this notebook.
415 *
416 * @method ncells
417 * @return {Number} The number of cells in this notebook
418 */
419 Notebook.prototype.ncells = function () {
361 return this.get_cell_elements().length;
420 return this.get_cell_elements().length;
362 };
421 };
363
422
364
423 /**
424 * Get all Cell objects in this notebook.
425 *
426 * @method get_cells
427 * @return {Array} This notebook's Cell objects
428 */
365 // TODO: we are often calling cells as cells()[i], which we should optimize
429 // TODO: we are often calling cells as cells()[i], which we should optimize
366 // to cells(i) or a new method.
430 // to cells(i) or a new method.
367 Notebook.prototype.get_cells = function () {
431 Notebook.prototype.get_cells = function () {
@@ -370,7 +434,13 b' var IPython = (function (IPython) {'
370 });
434 });
371 };
435 };
372
436
373
437 /**
438 * Get a Cell object from this notebook.
439 *
440 * @method get_cell
441 * @param {Number} index An index of a cell to retrieve
442 * @return {Cell} A particular cell
443 */
374 Notebook.prototype.get_cell = function (index) {
444 Notebook.prototype.get_cell = function (index) {
375 var result = null;
445 var result = null;
376 var ce = this.get_cell_element(index);
446 var ce = this.get_cell_element(index);
@@ -380,7 +450,13 b' var IPython = (function (IPython) {'
380 return result;
450 return result;
381 }
451 }
382
452
383
453 /**
454 * Get the cell below a given cell.
455 *
456 * @method get_next_cell
457 * @param {Cell} cell The provided cell
458 * @return {Cell} The next cell
459 */
384 Notebook.prototype.get_next_cell = function (cell) {
460 Notebook.prototype.get_next_cell = function (cell) {
385 var result = null;
461 var result = null;
386 var index = this.find_cell_index(cell);
462 var index = this.find_cell_index(cell);
@@ -390,8 +466,16 b' var IPython = (function (IPython) {'
390 return result;
466 return result;
391 }
467 }
392
468
393
469 /**
470 * Get the cell above a given cell.
471 *
472 * @method get_prev_cell
473 * @param {Cell} cell The provided cell
474 * @return {Cell} The previous cell
475 */
394 Notebook.prototype.get_prev_cell = function (cell) {
476 Notebook.prototype.get_prev_cell = function (cell) {
477 // TODO: off-by-one
478 // nb.get_prev_cell(nb.get_cell(1)) is null
395 var result = null;
479 var result = null;
396 var index = this.find_cell_index(cell);
480 var index = this.find_cell_index(cell);
397 if (index !== null && index > 1) {
481 if (index !== null && index > 1) {
@@ -399,7 +483,14 b' var IPython = (function (IPython) {'
399 }
483 }
400 return result;
484 return result;
401 }
485 }
402
486
487 /**
488 * Get the numeric index of a given cell.
489 *
490 * @method find_cell_index
491 * @param {Cell} cell The provided cell
492 * @return {Number} The cell's numeric index
493 */
403 Notebook.prototype.find_cell_index = function (cell) {
494 Notebook.prototype.find_cell_index = function (cell) {
404 var result = null;
495 var result = null;
405 this.get_cell_elements().filter(function (index) {
496 this.get_cell_elements().filter(function (index) {
@@ -410,7 +501,13 b' var IPython = (function (IPython) {'
410 return result;
501 return result;
411 };
502 };
412
503
413
504 /**
505 * Get a given index , or the selected index if none is provided.
506 *
507 * @method index_or_selected
508 * @param {Number} index A cell's index
509 * @return {Number} The given index, or selected index if none is provided.
510 */
414 Notebook.prototype.index_or_selected = function (index) {
511 Notebook.prototype.index_or_selected = function (index) {
415 var i;
512 var i;
416 if (index === undefined || index === null) {
513 if (index === undefined || index === null) {
@@ -424,13 +521,23 b' var IPython = (function (IPython) {'
424 return i;
521 return i;
425 };
522 };
426
523
427
524 /**
525 * Get the currently selected cell.
526 * @method get_selected_cell
527 * @return {Cell} The selected cell
528 */
428 Notebook.prototype.get_selected_cell = function () {
529 Notebook.prototype.get_selected_cell = function () {
429 var index = this.get_selected_index();
530 var index = this.get_selected_index();
430 return this.get_cell(index);
531 return this.get_cell(index);
431 };
532 };
432
533
433
534 /**
535 * Check whether a cell index is valid.
536 *
537 * @method is_valid_cell_index
538 * @param {Number} index A cell index
539 * @return True if the index is valid, false otherwise
540 */
434 Notebook.prototype.is_valid_cell_index = function (index) {
541 Notebook.prototype.is_valid_cell_index = function (index) {
435 if (index !== null && index >= 0 && index < this.ncells()) {
542 if (index !== null && index >= 0 && index < this.ncells()) {
436 return true;
543 return true;
@@ -439,6 +546,12 b' var IPython = (function (IPython) {'
439 };
546 };
440 }
547 }
441
548
549 /**
550 * Get the index of the currently selected cell.
551
552 * @method get_selected_index
553 * @return {Number} The selected cell's numeric index
554 */
442 Notebook.prototype.get_selected_index = function () {
555 Notebook.prototype.get_selected_index = function () {
443 var result = null;
556 var result = null;
444 this.get_cell_elements().filter(function (index) {
557 this.get_cell_elements().filter(function (index) {
@@ -452,6 +565,13 b' var IPython = (function (IPython) {'
452
565
453 // Cell selection.
566 // Cell selection.
454
567
568 /**
569 * Programmatically select a cell.
570 *
571 * @method select
572 * @param {Number} index A cell's index
573 * @return {Notebook} This notebook
574 */
455 Notebook.prototype.select = function (index) {
575 Notebook.prototype.select = function (index) {
456 if (this.is_valid_cell_index(index)) {
576 if (this.is_valid_cell_index(index)) {
457 var sindex = this.get_selected_index()
577 var sindex = this.get_selected_index()
@@ -473,14 +593,24 b' var IPython = (function (IPython) {'
473 return this;
593 return this;
474 };
594 };
475
595
476
596 /**
597 * Programmatically select the next cell.
598 *
599 * @method select_next
600 * @return {Notebook} This notebook
601 */
477 Notebook.prototype.select_next = function () {
602 Notebook.prototype.select_next = function () {
478 var index = this.get_selected_index();
603 var index = this.get_selected_index();
479 this.select(index+1);
604 this.select(index+1);
480 return this;
605 return this;
481 };
606 };
482
607
483
608 /**
609 * Programmatically select the previous cell.
610 *
611 * @method select_prev
612 * @return {Notebook} This notebook
613 */
484 Notebook.prototype.select_prev = function () {
614 Notebook.prototype.select_prev = function () {
485 var index = this.get_selected_index();
615 var index = this.get_selected_index();
486 this.select(index-1);
616 this.select(index-1);
@@ -491,9 +621,11 b' var IPython = (function (IPython) {'
491 // Cell movement
621 // Cell movement
492
622
493 /**
623 /**
494 * Move given (or selected) cell up and select it
624 * Move given (or selected) cell up and select it.
625 *
495 * @method move_cell_up
626 * @method move_cell_up
496 * @param [index] {integer} cell index
627 * @param [index] {integer} cell index
628 * @return {Notebook} This notebook
497 **/
629 **/
498 Notebook.prototype.move_cell_up = function (index) {
630 Notebook.prototype.move_cell_up = function (index) {
499 var i = this.index_or_selected(index);
631 var i = this.index_or_selected(index);
@@ -513,8 +645,10 b' var IPython = (function (IPython) {'
513
645
514 /**
646 /**
515 * Move given (or selected) cell down and select it
647 * Move given (or selected) cell down and select it
648 *
516 * @method move_cell_down
649 * @method move_cell_down
517 * @param [index] {integer} cell index
650 * @param [index] {integer} cell index
651 * @return {Notebook} This notebook
518 **/
652 **/
519 Notebook.prototype.move_cell_down = function (index) {
653 Notebook.prototype.move_cell_down = function (index) {
520 var i = this.index_or_selected(index);
654 var i = this.index_or_selected(index);
@@ -534,6 +668,13 b' var IPython = (function (IPython) {'
534
668
535 // Insertion, deletion.
669 // Insertion, deletion.
536
670
671 /**
672 * Delete a cell from the notebook.
673 *
674 * @method delete_cell
675 * @param [index] A cell's numeric index
676 * @return {Notebook} This notebook
677 */
537 Notebook.prototype.delete_cell = function (index) {
678 Notebook.prototype.delete_cell = function (index) {
538 var i = this.index_or_selected(index);
679 var i = this.index_or_selected(index);
539 var cell = this.get_selected_cell();
680 var cell = this.get_selected_cell();
@@ -556,9 +697,6 b' var IPython = (function (IPython) {'
556 return this;
697 return this;
557 };
698 };
558
699
559
560
561
562 /**
700 /**
563 * Insert a cell so that after insertion the cell is at given index.
701 * Insert a cell so that after insertion the cell is at given index.
564 *
702 *
@@ -566,6 +704,7 b' var IPython = (function (IPython) {'
566 *
704 *
567 * Index will be brought back into the accissible range [0,n]
705 * Index will be brought back into the accissible range [0,n]
568 *
706 *
707 * @method insert_cell_at_index
569 * @param type {string} in ['code','html','markdown','heading']
708 * @param type {string} in ['code','html','markdown','heading']
570 * @param [index] {int} a valid index where to inser cell
709 * @param [index] {int} a valid index where to inser cell
571 *
710 *
@@ -605,6 +744,7 b' var IPython = (function (IPython) {'
605 /**
744 /**
606 * Insert an element at given cell index.
745 * Insert an element at given cell index.
607 *
746 *
747 * @method _insert_element_at_index
608 * @param element {dom element} a cell element
748 * @param element {dom element} a cell element
609 * @param [index] {int} a valid index where to inser cell
749 * @param [index] {int} a valid index where to inser cell
610 * @private
750 * @private
@@ -644,6 +784,7 b' var IPython = (function (IPython) {'
644 *
784 *
645 * default index value is the one of currently selected cell
785 * default index value is the one of currently selected cell
646 *
786 *
787 * @method insert_cell_above
647 * @param type {string} cell type
788 * @param type {string} cell type
648 * @param [index] {integer}
789 * @param [index] {integer}
649 *
790 *
@@ -677,7 +818,7 b' var IPython = (function (IPython) {'
677 * Insert cell at end of notebook
818 * Insert cell at end of notebook
678 *
819 *
679 * @method insert_cell_at_bottom
820 * @method insert_cell_at_bottom
680 * @param type {String} cell type
821 * @param {String} type cell type
681 *
822 *
682 * @return the added cell; or null
823 * @return the added cell; or null
683 **/
824 **/
@@ -686,8 +827,12 b' var IPython = (function (IPython) {'
686 return this.insert_cell_below(type,len-1);
827 return this.insert_cell_below(type,len-1);
687 };
828 };
688
829
689
830 /**
690
831 * Turn a cell into a code cell.
832 *
833 * @method to_code
834 * @param {Number} [index] A cell's index
835 */
691 Notebook.prototype.to_code = function (index) {
836 Notebook.prototype.to_code = function (index) {
692 var i = this.index_or_selected(index);
837 var i = this.index_or_selected(index);
693 if (this.is_valid_cell_index(i)) {
838 if (this.is_valid_cell_index(i)) {
@@ -709,7 +854,12 b' var IPython = (function (IPython) {'
709 };
854 };
710 };
855 };
711
856
712
857 /**
858 * Turn a cell into a Markdown cell.
859 *
860 * @method to_markdown
861 * @param {Number} [index] A cell's index
862 */
713 Notebook.prototype.to_markdown = function (index) {
863 Notebook.prototype.to_markdown = function (index) {
714 var i = this.index_or_selected(index);
864 var i = this.index_or_selected(index);
715 if (this.is_valid_cell_index(i)) {
865 if (this.is_valid_cell_index(i)) {
@@ -733,8 +883,14 b' var IPython = (function (IPython) {'
733 };
883 };
734 };
884 };
735
885
736
886 /**
887 * Turn a cell into an HTML cell.
888 *
889 * @method to_html
890 * @param {Number} [index] A cell's index
891 */
737 Notebook.prototype.to_html = function (index) {
892 Notebook.prototype.to_html = function (index) {
893 // TODO: remove? This is never called
738 var i = this.index_or_selected(index);
894 var i = this.index_or_selected(index);
739 if (this.is_valid_cell_index(i)) {
895 if (this.is_valid_cell_index(i)) {
740 var source_element = this.get_cell_element(i);
896 var source_element = this.get_cell_element(i);
@@ -758,7 +914,12 b' var IPython = (function (IPython) {'
758 };
914 };
759 };
915 };
760
916
761
917 /**
918 * Turn a cell into a raw text cell.
919 *
920 * @method to_raw
921 * @param {Number} [index] A cell's index
922 */
762 Notebook.prototype.to_raw = function (index) {
923 Notebook.prototype.to_raw = function (index) {
763 var i = this.index_or_selected(index);
924 var i = this.index_or_selected(index);
764 if (this.is_valid_cell_index(i)) {
925 if (this.is_valid_cell_index(i)) {
@@ -783,7 +944,13 b' var IPython = (function (IPython) {'
783 };
944 };
784 };
945 };
785
946
786
947 /**
948 * Turn a cell into a heading cell.
949 *
950 * @method to_heading
951 * @param {Number} [index] A cell's index
952 * @param {Number} [level] A heading level (e.g., 1 becomes &lt;h1&gt;)
953 */
787 Notebook.prototype.to_heading = function (index, level) {
954 Notebook.prototype.to_heading = function (index, level) {
788 level = level || 1;
955 level = level || 1;
789 var i = this.index_or_selected(index);
956 var i = this.index_or_selected(index);
@@ -818,6 +985,11 b' var IPython = (function (IPython) {'
818
985
819 // Cut/Copy/Paste
986 // Cut/Copy/Paste
820
987
988 /**
989 * Enable UI elements for pasting cells.
990 *
991 * @method enable_paste
992 */
821 Notebook.prototype.enable_paste = function () {
993 Notebook.prototype.enable_paste = function () {
822 var that = this;
994 var that = this;
823 if (!this.paste_enabled) {
995 if (!this.paste_enabled) {
@@ -831,7 +1003,11 b' var IPython = (function (IPython) {'
831 };
1003 };
832 };
1004 };
833
1005
834
1006 /**
1007 * Disable UI elements for pasting cells.
1008 *
1009 * @method disable_paste
1010 */
835 Notebook.prototype.disable_paste = function () {
1011 Notebook.prototype.disable_paste = function () {
836 if (this.paste_enabled) {
1012 if (this.paste_enabled) {
837 $('#paste_cell_replace').addClass('ui-state-disabled').off('click');
1013 $('#paste_cell_replace').addClass('ui-state-disabled').off('click');
@@ -841,19 +1017,32 b' var IPython = (function (IPython) {'
841 };
1017 };
842 };
1018 };
843
1019
844
1020 /**
1021 * Cut a cell.
1022 *
1023 * @method cut_cell
1024 */
845 Notebook.prototype.cut_cell = function () {
1025 Notebook.prototype.cut_cell = function () {
846 this.copy_cell();
1026 this.copy_cell();
847 this.delete_cell();
1027 this.delete_cell();
848 }
1028 }
849
1029
1030 /**
1031 * Copy a cell.
1032 *
1033 * @method copy_cell
1034 */
850 Notebook.prototype.copy_cell = function () {
1035 Notebook.prototype.copy_cell = function () {
851 var cell = this.get_selected_cell();
1036 var cell = this.get_selected_cell();
852 this.clipboard = cell.toJSON();
1037 this.clipboard = cell.toJSON();
853 this.enable_paste();
1038 this.enable_paste();
854 };
1039 };
855
1040
856
1041 /**
1042 * Replace the selected cell with a cell in the clipboard.
1043 *
1044 * @method paste_cell_replace
1045 */
857 Notebook.prototype.paste_cell_replace = function () {
1046 Notebook.prototype.paste_cell_replace = function () {
858 if (this.clipboard !== null && this.paste_enabled) {
1047 if (this.clipboard !== null && this.paste_enabled) {
859 var cell_data = this.clipboard;
1048 var cell_data = this.clipboard;
@@ -865,7 +1054,11 b' var IPython = (function (IPython) {'
865 };
1054 };
866 };
1055 };
867
1056
868
1057 /**
1058 * Paste a cell from the clipboard above the selected cell.
1059 *
1060 * @method paste_cell_above
1061 */
869 Notebook.prototype.paste_cell_above = function () {
1062 Notebook.prototype.paste_cell_above = function () {
870 if (this.clipboard !== null && this.paste_enabled) {
1063 if (this.clipboard !== null && this.paste_enabled) {
871 var cell_data = this.clipboard;
1064 var cell_data = this.clipboard;
@@ -874,7 +1067,11 b' var IPython = (function (IPython) {'
874 };
1067 };
875 };
1068 };
876
1069
877
1070 /**
1071 * Paste a cell from the clipboard below the selected cell.
1072 *
1073 * @method paste_cell_below
1074 */
878 Notebook.prototype.paste_cell_below = function () {
1075 Notebook.prototype.paste_cell_below = function () {
879 if (this.clipboard !== null && this.paste_enabled) {
1076 if (this.clipboard !== null && this.paste_enabled) {
880 var cell_data = this.clipboard;
1077 var cell_data = this.clipboard;
@@ -885,6 +1082,11 b' var IPython = (function (IPython) {'
885
1082
886 // Cell undelete
1083 // Cell undelete
887
1084
1085 /**
1086 * Restore the most recently deleted cell.
1087 *
1088 * @method undelete
1089 */
888 Notebook.prototype.undelete = function() {
1090 Notebook.prototype.undelete = function() {
889 if (this.undelete_backup !== null && this.undelete_index !== null) {
1091 if (this.undelete_backup !== null && this.undelete_index !== null) {
890 var current_index = this.get_selected_index();
1092 var current_index = this.get_selected_index();
@@ -914,6 +1116,11 b' var IPython = (function (IPython) {'
914
1116
915 // Split/merge
1117 // Split/merge
916
1118
1119 /**
1120 * Split the selected cell into two, at the cursor.
1121 *
1122 * @method split_cell
1123 */
917 Notebook.prototype.split_cell = function () {
1124 Notebook.prototype.split_cell = function () {
918 // Todo: implement spliting for other cell types.
1125 // Todo: implement spliting for other cell types.
919 var cell = this.get_selected_cell();
1126 var cell = this.get_selected_cell();
@@ -942,7 +1149,11 b' var IPython = (function (IPython) {'
942 };
1149 };
943 };
1150 };
944
1151
945
1152 /**
1153 * Combine the selected cell into the cell above it.
1154 *
1155 * @method merge_cell_above
1156 */
946 Notebook.prototype.merge_cell_above = function () {
1157 Notebook.prototype.merge_cell_above = function () {
947 var index = this.get_selected_index();
1158 var index = this.get_selected_index();
948 var cell = this.get_cell(index);
1159 var cell = this.get_cell(index);
@@ -962,7 +1173,11 b' var IPython = (function (IPython) {'
962 };
1173 };
963 };
1174 };
964
1175
965
1176 /**
1177 * Combine the selected cell into the cell below it.
1178 *
1179 * @method merge_cell_below
1180 */
966 Notebook.prototype.merge_cell_below = function () {
1181 Notebook.prototype.merge_cell_below = function () {
967 var index = this.get_selected_index();
1182 var index = this.get_selected_index();
968 var cell = this.get_cell(index);
1183 var cell = this.get_cell(index);
@@ -985,33 +1200,57 b' var IPython = (function (IPython) {'
985
1200
986 // Cell collapsing and output clearing
1201 // Cell collapsing and output clearing
987
1202
1203 /**
1204 * Hide a cell's output.
1205 *
1206 * @method collapse
1207 * @param {Number} index A cell's numeric index
1208 */
988 Notebook.prototype.collapse = function (index) {
1209 Notebook.prototype.collapse = function (index) {
989 var i = this.index_or_selected(index);
1210 var i = this.index_or_selected(index);
990 this.get_cell(i).collapse();
1211 this.get_cell(i).collapse();
991 this.dirty = true;
1212 this.dirty = true;
992 };
1213 };
993
1214
994
1215 /**
1216 * Show a cell's output.
1217 *
1218 * @method expand
1219 * @param {Number} index A cell's numeric index
1220 */
995 Notebook.prototype.expand = function (index) {
1221 Notebook.prototype.expand = function (index) {
996 var i = this.index_or_selected(index);
1222 var i = this.index_or_selected(index);
997 this.get_cell(i).expand();
1223 this.get_cell(i).expand();
998 this.dirty = true;
1224 this.dirty = true;
999 };
1225 };
1000
1226
1001
1227 /** Toggle whether a cell's output is collapsed or expanded.
1228 *
1229 * @method toggle_output
1230 * @param {Number} index A cell's numeric index
1231 */
1002 Notebook.prototype.toggle_output = function (index) {
1232 Notebook.prototype.toggle_output = function (index) {
1003 var i = this.index_or_selected(index);
1233 var i = this.index_or_selected(index);
1004 this.get_cell(i).toggle_output();
1234 this.get_cell(i).toggle_output();
1005 this.dirty = true;
1235 this.dirty = true;
1006 };
1236 };
1007
1237
1008
1238 /**
1239 * Toggle a scrollbar for long cell outputs.
1240 *
1241 * @method toggle_output_scroll
1242 * @param {Number} index A cell's numeric index
1243 */
1009 Notebook.prototype.toggle_output_scroll = function (index) {
1244 Notebook.prototype.toggle_output_scroll = function (index) {
1010 var i = this.index_or_selected(index);
1245 var i = this.index_or_selected(index);
1011 this.get_cell(i).toggle_output_scroll();
1246 this.get_cell(i).toggle_output_scroll();
1012 };
1247 };
1013
1248
1014
1249 /**
1250 * Hide each code cell's output area.
1251 *
1252 * @method collapse_all_output
1253 */
1015 Notebook.prototype.collapse_all_output = function () {
1254 Notebook.prototype.collapse_all_output = function () {
1016 var ncells = this.ncells();
1255 var ncells = this.ncells();
1017 var cells = this.get_cells();
1256 var cells = this.get_cells();
@@ -1024,7 +1263,11 b' var IPython = (function (IPython) {'
1024 this.dirty = true;
1263 this.dirty = true;
1025 };
1264 };
1026
1265
1027
1266 /**
1267 * Expand each code cell's output area, and add a scrollbar for long output.
1268 *
1269 * @method scroll_all_output
1270 */
1028 Notebook.prototype.scroll_all_output = function () {
1271 Notebook.prototype.scroll_all_output = function () {
1029 var ncells = this.ncells();
1272 var ncells = this.ncells();
1030 var cells = this.get_cells();
1273 var cells = this.get_cells();
@@ -1038,7 +1281,11 b' var IPython = (function (IPython) {'
1038 this.dirty = true;
1281 this.dirty = true;
1039 };
1282 };
1040
1283
1041
1284 /**
1285 * Expand each code cell's output area, and remove scrollbars.
1286 *
1287 * @method expand_all_output
1288 */
1042 Notebook.prototype.expand_all_output = function () {
1289 Notebook.prototype.expand_all_output = function () {
1043 var ncells = this.ncells();
1290 var ncells = this.ncells();
1044 var cells = this.get_cells();
1291 var cells = this.get_cells();
@@ -1052,7 +1299,11 b' var IPython = (function (IPython) {'
1052 this.dirty = true;
1299 this.dirty = true;
1053 };
1300 };
1054
1301
1055
1302 /**
1303 * Clear each code cell's output area.
1304 *
1305 * @method clear_all_output
1306 */
1056 Notebook.prototype.clear_all_output = function () {
1307 Notebook.prototype.clear_all_output = function () {
1057 var ncells = this.ncells();
1308 var ncells = this.ncells();
1058 var cells = this.get_cells();
1309 var cells = this.get_cells();
@@ -1070,12 +1321,22 b' var IPython = (function (IPython) {'
1070
1321
1071 // Other cell functions: line numbers, ...
1322 // Other cell functions: line numbers, ...
1072
1323
1324 /**
1325 * Toggle line numbers in the selected cell's input area.
1326 *
1327 * @method cell_toggle_line_numbers
1328 */
1073 Notebook.prototype.cell_toggle_line_numbers = function() {
1329 Notebook.prototype.cell_toggle_line_numbers = function() {
1074 this.get_selected_cell().toggle_line_numbers();
1330 this.get_selected_cell().toggle_line_numbers();
1075 };
1331 };
1076
1332
1077 // Kernel related things
1333 // Kernel related things
1078
1334
1335 /**
1336 * Start a new kernel and set it on each code cell.
1337 *
1338 * @method start_kernel
1339 */
1079 Notebook.prototype.start_kernel = function () {
1340 Notebook.prototype.start_kernel = function () {
1080 var base_url = $('body').data('baseKernelUrl') + "kernels";
1341 var base_url = $('body').data('baseKernelUrl') + "kernels";
1081 this.kernel = new IPython.Kernel(base_url);
1342 this.kernel = new IPython.Kernel(base_url);
@@ -1090,7 +1351,11 b' var IPython = (function (IPython) {'
1090 };
1351 };
1091 };
1352 };
1092
1353
1093
1354 /**
1355 * Prompt the user to restart the IPython kernel.
1356 *
1357 * @method restart_kernel
1358 */
1094 Notebook.prototype.restart_kernel = function () {
1359 Notebook.prototype.restart_kernel = function () {
1095 var that = this;
1360 var that = this;
1096 var dialog = $('<div/>');
1361 var dialog = $('<div/>');
@@ -1113,7 +1378,14 b' var IPython = (function (IPython) {'
1113 });
1378 });
1114 };
1379 };
1115
1380
1116
1381 /**
1382 * Run the selected cell.
1383 *
1384 * This executes code cells, and skips all others.
1385 *
1386 * @method execute_selected_cell
1387 * @param {Object} options Customize post-execution behavior
1388 */
1117 Notebook.prototype.execute_selected_cell = function (options) {
1389 Notebook.prototype.execute_selected_cell = function (options) {
1118 // add_new: should a new cell be added if we are at the end of the nb
1390 // add_new: should a new cell be added if we are at the end of the nb
1119 // terminal: execute in terminal mode, which stays in the current cell
1391 // terminal: execute in terminal mode, which stays in the current cell
@@ -1141,21 +1413,42 b' var IPython = (function (IPython) {'
1141 this.dirty = true;
1413 this.dirty = true;
1142 };
1414 };
1143
1415
1144
1416 /**
1417 * Execute all cells below the selected cell.
1418 *
1419 * @method execute_cells_below
1420 */
1145 Notebook.prototype.execute_cells_below = function () {
1421 Notebook.prototype.execute_cells_below = function () {
1146 this.execute_cell_range(this.get_selected_index(), this.ncells());
1422 this.execute_cell_range(this.get_selected_index(), this.ncells());
1147 this.scroll_to_bottom();
1423 this.scroll_to_bottom();
1148 };
1424 };
1149
1425
1426 /**
1427 * Execute all cells above the selected cell.
1428 *
1429 * @method execute_cells_above
1430 */
1150 Notebook.prototype.execute_cells_above = function () {
1431 Notebook.prototype.execute_cells_above = function () {
1151 this.execute_cell_range(0, this.get_selected_index());
1432 this.execute_cell_range(0, this.get_selected_index());
1152 };
1433 };
1153
1434
1435 /**
1436 * Execute all cells.
1437 *
1438 * @method execute_all_cells
1439 */
1154 Notebook.prototype.execute_all_cells = function () {
1440 Notebook.prototype.execute_all_cells = function () {
1155 this.execute_cell_range(0, this.ncells());
1441 this.execute_cell_range(0, this.ncells());
1156 this.scroll_to_bottom();
1442 this.scroll_to_bottom();
1157 };
1443 };
1158
1444
1445 /**
1446 * Execute a contiguous range of cells.
1447 *
1448 * @method execute_cell_range
1449 * @param {Number} start Index of the first cell to execute (inclusive)
1450 * @param {Number} end Index of the last cell to execute (exclusive)
1451 */
1159 Notebook.prototype.execute_cell_range = function (start, end) {
1452 Notebook.prototype.execute_cell_range = function (start, end) {
1160 for (var i=start; i<end; i++) {
1453 for (var i=start; i<end; i++) {
1161 this.select(i);
1454 this.select(i);
@@ -1165,21 +1458,43 b' var IPython = (function (IPython) {'
1165
1458
1166 // Persistance and loading
1459 // Persistance and loading
1167
1460
1461 /**
1462 * Getter method for this notebook's ID.
1463 *
1464 * @method get_notebook_id
1465 * @return {String} This notebook's ID
1466 */
1168 Notebook.prototype.get_notebook_id = function () {
1467 Notebook.prototype.get_notebook_id = function () {
1169 return this.notebook_id;
1468 return this.notebook_id;
1170 };
1469 };
1171
1470
1172
1471 /**
1472 * Getter method for this notebook's name.
1473 *
1474 * @method get_notebook_name
1475 * @return {String} This notebook's name
1476 */
1173 Notebook.prototype.get_notebook_name = function () {
1477 Notebook.prototype.get_notebook_name = function () {
1174 return this.notebook_name;
1478 return this.notebook_name;
1175 };
1479 };
1176
1480
1177
1481 /**
1482 * Setter method for this notebook's name.
1483 *
1484 * @method set_notebook_name
1485 * @param {String} name A new name for this notebook
1486 */
1178 Notebook.prototype.set_notebook_name = function (name) {
1487 Notebook.prototype.set_notebook_name = function (name) {
1179 this.notebook_name = name;
1488 this.notebook_name = name;
1180 };
1489 };
1181
1490
1182
1491 /**
1492 * Check that a notebook's name is valid.
1493 *
1494 * @method test_notebook_name
1495 * @param {String} nbname A name for this notebook
1496 * @return {Boolean} True if the name is valid, false if invalid
1497 */
1183 Notebook.prototype.test_notebook_name = function (nbname) {
1498 Notebook.prototype.test_notebook_name = function (nbname) {
1184 nbname = nbname || '';
1499 nbname = nbname || '';
1185 if (this.notebook_name_blacklist_re.test(nbname) == false && nbname.length>0) {
1500 if (this.notebook_name_blacklist_re.test(nbname) == false && nbname.length>0) {
@@ -1189,7 +1504,14 b' var IPython = (function (IPython) {'
1189 };
1504 };
1190 };
1505 };
1191
1506
1192
1507 /**
1508 * Load a notebook from JSON (.ipynb).
1509 *
1510 * This currently handles one worksheet: others are deleted.
1511 *
1512 * @method fromJSON
1513 * @param {Object} data JSON representation of a notebook
1514 */
1193 Notebook.prototype.fromJSON = function (data) {
1515 Notebook.prototype.fromJSON = function (data) {
1194 var ncells = this.ncells();
1516 var ncells = this.ncells();
1195 var i;
1517 var i;
@@ -1245,7 +1567,12 b' var IPython = (function (IPython) {'
1245 }
1567 }
1246 };
1568 };
1247
1569
1248
1570 /**
1571 * Dump this notebook into a JSON-friendly object.
1572 *
1573 * @method toJSON
1574 * @return {Object} A JSON-friendly representation of this notebook.
1575 */
1249 Notebook.prototype.toJSON = function () {
1576 Notebook.prototype.toJSON = function () {
1250 var cells = this.get_cells();
1577 var cells = this.get_cells();
1251 var ncells = cells.length;
1578 var ncells = cells.length;
@@ -1264,6 +1591,11 b' var IPython = (function (IPython) {'
1264 return data;
1591 return data;
1265 };
1592 };
1266
1593
1594 /**
1595 * Save this notebook on the server.
1596 *
1597 * @method save_notebook
1598 */
1267 Notebook.prototype.save_notebook = function () {
1599 Notebook.prototype.save_notebook = function () {
1268 // We may want to move the name/id/nbformat logic inside toJSON?
1600 // We may want to move the name/id/nbformat logic inside toJSON?
1269 var data = this.toJSON();
1601 var data = this.toJSON();
@@ -1285,18 +1617,37 b' var IPython = (function (IPython) {'
1285 $.ajax(url, settings);
1617 $.ajax(url, settings);
1286 };
1618 };
1287
1619
1288
1620 /**
1621 * Success callback for saving a notebook.
1622 *
1623 * @method save_notebook_success
1624 * @param {Object} data JSON representation of a notebook
1625 * @param {String} status Description of response status
1626 * @param {jqXHR} xhr jQuery Ajax object
1627 */
1289 Notebook.prototype.save_notebook_success = function (data, status, xhr) {
1628 Notebook.prototype.save_notebook_success = function (data, status, xhr) {
1290 this.dirty = false;
1629 this.dirty = false;
1291 $([IPython.events]).trigger('notebook_saved.Notebook');
1630 $([IPython.events]).trigger('notebook_saved.Notebook');
1292 };
1631 };
1293
1632
1294
1633 /**
1634 * Failure callback for saving a notebook.
1635 *
1636 * @method save_notebook_error
1637 * @param {jqXHR} xhr jQuery Ajax object
1638 * @param {String} status Description of response status
1639 * @param {String} error_msg HTTP error message
1640 */
1295 Notebook.prototype.save_notebook_error = function (xhr, status, error_msg) {
1641 Notebook.prototype.save_notebook_error = function (xhr, status, error_msg) {
1296 $([IPython.events]).trigger('notebook_save_failed.Notebook');
1642 $([IPython.events]).trigger('notebook_save_failed.Notebook');
1297 };
1643 };
1298
1644
1299
1645 /**
1646 * Request a notebook's data from the server.
1647 *
1648 * @method load_notebook
1649 * @param {String} notebook_id A notebook to load
1650 */
1300 Notebook.prototype.load_notebook = function (notebook_id) {
1651 Notebook.prototype.load_notebook = function (notebook_id) {
1301 var that = this;
1652 var that = this;
1302 this.notebook_id = notebook_id;
1653 this.notebook_id = notebook_id;
@@ -1314,7 +1665,16 b' var IPython = (function (IPython) {'
1314 $.ajax(url, settings);
1665 $.ajax(url, settings);
1315 };
1666 };
1316
1667
1317
1668 /**
1669 * Success callback for loading a notebook from the server.
1670 *
1671 * Load notebook data from the JSON response.
1672 *
1673 * @method load_notebook_success
1674 * @param {Object} data JSON representation of a notebook
1675 * @param {String} status Description of response status
1676 * @param {jqXHR} xhr jQuery Ajax object
1677 */
1318 Notebook.prototype.load_notebook_success = function (data, status, xhr) {
1678 Notebook.prototype.load_notebook_success = function (data, status, xhr) {
1319 this.fromJSON(data);
1679 this.fromJSON(data);
1320 if (this.ncells() === 0) {
1680 if (this.ncells() === 0) {
@@ -1380,7 +1740,14 b' var IPython = (function (IPython) {'
1380 $([IPython.events]).trigger('notebook_loaded.Notebook');
1740 $([IPython.events]).trigger('notebook_loaded.Notebook');
1381 };
1741 };
1382
1742
1383
1743 /**
1744 * Failure callback for loading a notebook from the server.
1745 *
1746 * @method load_notebook_error
1747 * @param {jqXHR} xhr jQuery Ajax object
1748 * @param {String} textStatus Description of response status
1749 * @param {String} errorThrow HTTP error message
1750 */
1384 Notebook.prototype.load_notebook_error = function (xhr, textStatus, errorThrow) {
1751 Notebook.prototype.load_notebook_error = function (xhr, textStatus, errorThrow) {
1385 if (xhr.status === 500) {
1752 if (xhr.status === 500) {
1386 var msg = "An error occurred while loading this notebook. Most likely " +
1753 var msg = "An error occurred while loading this notebook. Most likely " +
General Comments 0
You need to be logged in to leave comments. Login now