File size: 4,830 Bytes
afafe68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright (c) Jupyter-Contrib Team.
// Distributed under the terms of the Modified BSD License.
// Author: Jean-François Bercher 

define([
    'module',
    'require',
    'jquery',
    'base/js/namespace',
    './nbTranslate',
    './mutils',
], function(
    module,
    requirejs,
    $,
    Jupyter,
    nbt,
    mutils
) {
    'use strict';

    var sourceLang;
    var targetLang;
    var displayLangs;
    var useGoogleTranslate;

    var add_edit_shortcuts = {};
    var log_prefix = '[' + module.id + '] ';

    // default config (updated on nbextension load)
    var conf = {
        hotkey: 'alt-t',
        sourceLang: 'en',
        targetLang: 'fr',
        displayLangs: ['*'],
        langInMainMenu: true,
        useGoogleTranslate: true
    }


    function initialize(conf) {
        Jupyter.notebook.config.loaded.then(function config_loaded_callback() {            
            // config may be specified at system level or at document level.
      // first, update defaults with config loaded from server
      conf =  $.extend(false, {}, conf, Jupyter.notebook.config.data.nbTranslate);
      // then update cfg with any found in current notebook metadata
      // and save in nb metadata (then can be modified per document)
      conf = Jupyter.notebook.metadata.nbTranslate = $.extend(false, {}, conf,
          Jupyter.notebook.metadata.nbTranslate);
      //conf.displayLangs = Jupyter.notebook.metadata.nbTranslate.displayLangs = $.makeArray($.extend(true, {}, conf.displayLangs, Jupyter.notebook.metadata.nbTranslate.displayLangs));
            // other initializations
            sourceLang = conf.sourceLang;
            targetLang = conf.targetLang;
            displayLangs = conf.displayLangs;
            useGoogleTranslate = conf.useGoogleTranslate;
            // then
            translateHotkey(conf);
            showToolbar();
            main_function(conf);
            buildTranslateToolbar();
        })
        return conf
    }


    function showToolbar() {
        if ($('#showToolbar').length == 0) {
            $(Jupyter.toolbar.add_buttons_group([
                Jupyter.keyboard_manager.actions.register({
                    'help'   : 'Translate current cell',
                    'icon'   : 'fa-language',
                    'handler': translateCurrentCell,
                }, 'translate-cell', 'nbTranslate'),
                Jupyter.keyboard_manager.actions.register({
                    'help'   : 'nbTranslate: Configuration (toggle toolbar)',
                    'icon'   : 'fa-wrench',
                    'handler': translateToolbarToggle //translateToolbar
                }, 'show-nbTranslate-toolbar', 'nbTranslate'),
            ])).find('.btn').eq(0).attr('id', 'showToolbar');
        }
    }

    function translateHotkey(conf) {
        add_edit_shortcuts[conf['hotkey']] = {
            help: "Translate current cell",
            help_index: 'yf',
            handler: translateCurrentCell
        };
        Jupyter.keyboard_manager.edit_shortcuts.add_shortcuts(add_edit_shortcuts);
        Jupyter.keyboard_manager.command_shortcuts.add_shortcuts(add_edit_shortcuts);
    }

    function main_function(conf) {
        //alert(log_prefix+" main_function output")
        show_mdcells(conf.displayLangs);
        // add the targetLang to the list of langs, if not already present
        if (listOfLangsInNotebook.indexOf(conf.targetLang) == -1) {
                listOfLangsInNotebook.push(targetLang);
            }
        // add the sourceLang to the list of langs, if not already present
        if (listOfLangsInNotebook.indexOf(conf.sourceLang) == -1) {
                listOfLangsInNotebook.push(sourceLang);
            }
        // Display only the langs present in notebook 
        if (conf.displayLangs.indexOf('*') == -1) {
            conf.displayLangs = $.makeArray($(listOfLangsInNotebook).filter(conf.displayLangs));
        }
        else {
            conf.displayLangs = ['*'];
        }
        // console.log(log_prefix, "Displaying langs ", conf.displayLangs);

    }

    function load_notebook_extension() {

        // Wait for the notebook to be fully loaded
        if (Jupyter.notebook._fully_loaded) {
            // this tests if the notebook is fully loaded 
            console.log(log_prefix + "Notebook fully loaded -- nbTranslate initialized ")
            conf = initialize(conf);
        } else {
            console.log(log_prefix + "Waiting for notebook availability")
            $([Jupyter.events]).on("notebook_loaded.Notebook", function() {
                console.log(log_prefix + "nbTranslate initialized (via notebook_loaded)")
                conf = initialize(conf);                
            })
        }

    }


    return {
        load_ipython_extension: load_notebook_extension
    };
});