Move display templates into a single file

This commit is contained in:
toasted-nutbread 2019-12-27 18:58:11 -05:00
parent d512c111f6
commit 7686e56c00
9 changed files with 174 additions and 180 deletions

View File

@ -25,6 +25,10 @@ function apiAudioGetUrl(definition, source, optionsContext) {
return _apiInvoke('audioGetUrl', {definition, source, optionsContext});
}
function apiGetDisplayTemplatesHtml() {
return _apiInvoke('getDisplayTemplatesHtml');
}
function _apiInvoke(action, params={}) {
const data = {action, params};
return new Promise((resolve, reject) => {

View File

@ -522,6 +522,11 @@ class Backend {
return result;
}
async _onApiGetDisplayTemplatesHtml() {
const url = chrome.runtime.getURL('/mixed/display-templates.html');
return await requestText(url, 'GET');
}
// Command handlers
async _onCommandSearch(params) {
@ -735,7 +740,8 @@ Backend._messageHandlers = new Map([
['frameInformationGet', (self, ...args) => self._onApiFrameInformationGet(...args)],
['injectStylesheet', (self, ...args) => self._onApiInjectStylesheet(...args)],
['getEnvironmentInfo', (self, ...args) => self._onApiGetEnvironmentInfo(...args)],
['clipboardGet', (self, ...args) => self._onApiClipboardGet(...args)]
['clipboardGet', (self, ...args) => self._onApiClipboardGet(...args)],
['getDisplayTemplatesHtml', (self, ...args) => self._onApiGetDisplayTemplatesHtml(...args)]
]);
Backend._commandHandlers = new Map([

View File

@ -17,10 +17,10 @@
*/
function requestJson(url, action, params) {
function requestText(url, action, params) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.overrideMimeType('application/json');
xhr.overrideMimeType('text/plain');
xhr.addEventListener('load', () => resolve(xhr.responseText));
xhr.addEventListener('error', () => reject(new Error('Failed to connect')));
xhr.open(action, url);
@ -29,12 +29,15 @@ function requestJson(url, action, params) {
} else {
xhr.send();
}
}).then((responseText) => {
});
}
async function requestJson(url, action, params) {
const responseText = await requestText(url, action, params);
try {
return JSON.parse(responseText);
}
catch (e) {
return Promise.reject(new Error('Invalid response'));
throw new Error('Invalid response');
}
});
}

View File

@ -66,84 +66,6 @@
</div>
</div>
<template id="term-entry-template"><div class="entry" data-type="term">
<div class="entry-header1">
<div class="entry-header2">
<div class="entry-header3">
<div class="actions">
<button class="action-button action-view-note pending disabled"><img src="/mixed/img/view-note.svg" class="icon-image" title="View added note (Alt + V)" alt></button>
<button class="action-button action-add-note pending disabled" data-mode="term-kanji"><img src="/mixed/img/add-term-kanji.svg" class="icon-image" title="Add expression (Alt + E)" alt></button>
<button class="action-button action-add-note pending disabled" data-mode="term-kana"><img src="/mixed/img/add-term-kana.svg" class="icon-image" title="Add reading (Alt + R)" alt></button>
<button class="action-button action-play-audio"><img src="/mixed/img/play-audio.svg" class="icon-image" title="Play audio (Alt + P)" alt></button>
<span class="action-button action-current-indicator"><img src="/mixed/img/entry-current.svg" class="icon-image current" title="Current entry (Alt + Up/Down/Home/End/PgUp/PgDn)" alt></span>
</div>
<div class="term-expression-list"></div>
</div>
<div class="term-reasons"></div>
</div>
<div class="frequencies"></div>
</div>
<div class="term-definition-container"><ol class="term-definition-list"></ol></div>
<pre class="debug-info"></pre>
</div></template>
<template id="term-expression-template"><div class="term-expression"><span class="term-expression-text"></span><div class="term-expression-details">
<button class="action-button action-play-audio"><img src="/mixed/img/play-audio.svg" class="icon-image" title="Play audio" alt></button>
<div class="tags tag-list"></div>
<div class="frequencies tag-list"></div>
</div></div></template>
<template id="term-definition-item-template"><li class="term-definition-item">
<div class="term-definition-tag-list tag-list"></div>
<div class="term-definition-only-list"></div>
<ul class="term-glossary-list"></ul>
</li></template>
<template id="term-definition-only-template"><span class="term-definition-only"></span></template>
<template id="term-glossary-item-template"><li class="term-glossary-item"><span class="term-glossary"></span></li></template>
<template id="term-reason-template"><span class="term-reason"></span></template>
<template id="kanji-entry-template"><div class="entry" data-type="kanji">
<div class="entry-header1">
<div class="entry-header2">
<div class="entry-header3">
<div class="actions">
<button class="action-button action-view-note pending disabled"><img src="/mixed/img/view-note.svg" class="icon-image" title="View added note (Alt + V)" alt></button>
<button class="action-button action-add-note pending disabled" data-mode="kanji"><img src="/mixed/img/add-term-kanji.svg" class="icon-image" title="Add Kanji (Alt + K)" alt></button>
<span class="action-button action-current-indicator"><img src="/mixed/img/entry-current.svg" class="icon-image current" title="Current entry (Alt + Up/Down/Home/End/PgUp/PgDn)" alt></span>
</div>
<div class="kanji-glyph"></div>
</div>
</div>
<div class="frequencies"></div>
</div>
<div class="tags tag-list"></div>
<table class="kanji-glyph-data"><tbody>
<tr>
<th>Glossary</th>
<th>Readings</th>
<th>Statistics</th>
</tr>
<tr>
<td class="kanji-glossary-container"><ol class="kanji-glossary-list"></ol></td>
<td class="kanji-readings"><dl class="kanji-readings-chinese"></dl><dl class="kanji-readings-japanese"></dl></td>
<td class="kanji-statistics"></td>
</tr>
<tr><th colspan="3">Classifications</th></tr>
<tr><td colspan="3" class="kanji-classifications"></td></tr>
<tr><th colspan="3">Codepoints</th></tr>
<tr><td colspan="3" class="kanji-codepoints"></td></tr>
<tr><th colspan="3">Dictionary Indices</th></tr>
<tr><td colspan="3" class="kanji-dictionary-indices"></td></tr>
</tbody></table>
<pre class="debug-info"></pre>
</div></template>
<template id="kanji-info-table-template"><table class="kanji-info-table"><tbody class="kanji-info-table-body"></tbody></table></template>
<template id="kanji-info-table-item-template"><tr class="kanji-info-table-item"><th class="kanji-info-table-item-header"></th><td class="kanji-info-table-item-value"></td></tr></template>
<template id="kanji-info-table-empty-template"><tr class="kanji-info-table-item kanji-info-table-item-empty"><td class="kanji-info-table-item-value-empty">No data found</td></tr class="kanji-info-table-item"></template>
<template id="kanji-glossary-item-template"><li class="kanji-glossary-item"><span class="kanji-glossary"></span></li></template>
<template id="kanji-reading-template"><dd class="kanji-reading"></dd></template>
<template id="tag-template"><span class="tag"></span></template>
<template id="tag-frequency-template"><span class="tag" data-category="frequency"><span class="term-frequency-dictionary-name"></span><span class="term-frequency-separator"></span><span class="term-frequency-value"></span></template>
<script src="/mixed/lib/handlebars.min.js"></script>
<script src="/mixed/lib/wanakana.min.js"></script>

View File

@ -40,84 +40,6 @@
</div>
</div>
<template id="term-entry-template"><div class="entry" data-type="term">
<div class="entry-header1">
<div class="entry-header2">
<div class="entry-header3">
<div class="actions">
<button class="action-button action-view-note pending disabled"><img src="/mixed/img/view-note.svg" class="icon-image" title="View added note (Alt + V)" alt></button>
<button class="action-button action-add-note pending disabled" data-mode="term-kanji"><img src="/mixed/img/add-term-kanji.svg" class="icon-image" title="Add expression (Alt + E)" alt></button>
<button class="action-button action-add-note pending disabled" data-mode="term-kana"><img src="/mixed/img/add-term-kana.svg" class="icon-image" title="Add reading (Alt + R)" alt></button>
<button class="action-button action-play-audio"><img src="/mixed/img/play-audio.svg" class="icon-image" title="Play audio (Alt + P)" alt></button>
<span class="action-button action-current-indicator"><img src="/mixed/img/entry-current.svg" class="icon-image current" title="Current entry (Alt + Up/Down/Home/End/PgUp/PgDn)" alt></span>
</div>
<div class="term-expression-list"></div>
</div>
<div class="term-reasons"></div>
</div>
<div class="frequencies"></div>
</div>
<div class="term-definition-container"><ol class="term-definition-list"></ol></div>
<pre class="debug-info"></pre>
</div></template>
<template id="term-expression-template"><div class="term-expression"><span class="term-expression-text"></span><div class="term-expression-details">
<button class="action-button action-play-audio"><img src="/mixed/img/play-audio.svg" class="icon-image" title="Play audio" alt></button>
<div class="tags tag-list"></div>
<div class="frequencies tag-list"></div>
</div></div></template>
<template id="term-definition-item-template"><li class="term-definition-item">
<div class="term-definition-tag-list tag-list"></div>
<div class="term-definition-only-list"></div>
<ul class="term-glossary-list"></ul>
</li></template>
<template id="term-definition-only-template"><span class="term-definition-only"></span></template>
<template id="term-glossary-item-template"><li class="term-glossary-item"><span class="term-glossary"></span></li></template>
<template id="term-reason-template"><span class="term-reason"></span></template>
<template id="kanji-entry-template"><div class="entry" data-type="kanji">
<div class="entry-header1">
<div class="entry-header2">
<div class="entry-header3">
<div class="actions">
<button class="action-button action-view-note pending disabled"><img src="/mixed/img/view-note.svg" class="icon-image" title="View added note (Alt + V)" alt></button>
<button class="action-button action-add-note pending disabled" data-mode="kanji"><img src="/mixed/img/add-term-kanji.svg" class="icon-image" title="Add Kanji (Alt + K)" alt></button>
<span class="action-button action-current-indicator"><img src="/mixed/img/entry-current.svg" class="icon-image current" title="Current entry (Alt + Up/Down/Home/End/PgUp/PgDn)" alt></span>
</div>
<div class="kanji-glyph"></div>
</div>
</div>
<div class="frequencies"></div>
</div>
<div class="tags tag-list"></div>
<table class="kanji-glyph-data"><tbody>
<tr>
<th>Glossary</th>
<th>Readings</th>
<th>Statistics</th>
</tr>
<tr>
<td class="kanji-glossary-container"><ol class="kanji-glossary-list"></ol></td>
<td class="kanji-readings"><dl class="kanji-readings-chinese"></dl><dl class="kanji-readings-japanese"></dl></td>
<td class="kanji-statistics"></td>
</tr>
<tr><th colspan="3">Classifications</th></tr>
<tr><td colspan="3" class="kanji-classifications"></td></tr>
<tr><th colspan="3">Codepoints</th></tr>
<tr><td colspan="3" class="kanji-codepoints"></td></tr>
<tr><th colspan="3">Dictionary Indices</th></tr>
<tr><td colspan="3" class="kanji-dictionary-indices"></td></tr>
</tbody></table>
<pre class="debug-info"></pre>
</div></template>
<template id="kanji-info-table-template"><table class="kanji-info-table"><tbody class="kanji-info-table-body"></tbody></table></template>
<template id="kanji-info-table-item-template"><tr class="kanji-info-table-item"><th class="kanji-info-table-item-header"></th><td class="kanji-info-table-item-value"></td></tr></template>
<template id="kanji-info-table-empty-template"><tr class="kanji-info-table-item kanji-info-table-item-empty"><td class="kanji-info-table-item-value-empty">No data found</td></tr class="kanji-info-table-item"></template>
<template id="kanji-glossary-item-template"><li class="kanji-glossary-item"><span class="kanji-glossary"></span></li></template>
<template id="kanji-reading-template"><dd class="kanji-reading"></dd></template>
<template id="tag-template"><span class="tag"></span></template>
<template id="tag-frequency-template"><span class="tag" data-category="frequency"><span class="term-frequency-dictionary-name"></span><span class="term-frequency-separator"></span><span class="term-frequency-value"></span></template>
<script src="/mixed/js/core.js"></script>
<script src="/mixed/js/dom.js"></script>
<script src="/mixed/js/api.js"></script>

View File

@ -0,0 +1,81 @@
<!DOCTYPE html><html><head></head><body>
<template id="term-entry-template"><div class="entry" data-type="term">
<div class="entry-header1">
<div class="entry-header2">
<div class="entry-header3">
<div class="actions">
<button class="action-button action-view-note pending disabled"><img src="/mixed/img/view-note.svg" class="icon-image" title="View added note (Alt + V)" alt></button>
<button class="action-button action-add-note pending disabled" data-mode="term-kanji"><img src="/mixed/img/add-term-kanji.svg" class="icon-image" title="Add expression (Alt + E)" alt></button>
<button class="action-button action-add-note pending disabled" data-mode="term-kana"><img src="/mixed/img/add-term-kana.svg" class="icon-image" title="Add reading (Alt + R)" alt></button>
<button class="action-button action-play-audio"><img src="/mixed/img/play-audio.svg" class="icon-image" title="Play audio (Alt + P)" alt></button>
<span class="action-button action-current-indicator"><img src="/mixed/img/entry-current.svg" class="icon-image current" title="Current entry (Alt + Up/Down/Home/End/PgUp/PgDn)" alt></span>
</div>
<div class="term-expression-list"></div>
</div>
<div class="term-reasons"></div>
</div>
<div class="frequencies"></div>
</div>
<div class="term-definition-container"><ol class="term-definition-list"></ol></div>
<pre class="debug-info"></pre>
</div></template>
<template id="term-expression-template"><div class="term-expression"><span class="term-expression-text"></span><div class="term-expression-details">
<button class="action-button action-play-audio"><img src="/mixed/img/play-audio.svg" class="icon-image" title="Play audio" alt></button>
<div class="tags tag-list"></div>
<div class="frequencies tag-list"></div>
</div></div></template>
<template id="term-definition-item-template"><li class="term-definition-item">
<div class="term-definition-tag-list tag-list"></div>
<div class="term-definition-only-list"></div>
<ul class="term-glossary-list"></ul>
</li></template>
<template id="term-definition-only-template"><span class="term-definition-only"></span></template>
<template id="term-glossary-item-template"><li class="term-glossary-item"><span class="term-glossary"></span></li></template>
<template id="term-reason-template"><span class="term-reason"></span></template>
<template id="kanji-entry-template"><div class="entry" data-type="kanji">
<div class="entry-header1">
<div class="entry-header2">
<div class="entry-header3">
<div class="actions">
<button class="action-button action-view-note pending disabled"><img src="/mixed/img/view-note.svg" class="icon-image" title="View added note (Alt + V)" alt></button>
<button class="action-button action-add-note pending disabled" data-mode="kanji"><img src="/mixed/img/add-term-kanji.svg" class="icon-image" title="Add Kanji (Alt + K)" alt></button>
<span class="action-button action-current-indicator"><img src="/mixed/img/entry-current.svg" class="icon-image current" title="Current entry (Alt + Up/Down/Home/End/PgUp/PgDn)" alt></span>
</div>
<div class="kanji-glyph"></div>
</div>
</div>
<div class="frequencies"></div>
</div>
<div class="tags tag-list"></div>
<table class="kanji-glyph-data"><tbody>
<tr>
<th>Glossary</th>
<th>Readings</th>
<th>Statistics</th>
</tr>
<tr>
<td class="kanji-glossary-container"><ol class="kanji-glossary-list"></ol></td>
<td class="kanji-readings"><dl class="kanji-readings-chinese"></dl><dl class="kanji-readings-japanese"></dl></td>
<td class="kanji-statistics"></td>
</tr>
<tr><th colspan="3">Classifications</th></tr>
<tr><td colspan="3" class="kanji-classifications"></td></tr>
<tr><th colspan="3">Codepoints</th></tr>
<tr><td colspan="3" class="kanji-codepoints"></td></tr>
<tr><th colspan="3">Dictionary Indices</th></tr>
<tr><td colspan="3" class="kanji-dictionary-indices"></td></tr>
</tbody></table>
<pre class="debug-info"></pre>
</div></template>
<template id="kanji-info-table-template"><table class="kanji-info-table"><tbody class="kanji-info-table-body"></tbody></table></template>
<template id="kanji-info-table-item-template"><tr class="kanji-info-table-item"><th class="kanji-info-table-item-header"></th><td class="kanji-info-table-item-value"></td></tr></template>
<template id="kanji-info-table-empty-template"><tr class="kanji-info-table-item kanji-info-table-item-empty"><td class="kanji-info-table-item-value-empty">No data found</td></tr class="kanji-info-table-item"></template>
<template id="kanji-glossary-item-template"><li class="kanji-glossary-item"><span class="kanji-glossary"></span></li></template>
<template id="kanji-reading-template"><dd class="kanji-reading"></dd></template>
<template id="tag-template"><span class="tag"></span></template>
<template id="tag-frequency-template"><span class="tag" data-category="frequency"><span class="term-frequency-dictionary-name"></span><span class="term-frequency-separator"></span><span class="term-frequency-value"></span></template>
</body></html>

View File

@ -101,6 +101,10 @@ function apiClipboardGet() {
return _apiInvoke('clipboardGet');
}
function apiGetDisplayTemplatesHtml() {
return _apiInvoke('getDisplayTemplatesHtml');
}
function _apiInvoke(action, params={}) {
const data = {action, params};
return new Promise((resolve, reject) => {

View File

@ -19,22 +19,39 @@
class DisplayGenerator {
constructor() {
this._termEntryTemplate = document.querySelector('#term-entry-template');
this._termExpressionTemplate = document.querySelector('#term-expression-template');
this._termDefinitionItemTemplate = document.querySelector('#term-definition-item-template');
this._termDefinitionOnlyTemplate = document.querySelector('#term-definition-only-template');
this._termGlossaryItemTemplate = document.querySelector('#term-glossary-item-template');
this._termReasonTemplate = document.querySelector('#term-reason-template');
this._isInitialized = false;
this._initializationPromise = null;
this._kanjiEntryTemplate = document.querySelector('#kanji-entry-template');
this._kanjiInfoTableTemplate = document.querySelector('#kanji-info-table-template');
this._kanjiInfoTableItemTemplate = document.querySelector('#kanji-info-table-item-template');
this._kanjiInfoTableEmptyTemplate = document.querySelector('#kanji-info-table-empty-template');
this._kanjiGlossaryItemTemplate = document.querySelector('#kanji-glossary-item-template');
this._kanjiReadingTemplate = document.querySelector('#kanji-reading-template');
this._termEntryTemplate = null;
this._termExpressionTemplate = null;
this._termDefinitionItemTemplate = null;
this._termDefinitionOnlyTemplate = null;
this._termGlossaryItemTemplate = null;
this._termReasonTemplate = null;
this._tagTemplate = document.querySelector('#tag-template');
this._tagFrequencyTemplate = document.querySelector('#tag-frequency-template');
this._kanjiEntryTemplate = null;
this._kanjiInfoTableTemplate = null;
this._kanjiInfoTableItemTemplate = null;
this._kanjiInfoTableEmptyTemplate = null;
this._kanjiGlossaryItemTemplate = null;
this._kanjiReadingTemplate = null;
this._tagTemplate = null;
this._tagFrequencyTemplate = null;
}
isInitialized() {
return this._isInitialized;
}
initialize() {
if (this._isInitialized) {
return Promise.resolve();
}
if (this._initializationPromise === null) {
this._initializationPromise = this._initializeInternal();
}
return this._initializationPromise;
}
createTermEntry(details) {
@ -259,6 +276,31 @@ class DisplayGenerator {
return node;
}
async _initializeInternal() {
const html = await apiGetDisplayTemplatesHtml();
const doc = new DOMParser().parseFromString(html, 'text/html');
this._setTemplates(doc);
}
_setTemplates(doc) {
this._termEntryTemplate = doc.querySelector('#term-entry-template');
this._termExpressionTemplate = doc.querySelector('#term-expression-template');
this._termDefinitionItemTemplate = doc.querySelector('#term-definition-item-template');
this._termDefinitionOnlyTemplate = doc.querySelector('#term-definition-only-template');
this._termGlossaryItemTemplate = doc.querySelector('#term-glossary-item-template');
this._termReasonTemplate = doc.querySelector('#term-reason-template');
this._kanjiEntryTemplate = doc.querySelector('#kanji-entry-template');
this._kanjiInfoTableTemplate = doc.querySelector('#kanji-info-table-template');
this._kanjiInfoTableItemTemplate = doc.querySelector('#kanji-info-table-item-template');
this._kanjiInfoTableEmptyTemplate = doc.querySelector('#kanji-info-table-empty-template');
this._kanjiGlossaryItemTemplate = doc.querySelector('#kanji-glossary-item-template');
this._kanjiReadingTemplate = doc.querySelector('#kanji-reading-template');
this._tagTemplate = doc.querySelector('#tag-template');
this._tagFrequencyTemplate = doc.querySelector('#tag-frequency-template');
}
_appendKanjiLinks(container, text) {
let part = '';
for (const c of text) {

View File

@ -356,6 +356,11 @@ class Display {
window.focus();
}
if (!this.displayGenerator.isInitialized()) {
await this.displayGenerator.initialize();
if (this.setContentToken !== token) { return; }
}
this.definitions = definitions;
if (context.disableHistory) {
delete context.disableHistory;
@ -415,6 +420,11 @@ class Display {
window.focus();
}
if (!this.displayGenerator.isInitialized()) {
await this.displayGenerator.initialize();
if (this.setContentToken !== token) { return; }
}
this.definitions = definitions;
if (context.disableHistory) {
delete context.disableHistory;