Remove jQuery calls from adderButtonFind and viewerButtonFind

This commit is contained in:
toasted-nutbread 2019-09-15 18:29:11 -04:00
parent 946e577e42
commit 3ca84e3a85

View File

@ -150,16 +150,16 @@ class Display {
onKeyDown(e) { onKeyDown(e) {
const noteTryAdd = mode => { const noteTryAdd = mode => {
const button = Display.adderButtonFind(this.index, mode); const button = this.adderButtonFind(this.index, mode);
if (button.length !== 0 && !button.hasClass('disabled')) { if (button !== null && !button.classList.contains('disabled')) {
this.noteAdd(this.definitions[this.index], mode); this.noteAdd(this.definitions[this.index], mode);
} }
}; };
const noteTryView = mode => { const noteTryView = mode => {
const button = Display.viewerButtonFind(this.index); const button = this.viewerButtonFind(this.index);
if (button.length !== 0 && !button.hasClass('disabled')) { if (button !== null && !button.classList.contains('disabled')) {
apiNoteView(button.data('noteId')); apiNoteView(button.dataset.noteId);
} }
}; };
@ -241,7 +241,8 @@ class Display {
80: /* p */ () => { 80: /* p */ () => {
if (e.altKey) { if (e.altKey) {
if ($('.entry').eq(this.index).data('type') === 'term') { const entry = this.getEntry(this.index);
if (entry !== null && entry.dataset.type === 'term') {
this.audioPlay(this.definitions[this.index], this.firstExpressionIndex); this.audioPlay(this.definitions[this.index], this.firstExpressionIndex);
} }
@ -393,14 +394,13 @@ class Display {
for (let i = 0; i < states.length; ++i) { for (let i = 0; i < states.length; ++i) {
const state = states[i]; const state = states[i];
for (const mode in state) { for (const mode in state) {
const button = Display.adderButtonFind(i, mode); const button = this.adderButtonFind(i, mode);
if (state[mode]) { if (button === null) {
button.removeClass('disabled'); continue;
} else {
button.addClass('disabled');
} }
button.removeClass('pending'); button.classList.toggle('disabled', !state[mode]);
button.classList.remove('pending');
} }
} }
} catch (e) { } catch (e) {
@ -462,8 +462,15 @@ class Display {
const noteId = await apiDefinitionAdd(definition, mode, context, this.optionsContext); const noteId = await apiDefinitionAdd(definition, mode, context, this.optionsContext);
if (noteId) { if (noteId) {
const index = this.definitions.indexOf(definition); const index = this.definitions.indexOf(definition);
Display.adderButtonFind(index, mode).addClass('disabled'); const adderButton = this.adderButtonFind(index, mode);
Display.viewerButtonFind(index).removeClass('pending disabled').data('noteId', noteId); if (adderButton !== null) {
adderButton.classList.add('disabled');
}
const viewerButton = this.viewerButtonFind(index);
if (viewerButton !== null) {
viewerButton.classList.remove('pending', 'disabled');
viewerButton.dataset.noteId = noteId;
}
} else { } else {
throw 'Note could note be added'; throw 'Note could note be added';
} }
@ -549,6 +556,11 @@ class Display {
this.spinner.style.display = visible ? 'block' : ''; this.spinner.style.display = visible ? 'block' : '';
} }
getEntry(index) {
const entries = this.container.querySelectorAll('.entry');
return index >= 0 && index < entries.length ? entries[index] : null;
}
static clozeBuild(sentence, source) { static clozeBuild(sentence, source) {
const result = { const result = {
sentence: sentence.text.trim() sentence: sentence.text.trim()
@ -568,12 +580,14 @@ class Display {
return entry !== null ? Display.indexOf(this.container.querySelectorAll('.entry'), entry) : -1; return entry !== null ? Display.indexOf(this.container.querySelectorAll('.entry'), entry) : -1;
} }
static adderButtonFind(index, mode) { adderButtonFind(index, mode) {
return $('.entry').eq(index).find(`.action-add-note[data-mode="${mode}"]`); const entry = this.getEntry(index);
return entry !== null ? entry.querySelector(`.action-add-note[data-mode="${mode}"]`) : null;
} }
static viewerButtonFind(index) { viewerButtonFind(index) {
return $('.entry').eq(index).find('.action-view-note'); const entry = this.getEntry(index);
return entry !== null ? entry.querySelector('.action-view-note') : null;
} }
static delay(time) { static delay(time) {