Use entryIndexFind without jQuery

This commit is contained in:
toasted-nutbread 2019-09-15 14:06:27 -04:00
parent b61fc81012
commit 355fb29c3b

View File

@ -52,11 +52,11 @@ class Display {
try { try {
e.preventDefault(); e.preventDefault();
const link = $(e.target); const link = e.target;
const context = { const context = {
source: { source: {
definitions: this.definitions, definitions: this.definitions,
index: Display.entryIndexFind(link), index: this.entryIndexFind(link),
scroll: $('html,body').scrollTop() scroll: $('html,body').scrollTop()
} }
}; };
@ -67,7 +67,7 @@ class Display {
context.source.source = this.context.source; context.source.source = this.context.source;
} }
const kanjiDefs = await apiKanjiFind(link.text(), this.optionsContext); const kanjiDefs = await apiKanjiFind(link.textContent, this.optionsContext);
this.kanjiShow(kanjiDefs, this.options, context); this.kanjiShow(kanjiDefs, this.options, context);
} catch (e) { } catch (e) {
this.onError(e); this.onError(e);
@ -80,7 +80,7 @@ class Display {
const {docRangeFromPoint, docSentenceExtract} = this.dependencies; const {docRangeFromPoint, docSentenceExtract} = this.dependencies;
const clickedElement = $(e.target); const clickedElement = e.target;
const textSource = docRangeFromPoint(e.clientX, e.clientY, this.options); const textSource = docRangeFromPoint(e.clientX, e.clientY, this.options);
if (textSource === null) { if (textSource === null) {
return false; return false;
@ -105,7 +105,7 @@ class Display {
const context = { const context = {
source: { source: {
definitions: this.definitions, definitions: this.definitions,
index: Display.entryIndexFind(clickedElement), index: this.entryIndexFind(clickedElement),
scroll: $('html,body').scrollTop() scroll: $('html,body').scrollTop()
} }
}; };
@ -124,24 +124,24 @@ class Display {
onAudioPlay(e) { onAudioPlay(e) {
e.preventDefault(); e.preventDefault();
const link = $(e.currentTarget); const link = e.currentTarget;
const definitionIndex = Display.entryIndexFind(link); const entry = link.closest('.entry');
const expressionIndex = link.closest('.entry').find('.expression .action-play-audio').index(link); const definitionIndex = this.entryIndexFind(entry);
const expressionIndex = Display.indexOf(entry.querySelectorAll('.expression .action-play-audio'), link);
this.audioPlay(this.definitions[definitionIndex], expressionIndex); this.audioPlay(this.definitions[definitionIndex], expressionIndex);
} }
onNoteAdd(e) { onNoteAdd(e) {
e.preventDefault(); e.preventDefault();
const link = $(e.currentTarget); const link = e.currentTarget;
const index = Display.entryIndexFind(link); const index = this.entryIndexFind(link);
this.noteAdd(this.definitions[index], link.data('mode')); this.noteAdd(this.definitions[index], link.dataset.mode);
} }
onNoteView(e) { onNoteView(e) {
e.preventDefault(); e.preventDefault();
const link = $(e.currentTarget); const link = e.currentTarget;
const index = Display.entryIndexFind(link); apiNoteView(link.dataset.noteId);
apiNoteView(link.data('noteId'));
} }
onKeyDown(e) { onKeyDown(e) {
@ -556,8 +556,9 @@ class Display {
return result; return result;
} }
static entryIndexFind(element) { entryIndexFind(element) {
return $('.entry').index(element.closest('.entry')); const entry = element.closest('.entry');
return entry !== null ? Display.indexOf(this.container.get(0).querySelectorAll('.entry'), entry) : -1;
} }
static adderButtonFind(index, mode) { static adderButtonFind(index, mode) {
@ -571,4 +572,13 @@ class Display {
static delay(time) { static delay(time) {
return new Promise((resolve) => setTimeout(resolve, time)); return new Promise((resolve) => setTimeout(resolve, time));
} }
static indexOf(nodeList, node) {
for (let i = 0, ii = nodeList.length; i < ii; ++i) {
if (nodeList[i] === node) {
return i;
}
}
return -1;
}
} }