store local copy of selected parser

Options don't update early enough even after awaiting
This commit is contained in:
siikamiika 2019-11-13 13:09:23 +02:00
parent 0d6e0edc31
commit 707b039927

View File

@ -24,6 +24,7 @@ class QueryParser {
this.clickScanPrevent = false; this.clickScanPrevent = false;
this.parseResults = []; this.parseResults = [];
this.selectedParser = null;
this.queryParser = document.querySelector('#query-parser'); this.queryParser = document.querySelector('#query-parser');
this.queryParserSelect = document.querySelector('#query-parser-select'); this.queryParserSelect = document.querySelector('#query-parser-select');
@ -85,14 +86,15 @@ class QueryParser {
})(); })();
} }
onParserChange(e) { async onParserChange(e) {
const selectedParser = e.target.value; const selectedParser = e.target.value;
this.selectedParser = selectedParser;
apiOptionsSet({parsing: {selectedParser}}, this.search.getOptionsContext()); apiOptionsSet({parsing: {selectedParser}}, this.search.getOptionsContext());
this.renderParseResult(this.getParseResult()); this.renderParseResult(this.getParseResult());
} }
getParseResult() { getParseResult() {
return this.parseResults.find(r => r.id === this.search.options.parsing.selectedParser); return this.parseResults.find(r => r.id === this.selectedParser);
} }
async setText(text) { async setText(text) {
@ -102,8 +104,12 @@ class QueryParser {
this.parseResults = await this.parseText(text); this.parseResults = await this.parseText(text);
if (this.parseResults.length > 0) { if (this.parseResults.length > 0) {
if (this.search.options.parsing.selectedParser === null || !this.getParseResult()) { if (this.selectedParser === null) {
this.selectedParser = this.search.options.parsing.selectedParser;
}
if (this.selectedParser === null || !this.getParseResult()) {
const selectedParser = this.parseResults[0].id; const selectedParser = this.parseResults[0].id;
this.selectedParser = selectedParser;
apiOptionsSet({parsing: {selectedParser}}, this.search.getOptionsContext()); apiOptionsSet({parsing: {selectedParser}}, this.search.getOptionsContext());
} }
} }
@ -162,7 +168,7 @@ class QueryParser {
const option = document.createElement('option'); const option = document.createElement('option');
option.value = parseResult.id; option.value = parseResult.id;
option.innerText = parseResult.name; option.innerText = parseResult.name;
option.defaultSelected = this.search.options.parsing.selectedParser === parseResult.id; option.defaultSelected = this.selectedParser === parseResult.id;
select.appendChild(option); select.appendChild(option);
} }
select.addEventListener('change', this.onParserChange.bind(this)); select.addEventListener('change', this.onParserChange.bind(this));