2016-04-04 03:05:22 +00:00
/ *
2017-08-15 04:43:09 +00:00
* Copyright ( C ) 2016 - 2017 Alex Yatskov < alex @ foosoft . net >
2016-04-04 03:05:22 +00:00
* Author : Alex Yatskov < alex @ foosoft . net >
*
* 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 < http : //www.gnu.org/licenses/>.
* /
2017-07-17 02:29:43 +00:00
async function formRead ( ) {
const optionsOld = await optionsLoad ( ) ;
const optionsNew = $ . extend ( true , { } , optionsOld ) ;
optionsNew . general . showGuide = $ ( '#show-usage-guide' ) . prop ( 'checked' ) ;
2017-10-12 06:59:09 +00:00
optionsNew . general . compactTags = $ ( '#compact-tags' ) . prop ( 'checked' ) ;
2017-10-15 06:45:00 +00:00
optionsNew . general . compactGlossaries = $ ( '#compact-glossaries' ) . prop ( 'checked' ) ;
2017-12-16 18:56:53 +00:00
optionsNew . general . autoPlayAudio = $ ( '#auto-play-audio' ) . prop ( 'checked' ) ;
2017-09-29 02:41:29 +00:00
optionsNew . general . resultOutputMode = $ ( '#result-output-mode' ) . val ( ) ;
2017-07-17 02:29:43 +00:00
optionsNew . general . audioSource = $ ( '#audio-playback-source' ) . val ( ) ;
optionsNew . general . audioVolume = parseFloat ( $ ( '#audio-playback-volume' ) . val ( ) ) ;
optionsNew . general . debugInfo = $ ( '#show-debug-info' ) . prop ( 'checked' ) ;
optionsNew . general . showAdvanced = $ ( '#show-advanced-options' ) . prop ( 'checked' ) ;
optionsNew . general . maxResults = parseInt ( $ ( '#max-displayed-results' ) . val ( ) , 10 ) ;
2019-02-15 02:42:59 +00:00
optionsNew . general . popupDisplayMode = $ ( '#popup-display-mode' ) . val ( ) ;
2017-07-17 02:29:43 +00:00
optionsNew . general . popupWidth = parseInt ( $ ( '#popup-width' ) . val ( ) , 10 ) ;
optionsNew . general . popupHeight = parseInt ( $ ( '#popup-height' ) . val ( ) , 10 ) ;
2018-10-02 14:27:59 +00:00
optionsNew . general . popupHorizontalOffset = parseInt ( $ ( '#popup-horizontal-offset' ) . val ( ) , 0 ) ;
optionsNew . general . popupVerticalOffset = parseInt ( $ ( '#popup-vertical-offset' ) . val ( ) , 10 ) ;
2019-07-09 21:52:44 +00:00
optionsNew . general . customPopupCss = $ ( '#custom-popup-css' ) . val ( ) ;
2017-07-17 02:29:43 +00:00
optionsNew . scanning . middleMouse = $ ( '#middle-mouse-button-scan' ) . prop ( 'checked' ) ;
2019-02-11 01:44:16 +00:00
optionsNew . scanning . touchInputEnabled = $ ( '#touch-input-enabled' ) . prop ( 'checked' ) ;
2017-07-17 02:29:43 +00:00
optionsNew . scanning . selectText = $ ( '#select-matched-text' ) . prop ( 'checked' ) ;
optionsNew . scanning . alphanumeric = $ ( '#search-alphanumeric' ) . prop ( 'checked' ) ;
2017-09-17 06:08:43 +00:00
optionsNew . scanning . autoHideResults = $ ( '#auto-hide-results' ) . prop ( 'checked' ) ;
2017-07-17 02:29:43 +00:00
optionsNew . scanning . delay = parseInt ( $ ( '#scan-delay' ) . val ( ) , 10 ) ;
optionsNew . scanning . length = parseInt ( $ ( '#scan-length' ) . val ( ) , 10 ) ;
optionsNew . scanning . modifier = $ ( '#scan-modifier-key' ) . val ( ) ;
optionsNew . anki . enable = $ ( '#anki-enable' ) . prop ( 'checked' ) ;
optionsNew . anki . tags = $ ( '#card-tags' ) . val ( ) . split ( /[,; ]+/ ) ;
optionsNew . anki . sentenceExt = parseInt ( $ ( '#sentence-detection-extent' ) . val ( ) , 10 ) ;
optionsNew . anki . server = $ ( '#interface-server' ) . val ( ) ;
2019-08-15 23:56:14 +00:00
optionsNew . anki . screenshot . format = $ ( '#screenshot-format' ) . val ( ) ;
optionsNew . anki . screenshot . quality = parseInt ( $ ( '#screenshot-quality' ) . val ( ) , 10 ) ;
2017-09-06 20:18:06 +00:00
optionsNew . anki . fieldTemplates = $ ( '#field-templates' ) . val ( ) ;
2017-07-17 02:29:43 +00:00
2017-07-17 02:55:33 +00:00
if ( optionsOld . anki . enable && ! ankiErrorShown ( ) ) {
2017-07-17 02:29:43 +00:00
optionsNew . anki . terms . deck = $ ( '#anki-terms-deck' ) . val ( ) ;
optionsNew . anki . terms . model = $ ( '#anki-terms-model' ) . val ( ) ;
optionsNew . anki . terms . fields = ankiFieldsToDict ( $ ( '#terms .anki-field-value' ) ) ;
optionsNew . anki . kanji . deck = $ ( '#anki-kanji-deck' ) . val ( ) ;
optionsNew . anki . kanji . model = $ ( '#anki-kanji-model' ) . val ( ) ;
optionsNew . anki . kanji . fields = ankiFieldsToDict ( $ ( '#kanji .anki-field-value' ) ) ;
}
2017-03-03 05:01:49 +00:00
2017-10-29 17:18:15 +00:00
optionsNew . general . mainDictionary = $ ( '#dict-main' ) . val ( ) ;
2017-07-17 02:29:43 +00:00
$ ( '.dict-group' ) . each ( ( index , element ) => {
const dictionary = $ ( element ) ;
2017-10-29 19:20:56 +00:00
optionsNew . dictionaries [ dictionary . data ( 'title' ) ] = {
priority : parseInt ( dictionary . find ( '.dict-priority' ) . val ( ) , 10 ) ,
enabled : dictionary . find ( '.dict-enabled' ) . prop ( 'checked' ) ,
allowSecondarySearches : dictionary . find ( '.dict-allow-secondary-searches' ) . prop ( 'checked' )
} ;
2017-03-03 05:01:49 +00:00
} ) ;
2017-07-17 02:29:43 +00:00
return { optionsNew , optionsOld } ;
2017-03-03 05:01:49 +00:00
}
2017-07-17 02:55:33 +00:00
function formUpdateVisibility ( options ) {
2017-03-03 05:01:49 +00:00
const general = $ ( '#anki-general' ) ;
if ( options . anki . enable ) {
general . show ( ) ;
} else {
general . hide ( ) ;
}
const advanced = $ ( '.options-advanced' ) ;
if ( options . general . showAdvanced ) {
advanced . show ( ) ;
} else {
advanced . hide ( ) ;
}
2017-05-25 02:13:56 +00:00
2017-10-29 17:18:15 +00:00
const mainGroup = $ ( '#dict-main-group' ) ;
2017-10-24 13:23:13 +00:00
if ( options . general . resultOutputMode === 'merge' ) {
2017-10-29 17:18:15 +00:00
mainGroup . show ( ) ;
2017-10-24 13:23:13 +00:00
} else {
2017-10-29 17:18:15 +00:00
mainGroup . hide ( ) ;
2017-10-24 13:23:13 +00:00
}
2017-05-25 02:13:56 +00:00
const debug = $ ( '#debug' ) ;
if ( options . general . debugInfo ) {
2017-09-13 03:20:03 +00:00
const temp = utilIsolate ( options ) ;
temp . anki . fieldTemplates = '...' ;
const text = JSON . stringify ( temp , null , 4 ) ;
2017-05-25 02:13:56 +00:00
debug . html ( handlebarsEscape ( text ) ) ;
debug . show ( ) ;
} else {
debug . hide ( ) ;
}
2017-03-03 05:01:49 +00:00
}
2017-10-24 13:23:13 +00:00
async function formMainDictionaryOptionsPopulate ( options ) {
2017-10-29 17:18:15 +00:00
const select = $ ( '#dict-main' ) . empty ( ) ;
select . append ( $ ( '<option class="text-muted" value="">Not selected</option>' ) ) ;
2017-10-24 13:23:13 +00:00
2017-10-29 17:18:15 +00:00
let mainDictionary = '' ;
2017-10-29 17:42:39 +00:00
for ( const dictRow of await utilDatabaseSummarize ( ) ) {
2017-10-29 17:59:50 +00:00
if ( dictRow . sequenced ) {
2017-10-29 17:42:39 +00:00
select . append ( $ ( ` <option value=" ${ dictRow . title } "> ${ dictRow . title } </option> ` ) ) ;
if ( dictRow . title === options . general . mainDictionary ) {
mainDictionary = dictRow . title ;
}
2017-10-24 13:23:13 +00:00
}
}
2017-10-29 17:18:15 +00:00
select . val ( mainDictionary ) ;
2017-10-24 13:23:13 +00:00
}
2017-08-15 04:43:09 +00:00
async function onFormOptionsChanged ( e ) {
2017-10-29 17:18:15 +00:00
if ( ! e . originalEvent && ! e . isTrigger ) {
return ;
}
2017-03-05 23:54:03 +00:00
2017-10-29 17:18:15 +00:00
const { optionsNew , optionsOld } = await formRead ( ) ;
await optionsSave ( optionsNew ) ;
formUpdateVisibility ( optionsNew ) ;
2017-03-05 23:54:03 +00:00
2017-10-29 17:18:15 +00:00
try {
2017-07-17 02:29:43 +00:00
const ankiUpdated =
optionsNew . anki . enable !== optionsOld . anki . enable ||
optionsNew . anki . server !== optionsOld . anki . server ;
if ( ankiUpdated ) {
2017-08-16 03:58:50 +00:00
ankiSpinnerShow ( true ) ;
2017-07-17 02:29:43 +00:00
await ankiDeckAndModelPopulate ( optionsNew ) ;
2017-08-16 03:58:50 +00:00
ankiErrorShow ( ) ;
2017-07-17 02:29:43 +00:00
}
} catch ( e ) {
ankiErrorShow ( e ) ;
} finally {
ankiSpinnerShow ( false ) ;
}
2017-08-15 04:43:09 +00:00
}
2017-07-17 02:29:43 +00:00
2017-08-15 04:43:09 +00:00
async function onReady ( ) {
2017-07-17 02:29:43 +00:00
const options = await optionsLoad ( ) ;
$ ( '#show-usage-guide' ) . prop ( 'checked' , options . general . showGuide ) ;
2017-10-12 06:59:09 +00:00
$ ( '#compact-tags' ) . prop ( 'checked' , options . general . compactTags ) ;
2017-10-15 06:45:00 +00:00
$ ( '#compact-glossaries' ) . prop ( 'checked' , options . general . compactGlossaries ) ;
2017-12-16 18:56:53 +00:00
$ ( '#auto-play-audio' ) . prop ( 'checked' , options . general . autoPlayAudio ) ;
2017-09-29 02:41:29 +00:00
$ ( '#result-output-mode' ) . val ( options . general . resultOutputMode ) ;
2017-07-17 02:29:43 +00:00
$ ( '#audio-playback-source' ) . val ( options . general . audioSource ) ;
$ ( '#audio-playback-volume' ) . val ( options . general . audioVolume ) ;
$ ( '#show-debug-info' ) . prop ( 'checked' , options . general . debugInfo ) ;
$ ( '#show-advanced-options' ) . prop ( 'checked' , options . general . showAdvanced ) ;
$ ( '#max-displayed-results' ) . val ( options . general . maxResults ) ;
2019-02-15 02:42:59 +00:00
$ ( '#popup-display-mode' ) . val ( options . general . popupDisplayMode ) ;
2017-07-17 02:29:43 +00:00
$ ( '#popup-width' ) . val ( options . general . popupWidth ) ;
$ ( '#popup-height' ) . val ( options . general . popupHeight ) ;
2018-10-02 14:27:59 +00:00
$ ( '#popup-horizontal-offset' ) . val ( options . general . popupHorizontalOffset ) ;
$ ( '#popup-vertical-offset' ) . val ( options . general . popupVerticalOffset ) ;
2019-07-09 21:52:44 +00:00
$ ( '#custom-popup-css' ) . val ( options . general . customPopupCss ) ;
2017-07-17 02:29:43 +00:00
$ ( '#middle-mouse-button-scan' ) . prop ( 'checked' , options . scanning . middleMouse ) ;
2019-02-11 01:44:16 +00:00
$ ( '#touch-input-enabled' ) . prop ( 'checked' , options . scanning . touchInputEnabled ) ;
2017-07-17 02:29:43 +00:00
$ ( '#select-matched-text' ) . prop ( 'checked' , options . scanning . selectText ) ;
$ ( '#search-alphanumeric' ) . prop ( 'checked' , options . scanning . alphanumeric ) ;
2017-09-17 06:08:43 +00:00
$ ( '#auto-hide-results' ) . prop ( 'checked' , options . scanning . autoHideResults ) ;
2017-07-17 02:29:43 +00:00
$ ( '#scan-delay' ) . val ( options . scanning . delay ) ;
$ ( '#scan-length' ) . val ( options . scanning . length ) ;
$ ( '#scan-modifier-key' ) . val ( options . scanning . modifier ) ;
2017-09-23 02:39:05 +00:00
$ ( '#dict-purge-link' ) . click ( utilAsync ( onDictionaryPurge ) ) ;
2017-08-15 04:43:09 +00:00
$ ( '#dict-file' ) . change ( utilAsync ( onDictionaryImport ) ) ;
2017-07-17 02:29:43 +00:00
$ ( '#anki-enable' ) . prop ( 'checked' , options . anki . enable ) ;
$ ( '#card-tags' ) . val ( options . anki . tags . join ( ' ' ) ) ;
$ ( '#sentence-detection-extent' ) . val ( options . anki . sentenceExt ) ;
$ ( '#interface-server' ) . val ( options . anki . server ) ;
2019-08-15 23:56:14 +00:00
$ ( '#screenshot-format' ) . val ( options . anki . screenshot . format ) ;
$ ( '#screenshot-quality' ) . val ( options . anki . screenshot . quality ) ;
2017-09-06 20:18:06 +00:00
$ ( '#field-templates' ) . val ( options . anki . fieldTemplates ) ;
$ ( '#field-templates-reset' ) . click ( utilAsync ( onAnkiFieldTemplatesReset ) ) ;
$ ( 'input, select, textarea' ) . not ( '.anki-model' ) . change ( utilAsync ( onFormOptionsChanged ) ) ;
2017-08-15 04:43:09 +00:00
$ ( '.anki-model' ) . change ( utilAsync ( onAnkiModelChanged ) ) ;
2017-07-17 02:29:43 +00:00
try {
await dictionaryGroupsPopulate ( options ) ;
2017-10-29 17:18:15 +00:00
await formMainDictionaryOptionsPopulate ( options ) ;
2017-07-17 02:29:43 +00:00
} catch ( e ) {
2019-02-27 02:01:32 +00:00
dictionaryErrorsShow ( [ e ] ) ;
2017-07-17 02:29:43 +00:00
}
try {
await ankiDeckAndModelPopulate ( options ) ;
} catch ( e ) {
ankiErrorShow ( e ) ;
}
2017-07-17 02:55:33 +00:00
formUpdateVisibility ( options ) ;
2019-05-04 16:57:55 +00:00
storageInfoInitialize ( ) ;
2017-08-15 04:43:09 +00:00
}
2017-07-17 02:29:43 +00:00
2017-08-15 04:43:09 +00:00
$ ( document ) . ready ( utilAsync ( onReady ) ) ;
2017-03-03 05:01:49 +00:00
/ *
* Dictionary
* /
2019-02-27 02:01:32 +00:00
function dictionaryErrorToString ( error ) {
if ( error . toString ) {
error = error . toString ( ) ;
} else {
error = ` ${ error } ` ;
}
for ( const [ match , subst ] of dictionaryErrorToString . overrides ) {
if ( error . includes ( match ) ) {
error = subst ;
break ;
}
}
return error ;
}
dictionaryErrorToString . overrides = [
[
'A mutation operation was attempted on a database that did not allow mutations.' ,
'Access to IndexedDB appears to be restricted. Firefox seems to require that the history preference is set to "Remember history" before IndexedDB use of any kind is allowed.'
] ,
[
'The operation failed for reasons unrelated to the database itself and not covered by any other error code.' ,
'Unable to access IndexedDB due to a possibly corrupt user profile. Try using the "Refresh Firefox" feature to reset your user profile.'
] ,
[
'BulkError' ,
'Unable to finish importing dictionary data into IndexedDB. This may indicate that you do not have sufficient disk space available to complete this operation.'
]
] ;
function dictionaryErrorsShow ( errors ) {
2017-03-03 05:01:49 +00:00
const dialog = $ ( '#dict-error' ) ;
2019-02-27 02:01:32 +00:00
dialog . show ( ) . text ( '' ) ;
if ( errors !== null && errors . length > 0 ) {
const uniqueErrors = { } ;
for ( let e of errors ) {
e = dictionaryErrorToString ( e ) ;
uniqueErrors [ e ] = uniqueErrors . hasOwnProperty ( e ) ? uniqueErrors [ e ] + 1 : 1 ;
2017-09-23 03:38:23 +00:00
}
2019-02-27 02:01:32 +00:00
for ( const e in uniqueErrors ) {
const count = uniqueErrors [ e ] ;
const div = document . createElement ( 'p' ) ;
if ( count > 1 ) {
div . textContent = ` ${ e } ` ;
const em = document . createElement ( 'em' ) ;
em . textContent = ` ( ${ count } ) ` ;
div . appendChild ( em ) ;
} else {
div . textContent = ` ${ e } ` ;
2017-09-23 03:38:23 +00:00
}
2019-02-27 02:01:32 +00:00
dialog . append ( $ ( div ) ) ;
2017-09-23 03:38:23 +00:00
}
2019-02-27 02:01:32 +00:00
dialog . show ( ) ;
2017-03-03 05:01:49 +00:00
} else {
dialog . hide ( ) ;
}
}
2017-03-05 23:54:03 +00:00
function dictionarySpinnerShow ( show ) {
2017-03-03 05:01:49 +00:00
const spinner = $ ( '#dict-spinner' ) ;
if ( show ) {
spinner . show ( ) ;
} else {
spinner . hide ( ) ;
}
}
2017-03-05 23:54:03 +00:00
function dictionaryGroupsSort ( ) {
const dictGroups = $ ( '#dict-groups' ) ;
const dictGroupChildren = dictGroups . children ( '.dict-group' ) . sort ( ( ca , cb ) => {
const pa = parseInt ( $ ( ca ) . find ( '.dict-priority' ) . val ( ) , 10 ) ;
const pb = parseInt ( $ ( cb ) . find ( '.dict-priority' ) . val ( ) , 10 ) ;
if ( pa < pb ) {
return 1 ;
} else if ( pa > pb ) {
return - 1 ;
} else {
return 0 ;
}
} ) ;
dictGroups . append ( dictGroupChildren ) ;
}
2017-07-17 02:29:43 +00:00
async function dictionaryGroupsPopulate ( options ) {
2017-03-03 05:01:49 +00:00
const dictGroups = $ ( '#dict-groups' ) . empty ( ) ;
const dictWarning = $ ( '#dict-warning' ) . hide ( ) ;
2017-10-29 17:42:39 +00:00
const dictRows = await utilDatabaseSummarize ( ) ;
2017-07-17 02:29:43 +00:00
if ( dictRows . length === 0 ) {
dictWarning . show ( ) ;
}
2017-03-05 23:54:03 +00:00
2017-07-17 02:29:43 +00:00
for ( const dictRow of dictRowsSort ( dictRows , options ) ) {
2017-10-29 19:20:56 +00:00
const dictOptions = options . dictionaries [ dictRow . title ] || {
enabled : false ,
priority : 0 ,
allowSecondarySearches : false
} ;
2017-09-09 19:59:49 +00:00
const dictHtml = await apiTemplateRender ( 'dictionary.html' , {
2017-10-29 19:20:56 +00:00
enabled : dictOptions . enabled ,
priority : dictOptions . priority ,
allowSecondarySearches : dictOptions . allowSecondarySearches ,
2017-07-17 02:29:43 +00:00
title : dictRow . title ,
version : dictRow . version ,
revision : dictRow . revision ,
2017-10-29 19:20:56 +00:00
outdated : dictRow . version < 3
2017-07-17 02:29:43 +00:00
} ) ;
2017-03-03 05:01:49 +00:00
2017-07-17 02:29:43 +00:00
dictGroups . append ( $ ( dictHtml ) ) ;
}
2017-03-03 05:01:49 +00:00
2017-07-17 02:55:33 +00:00
formUpdateVisibility ( options ) ;
2017-07-17 02:29:43 +00:00
2017-10-15 02:19:16 +00:00
$ ( '.dict-enabled, .dict-priority, .dict-allow-secondary-searches' ) . change ( e => {
2017-07-17 02:29:43 +00:00
dictionaryGroupsSort ( ) ;
2017-07-17 02:55:33 +00:00
onFormOptionsChanged ( e ) ;
2017-07-17 02:29:43 +00:00
} ) ;
2017-03-03 05:01:49 +00:00
}
2017-08-15 04:43:09 +00:00
async function onDictionaryPurge ( e ) {
2017-03-03 05:01:49 +00:00
e . preventDefault ( ) ;
2017-10-29 17:18:15 +00:00
const dictControls = $ ( '#dict-importer, #dict-groups, #dict-main-group' ) . hide ( ) ;
2017-09-23 02:39:05 +00:00
const dictProgress = $ ( '#dict-purge' ) . show ( ) ;
2017-03-03 05:01:49 +00:00
2017-07-17 02:29:43 +00:00
try {
2019-02-27 02:01:32 +00:00
dictionaryErrorsShow ( null ) ;
2017-07-17 02:29:43 +00:00
dictionarySpinnerShow ( true ) ;
2017-07-28 04:42:14 +00:00
await utilDatabasePurge ( ) ;
2017-07-17 02:29:43 +00:00
const options = await optionsLoad ( ) ;
options . dictionaries = { } ;
2017-10-24 13:23:13 +00:00
options . general . mainDictionary = '' ;
2017-07-17 02:29:43 +00:00
await optionsSave ( options ) ;
await dictionaryGroupsPopulate ( options ) ;
2017-10-24 13:23:13 +00:00
await formMainDictionaryOptionsPopulate ( options ) ;
2017-07-17 02:29:43 +00:00
} catch ( e ) {
2019-02-27 02:01:32 +00:00
dictionaryErrorsShow ( [ e ] ) ;
2017-07-17 02:29:43 +00:00
} finally {
2017-03-05 23:54:03 +00:00
dictionarySpinnerShow ( false ) ;
2017-07-17 02:29:43 +00:00
2017-03-03 05:01:49 +00:00
dictControls . show ( ) ;
dictProgress . hide ( ) ;
2019-02-27 02:03:34 +00:00
if ( storageEstimate . mostRecent !== null ) {
storageUpdateStats ( ) ;
}
2017-07-17 02:29:43 +00:00
}
2017-08-15 04:43:09 +00:00
}
2017-06-25 22:36:28 +00:00
2017-08-15 04:43:09 +00:00
async function onDictionaryImport ( e ) {
2017-06-25 22:43:29 +00:00
const dictFile = $ ( '#dict-file' ) ;
2017-07-17 02:29:43 +00:00
const dictControls = $ ( '#dict-importer' ) . hide ( ) ;
2017-06-25 22:36:28 +00:00
const dictProgress = $ ( '#dict-import-progress' ) . show ( ) ;
2017-07-17 02:29:43 +00:00
try {
2019-02-27 02:01:32 +00:00
dictionaryErrorsShow ( null ) ;
2017-07-17 02:29:43 +00:00
dictionarySpinnerShow ( true ) ;
2017-06-25 22:36:28 +00:00
2017-07-17 02:29:43 +00:00
const setProgress = percent => dictProgress . find ( '.progress-bar' ) . css ( 'width' , ` ${ percent } % ` ) ;
2019-02-27 02:03:34 +00:00
const updateProgress = ( total , current ) => {
setProgress ( current / total * 100.0 ) ;
if ( storageEstimate . mostRecent !== null && ! storageUpdateStats . isUpdating ) {
storageUpdateStats ( ) ;
}
} ;
2017-07-17 02:29:43 +00:00
setProgress ( 0.0 ) ;
2019-02-27 02:01:32 +00:00
const exceptions = [ ] ;
2017-07-17 02:29:43 +00:00
const options = await optionsLoad ( ) ;
2019-02-27 02:01:32 +00:00
const summary = await utilDatabaseImport ( e . target . files [ 0 ] , updateProgress , exceptions ) ;
2017-10-24 13:23:13 +00:00
options . dictionaries [ summary . title ] = { enabled : true , priority : 0 , allowSecondarySearches : false } ;
2017-10-29 17:59:50 +00:00
if ( summary . sequenced && options . general . mainDictionary === '' ) {
2017-10-24 13:23:13 +00:00
options . general . mainDictionary = summary . title ;
}
2019-02-27 02:01:32 +00:00
if ( exceptions . length > 0 ) {
exceptions . push ( ` Dictionary may not have been imported properly: ${ exceptions . length } error ${ exceptions . length === 1 ? '' : 's' } reported. ` ) ;
dictionaryErrorsShow ( exceptions ) ;
}
2017-07-17 02:29:43 +00:00
await optionsSave ( options ) ;
await dictionaryGroupsPopulate ( options ) ;
2017-10-24 13:23:13 +00:00
await formMainDictionaryOptionsPopulate ( options ) ;
2017-07-17 02:29:43 +00:00
} catch ( e ) {
2019-02-27 02:01:32 +00:00
dictionaryErrorsShow ( [ e ] ) ;
2017-07-17 02:29:43 +00:00
} finally {
2017-06-25 22:36:28 +00:00
dictionarySpinnerShow ( false ) ;
2017-07-17 02:29:43 +00:00
dictFile . val ( '' ) ;
dictControls . show ( ) ;
2017-06-25 22:36:28 +00:00
dictProgress . hide ( ) ;
2017-07-17 02:29:43 +00:00
}
2017-08-15 04:43:09 +00:00
}
2017-03-03 05:01:49 +00:00
2017-07-17 06:56:36 +00:00
2017-03-03 05:01:49 +00:00
/ *
* Anki
* /
2017-03-05 23:54:03 +00:00
function ankiSpinnerShow ( show ) {
2017-03-03 05:01:49 +00:00
const spinner = $ ( '#anki-spinner' ) ;
if ( show ) {
spinner . show ( ) ;
} else {
spinner . hide ( ) ;
}
}
2017-03-05 23:54:03 +00:00
function ankiErrorShow ( error ) {
2017-03-03 05:01:49 +00:00
const dialog = $ ( '#anki-error' ) ;
if ( error ) {
2017-09-23 02:39:05 +00:00
dialog . show ( ) . text ( error ) ;
2017-03-03 05:01:49 +00:00
}
else {
dialog . hide ( ) ;
2016-04-04 03:05:22 +00:00
}
2017-03-03 05:01:49 +00:00
}
2017-07-17 02:55:33 +00:00
function ankiErrorShown ( ) {
return $ ( '#anki-error' ) . is ( ':visible' ) ;
}
2017-03-03 05:01:49 +00:00
function ankiFieldsToDict ( selection ) {
const result = { } ;
selection . each ( ( index , element ) => {
result [ $ ( element ) . data ( 'field' ) ] = $ ( element ) . val ( ) ;
} ) ;
2016-04-04 03:05:22 +00:00
2017-03-03 05:01:49 +00:00
return result ;
2016-04-04 03:05:22 +00:00
}
2017-07-17 02:29:43 +00:00
async function ankiDeckAndModelPopulate ( options ) {
2017-03-03 05:01:49 +00:00
const ankiFormat = $ ( '#anki-format' ) . hide ( ) ;
2017-07-28 04:42:14 +00:00
const deckNames = await utilAnkiGetDeckNames ( ) ;
2017-07-17 02:29:43 +00:00
const ankiDeck = $ ( '.anki-deck' ) ;
ankiDeck . find ( 'option' ) . remove ( ) ;
deckNames . sort ( ) . forEach ( name => ankiDeck . append ( $ ( '<option/>' , { value : name , text : name } ) ) ) ;
2017-07-28 04:42:14 +00:00
const modelNames = await utilAnkiGetModelNames ( ) ;
2017-07-17 02:29:43 +00:00
const ankiModel = $ ( '.anki-model' ) ;
ankiModel . find ( 'option' ) . remove ( ) ;
modelNames . sort ( ) . forEach ( name => ankiModel . append ( $ ( '<option/>' , { value : name , text : name } ) ) ) ;
2017-03-03 05:01:49 +00:00
2017-08-19 18:23:19 +00:00
$ ( '#anki-terms-deck' ) . val ( options . anki . terms . deck ) ;
await ankiFieldsPopulate ( $ ( '#anki-terms-model' ) . val ( options . anki . terms . model ) , options ) ;
$ ( '#anki-kanji-deck' ) . val ( options . anki . kanji . deck ) ;
await ankiFieldsPopulate ( $ ( '#anki-kanji-model' ) . val ( options . anki . kanji . model ) , options ) ;
2017-07-17 02:29:43 +00:00
ankiFormat . show ( ) ;
}
async function ankiFieldsPopulate ( element , options ) {
2017-03-03 05:01:49 +00:00
const modelName = element . val ( ) ;
2017-07-09 22:23:11 +00:00
if ( ! modelName ) {
2017-07-17 02:29:43 +00:00
return ;
2017-03-03 05:01:49 +00:00
}
2017-07-17 02:29:43 +00:00
const tab = element . closest ( '.tab-pane' ) ;
const tabId = tab . attr ( 'id' ) ;
const container = tab . find ( 'tbody' ) . empty ( ) ;
2017-03-03 05:01:49 +00:00
const markers = {
2017-03-29 03:49:26 +00:00
'terms' : [
'audio' ,
'cloze-body' ,
'cloze-prefix' ,
'cloze-suffix' ,
'dictionary' ,
'expression' ,
'furigana' ,
2017-08-26 19:18:35 +00:00
'furigana-plain' ,
2017-03-29 03:49:26 +00:00
'glossary' ,
2017-07-16 08:05:23 +00:00
'glossary-brief' ,
2017-03-29 03:49:26 +00:00
'reading' ,
'sentence' ,
'tags' ,
2019-08-15 23:39:58 +00:00
'url' ,
'screenshot'
2017-03-29 03:49:26 +00:00
] ,
'kanji' : [
'character' ,
'dictionary' ,
'glossary' ,
'kunyomi' ,
'onyomi' ,
'sentence' ,
'tags' ,
'url'
]
2017-03-03 05:01:49 +00:00
} [ tabId ] || { } ;
2017-07-28 04:42:14 +00:00
for ( const name of await utilAnkiGetModelFieldNames ( modelName ) ) {
2017-07-17 02:29:43 +00:00
const value = options . anki [ tabId ] . fields [ name ] || '' ;
const html = Handlebars . templates [ 'model.html' ] ( { name , markers , value } ) ;
container . append ( $ ( html ) ) ;
}
2017-03-03 05:01:49 +00:00
2017-08-15 04:43:09 +00:00
tab . find ( '.anki-field-value' ) . change ( utilAsync ( onFormOptionsChanged ) ) ;
2017-07-17 02:55:33 +00:00
tab . find ( '.marker-link' ) . click ( onAnkiMarkerClicked ) ;
}
function onAnkiMarkerClicked ( e ) {
e . preventDefault ( ) ;
const link = e . target ;
$ ( link ) . closest ( '.input-group' ) . find ( '.anki-field-value' ) . val ( ` { ${ link . text } } ` ) . trigger ( 'change' ) ;
2016-04-04 03:05:22 +00:00
}
2017-08-15 04:43:09 +00:00
async function onAnkiModelChanged ( e ) {
2017-07-17 02:29:43 +00:00
try {
2017-08-15 04:43:09 +00:00
if ( ! e . originalEvent ) {
return ;
}
2017-07-17 02:29:43 +00:00
const element = $ ( this ) ;
2017-03-03 05:01:49 +00:00
const tab = element . closest ( '.tab-pane' ) ;
const tabId = tab . attr ( 'id' ) ;
2017-07-17 02:29:43 +00:00
const { optionsNew , optionsOld } = await formRead ( ) ;
2017-03-03 05:01:49 +00:00
optionsNew . anki [ tabId ] . fields = { } ;
2017-07-17 02:29:43 +00:00
await optionsSave ( optionsNew ) ;
2017-08-16 03:58:50 +00:00
ankiSpinnerShow ( true ) ;
2017-07-17 02:29:43 +00:00
await ankiFieldsPopulate ( element , optionsNew ) ;
2017-08-16 03:58:50 +00:00
ankiErrorShow ( ) ;
2017-07-17 02:29:43 +00:00
} catch ( e ) {
ankiErrorShow ( e ) ;
} finally {
ankiSpinnerShow ( false ) ;
}
2017-08-15 04:43:09 +00:00
}
2017-09-06 20:18:06 +00:00
async function onAnkiFieldTemplatesReset ( e ) {
try {
e . preventDefault ( ) ;
const options = await optionsLoad ( ) ;
$ ( '#field-templates' ) . val ( options . anki . fieldTemplates = optionsFieldTemplates ( ) ) ;
await optionsSave ( options ) ;
} catch ( e ) {
ankiErrorShow ( e ) ;
}
}
2019-05-04 16:57:55 +00:00
/ *
* Storage
* /
async function getBrowser ( ) {
if ( typeof chrome !== "undefined" ) {
if ( typeof browser !== "undefined" ) {
try {
const info = await browser . runtime . getBrowserInfo ( ) ;
if ( info . name === "Fennec" ) {
return "firefox-mobile" ;
}
} catch ( e ) { }
return "firefox" ;
} else {
return "chrome" ;
}
} else {
return "edge" ;
}
}
function storageBytesToLabeledString ( size ) {
const base = 1000 ;
const labels = [ "bytes" , "KB" , "MB" , "GB" ] ;
let labelIndex = 0 ;
while ( size >= base ) {
size /= base ;
++ labelIndex ;
}
2019-02-27 02:03:34 +00:00
const label = size . toFixed ( 1 ) ;
2019-05-04 16:57:55 +00:00
return ` ${ label } ${ labels [ labelIndex ] } ` ;
}
async function storageEstimate ( ) {
try {
2019-02-27 02:03:34 +00:00
return ( storageEstimate . mostRecent = await navigator . storage . estimate ( ) ) ;
2019-05-04 16:57:55 +00:00
} catch ( e ) { }
return null ;
}
2019-02-27 02:03:34 +00:00
storageEstimate . mostRecent = null ;
2019-05-04 16:57:55 +00:00
async function storageInfoInitialize ( ) {
const browser = await getBrowser ( ) ;
const container = document . querySelector ( "#storage-info" ) ;
container . setAttribute ( "data-browser" , browser ) ;
await storageShowInfo ( ) ;
container . classList . remove ( "storage-hidden" ) ;
document . querySelector ( "#storage-refresh" ) . addEventListener ( 'click' , ( ) => storageShowInfo ( ) , false ) ;
}
2019-02-27 02:03:34 +00:00
async function storageUpdateStats ( ) {
storageUpdateStats . isUpdating = true ;
2019-05-04 16:57:55 +00:00
const estimate = await storageEstimate ( ) ;
const valid = ( estimate !== null ) ;
if ( valid ) {
document . querySelector ( "#storage-usage" ) . textContent = storageBytesToLabeledString ( estimate . usage ) ;
document . querySelector ( "#storage-quota" ) . textContent = storageBytesToLabeledString ( estimate . quota ) ;
}
2019-02-27 02:03:34 +00:00
storageUpdateStats . isUpdating = false ;
return valid ;
}
storageUpdateStats . isUpdating = false ;
async function storageShowInfo ( ) {
storageSpinnerShow ( true ) ;
const valid = await storageUpdateStats ( ) ;
2019-05-04 16:57:55 +00:00
document . querySelector ( "#storage-use" ) . classList . toggle ( "storage-hidden" , ! valid ) ;
document . querySelector ( "#storage-error" ) . classList . toggle ( "storage-hidden" , valid ) ;
storageSpinnerShow ( false ) ;
}
function storageSpinnerShow ( show ) {
const spinner = $ ( '#storage-spinner' ) ;
if ( show ) {
spinner . show ( ) ;
} else {
spinner . hide ( ) ;
}
}