2
2
define ( [
3
3
'base/js/namespace' ,
4
4
'base/js/events' ,
5
+ 'services/config' ,
5
6
'notebook/js/codecell' ,
6
7
'codemirror/lib/codemirror' ,
7
8
'codemirror/addon/display/rulers'
8
- ] , function ( Jupyter , events , codecell , codemirror ) {
9
+ ] , function ( Jupyter , events , configmod , codecell , codemirror ) {
9
10
"use strict" ;
10
11
11
12
var log_prefix = '[ruler]' ;
12
13
13
- var ruler_column = [ 78 ] ;
14
- var ruler_color = [ "#ff0000" ] ;
15
- var ruler_linestyle = [ "dashed" ] ;
16
- var ruler_do_css_patch ;
14
+ // define default config parameter values
15
+ var params = {
16
+ ruler_column : [ 78 ] ,
17
+ ruler_color : [ "#ff0000" ] ,
18
+ ruler_linestyle : [ "dashed" ] ,
19
+ ruler_do_css_patch : false
20
+ } ;
21
+
17
22
18
23
var rulers = [ ] ;
19
24
20
- var isNumber = function ( n ) {
25
+ var isNumber = function ( n ) {
21
26
return ! isNaN ( parseFloat ( n ) ) && isFinite ( n ) ;
22
27
} ;
23
28
24
- function update_options ( ) {
25
- var i , config = Jupyter . notebook . config ;
29
+ // updates default params with any specified in the provided config data
30
+ var update_params = function ( config_data ) {
31
+ for ( var key in params ) {
32
+ if ( config_data . hasOwnProperty ( key ) ) {
33
+ params [ key ] = config_data [ key ] ;
34
+ }
35
+ }
36
+ } ;
37
+
38
+ var on_config_loaded = function ( ) {
39
+
40
+ if ( Jupyter . notebook !== undefined ) {
41
+ var i , config = Jupyter . notebook . config ;
42
+ } else {
43
+ var i , config = Jupyter . editor . config ;
44
+ }
26
45
27
- if ( config . data . hasOwnProperty ( 'ruler_color' ) && config . data . ruler_color . length > 0 ) {
28
- ruler_color = config . data . ruler_color ;
46
+ if ( config . data . hasOwnProperty ( 'ruler_color' ) && config . data . ruler_color . length > 0 ) {
47
+ params . ruler_color = config . data . ruler_color ;
29
48
}
30
49
31
50
if ( config . data . hasOwnProperty ( 'ruler_column' ) ) {
@@ -35,48 +54,26 @@ define([
35
54
new_columns . push ( config . data . ruler_column [ i ] ) ;
36
55
}
37
56
}
38
- if ( new_columns . length > 0 ) {
39
- ruler_column = new_columns ;
57
+ if ( new_columns . length > 0 ) {
58
+ params . ruler_column = new_columns ;
40
59
}
41
60
}
42
61
43
- if ( config . data . hasOwnProperty ( 'ruler_linestyle' ) && config . data . ruler_linestyle . length > 0 ) {
44
- ruler_linestyle = config . data . ruler_linestyle ;
62
+ if ( config . data . hasOwnProperty ( 'ruler_linestyle' ) && config . data . ruler_linestyle . length > 0 ) {
63
+ params . ruler_linestyle = config . data . ruler_linestyle ;
45
64
}
46
65
47
- for ( i in ruler_column ) {
66
+ for ( i in params . ruler_column ) {
48
67
rulers . push ( {
49
- color : ruler_color [ i % ruler_color . length ] ,
50
- column : ruler_column [ i ] ,
51
- lineStyle : ruler_linestyle [ i % ruler_linestyle . length ]
68
+ color : params . ruler_color [ i % params . ruler_color . length ] ,
69
+ column : params . ruler_column [ i ] ,
70
+ lineStyle : params . ruler_linestyle [ i % params . ruler_linestyle . length ]
52
71
} ) ;
53
72
}
54
73
console . debug ( log_prefix , 'ruler specs:' , rulers ) ;
55
74
56
- ruler_do_css_patch = config . data . ruler_do_css_patch ; // undefined is ok
57
- }
58
-
59
- var load_ipython_extension = function ( ) {
60
- Jupyter . notebook . config . loaded
61
- . then ( update_options , function on_error ( reason ) {
62
- console . warn ( log_prefix , 'error loading config:' , reason ) ;
63
- } )
64
- . then ( function ( ) {
65
- // Add css patch fix for notebook > 4.2.3 - see
66
- // https://github.com/jupyter/notebook/issues/2869
67
- // for details
68
- if ( ruler_do_css_patch !== false ) {
69
- var sheet = document . createElement ( 'style' ) ;
70
- sheet . innerHTML = [
71
- '.CodeMirror-lines {' ,
72
- ' padding: 0.4em 0; /* Vertical padding around content */' ,
73
- '}' ,
74
- '.CodeMirror pre {' ,
75
- ' padding: 0 0.4em; /* Horizonal padding around content */' ,
76
- '}' ,
77
- ] . join ( '\n' ) ;
78
- document . head . appendChild ( sheet ) ;
79
- }
75
+ if ( Jupyter . notebook !== undefined ) {
76
+ var i , config = Jupyter . notebook . config ;
80
77
81
78
// Change default for new cells
82
79
codecell . CodeCell . options_default . cm_config . rulers = rulers ;
@@ -86,14 +83,42 @@ define([
86
83
cell . code_mirror . setOption ( 'rulers' , rulers ) ;
87
84
}
88
85
} ) ;
89
- } )
90
- . catch ( function on_error ( reason ) {
91
- console . warn ( log_prefix , 'error:' , reason ) ;
92
- } ) ;
86
+
87
+ }
88
+ else {
89
+ Jupyter . editor . codemirror . setOption ( 'rulers' , rulers ) ;
90
+ }
91
+ } ;
92
+
93
+ var load_extension = function ( ) {
94
+
95
+ // first, check which view we're in, in order to decide whether to load
96
+ var conf_sect ;
97
+ if ( Jupyter . notebook ) {
98
+ // we're in notebook view
99
+ conf_sect = Jupyter . notebook . config ;
100
+ }
101
+ else if ( Jupyter . editor ) {
102
+ // we're in file-editor view
103
+ conf_sect = Jupyter . editor . config ;
104
+ }
105
+ else {
106
+ // we're some other view like dashboard, terminal, etc, so bail now
107
+ return ;
108
+ }
109
+
110
+ conf_sect . loaded
111
+ . then ( function ( ) {
112
+ update_params ( conf_sect . data ) ;
113
+ } )
114
+ . then ( on_config_loaded )
115
+ . catch ( function on_error ( reason ) {
116
+ console . warn ( log_prefix , 'error:' , reason ) ;
117
+ } ) ;
93
118
} ;
94
119
95
120
var extension = {
96
- load_ipython_extension : load_ipython_extension
121
+ load_ipython_extension : load_extension
97
122
} ;
98
123
return extension ;
99
124
} ) ;
0 commit comments