1
- define ( [ 'jquery' , 'require' ] , function ( $ , require ) {
1
+ ( require . specified ( 'base/js/namespace' ) ? define : function ( deps , callback ) {
2
+ // if here, the Jupyter namespace hasn't been specified to be loaded.
3
+ // This means that we're probably embedded in a page, so we need to make
4
+ // our definition with a specific module name
5
+ return define ( 'nbextensions/collapsible_headings/main' , deps , callback ) ;
6
+ } ) ( [ 'jquery' , 'require' ] , function ( $ , require ) {
2
7
"use strict" ;
3
8
4
9
var mod_name = 'collapsible_headings' ;
@@ -15,6 +20,7 @@ define(['jquery', 'require'], function ($, require) {
15
20
// define default values for config parameters
16
21
var params = {
17
22
add_button : false ,
23
+ add_all_cells_button : false ,
18
24
add_insert_header_buttons : false ,
19
25
use_toggle_controls : true ,
20
26
make_toggle_controls_buttons : false ,
@@ -25,7 +31,9 @@ define(['jquery', 'require'], function ($, require) {
25
31
use_shortcuts : true ,
26
32
shortcuts : {
27
33
collapse : 'left' ,
34
+ collapse_all : 'ctrl-shift-left' ,
28
35
uncollapse : 'right' ,
36
+ uncollapse_all : 'ctrl-shift-right' ,
29
37
select : 'shift-right' ,
30
38
insert_above : 'shift-a' ,
31
39
insert_below : 'shift-b' ,
@@ -171,6 +179,11 @@ define(['jquery', 'require'], function ($, require) {
171
179
// Restrict the search to cells that are of the same level and lower
172
180
// than the currently selected cell by index.
173
181
var ref_cell = _get_cell_at_index ( index ) ;
182
+ // ref_cell may be null, if we've attempted to extend selection beyond
183
+ // the existing cells
184
+ if ( ! ref_cell ) {
185
+ return ;
186
+ }
174
187
var pivot_level = get_cell_level ( ref_cell ) ;
175
188
var cells = _get_cells ( ) ;
176
189
while ( index > 0 && pivot_level > 1 ) {
@@ -629,7 +642,7 @@ define(['jquery', 'require'], function ($, require) {
629
642
var filter_func ;
630
643
if ( is_h ) {
631
644
var lvl = get_cell_level ( cell ) ;
632
- filter_func = function ( c ) { return get_cell_level ( c ) < lvl ; }
645
+ filter_func = function ( c ) { return get_cell_level ( c ) < lvl ; } ;
633
646
}
634
647
cell = find_header_cell ( cell , filter_func ) ;
635
648
if ( cell !== undefined ) {
@@ -644,6 +657,27 @@ define(['jquery', 'require'], function ($, require) {
644
657
'collapse_heading' , mod_name
645
658
) ;
646
659
660
+ action_names . collapse_all = Jupyter . keyboard_manager . actions . register ( {
661
+ handler : function ( env ) {
662
+ env . notebook . get_cells ( ) . forEach ( function ( c , idx , arr ) {
663
+ toggle_heading ( c , true ) ;
664
+ } ) ;
665
+ var cell = env . notebook . get_selected_cell ( ) ;
666
+ if ( cell . element . is ( ':hidden' ) ) {
667
+ cell = find_header_cell ( cell , function ( c ) { return c . element . is ( ':visible' ) ; } ) ;
668
+ if ( cell !== undefined ) {
669
+ Jupyter . notebook . select ( Jupyter . notebook . find_cell_index ( cell ) ) ;
670
+ cell . focus_cell ( ) ;
671
+ }
672
+ }
673
+ } ,
674
+ help : "Collapse all heading cells' sections" ,
675
+ icon : params . toggle_closed_icon ,
676
+ help_index : 'c2'
677
+ } ,
678
+ 'collapse_all_headings' , mod_name
679
+ ) ;
680
+
647
681
action_names . uncollapse = Jupyter . keyboard_manager . actions . register ( {
648
682
handler : function ( env ) {
649
683
var cell = env . notebook . get_selected_cell ( ) ;
@@ -664,11 +698,25 @@ define(['jquery', 'require'], function ($, require) {
664
698
} ,
665
699
help : "Un-collapse (expand) the selected heading cell's section" ,
666
700
icon : params . toggle_open_icon ,
667
- help_index : 'c2 '
701
+ help_index : 'c3 '
668
702
} ,
669
703
'uncollapse_heading' , mod_name
670
704
) ;
671
705
706
+ action_names . uncollapse_all = Jupyter . keyboard_manager . actions . register ( {
707
+ handler : function ( env ) {
708
+ env . notebook . get_cells ( ) . forEach ( function ( c , idx , arr ) {
709
+ toggle_heading ( c , false ) ;
710
+ } ) ;
711
+ env . notebook . get_selected_cell ( ) . focus_cell ( ) ;
712
+ } ,
713
+ help : "Un-collapse (expand) all heading cells' sections" ,
714
+ icon : params . toggle_open_icon ,
715
+ help_index : 'c4'
716
+ } ,
717
+ 'uncollapse_all_headings' , mod_name
718
+ ) ;
719
+
672
720
action_names . select = Jupyter . keyboard_manager . actions . register ( {
673
721
handler : function ( env ) {
674
722
var cell = env . notebook . get_selected_cell ( ) ;
@@ -807,6 +855,25 @@ define(['jquery', 'require'], function ($, require) {
807
855
}
808
856
} ] ) ;
809
857
}
858
+ if ( params . add_all_cells_button ) {
859
+ Jupyter . toolbar . add_buttons_group ( [ {
860
+ label : 'toggle all headings' ,
861
+ icon : 'fa-angle-double-up' ,
862
+ callback : function ( ) {
863
+ /**
864
+ * Collapse/uncollapse all heading cells based on status of first
865
+ */
866
+ var cells = Jupyter . notebook . get_cells ( ) ;
867
+ for ( var ii = 0 ; ii < cells . length ; ii ++ ) {
868
+ if ( is_heading ( cells [ ii ] ) ) {
869
+ Jupyter . keyboard_manager . actions . call ( action_names [
870
+ is_collapsed_heading ( cells [ ii ] ) ? 'uncollapse_all' : 'collapse_all' ] ) ;
871
+ return ;
872
+ }
873
+ }
874
+ }
875
+ } ] ) ;
876
+ }
810
877
if ( params . add_insert_header_buttons ) {
811
878
Jupyter . toolbar . add_buttons_group ( [
812
879
action_names . insert_above , action_names . insert_below
@@ -968,7 +1035,7 @@ define(['jquery', 'require'], function ($, require) {
968
1035
add_buttons_and_shortcuts ( ) ;
969
1036
} )
970
1037
. catch ( function on_reject ( reason ) {
971
- console . error ( log_prefix , 'error:' , reason )
1038
+ console . error ( log_prefix , 'error:' , reason ) ;
972
1039
} ) ;
973
1040
}
974
1041
0 commit comments