Remove search query parser inheritance (#509)

* Remove getMouseEventListeners override

* Use this._options instead of this.options

* Make QueryParser use composition instead of inheritance for TextScanner

* Remove repeat call to document.querySelector('#query-parser-content')
This commit is contained in:
toasted-nutbread 2020-05-06 19:36:42 -04:00 committed by GitHub
parent 38d6f58fac
commit 4eeb879fb8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -24,10 +24,9 @@
* docSentenceExtract
*/
class QueryParser extends TextScanner {
class QueryParser {
constructor({getOptionsContext, setContent, setSpinnerVisible}) {
super(document.querySelector('#query-parser-content'), () => [], []);
this._options = null;
this.getOptionsContext = getOptionsContext;
this.setContent = setContent;
this.setSpinnerVisible = setSpinnerVisible;
@ -38,26 +37,34 @@ class QueryParser extends TextScanner {
this.queryParserSelect = document.querySelector('#query-parser-select-container');
this.queryParserGenerator = new QueryParserGenerator();
this._textScanner = new TextScanner(
this.queryParser,
() => [],
[]
);
this._textScanner.onSearchSource = this.onSearchSource.bind(this);
}
async prepare() {
await this.queryParserGenerator.prepare();
this.queryParser.addEventListener('click', this.onClick2.bind(this));
}
onClick2(e) {
this.searchAt(e.clientX, e.clientY, 'click');
this._textScanner.searchAt(e.clientX, e.clientY, 'click');
}
async onSearchSource(textSource, cause) {
if (textSource === null) { return null; }
const searchText = this.getTextSourceContent(textSource, this.options.scanning.length);
const searchText = this._textScanner.getTextSourceContent(textSource, this._options.scanning.length);
if (searchText.length === 0) { return; }
const {definitions, length} = await apiTermsFind(searchText, {}, this.getOptionsContext());
if (definitions.length === 0) { return null; }
const sentence = docSentenceExtract(textSource, this.options.anki.sentenceExt);
const sentence = docSentenceExtract(textSource, this._options.anki.sentenceExt);
textSource.setEndOffset(length);
@ -82,16 +89,10 @@ class QueryParser extends TextScanner {
}], 'search');
}
getMouseEventListeners() {
return [
...super.getMouseEventListeners(),
[this.node, 'click', this.onClick2.bind(this)]
];
}
setOptions(options) {
super.setOptions(options);
super.setEnabled(true);
this._options = options;
this._textScanner.setOptions(options);
this._textScanner.setEnabled(true);
this.queryParser.dataset.termSpacing = `${options.parsing.termSpacing}`;
}
@ -111,7 +112,7 @@ class QueryParser extends TextScanner {
}
getParseResult() {
const {selectedParser} = this.options.parsing;
const {selectedParser} = this._options.parsing;
return this.parseResults.find((r) => r.id === selectedParser);
}
@ -142,7 +143,7 @@ class QueryParser extends TextScanner {
renderParserSelect() {
this.queryParserSelect.textContent = '';
if (this.parseResults.length > 1) {
const {selectedParser} = this.options.parsing;
const {selectedParser} = this._options.parsing;
const select = this.queryParserGenerator.createParserSelect(this.parseResults, selectedParser);
select.addEventListener('change', this.onParserChange.bind(this));
this.queryParserSelect.appendChild(select);