##// END OF EJS Templates
Merge pull request #3076 from dwyde/improve-notebook-js-yuidoc...
Matthias Bussonnier -
r10042:699a45a9 merge
parent child Browse files
Show More
@@ -29,7 +29,7 b' First, cd into js directory :'
29 29 ```bash
30 30 cd IPython/frontend/html/notebook/static/js/
31 31 # install yuidoc
32 npm install yuidoc
32 npm install yuidocjs
33 33 ```
34 34
35 35
@@ -14,6 +14,14 b' var IPython = (function (IPython) {'
14 14 var utils = IPython.utils;
15 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 25 var Notebook = function (selector, options) {
18 26 var options = options || {};
19 27 this._baseProjectUrl = options.baseProjectUrl;
@@ -44,16 +52,30 b' var IPython = (function (IPython) {'
44 52 this.bind_events();
45 53 };
46 54
47
55 /**
56 * Tweak the notebook's CSS style.
57 *
58 * @method style
59 */
48 60 Notebook.prototype.style = function () {
49 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 70 Notebook.prototype.baseProjectUrl = function(){
53 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 79 Notebook.prototype.create_elements = function () {
58 80 // We add this end_space div to the end of the notebook div to:
59 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 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 100 Notebook.prototype.bind_events = function () {
75 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 356 Notebook.prototype.scroll_to_cell = function (cell_number, time) {
323 357 var cells = this.get_cells();
324 358 var time = time || 0;
@@ -329,12 +363,20 b' var IPython = (function (IPython) {'
329 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 371 Notebook.prototype.scroll_to_bottom = function () {
334 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 380 Notebook.prototype.scroll_to_top = function () {
339 381 this.element.animate({scrollTop:0}, 0);
340 382 };
@@ -342,11 +384,23 b' var IPython = (function (IPython) {'
342 384
343 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 393 Notebook.prototype.get_cell_elements = function () {
346 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 404 Notebook.prototype.get_cell_element = function (index) {
351 405 var result = null;
352 406 var e = this.get_cell_elements().eq(index);
@@ -356,12 +410,22 b' var IPython = (function (IPython) {'
356 410 return result;
357 411 };
358 412
359
360 Notebook.prototype.ncells = function (cell) {
413 /**
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 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 429 // TODO: we are often calling cells as cells()[i], which we should optimize
366 430 // to cells(i) or a new method.
367 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 444 Notebook.prototype.get_cell = function (index) {
375 445 var result = null;
376 446 var ce = this.get_cell_element(index);
@@ -380,7 +450,13 b' var IPython = (function (IPython) {'
380 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 460 Notebook.prototype.get_next_cell = function (cell) {
385 461 var result = null;
386 462 var index = this.find_cell_index(cell);
@@ -390,8 +466,16 b' var IPython = (function (IPython) {'
390 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 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 479 var result = null;
396 480 var index = this.find_cell_index(cell);
397 481 if (index !== null && index > 1) {
@@ -399,7 +483,14 b' var IPython = (function (IPython) {'
399 483 }
400 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 494 Notebook.prototype.find_cell_index = function (cell) {
404 495 var result = null;
405 496 this.get_cell_elements().filter(function (index) {
@@ -410,7 +501,13 b' var IPython = (function (IPython) {'
410 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 511 Notebook.prototype.index_or_selected = function (index) {
415 512 var i;
416 513 if (index === undefined || index === null) {
@@ -424,13 +521,23 b' var IPython = (function (IPython) {'
424 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 529 Notebook.prototype.get_selected_cell = function () {
429 530 var index = this.get_selected_index();
430 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 541 Notebook.prototype.is_valid_cell_index = function (index) {
435 542 if (index !== null && index >= 0 && index < this.ncells()) {
436 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 555 Notebook.prototype.get_selected_index = function () {
443 556 var result = null;
444 557 this.get_cell_elements().filter(function (index) {
@@ -452,6 +565,13 b' var IPython = (function (IPython) {'
452 565
453 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 575 Notebook.prototype.select = function (index) {
456 576 if (this.is_valid_cell_index(index)) {
457 577 var sindex = this.get_selected_index()
@@ -473,14 +593,24 b' var IPython = (function (IPython) {'
473 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 602 Notebook.prototype.select_next = function () {
478 603 var index = this.get_selected_index();
479 604 this.select(index+1);
480 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 614 Notebook.prototype.select_prev = function () {
485 615 var index = this.get_selected_index();
486 616 this.select(index-1);
@@ -491,9 +621,11 b' var IPython = (function (IPython) {'
491 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 626 * @method move_cell_up
496 627 * @param [index] {integer} cell index
628 * @return {Notebook} This notebook
497 629 **/
498 630 Notebook.prototype.move_cell_up = function (index) {
499 631 var i = this.index_or_selected(index);
@@ -513,8 +645,10 b' var IPython = (function (IPython) {'
513 645
514 646 /**
515 647 * Move given (or selected) cell down and select it
648 *
516 649 * @method move_cell_down
517 650 * @param [index] {integer} cell index
651 * @return {Notebook} This notebook
518 652 **/
519 653 Notebook.prototype.move_cell_down = function (index) {
520 654 var i = this.index_or_selected(index);
@@ -534,6 +668,13 b' var IPython = (function (IPython) {'
534 668
535 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 678 Notebook.prototype.delete_cell = function (index) {
538 679 var i = this.index_or_selected(index);
539 680 var cell = this.get_selected_cell();
@@ -556,9 +697,6 b' var IPython = (function (IPython) {'
556 697 return this;
557 698 };
558 699
559
560
561
562 700 /**
563 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 705 * Index will be brought back into the accissible range [0,n]
568 706 *
707 * @method insert_cell_at_index
569 708 * @param type {string} in ['code','html','markdown','heading']
570 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 745 * Insert an element at given cell index.
607 746 *
747 * @method _insert_element_at_index
608 748 * @param element {dom element} a cell element
609 749 * @param [index] {int} a valid index where to inser cell
610 750 * @private
@@ -644,6 +784,7 b' var IPython = (function (IPython) {'
644 784 *
645 785 * default index value is the one of currently selected cell
646 786 *
787 * @method insert_cell_above
647 788 * @param type {string} cell type
648 789 * @param [index] {integer}
649 790 *
@@ -677,7 +818,7 b' var IPython = (function (IPython) {'
677 818 * Insert cell at end of notebook
678 819 *
679 820 * @method insert_cell_at_bottom
680 * @param type {String} cell type
821 * @param {String} type cell type
681 822 *
682 823 * @return the added cell; or null
683 824 **/
@@ -686,8 +827,12 b' var IPython = (function (IPython) {'
686 827 return this.insert_cell_below(type,len-1);
687 828 };
688 829
689
690
830 /**
831 * Turn a cell into a code cell.
832 *
833 * @method to_code
834 * @param {Number} [index] A cell's index
835 */
691 836 Notebook.prototype.to_code = function (index) {
692 837 var i = this.index_or_selected(index);
693 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 863 Notebook.prototype.to_markdown = function (index) {
714 864 var i = this.index_or_selected(index);
715 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 892 Notebook.prototype.to_html = function (index) {
893 // TODO: remove? This is never called
738 894 var i = this.index_or_selected(index);
739 895 if (this.is_valid_cell_index(i)) {
740 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 923 Notebook.prototype.to_raw = function (index) {
763 924 var i = this.index_or_selected(index);
764 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 954 Notebook.prototype.to_heading = function (index, level) {
788 955 level = level || 1;
789 956 var i = this.index_or_selected(index);
@@ -818,6 +985,11 b' var IPython = (function (IPython) {'
818 985
819 986 // Cut/Copy/Paste
820 987
988 /**
989 * Enable UI elements for pasting cells.
990 *
991 * @method enable_paste
992 */
821 993 Notebook.prototype.enable_paste = function () {
822 994 var that = this;
823 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 1011 Notebook.prototype.disable_paste = function () {
836 1012 if (this.paste_enabled) {
837 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 1025 Notebook.prototype.cut_cell = function () {
846 1026 this.copy_cell();
847 1027 this.delete_cell();
848 1028 }
849 1029
1030 /**
1031 * Copy a cell.
1032 *
1033 * @method copy_cell
1034 */
850 1035 Notebook.prototype.copy_cell = function () {
851 1036 var cell = this.get_selected_cell();
852 1037 this.clipboard = cell.toJSON();
853 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 1046 Notebook.prototype.paste_cell_replace = function () {
858 1047 if (this.clipboard !== null && this.paste_enabled) {
859 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 1062 Notebook.prototype.paste_cell_above = function () {
870 1063 if (this.clipboard !== null && this.paste_enabled) {
871 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 1075 Notebook.prototype.paste_cell_below = function () {
879 1076 if (this.clipboard !== null && this.paste_enabled) {
880 1077 var cell_data = this.clipboard;
@@ -885,6 +1082,11 b' var IPython = (function (IPython) {'
885 1082
886 1083 // Cell undelete
887 1084
1085 /**
1086 * Restore the most recently deleted cell.
1087 *
1088 * @method undelete
1089 */
888 1090 Notebook.prototype.undelete = function() {
889 1091 if (this.undelete_backup !== null && this.undelete_index !== null) {
890 1092 var current_index = this.get_selected_index();
@@ -914,6 +1116,11 b' var IPython = (function (IPython) {'
914 1116
915 1117 // Split/merge
916 1118
1119 /**
1120 * Split the selected cell into two, at the cursor.
1121 *
1122 * @method split_cell
1123 */
917 1124 Notebook.prototype.split_cell = function () {
918 1125 // Todo: implement spliting for other cell types.
919 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 1157 Notebook.prototype.merge_cell_above = function () {
947 1158 var index = this.get_selected_index();
948 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 1181 Notebook.prototype.merge_cell_below = function () {
967 1182 var index = this.get_selected_index();
968 1183 var cell = this.get_cell(index);
@@ -985,33 +1200,57 b' var IPython = (function (IPython) {'
985 1200
986 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 1209 Notebook.prototype.collapse = function (index) {
989 1210 var i = this.index_or_selected(index);
990 1211 this.get_cell(i).collapse();
991 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 1221 Notebook.prototype.expand = function (index) {
996 1222 var i = this.index_or_selected(index);
997 1223 this.get_cell(i).expand();
998 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 1232 Notebook.prototype.toggle_output = function (index) {
1003 1233 var i = this.index_or_selected(index);
1004 1234 this.get_cell(i).toggle_output();
1005 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 1244 Notebook.prototype.toggle_output_scroll = function (index) {
1010 1245 var i = this.index_or_selected(index);
1011 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 1254 Notebook.prototype.collapse_all_output = function () {
1016 1255 var ncells = this.ncells();
1017 1256 var cells = this.get_cells();
@@ -1024,7 +1263,11 b' var IPython = (function (IPython) {'
1024 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 1271 Notebook.prototype.scroll_all_output = function () {
1029 1272 var ncells = this.ncells();
1030 1273 var cells = this.get_cells();
@@ -1038,7 +1281,11 b' var IPython = (function (IPython) {'
1038 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 1289 Notebook.prototype.expand_all_output = function () {
1043 1290 var ncells = this.ncells();
1044 1291 var cells = this.get_cells();
@@ -1052,7 +1299,11 b' var IPython = (function (IPython) {'
1052 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 1307 Notebook.prototype.clear_all_output = function () {
1057 1308 var ncells = this.ncells();
1058 1309 var cells = this.get_cells();
@@ -1070,12 +1321,22 b' var IPython = (function (IPython) {'
1070 1321
1071 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 1329 Notebook.prototype.cell_toggle_line_numbers = function() {
1074 1330 this.get_selected_cell().toggle_line_numbers();
1075 1331 };
1076 1332
1077 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 1340 Notebook.prototype.start_kernel = function () {
1080 1341 var base_url = $('body').data('baseKernelUrl') + "kernels";
1081 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 1359 Notebook.prototype.restart_kernel = function () {
1095 1360 var that = this;
1096 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 1389 Notebook.prototype.execute_selected_cell = function (options) {
1118 1390 // add_new: should a new cell be added if we are at the end of the nb
1119 1391 // terminal: execute in terminal mode, which stays in the current cell
@@ -1141,21 +1413,42 b' var IPython = (function (IPython) {'
1141 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 1421 Notebook.prototype.execute_cells_below = function () {
1146 1422 this.execute_cell_range(this.get_selected_index(), this.ncells());
1147 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 1431 Notebook.prototype.execute_cells_above = function () {
1151 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 1440 Notebook.prototype.execute_all_cells = function () {
1155 1441 this.execute_cell_range(0, this.ncells());
1156 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 1452 Notebook.prototype.execute_cell_range = function (start, end) {
1160 1453 for (var i=start; i<end; i++) {
1161 1454 this.select(i);
@@ -1165,21 +1458,43 b' var IPython = (function (IPython) {'
1165 1458
1166 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 1467 Notebook.prototype.get_notebook_id = function () {
1169 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 1477 Notebook.prototype.get_notebook_name = function () {
1174 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 1487 Notebook.prototype.set_notebook_name = function (name) {
1179 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 1498 Notebook.prototype.test_notebook_name = function (nbname) {
1184 1499 nbname = nbname || '';
1185 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 1515 Notebook.prototype.fromJSON = function (data) {
1194 1516 var ncells = this.ncells();
1195 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 1576 Notebook.prototype.toJSON = function () {
1250 1577 var cells = this.get_cells();
1251 1578 var ncells = cells.length;
@@ -1264,6 +1591,11 b' var IPython = (function (IPython) {'
1264 1591 return data;
1265 1592 };
1266 1593
1594 /**
1595 * Save this notebook on the server.
1596 *
1597 * @method save_notebook
1598 */
1267 1599 Notebook.prototype.save_notebook = function () {
1268 1600 // We may want to move the name/id/nbformat logic inside toJSON?
1269 1601 var data = this.toJSON();
@@ -1285,18 +1617,37 b' var IPython = (function (IPython) {'
1285 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 1628 Notebook.prototype.save_notebook_success = function (data, status, xhr) {
1290 1629 this.dirty = false;
1291 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 1641 Notebook.prototype.save_notebook_error = function (xhr, status, error_msg) {
1296 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 1651 Notebook.prototype.load_notebook = function (notebook_id) {
1301 1652 var that = this;
1302 1653 this.notebook_id = notebook_id;
@@ -1314,7 +1665,16 b' var IPython = (function (IPython) {'
1314 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 1678 Notebook.prototype.load_notebook_success = function (data, status, xhr) {
1319 1679 this.fromJSON(data);
1320 1680 if (this.ncells() === 0) {
@@ -1380,7 +1740,14 b' var IPython = (function (IPython) {'
1380 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 1751 Notebook.prototype.load_notebook_error = function (xhr, textStatus, errorThrow) {
1385 1752 if (xhr.status === 500) {
1386 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