From fd2820bc1af1f2a4dac477f1d2963a3b913fdb32 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sat, 5 Nov 2016 16:44:29 -0700 Subject: WIP --- ext/bg/js/dictionary.js | 94 ++++++++++++++++--------------------------------- ext/bg/js/util.js | 24 +++++++++++++ 2 files changed, 55 insertions(+), 63 deletions(-) (limited to 'ext/bg/js') diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index 1d54190e..2c9636ab 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -20,7 +20,7 @@ class Dictionary { constructor() { this.db = null; - this.dbVer = 2; + this.dbVer = 3; this.entities = null; } @@ -135,49 +135,31 @@ class Dictionary { return Promise.reject('database not initialized'); } - const indexDir = indexUrl.slice(0, indexUrl.lastIndexOf('/')); - return loadJson(indexUrl).then(index => { - const entities = []; - for (const [name, value] of index.ents) { - entities.push({name, value}); + const entitiesLoaded = entities => { + this.entities = entities || {}; + + const rows = []; + for (const name in entities || {}) { + rows.push({name, value: entities[name]}); } - return this.db.entities.bulkAdd(entities).then(() => { - if (this.entities === null) { - this.entities = {}; - } + return this.db.entities.bulkAdd(rows); + }; - for (const entity of entities) { - this.entities[entity.name] = entity.value; - } - }).then(() => { - const loaders = []; - for (let i = 1; i <= index.banks; ++i) { - const bankUrl = `${indexDir}/bank_${i}.json`; - loaders.push(() => { - return loadJson(bankUrl).then(definitions => { - const rows = []; - for (const [expression, reading, tags, ...glossary] of definitions) { - rows.push({expression, reading, tags, glossary}); - } - - return this.db.terms.bulkAdd(rows).then(() => { - if (callback) { - callback(i, index.banks, indexUrl); - } - }); - }); - }); - } + const entriesLoaded = (entries, total, current) => { + const rows = []; + for (const [expression, reading, tags, ...glossary] of entries) { + rows.push({expression, reading, tags, glossary}); + } - let chain = Promise.resolve(); - for (const loader of loaders) { - chain = chain.then(loader); + return this.db.terms.bulkAdd(rows).then(() => { + if (callback) { + callback(current, total, indexUrl); } - - return chain; }); - }); + }; + + return importJsonDb(indexUrl, entitiesLoaded, entriesLoaded); } importKanjiDict(indexUrl, callback) { @@ -185,33 +167,19 @@ class Dictionary { return Promise.reject('database not initialized'); } - const indexDir = indexUrl.slice(0, indexUrl.lastIndexOf('/')); - return loadJson(indexUrl).then(index => { - const loaders = []; - for (let i = 1; i <= index.banks; ++i) { - const bankUrl = `${indexDir}/bank_${i}.json`; - loaders.push(() => { - return loadJson(bankUrl).then(definitions => { - const rows = []; - for (const [character, onyomi, kunyomi, tags, ...meanings] of definitions) { - rows.push({character, onyomi, kunyomi, tags, meanings}); - } - - return this.db.kanji.bulkAdd(rows).then(() => { - if (callback) { - callback(i, index.banks, indexUrl); - } - }); - }); - }); + const entriesLoaded = (entries, total, current) => { + const rows = []; + for (const [character, onyomi, kunyomi, tags, ...meanings] of entries) { + rows.push({character, onyomi, kunyomi, tags, meanings}); } - let chain = Promise.resolve(); - for (const loader of loaders) { - chain = chain.then(loader); - } + return this.db.kanji.bulkAdd(rows).then(() => { + if (callback) { + callback(current, total, indexUrl); + } + }); + }; - return chain; - }); + return importJsonDb(indexUrl, null, entriesLoaded); } } diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js index 4e0cc671..888bcb33 100644 --- a/ext/bg/js/util.js +++ b/ext/bg/js/util.js @@ -116,3 +116,27 @@ function applyTagMeta(tag, meta) { function splitField(field) { return field.length === 0 ? [] : field.split(' '); } + +function importJsonDb(indexUrl, entitiesLoaded, entriesLoaded) { + const indexDir = indexUrl.slice(0, indexUrl.lastIndexOf('/')); + return loadJson(indexUrl).then(index => { + if (entitiesLoaded !== null) { + return entitiesLoaded(index.entities, index.banks).then(() => index); + } + + return index; + }).then(index => { + const loaders = []; + for (let i = 1; i <= index.banks; ++i) { + const bankUrl = `${indexDir}/bank_${i}.json`; + loaders.push(() => loadJson(bankUrl).then(entries => entriesLoaded(entries, index.banks, i))); + } + + let chain = Promise.resolve(); + for (const loader of loaders) { + chain = chain.then(loader); + } + + return chain; + }); +} -- cgit v1.2.3 From e2f1560afa25274d3b0efd3ce8fc8a17740ab7cc Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sat, 5 Nov 2016 17:30:00 -0700 Subject: WIP --- ext/bg/js/dictionary.js | 41 +++++++++++++++++++++++++++++------------ ext/bg/js/util.js | 8 ++++---- 2 files changed, 33 insertions(+), 16 deletions(-) (limited to 'ext/bg/js') diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index 2c9636ab..a2a4047a 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -20,7 +20,7 @@ class Dictionary { constructor() { this.db = null; - this.dbVer = 3; + this.dbVer = 4; this.entities = null; } @@ -31,10 +31,10 @@ class Dictionary { this.db = new Dexie('dict'); this.db.version(1).stores({ - terms: '++id,expression,reading', - entities: '++,name', - kanji: '++,character', - meta: 'name,value', + terms: '++id, dictionary, expression, reading', + kanji: '++, dictionary, character', + entities: '++, dictionary, name', + meta: 'name, value', }); } @@ -135,21 +135,31 @@ class Dictionary { return Promise.reject('database not initialized'); } - const entitiesLoaded = entities => { + const indexLoaded = (dictionary, entities) => { this.entities = entities || {}; const rows = []; for (const name in entities || {}) { - rows.push({name, value: entities[name]}); + rows.push({ + dictionary, + name, + value: entities[name] + }); } return this.db.entities.bulkAdd(rows); }; - const entriesLoaded = (entries, total, current) => { + const entriesLoaded = (dictionary, entries, total, current) => { const rows = []; for (const [expression, reading, tags, ...glossary] of entries) { - rows.push({expression, reading, tags, glossary}); + rows.push({ + dictionary, + expression, + reading, + tags, + glossary + }); } return this.db.terms.bulkAdd(rows).then(() => { @@ -159,7 +169,7 @@ class Dictionary { }); }; - return importJsonDb(indexUrl, entitiesLoaded, entriesLoaded); + return importJsonDb(indexUrl, indexLoaded, entriesLoaded); } importKanjiDict(indexUrl, callback) { @@ -167,10 +177,17 @@ class Dictionary { return Promise.reject('database not initialized'); } - const entriesLoaded = (entries, total, current) => { + const entriesLoaded = (dictionary, entries, total, current) => { const rows = []; for (const [character, onyomi, kunyomi, tags, ...meanings] of entries) { - rows.push({character, onyomi, kunyomi, tags, meanings}); + rows.push({ + dictionary, + character, + onyomi, + kunyomi, + tags, + meanings + }); } return this.db.kanji.bulkAdd(rows).then(() => { diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js index 888bcb33..9b17f43c 100644 --- a/ext/bg/js/util.js +++ b/ext/bg/js/util.js @@ -117,11 +117,11 @@ function splitField(field) { return field.length === 0 ? [] : field.split(' '); } -function importJsonDb(indexUrl, entitiesLoaded, entriesLoaded) { +function importJsonDb(indexUrl, indexLoaded, entriesLoaded) { const indexDir = indexUrl.slice(0, indexUrl.lastIndexOf('/')); return loadJson(indexUrl).then(index => { - if (entitiesLoaded !== null) { - return entitiesLoaded(index.entities, index.banks).then(() => index); + if (indexLoaded !== null) { + return indexLoaded(index.title, index.entities, index.banks).then(() => index); } return index; @@ -129,7 +129,7 @@ function importJsonDb(indexUrl, entitiesLoaded, entriesLoaded) { const loaders = []; for (let i = 1; i <= index.banks; ++i) { const bankUrl = `${indexDir}/bank_${i}.json`; - loaders.push(() => loadJson(bankUrl).then(entries => entriesLoaded(entries, index.banks, i))); + loaders.push(() => loadJson(bankUrl).then(entries => entriesLoaded(index.title, entries, index.banks, i))); } let chain = Promise.resolve(); -- cgit v1.2.3 From d3a01738740c9a38011e5b98b3f37709bea515e1 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sat, 5 Nov 2016 17:51:01 -0700 Subject: Dictionary naming --- ext/bg/js/dictionary.js | 38 +++++++++++++++++++++++--------------- ext/bg/js/util.js | 4 ++-- 2 files changed, 25 insertions(+), 17 deletions(-) (limited to 'ext/bg/js') diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index a2a4047a..936f3dd8 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -33,7 +33,9 @@ class Dictionary { this.db.version(1).stores({ terms: '++id, dictionary, expression, reading', kanji: '++, dictionary, character', - entities: '++, dictionary, name', + entities: '++, dictionary', + termDicts: '++, dictionary', + kanjiDicts: '++, dictionary, version', meta: 'name, value', }); } @@ -135,22 +137,24 @@ class Dictionary { return Promise.reject('database not initialized'); } - const indexLoaded = (dictionary, entities) => { - this.entities = entities || {}; - - const rows = []; - for (const name in entities || {}) { - rows.push({ - dictionary, - name, - value: entities[name] - }); - } + const indexLoaded = (dictionary, version, entities) => { + return this.db.termDicts.add({dictionary, version}).then(() => { + this.entities = entities || {}; + + const rows = []; + for (const name in entities || {}) { + rows.push({ + dictionary, + name, + value: entities[name] + }); + } - return this.db.entities.bulkAdd(rows); + return this.db.entities.bulkAdd(rows); + }); }; - const entriesLoaded = (dictionary, entries, total, current) => { + const entriesLoaded = (dictionary, version, entries, total, current) => { const rows = []; for (const [expression, reading, tags, ...glossary] of entries) { rows.push({ @@ -177,7 +181,11 @@ class Dictionary { return Promise.reject('database not initialized'); } - const entriesLoaded = (dictionary, entries, total, current) => { + const indexLoaded = (dictionary, version) => { + return this.db.kanjiDicts.add({dictionary, version}); + }; + + const entriesLoaded = (dictionary, version, entries, total, current) => { const rows = []; for (const [character, onyomi, kunyomi, tags, ...meanings] of entries) { rows.push({ diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js index 9b17f43c..ac365135 100644 --- a/ext/bg/js/util.js +++ b/ext/bg/js/util.js @@ -121,7 +121,7 @@ function importJsonDb(indexUrl, indexLoaded, entriesLoaded) { const indexDir = indexUrl.slice(0, indexUrl.lastIndexOf('/')); return loadJson(indexUrl).then(index => { if (indexLoaded !== null) { - return indexLoaded(index.title, index.entities, index.banks).then(() => index); + return indexLoaded(index.title, index.version, index.entities, index.banks).then(() => index); } return index; @@ -129,7 +129,7 @@ function importJsonDb(indexUrl, indexLoaded, entriesLoaded) { const loaders = []; for (let i = 1; i <= index.banks; ++i) { const bankUrl = `${indexDir}/bank_${i}.json`; - loaders.push(() => loadJson(bankUrl).then(entries => entriesLoaded(index.title, entries, index.banks, i))); + loaders.push(() => loadJson(bankUrl).then(entries => entriesLoaded(index.title, index.version, entries, index.banks, i))); } let chain = Promise.resolve(); -- cgit v1.2.3 From 92fbc14e0bea1ca04dd78f0ac788c528918cb133 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sat, 5 Nov 2016 18:10:49 -0700 Subject: WIP --- ext/bg/js/dictionary.js | 9 ++++----- ext/bg/options.html | 7 +++++++ 2 files changed, 11 insertions(+), 5 deletions(-) (limited to 'ext/bg/js') diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index 936f3dd8..37a799a6 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -20,7 +20,7 @@ class Dictionary { constructor() { this.db = null; - this.dbVer = 4; + this.dbVer = 5; this.entities = null; } @@ -34,8 +34,7 @@ class Dictionary { terms: '++id, dictionary, expression, reading', kanji: '++, dictionary, character', entities: '++, dictionary', - termDicts: '++, dictionary', - kanjiDicts: '++, dictionary, version', + dictionaries: '++, dictionary, version', meta: 'name, value', }); } @@ -138,7 +137,7 @@ class Dictionary { } const indexLoaded = (dictionary, version, entities) => { - return this.db.termDicts.add({dictionary, version}).then(() => { + return this.db.dictionaries.add({dictionary, version}).then(() => { this.entities = entities || {}; const rows = []; @@ -182,7 +181,7 @@ class Dictionary { } const indexLoaded = (dictionary, version) => { - return this.db.kanjiDicts.add({dictionary, version}); + return this.db.dictionaries.add({dictionary, version}); }; const entriesLoaded = (dictionary, version, entries, total, current) => { diff --git a/ext/bg/options.html b/ext/bg/options.html index 42029727..458174bb 100644 --- a/ext/bg/options.html +++ b/ext/bg/options.html @@ -62,6 +62,13 @@ +
+

Dictionaries

+ +
+
+
+
-- cgit v1.2.3 From c24e179b10d31c445ab6393fe1a39b570887a3aa Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sat, 5 Nov 2016 18:24:45 -0700 Subject: WIP --- ext/bg/js/dictionary.js | 12 ++++++++++-- ext/bg/js/options-form.js | 11 +++++++++++ ext/bg/options.html | 2 +- 3 files changed, 22 insertions(+), 3 deletions(-) (limited to 'ext/bg/js') diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index 37a799a6..aa5f0b47 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -20,7 +20,7 @@ class Dictionary { constructor() { this.db = null; - this.dbVer = 5; + this.dbVer = 4; this.entities = null; } @@ -131,6 +131,14 @@ class Dictionary { }); } + getDictionaries() { + if (this.db === null) { + return Promise.reject('database not initialized'); + } + + return this.db.dictionaries.toArray(); + } + importTermDict(indexUrl, callback) { if (this.db === null) { return Promise.reject('database not initialized'); @@ -204,6 +212,6 @@ class Dictionary { }); }; - return importJsonDb(indexUrl, null, entriesLoaded); + return importJsonDb(indexUrl, indexLoaded, entriesLoaded); } } diff --git a/ext/bg/js/options-form.js b/ext/bg/js/options-form.js index eb562142..589d92b1 100644 --- a/ext/bg/js/options-form.js +++ b/ext/bg/js/options-form.js @@ -154,6 +154,16 @@ function populateAnkiDeckAndModel(opts) { }); } +function populateDictionaries() { + const dictionaries = $('.dictionaries'); + yomichan().translator.dictionary.getDictionaries().then(rows => { + for (const row of rows) { + const dictionary = $('

').text(row.dictionary); + dictionaries.append(dictionary); + } + }); +} + function populateAnkiFields(element, opts) { const table = element.closest('.tab-pane').find('.anki-fields'); table.find('tbody').remove(); @@ -274,6 +284,7 @@ $(document).ready(() => { $('input, select').not('.anki-model').change(onOptionsChanged); $('.anki-model').change(onAnkiModelChanged); + populateDictionaries(); populateAnkiDeckAndModel(opts); updateVisibility(opts); }); diff --git a/ext/bg/options.html b/ext/bg/options.html index 458174bb..77d7edf1 100644 --- a/ext/bg/options.html +++ b/ext/bg/options.html @@ -65,7 +65,7 @@

Dictionaries

-
+
-- cgit v1.2.3 From 81b7f504f4b48c1514b4c6dd88eba6aafcd9a151 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sat, 5 Nov 2016 22:49:13 -0700 Subject: WIP --- ext/bg/js/options-form.js | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) (limited to 'ext/bg/js') diff --git a/ext/bg/js/options-form.js b/ext/bg/js/options-form.js index 589d92b1..5327a04a 100644 --- a/ext/bg/js/options-form.js +++ b/ext/bg/js/options-form.js @@ -155,13 +155,25 @@ function populateAnkiDeckAndModel(opts) { } function populateDictionaries() { - const dictionaries = $('.dictionaries'); - yomichan().translator.dictionary.getDictionaries().then(rows => { - for (const row of rows) { - const dictionary = $('

').text(row.dictionary); - dictionaries.append(dictionary); - } - }); + // const dictGroups = $('.dictionaries'); + // dictGroups.empty(); + + // yomichan().translator.dictionary.getDictionaries().then(rows => { + // for (const row of rows) { + // const dictPanel = $('

', {class: 'dictionary panel panel-default'}); + // const dictRow = $('
', {class: 'panel-body row'}).appendTo(dictPanel); + + // const title = $('
', {class: 'col-xs-8'}); + // $('

').append().html(` ${row.dictionary} v.${row.version}`).appendTo(title); + // title.appendTo(dictRow); + + // const controls = $('
', {class: 'col-xs-4 text-right'}); + // $('\n
\n

\n\n" + + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.hasTerms : depth0),{"name":"if","hash":{},"fn":container.program(1, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.hasKanji : depth0),{"name":"if","hash":{},"fn":container.program(3, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + "
\n"; +},"useData":true}); templates['footer.html'] = template({"compiler":[7,">= 4.0.0"],"main":function(container,depth0,helpers,partials,data) { var helper; diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js index eaa7dd01..e99cdeab 100644 --- a/ext/bg/js/util.js +++ b/ext/bg/js/util.js @@ -121,7 +121,13 @@ function importJsonDb(indexUrl, indexLoaded, termsLoaded, kanjiLoaded) { const indexDir = indexUrl.slice(0, indexUrl.lastIndexOf('/')); return loadJson(indexUrl).then(index => { if (indexLoaded !== null) { - return indexLoaded(index.title, index.version).then(() => index); + return indexLoaded( + index.title, + index.version, + index.entities, + index.termBanks > 0, + index.kanjiBanks > 0 + ).then(() => index); } return index; @@ -134,7 +140,6 @@ function importJsonDb(indexUrl, indexLoaded, termsLoaded, kanjiLoaded) { const bankUrl = `${indexDir}/term_bank_${i}.json`; loaders.push(() => loadJson(bankUrl).then(entries => termsLoaded( index.title, - index.version, entries, banksTotal, banksLoaded++ @@ -145,7 +150,6 @@ function importJsonDb(indexUrl, indexLoaded, termsLoaded, kanjiLoaded) { const bankUrl = `${indexDir}/kanji_bank_${i}.json`; loaders.push(() => loadJson(bankUrl).then(entries => kanjiLoaded( index.title, - index.version, entries, banksTotal, banksLoaded++ diff --git a/ext/bg/options.html b/ext/bg/options.html index 1136eefc..acd786cf 100644 --- a/ext/bg/options.html +++ b/ext/bg/options.html @@ -124,18 +124,18 @@
- -

Yomichan features automatic flashcard creation for Anki, a free application designed to help you retain knowledge. While the AnkiConnect plugin offers the best experience, it is also possible to create flashcards through AnkiWeb, provided you already have an account.

+ +
@@ -217,6 +217,8 @@ + + diff --git a/tmpl/dictionary.html b/tmpl/dictionary.html new file mode 100644 index 00000000..21847722 --- /dev/null +++ b/tmpl/dictionary.html @@ -0,0 +1,21 @@ +
+
+
+

{{name}} v.{{version}}

+
+
+ +
+
+ + {{#if hasTerms}} +
+ +
+ {{/if}} + {{#if hasKanji}} +
+ +
+ {{/if}} +
-- cgit v1.2.3 From 712cd6a9ab4e627e3c1504740ff853a6d12025f4 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 6 Nov 2016 17:56:47 -0800 Subject: Simplify field code --- ext/bg/js/options-form.js | 56 ++++++++++++++++------------------------------- ext/bg/js/templates.js | 17 ++++++++++++++ tmpl/model.html | 18 +++++++++++++++ 3 files changed, 54 insertions(+), 37 deletions(-) create mode 100644 tmpl/model.html (limited to 'ext/bg/js') diff --git a/ext/bg/js/options-form.js b/ext/bg/js/options-form.js index ec603eed..98ec15a3 100644 --- a/ext/bg/js/options-form.js +++ b/ext/bg/js/options-form.js @@ -155,11 +155,11 @@ function populateAnkiDeckAndModel(opts) { } function populateDictionaries() { - const dictGroups = $('.dictionaries'); - dictGroups.empty(); + const container = $('.dictionaries'); + container.empty(); yomichan().translator.dictionary.getInfo().then(rows => { - for (const row of rows) { + rows.forEach(row => { const html = Handlebars.templates['dictionary.html']({ name: row.dictionary, version: row.version, @@ -167,14 +167,15 @@ function populateDictionaries() { hasKanji: row.hasKanji }); - dictGroups.append($(html)); - } + container.append($(html)); + }); }); } function populateAnkiFields(element, opts) { - const table = element.closest('.tab-pane').find('.anki-fields'); - table.find('tbody').remove(); + const tab = element.closest('.tab-pane'); + const container = tab.find('.anki-fields tbody'); + container.empty(); const modelName = element.val(); if (modelName === null) { @@ -186,41 +187,22 @@ function populateAnkiFields(element, opts) { const markers = modelIdToMarkers(modelId); return anki().getModelFieldNames(modelName).then(names => { - const tbody = $(''); names.forEach(name => { - const button = $('\n
    \n" + + ((stack1 = helpers.each.call(alias1,(depth0 != null ? depth0.markers : depth0),{"name":"each","hash":{},"fn":container.program(1, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + "
\n
\n
\n \n\n"; +},"useData":true}); templates['term.html'] = template({"1":function(container,depth0,helpers,partials,data) { var helper, alias1=depth0 != null ? depth0 : {}, alias2=helpers.helperMissing, alias3="function", alias4=container.escapeExpression; diff --git a/tmpl/model.html b/tmpl/model.html new file mode 100644 index 00000000..94772316 --- /dev/null +++ b/tmpl/model.html @@ -0,0 +1,18 @@ + + {{name}} + +
+ +
+ + +
+
+ + -- cgit v1.2.3 From 20304b5460e54621511a98b9dba9236d4d192ac3 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 6 Nov 2016 18:09:12 -0800 Subject: WIP --- ext/bg/js/options-form.js | 9 ++++++--- ext/bg/js/options.js | 2 ++ ext/bg/js/templates.js | 21 +++++++++++++++------ tmpl/dictionary.html | 12 ++++-------- 4 files changed, 27 insertions(+), 17 deletions(-) (limited to 'ext/bg/js') diff --git a/ext/bg/js/options-form.js b/ext/bg/js/options-form.js index 98ec15a3..3ea850b7 100644 --- a/ext/bg/js/options-form.js +++ b/ext/bg/js/options-form.js @@ -154,17 +154,20 @@ function populateAnkiDeckAndModel(opts) { }); } -function populateDictionaries() { +function populateDictionaries(opts) { const container = $('.dictionaries'); container.empty(); yomichan().translator.dictionary.getInfo().then(rows => { rows.forEach(row => { + const dictOpts = opts.dictionaries[row.dictionary] || {enableTerms: true, enableKanji: false}; const html = Handlebars.templates['dictionary.html']({ name: row.dictionary, version: row.version, hasTerms: row.hasTerms, - hasKanji: row.hasKanji + hasKanji: row.hasKanji, + enableTerms: dictOpts.enableTerms, + enableKanji: dictOpts.enableKanji }); container.append($(html)); @@ -276,7 +279,7 @@ $(document).ready(() => { $('input, select').not('.anki-model').change(onOptionsChanged); $('.anki-model').change(onAnkiModelChanged); - populateDictionaries(); + populateDictionaries(opts); populateAnkiDeckAndModel(opts); updateVisibility(opts); }); diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index 15288afc..28448b96 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -28,6 +28,8 @@ function sanitizeOptions(options) { scanDelay: 15, scanLength: 20, + dictionaries: {}, + ankiMethod: 'disabled', ankiUsername: '', ankiPassword: '', diff --git a/ext/bg/js/templates.js b/ext/bg/js/templates.js index 7e78db3b..fe4d95ca 100644 --- a/ext/bg/js/templates.js +++ b/ext/bg/js/templates.js @@ -1,9 +1,9 @@ (function() { var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {}; templates['dictionary.html'] = template({"1":function(container,depth0,helpers,partials,data) { - return "
\n \n
\n"; + return "disabled"; },"3":function(container,depth0,helpers,partials,data) { - return "
\n \n
\n"; + return "checked"; },"compiler":[7,">= 4.0.0"],"main":function(container,depth0,helpers,partials,data) { var stack1, helper, alias1=depth0 != null ? depth0 : {}, alias2=helpers.helperMissing, alias3="function", alias4=container.escapeExpression; @@ -13,10 +13,19 @@ templates['dictionary.html'] = template({"1":function(container,depth0,helpers,p + alias4(((helper = (helper = helpers.name || (depth0 != null ? depth0.name : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"name","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" - + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.hasTerms : depth0),{"name":"if","hash":{},"fn":container.program(1, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") - + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.hasKanji : depth0),{"name":"if","hash":{},"fn":container.program(3, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") - + "\n"; + + "\n \n
\n \n
\n \n\n
\n \n
\n
\n \n
\n\n"; },"useData":true}); templates['footer.html'] = template({"compiler":[7,">= 4.0.0"],"main":function(container,depth0,helpers,partials,data) { var helper; diff --git a/tmpl/dictionary.html b/tmpl/dictionary.html index 21847722..77493d94 100644 --- a/tmpl/dictionary.html +++ b/tmpl/dictionary.html @@ -8,14 +8,10 @@ - {{#if hasTerms}} -
- +
+
- {{/if}} - {{#if hasKanji}} -
- +
+
- {{/if}}
-- cgit v1.2.3 From 73c7c225f11b9a1da550b9b866f106b188bd82ea Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 6 Nov 2016 18:30:51 -0800 Subject: Save dictionary options --- ext/bg/js/options-form.js | 12 +++++++++++- ext/bg/options.html | 20 +------------------- 2 files changed, 12 insertions(+), 20 deletions(-) (limited to 'ext/bg/js') diff --git a/ext/bg/js/options-form.js b/ext/bg/js/options-form.js index 3ea850b7..f9346d7d 100644 --- a/ext/bg/js/options-form.js +++ b/ext/bg/js/options-form.js @@ -91,6 +91,14 @@ function getFormValues() { optsNew.ankiKanjiModel = $('#anki-kanji-model').val(); optsNew.ankiKanjiFields = fieldsToDict($('#kanji .anki-field-value')); + $('.dictionary').each((index, element) => { + const dictionary = $(element); + const name = dictionary.data('name'); + const enableTerms = dictionary.find('.dict-enable-terms').prop('checked'); + const enableKanji = dictionary.find('.dict-enable-kanji').prop('checked'); + optsNew.dictionaries[name] = {enableTerms, enableKanji}; + }); + return { optsNew: sanitizeOptions(optsNew), optsOld: sanitizeOptions(optsOld) @@ -172,12 +180,14 @@ function populateDictionaries(opts) { container.append($(html)); }); + + container.find('.dictionary input').change(onOptionsChanged); }); } function populateAnkiFields(element, opts) { const tab = element.closest('.tab-pane'); - const container = tab.find('.anki-fields tbody'); + const container = tab.find('tbody'); container.empty(); const modelName = element.val(); diff --git a/ext/bg/options.html b/ext/bg/options.html index acd786cf..bc87d654 100644 --- a/ext/bg/options.html +++ b/ext/bg/options.html @@ -65,25 +65,7 @@

Dictionaries

-
-
-
-
-

Dictionary v.1

-
-
- -
-
- -
- -
-
- -
-
-
+
Error: -- cgit v1.2.3 From 7fa51d682f29fbf51ed1228ca8840ff60be48e5e Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 6 Nov 2016 18:58:00 -0800 Subject: More work on options --- ext/bg/js/options-form.js | 23 ++++++++++++++++++++--- ext/bg/js/templates.js | 2 +- ext/bg/options.html | 16 ++++++++-------- tmpl/dictionary.html | 2 +- 4 files changed, 30 insertions(+), 13 deletions(-) (limited to 'ext/bg/js') diff --git a/ext/bg/js/options-form.js b/ext/bg/js/options-form.js index f9346d7d..9919031a 100644 --- a/ext/bg/js/options-form.js +++ b/ext/bg/js/options-form.js @@ -91,7 +91,7 @@ function getFormValues() { optsNew.ankiKanjiModel = $('#anki-kanji-model').val(); optsNew.ankiKanjiFields = fieldsToDict($('#kanji .anki-field-value')); - $('.dictionary').each((index, element) => { + $('.dict').each((index, element) => { const dictionary = $(element); const name = dictionary.data('name'); const enableTerms = dictionary.find('.dict-enable-terms').prop('checked'); @@ -163,7 +163,7 @@ function populateAnkiDeckAndModel(opts) { } function populateDictionaries(opts) { - const container = $('.dictionaries'); + const container = $('.dicts'); container.empty(); yomichan().translator.dictionary.getInfo().then(rows => { @@ -181,7 +181,7 @@ function populateDictionaries(opts) { container.append($(html)); }); - container.find('.dictionary input').change(onOptionsChanged); + container.find('.dict input').change(onOptionsChanged); }); } @@ -289,6 +289,23 @@ $(document).ready(() => { $('input, select').not('.anki-model').change(onOptionsChanged); $('.anki-model').change(onAnkiModelChanged); + $('#dict-import a').click(e => { + e.preventDefault(); + const control = $('#dict-import-url'); + const url = $(e.target).data('url'); + if (url.includes('/')) { + control.val(url); + } else { + control.val(chrome.extension.getURL(`bg/data/${url}`)); + } + control.trigger('change'); + }); + + $('#dict-import-url').on('change keyup paste', () => { + const disable = $('#dict-import-url').val().trim().length === 0; + $('#dict-import-start').prop('disabled', disable); + }); + populateDictionaries(opts); populateAnkiDeckAndModel(opts); updateVisibility(opts); diff --git a/ext/bg/js/templates.js b/ext/bg/js/templates.js index fe4d95ca..2caf6ba8 100644 --- a/ext/bg/js/templates.js +++ b/ext/bg/js/templates.js @@ -7,7 +7,7 @@ templates['dictionary.html'] = template({"1":function(container,depth0,helpers,p },"compiler":[7,">= 4.0.0"],"main":function(container,depth0,helpers,partials,data) { var stack1, helper, alias1=depth0 != null ? depth0 : {}, alias2=helpers.helperMissing, alias3="function", alias4=container.escapeExpression; - return "
\n
\n
\n

" + alias4(((helper = (helper = helpers.name || (depth0 != null ? depth0.name : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"name","hash":{},"data":data}) : helper))) diff --git a/ext/bg/options.html b/ext/bg/options.html index bc87d654..800fe8e9 100644 --- a/ext/bg/options.html +++ b/ext/bg/options.html @@ -6,7 +6,7 @@ - - -
- - -

Thank you for downloading this extension! I sincerely hope that it will assist you on your language learning journey.

- -
-

Dictionary Import

- -

- Before it can be used for the first time, Yomichan must import the Japanese dictionary data included with this extension. This process can take a - couple of minutes to finish so please be patient! Please do not completely exit out of your browser until this process completes. -

- -
-
-
- -
Dictionary import complete!
-
- -
-

Quick Guide

- -

- Please read the steps outlined below to get quickly get up and running with Yomichan. For complete documentation, - visit the official homepage. -

- -
    -
  1. Left-click on the icon to enable or disable Yomichan for the current browser instance.
  2. -
  3. Right-click on the icon and select Options to open the Yomichan options page.
  4. -
  5. Hold down Shift (or the middle mouse button) as you hover over text to see term definitions.
  6. -
  7. Resize the definitions window by dragging the bottom-left corner inwards or outwards.
  8. -
  9. Click on Kanji in the definition window to view additional information about that character.
  10. -
-
-
- - - - - diff --git a/ext/bg/js/import.js b/ext/bg/js/import.js deleted file mode 100644 index 0601cb9f..00000000 --- a/ext/bg/js/import.js +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (C) 2016 Alex Yatskov - * Author: Alex Yatskov - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - - -function api_setProgress(progress) { - $('.progress-bar').css('width', `${progress}%`); - - if (progress === 100.0) { - $('.progress').hide(); - $('.alert').show(); - } -} - -chrome.runtime.onMessage.addListener(({action, params}, sender, callback) => { - const method = this['api_' + action]; - if (typeof(method) === 'function') { - method.call(this, params); - } - - callback(); -}); diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index ed8c720b..01213bed 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -25,54 +25,20 @@ class Translator { this.deinflector = new Deinflector(); } - loadData(callback) { + prepare() { if (this.loaded) { return Promise.resolve(); } - return loadJsonInt('bg/data/rules.json').then(rules => { - this.deinflector.setRules(rules); - return loadJsonInt('bg/data/tags.json'); - }).then(tagMeta => { - this.tagMeta = tagMeta; - return this.database.prepare(); - }).then(exists => { - if (exists) { - return; - } - - if (callback) { - callback({state: 'begin', progress: 0}); - } - - const banks = {}; - const bankCallback = (total, loaded, indexUrl) => { - banks[indexUrl] = {loaded, total}; - - let percent = 0.0; - for (const url in banks) { - percent += banks[url].loaded / banks[url].total; - } + const promises = [ + loadJsonInt('bg/data/rules.json'), + loadJsonInt('bg/data/tags.json'), + this.database.prepare() + ]; - percent /= 3.0; - - if (callback) { - callback({state: 'update', progress: Math.ceil(100.0 * percent)}); - } - }; - - return Promise.all([ - this.database.importDictionary(chrome.extension.getURL('bg/data/edict/index.json'), bankCallback), - this.database.importDictionary(chrome.extension.getURL('bg/data/enamdict/index.json'), bankCallback), - this.database.importDictionary(chrome.extension.getURL('bg/data/kanjidic/index.json'), bankCallback), - ]).then(() => { - return this.database.seal(); - }).then(() => { - if (callback) { - callback({state: 'end', progress: 100.0}); - } - }); - }).then(() => { + return Promise.all(promises).then(([rules, tags]) => { + this.deinflector.setRules(rules); + this.tagMeta = tags; this.loaded = true; }); } diff --git a/ext/bg/js/yomichan.js b/ext/bg/js/yomichan.js index d471add4..62b6c7b1 100644 --- a/ext/bg/js/yomichan.js +++ b/ext/bg/js/yomichan.js @@ -25,7 +25,6 @@ class Yomichan { this.translator = new Translator(); this.anki = new AnkiNull(); this.options = null; - this.importTabId = null; this.setState('disabled'); chrome.runtime.onMessage.addListener(this.onMessage.bind(this)); @@ -39,20 +38,6 @@ class Yomichan { }); } - onImport({state, progress}) { - if (state === 'begin') { - chrome.tabs.create({url: chrome.extension.getURL('bg/import.html')}, tab => this.importTabId = tab.id); - } - - if (this.importTabId !== null) { - this.tabInvoke(this.importTabId, 'setProgress', progress); - } - - if (state === 'end') { - this.importTabId = null; - } - } - onMessage(request, sender, callback) { const {action, params} = request, method = this['api_' + action]; @@ -91,7 +76,7 @@ class Yomichan { break; case 'loading': chrome.browserAction.setBadgeText({text: '...'}); - this.translator.loadData(this.onImport.bind(this)).then(() => this.setState('enabled')); + this.translator.prepare().then(this.setState('enabled')); break; } -- cgit v1.2.3 From 2587b9ae6bb9a9b5f4fc5dd17a7f043c17596a06 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 13 Nov 2016 12:49:28 -0800 Subject: Dictionary simplification --- ext/bg/js/database.js | 37 ++----------------------------------- 1 file changed, 2 insertions(+), 35 deletions(-) (limited to 'ext/bg/js') diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index 4c8703c3..6a171dd4 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -20,11 +20,10 @@ class Database { constructor() { this.db = null; - this.dbVer = 6; this.entities = {}; } - init() { + prepare() { if (this.db !== null) { return Promise.reject('database already initialized'); } @@ -35,41 +34,9 @@ class Database { kanji: '++, dictionary, character', entities: '++, dictionary', dictionaries: '++, title, version', - meta: 'name, value', }); - return Promise.resolve(); - } - - prepare() { - this.init(); - - return this.db.meta.get('version').then(row => { - return row ? row.value : 0; - }).catch(() => { - return 0; - }).then(version => { - if (this.dbVer === version) { - return true; - } - - const db = this.db; - this.db.close(); - this.db = null; - - return db.delete().then(() => { - this.init(); - return false; - }); - }); - } - - seal() { - if (this.db === null) { - return Promise.reject('database not initialized'); - } - - return this.db.meta.put({name: 'version', value: this.dbVer}); + return this.db.open(); } findTerm(term, dictionaries) { -- cgit v1.2.3 From a9eea9f63497f2fe829f5b8d1ecf6c58f18b2b79 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 13 Nov 2016 13:29:35 -0800 Subject: WIP --- ext/bg/js/database.js | 4 +-- ext/bg/js/options-form.js | 79 ++++++++++++++++++++++++++--------------------- ext/bg/options.html | 7 ++++- 3 files changed, 52 insertions(+), 38 deletions(-) (limited to 'ext/bg/js') diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index 6a171dd4..23041e40 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -159,7 +159,7 @@ class Database { let termDeleter = Promise.resolve(); if (info.hasTerms) { const termDeleterFunc = () => { - return this.db.terms.where('dictionary').equals(title).limit(1000).delete().then(count => { + return this.db.terms.where('dictionary').equals(title).limit(500).delete().then(count => { if (count === 0) { return Promise.resolve(); } @@ -179,7 +179,7 @@ class Database { let kanjiDeleter = Promise.resolve(); if (info.hasKanji) { const kanjiDeleterFunc = () => { - return this.db.kanji.where('dictionary').equals(title).limit(1000).delete().then(count => { + return this.db.kanji.where('dictionary').equals(title).limit(500).delete().then(count => { if (count === 0) { return Promise.resolve(); } diff --git a/ext/bg/js/options-form.js b/ext/bg/js/options-form.js index 736ec023..2cabe162 100644 --- a/ext/bg/js/options-form.js +++ b/ext/bg/js/options-form.js @@ -115,40 +115,6 @@ function updateVisibility(opts) { } } -function populateDictionaries(opts) { - const dictGroups = $('#dict-groups'); - dictGroups.empty(); - - const dictError = $('#dict-error'); - dictError.hide(); - - const dictSpinner = $('#dict-spinner'); - dictSpinner.show(); - - return database().getDictionaries().then(rows => { - rows.forEach(row => { - const dictOpts = opts.dictionaries[row.title] || {enableTerms: false, enableKanji: false}; - const html = Handlebars.templates['dictionary.html']({ - title: row.title, - version: row.version, - hasTerms: row.hasTerms, - hasKanji: row.hasKanji, - enableTerms: dictOpts.enableTerms, - enableKanji: dictOpts.enableKanji - }); - - dictGroups.append($(html)); - }); - - $('.dict-enable-terms, .dict-enable-kanji').change(onOptionsChanged); - $('.dict-delete').click(onDictionaryDelete); - }).catch(error => { - dictError.show().find('span').text(error); - }).then(() => { - dictSpinner.hide(); - }); -} - function onDictionaryDelete() { const dictGroup = $(this).closest('.dict-group'); @@ -199,7 +165,7 @@ function onDictionaryImport() { const dictUrl = $('#dict-url'); loadOptions().then(opts => { database().importDictionary(dictUrl.val(), callback).then(summary => { - opts.dictionaries[summary.title] = {hasTerms: summary.hasTerms, hasKanji: summary.hasKanji}; + opts.dictionaries[summary.title] = {enableTerms: summary.hasTerms, enableKanji: summary.hasKanji}; return saveOptions(opts); }).then(() => { return populateDictionaries(opts); @@ -302,6 +268,49 @@ function populateAnkiFields(element, opts) { }); } +function populateDictionaries(opts) { + const dictGroups = $('#dict-groups'); + dictGroups.empty(); + + const dictError = $('#dict-error'); + dictError.hide(); + + const dictWarning = $('#dict-warning'); + dictWarning.hide(); + + const dictSpinner = $('#dict-spinner'); + dictSpinner.show(); + + let dictCount = 0; + return database().getDictionaries().then(rows => { + rows.forEach(row => { + const dictOpts = opts.dictionaries[row.title] || {enableTerms: false, enableKanji: false}; + const html = Handlebars.templates['dictionary.html']({ + title: row.title, + version: row.version, + hasTerms: row.hasTerms, + hasKanji: row.hasKanji, + enableTerms: dictOpts.enableTerms, + enableKanji: dictOpts.enableKanji + }); + + dictGroups.append($(html)); + ++dictCount; + }); + + $('.dict-enable-terms, .dict-enable-kanji').change(onOptionsChanged); + $('.dict-delete').click(onDictionaryDelete); + }).catch(error => { + dictError.show().find('span').text(error); + }).then(() => { + if (dictCount === 0) { + dictWarning.show(); + } + + dictSpinner.hide(); + }); +} + function onOptionsChanged(e) { if (!e.originalEvent && !e.isTrigger) { return; diff --git a/ext/bg/options.html b/ext/bg/options.html index 4f74a58e..5d949bc2 100644 --- a/ext/bg/options.html +++ b/ext/bg/options.html @@ -7,7 +7,7 @@