move Display context to a new class
This commit is contained in:
parent
be23ad7213
commit
5929018fac
@ -220,6 +220,7 @@ class DisplaySearch extends Display {
|
||||
const {definitions} = await apiTermsFind(query, details, this.optionsContext);
|
||||
this.setContentTerms(definitions, {
|
||||
focus: false,
|
||||
disableHistory: true,
|
||||
sentence: {text: query, offset: 0},
|
||||
url: window.location.href
|
||||
});
|
||||
|
@ -71,6 +71,7 @@
|
||||
<script src="/fg/js/source.js"></script>
|
||||
<script src="/fg/js/util.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/japanese.js"></script>
|
||||
<script src="/mixed/js/scroll.js"></script>
|
||||
|
@ -39,6 +39,7 @@
|
||||
<script src="/fg/js/document.js"></script>
|
||||
<script src="/fg/js/source.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/scroll.js"></script>
|
||||
|
||||
|
55
ext/mixed/js/display-context.js
Normal file
55
ext/mixed/js/display-context.js
Normal 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;
|
||||
}
|
||||
}
|
@ -64,27 +64,17 @@ class Display {
|
||||
try {
|
||||
e.preventDefault();
|
||||
if (!this.context) { return; }
|
||||
this.context.details.context.next = null;
|
||||
|
||||
const link = e.target;
|
||||
const {type, details} = this.context;
|
||||
const context = {
|
||||
source: {
|
||||
type,
|
||||
details: {
|
||||
definitions: this.definitions,
|
||||
context: Object.assign({}, details.context, {
|
||||
this.context.update({
|
||||
index: this.entryIndexFind(link),
|
||||
scroll: this.windowScroll.y
|
||||
})
|
||||
}
|
||||
},
|
||||
sentence: details.context.sentence,
|
||||
url: details.context.url
|
||||
});
|
||||
const context = {
|
||||
sentence: this.context.get('sentence'),
|
||||
url: this.context.get('url')
|
||||
};
|
||||
|
||||
this.windowScroll.toY(0);
|
||||
|
||||
const definitions = await apiKanjiFind(link.textContent, this.getOptionsContext());
|
||||
this.setContentKanji(definitions, context);
|
||||
} catch (error) {
|
||||
@ -111,7 +101,7 @@ class Display {
|
||||
async onTermLookup(e, {disableScroll, selectText, disableHistory}={}) {
|
||||
try {
|
||||
if (!this.context) { return; }
|
||||
this.context.details.context.next = null;
|
||||
|
||||
const termLookupResults = await this.termLookup(e);
|
||||
if (!termLookupResults) { return; }
|
||||
const {textSource, definitions} = termLookupResults;
|
||||
@ -119,29 +109,29 @@ class Display {
|
||||
const scannedElement = e.target;
|
||||
const sentence = docSentenceExtract(textSource, this.options.anki.sentenceExt);
|
||||
|
||||
const {type, details} = this.context;
|
||||
const context = {
|
||||
source: disableHistory ? details.context.source : {
|
||||
type,
|
||||
details: {
|
||||
definitions: this.definitions,
|
||||
context: Object.assign({}, details.context, {
|
||||
disableScroll,
|
||||
disableHistory,
|
||||
sentence,
|
||||
url: this.context.get('url')
|
||||
};
|
||||
if (disableHistory) {
|
||||
Object.assign(context, {
|
||||
previous: this.context.previous,
|
||||
next: this.context.next
|
||||
});
|
||||
} else {
|
||||
this.context.update({
|
||||
index: this.entryIndexFind(scannedElement),
|
||||
scroll: this.windowScroll.y
|
||||
})
|
||||
});
|
||||
Object.assign(context, {
|
||||
previous: this.context
|
||||
});
|
||||
}
|
||||
},
|
||||
disableScroll,
|
||||
sentence,
|
||||
url: details.context.url
|
||||
};
|
||||
|
||||
this.setContentTerms(definitions, context);
|
||||
|
||||
if (!disableScroll) {
|
||||
this.windowScroll.toY(0);
|
||||
}
|
||||
|
||||
if (selectText) {
|
||||
textSource.select();
|
||||
}
|
||||
@ -354,16 +344,18 @@ class Display {
|
||||
}
|
||||
|
||||
this.definitions = definitions;
|
||||
this.context = {
|
||||
type: 'terms',
|
||||
details: {definitions, context}
|
||||
};
|
||||
if (context.disableHistory) {
|
||||
delete context.disableHistory;
|
||||
this.context = new DisplayContext('terms', definitions, context);
|
||||
} else {
|
||||
this.context = DisplayContext.push(this.context, 'terms', definitions, context);
|
||||
}
|
||||
|
||||
const sequence = ++this.sequence;
|
||||
const params = {
|
||||
definitions,
|
||||
source: context.source,
|
||||
next: context.next,
|
||||
source: this.context.previous,
|
||||
next: this.context.next,
|
||||
addable: options.anki.enable,
|
||||
grouped: options.general.resultOutputMode === 'group',
|
||||
merged: options.general.resultOutputMode === 'merge',
|
||||
@ -383,6 +375,7 @@ class Display {
|
||||
if (!disableScroll) {
|
||||
this.entryScrollIntoView(index || 0, scroll);
|
||||
} else {
|
||||
delete context.disableScroll;
|
||||
this.entrySetCurrent(index || 0);
|
||||
}
|
||||
|
||||
@ -412,16 +405,18 @@ class Display {
|
||||
}
|
||||
|
||||
this.definitions = definitions;
|
||||
this.context = {
|
||||
type: 'kanji',
|
||||
details: {definitions, context}
|
||||
};
|
||||
if (context.disableHistory) {
|
||||
delete context.disableHistory;
|
||||
this.context = new DisplayContext('kanji', definitions, context);
|
||||
} else {
|
||||
this.context = DisplayContext.push(this.context, 'kanji', definitions, context);
|
||||
}
|
||||
|
||||
const sequence = ++this.sequence;
|
||||
const params = {
|
||||
definitions,
|
||||
source: context.source,
|
||||
next: context.next,
|
||||
source: this.context.previous,
|
||||
next: this.context.next,
|
||||
addable: options.anki.enable,
|
||||
debug: options.general.debugInfo
|
||||
};
|
||||
@ -531,28 +526,33 @@ class Display {
|
||||
}
|
||||
|
||||
sourceTermView() {
|
||||
if (!this.context || !this.context.details.context.source) { return; }
|
||||
const {type, details} = this.context;
|
||||
const sourceContext = details.context.source;
|
||||
sourceContext.details.context.next = {
|
||||
type,
|
||||
details: {
|
||||
definitions: this.definitions,
|
||||
context: Object.assign({}, details.context, {
|
||||
if (!this.context || !this.context.previous) { return; }
|
||||
this.context.update({
|
||||
index: this.index,
|
||||
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() {
|
||||
if (!this.context.details.context.next) { return; }
|
||||
this.context.details.context.index = this.index;
|
||||
this.context.details.context.scroll = this.windowScroll.y;
|
||||
const {type, details} = this.context.details.context.next;
|
||||
this.setContent(type, details);
|
||||
if (!this.context || !this.context.next) { return; }
|
||||
this.context.update({
|
||||
index: this.index,
|
||||
scroll: this.windowScroll.y
|
||||
});
|
||||
const nextContext = this.context.next;
|
||||
nextContext.set('disableHistory', true);
|
||||
const details = {
|
||||
definitions: nextContext.definitions,
|
||||
context: nextContext.context
|
||||
};
|
||||
this.setContent(nextContext.type, details);
|
||||
}
|
||||
|
||||
noteTryAdd(mode) {
|
||||
|
Loading…
Reference in New Issue
Block a user