aboutsummaryrefslogtreecommitdiff
path: root/ext/mixed/js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-02-14 21:25:25 -0500
committerGitHub <noreply@github.com>2020-02-14 21:25:25 -0500
commit853faaf48c63b6efba09d3de0c0c508f7c24ff50 (patch)
tree4411d78032b2a7e595a2d573ab0e3fd264788a51 /ext/mixed/js
parent548c67ef1ce2a0aa8859dc458e003a98ad5b9ed6 (diff)
parent4dd4926672b4db39721c10a3941246ab1225f988 (diff)
Merge pull request #358 from toasted-nutbread/general-refactoring
General refactoring
Diffstat (limited to 'ext/mixed/js')
-rw-r--r--ext/mixed/js/api.js4
-rw-r--r--ext/mixed/js/audio.js9
-rw-r--r--ext/mixed/js/core.js6
-rw-r--r--ext/mixed/js/display.js28
4 files changed, 29 insertions, 18 deletions
diff --git a/ext/mixed/js/api.js b/ext/mixed/js/api.js
index 0b1e7e4f..715c8286 100644
--- a/ext/mixed/js/api.js
+++ b/ext/mixed/js/api.js
@@ -65,8 +65,8 @@ function apiNoteView(noteId) {
return _apiInvoke('noteView', {noteId});
}
-function apiTemplateRender(template, data, dynamic) {
- return _apiInvoke('templateRender', {data, template, dynamic});
+function apiTemplateRender(template, data) {
+ return _apiInvoke('templateRender', {data, template});
}
function apiAudioGetUrl(definition, source, optionsContext) {
diff --git a/ext/mixed/js/audio.js b/ext/mixed/js/audio.js
index 76a3e7da..47db5c75 100644
--- a/ext/mixed/js/audio.js
+++ b/ext/mixed/js/audio.js
@@ -114,8 +114,11 @@ function audioGetFromUrl(url, willDownload) {
async function audioGetFromSources(expression, sources, optionsContext, willDownload, cache=null) {
const key = `${expression.expression}:${expression.reading}`;
- if (cache !== null && hasOwn(cache, expression)) {
- return cache[key];
+ if (cache !== null) {
+ const cacheValue = cache.get(expression);
+ if (typeof cacheValue !== 'undefined') {
+ return cacheValue;
+ }
}
for (let i = 0, ii = sources.length; i < ii; ++i) {
@@ -133,7 +136,7 @@ async function audioGetFromSources(expression, sources, optionsContext, willDown
}
const result = {audio, url, source};
if (cache !== null) {
- cache[key] = result;
+ cache.set(key, result);
}
return result;
} catch (e) {
diff --git a/ext/mixed/js/core.js b/ext/mixed/js/core.js
index ca9e98e5..c273bb87 100644
--- a/ext/mixed/js/core.js
+++ b/ext/mixed/js/core.js
@@ -113,11 +113,7 @@ function toIterable(value) {
if (value !== null && typeof value === 'object') {
const length = value.length;
if (typeof length === 'number' && Number.isFinite(length)) {
- const array = [];
- for (let i = 0; i < length; ++i) {
- array.push(value[i]);
- }
- return array;
+ return Array.from(value);
}
}
diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js
index e4be4c83..90b7aaf3 100644
--- a/ext/mixed/js/display.js
+++ b/ext/mixed/js/display.js
@@ -32,7 +32,7 @@ class Display {
this.index = 0;
this.audioPlaying = null;
this.audioFallback = null;
- this.audioCache = {};
+ this.audioCache = new Map();
this.styleNode = null;
this.eventListeners = [];
@@ -186,13 +186,15 @@ class Display {
e.preventDefault();
const link = e.currentTarget;
const entry = link.closest('.entry');
- const definitionIndex = this.entryIndexFind(entry);
+ const index = this.entryIndexFind(entry);
+ if (index < 0 || index >= this.definitions.length) { return; }
+
const expressionIndex = Display.indexOf(entry.querySelectorAll('.term-expression .action-play-audio'), link);
this.audioPlay(
- this.definitions[definitionIndex],
+ this.definitions[index],
// expressionIndex is used in audioPlay to detect result output mode
Math.max(expressionIndex, this.options.general.resultOutputMode === 'merge' ? 0 : -1),
- definitionIndex
+ index
);
}
@@ -200,6 +202,8 @@ class Display {
e.preventDefault();
const link = e.currentTarget;
const index = this.entryIndexFind(link);
+ if (index < 0 || index >= this.definitions.length) { return; }
+
this.noteAdd(this.definitions[index], link.dataset.mode);
}
@@ -498,6 +502,8 @@ class Display {
}
autoPlayAudio() {
+ if (this.definitions.length === 0) { return; }
+
this.audioPlay(this.definitions[0], this.firstExpressionIndex, 0);
}
@@ -597,9 +603,12 @@ class Display {
}
noteTryAdd(mode) {
- const button = this.adderButtonFind(this.index, mode);
+ const index = this.index;
+ if (index < 0 || index >= this.definitions.length) { return; }
+
+ const button = this.adderButtonFind(index, mode);
if (button !== null && !button.classList.contains('disabled')) {
- this.noteAdd(this.definitions[this.index], mode);
+ this.noteAdd(this.definitions[index], mode);
}
}
@@ -901,9 +910,12 @@ Display._onKeyDownHandlers = new Map([
['P', (self, e) => {
if (e.altKey) {
- const entry = self.getEntry(self.index);
+ const index = self.index;
+ if (index < 0 || index >= self.definitions.length) { return; }
+
+ const entry = self.getEntry(index);
if (entry !== null && entry.dataset.type === 'term') {
- self.audioPlay(self.definitions[self.index], self.firstExpressionIndex, self.index);
+ self.audioPlay(self.definitions[index], self.firstExpressionIndex, index);
}
return true;
}