Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Stats in Dictionary Details #1248

Merged
merged 2 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions ext/js/pages/settings/dictionary-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,22 +254,34 @@ class DictionaryEntry {
* @returns {boolean}
*/
_setupDetails(detailsTable) {
/** @type {[label: string, key: 'author'|'url'|'description'|'attribution'|'sourceLanguage'|'targetLanguage'][]} */
const targets = [
['Author', 'author'],
['URL', 'url'],
['Description', 'description'],
['Attribution', 'attribution'],
['Source Language', 'sourceLanguage'],
['Target Language', 'targetLanguage'],
];

const dictionaryInfo = this._dictionaryInfo;
/** @type {Partial<Record<keyof (typeof this._dictionaryInfo & typeof this._dictionaryInfo.counts), string>>} */
const targets = {
author: 'Author',
url: 'URL',
description: 'Description',
attribution: 'Attribution',
sourceLanguage: 'Source Language',
targetLanguage: 'Target Language',
terms: 'Term Count',
kanji: 'Kanji Count',
kanjiMeta: 'Kanji Meta Count',
tagMeta: 'Tag Count',
media: 'Media Count',
};

const dictionaryInfo = {...this._dictionaryInfo, ...this._dictionaryInfo.counts};
const fragment = document.createDocumentFragment();
let any = false;
for (const [label, key] of targets) {
for (const [key, label] of /** @type {([keyof (typeof this._dictionaryInfo & typeof this._dictionaryInfo.counts), string])[]} */ (Object.entries(targets))) {
const info = dictionaryInfo[key];
if (typeof info !== 'string') { continue; }
const displayText = ((_info) => {
if (typeof _info === 'string') { return _info; }
if (_info && typeof _info === 'object' && 'total' in _info) {
return _info.total ? `${_info.total}` : false;
}
return false;
})(info);
if (!displayText) { continue; }

const details = /** @type {HTMLElement} */ (this._dictionaryController.instantiateTemplate('dictionary-details-entry'));
details.dataset.type = key;
Expand All @@ -280,7 +292,7 @@ class DictionaryEntry {
const infoElement = querySelectorNotNull(details, '.dictionary-details-entry-info');

labelElement.textContent = `${label}:`;
infoElement.textContent = info;
infoElement.textContent = displayText;
fragment.appendChild(details);

any = true;
Expand Down
8 changes: 8 additions & 0 deletions ext/templates-settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@
<span class="dictionary-details-entry-label"></span>
<span class="dictionary-details-entry-info"></span>
</div></template>
<template id="dictionary-stats-table-template">
<table class="dictionary-stats-table">
<thead class="dictionary-stats-table-head"></thead>
<tbody class="dictionary-stats-table-body">
<tr class="dictionary-stats-table-row"></tr>
</tbody>
</table>
</template>
<template id="dictionary-extra-template">
<div class="dictionary-item-bottom"></div>
<div class="dictionary-item-bottom"></div>
Expand Down