From 98d22a92f33e50d6c75df08be70cf801610b4fb7 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Mon, 12 Jun 2017 22:58:02 -0700 Subject: prepare ui for zip dictionaries --- ext/bg/js/options.js | 69 +++++++++++++++++++--------------------------------- 1 file changed, 25 insertions(+), 44 deletions(-) (limited to 'ext/bg/js') diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index c3321012..0b8b7ac3 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -140,9 +140,7 @@ $(document).ready(() => { $('#scan-modifier-key').val(options.scanning.modifier); $('#dict-purge').click(onDictionaryPurge); - $('#dict-importer a').click(onDictionarySetUrl); - $('#dict-import').click(onDictionaryImport); - $('#dict-url').on('input', onDictionaryUpdateUrl); + $('#dict-file').change(onDictionaryImport); $('#anki-enable').prop('checked', options.anki.enable); $('#card-tags').val(options.anki.tags.join(' ')); @@ -253,49 +251,32 @@ function onDictionaryPurge(e) { } function onDictionaryImport() { - dictionaryErrorShow(null); - dictionarySpinnerShow(true); - - const dictUrl = $('#dict-url'); - const dictImporter = $('#dict-importer').hide(); - const dictProgress = $('#dict-import-progress').show(); - const setProgress = percent => dictProgress.find('.progress-bar').css('width', `${percent}%`); - - setProgress(0.0); - - optionsLoad().then(options => { - instDb().importDictionary(dictUrl.val(), (total, current) => setProgress(current / total * 100.0)).then(summary => { - options.dictionaries[summary.title] = {enabled: true, priority: 0}; - return optionsSave(options); - }).then(() => dictionaryGroupsPopulate(options)).catch(dictionaryErrorShow).then(() => { - dictionarySpinnerShow(false); - dictProgress.hide(); - dictImporter.show(); - dictUrl.val(''); - dictUrl.trigger('input'); - }); - }); -} - -function onDictionarySetUrl(e) { - e.preventDefault(); - - const dictUrl = $('#dict-url'); - const url = $(this).data('url'); - if (url.includes('/')) { - dictUrl.val(url); - } else { - dictUrl.val(chrome.extension.getURL(`bg/lang/dict/${url}/index.json`)); - } - - dictUrl.trigger('input'); -} - -function onDictionaryUpdateUrl() { - $('#dict-import').prop('disabled', $(this).val().length === 0); + alert('import'); + + // dictionaryErrorShow(null); + // dictionarySpinnerShow(true); + + // const dictUrl = $('#dict-url'); + // const dictImporter = $('#dict-importer').hide(); + // const dictProgress = $('#dict-import-progress').show(); + // const setProgress = percent => dictProgress.find('.progress-bar').css('width', `${percent}%`); + + // setProgress(0.0); + + // optionsLoad().then(options => { + // instDb().importDictionary(dictUrl.val(), (total, current) => setProgress(current / total * 100.0)).then(summary => { + // options.dictionaries[summary.title] = {enabled: true, priority: 0}; + // return optionsSave(options); + // }).then(() => dictionaryGroupsPopulate(options)).catch(dictionaryErrorShow).then(() => { + // dictionarySpinnerShow(false); + // dictProgress.hide(); + // dictImporter.show(); + // dictUrl.val(''); + // dictUrl.trigger('input'); + // }); + // }); } - /* * Anki */ -- cgit v1.2.3 From d3c342071f1397a59f94d89ca617e6294c50f861 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 25 Jun 2017 15:36:28 -0700 Subject: support importing from zip files --- ext/bg/background.html | 1 + ext/bg/js/database.js | 8 ++--- ext/bg/js/options.js | 46 ++++++++++++------------- ext/bg/js/util.js | 91 ++++++++++++++++++++++++++++++-------------------- 4 files changed, 80 insertions(+), 66 deletions(-) (limited to 'ext/bg/js') diff --git a/ext/bg/background.html b/ext/bg/background.html index b5ae147b..4410c249 100644 --- a/ext/bg/background.html +++ b/ext/bg/background.html @@ -7,6 +7,7 @@ + diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index 51f639d9..70aeb0d7 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -155,7 +155,7 @@ class Database { return this.db.dictionaries.toArray(); } - importDictionary(indexUrl, callback) { + importDictionary(archive, callback) { if (this.db === null) { return Promise.reject('database not initialized'); } @@ -204,7 +204,7 @@ class Database { return this.db.terms.bulkAdd(rows).then(() => { if (callback) { - callback(total, current, indexUrl); + callback(total, current); } }); }; @@ -224,11 +224,11 @@ class Database { return this.db.kanji.bulkAdd(rows).then(() => { if (callback) { - callback(total, current, indexUrl); + callback(total, current); } }); }; - return jsonLoadDb(indexUrl, indexLoaded, termsLoaded, kanjiLoaded).then(() => summary); + return zipLoadDb(archive, indexLoaded, termsLoaded, kanjiLoaded).then(() => summary); } } diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index 0b8b7ac3..cde1fc3f 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -250,31 +250,27 @@ function onDictionaryPurge(e) { }); } -function onDictionaryImport() { - alert('import'); - - // dictionaryErrorShow(null); - // dictionarySpinnerShow(true); - - // const dictUrl = $('#dict-url'); - // const dictImporter = $('#dict-importer').hide(); - // const dictProgress = $('#dict-import-progress').show(); - // const setProgress = percent => dictProgress.find('.progress-bar').css('width', `${percent}%`); - - // setProgress(0.0); - - // optionsLoad().then(options => { - // instDb().importDictionary(dictUrl.val(), (total, current) => setProgress(current / total * 100.0)).then(summary => { - // options.dictionaries[summary.title] = {enabled: true, priority: 0}; - // return optionsSave(options); - // }).then(() => dictionaryGroupsPopulate(options)).catch(dictionaryErrorShow).then(() => { - // dictionarySpinnerShow(false); - // dictProgress.hide(); - // dictImporter.show(); - // dictUrl.val(''); - // dictUrl.trigger('input'); - // }); - // }); +function onDictionaryImport(e) { + dictionaryErrorShow(null); + dictionarySpinnerShow(true); + + const dictUrl = $('#dict-url'); + const dictImporter = $('#dict-importer').hide(); + const dictProgress = $('#dict-import-progress').show(); + const setProgress = percent => dictProgress.find('.progress-bar').css('width', `${percent}%`); + + setProgress(0.0); + + optionsLoad().then(options => { + return instDb().importDictionary(e.target.files[0], (total, current) => setProgress(current / total * 100.0)).then(summary => { + options.dictionaries[summary.title] = {enabled: true, priority: 0}; + return optionsSave(options); + }).then(() => dictionaryGroupsPopulate(options)); + }).catch(dictionaryErrorShow).then(() => { + dictionarySpinnerShow(false); + dictProgress.hide(); + dictImporter.show(); + }); } /* diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js index 75833871..ea307b6b 100644 --- a/ext/bg/js/util.js +++ b/ext/bg/js/util.js @@ -451,14 +451,30 @@ function jsonLoadInt(url) { return jsonLoad(chrome.extension.getURL(url)); } -function jsonLoadDb(indexUrl, indexLoaded, termsLoaded, kanjiLoaded) { - const indexDir = indexUrl.slice(0, indexUrl.lastIndexOf('/')); - return jsonLoad(indexUrl).then(index => { - if (!index.title || !index.version || !index.revision) { - return Promise.reject('unrecognized dictionary format'); +/* + * Zip + */ + +function zipLoadDb(archive, indexLoaded, termsLoaded, kanjiLoaded) { + return JSZip.loadAsync(archive).then(files => { + const fileMap = {}; + files.forEach((path, file) => { + fileMap[path] = file; + }); + + return fileMap; + }).then(files => { + const indexFile = files['index.json']; + if (!indexFile) { + return Promise.reject('no dictionary index found in archive'); } - if (indexLoaded !== null) { + return indexFile.async('string').then(indexJson => { + const index = JSON.parse(indexJson); + if (!index.title || !index.version || !index.revision) { + return Promise.reject('unrecognized dictionary format'); + } + return indexLoaded( index.title, index.version, @@ -467,44 +483,45 @@ function jsonLoadDb(indexUrl, indexLoaded, termsLoaded, kanjiLoaded) { index.termBanks > 0, index.kanjiBanks > 0 ).then(() => index); - } + }).then(index => { + const loaders = []; + const banksTotal = index.termBanks + index.kanjiBanks; + let banksLoaded = 0; + + for (let i = 1; i <= index.termBanks; ++i) { + const bankFile = files[`term_bank_${i}.json`]; + if (!bankFile) { + return Promise.reject('missing term bank file'); + } + + loaders.push(() => bankFile.async('string').then(bankJson => { + const bank = JSON.parse(bankJson); + return termsLoaded(index.title, bank, banksTotal, banksLoaded++); + })); + } - return index; - }).then(index => { - const loaders = []; - const banksTotal = index.termBanks + index.kanjiBanks; - let banksLoaded = 0; + for (let i = 1; i <= index.kanjiBanks; ++i) { + const bankFile = files[`kanji_bank_${i}.json`]; + if (!bankFile) { + return Promise.reject('missing kanji bank file'); + } - for (let i = 1; i <= index.termBanks; ++i) { - const bankUrl = `${indexDir}/term_bank_${i}.json`; - loaders.push(() => jsonLoad(bankUrl).then(entries => termsLoaded( - index.title, - entries, - banksTotal, - banksLoaded++ - ))); - } - - for (let i = 1; i <= index.kanjiBanks; ++i) { - const bankUrl = `${indexDir}/kanji_bank_${i}.json`; - loaders.push(() => jsonLoad(bankUrl).then(entries => kanjiLoaded( - index.title, - entries, - banksTotal, - banksLoaded++ - ))); - } + loaders.push(() => bankFile.async('string').then(bankJson => { + const bank = JSON.parse(bankJson); + return kanjiLoaded(index.title, bank, banksTotal, banksLoaded++); + })); + } - let chain = Promise.resolve(); - for (const loader of loaders) { - chain = chain.then(loader); - } + let chain = Promise.resolve(); + for (const loader of loaders) { + chain = chain.then(loader); + } - return chain; + return chain; + }); }); } - /* * Helpers */ -- cgit v1.2.3 From 4cb7e8d2ac9bca9912dc728a0bd43f643bff6dda Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 25 Jun 2017 15:43:29 -0700 Subject: reset file input after upload --- ext/bg/js/options.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'ext/bg/js') diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index cde1fc3f..5aa18366 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -254,19 +254,21 @@ function onDictionaryImport(e) { dictionaryErrorShow(null); dictionarySpinnerShow(true); - const dictUrl = $('#dict-url'); + const dictFile = $('#dict-file'); const dictImporter = $('#dict-importer').hide(); const dictProgress = $('#dict-import-progress').show(); const setProgress = percent => dictProgress.find('.progress-bar').css('width', `${percent}%`); + const updateProgress = (total, current) => setProgress(current / total * 100.0); setProgress(0.0); optionsLoad().then(options => { - return instDb().importDictionary(e.target.files[0], (total, current) => setProgress(current / total * 100.0)).then(summary => { + return instDb().importDictionary(e.target.files[0], updateProgress).then(summary => { options.dictionaries[summary.title] = {enabled: true, priority: 0}; return optionsSave(options); }).then(() => dictionaryGroupsPopulate(options)); }).catch(dictionaryErrorShow).then(() => { + dictFile.val(''); dictionarySpinnerShow(false); dictProgress.hide(); dictImporter.show(); -- cgit v1.2.3 From 1849f95b4ecf618a250d37268ad6d2a7f3ea8888 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 25 Jun 2017 15:56:30 -0700 Subject: simplify file loading --- ext/bg/js/util.js | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) (limited to 'ext/bg/js') diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js index ea307b6b..cdd5ec31 100644 --- a/ext/bg/js/util.js +++ b/ext/bg/js/util.js @@ -456,14 +456,7 @@ function jsonLoadInt(url) { */ function zipLoadDb(archive, indexLoaded, termsLoaded, kanjiLoaded) { - return JSZip.loadAsync(archive).then(files => { - const fileMap = {}; - files.forEach((path, file) => { - fileMap[path] = file; - }); - - return fileMap; - }).then(files => { + return JSZip.loadAsync(archive).then(files => files.files).then(files => { const indexFile = files['index.json']; if (!indexFile) { return Promise.reject('no dictionary index found in archive'); -- cgit v1.2.3 From 5e72c387ccf37501d79536c8dde4d96fd15c6a3e Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 25 Jun 2017 21:14:39 -0700 Subject: fix result page for no items found --- ext/bg/js/templates.js | 4 ++-- ext/fg/frame.html | 2 +- ext/mixed/css/frame.css | 2 +- tmpl/kanji.html | 2 +- tmpl/terms.html | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) (limited to 'ext/bg/js') diff --git a/ext/bg/js/templates.js b/ext/bg/js/templates.js index d2e5435e..f10134ee 100644 --- a/ext/bg/js/templates.js +++ b/ext/bg/js/templates.js @@ -404,7 +404,7 @@ templates['kanji.html'] = template({"1":function(container,depth0,helpers,partia },"23":function(container,depth0,helpers,partials,data) { return "
"; },"25":function(container,depth0,helpers,partials,data) { - return "

No results found.

\n"; + return "

No results found.

\n"; },"compiler":[7,">= 4.0.0"],"main":function(container,depth0,helpers,partials,data,blockParams,depths) { var stack1; @@ -586,7 +586,7 @@ templates['terms.html'] = template({"1":function(container,depth0,helpers,partia },"39":function(container,depth0,helpers,partials,data) { return "
"; },"41":function(container,depth0,helpers,partials,data) { - return "

No results found.

\n"; + return "

No results found.

\n"; },"compiler":[7,">= 4.0.0"],"main":function(container,depth0,helpers,partials,data,blockParams,depths) { var stack1; diff --git a/ext/fg/frame.html b/ext/fg/frame.html index 43f46d33..c20745af 100644 --- a/ext/fg/frame.html +++ b/ext/fg/frame.html @@ -7,7 +7,7 @@