|
|
|
|
|
|
|
|
|
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 + '] '; |
|
|
|
|
|
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() { |
|
|
|
|
|
conf = $.extend(false, {}, conf, Jupyter.notebook.config.data.nbTranslate); |
|
|
|
|
|
conf = Jupyter.notebook.metadata.nbTranslate = $.extend(false, {}, conf, |
|
Jupyter.notebook.metadata.nbTranslate); |
|
|
|
|
|
sourceLang = conf.sourceLang; |
|
targetLang = conf.targetLang; |
|
displayLangs = conf.displayLangs; |
|
useGoogleTranslate = conf.useGoogleTranslate; |
|
|
|
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 |
|
}, '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) { |
|
|
|
show_mdcells(conf.displayLangs); |
|
|
|
if (listOfLangsInNotebook.indexOf(conf.targetLang) == -1) { |
|
listOfLangsInNotebook.push(targetLang); |
|
} |
|
|
|
if (listOfLangsInNotebook.indexOf(conf.sourceLang) == -1) { |
|
listOfLangsInNotebook.push(sourceLang); |
|
} |
|
|
|
if (conf.displayLangs.indexOf('*') == -1) { |
|
conf.displayLangs = $.makeArray($(listOfLangsInNotebook).filter(conf.displayLangs)); |
|
} |
|
else { |
|
conf.displayLangs = ['*']; |
|
} |
|
|
|
|
|
} |
|
|
|
function load_notebook_extension() { |
|
|
|
|
|
if (Jupyter.notebook._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 |
|
}; |
|
}); |
|
|