22define ( [
33 'base/js/namespace' ,
44 'base/js/events' ,
5+ 'services/config' ,
56 'notebook/js/codecell' ,
67 'codemirror/lib/codemirror' ,
78 'codemirror/addon/display/rulers'
8- ] , function ( Jupyter , events , codecell , codemirror ) {
9+ ] , function ( Jupyter , events , configmod , codecell , codemirror ) {
910 "use strict" ;
1011
1112 var log_prefix = '[ruler]' ;
1213
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+
1722
1823 var rulers = [ ] ;
1924
20- var isNumber = function ( n ) {
25+ var isNumber = function ( n ) {
2126 return ! isNaN ( parseFloat ( n ) ) && isFinite ( n ) ;
2227 } ;
2328
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+ }
2645
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 ;
2948 }
3049
3150 if ( config . data . hasOwnProperty ( 'ruler_column' ) ) {
@@ -35,48 +54,26 @@ define([
3554 new_columns . push ( config . data . ruler_column [ i ] ) ;
3655 }
3756 }
38- if ( new_columns . length > 0 ) {
39- ruler_column = new_columns ;
57+ if ( new_columns . length > 0 ) {
58+ params . ruler_column = new_columns ;
4059 }
4160 }
4261
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 ;
4564 }
4665
47- for ( i in ruler_column ) {
66+ for ( i in params . ruler_column ) {
4867 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 ]
5271 } ) ;
5372 }
5473 console . debug ( log_prefix , 'ruler specs:' , rulers ) ;
5574
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 ;
8077
8178 // Change default for new cells
8279 codecell . CodeCell . options_default . cm_config . rulers = rulers ;
@@ -86,14 +83,42 @@ define([
8683 cell . code_mirror . setOption ( 'rulers' , rulers ) ;
8784 }
8885 } ) ;
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+ } ) ;
93118 } ;
94119
95120 var extension = {
96- load_ipython_extension : load_ipython_extension
121+ load_ipython_extension : load_extension
97122 } ;
98123 return extension ;
99124} ) ;
0 commit comments