File size: 3,815 Bytes
71c6277
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Add rulers to codecells
define([
    'base/js/namespace',
    'base/js/events',
    'services/config',
    'notebook/js/codecell',
    'codemirror/lib/codemirror',
    'codemirror/addon/display/rulers'
], function (Jupyter, events, configmod, codecell, codemirror) {
    "use strict";

    var log_prefix = '[ruler]';

    // define default config parameter values
    var params = {
        ruler_column: [78],
        ruler_color: ["#ff0000"],
        ruler_linestyle: ["dashed"],
        ruler_do_css_patch: false
    };


    var rulers = [];

    var isNumber = function (n) {
        return !isNaN(parseFloat(n)) && isFinite(n);
    };

    // updates default params with any specified in the provided config data
    var update_params = function (config_data) {
        for (var key in params) {
            if (config_data.hasOwnProperty(key)) {
                params[key] = config_data[key];
            }
        }
    };

    var on_config_loaded = function () {

        if (Jupyter.notebook !== undefined) {
            var i, config = Jupyter.notebook.config;
        } else {
            var i, config = Jupyter.editor.config;
        }

        if (config.data.hasOwnProperty('ruler_color') && config.data.ruler_color.length > 0) {
            params.ruler_color = config.data.ruler_color;
        }

        if (config.data.hasOwnProperty('ruler_column')) {
            var new_columns = [];
            for (i in config.data.ruler_column) {
                if (isNumber(config.data.ruler_column[i])) {
                    new_columns.push(config.data.ruler_column[i]);
                }
            }
            if (new_columns.length > 0) {
                params.ruler_column = new_columns;
            }
        }

        if (config.data.hasOwnProperty('ruler_linestyle') && config.data.ruler_linestyle.length > 0) {
            params.ruler_linestyle = config.data.ruler_linestyle;
        }

        for (i in params.ruler_column) {
            rulers.push({
                color: params.ruler_color[i % params.ruler_color.length],
                column: params.ruler_column[i],
                lineStyle: params.ruler_linestyle[i % params.ruler_linestyle.length]
            });
        }
        console.debug(log_prefix, 'ruler specs:', rulers);

        if (Jupyter.notebook !== undefined) {
            var i, config = Jupyter.notebook.config;

            // Change default for new cells
            codecell.CodeCell.options_default.cm_config.rulers = rulers;
            // Apply to any already-existing cells
            var cells = Jupyter.notebook.get_cells().forEach(function (cell) {
                if (cell instanceof codecell.CodeCell) {
                    cell.code_mirror.setOption('rulers', rulers);
                }
            });

        }
        else {
            Jupyter.editor.codemirror.setOption('rulers', rulers);
        }
    };

    var load_extension = function () {

        // first, check which view we're in, in order to decide whether to load
        var conf_sect;
        if (Jupyter.notebook) {
            // we're in notebook view
            conf_sect = Jupyter.notebook.config;
        }
        else if (Jupyter.editor) {
            // we're in file-editor view
            conf_sect = Jupyter.editor.config;
        }
        else {
            // we're some other view like dashboard, terminal, etc, so bail now
            return;
        }

        conf_sect.loaded
            .then(function () {
                update_params(conf_sect.data);
            })
            .then(on_config_loaded)
            .catch(function on_error(reason) {
                console.warn(log_prefix, 'error:', reason);
            });
    };

    var extension = {
        load_ipython_extension: load_extension
    };
    return extension;
});