From 371c07ab1fb6125c065589d71c93477e0456aada Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sat, 12 Nov 2016 19:34:02 -0800 Subject: Revert "Delete dictionary deletion" This reverts commit 0ff41d5843b3bf39994ae32e4ab623bfe0e4e3ca. --- ext/bg/js/database.js | 73 +++++++++++++++++++++++++++++++++++++++++++++++ ext/bg/js/options-form.js | 33 ++++++++++++++++++++- ext/bg/js/templates.js | 6 ++-- ext/bg/options.html | 6 ++-- 4 files changed, 111 insertions(+), 7 deletions(-) (limited to 'ext/bg') diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index 7f4c6ac6..06786881 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -148,6 +148,79 @@ class Database { return this.db.dictionaries.toArray(); } + deleteDictionary(title, callback) { + if (this.db === null) { + return Promise.reject('database not initialized'); + } + + return this.db.dictionaries.where('title').equals(title).first(info => { + if (!info) { + return; + } + + let termCounter = Promise.resolve(0); + if (info.hasTerms) { + termCounter = this.db.terms.where('dictionary').equals(title).count(); + } + + let kanjiCounter = Promise.resolve(0); + if (info.hasKanji) { + kanjiCounter = this.db.kanji.where('dictionary').equals(title).count(); + } + + return Promise.all([termCounter, kanjiCounter]).then(([termCount, kanjiCount]) => { + const totalCount = termCount + kanjiCount; + let deletedCount = 0; + + let termDeleter = Promise.resolve(); + if (info.hasTerms) { + const termDeleterFunc = () => { + return this.db.terms.where('dictionary').equals(title).limit(1000).delete().then(count => { + if (count === 0) { + return Promise.resolve(); + } + + deletedCount += count; + if (callback) { + callback(totalCount, deletedCount); + } + + return termDeleterFunc(); + }); + }; + + termDeleter = termDeleterFunc(); + } + + let kanjiDeleter = Promise.resolve(); + if (info.hasKanji) { + const kanjiDeleterFunc = () => { + return this.db.kanji.where('dictionary').equals(title).limit(1000).delete().then(count => { + if (count === 0) { + return Promise.resolve(); + } + + deletedCount += count; + if (callback) { + callback(totalCount, deletedCount); + } + + return kanjiDeleterFunc(); + }); + }; + + kanjiDeleter = kanjiDeleterFunc(); + } + + return Promise.all([termDeleter, kanjiDeleter]); + }); + }).then(() => { + return this.db.entities.where('dictionary').equals(title).delete(); + }).then(() => { + return this.db.dictionaries.where('title').equals(title).delete(); + }); + } + importDictionary(indexUrl, callback) { if (this.db === null) { return Promise.reject('database not initialized'); diff --git a/ext/bg/js/options-form.js b/ext/bg/js/options-form.js index 3d1530ad..3a201bc5 100644 --- a/ext/bg/js/options-form.js +++ b/ext/bg/js/options-form.js @@ -141,6 +141,7 @@ function populateDictionaries(opts) { }); $('.dict-enable-terms, .dict-enable-kanji').change(onOptionsChanged); + $('.dict-delete').click(onDictionaryDelete); }).catch(error => { dictError.show().find('span').text(error); }).then(() => { @@ -148,6 +149,36 @@ function populateDictionaries(opts) { }); } +function onDictionaryDelete() { + const dictGroup = $(this).closest('.dict-group'); + + const dictError = $('#dict-error'); + dictError.hide(); + + const dictSpinner = $('#dict-spinner'); + dictSpinner.show(); + + const dictProgress = dictGroup.find('.dict-delete-progress'); + dictProgress.show(); + + const dictControls = dictGroup.find('.dict-controls'); + dictControls.hide(); + + const callback = (total, current) => { + dictProgress.find('div').css('width', `${current / total * 100.0}%`); + }; + + database().deleteDictionary(dictGroup.data('title'), callback).then(() => { + dictGroup.slideUp(); + }).catch(error => { + dictError.show().find('span').text(error); + }).then(() => { + dictSpinner.hide(); + dictProgress.hide(); + dictControls.show(); + }); +} + function onDictionaryImport() { const dictImport = $(this); dictImport.prop('disabled', true); @@ -155,7 +186,7 @@ function onDictionaryImport() { const dictError = $('#dict-error'); dictError.hide(); - const dictProgress = $('.dict-progress'); + const dictProgress = $('.dict-import-progress'); dictProgress.show(); const dictSpinner = $('#dict-spinner'); diff --git a/ext/bg/js/templates.js b/ext/bg/js/templates.js index 988533fb..f80824f9 100644 --- a/ext/bg/js/templates.js +++ b/ext/bg/js/templates.js @@ -9,17 +9,17 @@ templates['dictionary.html'] = template({"1":function(container,depth0,helpers,p return "
\n

" + + "\">\n
\n
\n

" + 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
Dictionary data is being deleted, please be patient...
\n
\n
\n
\n
\n \n
\n
Enable term search\n
\n
\n