From 4eeb879fb88402503f6ee849fa71e29ab5047b7c Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Wed, 6 May 2020 19:36:42 -0400 Subject: [PATCH] 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') --- ext/bg/js/search-query-parser.js | 35 ++++++++++++++++---------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/ext/bg/js/search-query-parser.js b/ext/bg/js/search-query-parser.js index 1c89583f..cdface15 100644 --- a/ext/bg/js/search-query-parser.js +++ b/ext/bg/js/search-query-parser.js @@ -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);