move Display context to a new class

This commit is contained in:
siikamiika 2019-12-01 05:38:23 +02:00
parent be23ad7213
commit 5929018fac
5 changed files with 121 additions and 63 deletions

View File

@ -220,6 +220,7 @@ class DisplaySearch extends Display {
const {definitions} = await apiTermsFind(query, details, this.optionsContext); const {definitions} = await apiTermsFind(query, details, this.optionsContext);
this.setContentTerms(definitions, { this.setContentTerms(definitions, {
focus: false, focus: false,
disableHistory: true,
sentence: {text: query, offset: 0}, sentence: {text: query, offset: 0},
url: window.location.href url: window.location.href
}); });

View File

@ -71,6 +71,7 @@
<script src="/fg/js/source.js"></script> <script src="/fg/js/source.js"></script>
<script src="/fg/js/util.js"></script> <script src="/fg/js/util.js"></script>
<script src="/mixed/js/audio.js"></script> <script src="/mixed/js/audio.js"></script>
<script src="/mixed/js/display-context.js"></script>
<script src="/mixed/js/display.js"></script> <script src="/mixed/js/display.js"></script>
<script src="/mixed/js/japanese.js"></script> <script src="/mixed/js/japanese.js"></script>
<script src="/mixed/js/scroll.js"></script> <script src="/mixed/js/scroll.js"></script>

View File

@ -39,6 +39,7 @@
<script src="/fg/js/document.js"></script> <script src="/fg/js/document.js"></script>
<script src="/fg/js/source.js"></script> <script src="/fg/js/source.js"></script>
<script src="/mixed/js/audio.js"></script> <script src="/mixed/js/audio.js"></script>
<script src="/mixed/js/display-context.js"></script>
<script src="/mixed/js/display.js"></script> <script src="/mixed/js/display.js"></script>
<script src="/mixed/js/scroll.js"></script> <script src="/mixed/js/scroll.js"></script>

View File

@ -0,0 +1,55 @@
/*
* Copyright (C) 2019 Alex Yatskov <alex@foosoft.net>
* Author: Alex Yatskov <alex@foosoft.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class DisplayContext {
constructor(type, definitions, context) {
this.type = type;
this.definitions = definitions;
this.context = context;
}
get(key) {
return this.context[key];
}
set(key, value) {
this.context[key] = value;
}
update(data) {
Object.assign(this.context, data);
}
get previous() {
return this.context.previous;
}
get next() {
return this.context.next;
}
static push(self, type, definitions, context) {
const newContext = new DisplayContext(type, definitions, context);
if (self !== null) {
newContext.update({previous: self});
self.update({next: newContext});
}
return newContext;
}
}

View File

@ -64,27 +64,17 @@ class Display {
try { try {
e.preventDefault(); e.preventDefault();
if (!this.context) { return; } if (!this.context) { return; }
this.context.details.context.next = null;
const link = e.target; const link = e.target;
const {type, details} = this.context; this.context.update({
const context = {
source: {
type,
details: {
definitions: this.definitions,
context: Object.assign({}, details.context, {
index: this.entryIndexFind(link), index: this.entryIndexFind(link),
scroll: this.windowScroll.y scroll: this.windowScroll.y
}) });
} const context = {
}, sentence: this.context.get('sentence'),
sentence: details.context.sentence, url: this.context.get('url')
url: details.context.url
}; };
this.windowScroll.toY(0);
const definitions = await apiKanjiFind(link.textContent, this.getOptionsContext()); const definitions = await apiKanjiFind(link.textContent, this.getOptionsContext());
this.setContentKanji(definitions, context); this.setContentKanji(definitions, context);
} catch (error) { } catch (error) {
@ -111,7 +101,7 @@ class Display {
async onTermLookup(e, {disableScroll, selectText, disableHistory}={}) { async onTermLookup(e, {disableScroll, selectText, disableHistory}={}) {
try { try {
if (!this.context) { return; } if (!this.context) { return; }
this.context.details.context.next = null;
const termLookupResults = await this.termLookup(e); const termLookupResults = await this.termLookup(e);
if (!termLookupResults) { return; } if (!termLookupResults) { return; }
const {textSource, definitions} = termLookupResults; const {textSource, definitions} = termLookupResults;
@ -119,29 +109,29 @@ class Display {
const scannedElement = e.target; const scannedElement = e.target;
const sentence = docSentenceExtract(textSource, this.options.anki.sentenceExt); const sentence = docSentenceExtract(textSource, this.options.anki.sentenceExt);
const {type, details} = this.context;
const context = { const context = {
source: disableHistory ? details.context.source : { disableScroll,
type, disableHistory,
details: { sentence,
definitions: this.definitions, url: this.context.get('url')
context: Object.assign({}, details.context, { };
if (disableHistory) {
Object.assign(context, {
previous: this.context.previous,
next: this.context.next
});
} else {
this.context.update({
index: this.entryIndexFind(scannedElement), index: this.entryIndexFind(scannedElement),
scroll: this.windowScroll.y scroll: this.windowScroll.y
}) });
Object.assign(context, {
previous: this.context
});
} }
},
disableScroll,
sentence,
url: details.context.url
};
this.setContentTerms(definitions, context); this.setContentTerms(definitions, context);
if (!disableScroll) {
this.windowScroll.toY(0);
}
if (selectText) { if (selectText) {
textSource.select(); textSource.select();
} }
@ -354,16 +344,18 @@ class Display {
} }
this.definitions = definitions; this.definitions = definitions;
this.context = { if (context.disableHistory) {
type: 'terms', delete context.disableHistory;
details: {definitions, context} this.context = new DisplayContext('terms', definitions, context);
}; } else {
this.context = DisplayContext.push(this.context, 'terms', definitions, context);
}
const sequence = ++this.sequence; const sequence = ++this.sequence;
const params = { const params = {
definitions, definitions,
source: context.source, source: this.context.previous,
next: context.next, next: this.context.next,
addable: options.anki.enable, addable: options.anki.enable,
grouped: options.general.resultOutputMode === 'group', grouped: options.general.resultOutputMode === 'group',
merged: options.general.resultOutputMode === 'merge', merged: options.general.resultOutputMode === 'merge',
@ -383,6 +375,7 @@ class Display {
if (!disableScroll) { if (!disableScroll) {
this.entryScrollIntoView(index || 0, scroll); this.entryScrollIntoView(index || 0, scroll);
} else { } else {
delete context.disableScroll;
this.entrySetCurrent(index || 0); this.entrySetCurrent(index || 0);
} }
@ -412,16 +405,18 @@ class Display {
} }
this.definitions = definitions; this.definitions = definitions;
this.context = { if (context.disableHistory) {
type: 'kanji', delete context.disableHistory;
details: {definitions, context} this.context = new DisplayContext('kanji', definitions, context);
}; } else {
this.context = DisplayContext.push(this.context, 'kanji', definitions, context);
}
const sequence = ++this.sequence; const sequence = ++this.sequence;
const params = { const params = {
definitions, definitions,
source: context.source, source: this.context.previous,
next: context.next, next: this.context.next,
addable: options.anki.enable, addable: options.anki.enable,
debug: options.general.debugInfo debug: options.general.debugInfo
}; };
@ -531,28 +526,33 @@ class Display {
} }
sourceTermView() { sourceTermView() {
if (!this.context || !this.context.details.context.source) { return; } if (!this.context || !this.context.previous) { return; }
const {type, details} = this.context; this.context.update({
const sourceContext = details.context.source;
sourceContext.details.context.next = {
type,
details: {
definitions: this.definitions,
context: Object.assign({}, details.context, {
index: this.index, index: this.index,
scroll: this.windowScroll.y scroll: this.windowScroll.y
}) });
} const previousContext = this.context.previous;
previousContext.set('disableHistory', true);
const details = {
definitions: previousContext.definitions,
context: previousContext.context
}; };
this.setContent(sourceContext.type, sourceContext.details); this.setContent(previousContext.type, details);
} }
nextTermView() { nextTermView() {
if (!this.context.details.context.next) { return; } if (!this.context || !this.context.next) { return; }
this.context.details.context.index = this.index; this.context.update({
this.context.details.context.scroll = this.windowScroll.y; index: this.index,
const {type, details} = this.context.details.context.next; scroll: this.windowScroll.y
this.setContent(type, details); });
const nextContext = this.context.next;
nextContext.set('disableHistory', true);
const details = {
definitions: nextContext.definitions,
context: nextContext.context
};
this.setContent(nextContext.type, details);
} }
noteTryAdd(mode) { noteTryAdd(mode) {