From 8f995194bbf23a074a901c8fdaf4ddad22370dd3 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Mon, 7 Nov 2016 09:16:38 -0800 Subject: [PATCH] WIP --- ext/bg/js/options-form.js | 47 +++++++++++++++++++++++++++++++++++---- ext/bg/js/templates.js | 2 +- ext/bg/js/util.js | 1 + ext/bg/options.html | 6 ++--- tmpl/dictionary.html | 2 +- 5 files changed, 49 insertions(+), 9 deletions(-) diff --git a/ext/bg/js/options-form.js b/ext/bg/js/options-form.js index 14143d81..ce829698 100644 --- a/ext/bg/js/options-form.js +++ b/ext/bg/js/options-form.js @@ -166,6 +166,9 @@ function populateDictionaries(opts) { const container = $('.dicts'); container.empty(); + const dictError = $('#dict-error'); + dictError.hide(); + yomichan().translator.database.getDictionaries().then(rows => { rows.forEach(row => { const dictOpts = opts.dictionaries[row.title] || {enableTerms: true, enableKanji: false}; @@ -182,13 +185,47 @@ function populateDictionaries(opts) { }); $('.dict-delete').click(e => { - const dict = $(e.target).closest('.dict'); + const button = $(e.target); + const dict = button.closest('.dict'); const title = dict.data('title'); - yomichan().translator.database.deleteDictionary(title); - dict.slideUp(); + + button.prop('disabled', true); + yomichan().translator.database.deleteDictionary(title).then(() => { + dict.slideUp(); + }).catch(error => { + dictError.show().find('span').text(error); + }).then(() => { + button.prop('disabled', false); + }); }); container.find('.dict input').change(onOptionsChanged); + }).catch(error => { + dictError.show().find('span').text(error); + }); +} + +function onImportDictionary() { + const dictInputs = $('#dict-import').find('input'); + dictInputs.prop('disabled', true); + + const dictError = $('#dict-error'); + dictError.hide(); + + const progressbar = $('#dict-import-progress'); + const progressValue = progressbar.find('div'); + progressbar.show(); + + const callback = (total, current) => { + $('.progress-bar').css('width', `${current / total * 100.0}%`); + }; + + const dictUrl = $('#dict-import-url').val(); + yomichan().translator.database.importDictionary(dictUrl, callback).catch(error => { + dictError.show().find('span').text(error); + }).then(() => { + dictInputs.prop('disabled', false); + progressbar.hide(); }); } @@ -303,7 +340,7 @@ $(document).ready(() => { if (url.includes('/')) { control.val(url); } else { - control.val(chrome.extension.getURL(`bg/data/${url}`)); + control.val(chrome.extension.getURL(`bg/data/${url}/index.json`)); } control.trigger('input'); }); @@ -314,6 +351,8 @@ $(document).ready(() => { $('#dict-import-start').prop('disabled', disable); }); + $('#dict-import-start').click(onImportDictionary); + populateDictionaries(opts); populateAnkiDeckAndModel(opts); updateVisibility(opts); diff --git a/ext/bg/js/templates.js b/ext/bg/js/templates.js index d42d972d..ce44de8d 100644 --- a/ext/bg/js/templates.js +++ b/ext/bg/js/templates.js @@ -13,7 +13,7 @@ templates['dictionary.html'] = template({"1":function(container,depth0,helpers,p + alias4(((helper = (helper = helpers.title || (depth0 != null ? depth0.title : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"title","hash":{},"data":data}) : helper))) + " v." + alias4(((helper = (helper = helpers.version || (depth0 != null ? depth0.version : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"version","hash":{},"data":data}) : helper))) - + "\n \n
\n \n
\n \n\n
\n
\n
\n \n
\n \n\n
\n