aboutsummaryrefslogtreecommitdiff
path: root/ext/bg/js/search-query-parser.js
diff options
context:
space:
mode:
Diffstat (limited to 'ext/bg/js/search-query-parser.js')
-rw-r--r--ext/bg/js/search-query-parser.js174
1 files changed, 83 insertions, 91 deletions
diff --git a/ext/bg/js/search-query-parser.js b/ext/bg/js/search-query-parser.js
index eb3b681c..e1e37d1c 100644
--- a/ext/bg/js/search-query-parser.js
+++ b/ext/bg/js/search-query-parser.js
@@ -18,56 +18,76 @@
/* global
* QueryParserGenerator
* TextScanner
- * apiOptionsSet
+ * apiModifySettings
* apiTermsFind
* apiTextParse
* 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;
+ this._parseResults = [];
+ this._queryParser = document.querySelector('#query-parser-content');
+ this._queryParserSelect = document.querySelector('#query-parser-select-container');
+ this._queryParserGenerator = new QueryParserGenerator();
+ this._textScanner = new TextScanner({
+ node: this._queryParser,
+ ignoreElements: () => [],
+ ignorePoint: null,
+ search: this._search.bind(this)
+ });
+ }
- this.getOptionsContext = getOptionsContext;
- this.setContent = setContent;
- this.setSpinnerVisible = setSpinnerVisible;
+ async prepare() {
+ await this._queryParserGenerator.prepare();
+ this._queryParser.addEventListener('click', this._onClick.bind(this));
+ }
- this.parseResults = [];
+ setOptions(options) {
+ this._options = options;
+ this._textScanner.setOptions(options);
+ this._textScanner.setEnabled(true);
+ this._queryParser.dataset.termSpacing = `${options.parsing.termSpacing}`;
+ }
- this.queryParser = document.querySelector('#query-parser-content');
- this.queryParserSelect = document.querySelector('#query-parser-select-container');
+ async setText(text) {
+ this._setSpinnerVisible(true);
- this.queryParserGenerator = new QueryParserGenerator();
- }
+ this._setPreview(text);
- async prepare() {
- await this.queryParserGenerator.prepare();
- }
+ this._parseResults = await apiTextParse(text, this._getOptionsContext());
+ this._refreshSelectedParser();
+
+ this._renderParserSelect();
+ this._renderParseResult();
- onError(error) {
- logError(error, false);
+ this._setSpinnerVisible(false);
}
- onClick(e) {
- super.onClick(e);
- this.searchAt(e.clientX, e.clientY, 'click');
+ // Private
+
+ _onClick(e) {
+ this._textScanner.searchAt(e.clientX, e.clientY, 'click');
}
- async onSearchSource(textSource, cause) {
+ async _search(textSource, cause) {
if (textSource === null) { return null; }
- this.setTextSourceScanLength(textSource, this.options.scanning.length);
- const searchText = textSource.text();
- if (searchText.length === 0) { return; }
+ const searchText = this._textScanner.getTextSourceContent(textSource, this._options.scanning.length);
+ if (searchText.length === 0) { return null; }
- const {definitions, length} = await apiTermsFind(searchText, {}, this.getOptionsContext());
+ 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);
- this.setContent('terms', {definitions, context: {
+ this._setContent('terms', {definitions, context: {
focus: false,
disableHistory: cause === 'mouse',
sentence,
@@ -77,89 +97,61 @@ class QueryParser extends TextScanner {
return {definitions, type: 'terms'};
}
- onParserChange(e) {
- const selectedParser = e.target.value;
- apiOptionsSet({parsing: {selectedParser}}, this.getOptionsContext());
- }
-
- getMouseEventListeners() {
- return [
- [this.node, 'click', this.onClick.bind(this)],
- [this.node, 'mousedown', this.onMouseDown.bind(this)],
- [this.node, 'mousemove', this.onMouseMove.bind(this)],
- [this.node, 'mouseover', this.onMouseOver.bind(this)],
- [this.node, 'mouseout', this.onMouseOut.bind(this)]
- ];
+ _onParserChange(e) {
+ const value = e.target.value;
+ apiModifySettings([{
+ action: 'set',
+ path: 'parsing.selectedParser',
+ value,
+ scope: 'profile',
+ optionsContext: this._getOptionsContext()
+ }], 'search');
}
- getTouchEventListeners() {
- return [
- [this.node, 'auxclick', this.onAuxClick.bind(this)],
- [this.node, 'touchstart', this.onTouchStart.bind(this)],
- [this.node, 'touchend', this.onTouchEnd.bind(this)],
- [this.node, 'touchcancel', this.onTouchCancel.bind(this)],
- [this.node, 'touchmove', this.onTouchMove.bind(this), {passive: false}],
- [this.node, 'contextmenu', this.onContextMenu.bind(this)]
- ];
- }
-
- setOptions(options) {
- super.setOptions(options);
- this.queryParser.dataset.termSpacing = `${options.parsing.termSpacing}`;
- }
-
- refreshSelectedParser() {
- if (this.parseResults.length > 0) {
- if (!this.getParseResult()) {
- const selectedParser = this.parseResults[0].id;
- apiOptionsSet({parsing: {selectedParser}}, this.getOptionsContext());
+ _refreshSelectedParser() {
+ if (this._parseResults.length > 0) {
+ if (!this._getParseResult()) {
+ const value = this._parseResults[0].id;
+ apiModifySettings([{
+ action: 'set',
+ path: 'parsing.selectedParser',
+ value,
+ scope: 'profile',
+ optionsContext: this._getOptionsContext()
+ }], 'search');
}
}
}
- getParseResult() {
- const {selectedParser} = this.options.parsing;
- return this.parseResults.find((r) => r.id === selectedParser);
- }
-
- async setText(text) {
- this.setSpinnerVisible(true);
-
- this.setPreview(text);
-
- this.parseResults = await apiTextParse(text, this.getOptionsContext());
- this.refreshSelectedParser();
-
- this.renderParserSelect();
- this.renderParseResult();
-
- this.setSpinnerVisible(false);
+ _getParseResult() {
+ const {selectedParser} = this._options.parsing;
+ return this._parseResults.find((r) => r.id === selectedParser);
}
- setPreview(text) {
+ _setPreview(text) {
const previewTerms = [];
for (let i = 0, ii = text.length; i < ii; i += 2) {
const tempText = text.substring(i, i + 2);
previewTerms.push([{text: tempText, reading: ''}]);
}
- this.queryParser.textContent = '';
- this.queryParser.appendChild(this.queryParserGenerator.createParseResult(previewTerms, true));
+ this._queryParser.textContent = '';
+ this._queryParser.appendChild(this._queryParserGenerator.createParseResult(previewTerms, true));
}
- renderParserSelect() {
- this.queryParserSelect.textContent = '';
- if (this.parseResults.length > 1) {
- const {selectedParser} = this.options.parsing;
- const select = this.queryParserGenerator.createParserSelect(this.parseResults, selectedParser);
- select.addEventListener('change', this.onParserChange.bind(this));
- this.queryParserSelect.appendChild(select);
+ _renderParserSelect() {
+ this._queryParserSelect.textContent = '';
+ if (this._parseResults.length > 1) {
+ const {selectedParser} = this._options.parsing;
+ const select = this._queryParserGenerator.createParserSelect(this._parseResults, selectedParser);
+ select.addEventListener('change', this._onParserChange.bind(this));
+ this._queryParserSelect.appendChild(select);
}
}
- renderParseResult() {
- const parseResult = this.getParseResult();
- this.queryParser.textContent = '';
+ _renderParseResult() {
+ const parseResult = this._getParseResult();
+ this._queryParser.textContent = '';
if (!parseResult) { return; }
- this.queryParser.appendChild(this.queryParserGenerator.createParseResult(parseResult.content));
+ this._queryParser.appendChild(this._queryParserGenerator.createParseResult(parseResult.content));
}
}